"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
feat(grounding): record fired check names on each validation attempt
Yufeng Heb8d33bf44bf3
diff --git a/agent/src/agent/grounding/ledger.py b/agent/src/agent/grounding/ledger.pyindex 691e91c02..3c8948fd0 100644--- a/agent/src/agent/grounding/ledger.py+++ b/agent/src/agent/grounding/ledger.py@@ -21,6 +21,7 @@ from src.agent.grounding.identity import (from src.agent.grounding.evidence import EvidenceRecord, _EvidenceMixin, _json_objectfrom src.agent.grounding.figures import parse_figures_block, scan_figures, strip_figures_blockfrom src.agent.grounding.policies import ValidationResult, _PolicyMixin+from src.agent.grounding.registry import GROUNDING_CHECKSfrom src.agent.grounding.release import (MAX_GROUNDING_RECOVERY_ROUNDS,MAX_PRICE_EVIDENCE_ATTEMPTS,@@ -290,8 +291,9 @@ class GroundingLedger(content: Candidate assistant answer.Returns:- A deterministic validation result. A record containing only the- answer hash and structured issues is appended to the artifact.+ A deterministic validation result. A record containing the answer+ hash, structured issues, and the names of the declared checks that+ fired is appended to the artifact."""return self._validate(content, record=True)@@ -348,6 +350,11 @@ class GroundingLedger("content_sha256": hashlib.sha256(content.encode("utf-8")).hexdigest(),"valid": result.valid,"issues": issues,+ # Names let a run card say which declared checks fired without+ # re-running the gate; inline rules carry codes only until migrated.+ "fired_checks": GROUNDING_CHECKS.names_for_codes(+ {str(issue.get("code") or "") for issue in issues}+ ),"figures_block": block.raw,})diff --git a/agent/src/agent/grounding/registry.py b/agent/src/agent/grounding/registry.pyindex 30e91898b..e738cbc76 100644--- a/agent/src/agent/grounding/registry.py+++ b/agent/src/agent/grounding/registry.py@@ -67,6 +67,14 @@ class GroundingRegistry:"""Every check with its issues, in registration order."""return [(check, check.predicate(ledger, content)) for check in self._checks.values()]+ def names_for_codes(self, codes: set[str]) -> list[str]:+ """Names of registered checks emitting any of ``codes``, in registration order.++ Codes from rules that still run inline resolve to no name; they get one+ when the check is migrated.+ """+ return [check.name for check in self._checks.values() if check.code in codes]+def describe(self) -> list[dict[str, str]]:"""The check catalog for run cards and docs, in registration order."""return [{"name": c.name, "code": c.code, "description": c.description} for c in self._checks.values()]diff --git a/agent/tests/test_grounding_registry.py b/agent/tests/test_grounding_registry.pyindex 51c5b69f6..df58069b7 100644--- a/agent/tests/test_grounding_registry.py+++ b/agent/tests/test_grounding_registry.py@@ -77,3 +77,40 @@ def test_migrated_check_fires_identically_through_the_gate(tmp_path: Path) -> Nodef test_migrated_check_is_silent_with_nothing_locked(tmp_path: Path) -> None:ledger = GroundingLedger(run_dir=tmp_path, user_message="随便聊聊")assert GROUNDING_CHECKS.run("listed-identity-relabelled-private", ledger, "这是一家私人公司。") == []+++def test_names_for_codes_maps_back_in_registration_order() -> None:+ predicate = lambda ledger, content: [] # noqa: E731+ registry = GroundingRegistry()+ registry.register(GroundingCheck(name="b-check", code="shared_code", description="d", predicate=predicate))+ registry.register(GroundingCheck(name="a-check", code="other_code", description="d", predicate=predicate))+ registry.register(GroundingCheck(name="c-check", code="shared_code", description="d", predicate=predicate))+ assert registry.names_for_codes({"shared_code"}) == ["b-check", "c-check"]+ assert registry.names_for_codes({"shared_code", "other_code"}) == ["b-check", "a-check", "c-check"]+ assert registry.names_for_codes({"never_emitted"}) == []+++def _recorded_validations(tmp_path: Path) -> list[dict[str, object]]:+ artifact = json.loads(+ (tmp_path / "artifacts" / "grounding_evidence.json").read_text(encoding="utf-8")+ )+ return artifact["validations"]+++def test_fired_checks_name_the_declared_check_that_fired(tmp_path: Path) -> None:+ ledger = _ledger_with_locked_listed_identity(tmp_path)+ ledger.validate_final_answer("GLD 仍是一家私人公司,建议观望。")+ assert _recorded_validations(tmp_path)[-1]["fired_checks"] == ["listed-identity-relabelled-private"]+++def test_fired_checks_stay_empty_when_only_inline_rules_fire(tmp_path: Path) -> None:+ ledger = _ledger_with_locked_listed_identity(tmp_path)+ result = ledger.validate_final_answer("GLD 现价 999.99 美元,建议买入。")+ assert result.issues+ assert _recorded_validations(tmp_path)[-1]["fired_checks"] == []+++def test_fired_checks_empty_for_a_clean_answer(tmp_path: Path) -> None:+ ledger = _ledger_with_locked_listed_identity(tmp_path)+ ledger.validate_final_answer("GLD 是上市 ETF,维持观察。")+ assert _recorded_validations(tmp_path)[-1]["fired_checks"] == []