Skip to content

The agent that grows with you

fix(cron): bound Codex max-iteration summaries

Kevin Yinfe1426776977
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index e1ff712d684..c7cfcfdb32c 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -2250,7 +2250,10 @@ def _codex_summary_attempt(agent, api_messages: list, api_request_id: str):
codex_kwargs.pop("tools", None)
codex_kwargs.pop("tool_choice", None)
codex_kwargs.pop("parallel_tool_calls", None)
- return _summary_text(agent, agent._run_codex_stream(codex_kwargs))
+ # Route through the same seam as normal Codex turns: a direct _run_codex_stream
+ # bypasses the stale/TTFB watchdogs, interrupt handling and client cleanup, so an
+ # unattended cron summary could wedge forever (#70943).
+ return _summary_text(agent, agent._interruptible_api_call(codex_kwargs))
return _attempt
diff --git a/tests/agent/test_run_agent.py b/tests/agent/test_run_agent.py
index 25d64e224b9..46db617e96b 100644
--- a/tests/agent/test_run_agent.py
+++ b/tests/agent/test_run_agent.py
@@ -2677,6 +2677,86 @@ class TestHandleMaxIterations:
assert messages[1]["codex_reasoning_items"] == [{"id": "rs_1"}]
+ def test_codex_summary_uses_interruptible_request_path(self, agent):
+ """Max-iteration Codex summaries must retain request watchdogs.
+
+ A direct ``_run_codex_stream`` call bypasses the absolute stale timeout,
+ interrupt handling, and request-local client cleanup. In unattended cron
+ sessions that turns a wedged summary stream into a job that never returns
+ to cron's completion/error delivery lifecycle (#70943).
+ """
+ agent.api_mode = "codex_responses"
+ agent.provider = "xai-oauth"
+ agent.base_url = "https://api.x.ai/v1"
+ agent._base_url_lower = agent.base_url.lower()
+ agent._base_url_hostname = "api.x.ai"
+ agent.model = "grok-4.5"
+ agent.platform = "cron"
+ agent._cached_system_prompt = "You are helpful."
+ response = SimpleNamespace(
+ status="completed",
+ output=[
+ SimpleNamespace(
+ type="message",
+ status="completed",
+ content=[SimpleNamespace(type="output_text", text="Summary")],
+ )
+ ],
+ )
+
+ with patch.object(
+ agent, "_interruptible_api_call", return_value=response
+ ) as guarded_call, patch.object(
+ agent,
+ "_run_codex_stream",
+ side_effect=AssertionError("summary bypassed request watchdogs"),
+ ):
+ result = agent._handle_max_iterations(
+ [{"role": "user", "content": "do stuff"}], 4
+ )
+
+ assert result == "Summary"
+ guarded_call.assert_called_once()
+
+ def test_codex_summary_retry_uses_interruptible_request_path(self, agent):
+ """The empty-summary retry must use the same bounded request seam."""
+ agent.api_mode = "codex_responses"
+ agent.provider = "xai-oauth"
+ agent.base_url = "https://api.x.ai/v1"
+ agent._base_url_lower = agent.base_url.lower()
+ agent._base_url_hostname = "api.x.ai"
+ agent.model = "grok-4.5"
+ agent.platform = "cron"
+ agent._cached_system_prompt = "You are helpful."
+
+ def codex_response(text):
+ return SimpleNamespace(
+ status="completed",
+ output=[
+ SimpleNamespace(
+ type="message",
+ status="completed",
+ content=[SimpleNamespace(type="output_text", text=text)],
+ )
+ ],
+ )
+
+ with patch.object(
+ agent,
+ "_interruptible_api_call",
+ side_effect=[codex_response(""), codex_response("Summary after retry")],
+ ) as guarded_call, patch.object(
+ agent,
+ "_run_codex_stream",
+ side_effect=AssertionError("summary retry bypassed request watchdogs"),
+ ):
+ result = agent._handle_max_iterations(
+ [{"role": "user", "content": "do stuff"}], 4
+ )
+
+ assert result == "Summary after retry"
+ assert guarded_call.call_count == 2
+
def test_codex_summary_sanitizes_orphan_tool_results(self, agent):
agent.api_mode = "codex_responses"
agent.provider = "openai-codex"
@@ -2687,7 +2767,7 @@ class TestHandleMaxIterations:
agent._cached_system_prompt = "You are helpful."
captured = {}
- def fake_run_codex_stream(kwargs):
+ def fake_run_codex_stream(kwargs, client=None, on_first_delta=None):
captured.update(kwargs)
return SimpleNamespace(
status="completed",