Skip to content

Official Codebase of 3DCodeVerse

Python

api-agent: a turn cut off mid-thought is not a finished session

gaoypeng44ac549b9efb
diff --git a/harness/codeverse/agents/api_agent.py b/harness/codeverse/agents/api_agent.py
index 99befead..23682a44 100644
--- a/harness/codeverse/agents/api_agent.py
+++ b/harness/codeverse/agents/api_agent.py
@@ -55,6 +55,19 @@ FRESHNESS_NUDGE = (
"You edited files after your last `build`. Run the `build` tool now, read its result, fix any "
"errors, and only then give your final summary."
)
+#: A turn that ran out of output tokens mid-thought emits partial text and NO tool call,
+#: which is indistinguishable from "I am done" at the loop's `if not resp.tool_calls`.
+#: Measured 2026-08-27 (art_med_tool_chest): the baseline spent 15 359 thinking tokens on
+#: turn 5, was truncated at MAX_TOKENS, and the session was recorded as completed with
+#: zero files written. Thinking is billed either way, so the cure is to ask for the call
+#: rather than the reasoning.
+TRUNCATED_NUDGE = (
+ "Your previous turn hit the output limit while thinking, so no tool call arrived. Do not "
+ "re-derive anything: make the single most useful tool call NOW (write the file you had "
+ "planned, or `build`), with no analysis before it."
+)
+#: how many truncated turns in one session get a nudge before it is called a failure
+MAX_TRUNCATED_NUDGES = 2
class ApiAgent:
@@ -139,6 +152,7 @@ class _Loop:
self.last_write_turn = -1
self.last_build_turn = -1
self.nudged = False
+ self.truncated = 0
self.errors: list[str] = []
# the images ride on the first message only: reference photos, and for a refine
# session the contact sheet the judge scored. Until 2026-08-26 no image reached an
@@ -187,6 +201,18 @@ class _Loop:
finish_reason=resp.finish_reason,
)
if not resp.tool_calls:
+ if resp.finish_reason == "MAX_TOKENS" and self.truncated < MAX_TRUNCATED_NUDGES:
+ # cut off mid-thought, not finished: ask for the call, not more reasoning
+ self.truncated += 1
+ self.messages.append(ChatMessage.user(TRUNCATED_NUDGE))
+ traj.append("nudge", turn=turn, text=TRUNCATED_NUDGE, reason="max_tokens")
+ continue
+ if resp.finish_reason == "MAX_TOKENS":
+ ok, reason = False, "truncated"
+ self.errors.append(
+ f"{self.truncated + 1} turns ran out of output tokens while thinking and "
+ "never emitted a tool call")
+ break
if self._needs_build_nudge():
self.nudged = True
self.messages.append(ChatMessage.user(FRESHNESS_NUDGE))
diff --git a/harness/tests/agents/test_api_agent.py b/harness/tests/agents/test_api_agent.py
index c2ccf82c..40434ee7 100644
--- a/harness/tests/agents/test_api_agent.py
+++ b/harness/tests/agents/test_api_agent.py
@@ -289,3 +289,35 @@ def test_a_turn_stops_retrying_once_the_session_deadline_has_passed(tmp_ws, monk
rows = [json.loads(ln) for ln in Path(res.transcript_path).read_text().splitlines()]
(err_row,) = [r for r in rows if r["kind"] == "model_error"]
assert err_row["deadline_passed"] is True and err_row["retryable"] is True
+
+
+def cut(text: str = "thinking...") -> ChatResponse:
+ """A turn that ran out of output tokens mid-thought: partial text, no tool call."""
+ return ChatResponse(text=text, tool_calls=[], finish_reason="MAX_TOKENS",
+ usage=Usage(input_tokens=10, output_tokens=637, cost_usd=0.02))
+
+
+def test_a_turn_truncated_while_thinking_is_nudged_not_treated_as_finished(tmp_ws):
+ """art_med_tool_chest, 2026-08-27: turn 5 spent 15 359 thinking tokens, hit
+ MAX_TOKENS, emitted partial text and no tool call — and `if not resp.tool_calls`
+ recorded the session as completed with zero files written."""
+ from codeverse.agents.api_agent import TRUNCATED_NUDGE
+
+ fake = FakeChatModel([
+ cut("<thought>let me work out the hinge axis and"),
+ resp("", call("write_file", path="src/model.py", content="import bpy\n")),
+ resp("done"),
+ ])
+ res = ApiAgent("fake:fake-1", chat_model=fake).run(_job(tmp_ws))
+ assert res.ok and res.files_changed, "a truncated turn must not end the session"
+ assert (tmp_ws.src / "model.py").is_file()
+ assert any(m.text == TRUNCATED_NUDGE for r in fake.requests for m in r.messages if m.role == "user")
+
+
+def test_repeated_truncation_fails_the_session_instead_of_looping(tmp_ws):
+ from codeverse.agents.api_agent import MAX_TRUNCATED_NUDGES
+
+ fake = FakeChatModel([cut()] * (MAX_TRUNCATED_NUDGES + 2))
+ res = ApiAgent("fake:fake-1", chat_model=fake).run(_job(tmp_ws))
+ assert not res.ok and res.exit_reason == "truncated"
+ assert any("output tokens" in e for e in res.errors)