The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): a retired authority refuses its own mutators and worker writes
Teknium6f86a36cc1bd
diff --git a/gateway/session_authority.py b/gateway/session_authority.pyindex ddf416d7c7d..914dd088024 100644--- a/gateway/session_authority.py+++ b/gateway/session_authority.py@@ -97,11 +97,14 @@ class SessionAuthority:self.cancel_obligations = set()# Set by profile retirement (unserve): the drain claims no successor after its running turn.self.retiring = False+ self.retired = Falsedef authorize(self, actor, ref, capability):"""Every handler calls this first, so a later ``self.sessions[ref.session_id]`` issafe: a deleted/evicted live entry surfaces here as ``not_found``, not as a KeyErrordeeper in the handler."""+ from gateway.session_authorities import require_serving+ require_serving(self)if actor.profile_id != self.profile_id or ref.profile_id != self.profile_id:raise RuntimeStoreError('profile_mismatch')if capability not in actor.capabilities:diff --git a/gateway/session_worker.py b/gateway/session_worker.pyindex 7a87cd707ed..b62ef1927a8 100644--- a/gateway/session_worker.py+++ b/gateway/session_worker.py@@ -62,6 +62,8 @@ def _verify(connection, ref, params, claim):async def worker_request(connection, ref, params, *, operation):+ from gateway.session_authorities import require_serving+ require_serving(connection.authority) # adopt/persist would write with the released epochif operation == "persist" and isinstance(params.get("operation"), str) and params["operation"].startswith("delegation."):from gateway.session_worker_delegation import worker_delegation_requestreturn await worker_delegation_request(connection, ref, params)diff --git a/tests/gateway/test_managed_turn_shutdown_drain.py b/tests/gateway/test_managed_turn_shutdown_drain.pyindex ee4f7824f5c..9e516e70b68 100644--- a/tests/gateway/test_managed_turn_shutdown_drain.py+++ b/tests/gateway/test_managed_turn_shutdown_drain.py@@ -51,7 +51,7 @@ def test_settlement_stops_a_silent_managed_worker_instead_of_waiting_unbounded(mref = SessionRef('profile', 's')live = SimpleNamespace(task=None, route='r', event_stream=SimpleNamespace(execution={}))authority = SimpleNamespace(profile_id='profile', pending_results={}, waiters={}, sessions={'s': live},- check_approval_generation=lambda *a: None, pending_stops={},+ check_approval_generation=lambda *a: None, pending_stops={}, adopted={},adopt_agent=lambda session_id, generation, worker: None)runner = SimpleNamespace(session_authority=authority)authority.runner = runner # settle also hard-stops in-process turns through the runnerdiff --git a/tests/gateway/test_retired_authority_cron_fence.py b/tests/gateway/test_retired_authority_cron_fence.pyindex 2765d599f22..8e94e298a73 100644--- a/tests/gateway/test_retired_authority_cron_fence.py+++ b/tests/gateway/test_retired_authority_cron_fence.py@@ -32,7 +32,27 @@ def _outbox(home):(box / 'e1.json').write_text(json.dumps({'id': 'e1', 'created_at': 9e12, 'target_handle': 'x'}))+async def _cancel_queued(authority, receipt):+ await authority.cancel_queued(CRON, REF, receipt.admission_id)+++async def _submit_more(authority, receipt):+ await authority.submit(CRON, Submission(request_id='cron:j:2', ref=REF, payload={'text': ''}, intent='queue'))+++async def _worker_adopt(authority, receipt):+ from gateway.session_worker import worker_request+ connection = SimpleNamespace(authority=authority, actor=CRON)+ await worker_request(connection, REF, {}, operation='adopt')++SITES = {+ 'authority.cancel_queued': (_cancel_queued, None, lambda db, home: list_session_admissions(+ db, session_id='s', pending_only=False)[0]['status'] == 'queued'),+ 'authority.submit': (_submit_more, None, lambda db, home: len(list_session_admissions(+ db, session_id='s', pending_only=False)) == 1),+ 'worker.adopt': (_worker_adopt, None, lambda db, home: list_session_admissions(+ db, session_id='s', pending_only=False)[0]['status'] == 'queued'),'cron.cancel': (_cron_cancel, None, lambda db, home: list_session_admissions(db, session_id='s', pending_only=False)[0]['status'] == 'queued'),'bot_relay.outbox.drain': (_relay('outbox', {}), _outbox,diff --git a/tests/gateway/test_run_runtime_hot_serve_cleanup.py b/tests/gateway/test_run_runtime_hot_serve_cleanup.pyindex 1b00c276f99..d70b423680e 100644--- a/tests/gateway/test_run_runtime_hot_serve_cleanup.py+++ b/tests/gateway/test_run_runtime_hot_serve_cleanup.py@@ -198,7 +198,7 @@ async def test_failed_hot_serve_with_a_live_writer_parks_without_aborting_or_re_live = SimpleNamespace(task=writer if name == "beta" else None, route=name,event_stream=SimpleNamespace(execution={"execution_generation": 1}))authority = SimpleNamespace(runner=runner_, profile_id=str(home), sessions={"s": live},- pending_stops={}, retiring=False, hosted_room_service=None)+ pending_stops={}, adopted={}, retiring=False, hosted_room_service=None)registry.add(home, authority, name=name)return authoritydiff --git a/tests/gateway/test_runtime_worker_shutdown.py b/tests/gateway/test_runtime_worker_shutdown.pyindex e9e74781213..63aabbfba99 100644--- a/tests/gateway/test_runtime_worker_shutdown.py+++ b/tests/gateway/test_runtime_worker_shutdown.py@@ -25,6 +25,7 @@ async def test_managed_claims_participate_in_zero_and_nonzero_drain(tmp_path):runner._drain_work_counts = GatewayShutdownMixin._drain_work_counts.__get__(runner)authority.runner = runnerauthority.pending_stops = {}+ authority.adopted = {}try:assert GatewayShutdownMixin._active_work_count(runner) == 1_, timed_out = await GatewayShutdownMixin._drain_active_agents(runner, timeout=0)@@ -60,7 +61,7 @@ async def test_profile_timeout_keeps_authority_until_physical_worker_exits(tmp_pregistry.add(launch, SimpleNamespace(profile_id=str(launch)))agent = SimpleNamespace(hard_interrupt=lambda message: stopped.set())live = SimpleNamespace(task=task, route='beta-route', event_stream=SimpleNamespace(execution={'execution_generation': 2}))- authority = SimpleNamespace(sessions={'s': live}, profile_id=str(beta), pending_stops={},+ authority = SimpleNamespace(sessions={'s': live}, profile_id=str(beta), pending_stops={}, adopted={},waiters={}, native_waiters=set(), agent=lambda ref: agent, _turn_workers={1: (SimpleNamespace(worker_done=done), [agent])})registry.add(beta, authority, name='beta')runner = SimpleNamespace(session_authorities=registry, session_runtime_descriptor={},@@ -95,7 +96,7 @@ async def test_whole_runtime_settle_logs_late_work_and_finishes_shutdown(monkeyptask = asyncio.create_task(late.wait())agent = SimpleNamespace(hard_interrupt=lambda message=None: None)live = SimpleNamespace(task=task, route='r', event_stream=SimpleNamespace(execution={}))- authority = SimpleNamespace(sessions={'s': live}, _managed_workers={}, pending_stops={}, profile_id='p',+ authority = SimpleNamespace(sessions={'s': live}, _managed_workers={}, pending_stops={}, adopted={}, profile_id='p',_turn_workers={1: (SimpleNamespace(worker_done=wedged), [agent])})revoked, stopped = [], []runner = SimpleNamespace(session_authority=authority, _running_agents={},