The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): continue a length stop that only has side-channel reasoning
brooklyn!2c42fcb50778
diff --git a/agent/turn_truncation.py b/agent/turn_truncation.pyindex 265a391cfc7..6405bd97ccc 100644--- a/agent/turn_truncation.py+++ b/agent/turn_truncation.py@@ -242,47 +242,23 @@ class _Trunc(TruncationVerdict):return getattr(self.response, "id", "") == PARTIAL_STREAM_STUB_ID-def _abort_reason(- agent: Any, content: Any, has_tool_calls: bool, *,- reasoning: Any = None, reasoning_content: Any = None,-) -> Optional[tuple]:+def _abort_reason(agent: Any, content: Any, has_tool_calls: bool) -> Optional[tuple]:"""``(vprint, user response, error)`` when continuation must NOT be attempted:- thinking exhausted the budget (a ``<think>`` block with no visible text after- it, or ``finish_reason=length`` with empty visible content and a non-empty- ``reasoning`` / ``reasoning_content`` side channel), or a repetition loop- burned the budget on one fragment (reasoning stripped first). Empty content- with no side-channel reasoning is still a normal truncation."""+ thinking exhausted the budget (reasoning blocks with no visible text after them —+ ``content=None`` from non-<think> models is normal truncation), or a repetition loop+ burned the budget on one fragment (reasoning stripped first). A length stop whose+ only text is a reasoning side channel is unfinished thought: continuation owns it.+ """if has_tool_calls:return Noneif content and _THINK_TAG_RE.search(content) and not agent._has_content_after_think_block(content):return _THINKING_EXHAUSTED- if _visible_content_empty(agent, content) and (- _nonempty_side_channel(reasoning) or _nonempty_side_channel(reasoning_content)- ):- return _THINKING_EXHAUSTEDvisible = agent._strip_think_blocks(content) if isinstance(content, str) else contentif visible and is_repetition_dominated(visible):return _REPETITION_DOMINATEDreturn None-def _nonempty_side_channel(value: Any) -> bool:- """True when a reasoning side channel actually carried text. No model id."""- if isinstance(value, str):- return bool(value.strip())- if isinstance(value, (list, tuple, dict, set)):- return len(value) > 0- return bool(value)---def _visible_content_empty(agent: Any, content: Any) -> bool:- if content is None:- return True- if isinstance(content, str):- return not (agent._strip_think_blocks(content) or "").strip()- return not content--def _content_filter_fallback(st: _Trunc, _retry: TurnRetryState) -> Optional[TruncationVerdict]:"""Content-filter stream stall → fallback. ``_content_filter_terminated`` iscontent-deterministic, so escalate before retrying the primary; without a fallback@@ -519,11 +495,7 @@ def recover_from_truncation(_trunc_content = getattr(_trunc_msg, "content", None) if _trunc_msg else None_trunc_has_tool_calls = bool(getattr(_trunc_msg, "tool_calls", None)) if _trunc_msg else False- abort = _abort_reason(- agent, _trunc_content, _trunc_has_tool_calls,- reasoning=getattr(_trunc_msg, "reasoning", None) if _trunc_msg else None,- reasoning_content=getattr(_trunc_msg, "reasoning_content", None) if _trunc_msg else None,- )+ abort = _abort_reason(agent, _trunc_content, _trunc_has_tool_calls)if abort is not None:line, user_response, error = abortagent._vprint(f"{agent.log_prefix}{line}", force=True, diagnostic=True)diff --git a/tests/agent/test_length_continuation_thinking_exhaustion.py b/tests/agent/test_length_continuation_thinking_exhaustion.pyindex 39fdc963a70..90d33d0493a 100644--- a/tests/agent/test_length_continuation_thinking_exhaustion.py+++ b/tests/agent/test_length_continuation_thinking_exhaustion.py@@ -343,26 +343,24 @@ class TestReasoningOffReachesTheWire:assert first == {"enabled": True, "effort": "high"}, first-class TestReasoningSideChannelBudgetAbort:- """finish_reason=length with nothing visible and a non-empty reasoning- side channel is the existing thinking-budget abort, not a continuation.- No model id is consulted."""+class TestReasoningSideChannelLengthContinues:+ """finish_reason=length with nothing visible and a reasoning side channel is+ unfinished thought. Continuation owns it; the side channel is not the answer.+ """@pytest.mark.parametrize("field", ["reasoning", "reasoning_content"])- def test_empty_visible_content_aborts_on_side_channel(self, loop_agent, field):+ def test_empty_visible_content_continues(self, loop_agent, field):kwargs = {field: "the model spent the whole budget here"}loop_agent.client.chat.completions.create.side_effect = [_side_channel_length_response(**kwargs),- _full_response("this continuation must not run"),+ _full_response("the visible answer"),]result = _run(loop_agent, "write me a long report")- assert result["completed"] is False- assert "Thinking Budget Exhausted" in (result["final_response"] or "")- assert loop_agent.client.chat.completions.create.call_count == 1, (- "A length stop with empty visible content and a non-empty "- f"{field} field must abort, not continue."- )+ assert result["completed"] is True+ assert "the visible answer" in (result["final_response"] or "")+ assert "the model spent the whole budget here" not in (result["final_response"] or "")+ assert loop_agent.client.chat.completions.create.call_count == 2def test_visible_answer_with_reasoning_still_continues(self, loop_agent):loop_agent.client.chat.completions.create.side_effect = [