The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix: Responses-wire length continuation raises the output cap and drops reasoning after an empty fragment
teknium192c24d410d0a
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex 18bd54e6a4c..10807d84f94 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -1317,10 +1317,12 @@ def _build_codex_kwargs(agent, api_messages, tools_for_api, reasoning_config, retools_for_api, _ = strip_slash_enum(tools_for_api)except Exception as exc:logger.warning("%s⚠️ Failed to sanitize tool schemas for xAI: %s", getattr(agent, "log_prefix", ""), exc)+ ephemeral_out = _consume_ephemeral_max_output(agent)return agent._get_transport().build_kwargs(model=agent.model,messages=agent._prepare_messages_for_non_vision_model(api_messages), tools=tools_for_api,reasoning_config=reasoning_config, session_id=getattr(agent, "session_id", None),- cache_scope_id=cache_scope_id, base_url=agent.base_url, max_tokens=agent.max_tokens,+ cache_scope_id=cache_scope_id, base_url=agent.base_url,+ max_tokens=ephemeral_out if ephemeral_out is not None else agent.max_tokens,timeout=agent._resolved_api_call_timeout(), request_overrides=request_overrides,provider=getattr(agent, "provider", None), is_github_responses=is_github_responses,is_codex_backend=is_codex_backend, is_xai_responses=is_xai_responses,diff --git a/agent/turn_response_intake.py b/agent/turn_response_intake.pyindex 8c4b5949ea9..072fd700cb4 100644--- a/agent/turn_response_intake.py+++ b/agent/turn_response_intake.py@@ -173,6 +173,7 @@ def normalize_model_response(_codex_result = continue_codex_incomplete(agent, assistant_message, finish_reason, messages=messages,conversation_history=conversation_history, api_call_count=api_call_count,+ response=response,)if _codex_result is not None:return _verdict("return", _codex_result)diff --git a/agent/turn_truncation.py b/agent/turn_truncation.pyindex 5e806746bb5..5d60d1144c4 100644--- a/agent/turn_truncation.py+++ b/agent/turn_truncation.py@@ -450,7 +450,7 @@ _CODEX_REPLAY_KEYS = (def continue_codex_incomplete(agent: Any, assistant_message: Any, finish_reason: str, *, messages: List[Dict[str, Any]],- conversation_history: Any, api_call_count: int,+ conversation_history: Any, api_call_count: int, response: Any = None,) -> Optional[Dict[str, Any]]:"""Codex Responses ``status=incomplete`` continuation (max 3 per turn).@@ -459,8 +459,14 @@ def continue_codex_incomplete(overwritten, because the earlier response holds the only native-compactioncheckpoint) and, when a bare retry would be byte-identical, a user-role nudge — onlyafter an assistant row, to preserve role alternation. Returns ``None`` to continue- the turn loop, or the terminal ``partial`` result once retries are exhausted."""+ the turn loop, or the terminal ``partial`` result once retries are exhausted.++ When ``response`` hit ``max_output_tokens`` with no visible text (reasoning ate the+ whole budget), the next attempt goes out with reasoning off and a doubled output+ cap — the same one-shot overrides the chat-completions length path uses — because+ re-sending the identical budget and effort re-burns the budget identically (#90393)."""from agent.conversation_loop import _CODEX_INCOMPLETE_NUDGE+ from agent.turn_response_check import _codex_finish_reasonagent._codex_incomplete_retries += 1n = agent._codex_incomplete_retries@@ -518,6 +524,12 @@ def continue_codex_incomplete(# Alternation guard: the nudge may only follow an assistant row.if not _already_nudged and _last_msg.get("role") == "assistant":append_message(messages, {"role": "user", "content": _CODEX_INCOMPLETE_NUDGE})+ if not interim_has_content and _codex_finish_reason(response) == "incomplete":+ agent._ephemeral_reasoning_off = True+ if agent.max_tokens:+ agent._ephemeral_max_output_tokens = min(+ agent.max_tokens * (2 ** n), max(32768, agent.max_tokens)+ )if not agent.quiet_mode:agent._vprint(f"{agent.log_prefix}↻ Codex response incomplete; continuing turn ({n}/3)")# Spinner/heartbeat notice: these retries can take minutes and otherwise lookdiff --git a/tests/agent/test_codex_incomplete_budget_escalation.py b/tests/agent/test_codex_incomplete_budget_escalation.pynew file mode 100644index 00000000000..db325949b86--- /dev/null+++ b/tests/agent/test_codex_incomplete_budget_escalation.py@@ -0,0 +1,65 @@+"""Responses-wire ``status=incomplete`` continuation must change the request when reasoning+consumed the whole output budget (#90393): a retry with the same ``max_output_tokens`` and the+same effort re-burns the budget identically and the turn can never converge. A reasoning-only+``status=completed`` response (Codex "still thinking") keeps today's bare-retry behaviour."""++from __future__ import annotations++from types import SimpleNamespace+from unittest.mock import MagicMock++from agent.turn_truncation import continue_codex_incomplete+++def _agent(max_tokens=2000):+ agent = MagicMock()+ agent.max_tokens = max_tokens+ agent.quiet_mode = True+ agent.log_prefix = ""+ agent._codex_incomplete_retries = 0+ agent._ephemeral_reasoning_off = False+ agent._ephemeral_max_output_tokens = None+ agent._build_assistant_message.side_effect = lambda msg, fr: {+ "role": "assistant", "content": msg.content or "", "finish_reason": fr,+ "codex_reasoning_items": msg.codex_reasoning_items,+ }+ agent._interim_assistant_visible_text.return_value = ""+ return agent+++def _reasoning_only_message():+ return SimpleNamespace(+ content="", tool_calls=None, reasoning=None,+ codex_reasoning_items=[{"type": "reasoning", "id": "rs_1", "encrypted_content": "x"}],+ )+++def _run(agent, response):+ messages = [{"role": "user", "content": "write an essay"}]+ return continue_codex_incomplete(+ agent, _reasoning_only_message(), "incomplete", messages=messages,+ conversation_history=None, api_call_count=1, response=response,+ )+++def test_budget_exhausted_empty_fragment_raises_cap_and_drops_reasoning():+ agent = _agent()+ exhausted = SimpleNamespace(+ status="incomplete", incomplete_details={"reason": "max_output_tokens"},+ usage=SimpleNamespace(output_tokens=2000, output_tokens_details={"reasoning_tokens": 1997}),+ )+ assert _run(agent, exhausted) is None+ assert agent._ephemeral_reasoning_off is True+ first_boost = agent._ephemeral_max_output_tokens+ assert first_boost > 2000+ agent._ephemeral_max_output_tokens = None # request builder consumes it+ assert _run(agent, exhausted) is None+ assert agent._ephemeral_max_output_tokens > first_boost+++def test_reasoning_only_completed_response_keeps_bare_retry():+ agent = _agent()+ still_thinking = SimpleNamespace(status="completed", incomplete_details=None)+ assert _run(agent, still_thinking) is None+ assert agent._ephemeral_reasoning_off is False+ assert agent._ephemeral_max_output_tokens is None