Skip to content

The agent that grows with you

fix: recover a persisted Codex session that loops on 401 token_expired

teknium1283a4c7ae94d
diff --git a/agent/turn_recovery.py b/agent/turn_recovery.py
index b7dc891a4dd..c411e1ad826 100644
--- a/agent/turn_recovery.py
+++ b/agent/turn_recovery.py
@@ -380,6 +380,19 @@ def _refresh_credentials_after_401(
_print_anthropic_401_diagnostics(agent, agent._anthropic_api_key)
return False
+
+def _is_lingering_codex_token_expired(agent: Any, api_error: Exception, _retry: TurnRetryState) -> bool:
+ """401 ``token_expired`` that survived the one-shot Codex OAuth refresh (#88510). The Codex
+ backend rejects a stale replayed ``encrypted_content`` blob with this auth signature, so a
+ persisted session loops on "sign in again" while a fresh session on the same bearer works.
+ Once the credential path has had its turn, the caller treats it like
+ ``invalid_encrypted_content`` — but only while cached reasoning items remain to strip."""
+ if getattr(api_error, "status_code", None) != 401 or not _retry.codex_auth_retry_attempted:
+ return False
+ reason = agent._extract_api_error_context(api_error).get("reason")
+ return isinstance(reason, str) and reason.strip().lower() == "token_expired"
+
+
def _recover_format_errors(
agent: Any, api_error: Exception, classified: Any, _retry: TurnRetryState,
messages: List[Dict[str, Any]], api_messages: Any,
@@ -405,10 +418,14 @@ def _recover_format_errors(
)
return True
- # 400 ``invalid_encrypted_content`` on a stale ``codex_reasoning_items`` blob:
+ # 400 ``invalid_encrypted_content`` on a stale ``codex_reasoning_items`` blob — or the same
+ # rejection wearing a 401 ``token_expired`` after the credential refresh changed nothing:
# disable replay for the session, strip cached items, retry once.
if (
- classified.reason == FailoverReason.invalid_encrypted_content
+ (
+ classified.reason == FailoverReason.invalid_encrypted_content
+ or _is_lingering_codex_token_expired(agent, api_error, _retry)
+ )
and not _retry.invalid_encrypted_content_retry_attempted
and agent.api_mode == "codex_responses"
and bool(getattr(agent, "_codex_reasoning_replay_enabled", True))
diff --git a/tests/agent/test_codex_token_expired_replay_recovery.py b/tests/agent/test_codex_token_expired_replay_recovery.py
new file mode 100644
index 00000000000..da19e83fc4e
--- /dev/null
+++ b/tests/agent/test_codex_token_expired_replay_recovery.py
@@ -0,0 +1,105 @@
+"""A persisted Codex session must not loop on 401 ``token_expired`` when the credential is fine.
+
+Issue #88510: the Codex backend rejects a stale replayed ``encrypted_content`` blob with the
+auth signature (401 ``token_expired``), so a resumed session failed on every prompt while a
+fresh session on the same bearer worked. After the one-shot OAuth refresh changed nothing,
+``recover_after_classification`` must strip cached ``codex_reasoning_items`` and retry once,
+exactly like the 400 ``invalid_encrypted_content`` path; a 401 without cached reasoning is a
+real expiry and stays on the credential path.
+"""
+from __future__ import annotations
+
+import pytest
+
+from agent.error_classifier import FailoverReason, classify_api_error
+from agent.turn_recovery import recover_after_classification
+from agent.turn_retry_state import TurnRetryState
+
+_TOKEN_EXPIRED = "Provided authentication token is expired. Please try signing in again."
+
+
+class _Codex401(Exception):
+ def __init__(self, code: str | None = "token_expired"):
+ super().__init__(f"HTTP 401: {_TOKEN_EXPIRED}")
+ self.status_code = 401
+ self.message = _TOKEN_EXPIRED
+ err = {"message": _TOKEN_EXPIRED, "type": "invalid_request_error"}
+ if code:
+ err["code"] = code
+ self.body = {"error": err}
+
+
+class _Agent:
+ """Codex OAuth agent whose refresh path yields the same token (nothing to adopt)."""
+
+ log_prefix = ""
+ provider = "openai-codex"
+ api_mode = "codex_responses"
+ base_url = "https://chatgpt.com/backend-api/codex"
+ model = "gpt-5.3-codex"
+ api_key = "same-bearer"
+ _codex_reasoning_replay_enabled = True
+
+ def __init__(self):
+ self.refresh_calls = 0
+
+ def _recover_with_credential_pool(self, **kwargs):
+ return False, False
+
+ def _try_refresh_codex_client_credentials(self, *, force=True):
+ self.refresh_calls += 1
+ return False
+
+ def _extract_api_error_context(self, error):
+ from agent.agent_runtime_helpers import extract_api_error_context
+ return extract_api_error_context(error)
+
+ def _disable_codex_reasoning_replay(self, messages=None):
+ from run_agent import AIAgent
+ return AIAgent._disable_codex_reasoning_replay(self, messages)
+
+ def __getattr__(self, name):
+ return lambda *args, **kwargs: None
+
+
+def _cached_history(n: int = 2):
+ return [
+ {"role": "assistant", "content": f"answer {i}",
+ "codex_reasoning_items": [{"type": "reasoning", "id": f"rs_{i}", "encrypted_content": "gAAA" * 8}]}
+ for i in range(n)
+ ]
+
+
+def _recover(agent, err, retry, messages):
+ classified = classify_api_error(err, provider=agent.provider, model=agent.model)
+ assert classified.reason == FailoverReason.auth # never reclassified
+ return recover_after_classification(
+ agent, err, classified, retry, status_code=401,
+ error_context=agent._extract_api_error_context(err), messages=messages, api_messages=list(messages),
+ )
+
+
+def test_lingering_token_expired_strips_cached_reasoning_once_after_refresh():
+ agent, retry, messages = _Agent(), TurnRetryState(), _cached_history()
+
+ retried, _ = _recover(agent, _Codex401(), retry, messages)
+
+ assert retried is True
+ assert agent.refresh_calls == 1 # the credential path ran first and changed nothing
+ assert agent._codex_reasoning_replay_enabled is False
+ assert not any("codex_reasoning_items" in m for m in messages)
+ # A second identical 401 in the same turn is a real auth failure: no second strip.
+ assert _recover(agent, _Codex401(), retry, messages) == (False, False)
+
+
[email protected]("err, history", [
+ (_Codex401(), []), # real expiry: nothing cached to strip
+ (_Codex401(code=None), _cached_history()), # generic 401: not the token_expired signature
+])
+def test_token_expired_without_cached_reasoning_stays_on_auth_path(err, history):
+ agent, retry = _Agent(), TurnRetryState()
+
+ assert _recover(agent, err, retry, history) == (False, False)
+ assert agent.refresh_calls == 1
+ assert agent._codex_reasoning_replay_enabled is True
+ assert retry.invalid_encrypted_content_retry_attempted is False