Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(loop): size compaction by tool-call arguments, fold cleared-pair payloads

cgycorey224ebdddc6a7
diff --git a/agent/src/agent/loop.py b/agent/src/agent/loop.py
index 923c108cb..d1823563a 100644
--- a/agent/src/agent/loop.py
+++ b/agent/src/agent/loop.py
@@ -471,6 +471,72 @@ def _context_collapse(messages: list) -> None:
trimmed = len(content) - COLLAPSE_HEAD - COLLAPSE_TAIL
msg["content"] = f"{head}\n\n...[{trimmed} chars collapsed]...\n\n{tail}"
+ # Zero-cost relief for oversized tool-call payloads whose paired result
+ # was already compacted away (``[cleared]``): the arguments blob is now
+ # useless to the model (the data it requested is gone), so fold it to a
+ # valid JSON stub. The call id/name survive, so tool pairing and the
+ # model's "I called tool X" memory are intact, and the provider still
+ # receives well-formed ``arguments``. Nothing re-reads historical
+ # arguments for re-dispatch, so this is safe.
+ cleared_ids = {
+ m.get("tool_call_id")
+ for m in messages
+ if m.get("role") == "tool" and m.get("content") == "[cleared]"
+ }
+ for msg in messages[1:-COLLAPSE_PRESERVE_RECENT]:
+ for tc in msg.get("tool_calls") or []:
+ fn = tc.get("function")
+ if not isinstance(fn, dict):
+ continue
+ args = fn.get("arguments")
+ if (
+ isinstance(args, str)
+ and len(args) > COLLAPSE_TEXT_MIN
+ and tc.get("id") in cleared_ids
+ ):
+ fn["arguments"] = "{}"
+
+
+def _msg_estimate_chars(msg: dict) -> int:
+ """Rough character size of a message for token budgeting.
+
+ Sizes ``content`` plus every tool-call ``arguments`` payload. Assistant
+ tool-call messages carry their payload in ``tool_calls[].function.
+ arguments`` with empty ``content``; sizing them by content alone made the
+ layer-3 tail budget count a 100 KB arguments blob as ~10 tokens.
+ """
+ size = len(str(msg.get("content", "")))
+ for tc in msg.get("tool_calls") or []:
+ fn = tc.get("function")
+ if isinstance(fn, dict):
+ args = fn.get("arguments")
+ if isinstance(args, str):
+ size += len(args)
+ return size
+
+
+def _tail_cut_index(body: list, budget: int = TAIL_TOKEN_BUDGET) -> int:
+ """First index of the preserved ``body`` tail that fits ``budget`` tokens.
+
+ Walks back from the end accumulating each message's estimated tokens and
+ returns the earliest index that fits, never splitting a tool_call /
+ tool_result pair. Sized with ``_msg_estimate_chars`` so oversized tool-call
+ arguments push their message into the folded head instead of being counted
+ as a handful of tokens in the preserved tail.
+ """
+ accumulated = 0
+ cut_idx = len(body)
+ for i in range(len(body) - 1, -1, -1):
+ msg_tokens = (_msg_estimate_chars(body[i]) // 4) + 10
+ if accumulated + msg_tokens > budget:
+ cut_idx = i + 1
+ break
+ accumulated += msg_tokens
+ cut_idx = i
+ while 0 < cut_idx < len(body) and body[cut_idx].get("role") == "tool":
+ cut_idx += 1
+ return cut_idx
+
def _fix_tool_pairs(messages: list) -> None:
"""Repair orphaned tool_call / tool_result pairs after compression.
@@ -2646,21 +2712,9 @@ class AgentLoop:
system_msg = messages[0]
body = messages[1:]
- # Token-budget tail: walk backward to find how many recent messages to preserve
- accumulated = 0
- cut_idx = len(body)
- for i in range(len(body) - 1, -1, -1):
- content = body[i].get("content", "")
- msg_tokens = (len(str(content)) // 4) + 10
- if accumulated + msg_tokens > TAIL_TOKEN_BUDGET:
- cut_idx = i + 1
- break
- accumulated += msg_tokens
- cut_idx = i
-
- # Don't split in the middle of a tool_call/tool_result pair
- while 0 < cut_idx < len(body) and body[cut_idx].get("role") == "tool":
- cut_idx += 1
+ # Token-budget tail: size messages with their tool-call arguments so
+ # oversized tool calls are folded instead of hiding in the tail.
+ cut_idx = _tail_cut_index(body)
head = body[:cut_idx]
tail = body[cut_idx:]
diff --git a/agent/tests/test_loop_helpers.py b/agent/tests/test_loop_helpers.py
index b3e1db506..df631f27d 100644
--- a/agent/tests/test_loop_helpers.py
+++ b/agent/tests/test_loop_helpers.py
@@ -598,3 +598,87 @@ def test_stall_timeout_seconds_default_and_override(monkeypatch) -> None:
assert _stall_timeout_seconds() == 42.0
monkeypatch.delattr(loop_module, "STALL_TIMEOUT_SECONDS", raising=False)
assert _stall_timeout_seconds() > 0
+
+
+# ---------------------------------------------------------------------------
+# tool_calls[].function.arguments must count toward compaction sizing/relief
+# ---------------------------------------------------------------------------
+
+def test_tail_cut_index_counts_tool_call_arguments() -> None:
+ """#tail-budget: a fat tool_call (empty content, huge arguments) must be
+ pushed into the folded head, not counted as ~10 tokens in the tail."""
+ from src.agent.loop import _tail_cut_index # new helper: absent on pre-fix code
+
+ fat = "X" * 90_000 # ~22.5K tokens once arguments are counted
+ body = [
+ {"role": "user", "content": "old header"},
+ {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [
+ {"id": "c1", "type": "function", "function": {"name": "t", "arguments": fat}}
+ ],
+ },
+ ]
+ # Old content-only sizing: both messages fit the 20K budget -> cut at 0.
+ # New sizing: the call alone exceeds it -> cut at len(body), tail empty,
+ # the oversized call goes into the folded head.
+ assert _tail_cut_index(body) == len(body)
+ assert _tail_cut_index(body) != 0
+
+
+def test_context_collapse_stubs_args_of_cleared_paired_call() -> None:
+ """#layer-2: a tool_call whose paired result was [cleared] gets its huge
+ arguments folded to a valid JSON '{}' stub (call id/name preserved)."""
+ fat = "Y" * (COLLAPSE_TEXT_MIN * 3)
+ call_msg = {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [{"id": "c9", "type": "function", "function": {"name": "f", "arguments": fat}}],
+ }
+ msgs = [
+ {"role": "system", "content": "sys"},
+ {"role": "user", "content": "u0"},
+ call_msg, # index 2 — inside the collapse window
+ {"role": "tool", "tool_call_id": "c9", "content": "[cleared]"},
+ {"role": "assistant", "content": "a0"},
+ {"role": "user", "content": "u1"},
+ {"role": "assistant", "content": "a1"},
+ {"role": "user", "content": "u2"},
+ {"role": "assistant", "content": "a2"},
+ {"role": "user", "content": "u3"},
+ ]
+ _context_collapse(msgs)
+ assert call_msg["tool_calls"][0]["function"]["arguments"] == "{}"
+ assert call_msg["tool_calls"][0]["function"]["name"] == "f"
+
+
+def test_context_collapse_keeps_args_when_result_intact_or_pending() -> None:
+ """#layer-2 negative arm: arguments are NOT stubbed when the result still
+ carries data, nor when the call is pending (no result in the transcript)."""
+ fat = "Y" * (COLLAPSE_TEXT_MIN * 3)
+ c1 = {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [{"id": "c1", "type": "function", "function": {"name": "f", "arguments": fat}}],
+ }
+ c2 = {
+ "role": "assistant",
+ "content": "",
+ "tool_calls": [{"id": "c2", "type": "function", "function": {"name": "g", "arguments": fat}}],
+ }
+ msgs = [
+ {"role": "system", "content": "sys"},
+ {"role": "user", "content": "u0"},
+ c1, # index 2 — inside the collapse window
+ {"role": "tool", "tool_call_id": "c1", "content": "still have the data"},
+ c2, # pending — result never arrived
+ {"role": "assistant", "content": "continue"},
+ {"role": "user", "content": "u1"},
+ {"role": "assistant", "content": "a1"},
+ {"role": "user", "content": "u2"},
+ {"role": "assistant", "content": "a2"},
+ ]
+ _context_collapse(msgs)
+ assert c1["tool_calls"][0]["function"]["arguments"] == fat
+ assert c2["tool_calls"][0]["function"]["arguments"] == fat