"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(loop): check the deterministic cache after the identity gate, not before
Haozhe Wu664b9dc6cab6
diff --git a/agent/src/agent/loop.py b/agent/src/agent/loop.pyindex 35af98c87..3a96477e2 100644--- a/agent/src/agent/loop.py+++ b/agent/src/agent/loop.py@@ -1885,8 +1885,29 @@ class AgentLoop:react_trace.append({"type": "tool_skipped", "tool": tc.name})continue+ if self._grounding is not None:+ authorization = self._grounding.authorize_tool_call(+ tc.name,+ tc.arguments,+ batch_authorized_symbols=batch_authorized_symbols,+ call_id=tc.id,+ batch_identity_status=batch_identity_status,+ )+ if not authorization.allowed:+ execution_plan.append(+ (+ tc,+ authorization.error_payload(+ tc.name,+ self._grounding.identity_summary(),+ ),+ )+ )+ continue+# Deterministic tools (e.g. financial_rigor calc) return the same- # result for the same args. After auto-compact cleared earlier+ # result for the same args. Checked AFTER authorization above so a+ # cached repeat can never bypass the identity gate. After auto-compact cleared earlier# tool outputs, the model used to re-run identical expressions# 5-9x each (2026-08-20 INTC run) to re-verify numbers it could# no longer see. Serve an identical prior call from cache instead.@@ -1915,26 +1936,6 @@ class AgentLoop:)continue- if self._grounding is not None:- authorization = self._grounding.authorize_tool_call(- tc.name,- tc.arguments,- batch_authorized_symbols=batch_authorized_symbols,- call_id=tc.id,- batch_identity_status=batch_identity_status,- )- if not authorization.allowed:- execution_plan.append(- (- tc,- authorization.error_payload(- tc.name,- self._grounding.identity_summary(),- ),- )- )- continue-execution_plan.append((tc, None))if not execution_plan:diff --git a/agent/tests/test_agent_loop_deterministic_cache.py b/agent/tests/test_agent_loop_deterministic_cache.pyindex 18100de6e..9f22b9fd9 100644--- a/agent/tests/test_agent_loop_deterministic_cache.py+++ b/agent/tests/test_agent_loop_deterministic_cache.py@@ -178,3 +178,49 @@ def test_failed_deterministic_call_is_not_cached(agent_factory) -> None:assert len(tool.calls) == 2assert len(messages) == 2assert not any(r["type"] == "tool_result_cached" for r in records)+++def test_cache_hit_still_passes_the_identity_gate(agent_factory) -> None:+ """A cached repeat must not be a way around grounding authorization.++ ``financial_rigor`` carries no symbol arguments, so the gate allows it+ today either way. Any later tool marked deterministic that does carry+ one would otherwise skip identity checks from its second call onward.+ """+ from src.agent.grounding import ToolAuthorization++ tool = _CountingTool(deterministic=True)+ agent, run_dir, _ = agent_factory(tool)++ seen: list[str] = []+ ingested: list[str] = []++ class _Grounding:+ authorized_symbols: set[str] = set()+ identity_status = "locked"++ def authorize_tool_call(self, tool_name, arguments, **kwargs):+ seen.append(kwargs["call_id"])+ if len(seen) == 1:+ return ToolAuthorization(allowed=True)+ return ToolAuthorization(+ allowed=False,+ error_code="identity_required",+ message="blocked",+ )++ def identity_summary(self):+ return {}++ def ingest_tool_result(self, **kwargs):+ ingested.append(kwargs["call_id"])++ agent._grounding = _Grounding()+ messages, records = _drive(agent, tool.name, run_dir, [{"a": 1}, {"a": 1}])++ # Both calls reached the gate; the blocked repeat was not served cached.+ assert seen == ["call_1", "call_2"]+ assert not any(r["type"] == "tool_result_cached" for r in records)+ assert json.loads(messages[1]["content"])["error_code"] == "identity_required"+ # The blocked result is ingested too, so the ledger keeps the refusal.+ assert ingested == ["call_1", "call_2"]