Skip to content

The agent that grows with you

fix(gateway): restore a closed local session for watch notices and route wakes

unsupportedpastelscf0f210715d1
diff --git a/gateway/run_notifications.py b/gateway/run_notifications.py
index 538123325f1..5654cb7835b 100644
--- a/gateway/run_notifications.py
+++ b/gateway/run_notifications.py
@@ -884,7 +884,9 @@ class GatewayNotificationsMixin(GatewayUpdateNotificationsMixin, GatewayNotifica
gateway route or a refused notice pinned to a permanently-gone session (``_watch_pin_gone``).
Not transactional: a crash after acceptance can replay (at-least-once).
"""
+ from gateway.session_local_recovery import restore_completion_route
from gateway.wake import WakeNotAccepted, adapter_supports_push, admit_internal_event
+ restore_completion_route(self, str(evt.get("session_key") or "")) # watch notices and heartbeats too
source = await asyncio.to_thread(self._build_process_event_source, evt)
if not source:
# API-server sessions bind the RAW X-Hermes-Session-Id key, not a structured ``agent:...`` key.
diff --git a/gateway/session_automation.py b/gateway/session_automation.py
index c503e5e72ce..fe6e8df4c6d 100644
--- a/gateway/session_automation.py
+++ b/gateway/session_automation.py
@@ -687,8 +687,10 @@ async def admit_route_wake(runner, adapter, source, text, producer, metadata=Non
route holds no committed human turn to borrow provenance from: the caller keeps the adapter
path (nothing is queued there either). Raises ``RuntimeStoreError`` on a refusal."""
from gateway.session_authorities import active_authority
+ from gateway.session_local_recovery import restore_completion_route
authority = active_authority(runner)
key = runner._session_key_for_source(source)
+ restore_completion_route(runner, key) # a closed local session boot left unrestored has no store entry yet
entry = runner.session_store.lookup_by_session_key(key) if authority is not None else None
if authority is None or entry is None:
return False
diff --git a/tests/gateway/test_local_session_orphan_end.py b/tests/gateway/test_local_session_orphan_end.py
index 3b46f37fed5..841cf4ba5a2 100644
--- a/tests/gateway/test_local_session_orphan_end.py
+++ b/tests/gateway/test_local_session_orphan_end.py
@@ -381,3 +381,28 @@ async def test_a_session_the_tui_keeps_live_is_only_orphaned_when_its_connection
assert sorted(authority.orphan_closes) == sorted([kept, front])
assert [delay for delay, _ in armed] == [300.0, 300.0]
+
+async def test_a_watch_match_after_close_and_restart_is_noted_on_the_ended_session(tmp_path, monkeypatch):
+ import queue
+ from hermes_state_local import end_idle_local_session
+ from tests.gateway.test_session_end_delegations import _local
+ from tests.gateway.test_unviewed_completion_notes import _delegation_note, _restart, _target
+
+ runner, authority, ref, _actor = await _local(tmp_path, monkeypatch)
+ route, target = authority.sessions[ref.session_id].route, _target(runner, authority, ref)
+ evt = {'type': 'watch_match', 'session_id': 'proc_watch', 'event_id': 'evt-1', 'command': 'serve',
+ 'pattern': 'READY', 'output': 'READY_proc_watch', 'session_key': route, 'parent_session_id': target}
+ assert end_idle_local_session(authority.db, epoch=authority.epoch, session_id=ref.session_id,
+ target_id=target, reason='cli_close')
+ runner, authority = await _restart(tmp_path, monkeypatch, authority)
+ try:
+ completion_queue = queue.Queue()
+ completion_queue.put(evt)
+ await runner._drain_watch_notifications(completion_queue)
+ assert completion_queue.empty() # admitted, not requeued
+ await authority._drain(ref)
+ assert len(_delegation_note(authority, target, 'READY_proc_watch')) == 1
+ assert authority.db.get_session(target)['end_reason'] == 'cli_close'
+ finally:
+ authority.db.close()