Skip to content

The agent that grows with you

fix(cron): stabilize Codex cache scope for local sessions

KoNit-K1bafe3507531
diff --git a/agent/transports/codex.py b/agent/transports/codex.py
index 1d86a875770..7ecdcddbbde 100644
--- a/agent/transports/codex.py
+++ b/agent/transports/codex.py
@@ -32,7 +32,22 @@ def _cache_scope_from_session_id(session_id: Optional[str]) -> str:
"""Normalize a physical session_id into a stable logical cache scope."""
sid = str(session_id or "")
match = _CRON_SESSION_ID_RE.match(sid)
- return match.group(1) if match else sid
+ if match:
+ return match.group(1)
+ # The unified scheduler admits cron fires into a ``local-*`` session. Its
+ # physical id is intentionally opaque and changes on every fire, so recover
+ # the stable job scope from the bound scheduler admission instead.
+ try:
+ from gateway.session_cron import current_execution
+
+ execution = current_execution()
+ if execution is not None and str(execution[1]) == sid:
+ job_id = str(execution[2] or "").strip()
+ if job_id:
+ return f"cron_{job_id}"
+ except Exception:
+ logger.debug("cron prompt-cache scope lookup failed", exc_info=True)
+ return sid
def _bounded_prompt_cache_key(value: Any) -> Optional[str]:
diff --git a/tests/agent/transports/test_codex_transport.py b/tests/agent/transports/test_codex_transport.py
index 95fc50046db..1cd50a8e91b 100644
--- a/tests/agent/transports/test_codex_transport.py
+++ b/tests/agent/transports/test_codex_transport.py
@@ -207,6 +207,30 @@ class TestCodexBuildKwargs:
)
assert kw1["prompt_cache_key"] == kw2["prompt_cache_key"]
+ def test_cache_key_stable_for_unified_cron_session_ids(self, transport):
+ """A scheduler admission, not the local physical id, scopes cron cache reuse."""
+ from gateway.session_cron import _execution
+
+ messages = [{"role": "user", "content": "Hi"}]
+ token = _execution.set((object(), "local-first-fire", "job42", "fire-1"))
+ try:
+ first = transport.build_kwargs(
+ model="gpt-5.4", messages=messages, tools=[], session_id="local-first-fire",
+ )
+ _execution.set((object(), "local-second-fire", "job42", "fire-2"))
+ second = transport.build_kwargs(
+ model="gpt-5.4", messages=messages, tools=[], session_id="local-second-fire",
+ )
+ _execution.set((object(), "local-other-job", "job99", "fire-3"))
+ other = transport.build_kwargs(
+ model="gpt-5.4", messages=messages, tools=[], session_id="local-other-job",
+ )
+ finally:
+ _execution.reset(token)
+
+ assert first["prompt_cache_key"] == second["prompt_cache_key"]
+ assert first["prompt_cache_key"] != other["prompt_cache_key"]
+
def test_cache_key_differs_across_unrelated_sessions(self, transport):
"""#78941: two unrelated sessions (different users/conversations)
sharing the same static prefix must NOT collapse onto the same