"""A tool-calling loop. It checks the kill switch before every tool call. NOKILL=1 is the harness with no kill switch wired in.""" import os, sys, time, json, signal, urllib.request, urllib.error, pathlib import killswitch AGENT, TOKEN, NOKILL = sys.argv[1], sys.argv[2], os.environ.get("NOKILL") == "1" HERE = pathlib.Path(__file__).parent def post(path, body): r = urllib.request.Request("http://127.0.0.1:29200" + path, method="POST", data=json.dumps(body).encode(), headers={"Authorization": f"Bearer {TOKEN}"}) try: with urllib.request.urlopen(r) as x: return x.status except urllib.error.HTTPError as e: return e.code i = 0 while True: i += 1 s = "run" if NOKILL else killswitch.state(AGENT) if s == "kill": print(f"{AGENT} kill flag seen, exiting", flush=True); sys.exit(0) while s == "suspend": time.sleep(0.1); s = killswitch.state(AGENT) if s == "limit": time.sleep(2) if s == "warn" and i % 10 == 0: print(f"{AGENT} warn flag set", flush=True) if i == 3: post("/job", {"seconds": 10}) # the agent starts a long background job if i % 4 == 0: # memory write m = json.loads((HERE / "memory.json").read_text()); m["notes"].append(f"{AGENT} note {i}") tmp = HERE / f"memory.{AGENT}.tmp"; tmp.write_text(json.dumps(m)); tmp.replace(HERE / "memory.json") # atomic: three agents share the file tool = "send_email" if i % 5 == 0 else "search" code = post("/act", {"tool": tool, "duration": 0.6 if i % 7 == 0 else 0.05}) if code == 401: print(f"{AGENT} credential rejected, exiting", flush=True); sys.exit(1) time.sleep(0.2)