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,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)
|
||||
|
||||
Reference in New Issue
Block a user