Skip to content

The agent that grows with you

test(agent): pin the reasoning-off continuation to exactly one request; document its prompt-cache cost

teknium1c83ea9bed7ca
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 79d4b925c34..5f7467e2918 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -1946,6 +1946,20 @@ def _consume_ephemeral_reasoning_off(agent) -> bool:
then "Response remained truncated after 4 continuation attempts").
When True is returned the caller must override the wire reasoning_config
with ``{"enabled": False, "effort": "none"}`` for exactly the next call.
+
+ Prompt-cache cost (deliberate, bounded): the reasoning parameter is part
+ of the provider's cache key on config-sensitive providers — Anthropic
+ renders thinking/effort into the prompt, OpenAI lists reasoning.effort
+ among prefix-affecting settings — so THAT one request misses the prefix
+ cache and pays a cold write of the full prefix (1.25x input instead of
+ the 0.1x read). The next request goes out with the configured reasoning
+ again and hits the thinking-on entry written by the truncated request
+ (still within TTL), so the damage is exactly one write. Template-tail
+ providers (GLM/Qwen/Kimi-style, where thinking on/off is a chat-template
+ switch at the tail) see no prefix change at all. The system prompt bytes
+ are never touched. This is far cheaper than what the flag prevents: four
+ full-output-budget requests that produce nothing and end the turn with an
+ error.
"""
if getattr(agent, "_ephemeral_reasoning_off", False):
agent._ephemeral_reasoning_off = False
diff --git a/tests/run_agent/test_length_continuation_thinking_exhaustion.py b/tests/run_agent/test_length_continuation_thinking_exhaustion.py
index 5c8a0a25c4e..a70e71fdf7e 100644
--- a/tests/run_agent/test_length_continuation_thinking_exhaustion.py
+++ b/tests/run_agent/test_length_continuation_thinking_exhaustion.py
@@ -249,6 +249,51 @@ class TestReasoningOffReachesTheWire:
f"continuation must be sent with thinking off, got {second!r}"
)
+ def test_reasoning_off_is_exactly_one_request_and_prefix_stays_stable(self, loop_agent):
+ """Prompt-cache invariant for the override.
+
+ The reasoning parameter is part of the provider's cache key on
+ config-sensitive providers (Anthropic renders thinking/effort into
+ the prompt; OpenAI lists reasoning.effort as a prefix-affecting
+ setting), so the reasoning-off request is a deliberate one-request
+ cache miss. It must stay exactly one request: the request AFTER it
+ (a second, visible-text continuation) must go out with the
+ configured reasoning again, and the system prompt must be
+ byte-identical on every request so the miss never compounds into a
+ rebuilt prefix.
+ """
+ loop_agent.reasoning_config = {"enabled": True, "effort": "high"}
+ loop_agent._supports_reasoning_extra_body = lambda: True
+ loop_agent.client.chat.completions.create.side_effect = [
+ _thinking_only_length_response(),
+ _truncated_text_response("PART ONE of the answer"),
+ _full_response(" and PART TWO, done."),
+ ]
+ result = _run(loop_agent, "write me a long report")
+ assert result["completed"] is True
+ assert "PART ONE" in result["final_response"]
+ assert "PART TWO" in result["final_response"]
+
+ calls = loop_agent.client.chat.completions.create.call_args_list
+ assert len(calls) == 3
+ wire = [
+ (c.kwargs.get("extra_body") or {}).get("reasoning") for c in calls
+ ]
+ assert wire[0] == {"enabled": True, "effort": "high"}, wire
+ assert wire[1] == {"enabled": False, "effort": "none"}, wire
+ assert wire[2] == {"enabled": True, "effort": "high"}, (
+ f"reasoning must be restored on the very next request; got {wire!r}"
+ )
+ system_prompts = {
+ c.kwargs["messages"][0]["content"] for c in calls
+ if c.kwargs["messages"][0].get("role") == "system"
+ }
+ assert len(system_prompts) == 1, (
+ "system prompt must be byte-identical across the retry sequence "
+ "(the override may only change request parameters, never the prefix)"
+ )
+ assert loop_agent._ephemeral_reasoning_off is False
+
def test_stale_flag_does_not_leak_into_next_turn(self, loop_agent):
"""A flag armed by a previous turn that never reached build_api_kwargs
(interrupt/error between arm and consume) must not silently strip