refactor(protocol): swap S-expression grammar for strict JSON Schema
Sostituisce la grammatica S-expression con uno schema JSON stretto. La grammatica S-expression falliva il parsing nel 64% delle generazioni del modello Qwen3-235B sul run reale; JSON e' nativo per gli LLM moderni e si parsa con json.loads. Cambiamenti principali: - grammar.py: costanti rinominate LOGICAL_OPS / COMPARATOR_OPS / CROSSOVER_OPS / ACTION_VALUES / KIND_VALUES. - parser.py: nuovo AST a dataclass tipizzato (OpNode, IndicatorNode, FeatureNode, LiteralNode, Rule, Strategy); parse_strategy ora consuma JSON tramite json.loads. - validator.py: walk dispatchato per tipo (isinstance) invece di pattern-matching su 'kind'; arity check su operatori e indicator. - compiler.py: traversal del nuovo AST tipizzato, dispatch per isinstance; logica indicator/feature/literal invariata. - hypothesis.py: prompt SYSTEM riscritto con esempi JSON e vincoli espliciti su no-nesting; estrazione via fence ```json``` + fallback brace-balanced. - __init__.py: re-export pubblico delle entita' del protocollo. - Tutti i test (parser, validator, compiler, hypothesis_agent, falsification, adversarial, e2e, smoke_run) migrati a JSON. - Rimossa dipendenza sexpdata da pyproject.toml + uv.lock. Test: 135 passed (era 122; aggiunti casi parser/validator). ruff + mypy strict clean. Smoke run end-to-end OK. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
+29
-7
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
import numpy as np
|
||||
@@ -9,19 +10,40 @@ from multi_swarm.genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
from multi_swarm.llm.client import CompletionResult
|
||||
from multi_swarm.orchestrator.run import RunConfig, run_phase1
|
||||
|
||||
_MOCK_STRATEGY = json.dumps(
|
||||
{
|
||||
"rules": [
|
||||
{
|
||||
"condition": {
|
||||
"op": "gt",
|
||||
"args": [
|
||||
{"kind": "indicator", "name": "rsi", "params": [14]},
|
||||
{"kind": "literal", "value": 70.0},
|
||||
],
|
||||
},
|
||||
"action": "entry-short",
|
||||
},
|
||||
{
|
||||
"condition": {
|
||||
"op": "lt",
|
||||
"args": [
|
||||
{"kind": "indicator", "name": "rsi", "params": [14]},
|
||||
{"kind": "literal", "value": 30.0},
|
||||
],
|
||||
},
|
||||
"action": "entry-long",
|
||||
},
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
class MockLLMClient:
|
||||
def complete(
|
||||
self, genome: HypothesisAgentGenome, system: str, user: str,
|
||||
max_tokens: int = 2000,
|
||||
) -> CompletionResult:
|
||||
text = (
|
||||
"```lisp\n"
|
||||
"(strategy"
|
||||
" (when (gt (indicator rsi 14) 70.0) (entry-short))"
|
||||
" (when (lt (indicator rsi 14) 30.0) (entry-long)))\n"
|
||||
"```"
|
||||
)
|
||||
text = "```json\n" + _MOCK_STRATEGY + "\n```"
|
||||
return CompletionResult(
|
||||
text=text, input_tokens=120, output_tokens=60,
|
||||
tier=genome.model_tier, model="mock",
|
||||
|
||||
Reference in New Issue
Block a user