From df76906505761365a6cbbc3838ce9bd49292fb23 Mon Sep 17 00:00:00 2001 From: AdrianoDev Date: Sun, 10 May 2026 11:35:54 +0200 Subject: [PATCH] fix(protocol): arity check stretto per indicator + reject nested expressions Run reale phase1-real-003 ha rivelato: l'LLM genera occasionalmente "(indicator sma 20 50)" o "(indicator sma (feature close) 20)". Il primo crashava _ind_sma con TypeError. Il secondo passava attraverso il validator ma non era supportato dal compiler. Validator ora: - Aggiunge INDICATOR_ARITY: sma/rsi/atr/realized_vol = 1 arg, macd = 0-3. - Rifiuta esplicitamente Node fra gli args di indicator (no-nesting Phase 1). - Rifiuta arity fuori range con messaggio chiaro. Strategie con questi pattern vengono ora rigettate dal validator come parse_error invece di crashare il run. Test suite resta 122 PASSED. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/multi_swarm/protocol/validator.py | 28 +++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/multi_swarm/protocol/validator.py b/src/multi_swarm/protocol/validator.py index 6802d13..0e7ade7 100644 --- a/src/multi_swarm/protocol/validator.py +++ b/src/multi_swarm/protocol/validator.py @@ -6,6 +6,17 @@ from .parser import Node, Strategy KNOWN_INDICATORS: frozenset[str] = frozenset({"sma", "rsi", "atr", "macd", "realized_vol"}) KNOWN_FEATURES: frozenset[str] = frozenset({"open", "high", "low", "close", "volume"}) +# Numero di parametri numerici accettati dopo il nome dell'indicatore. +# La tupla (min, max) include solo i numeri (gli argomenti di tipo Node sono +# proibiti dal compiler - gli indicatori non sono annidabili in Phase 1). +INDICATOR_ARITY: dict[str, tuple[int, int]] = { + "sma": (1, 1), # length + "rsi": (1, 1), # length + "atr": (1, 1), # length + "realized_vol": (1, 1), # window + "macd": (0, 3), # fast, slow, signal (tutti opzionali con default) +} + class ValidationError(Exception): """Raised when an AST violates Phase 1 protocol semantics.""" @@ -55,12 +66,25 @@ def _validate_node(node: Node, _expect_bool: bool) -> None: return if node.kind == "indicator": - if len(node.args) < 2: - raise ValidationError("'indicator' needs >=2 args (name, length)") + if len(node.args) < 1: + raise ValidationError("'indicator' needs >=1 args (name [, params...])") name_node = node.args[0] ind_name = name_node.kind if isinstance(name_node, Node) else str(name_node) if ind_name not in KNOWN_INDICATORS: raise ValidationError(f"unknown indicator: {ind_name}") + # Gli indicatori non accettano Node come params (no-nesting in Phase 1). + for a in node.args[1:]: + if isinstance(a, Node): + raise ValidationError( + f"indicator '{ind_name}' does not accept nested expressions; " + f"only numeric literals (got node {a.kind})" + ) + n_params = len(node.args) - 1 + min_p, max_p = INDICATOR_ARITY[ind_name] + if not (min_p <= n_params <= max_p): + raise ValidationError( + f"indicator '{ind_name}' arity {n_params} out of [{min_p},{max_p}]" + ) return if node.kind == "feature":