Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(agent): count reasoning_content in the compaction tail budget

Lanre Shittu8f3acc7046ea
diff --git a/agent/src/agent/loop.py b/agent/src/agent/loop.py
index 54b055875..2030b84bc 100644
--- a/agent/src/agent/loop.py
+++ b/agent/src/agent/loop.py
@@ -613,12 +613,19 @@ def _context_collapse(messages: list) -> None:
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.
+ Sizes ``content`` plus ``reasoning_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. A thinking-model turn's ``reasoning_content`` (Kimi K2.5,
+ DeepSeek reasoner, Qwen thinking) is the same failure mode: ``estimate_tokens``
+ counts it via full JSON serialization, so leaving it out here undercounts
+ the tail relative to the trigger that decided compaction was needed.
"""
size = len(str(msg.get("content", "")))
+ reasoning_content = msg.get("reasoning_content")
+ if reasoning_content is not None:
+ size += len(str(reasoning_content))
for tc in msg.get("tool_calls") or []:
fn = tc.get("function")
if isinstance(fn, dict) and fn.get("arguments") is not None:
diff --git a/agent/tests/test_loop_helpers.py b/agent/tests/test_loop_helpers.py
index cb7a8e7e9..72dc9d04e 100644
--- a/agent/tests/test_loop_helpers.py
+++ b/agent/tests/test_loop_helpers.py
@@ -709,6 +709,23 @@ def test_tail_cut_index_counts_tool_call_arguments() -> None:
assert _tail_cut_index(body) != 0
+def test_tail_cut_index_counts_reasoning_content() -> None:
+ """#tail-budget: a thinking-model turn's huge reasoning_content must be
+ pushed into the folded head too, not counted as ~10 tokens in the tail."""
+ from src.agent.loop import _tail_cut_index
+
+ fat = "X" * 90_000 # ~22.5K tokens once reasoning_content is counted
+ body = [
+ {"role": "user", "content": "old header"},
+ {"role": "assistant", "content": "final answer", "reasoning_content": fat},
+ ]
+ # Old sizing (content only): both messages fit the 20K budget -> cut at 0.
+ # New sizing: the reasoning turn alone exceeds it -> cut at len(body), tail
+ # empty, the oversized turn 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)."""
@@ -811,6 +828,20 @@ def test_msg_estimate_chars_counts_dict_arguments() -> None:
assert _msg_estimate_chars({"role": "user", "content": "hi"}) >= 2
+def test_msg_estimate_chars_counts_reasoning_content() -> None:
+ """#adversarial: reasoning_content must count toward sizing too, consistent
+ with ``estimate_tokens``' full serialization gate."""
+ from src.agent.loop import _msg_estimate_chars
+
+ msg = {
+ "role": "assistant",
+ "content": "final answer",
+ "reasoning_content": "X" * 4000,
+ }
+ assert _msg_estimate_chars(msg) >= 4000
+ assert _msg_estimate_chars({"role": "user", "content": "hi"}) < 4000
+
+
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."""