Skip to content

The agent that grows with you

refactor(process): move the notify-await check into a sibling so the registry facade does not grow

Ben Barclay9d32d3f2cd23
diff --git a/tests/tui_gateway/test_detached_process_notify_keepalive.py b/tests/tui_gateway/test_detached_process_notify_keepalive.py
index 9b2aa89602b..4827c51c9c9 100644
--- a/tests/tui_gateway/test_detached_process_notify_keepalive.py
+++ b/tests/tui_gateway/test_detached_process_notify_keepalive.py
@@ -127,7 +127,7 @@ def test_exited_process_keeps_session_until_its_notice_can_be_delivered(server,
def test_session_released_once_delivery_grace_passes(server, registry, detached):
- from tools.process_registry import NOTIFY_DELIVERY_GRACE_SECONDS
+ from tools.process_registry_awaiting import NOTIFY_DELIVERY_GRACE_SECONDS
sid, session, popped = detached
proc = _spawn(registry, session["session_key"], notify_on_complete=True)
diff --git a/tools/process_registry.py b/tools/process_registry.py
index d21a8b3e635..e1949b6bbe2 100644
--- a/tools/process_registry.py
+++ b/tools/process_registry.py
@@ -61,9 +61,6 @@ MAX_OUTPUT_CHARS = 200_000 # rolling output buffer
COMPLETION_OUTPUT_CHARS = 2000
FINISHED_TTL_SECONDS = 1800 # keep finished processes 30 minutes
MAX_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
@@ -2485,25 +2482,6 @@ class ProcessRegistry(ProcessTerminationMixin, ProcessCheckpointMixin, ProcessOu
session.handoff_note = note
return 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/tools/process_registry_awaiting.py b/tools/process_registry_awaiting.py
new file mode 100644
index 00000000000..08a539b328b
--- /dev/null
+++ b/tools/process_registry_awaiting.py
@@ -0,0 +1,32 @@
+"""Whether a background process will still report back to the agent that spawned it.
+
+The session reapers keep an owner alive while this is true: the completion reaches the model only through that
+owner's notification poller, and closing the owner kills the process (``agent_close``).
+"""
+
+from __future__ import annotations
+
+import time
+
+# 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
+
+
+def awaits_notification(registry, 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 registry._lock:
+ sessions = [*registry._running.values(), *registry._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)
diff --git a/tui_gateway/session_notifications.py b/tui_gateway/session_notifications.py
index 9d3e27b61ef..c05b8ac6a9a 100644
--- a/tui_gateway/session_notifications.py
+++ b/tui_gateway/session_notifications.py
@@ -332,7 +332,8 @@ def _session_owns_pending_process_notification(session: dict) -> bool:
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 process_registry.awaits_notification(owners)
+ from tools.process_registry_awaiting import awaits_notification
+ return awaits_notification(process_registry, owners)
def _maybe_fire_tui_loop_tick(sid: str, session: dict) -> None: