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