Skip to content

The agent that grows with you

test(cron): bind the cache-scope admission through the production helper

Teknium3ad7f67f5a84
diff --git a/gateway/session_cron.py b/gateway/session_cron.py
index b680bd7662f..e3c78a25000 100644
--- a/gateway/session_cron.py
+++ b/gateway/session_cron.py
@@ -42,6 +42,12 @@ def current_execution():
return _execution.get()
+def bind_execution(authority, session_id, job_id, execution_id):
+ """Bind this context's cron admission ``(authority, session_id, job_id, execution_id)``, the
+ layout ``current_execution()`` readers index; returns the ``ContextVar`` reset token."""
+ return _execution.set((authority, session_id, job_id, execution_id))
+
+
def _actor(authority, actor):
if actor is None:
return Principal(CRON_PRINCIPAL_ID, authority.profile_id,
@@ -234,7 +240,7 @@ async def execute(authority, ref, row, policy):
execution_id = scheduler_execution_id(data['cron_job']['id'], row['request_id'])
# The frozen snapshot predates the fire's ledger row; the run's progress stamps need its id.
job = dict(data['cron_job'], execution_id=execution_id)
- token = _execution.set((authority, ref.session_id, job['id'], execution_id))
+ token = bind_execution(authority, ref.session_id, job['id'], execution_id)
try:
db_path = await asyncio.to_thread(Path(authority.db.db_path).resolve)
with _profile_runtime_scope(db_path.parent):
diff --git a/tests/agent/transports/test_codex_transport_cron_scope.py b/tests/agent/transports/test_codex_transport_cron_scope.py
index ff32b391488..ceea5d679d6 100644
--- a/tests/agent/transports/test_codex_transport_cron_scope.py
+++ b/tests/agent/transports/test_codex_transport_cron_scope.py
@@ -11,43 +11,40 @@ def transport():
return get_transport("codex_responses")
-def test_cache_key_stable_for_unified_cron_session_ids(transport):
- """A scheduler admission, not the opaque local physical id, scopes cron cache reuse."""
- from gateway.session_cron import _execution
+def execution_context():
+ """Binds through ``session_cron.bind_execution`` -- the call ``execute()`` makes -- so the
+ admission layout the transport reads is the production one."""
+ from gateway.session_cron import _execution, bind_execution
+ token = _execution.set(None)
+ try:
+ yield lambda sid, job_id, execution_id="fire": bind_execution(object(), sid, job_id, execution_id)
+ finally:
+ _execution.reset(token)
+
+
+def test_cache_key_stable_for_unified_cron_session_ids(transport, execution_context):
+ """A scheduler admission, not the opaque local physical id, scopes cron cache reuse."""
messages = [{"role": "user", "content": "Hi"}]
def build(sid):
return transport.build_kwargs(model="gpt-5.4", messages=messages, tools=[], session_id=sid)
- token = _execution.set((object(), "local-first-fire", "job42", "fire-1"))
- try:
- first = build("local-first-fire")
- _execution.set((object(), "local-second-fire", "job42", "fire-2"))
- second = build("local-second-fire")
- # A session that is not the bound admission (e.g. a delegated child) keeps its own scope.
- child = build("local-child-of-fire")
- _execution.set((object(), "local-other-job", "job99", "fire-3"))
- other = build("local-other-job")
- finally:
- _execution.reset(token)
+ execution_context("local-first-fire", "job42", "fire-1")
+ first = build("local-first-fire")
+ execution_context("local-second-fire", "job42", "fire-2")
+ second = build("local-second-fire")
+ # A session that is not the bound admission (e.g. a delegated child) keeps its own scope.
+ child = build("local-child-of-fire")
+ execution_context("local-other-job", "job99", "fire-3")
+ other = build("local-other-job")
assert first["prompt_cache_key"] == second["prompt_cache_key"]
assert first["prompt_cache_key"] != other["prompt_cache_key"]
assert child["prompt_cache_key"] != first["prompt_cache_key"]
-def execution_context():
- from gateway.session_cron import _execution
-
- token = _execution.set(None)
- try:
- yield _execution
- finally:
- _execution.reset(token)
-
-
@pytest.mark.parametrize("override", [None, "caller-cache-key"])
def test_cron_backend_headers_and_legacy_scope(transport, execution_context, override):
"""Logical routing is stable while physical headers and request content stay intact."""
@@ -76,7 +73,7 @@ def test_cron_backend_headers_and_legacy_scope(transport, execution_context, ove
legacy = build(legacy_sid)
assert _cache_scope_from_session_id(legacy_sid) == "cron_job42"
for sid in ("local-first-fire", "local-second-fire"):
- execution_context.set((object(), sid, "job42", "fire"))
+ execution_context(sid, "job42")
current = build(sid)
assert _cache_scope_from_session_id(sid) == "cron_job42"
assert current["prompt_cache_key"] == legacy["prompt_cache_key"]
@@ -99,7 +96,7 @@ def test_cron_compression_rotation_resolves_admitted_scope(transport, execution_
from hermes_state import SessionDB
root, rotated = "local-admitted-fire", "local-rotated-fire"
- execution_context.set((object(), root, "job42", "fire-1"))
+ execution_context(root, "job42", "fire-1")
with SessionDB(db_path=tmp_path / "state.db") as db:
db.create_session(root, source="cli")
agent = SimpleNamespace(session_id=root, _session_db=db)
@@ -137,5 +134,5 @@ def test_unadmitted_or_jobless_sessions_keep_scope(execution_context, sid, admis
from agent.transports.codex import _cache_scope_from_session_id
if admission is not None:
- execution_context.set((object(), *admission, "fire-1"))
+ execution_context(*admission, "fire-1")
assert _cache_scope_from_session_id(sid) == sid