"""Drill: three agents running; (1) set only the flag on a1 and watch what keeps running; (2) run the full kill command for a2. Prints a timing table. Run: python drill.py""" import os, sys, time, json, signal, subprocess, pathlib import fixture, killswitch HERE = pathlib.Path(__file__).parent; PY = sys.executable fixture.reset() srv = subprocess.Popen([PY, str(HERE / "toolsrv.py")], cwd=HERE); time.sleep(0.6) procs = {a: subprocess.Popen([PY, str(HERE / "agent.py"), a, f"tok-{a}-demo"], cwd=HERE, start_new_session=True, stdout=subprocess.DEVNULL) for a in ("a1", "a2", "a3")} time.sleep(4) def last(agent, kind): t = [c["t"] for c in fixture.calls() if c["agent"] == agent and c["kind"] == kind]; return max(t) if t else 0 def wait_exit(a, lim=5): t = time.time() while time.time() - t < lim and procs[a].poll() is None: time.sleep(0.02) return time.time() t1 = time.time(); killswitch.set_state("a1", "kill", "oncall"); e1 = wait_exit("a1") t2 = time.time(); subprocess.run([PY, str(HERE / "ops.py"), "kill", "a2"], check=True); e2 = wait_exit("a2") time.sleep(12) # let any leftover job run to its end print(f"{'step':<34}{'agent exit':>12}{'last tool call':>16}{'job heartbeats after kill':>28}") for name, a, t, e in (("flag only (a1)", "a1", t1, e1), ("ops.py kill (a2)", "a2", t2, e2)): hb = [c for c in fixture.calls() if c["agent"] == a and c["kind"] == "job-heartbeat" and c["t"] > t] span = f"{len(hb)} (job ran {hb[-1]['t'] - t:.1f} s more)" if hb else "0" print(f"{name:<34}{e - t:>10.2f} s{last(a, 'call') - t:>+14.2f} s{span:>28}") print("a3 (untouched) still calling:", last("a3", "call") > time.time() - 1) for p in procs.values(): try: os.killpg(p.pid, signal.SIGKILL) except ProcessLookupError: pass srv.kill()