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:
2026-05-10 21:17:26 +02:00
parent df76906505
commit 44eb6436c1
16 changed files with 1082 additions and 392 deletions
+31 -6
View File
@@ -1,3 +1,4 @@
import json
from pathlib import Path
import numpy as np
@@ -26,16 +27,40 @@ def synthetic_ohlcv():
)
_STRATEGY_PAYLOAD = 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",
},
]
}
)
@pytest.fixture
def fake_llm(mocker):
"""LLM mock che ritorna sempre una strategia valida."""
"""LLM mock che ritorna sempre una strategia JSON valida."""
fake = mocker.MagicMock()
fake.complete.return_value = 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" + _STRATEGY_PAYLOAD + "\n```",
input_tokens=200,
output_tokens=80,
tier=ModelTier.C,
+59 -6
View File
@@ -1,3 +1,5 @@
import json
import numpy as np
import pandas as pd
import pytest
@@ -23,7 +25,22 @@ def ohlcv() -> pd.DataFrame:
def test_degenerate_always_long_flagged(ohlcv: pd.DataFrame) -> None:
src = "(strategy (when (gt (feature close) -1e9) (entry-long)))"
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "gt",
"args": [
{"kind": "feature", "name": "close"},
{"kind": "literal", "value": -1e9},
],
},
"action": "entry-long",
}
]
}
)
ast = parse_strategy(src)
agent = AdversarialAgent()
report = agent.review(ast, ohlcv)
@@ -32,10 +49,31 @@ def test_degenerate_always_long_flagged(ohlcv: pd.DataFrame) -> None:
def test_no_findings_on_reasonable_strategy(ohlcv: pd.DataFrame) -> None:
src = (
"(strategy "
"(when (gt (indicator rsi 14) 70.0) (entry-short)) "
"(when (lt (indicator rsi 14) 30.0) (entry-long)))"
src = 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",
},
]
}
)
ast = parse_strategy(src)
agent = AdversarialAgent()
@@ -45,7 +83,22 @@ def test_no_findings_on_reasonable_strategy(ohlcv: pd.DataFrame) -> None:
def test_zero_trade_strategy_flagged(ohlcv: pd.DataFrame) -> None:
src = "(strategy (when (gt (feature close) 1e9) (entry-long)))"
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "gt",
"args": [
{"kind": "feature", "name": "close"},
{"kind": "literal", "value": 1e9},
],
},
"action": "entry-long",
}
]
}
)
ast = parse_strategy(src)
agent = AdversarialAgent()
report = agent.review(ast, ohlcv)
+43 -5
View File
@@ -1,3 +1,5 @@
import json
import numpy as np
import pandas as pd
import pytest
@@ -23,10 +25,31 @@ def trending_ohlcv() -> pd.DataFrame:
def test_falsification_returns_report(trending_ohlcv: pd.DataFrame) -> None:
src = (
"(strategy "
"(when (gt (indicator rsi 14) 70.0) (entry-short)) "
"(when (lt (indicator rsi 14) 30.0) (entry-long)))"
src = 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",
},
]
}
)
ast = parse_strategy(src)
agent = FalsificationAgent(fees_bp=5.0, n_trials_dsr=20)
@@ -40,7 +63,22 @@ def test_falsification_returns_report(trending_ohlcv: pd.DataFrame) -> None:
def test_falsification_zero_trades_returns_zero_metrics(trending_ohlcv: pd.DataFrame) -> None:
src = "(strategy (when (gt (feature close) 1e9) (entry-long)))"
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "gt",
"args": [
{"kind": "feature", "name": "close"},
{"kind": "literal", "value": 1e9},
],
},
"action": "entry-long",
}
]
}
)
ast = parse_strategy(src)
agent = FalsificationAgent(fees_bp=5.0, n_trials_dsr=20)
report = agent.evaluate(ast, trending_ohlcv)
+76 -39
View File
@@ -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
+60 -7
View File
@@ -1,5 +1,7 @@
from __future__ import annotations
import json
import numpy as np
import pandas as pd
import pytest
@@ -26,7 +28,22 @@ def ohlcv() -> pd.DataFrame:
def test_compile_simple_long(ohlcv: pd.DataFrame) -> None:
src = "(strategy (when (lt (indicator rsi 14) 100.0) (entry-long)))"
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "lt",
"args": [
{"kind": "indicator", "name": "rsi", "params": [14]},
{"kind": "literal", "value": 100.0},
],
},
"action": "entry-long",
}
]
}
)
ast = parse_strategy(src)
fn = compile_strategy(ast)
signals = fn(ohlcv)
@@ -35,7 +52,22 @@ def test_compile_simple_long(ohlcv: pd.DataFrame) -> None:
def test_compile_no_match_is_flat(ohlcv: pd.DataFrame) -> None:
src = "(strategy (when (gt (indicator rsi 14) 1000.0) (entry-long)))"
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "gt",
"args": [
{"kind": "indicator", "name": "rsi", "params": [14]},
{"kind": "literal", "value": 1000.0},
],
},
"action": "entry-long",
}
]
}
)
ast = parse_strategy(src)
fn = compile_strategy(ast)
signals = fn(ohlcv)
@@ -43,11 +75,32 @@ def test_compile_no_match_is_flat(ohlcv: pd.DataFrame) -> None:
def test_compile_two_rules_priority(ohlcv: pd.DataFrame) -> None:
src = """
(strategy
(when (gt (feature close) 110.0) (entry-long))
(when (lt (feature close) 105.0) (entry-short)))
"""
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "gt",
"args": [
{"kind": "feature", "name": "close"},
{"kind": "literal", "value": 110.0},
],
},
"action": "entry-long",
},
{
"condition": {
"op": "lt",
"args": [
{"kind": "feature", "name": "close"},
{"kind": "literal", "value": 105.0},
],
},
"action": "entry-short",
},
]
}
)
ast = parse_strategy(src)
fn = compile_strategy(ast)
signals = fn(ohlcv)
+176 -25
View File
@@ -1,47 +1,198 @@
import json
import pytest
from multi_swarm.protocol.grammar import VERBS
from multi_swarm.protocol.parser import ParseError, parse_strategy
from multi_swarm.protocol.grammar import (
ACTION_VALUES,
ALL_OPS,
COMPARATOR_OPS,
CROSSOVER_OPS,
KIND_VALUES,
LOGICAL_OPS,
)
from multi_swarm.protocol.parser import (
FeatureNode,
IndicatorNode,
LiteralNode,
OpNode,
ParseError,
parse_strategy,
)
def test_grammar_has_15_verbs():
assert len(VERBS) == 15
def test_grammar_constant_sets() -> None:
assert LOGICAL_OPS == {"and", "or", "not"}
assert COMPARATOR_OPS == {"gt", "lt", "eq"}
assert CROSSOVER_OPS == {"crossover", "crossunder"}
assert KIND_VALUES == {"indicator", "feature", "literal"}
assert ACTION_VALUES == {"entry-long", "entry-short", "exit", "flat"}
assert ALL_OPS == LOGICAL_OPS | COMPARATOR_OPS | CROSSOVER_OPS
def test_parse_simple_strategy():
src = "(strategy (when (gt (indicator rsi 14) 70.0) (entry-short)))"
def test_parse_simple_strategy() -> None:
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "gt",
"args": [
{"kind": "indicator", "name": "rsi", "params": [14]},
{"kind": "literal", "value": 70.0},
],
},
"action": "entry-short",
}
]
}
)
ast = parse_strategy(src)
assert ast.kind == "strategy"
assert len(ast.rules) == 1
rule = ast.rules[0]
assert rule.kind == "when"
assert rule.condition.kind == "gt"
assert rule.action.kind == "entry-short"
assert rule.action == "entry-short"
assert isinstance(rule.condition, OpNode)
assert rule.condition.op == "gt"
assert isinstance(rule.condition.args[0], IndicatorNode)
assert rule.condition.args[0].name == "rsi"
assert rule.condition.args[0].params == [14.0]
assert isinstance(rule.condition.args[1], LiteralNode)
assert rule.condition.args[1].value == 70.0
def test_parse_multiple_rules():
src = """
(strategy
(when (gt (indicator rsi 14) 70.0) (entry-short))
(when (lt (indicator rsi 14) 30.0) (entry-long)))
"""
def test_parse_multiple_rules() -> None:
src = 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",
},
]
}
)
ast = parse_strategy(src)
assert len(ast.rules) == 2
def test_parse_unknown_verb_raises():
src = "(strategy (when (frobnicate 1 2) (entry-long)))"
with pytest.raises(ParseError):
def test_parse_feature_leaf() -> None:
src = json.dumps(
{
"rules": [
{
"condition": {
"op": "crossover",
"args": [
{"kind": "feature", "name": "close"},
{"kind": "indicator", "name": "sma", "params": [50]},
],
},
"action": "entry-long",
}
]
}
)
ast = parse_strategy(src)
cond = ast.rules[0].condition
assert isinstance(cond, OpNode) and cond.op == "crossover"
assert isinstance(cond.args[0], FeatureNode)
assert cond.args[0].name == "close"
def test_parse_unknown_op_raises() -> None:
src = json.dumps(
{
"rules": [
{
"condition": {"op": "frobnicate", "args": [1, 2]},
"action": "entry-long",
}
]
}
)
with pytest.raises(ParseError, match="Unknown op"):
parse_strategy(src)
def test_parse_malformed_raises():
src = "(strategy (when"
with pytest.raises(ParseError):
def test_parse_invalid_action_raises() -> None:
src = json.dumps(
{
"rules": [
{
"condition": {"kind": "literal", "value": 1.0},
"action": "buy-now",
}
]
}
)
with pytest.raises(ParseError, match="action"):
parse_strategy(src)
def test_parse_empty_strategy_raises():
src = "(strategy)"
with pytest.raises(ParseError):
def test_parse_malformed_json_raises() -> None:
with pytest.raises(ParseError, match="invalid JSON"):
parse_strategy("{this is not json")
def test_parse_top_level_array_raises() -> None:
with pytest.raises(ParseError, match="JSON object"):
parse_strategy("[1, 2, 3]")
def test_parse_missing_rules_key_raises() -> None:
with pytest.raises(ParseError, match="rules"):
parse_strategy(json.dumps({"foo": "bar"}))
def test_parse_empty_rules_raises() -> None:
with pytest.raises(ParseError, match="at least one"):
parse_strategy(json.dumps({"rules": []}))
def test_parse_node_with_both_op_and_kind_raises() -> None:
src = json.dumps(
{
"rules": [
{
"condition": {"op": "gt", "kind": "indicator", "args": []},
"action": "flat",
}
]
}
)
with pytest.raises(ParseError, match="mutually exclusive"):
parse_strategy(src)
def test_parse_indicator_with_nested_node_raises() -> None:
src = json.dumps(
{
"rules": [
{
"condition": {
"kind": "indicator",
"name": "sma",
"params": [{"kind": "literal", "value": 14}],
},
"action": "flat",
}
]
}
)
with pytest.raises(ParseError, match="params"):
parse_strategy(src)
+123 -8
View File
@@ -1,38 +1,153 @@
import json
import pytest
from multi_swarm.protocol.parser import parse_strategy
from multi_swarm.protocol.validator import ValidationError, validate_strategy
def _wrap(condition: dict, action: str = "entry-long") -> str:
return json.dumps({"rules": [{"condition": condition, "action": action}]})
def test_valid_strategy_passes() -> None:
src = "(strategy (when (gt (indicator rsi 14) 70.0) (entry-short)))"
src = _wrap(
{
"op": "gt",
"args": [
{"kind": "indicator", "name": "rsi", "params": [14]},
{"kind": "literal", "value": 70.0},
],
},
action="entry-short",
)
ast = parse_strategy(src)
validate_strategy(ast) # no exception
def test_indicator_unknown_name_fails() -> None:
src = "(strategy (when (gt (indicator wibble 14) 70.0) (entry-short)))"
src = _wrap(
{
"op": "gt",
"args": [
{"kind": "indicator", "name": "wibble", "params": [14]},
{"kind": "literal", "value": 70.0},
],
}
)
ast = parse_strategy(src)
with pytest.raises(ValidationError, match="unknown indicator"):
validate_strategy(ast)
def test_indicator_wrong_arity_fails() -> None:
src = "(strategy (when (gt (indicator rsi) 70.0) (entry-short)))"
def test_indicator_arity_too_few_fails() -> None:
src = _wrap(
{
"op": "gt",
"args": [
{"kind": "indicator", "name": "rsi", "params": []},
{"kind": "literal", "value": 70.0},
],
}
)
ast = parse_strategy(src)
with pytest.raises(ValidationError):
with pytest.raises(ValidationError, match="arity"):
validate_strategy(ast)
def test_indicator_arity_too_many_fails() -> None:
src = _wrap(
{
"op": "gt",
"args": [
{"kind": "indicator", "name": "rsi", "params": [14, 28]},
{"kind": "literal", "value": 70.0},
],
}
)
ast = parse_strategy(src)
with pytest.raises(ValidationError, match="arity"):
validate_strategy(ast)
def test_macd_arity_zero_to_three_ok() -> None:
for params in [[], [12], [12, 26], [12, 26, 9]]:
src = _wrap(
{
"op": "gt",
"args": [
{"kind": "indicator", "name": "macd", "params": params},
{"kind": "literal", "value": 0.0},
],
}
)
ast = parse_strategy(src)
validate_strategy(ast)
def test_macd_arity_four_fails() -> None:
src = _wrap(
{
"op": "gt",
"args": [
{"kind": "indicator", "name": "macd", "params": [1, 2, 3, 4]},
{"kind": "literal", "value": 0.0},
],
}
)
ast = parse_strategy(src)
with pytest.raises(ValidationError, match="arity"):
validate_strategy(ast)
def test_comparator_wrong_arity_fails() -> None:
src = "(strategy (when (gt 1.0) (entry-long)))"
src = _wrap({"op": "gt", "args": [{"kind": "literal", "value": 1.0}]})
ast = parse_strategy(src)
with pytest.raises(ValidationError):
with pytest.raises(ValidationError, match="needs 2 args"):
validate_strategy(ast)
def test_logical_not_arity_fails() -> None:
src = _wrap(
{
"op": "not",
"args": [
{"kind": "literal", "value": 1.0},
{"kind": "literal", "value": 2.0},
],
}
)
ast = parse_strategy(src)
with pytest.raises(ValidationError, match="'not' needs 1"):
validate_strategy(ast)
def test_logical_and_arity_fails() -> None:
src = _wrap({"op": "and", "args": [{"kind": "literal", "value": 1.0}]})
ast = parse_strategy(src)
with pytest.raises(ValidationError, match="and"):
validate_strategy(ast)
def test_crossover_wrong_arity_fails() -> None:
src = _wrap(
{"op": "crossover", "args": [{"kind": "literal", "value": 1.0}]}
)
ast = parse_strategy(src)
with pytest.raises(ValidationError, match="crossover"):
validate_strategy(ast)
def test_feature_unknown_column_fails() -> None:
src = "(strategy (when (gt (feature wibble) 100.0) (entry-long)))"
src = _wrap(
{
"op": "gt",
"args": [
{"kind": "feature", "name": "wibble"},
{"kind": "literal", "value": 100.0},
],
}
)
ast = parse_strategy(src)
with pytest.raises(ValidationError, match="unknown feature"):
validate_strategy(ast)