Skip to content

Official Codebase of 3DCodeVerse

Python

agents: the context compactor was measuring the wrong thing and never fired

gaoypeng77e3973d90fb
diff --git a/harness/codeverse/agents/api_agent.py b/harness/codeverse/agents/api_agent.py
index a544534f..77b7b396 100644
--- a/harness/codeverse/agents/api_agent.py
+++ b/harness/codeverse/agents/api_agent.py
@@ -35,6 +35,15 @@ log = logging.getLogger(__name__)
BUILD_TOOL = "build"
COMPACT_AT_CHARS = 350_000
+#: compact once the PROVIDER says the context is this big. The char estimate above is
+#: what the harness can see (visible text + tool arguments); it misses the model's own
+#: thoughts, which are re-sent as context every turn. Measured 2026-08-27 on one
+#: fancy_bl_windsor_chair assemble session: message_chars said 204 732 (threshold
+#: 350 000, never tripped) while the provider was actually reading 113 956 tokens —
+#: 2.2x more than the estimate. The conversation grew ~1 650 tokens a turn for 60
+#: turns, 49 % of it file bodies the model had already written to disk, and every turn
+#: re-processed all of it. input_tokens is ground truth, so trigger on that.
+COMPACT_AT_INPUT_TOKENS = 60_000
MODEL_RETRIES = 3
#: per-turn output envelope, and how far a TRUNCATED turn may grow it. Measured
#: 2026-08-27 (art_med_tool_chest, 3.6-flash): thinking escalated 13 -> 3 144 -> 15 359
@@ -248,9 +257,11 @@ class _Loop:
self.errors + [f"cost {self.usage.cost_usd:.3f} > max_usd {self.max_usd}"],
)
break
- if message_chars(self.messages) > COMPACT_AT_CHARS:
+ seen = int(getattr(resp.usage, "input_tokens", 0) or 0)
+ if seen > COMPACT_AT_INPUT_TOKENS or message_chars(self.messages) > COMPACT_AT_CHARS:
self.messages = compact_messages(self.messages, keep_recent=6, facts=self._facts())
- traj.append("compact", turn=turn, chars=message_chars(self.messages))
+ traj.append("compact", turn=turn, chars=message_chars(self.messages),
+ input_tokens_seen=seen)
else:
self.errors.append(f"max_turns ({job.max_turns}) reached")
return finish_session(
diff --git a/harness/codeverse/agents/context.py b/harness/codeverse/agents/context.py
index 772bf4ab..a67b19fb 100644
--- a/harness/codeverse/agents/context.py
+++ b/harness/codeverse/agents/context.py
@@ -7,10 +7,16 @@ the task + a running summary while the transcript on disk stays complete.
from __future__ import annotations
-from codeverse.contracts.chat import ChatMessage, ImagePart, TextPart, ToolResultPart
+from codeverse.contracts.chat import ChatMessage, ImagePart, TextPart, ToolCallPart, ToolResultPart
STUB_CHARS = 240
FACTS_TAG = "<facts_so_far>"
+#: tool-call arguments whose payload is a file body: once written, the file is on disk
+#: and ``read_file`` can fetch it, so keeping the text in the conversation buys nothing
+#: and is re-processed on every later turn. Measured 2026-08-27 on one assemble
+#: session: 29 write_file calls were 49 % of the whole conversation.
+BODY_ARG_TOOLS = frozenset({"write_file", "edit_file"})
+BODY_ARG_KEYS = ("content", "new", "old", "text")
def message_chars(messages: list[ChatMessage]) -> int:
@@ -35,12 +41,31 @@ def _stub(content: str) -> str:
return content[:STUB_CHARS] + f" … [compacted; {len(content)} chars originally]"
+def _stub_written_bodies(m: ChatMessage) -> ChatMessage:
+ """Shorten the file bodies carried in this message's write/edit tool CALLS."""
+ if not any(isinstance(p, ToolCallPart) and p.name in BODY_ARG_TOOLS for p in m.parts):
+ return m
+ parts: list[object] = []
+ for p in m.parts:
+ if isinstance(p, ToolCallPart) and p.name in BODY_ARG_TOOLS:
+ args = dict(p.arguments or {})
+ for k in BODY_ARG_KEYS:
+ if isinstance(args.get(k), str):
+ args[k] = _stub(args[k])
+ parts.append(ToolCallPart(id=p.id, name=p.name, arguments=args))
+ else:
+ parts.append(p)
+ return ChatMessage(role=m.role, parts=parts) # type: ignore[arg-type]
+
+
def compact_messages(messages: list[ChatMessage], *, keep_recent: int = 8, facts: str = "") -> list[ChatMessage]:
- """Return a new message list with old tool results stubbed and a facts note.
+ """Return a new message list with old bodies stubbed and a facts note.
The first message (the task) and the last ``keep_recent`` messages are kept
- verbatim; everything in between has tool-result bodies shortened and images
- removed. Any earlier facts note is replaced.
+ verbatim; everything in between has tool-result bodies shortened, images removed,
+ and the file bodies inside old ``write_file``/``edit_file`` CALLS stubbed — the
+ file is on disk, so re-sending its text every turn buys nothing. Any earlier
+ facts note is replaced.
"""
if len(messages) <= keep_recent + 1:
return list(messages)
@@ -52,7 +77,7 @@ def compact_messages(messages: list[ChatMessage], *, keep_recent: int = 8, facts
if m.role == "user" and m.text.startswith(FACTS_TAG):
continue # superseded
if m.role != "tool":
- out.append(m)
+ out.append(_stub_written_bodies(m))
continue
parts = [
ToolResultPart(call_id=p.call_id, name=p.name, content=_stub(p.content), images=[], is_error=p.is_error)
@@ -64,4 +89,4 @@ def compact_messages(messages: list[ChatMessage], *, keep_recent: int = 8, facts
return out
-__all__ = ["compact_messages", "message_chars", "FACTS_TAG"]
+__all__ = ["compact_messages", "message_chars", "FACTS_TAG", "BODY_ARG_TOOLS"]
diff --git a/harness/tests/agents/test_context.py b/harness/tests/agents/test_context.py
new file mode 100644
index 00000000..0c80b22c
--- /dev/null
+++ b/harness/tests/agents/test_context.py
@@ -0,0 +1,23 @@
+
+
+def test_compaction_stubs_the_file_bodies_the_model_already_wrote():
+ """Measured 2026-08-27 (fancy_bl_windsor_chair assemble, 60 turns): 29 write_file
+ calls were 49 % of a conversation that reached 113 956 input tokens, and every later
+ turn re-processed all of it. The file is on disk; read_file can fetch it."""
+ from codeverse.agents.context import compact_messages, message_chars
+ from codeverse.contracts.chat import ChatMessage, ToolCallPart
+
+ body = "import bpy\n" * 400
+ msgs = [ChatMessage.user("build a chair")]
+ for i in range(10):
+ msgs.append(ChatMessage(role="assistant", parts=[
+ ToolCallPart(id=f"c{i}", name="write_file",
+ arguments={"path": f"src/parts/p{i}.py", "content": body})]))
+ msgs.append(ChatMessage.user(f"wrote p{i}"))
+ before = message_chars(msgs)
+ after = message_chars(compact_messages(msgs, keep_recent=4))
+ assert after < before / 2, f"{before} -> {after}: the written bodies must not survive"
+ kept = compact_messages(msgs, keep_recent=4)
+ assert kept[0].text == "build a chair" # the task is never touched
+ tail_call = next(p for p in kept[-2].parts if isinstance(p, ToolCallPart))
+ assert tail_call.arguments["content"] == body, "recent turns stay verbatim"