Skip to content

The agent that grows with you

fix(gateway): settle every pending observer when a profile retires

Teknium81bdbe76a13b
diff --git a/gateway/run_runtime.py b/gateway/run_runtime.py
index 9974bfc4496..93911466500 100644
--- a/gateway/run_runtime.py
+++ b/gateway/run_runtime.py
@@ -279,6 +279,14 @@ async def _retire_profile_authority(authority, timeout=None):
if rest:
await asyncio.gather(*rest, return_exceptions=True)
unbind_owner(authority)
+ # No drain is left to answer an observer (a session_busy pause keeps API/webhook/Bot ones
+ # waiting): release them all. Their rows stay queued for the next owner, so nothing is resent.
+ from hermes_state_runtime import RuntimeStoreError
+ authority.native_waiters.clear()
+ for waiter in authority.waiters.values():
+ if not waiter.done():
+ waiter.set_exception(RuntimeStoreError('runtime_draining'))
+ authority.waiters.clear()
return True
diff --git a/tests/gateway/test_retire_settles_waiters.py b/tests/gateway/test_retire_settles_waiters.py
new file mode 100644
index 00000000000..0e676c34a3b
--- /dev/null
+++ b/tests/gateway/test_retire_settles_waiters.py
@@ -0,0 +1,48 @@
+"""A successful profile retirement settles every observer still waiting on a queued admission:
+API/webhook/Bot futures fail with runtime_draining instead of hanging, the native set is cleared,
+and the durable row stays queued for the profile's next owner (never resent, never dropped)."""
+import asyncio
+
+import pytest
+
+from gateway import run_runtime
+from hermes_state_runtime import RuntimeStoreError, list_session_admissions
+from tests.gateway.test_profile_retire_stops_turns import ACTOR, REF, _Agent, _authority
+from gateway.session_contract import Submission
+
+
+async def test_successful_retirement_fails_pending_waiters_and_keeps_rows_queued(tmp_path, monkeypatch):
+ from gateway import session_finite
+ agent = _Agent()
+ db, authority, execute, _returned = _authority(tmp_path, agent)
+ monkeypatch.setattr(session_finite, 'execute_finite_admission', execute)
+ monkeypatch.setattr(run_runtime, 'TURN_SETTLE_SECONDS', 1.0, raising=False)
+ monkeypatch.setattr('gateway.session_cron.unbind_owner', lambda authority: None)
+ try:
+ with db:
+ await authority.submit(ACTOR, Submission(request_id='running', ref=REF, payload={'text': 'a'}, intent='queue'))
+ await asyncio.wait_for(asyncio.to_thread(agent.started.wait, 5), 6)
+ # A second session whose drain paused 'session_busy' (a live worker held its head):
+ # its API/webhook/Bot observer deliberately keeps waiting, and no drain is left to
+ # release it when retirement refuses the next claim.
+ authority.sessions['t'] = type(authority.sessions['s'])(authority.sessions['s'].source, 'route-t')
+ db.create_session('t', source='test')
+ monkeypatch.setattr(authority, '_schedule', lambda ref: None)
+ follower = await authority.submit(ACTOR, Submission(
+ request_id='follower', ref=REF.__class__('owned', 't'), payload={'text': 'b'}, intent='queue'))
+ waiter = authority.waiters.setdefault(follower.admission_id, asyncio.get_running_loop().create_future())
+ authority.native_waiters.add('messaging-delivery')
+
+ assert await run_runtime._retire_profile_authority(authority) is True
+
+ assert waiter.done(), 'observer left pending after retirement'
+ with pytest.raises(RuntimeStoreError, match='runtime_draining'):
+ waiter.result()
+ assert not authority.waiters and not authority.native_waiters
+ rows = {r['request_id']: r for r in list_session_admissions(db, session_id='t', pending_only=False)}
+ assert rows['follower']['status'] == 'queued' # durable: the next owner runs it
+ finally:
+ agent.release.set()
+ await asyncio.sleep(.05)
+ db.close()
diff --git a/tests/gateway/test_run_runtime_hot_serve_cleanup.py b/tests/gateway/test_run_runtime_hot_serve_cleanup.py
index 46bcd24b863..1b00c276f99 100644
--- a/tests/gateway/test_run_runtime_hot_serve_cleanup.py
+++ b/tests/gateway/test_run_runtime_hot_serve_cleanup.py
@@ -54,6 +54,7 @@ async def test_failed_hot_serve_retires_session_and_bot_recovery_tasks(monkeypat
profile_id="default",
sessions={"s": live},
waiters={},
+ native_waiters=set(),
hosted_room_service=None,
)
diff --git a/tests/gateway/test_runtime_worker_shutdown.py b/tests/gateway/test_runtime_worker_shutdown.py
index 39a1d97415e..0230fafc956 100644
--- a/tests/gateway/test_runtime_worker_shutdown.py
+++ b/tests/gateway/test_runtime_worker_shutdown.py
@@ -61,7 +61,7 @@ async def test_profile_timeout_keeps_authority_until_physical_worker_exits(tmp_p
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={},
- agent=lambda ref: agent, _turn_workers={1: (SimpleNamespace(worker_done=done), [agent])})
+ 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={},
session_ticket_store=SimpleNamespace(profile_ids=registry.profile_ids()))