Files
ai-agent/sidecar/test_liveness.py
gabrielvidalandClaude Opus 5.5 567a62d979 feat(conversations): a turn held for a background shell reads running
While the sidecar's Stop guard holds a run's ended turn for a background
shell (a deploy, a build), the hub let the 10-min recency window flip the
card to "finished", the result still to come (conv 01a1b0cf: "finished ·
ended 20m ago" while its process sat in the hold).

- sidecar /sessions: each live run reports `held` ({since, tasks}) from
  the guard's <sid>.hold file.
- core.liveruns keeps it per runner next to liveness (held(sid),
  held_snapshot() for the list's cache key); the exit watcher feeds it.
- _resolve_state: a live run that is held reads running; the meta carries
  `held` (schema HeldTurn, regenerated types).
- Cards: a "Waiting on a background shell · N min" chip next to
  Deploying…, the label truncating before the minutes on a narrow card.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 14:23:13 +01:00

80 lines
2.8 KiB
Python

"""Run liveness on both backends: /proc (Linux host) and psutil (macOS worker)."""
import os
import subprocess
import pytest
import pids
from routes_accounts import health
@pytest.fixture(params=["proc", "psutil"])
def backend(request, monkeypatch):
if request.param == "psutil":
pytest.importorskip("psutil")
monkeypatch.setattr(pids, "_HAVE_PROC", False)
elif not pids._HAVE_PROC:
pytest.skip("no /proc here")
return request.param
def test_self_is_live_and_fingerprinted(backend):
pid = os.getpid()
st = pids._proc_starttime(pid)
assert st is not None
assert pids._alive({"pid": pid, "starttime": st})
assert not pids._alive({"pid": pid, "starttime": st + 12345})
def test_zombie_is_dead(backend):
p = subprocess.Popen(["true"])
rec = {"pid": p.pid, "starttime": pids._proc_starttime(p.pid)}
# not in pids._procs, so _reap() won't wait on it: it stays a zombie
import time
time.sleep(0.3)
assert pids._proc_state(p.pid) == "Z"
assert not pids._alive(rec)
p.wait()
def test_health_reports_worker_identity(tmp_path, monkeypatch):
monkeypatch.setenv("WORKER_STATE_DIR", str(tmp_path))
h = health()
assert h["workerId"] and h["hostname"] and h["platform"]
assert "paired" in h and h["permissionMode"]
def test_stopping_a_run_drops_its_hold_mailbox(tmp_path, monkeypatch):
# A message /message left for a Stop-guard hold dies with the run: the
# forced resume carries its own prompt (often that very message).
import launch
monkeypatch.setattr(launch, "LOG_DIR", tmp_path)
monkeypatch.setattr(pids, "_pidfile", lambda sid: tmp_path / f"{sid}.pid")
sid = "11111111-2222-3333-4444-555555555555"
(tmp_path / f"{sid}.hold").write_text("{}")
box = tmp_path / f"{sid}.inbox"
box.mkdir()
(box / "1.json").write_text('{"prompt": "now"}')
p = subprocess.Popen(["sleep", "30"], start_new_session=True)
try:
launch._stop_run(sid, {"pid": p.pid, "starttime": pids._proc_starttime(p.pid)})
finally:
p.kill()
p.wait()
assert not (tmp_path / f"{sid}.hold").exists()
assert not box.exists()
def test_sessions_report_a_stop_guard_hold(tmp_path, monkeypatch):
# /sessions says which live runs the Stop guard holds (their <sid>.hold),
# so the hub keeps them "running" while their turn waits on a shell.
import routes_runs
monkeypatch.setattr(routes_runs, "LOG_DIR", tmp_path)
sid = "11111111-2222-3333-4444-555555555555"
assert routes_runs._held(sid) is None
(tmp_path / f"{sid}.hold").write_text('{"since": 1793968070.2, "tasks": ["bw1", 3]}')
assert routes_runs._held(sid) == {"since": 1793968070.2, "tasks": ["bw1"]}
(tmp_path / f"{sid}.hold").write_text("not json")
assert routes_runs._held(sid) is None