The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(tui_gateway): key the notify keepalive on the process owner and hold it through delivery
Ben Barclay7b37ac419151
diff --git a/tests/tui_gateway/test_detached_process_notify_keepalive.py b/tests/tui_gateway/test_detached_process_notify_keepalive.pyindex acba23506ce..9b2aa89602b 100644--- a/tests/tui_gateway/test_detached_process_notify_keepalive.py+++ b/tests/tui_gateway/test_detached_process_notify_keepalive.py@@ -34,11 +34,14 @@ def server(tmp_path, monkeypatch):def registry():from tools.process_registry import process_registry- saved = dict(process_registry._running)+ saved = dict(process_registry._running), dict(process_registry._finished)process_registry._running.clear()+ process_registry._finished.clear()yield process_registryprocess_registry._running.clear()- process_registry._running.update(saved)+ process_registry._finished.clear()+ process_registry._running.update(saved[0])+ process_registry._finished.update(saved[1])class _Timer:@@ -76,11 +79,12 @@ def detached(server, monkeypatch):return sid, session, popped-def _spawn(registry, session_key: str, **kw):+def _spawn(registry, owner: str, **kw):+ """A background process as the TUI turn spawns it: owner_task_id is the turn's task id (the session key)."""from tools.process_registry import ProcessSessionproc = ProcessSession(id=f"proc_{len(registry._running)}", command="hermes -p reviewer chat",- session_key=session_key, started_at=time.time(), **kw)+ session_key=owner, owner_task_id=owner, started_at=time.time(), **kw)registry._running[proc.id] = procreturn proc@@ -107,10 +111,28 @@ def test_orphan_reap_rearms_while_process_will_notify(server, registry, detachedassert server._pending_ws_reaps[sid] is _Timer.created[-1]-def test_session_released_once_process_exits(server, registry, detached):+def test_exited_process_keeps_session_until_its_notice_can_be_delivered(server, registry, detached):+ """The exit is recorded before the completion is queued, and the notice becomes a turn only once the poller+ dequeues it: a reaper firing in that window must not close the session and lose the follow-up."""+ sid, session, _ = detached+ proc = _spawn(registry, session["session_key"], notify_on_complete=True)+ proc.exited = True # mark_exited ran; _move_to_finished (exited_at, queue publish) has not yet++ assert not server._session_is_reapable(sid, session)++ registry._finished[proc.id] = registry._running.pop(proc.id)+ proc.exited_at = time.time() # published, not yet dequeued by the poller++ assert not server._session_is_reapable(sid, session)+++def test_session_released_once_delivery_grace_passes(server, registry, detached):+ from tools.process_registry import NOTIFY_DELIVERY_GRACE_SECONDS+sid, session, popped = detachedproc = _spawn(registry, session["session_key"], notify_on_complete=True)- proc.exited = True+ proc.exited, proc.exited_at = True, time.time() - NOTIFY_DELIVERY_GRACE_SECONDS - 1+ registry._finished[proc.id] = registry._running.pop(proc.id)server._schedule_ws_orphan_reap(sid)_Timer.created[-1].callback()@@ -131,3 +153,26 @@ def test_another_sessions_process_does_not_pin_session(server, registry, detache_spawn(registry, "some-other-session", notify_on_complete=True)assert server._session_is_reapable(sid, session)+++def test_process_started_before_compression_still_pins_session(server, registry, detached):+ """Compression rotates session_key to the continuation id; the process keeps its original owner id. The agent+ still kills it on close (its _process_owner_task_ids keep the old id), so it must still pin the session."""+ from types import SimpleNamespace++ sid, session, _ = detached+ original = session["session_key"]+ _spawn(registry, original, notify_on_complete=True)+ session["session_key"] = "desktop-proc-session-continuation"+ session["agent"] = SimpleNamespace(session_id=session["session_key"],+ _process_owner_task_ids={original, session["session_key"]})++ assert not server._session_is_reapable(sid, session)+++def test_disabled_watch_does_not_pin_session(server, registry, detached):+ sid, session, _ = detached+ proc = _spawn(registry, session["session_key"], watch_patterns=["VERDICT"])+ proc._watch_disabled = True++ assert server._session_is_reapable(sid, session)diff --git a/tools/process_registry.py b/tools/process_registry.pyindex e1949b6bbe2..d21a8b3e635 100644--- a/tools/process_registry.py+++ b/tools/process_registry.py@@ -61,6 +61,9 @@ MAX_OUTPUT_CHARS = 200_000 # rolling output bufferCOMPLETION_OUTPUT_CHARS = 2000FINISHED_TTL_SECONDS = 1800 # keep finished processes 30 minutesMAX_PROCESSES = 64 # max tracked processes (LRU pruning)+# How long an owner stays "awaited" after its notifying process exits: covers publish → poller dequeue →+# turn claim (sub-second when idle). Bounded so an undeliverable notice cannot pin its owner.+NOTIFY_DELIVERY_GRACE_SECONDS = 60.0# Watch-pattern rate limiting, PER SESSION: one watch-match notification per# WATCH_MIN_INTERVAL_SECONDS; a match inside the cooldown is dropped and counts as one@@ -2482,6 +2485,25 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOusession.handoff_note = notereturn session+ def awaits_notification(self, owner_task_ids, *, delivery_grace: float = NOTIFY_DELIVERY_GRACE_SECONDS) -> bool:+ """Whether a process spawned by one of ``owner_task_ids`` will still report back: it notifies+ (``notify_on_complete`` or live ``watch_patterns``) and is running, or exited less than+ ``delivery_grace`` seconds ago — its completion is published after the exit is recorded and becomes an+ agent turn only once a poller dequeues it, so the owner must outlive that hand-off too. Keyed on the+ RAW spawning owner, the same field agent close kills by, so it survives compression rotating the+ session key."""+ owners = {o for o in owner_task_ids if o}+ if not owners:+ return False+ now = time.time()+ with self._lock:+ sessions = [*self._running.values(), *self._finished.values()]+ return any(+ s.owner_task_id in owners+ and (s.notify_on_complete or (s.watch_patterns and not s._watch_disabled))+ and (not s.exited or now - (s.exited_at or now) < delivery_grace)+ for s in sessions)+def has_active_for_session(self, session_key: str, max_active_age: Optional[float] = None) -> bool:"""Active processes for a gateway session key. Processes older than``max_active_age`` seconds are ignored as stale so a forgotten ``http.server``diff --git a/tui_gateway/session_notifications.py b/tui_gateway/session_notifications.pyindex ae2f9ef7c16..9d3e27b61ef 100644--- a/tui_gateway/session_notifications.py+++ b/tui_gateway/session_notifications.py@@ -323,16 +323,16 @@ def _session_owns_live_wakeup_schedule(session: dict) -> bool:def _session_owns_pending_process_notification(session: dict) -> bool:- """True while a background process this session started will report back into it (``notify_on_complete`` or- ``watch_patterns``) and is still running. The completion reaches the model only through THIS session's- notification poller, and closing the session kills the process (``agent_close``), so the reapers must keep it- alive. Bounded by the process itself: it releases the session the moment it exits."""- keys = {str(session.get("session_key") or ""), _session_lookup_key(session)} - {""}- if not keys:- return False+ """True while a background process this session started will still report back into it (``notify_on_complete``+ or ``watch_patterns``): running, or exited so recently its completion may not have become a turn yet. The notice+ reaches the model only through THIS session's notification poller, and closing the session kills the process+ (``agent_close``), so the reapers must keep it alive. Ownership is the set close() kills by — the agent's raw+ spawning task ids, which keep pre-compression ids after the session key rotates. Bounded by the process: the+ session is released a short grace after it exits."""+ agent = session.get("agent")+ owners = {*getattr(agent, "_process_owner_task_ids", ()), str(session.get("session_key") or "")}from tools.process_registry import process_registry- return any(proc["status"] == "running" and (proc.get("notify_on_complete") or proc.get("watch_patterns"))- for key in keys for proc in process_registry.list_sessions(session_key=key))+ return process_registry.awaits_notification(owners)def _maybe_fire_tui_loop_tick(sid: str, session: dict) -> None: