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