Files
Multi_Swarm_Coevolutive/src/multi_swarm/protocol/grammar.py
T
Adriano 44eb6436c1 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>
2026-05-10 21:17:26 +02:00

28 lines
1.0 KiB
Python

from __future__ import annotations
# Grammatica JSON Schema (Phase 1, post S-expression refactor).
#
# Distinzione strutturale:
# * Nodi OPERATORE -> dict con chiave ``"op"`` (logici, comparatori, crossover)
# * Nodi LEAF -> dict con chiave ``"kind"`` (indicator, feature, literal)
# ``op`` e ``kind`` sono mutuamente esclusivi sullo stesso nodo.
LOGICAL_OPS: frozenset[str] = frozenset({"and", "or", "not"})
COMPARATOR_OPS: frozenset[str] = frozenset({"gt", "lt", "eq"})
CROSSOVER_OPS: frozenset[str] = frozenset({"crossover", "crossunder"})
ACTION_VALUES: frozenset[str] = frozenset(
{"entry-long", "entry-short", "exit", "flat"}
)
KIND_VALUES: frozenset[str] = frozenset({"indicator", "feature", "literal"})
KNOWN_INDICATORS: frozenset[str] = frozenset(
{"sma", "rsi", "atr", "macd", "realized_vol"}
)
KNOWN_FEATURES: frozenset[str] = frozenset(
{"open", "high", "low", "close", "volume"}
)
# Convenience union (utile a validator / parser).
ALL_OPS: frozenset[str] = LOGICAL_OPS | COMPARATOR_OPS | CROSSOVER_OPS