The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(cron): retain profile secret scope through delivery
ghosty9307f6518b6d62
diff --git a/cron/scheduler.py b/cron/scheduler.pyindex c69850f5d06..575d60b26c6 100644--- a/cron/scheduler.py+++ b/cron/scheduler.py@@ -7158,6 +7158,13 @@ def _run_one_job_body(# computation and the post-delivery "alerted" transition.incident_acked = Falsefailure_incident_id = None+ from agent.secret_scope import (+ build_profile_secret_scope,+ reset_secret_scope,+ set_secret_scope,+ )++ _scope_token = Nonetry:# Pre-run dispatch claim (issue #38758): atomically commit a finite# one-shot's dispatch BEFORE its side effect runs, so a tick that dies@@ -7182,18 +7189,11 @@ def _run_one_job_body(# becomes running only immediately before the actual run.mark_execution_running(execution_id)- # Run the job under the profile's secret scope. get_secret() fails- # closed outside a scope once profile isolation is in play (multiple- # gateway profiles / room→profile multiplexing), and cron fires from- # the ticker thread where no per-turn scope is installed — so- # resolve_runtime_provider() raised UnscopedSecretError before model- # selection, breaking every cron job. Mirrors the per-turn pattern in- # gateway/run.py (_profile_runtime_scope).- from agent.secret_scope import (- build_profile_secret_scope,- reset_secret_scope,- set_secret_scope,- )+ # Run and deliver under the profile's secret scope. get_secret() fails+ # closed outside a scope once profile isolation is active, and cron+ # fires from a ticker thread with no per-turn scope. Delivery adapters+ # can also resolve credentials, so resetting after run_job would leave+ # _deliver_result unscoped. Mirrors gateway/run.py's per-turn pattern._scope_token = set_secret_scope(build_profile_secret_scope(_get_hermes_home())@@ -7229,8 +7229,7 @@ def _run_one_job_body(for _deferred_agent in _deferred_agents:_teardown_cron_agent(_deferred_agent, job["id"])raise- finally:- reset_secret_scope(_scope_token)+ # The outer finally resets the scope after delivery and bookkeeping.if _fire_claim_ownership_lost():for _deferred_agent in _deferred_agents:@@ -7618,6 +7617,9 @@ def _run_one_job_body(if not isinstance(e, Exception):raisereturn False+ finally:+ if _scope_token is not None:+ reset_secret_scope(_scope_token)def _notify_provider_jobs_changed() -> None:diff --git a/tests/cron/test_run_one_job.py b/tests/cron/test_run_one_job.pyindex 6d370b9ff0f..a3b8bb425a9 100644--- a/tests/cron/test_run_one_job.py+++ b/tests/cron/test_run_one_job.py@@ -329,12 +329,12 @@ def test_run_one_job_keyboard_interrupt_skips_delivery_and_reraises(monkeypatch)def test_run_one_job_installs_secret_scope_under_multiplex(monkeypatch, tmp_path):"""Regression: under profile isolation (multiplex active), run_one_job must- execute run_job inside a profile secret scope so credential reads- (resolve_runtime_provider -> get_secret) don't fail-close with- UnscopedSecretError, and must tear the scope down afterward.+ keep one profile secret scope active through execution and delivery so+ credential reads do not fail closed or fall through to another profile,+ then tear the scope down after the complete job lifecycle.- Behavior contract: a scope is present during run_job and absent after,- regardless of the concrete secret values.+ Behavior contract: the same scope is present during run_job and+ _deliver_result, and no scope remains after run_one_job returns."""from agent import secret_scope as ss@@ -343,6 +343,7 @@ def test_run_one_job_installs_secret_scope_under_multiplex(monkeypatch, tmp_pathmonkeypatch.setattr(s, "_get_hermes_home", lambda: tmp_path)scope_during_run = {}+ scope_during_delivery = {}def fake_run_job(job, *, defer_agent_teardown=None, **kw):# This is where resolve_runtime_provider() would read a secret. Prove a@@ -351,9 +352,14 @@ def test_run_one_job_installs_secret_scope_under_multiplex(monkeypatch, tmp_pathscope_during_run["base_url"] = ss.get_secret("OPENROUTER_BASE_URL")return (True, "out", "final", None)+ def fake_deliver(*args, **kwargs):+ scope_during_delivery["scope"] = ss.current_secret_scope()+ scope_during_delivery["base_url"] = ss.get_secret("OPENROUTER_BASE_URL")+ return None+monkeypatch.setattr(s, "run_job", fake_run_job)monkeypatch.setattr(s, "save_job_output", lambda jid, out: f"/tmp/{jid}.txt")- monkeypatch.setattr(s, "_deliver_result", lambda *a, **k: None)+ monkeypatch.setattr(s, "_deliver_result", fake_deliver)monkeypatch.setattr(s, "mark_job_run", lambda *a, **k: None)ss.set_multiplex_active(True)@@ -363,10 +369,12 @@ def test_run_one_job_installs_secret_scope_under_multiplex(monkeypatch, tmp_pathss.set_multiplex_active(False)assert ok is True- # Scope was installed during run_job and the profile secret resolved.+ # The same profile scope covered both execution and delivery.assert scope_during_run["scope"] is not Noneassert scope_during_run["base_url"] == "https://openrouter.ai/api/v1"- # And it was torn down after run_one_job returned (no leak).+ assert scope_during_delivery["scope"] == scope_during_run["scope"]+ assert scope_during_delivery["base_url"] == "https://openrouter.ai/api/v1"+ # And it was torn down after the full lifecycle returned (no leak).assert ss.current_secret_scope() is None