Skip to content

The agent that grows with you

fix(agent): reasoning replay verdict resets when the route changes

dsad1798b335c43d
diff --git a/agent/agent_runtime_helpers.py b/agent/agent_runtime_helpers.py
index b2274f64abf..65d47d29335 100644
--- a/agent/agent_runtime_helpers.py
+++ b/agent/agent_runtime_helpers.py
@@ -1373,6 +1373,8 @@ def restore_primary_runtime(agent) -> bool:
provider_fallback_active = bool(getattr(agent, "_provider_fallback_active", False))
try:
_apply_primary_runtime_fields(agent, rt)
+ from agent.turn_recovery import reset_codex_reasoning_replay
+ reset_codex_reasoning_replay(agent)
_restore_runtime_capabilities(agent, rt)
agent._use_prompt_caching = rt["use_prompt_caching"]
# Default to native layout for snapshots predating the native-vs-proxy split.
@@ -2055,6 +2057,7 @@ _SWITCH_SNAPSHOT_FIELDS = (
"_anthropic_client", "_anthropic_api_key", "_anthropic_base_url", "_is_anthropic_oauth",
"_config_context_length", "_reasoning_echo_flag", "runtime_capabilities",
"_credential_pool", "_credential_pool_entry_id",
+ "_codex_reasoning_replay_enabled", "_codex_reasoning_replay_rejected",
)
_MISSING = object()
@@ -2213,6 +2216,8 @@ def _swap_switch_runtime(agent, new_model, new_provider, api_key, base_url, api_
# New api_mode may need a different transport.
if hasattr(agent, "_transport_cache"):
agent._transport_cache.clear()
+ from agent.turn_recovery import reset_codex_reasoning_replay
+ reset_codex_reasoning_replay(agent)
if api_key:
agent.api_key = api_key
# Reload the credential pool on provider change: a pool with a mismatched provider makes
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 5c114ec4be8..7eb42e981b0 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -2148,6 +2148,8 @@ def try_activate_fallback(agent, reason: "FailoverReason | None" = None, reset_a
agent._reasoning_echo_flag = bool(fb.get("reasoning_echo", False))
if hasattr(agent, "_transport_cache"):
agent._transport_cache.clear()
+ from agent.turn_recovery import reset_codex_reasoning_replay
+ reset_codex_reasoning_replay(agent)
agent._fallback_activated = True
_rebind_fallback_credential_pool(agent, fb_provider, fb_model)
diff --git a/agent/turn_recovery.py b/agent/turn_recovery.py
index 4f5f3a737e6..8472141cdb5 100644
--- a/agent/turn_recovery.py
+++ b/agent/turn_recovery.py
@@ -452,6 +452,13 @@ def _is_codex_token_expired(agent: Any, api_error: Exception) -> bool:
return isinstance(reason, str) and reason.strip().lower() == "token_expired"
+def reset_codex_reasoning_replay(agent: Any) -> None:
+ """The replay verdict belongs to the route that earned it: a ``/model`` switch, fallback
+ activation or primary restore starts the new route with replay on (#61552)."""
+ agent._codex_reasoning_replay_enabled = True
+ agent._codex_reasoning_replay_rejected = False
+
+
def _recover_stale_codex_reasoning(
agent: Any, _retry: TurnRetryState, messages: List[Dict[str, Any]], api_messages: Any,
) -> bool:
diff --git a/tests/agent/test_primary_runtime_restore.py b/tests/agent/test_primary_runtime_restore.py
index 02fc9c5ac17..865b88fdfab 100644
--- a/tests/agent/test_primary_runtime_restore.py
+++ b/tests/agent/test_primary_runtime_restore.py
@@ -90,6 +90,24 @@ class TestRestorePrimaryRuntime:
assert agent._fallback_activated is False
assert agent._restore_primary_runtime() is False
+ def test_reasoning_replay_verdict_does_not_follow_the_session_to_another_route(self):
+ """#61552: a replay kill switch tripped on one route must not disable replay on the next."""
+ agent = _make_agent(fallback_model={"provider": "openrouter", "model": "anthropic/claude-sonnet-4"})
+ tripped = (False, True)
+
+ def verdict():
+ return agent._codex_reasoning_replay_enabled, agent._codex_reasoning_replay_rejected
+
+ agent._codex_reasoning_replay_enabled, agent._codex_reasoning_replay_rejected = tripped
+ with patch("agent.auxiliary_client.resolve_provider_client", return_value=(_mock_resolve(), None)):
+ assert agent._try_activate_fallback() is True
+ assert verdict() == (True, False)
+
+ agent._codex_reasoning_replay_enabled, agent._codex_reasoning_replay_rejected = tripped
+ with patch("agent.process_bootstrap.OpenAI", return_value=MagicMock()):
+ assert agent._restore_primary_runtime() is True
+ assert verdict() == (True, False)
+
def test_does_not_label_temporary_model_restore_as_fallback_recovery(self):