process_registry_awaiting.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. """Whether a background process will still report back to the agent that spawned it.
  2. The session reapers keep an owner alive while this is true: the notice reaches the model only through that owner's
  3. notification poller, and closing the owner kills the process (``agent_close``).
  4. Pending is read from live state, never estimated with a timer. A notice is pending from the moment its process can
  5. produce it until a consumer has finished routing it:
  6. 1. a notifying process is still running;
  7. 2. its exit is recorded but the completion is not yet published (``_completion_event`` unset; the registry keeps
  8. such entries out of pruning);
  9. 3. the event sits in ``completion_queue``;
  10. 4. a consumer has taken it off the queue and is still routing it (an in-flight hold, taken atomically with the
  11. dequeue under the queue's own lock and released only after routing returns).
  12. Routing ends by admitting a turn (``running`` then protects the session), requeueing (state 3 again) or dropping the
  13. notice, so every state hands over to the next without a gap and none outlives delivery.
  14. """
  15. from __future__ import annotations
  16. import collections
  17. import contextlib
  18. import threading
  19. # Owner task id → number of dequeued-but-unrouted events. take_ready increments it under the queue's mutex in the same
  20. # critical section as the dequeue, and awaits_notification reads queue and counter under that mutex too, so a reader
  21. # sees each event either queued or held, never neither. Decrements (release) happen only after any requeue.
  22. _in_flight: collections.Counter = collections.Counter()
  23. _in_flight_lock = threading.Lock() # always taken after the queue mutex, never before
  24. def _owner(evt) -> str:
  25. return str(evt.get("owner_task_id") or "") if isinstance(evt, dict) else ""
  26. def _adjust(owners, delta: int) -> None:
  27. with _in_flight_lock:
  28. for owner in owners:
  29. if not owner:
  30. continue
  31. _in_flight[owner] += delta
  32. if _in_flight[owner] <= 0:
  33. del _in_flight[owner]
  34. def take_ready(queue, timeout: float) -> list:
  35. """Dequeue everything queued (waiting up to ``timeout`` for the first event) and hold each event's owner in the
  36. same critical section, so no reaper check can observe an event as neither queued nor held. Delegation result
  37. copies are marked held there too (``async_delegation.mark_copy_held``), so another session's orphan sweep never
  38. re-offers a copy this poller is still routing. The caller MUST pass the result to ``release`` once routing
  39. returns."""
  40. from tools.async_delegation import completion_copy_id, mark_copy_held
  41. with queue.not_empty:
  42. if not queue._qsize() and timeout > 0:
  43. queue.not_empty.wait(timeout)
  44. events = list(queue.queue)
  45. queue.queue.clear()
  46. if events:
  47. queue.not_full.notify_all()
  48. _adjust((_owner(e) for e in events), +1)
  49. for evt in events:
  50. mark_copy_held(completion_copy_id(evt))
  51. return events
  52. def release(events) -> None:
  53. """End the in-flight hold ``take_ready`` took for ``events`` (after any requeue, so coverage is continuous)."""
  54. if events:
  55. from tools.async_delegation import completion_copy_id, unmark_copy_held
  56. _adjust((_owner(e) for e in events), -1)
  57. for evt in events:
  58. unmark_copy_held(completion_copy_id(evt))
  59. @contextlib.contextmanager
  60. def drained_for(registry, owns):
  61. """Dequeue everything queued for a caller that consumes only the events ``owns`` accepts (the TUI's post-turn
  62. safety net). Every dequeued event's owner is held — foreign ones too — until the rejected events are back on
  63. the queue and the accepted ones (yielded) have been routed, so no owner is ever between queue and hold."""
  64. registry.restore_completions()
  65. ready = take_ready(registry.completion_queue, 0)
  66. try:
  67. mine = [evt for evt in ready if owns(evt)]
  68. for evt in ready:
  69. if not any(evt is m for m in mine):
  70. registry.completion_queue.put(evt)
  71. yield mine
  72. finally:
  73. release(ready)
  74. def retained_for_delivery(s) -> bool:
  75. """A finished entry whose completion is not yet published: pruning it would erase the only record that a notice
  76. is coming, so the registry keeps it until publication."""
  77. return bool(getattr(s, "_finish_claimed", False)) and not s._completion_event.is_set()
  78. def _notifies(s) -> bool:
  79. return bool(s.notify_on_complete or (s.watch_patterns and not s._watch_disabled))
  80. def awaits_notification(registry, owner_task_ids) -> bool:
  81. """Whether a process spawned by one of ``owner_task_ids`` still has a notice on its way. Keyed on the RAW
  82. spawning owner, the same field agent close kills by, so it survives compression rotating the session key."""
  83. owners = {o for o in owner_task_ids if o}
  84. if not owners:
  85. return False
  86. # (1) Running. _any_running refreshes checkpoint-recovered entries (outside the registry lock) first, so a
  87. # recovered PID that died unobserved reads as gone instead of pinning its owner.
  88. if registry._any_running(lambda s: s.owner_task_id in owners and _notifies(s)):
  89. return True
  90. with registry._lock:
  91. # (2) Exit recorded, completion not yet published. Entries closed without a collected exit (recovered PID
  92. # gone or reused) set _completion_event too, so they never count.
  93. if any(s.owner_task_id in owners and s.notify_on_complete and s.exited and not s._completion_event.is_set()
  94. for store in (registry._running, registry._finished) for s in store.values()):
  95. return True
  96. queue = registry.completion_queue
  97. with queue.mutex: # (3) queued and (4) in flight, read together: take_ready moves events between them here
  98. with _in_flight_lock:
  99. if any(_in_flight.get(o) for o in owners):
  100. return True
  101. return any(_owner(evt) in owners for evt in queue.queue)