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:
@@ -1,3 +1,5 @@
|
||||
import json
|
||||
|
||||
from multi_swarm.agents.hypothesis import HypothesisAgent, MarketSummary
|
||||
from multi_swarm.genome.hypothesis import HypothesisAgentGenome, ModelTier
|
||||
from multi_swarm.llm.client import CompletionResult
|
||||
@@ -16,16 +18,26 @@ def make_summary() -> MarketSummary:
|
||||
)
|
||||
|
||||
|
||||
def test_hypothesis_agent_calls_llm_and_parses(mocker): # type: ignore[no-untyped-def]
|
||||
fake_llm = mocker.MagicMock()
|
||||
fake_llm.complete.return_value = CompletionResult(
|
||||
text="(strategy (when (gt (indicator rsi 14) 70.0) (entry-short)))",
|
||||
input_tokens=200,
|
||||
output_tokens=80,
|
||||
tier=ModelTier.C,
|
||||
model="qwen",
|
||||
)
|
||||
g = HypothesisAgentGenome(
|
||||
VALID_STRATEGY_JSON = json.dumps(
|
||||
{
|
||||
"rules": [
|
||||
{
|
||||
"condition": {
|
||||
"op": "gt",
|
||||
"args": [
|
||||
{"kind": "indicator", "name": "rsi", "params": [14]},
|
||||
{"kind": "literal", "value": 70.0},
|
||||
],
|
||||
},
|
||||
"action": "entry-short",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def make_genome() -> HypothesisAgentGenome:
|
||||
return HypothesisAgentGenome(
|
||||
system_prompt="Pensa come un fisico.",
|
||||
feature_access=["close"],
|
||||
temperature=0.9,
|
||||
@@ -34,10 +46,20 @@ def test_hypothesis_agent_calls_llm_and_parses(mocker): # type: ignore[no-untyp
|
||||
lookback_window=200,
|
||||
cognitive_style="physicist",
|
||||
)
|
||||
|
||||
|
||||
def test_hypothesis_agent_calls_llm_and_parses(mocker): # type: ignore[no-untyped-def]
|
||||
fake_llm = mocker.MagicMock()
|
||||
fake_llm.complete.return_value = CompletionResult(
|
||||
text=VALID_STRATEGY_JSON,
|
||||
input_tokens=200,
|
||||
output_tokens=80,
|
||||
tier=ModelTier.C,
|
||||
model="qwen",
|
||||
)
|
||||
agent = HypothesisAgent(llm=fake_llm)
|
||||
proposal = agent.propose(g, make_summary())
|
||||
proposal = agent.propose(make_genome(), make_summary())
|
||||
assert proposal.strategy is not None
|
||||
assert proposal.raw_text.startswith("(strategy")
|
||||
assert proposal.completion.input_tokens == 200
|
||||
fake_llm.complete.assert_called_once()
|
||||
|
||||
@@ -45,49 +67,64 @@ def test_hypothesis_agent_calls_llm_and_parses(mocker): # type: ignore[no-untyp
|
||||
def test_hypothesis_agent_returns_none_on_parse_error(mocker): # type: ignore[no-untyped-def]
|
||||
fake_llm = mocker.MagicMock()
|
||||
fake_llm.complete.return_value = CompletionResult(
|
||||
text="this is not s-expression",
|
||||
text="this is not JSON",
|
||||
input_tokens=200,
|
||||
output_tokens=80,
|
||||
tier=ModelTier.C,
|
||||
model="qwen",
|
||||
)
|
||||
g = HypothesisAgentGenome(
|
||||
system_prompt="x",
|
||||
feature_access=["close"],
|
||||
temperature=0.9,
|
||||
top_p=0.95,
|
||||
model_tier=ModelTier.C,
|
||||
lookback_window=200,
|
||||
cognitive_style="physicist",
|
||||
)
|
||||
agent = HypothesisAgent(llm=fake_llm)
|
||||
proposal = agent.propose(g, make_summary())
|
||||
proposal = agent.propose(make_genome(), make_summary())
|
||||
assert proposal.strategy is None
|
||||
assert proposal.parse_error is not None
|
||||
|
||||
|
||||
def test_hypothesis_agent_extracts_sexp_from_markdown_fence(mocker): # type: ignore[no-untyped-def]
|
||||
def test_hypothesis_agent_extracts_json_from_markdown_fence(mocker): # type: ignore[no-untyped-def]
|
||||
fenced = (
|
||||
"Ecco la strategia:\n```json\n"
|
||||
+ VALID_STRATEGY_JSON
|
||||
+ "\n```\nFatta."
|
||||
)
|
||||
fake_llm = mocker.MagicMock()
|
||||
fake_llm.complete.return_value = CompletionResult(
|
||||
text=(
|
||||
"Ecco la strategia:\n```lisp\n"
|
||||
"(strategy (when (lt (indicator rsi 14) 30.0) (entry-long)))\n"
|
||||
"```\nFatta."
|
||||
),
|
||||
text=fenced,
|
||||
input_tokens=200,
|
||||
output_tokens=80,
|
||||
tier=ModelTier.C,
|
||||
model="qwen",
|
||||
)
|
||||
g = HypothesisAgentGenome(
|
||||
system_prompt="x",
|
||||
feature_access=["close"],
|
||||
temperature=0.9,
|
||||
top_p=0.95,
|
||||
model_tier=ModelTier.C,
|
||||
lookback_window=200,
|
||||
cognitive_style="physicist",
|
||||
agent = HypothesisAgent(llm=fake_llm)
|
||||
proposal = agent.propose(make_genome(), make_summary())
|
||||
assert proposal.strategy is not None
|
||||
|
||||
|
||||
def test_hypothesis_agent_returns_error_on_invalid_strategy(mocker): # type: ignore[no-untyped-def]
|
||||
bad = json.dumps(
|
||||
{
|
||||
"rules": [
|
||||
{
|
||||
"condition": {
|
||||
"op": "gt",
|
||||
"args": [
|
||||
{"kind": "indicator", "name": "wibble", "params": [14]},
|
||||
{"kind": "literal", "value": 70.0},
|
||||
],
|
||||
},
|
||||
"action": "entry-short",
|
||||
}
|
||||
]
|
||||
}
|
||||
)
|
||||
fake_llm = mocker.MagicMock()
|
||||
fake_llm.complete.return_value = CompletionResult(
|
||||
text=bad,
|
||||
input_tokens=200,
|
||||
output_tokens=80,
|
||||
tier=ModelTier.C,
|
||||
model="qwen",
|
||||
)
|
||||
agent = HypothesisAgent(llm=fake_llm)
|
||||
proposal = agent.propose(g, make_summary())
|
||||
assert proposal.strategy is not None
|
||||
proposal = agent.propose(make_genome(), make_summary())
|
||||
assert proposal.strategy is None
|
||||
assert proposal.parse_error is not None
|
||||
assert "wibble" in proposal.parse_error or "unknown" in proposal.parse_error
|
||||
|
||||
Reference in New Issue
Block a user