"""Emergency shutdown and stale-run detection.""" from __future__ import annotations from .config import NODES from . import launcher def kill_everything(log=print): for n in NODES: rc = launcher.kill_remote(n, "claude --bare"); launcher.ssh(n, "pkill -f 'wp42.portal'; true"); log(f"{n}: killed (rc={rc})") def stale_runs(store, max_age_s=3600): import datetime as dt out = [] for r in store.runs("status IN ('running','preparing','recovering')"): try: t0 = dt.datetime.strptime(r["started_utc"], "%Y-%m-%dT%H:%M:%S.%fZ").replace(tzinfo=dt.timezone.utc) if (dt.datetime.now(dt.timezone.utc) - t0).total_seconds() > max_age_s: out.append(r["run_id"]) except Exception: pass # noqa: BLE001 return out