"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
Merge #1349 into #1341 and fix the two defects the combination exposes
Haozhe Wu2b8488da259e
diff --cc agent/src/agent/loop.pyindex f1db1c246,0ae571d32..b6c52e99b--- a/agent/src/agent/loop.py+++ b/agent/src/agent/loop.py@@@ -434,7 -434,7 +434,28 @@@ def _verification_ledger(messages: listbreakreturn "\n".join(unique)- def _microcompact(messages: list) -> None:++# Marker written over a tool result whose payload layer 1 removed. Matched by++# PREFIX because the text carries the original length, so no two cleared++# results are the same string; never compare a content to it with ``==``.++_CLEARED_PREFIX = "[CLEARED FROM CONTEXT:"++++++def _cleared_text(original_len: int) -> str:++ """Build the self-describing placeholder that replaces a pruned result."""++ return (++ f"{_CLEARED_PREFIX} this tool call SUCCEEDED and returned "++ f"{original_len} characters, which were removed to free context "++ "space. This is NOT a tool failure and NOT an empty result. If you "++ "need these values, call the tool again with the same arguments.]"++ )++++++def _is_cleared(content: Any) -> bool:++ """True when ``content`` is a layer-1 cleared-result marker."""++ return isinstance(content, str) and content.startswith(_CLEARED_PREFIX)+++++ def _microcompact(messages: list) -> list:"""Layer 1: silently prune old tool results, keeping the most recent N intact.Args:@@@ -442,21 -448,16 +469,26 @@@"""tool_msgs = [m for m in messages if m.get("role") == "tool"]if len(tool_msgs) <= KEEP_RECENT:- return+ return []+ newly_cleared = []for msg in tool_msgs[:-KEEP_RECENT]:content = msg.get("content", "")-- if isinstance(content, str) and len(content) > 100:- msg["content"] = "[cleared]"++ # Skip a result already cleared: the marker is itself >100 chars, so++ # re-clearing it would rewrite the recorded original size with the++ # MARKER's length ("returned 287 characters") and re-report the tool++ # as newly unreadable on every later pass.++ if isinstance(content, str) and len(content) > 100 and not _is_cleared(content):+ # A bare "[cleared]" is indistinguishable from a tool that+ # returned nothing, so the model reports "no data was retrieved"+ # for data it did receive and this layer then deleted. Say which+ # it is, and say the result is recoverable.- msg["content"] = (- "[CLEARED FROM CONTEXT: this tool call SUCCEEDED and returned "- f"{len(content)} characters, which were removed to free "- "context space. This is NOT a tool failure and NOT an empty "- "result. If you need these values, call the tool again with "- "the same arguments.]"- )++ msg["content"] = _cleared_text(len(content))+ if msg.get("name"):+ newly_cleared.append(msg["name"])- surviving = {m.get("name") for m in tool_msgs if m.get("content") != "[cleared]"}++ # Identified by prefix, not equality: the marker carries the original++ # length, so every cleared result is a different string.++ surviving = {m.get("name") for m in tool_msgs if not _is_cleared(m.get("content"))}+ return sorted(set(newly_cleared) - surviving)def _context_collapse(messages: list) -> None:@@@ -474,7 -475,7 +506,7 @@@content = msg.get("content")if not isinstance(content, str) or len(content) <= COLLAPSE_TEXT_MIN:continue-- if content == "[cleared]":++ if _is_cleared(content):continuehead = content[:COLLAPSE_HEAD]tail = content[-COLLAPSE_TAIL:]@@@ -2521,6 -2501,36 +2553,45 @@@ class AgentLoop"file write is a failure.")+ def _microcompact_and_unblock(self, messages: list, trace: TraceWriter, iteration: int) -> list[str]:+ """Run layer-1 microcompact and re-open the tools it made unreadable.++ The dedup ledger may only point the model at a result that is still in+ context. Once microcompact clears every result a tool produced, the+ "already completed successfully, use the previous result" skip is+ pointing at ``[cleared]``, which is how #1343 deadlocked: the model+ needed the numbers, could not see them, re-called, and was blocked 44+ times.++ Args:+ messages: Message list, mutated in place by the compaction.+ trace: Run trace; a ``microcompact_cleared`` event is written+ whenever the ledger is re-opened, because this layer used to+ act silently and left no evidence for diagnosis.+ iteration: Current ReAct iteration, recorded on the trace event.++ Returns:+ The tool names re-opened, for callers and tests to assert on.+ """+ unreadable_tools = _microcompact(messages)+ if unreadable_tools:- self._called_ok.difference_update(unreadable_tools)++ # The ledger is keyed by (tool name, canonical arguments), so a++ # name cannot be subtracted from it directly: a plain++ # ``difference_update`` of names against tuples removes nothing and++ # the unblock silently becomes a no-op. Drop EVERY argument variant++ # of a cleared tool — microcompact only reports a name once no++ # readable result of any variant survives.++ cleared = set(unreadable_tools)++ self._called_ok = {++ key for key in self._called_ok if key[0] not in cleared++ }+ trace.write({+ "type": "microcompact_cleared",+ "iter": iteration,+ "tools": unreadable_tools,+ })+ return unreadable_tools+def _identical_call_key(self, tool_name: str, arguments: Mapping[str, Any]) -> tuple[str, str] | None:"""Build a stable key identifying a deterministic tool invocation.diff --cc agent/tests/test_loop_helpers.pyindex 05fc2839b,2083f18bc..ba9a64332--- a/agent/tests/test_loop_helpers.py+++ b/agent/tests/test_loop_helpers.py@@@ -610,3 -666,3 +678,44 @@@ def test_stall_timeout_seconds_default_assert _stall_timeout_seconds() == 42.0monkeypatch.delattr(loop_module, "STALL_TIMEOUT_SECONDS", raising=False)assert _stall_timeout_seconds() > 0++++++class TestMicrocompactMarkerIsStable:++ """The cleared-result marker is >100 chars, i.e. longer than the pruning++ threshold itself, so microcompact must not treat it as prunable payload."""++++ def _tool_msgs(self, payload_len: int) -> list:++ msgs = [++ {++ "role": "tool",++ "tool_call_id": "c0",++ "name": "get_fund_flow",++ "content": "x" * payload_len,++ }++ ]++ msgs += [++ {"role": "tool", "tool_call_id": f"p{i}", "name": "pad", "content": "y" * 200}++ for i in range(KEEP_RECENT + 1)++ ]++ return msgs++++ def test_marker_keeps_the_original_payload_length(self) -> None:++ payload_len = 4321++ messages = self._tool_msgs(payload_len)++ _microcompact(messages)++ first = messages[0]["content"]++ assert str(payload_len) in first, first++++ # A second pass must not rewrite the marker with the marker's own++ # length, which would report a fabricated original size to the model.++ _microcompact(messages)++ assert messages[0]["content"] == first++ assert str(payload_len) in messages[0]["content"]++++ def test_second_pass_reports_no_new_unreadable_tools(self) -> None:++ messages = self._tool_msgs(4321)++ assert _microcompact(messages) == ["get_fund_flow"]++ assert _microcompact(messages) == [], (++ "a tool already reported unreadable must not be re-reported, or the "++ "ledger is re-opened and traced on every later iteration"++ )