run_active_work_verb.py 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """``active-work`` control verb: what a stop/restart of this gateway would interrupt, answered live.
  2. ``gateway_state.json`` names each unit only while draining and its ``active_agents`` total leaves out
  3. async delegations, so a CLI asking "may I stop this gateway?" could not see a background subagent
  4. that ``stop()`` interrupts (``tools.async_delegation.interrupt_all``). The verb answers from the
  5. running process instead: the same units the drain report publishes plus every live delegation.
  6. """
  7. from __future__ import annotations
  8. import os
  9. import threading
  10. import time
  11. from typing import Any, Callable
  12. def _delegation_units(now: float) -> list[dict[str, Any]]:
  13. from tools.async_delegation import _LIVE_STATES, list_async_delegations
  14. units = []
  15. for record in list_async_delegations():
  16. if record.get("status") not in _LIVE_STATES:
  17. continue
  18. unit: dict[str, Any] = {"kind": "delegation", "delegation_id": record.get("delegation_id"),
  19. "goal": str(record.get("goal") or "")[:80], "pid": os.getpid()}
  20. if record.get("dispatched_at"):
  21. unit["elapsed_s"] = round(now - float(record["dispatched_at"]), 1)
  22. units.append(unit)
  23. return units
  24. def _profile_units(runner, profile: str, units: list[dict[str, Any]]) -> list[dict[str, Any]]:
  25. """What an ``unserve-profile`` of *profile* stops (``stop_authority_work``): its chat turns (session
  26. keys in its namespace) plus its authority's managed turns and cron runs. Delegations survive it."""
  27. from gateway.session import _session_key_namespace
  28. prefix = _session_key_namespace(profile) + ":"
  29. mine = [u for u in units if u.get("kind") == "chat" and str(u.get("session") or "").startswith(prefix)]
  30. home = (getattr(runner, "_served_profile_homes", None) or {}).get(profile)
  31. registry = getattr(runner, "session_authorities", None)
  32. authority = registry.for_home(home) if registry is not None and home is not None else None
  33. from gateway.session_runtime_workers import side_agents
  34. managed = sum(not w.closed.is_set() for w in getattr(authority, "_managed_workers", {}).values())
  35. crons = len(getattr(authority, "_cron_cancellations", None) or {})
  36. sides = len(side_agents(authority)) if authority is not None else 0
  37. pid = os.getpid()
  38. return (mine + [{"kind": "managed", "pid": pid}] * managed + [{"kind": "cron", "job_id": "?", "pid": pid}] * crons
  39. + [{"kind": "side_agent", "pid": pid}] * sides)
  40. def active_work_verb(runner, loop) -> Callable[[], dict[str, Any]]:
  41. """Handler for the control socket: runs on its executor thread, reads the runner on the loop."""
  42. def _handler(params: dict[str, Any]) -> dict[str, Any]:
  43. profile = params.get("profile")
  44. scoped = isinstance(profile, str) and bool(profile)
  45. box: list[dict[str, Any]] = []
  46. done = threading.Event()
  47. def _collect() -> None:
  48. # Everything that reads live authority state runs here, on the loop: a read on the socket
  49. # thread can race a worker being added and raise, and an error reads as "no work".
  50. try:
  51. units = list(runner._describe_active_work()) + _delegation_units(time.time())
  52. if scoped:
  53. units = _profile_units(runner, profile, units)
  54. box.append({"active_agents": len(units), "active_work": units, "profile": profile})
  55. else:
  56. box.append({"active_agents": runner._active_work_count(), "active_work": units})
  57. finally:
  58. done.set()
  59. loop.call_soon_threadsafe(_collect)
  60. if not done.wait(timeout=5.0) or not box:
  61. raise RuntimeError("active-work: the gateway loop did not answer")
  62. return {**box[0], "pid": os.getpid()}
  63. return _handler