The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(codex): strip unsupported cache retention at wire
Falko8e2949495a62
diff --git a/agent/codex_runtime.py b/agent/codex_runtime.pyindex f01d042b53f..de273f78626 100644--- a/agent/codex_runtime.py+++ b/agent/codex_runtime.py@@ -1305,6 +1305,37 @@ def _consume_codex_event_stream(return final+def _sanitize_consumer_codex_request(+ agent: Any,+ request: dict[str, Any],+) -> dict[str, Any]:+ """Drop fields the ChatGPT OAuth Codex endpoint does not accept.++ This guard intentionally lives at the final wire boundary, after Relay or+ other request middleware has had a chance to transform the request. The+ normal transport builder already omits ``prompt_cache_retention`` for this+ endpoint, but a late mutation must not be allowed to turn a valid tool+ follow-up into a non-retryable HTTP 400.++ Explicit ``request_overrides`` are subject to the same endpoint contract:+ unsupported retention is dropped with a warning instead of being sent and+ rejected by the provider.+ """+ sanitized = dict(request)+ backend_predicate = getattr(agent, "_is_codex_backend", None)+ is_consumer_codex = (+ bool(backend_predicate()) if callable(backend_predicate) else False+ )+ if is_consumer_codex and "prompt_cache_retention" in sanitized:+ sanitized.pop("prompt_cache_retention", None)+ logger.warning(+ "Dropped unsupported prompt_cache_retention at consumer Codex "+ "wire boundary (model=%s).",+ sanitized.get("model", getattr(agent, "model", "unknown")),+ )+ return sanitized++def run_codex_stream(agent, api_kwargs: dict, client: Any = None, on_first_delta=None):"""Execute one streaming Responses API request and return the final response.@@ -1347,7 +1378,10 @@ def run_codex_stream(agent, api_kwargs: dict, client: Any = None, on_first_deltawriter_token = {"value": None}def _open_codex_stream(next_api_kwargs: dict[str, Any]):- stream_kwargs = dict(next_api_kwargs)+ stream_kwargs = _sanitize_consumer_codex_request(+ agent,+ next_api_kwargs,+ )stream_kwargs["stream"] = Truereturn active_client.responses.create(**stream_kwargs)diff --git a/tests/agent/test_relay_llm.py b/tests/agent/test_relay_llm.pyindex 433ac634c6f..213fd86aea8 100644--- a/tests/agent/test_relay_llm.py+++ b/tests/agent/test_relay_llm.py@@ -207,6 +207,33 @@ def test_relay_metadata_preserves_provider_name():}+def test_provider_request_overlays_interceptor_added_codex_field():+ """Relay rewrites may introduce provider fields absent from the original."""+ original = {"model": "gpt-5.6-sol", "input": "hello"}+ relay_request_body = relay_llm._relay_request_body(+ original,+ {"api_mode": "codex_responses"},+ )+ intercepted = SimpleNamespace(+ content={+ **relay_request_body,+ "prompt_cache_retention": "24h",+ },+ headers={},+ )++ provider_request = relay_llm._provider_request(+ original,+ intercepted,+ relay_request_body=relay_request_body,+ codec_baseline_body=dict(relay_request_body),+ metadata={"api_mode": "codex_responses"},+ )++ assert "prompt_cache_retention" not in original+ assert provider_request["prompt_cache_retention"] == "24h"++def test_stream_uses_rewritten_request_and_post_intercept_chunks(relay_turn):relay, turn = relay_turncaptured_requests = []diff --git a/tests/run_agent/test_run_agent_codex_responses.py b/tests/run_agent/test_run_agent_codex_responses.pyindex 5de3418a340..a67091024b9 100644--- a/tests/run_agent/test_run_agent_codex_responses.py+++ b/tests/run_agent/test_run_agent_codex_responses.py@@ -537,6 +537,71 @@ def _build_xai_agent_with_slash_enum_tool(monkeypatch):+def test_run_codex_stream_strips_relay_added_retention_at_consumer_wire(+ monkeypatch,+ caplog,+):+ """A Relay-added unsupported field cannot reach consumer ChatGPT Codex."""+ from agent import relay_llm++ agent = _build_agent(monkeypatch)+ captured = {}++ def _fake_create(**kwargs):+ captured.update(kwargs)+ return _FakeCreateStream([+ SimpleNamespace(+ type="response.completed",+ response=SimpleNamespace(status="completed"),+ )+ ])++ agent.client = SimpleNamespace(+ responses=SimpleNamespace(create=_fake_create),+ )+ original = _codex_request_kwargs()+ relay_request_body = relay_llm._relay_request_body(+ original,+ {"api_mode": "codex_responses"},+ )+ relayed = relay_llm._provider_request(+ original,+ SimpleNamespace(+ content={+ **relay_request_body,+ "prompt_cache_retention": "24h",+ },+ headers={},+ ),+ relay_request_body=relay_request_body,+ codec_baseline_body=dict(relay_request_body),+ metadata={"api_mode": "codex_responses"},+ )+ assert relayed["prompt_cache_retention"] == "24h"++ with caplog.at_level("WARNING", logger="agent.codex_runtime"):+ agent._run_codex_stream(relayed)++ assert "prompt_cache_retention" not in captured+ assert any(+ "Dropped unsupported prompt_cache_retention at consumer Codex wire boundary"+ in record.message+ for record in caplog.records+ )+++def test_consumer_codex_wire_guard_preserves_compatible_endpoint_retention():+ from agent.codex_runtime import _sanitize_consumer_codex_request++ agent = SimpleNamespace(_is_codex_backend=lambda: False)+ request = {"model": "openai.gpt-5.5", "prompt_cache_retention": "24h"}++ sanitized = _sanitize_consumer_codex_request(agent, request)++ assert sanitized["prompt_cache_retention"] == "24h"+ assert sanitized is not request++def test_run_codex_stream_returns_collected_items_when_stream_ends_without_terminal(monkeypatch):"""The event-driven path tolerates streams that end without a terminal frame.