Files
ai-agent/backend/core/liveruns.py

89 lines
3.3 KiB
Python

"""Which sessions have a live CLI process on a runner, as of the exit
watcher's last poll of each runner's ``/sessions`` (runs/sidecar_client.py).
Only hub-launched runs are listed — a terminal session never is. A runner
not heard from for ``STALE_S`` (unreachable, unpaired) knows nothing: its
runs read as not alive, and their conversations fall back to the recency
window like a terminal session's.
A live run can also be **held**: its turn ended, but the runner's Stop guard
(harness-claude's sidecar/stop_guard.py) keeps the process alive until the background shell it
was waiting on ends — a deploy, a build. ``held(sid)`` is ``{since, tasks}``
then, and the conversation reads ``running``, not ``finished``."""
import threading
import time
STALE_S = 30.0
_lock = threading.Lock()
_by_runner: dict[str | None, tuple[float, frozenset[str]]] = {}
_held_by_runner: dict[str | None, dict[str, dict]] = {}
def update(runner: str | None, sids, held: dict[str, dict] | None = None) -> None:
"""The runner's live session ids, as just polled — and, of those, the ones
whose turn the Stop guard holds (``{sid: {since, tasks}}``)."""
sids = frozenset(sids)
with _lock:
_by_runner[runner] = (time.monotonic(), sids)
_held_by_runner[runner] = {k: v for k, v in (held or {}).items()
if k in sids and isinstance(v, dict)}
def drop(runner: str | None) -> None:
with _lock:
_by_runner.pop(runner, None)
_held_by_runner.pop(runner, None)
def snapshot() -> frozenset[str]:
"""Every session alive on a runner polled lately — what a cache keyed on
the conversations' states adds to its key."""
now = time.monotonic()
with _lock:
return frozenset().union(*(sids for at, sids in _by_runner.values()
if now - at < STALE_S))
def alive(sid: str | None) -> bool:
"""True while ``sid``'s process is running on a runner polled lately."""
return bool(sid) and sid in snapshot()
def held(sid: str | None) -> dict | None:
"""``{since, tasks}`` while ``sid``'s turn is held for a background shell
on a runner polled lately, else None."""
if not sid:
return None
now = time.monotonic()
with _lock:
for runner, (at, sids) in _by_runner.items():
if now - at < STALE_S and sid in sids:
return _held_by_runner.get(runner, {}).get(sid)
return None
def held_snapshot() -> frozenset[str]:
"""The held session ids — the part of a cache key ``snapshot()`` misses
(a hold starts and ends while the process stays alive)."""
now = time.monotonic()
with _lock:
return frozenset(sid for runner, (at, _) in _by_runner.items()
if now - at < STALE_S
for sid in _held_by_runner.get(runner, {}))
def held_map() -> dict[str, dict]:
"""``{sid: {since, tasks}}`` for every currently held live session — taken
once per list build instead of acquiring ``_lock`` once per card."""
now = time.monotonic()
out: dict[str, dict] = {}
with _lock:
for runner, (at, sids) in _by_runner.items():
if now - at < STALE_S:
for sid, info in _held_by_runner.get(runner, {}).items():
if sid in sids:
out[sid] = info
return out