"""Kill switch state. One JSON file, read at every loop iteration, editable without a deploy. Tiers (OWASP: warn -> limit -> suspend -> kill). Scope: global, per agent. Tools are revoked in tools.json.""" import json, pathlib HERE = pathlib.Path(__file__).parent ORDER = ["run", "warn", "limit", "suspend", "kill"] def state(agent_id): try: f = json.loads((HERE / "killswitch.json").read_text()) except (FileNotFoundError, json.JSONDecodeError): return "kill" # unreadable flag = fail closed a, g = f.get("agents", {}).get(agent_id, "run"), f.get("global", "run") return max(a, g, key=ORDER.index) def set_state(scope, value, who): f = json.loads((HERE / "killswitch.json").read_text()) if scope == "global": f["global"] = value else: f.setdefault("agents", {})[scope] = value f["last_change"] = {"by": who, "scope": scope, "value": value} tmp = HERE / "killswitch.json.tmp"; tmp.write_text(json.dumps(f)); tmp.replace(HERE / "killswitch.json") # atomic