Skip to content

The agent that grows with you

fix: retain queued inputs when shutdown crosses authorization awaits

Teknium733dde1cc9a8
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index 69c8e18e23b..93783b92bf8 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -71,6 +71,7 @@ async def drain_gateway_runtime(runner):
descriptor = getattr(runner, 'session_runtime_descriptor', None)
if descriptor is None:
return
+ runner._draining = True
descriptor.update(state='draining', capabilities=[])
# Withdraw the public ingress callback without disconnecting egress needed
# by already admitted work. Base adapters refuse before stamping acceptance.
diff --git a/gateway/session_authority.py b/gateway/session_authority.py
index 9bbeefcb637..a80ac4eefaf 100644
--- a/gateway/session_authority.py
+++ b/gateway/session_authority.py
@@ -55,6 +55,10 @@ class SessionAuthority:
if ref.session_id not in self.sessions:
raise RuntimeStoreError('not_found')
+ def _require_admission_open(self):
+ if self.runner._draining:
+ raise RuntimeStoreError('runtime_draining')
+
def register(self, source):
entry = self.runner.session_store.get_or_create_session(source)
sid = entry.session_id
@@ -124,7 +128,9 @@ class SessionAuthority:
"""Await current connector policy, then commit before ACK or scheduling execution."""
import json
from gateway.session_envelope import prepare_native, restore_native
+ self._require_admission_open()
payload = await prepare_native(self.runner, event)
+ self._require_admission_open()
source = restore_native(payload).source
ref = self.register(source)
identity = json.dumps([source.profile, source.platform.value, source.chat_id,
@@ -159,6 +165,7 @@ class SessionAuthority:
if 'native_text_v1' not in row['payload']:
raise RuntimeStoreError('invalid_params')
await check_native_route(self.runner, row['payload'], sid, available_source, adapter)
+ self._require_admission_open()
self.sessions.setdefault(sid, LiveSession(source, route))
if any(row['status'] == 'unknown' for row in rows):
raise RuntimeStoreError('unknown_execution')
@@ -170,6 +177,7 @@ class SessionAuthority:
async def submit(self, actor: Principal, request: Submission):
self.authorize(actor, request.ref, 'session:submit')
+ self._require_admission_open()
if request.intent != 'queue' or set(request.payload) != {'text'} or not isinstance(request.payload['text'], str):
raise RuntimeStoreError('invalid_params')
row = admit_session_input(self.db, epoch=self.epoch, principal_id=actor.subject,
@@ -265,6 +273,7 @@ class SessionAuthority:
current = get_session_admission(self.db, admission_id=first['admission_id'])
if current is None or current['status'] != 'queued':
continue
+ self._require_admission_open()
row = claim_session_input(self.db, epoch=self.epoch, session_id=ref.session_id)
except RuntimeStoreError as exc:
import logging
diff --git a/tests/gateway/fixtures/native_role_fences.py b/tests/gateway/fixtures/native_role_fences.py
index 1ac7542157d..350e140f75b 100644
--- a/tests/gateway/fixtures/native_role_fences.py
+++ b/tests/gateway/fixtures/native_role_fences.py
@@ -60,6 +60,22 @@ async def fences(runner, authority, adapter, guild, event, peer, mode):
finally:
guild.hook = None
runner.adapters[Platform.DISCORD] = adapter
+ before_drain = rows(second.ref.session_id)
+ guild.hook = lambda: setattr(runner, '_draining', True)
+ try:
+ try:
+ await accept(event('shutdown-during-auth'))
+ except RuntimeStoreError as exc:
+ assert exc.reason == 'runtime_draining', exc.reason
+ else:
+ failures.append('new input accepted after shutdown began during lookup')
+ runner._draining = False
+ await authority._drain(second.ref)
+ if rows(second.ref.session_id) != before_drain:
+ failures.append('shutdown consumed queued input after authorization await')
+ finally:
+ guild.hook = None
+ runner._draining = False
assert not failures, failures
assert len(rows(second.ref.session_id)) == 2
print(json.dumps({'mode': mode, 'rows': rows(second.ref.session_id), 'model_calls': 0}), flush=True)