Skip to content

The agent that grows with you

fix(agent): route a planning-monologue reasoning-only stop through the stall guard, not a fake completion

teknium103e17ba7e2b9
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index e0fa31554fb..43b095a340b 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -2933,6 +2933,34 @@ def trailing_continue_intent(text: str) -> bool:
return bool(_TRAILING_CONTINUE_INTENT_RE.search(t[-160:]))
+# Broader tail detector for PROMOTED REASONING only (reasoning-only clean stop with tools offered
+# and no tool call). Visible content keeps the narrow ``let me now`` shape above because a real
+# reply legitimately says "I'll" mid-text; chain-of-thought that ENDS on a first-person plan
+# ("Let me batch the terminal calls and run them in parallel.", "I need to check the log.") is a
+# stalled model whose turn would otherwise report "complete" with zero tool calls (#111761).
+# Tail-only and anchored on the last sentence, so reasoning that merely mentions a plan before
+# stating its answer ("...Let me check. The answer is 42.") still promotes.
+_PROMOTED_REASONING_PLAN_TAIL_RE = re.compile(
+ r"(?:^|[.!?:\u3002\uff01\uff1f\n]\s*|\u2026\s*)"
+ r"(?:let(?:['\u2019]s| me)\b|i(?:['\u2019]ll| will| need to| should| am going to|['\u2019]m going to)\b"
+ r"|next[,:]? i\b|now i(?:['\u2019]ll| will| need to)\b|first[,:]? i(?:['\u2019]ll| will| need to)\b)"
+ r"[^.!?\n\u3002\uff01\uff1f]{0,160}[.:\u2026]?\s*$",
+ re.IGNORECASE,
+)
+
+
+def promoted_reasoning_announces_action(text: str) -> bool:
+ """Whether promoted reasoning ENDS on a first-person plan to act (stall, not an answer).
+
+ No overall length cap: the reasoning block of a stalled model is often 300-1600 chars of
+ planning monologue; only the tail decides.
+ """
+ t = (text or "").strip()
+ if not t:
+ return False
+ return bool(_PROMOTED_REASONING_PLAN_TAIL_RE.search(t[-240:]))
+
+
_INTENT_ACK_ON = {"true", "always", "yes", "on"}
_INTENT_ACK_OFF = {"false", "never", "no", "off"}
diff --git a/agent/turn_final_response.py b/agent/turn_final_response.py
index 9dba07c3427..2fab0bc4f76 100644
--- a/agent/turn_final_response.py
+++ b/agent/turn_final_response.py
@@ -142,17 +142,25 @@ def finish_text_response(
# delivery channel (gateway status message / CLI print). NEVER appended to messages/api_messages:
# conversation context and the cached prompt prefix stay byte-identical.
from agent.agent_runtime_helpers import (
- intent_ack_continuation_mode, trailing_continue_intent
+ intent_ack_continuation_mode, promoted_reasoning_announces_action, trailing_continue_intent
)
_ack_mode = intent_ack_continuation_mode(agent)
# Said-continue-but-stopped guard: no tool calls but the short reply TAILS with an
# announced next action. Reuses the SAME bounded continuation counter (max 2 per turn).
+ # Promoted reasoning gets the broader first-person-plan tail detector: with tools offered
+ # and zero tool calls, chain-of-thought ending on "Let me batch the terminal calls..." is a
+ # stalled model, and returning it as the answer aborts the tool loop while reporting
+ # "complete" (#111761). Same cap, so a model that never acts still ends after 2 nudges.
+ _stall_text = agent._strip_think_blocks(final_response or "")
_stall_continue_intent = (
bool(getattr(agent, "_stall_guards", True))
and agent.valid_tool_names
and codex_ack_continuations < 2
- and trailing_continue_intent(agent._strip_think_blocks(final_response or ""))
+ and (
+ trailing_continue_intent(_stall_text)
+ or (bool(_promoted) and promoted_reasoning_announces_action(_stall_text))
+ )
)
if _stall_continue_intent or (
_ack_mode != "off"
diff --git a/tests/agent/test_reasoning_only_stop_persistence.py b/tests/agent/test_reasoning_only_stop_persistence.py
index 34dcc1e6521..20e619b588f 100644
--- a/tests/agent/test_reasoning_only_stop_persistence.py
+++ b/tests/agent/test_reasoning_only_stop_persistence.py
@@ -119,3 +119,74 @@ def test_reasoning_only_clean_stop_logs_warning_with_route(loop_agent, caplog):
assert f"model={loop_agent.model}" in hits[0].getMessage()
assert "provider=deepseek" in hits[0].getMessage()
assert "tool_turns=0" in hits[0].getMessage()
+
+
+# ── planning-monologue stall: promoted reasoning must not fake a completion ──────────────────
+
+PLAN_TAILS = [
+ "Let me batch the terminal calls and run them in parallel.",
+ "...Let me load the doctrine skill first, then run checks.",
+ "Attempting to read and process the file. Initial hypothesis: the config is stale. I need to check the log.",
+]
+
+
[email protected]("tail", PLAN_TAILS)
+def test_planning_tail_reasoning_only_stop_with_tools_runs_continuation_not_completion(loop_agent, tail):
+ """Tools offered, zero tool calls, and the promoted reasoning ENDS on a first-person plan
+ ("Let me batch...", "I need to check...") — the verbatim tails from the #111761 thread. This
+ is a stalled model, not an answer: the stall-guard continuation must run (bounded by the same
+ cap) instead of returning the monologue as a 'complete' final response."""
+ from tests.agent.test_run_agent import _mock_response
+
+ loop_agent.valid_tool_names = {"terminal", "read_file"}
+ loop_agent._stall_guards = True
+
+ result = _run(loop_agent, [
+ _mock_response(content="", finish_reason="stop", reasoning_content=tail),
+ _mock_response(content="Ran the checks; all green.", finish_reason="stop"),
+ ])
+
+ assert result["api_calls"] == 2
+ assert result["final_response"] == "Ran the checks; all green."
+ interim = [m for m in result["messages"] if m.get("role") == "assistant"][0]
+ assert not interim.get("content")
+ assert interim["api_content"] == tail # interim row keeps the sidecar shape
+
+
+def test_planning_tail_stall_is_bounded_by_the_continuation_cap(loop_agent):
+ """A model that never acts is nudged at most twice; the third planning-only stop is promoted
+ so the turn still ends instead of looping."""
+ from tests.agent.test_run_agent import _mock_response
+
+ loop_agent.valid_tool_names = {"terminal"}
+ loop_agent._stall_guards = True
+ tail = PLAN_TAILS[0]
+
+ result = _run(loop_agent, [
+ _mock_response(content="", finish_reason="stop", reasoning_content=tail),
+ _mock_response(content="", finish_reason="stop", reasoning_content=tail),
+ _mock_response(content="", finish_reason="stop", reasoning_content=tail),
+ _mock_response(content="NEVER REACHED", finish_reason="stop"),
+ ])
+
+ assert result["api_calls"] == 3
+ assert result["final_response"] == tail
+
+
+def test_genuine_reasoning_only_answer_with_tools_still_promotes_on_first_call(loop_agent):
+ """The parser-compat contract survives: reasoning that states an answer (no trailing plan) is
+ returned on the first call even with tools offered, and mentioning a plan BEFORE the answer
+ does not count as a stall."""
+ from tests.agent.test_run_agent import _mock_response
+
+ loop_agent.valid_tool_names = {"terminal", "read_file"}
+ loop_agent._stall_guards = True
+
+ for answer in ("The answer is 42.", "Let me check the arithmetic. 6 times 7 is 42, so the answer is 42."):
+ loop_agent.client.chat.completions.create.reset_mock()
+ result = _run(loop_agent, [
+ _mock_response(content="", finish_reason="stop", reasoning_content=answer),
+ _mock_response(content="NEVER REACHED", finish_reason="stop"),
+ ])
+ assert result["api_calls"] == 1
+ assert result["final_response"] == answer
diff --git a/tests/agent/test_stall_guards.py b/tests/agent/test_stall_guards.py
index 866171da8c9..4038435332e 100644
--- a/tests/agent/test_stall_guards.py
+++ b/tests/agent/test_stall_guards.py
@@ -434,3 +434,37 @@ def test_cycle_guard_stays_silent_for_progressing_and_poller_cycles():
for args in ({"action": "poll", "session_id": "a"}, {"action": "poll", "session_id": "b"}):
assert pollers.observe_call("process_manage", args, "running").notice is None
assert pollers.halt_decision is None
+
+
+# ── promoted-reasoning plan-tail detector (#111761) ─────────────────────────
+
+
+def test_promoted_reasoning_detector_catches_first_person_plan_tails():
+ from agent.agent_runtime_helpers import promoted_reasoning_announces_action
+
+ # Verbatim tails from the #111761 thread — none match the narrow visible-content detector.
+ for tail in (
+ "Let me batch the terminal calls and run them in parallel.",
+ "...Let me load the doctrine skill first, then run checks.",
+ "Initial hypothesis: the config is stale. I need to check the log.",
+ "I'm going to run the tests now",
+ "嗯,长度合适。Let me check the file first.",
+ ):
+ assert not trailing_continue_intent(tail), tail
+ assert promoted_reasoning_announces_action(tail), tail
+ # Long monologue: only the tail decides (no 400-char cap on this path).
+ assert promoted_reasoning_announces_action(("Thinking about the task. " * 60) + "Let me read the file.")
+
+
+def test_promoted_reasoning_detector_ignores_stated_answers():
+ from agent.agent_runtime_helpers import promoted_reasoning_announces_action
+
+ for text in (
+ "The answer is 42.",
+ "Let me check the arithmetic. 6 times 7 is 42, so the answer is 42.",
+ "If you want, I will happily review the PR once CI is green. Just say so!",
+ "structured reasoning answer",
+ "",
+ None,
+ ):
+ assert not promoted_reasoning_announces_action(text), text