O5: tool allowlist guard, env isolation, audit log (closes #6)
This commit is contained in:
1 parent
06662a085f
commit
8361a4411f
9 files changed
+890
-29
No files matched your search
@@ -0,0 +1,52 @@
|
||||
import json, sys, time, os
|
||||
body = sys.argv[sys.argv.index("-p") + 1]
|
||||
e = lambda o: print(json.dumps(o), flush=True)
|
||||
e({"type": "session_start"})
|
||||
import subprocess, tomllib
|
||||
ext = None
|
||||
def guard(name, args):
|
||||
"""Speak the hx extension protocol to whatever .hx/config.toml configures (like real hx)."""
|
||||
global ext
|
||||
if "NOGUARD" in body or not os.path.exists(".hx/config.toml"): return None
|
||||
if ext is None:
|
||||
cmd = tomllib.load(open(".hx/config.toml", "rb"))["extensions"][0]["command"]
|
||||
ext = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
|
||||
ext.stdin.write(json.dumps({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}) + "\n"); ext.stdin.flush()
|
||||
ext.stdout.readline()
|
||||
ext.stdin.write(json.dumps({"jsonrpc": "2.0", "id": 2, "method": "event", "params": {"event": {"type": "tool_call", "name": name, "args": args}}}) + "\n"); ext.stdin.flush()
|
||||
return json.loads(ext.stdout.readline())["result"]
|
||||
for n, l in enumerate(x for x in body.splitlines() if x.startswith("CALL ")):
|
||||
_, name, arg = l.split(" ", 2)
|
||||
args = {"command": arg} if name == "bash" else {"path": arg}
|
||||
e({"type": "tool_call", "id": f"c{n}", "name": name, "args": args})
|
||||
r = guard(name, args)
|
||||
if r and r["action"] == "block":
|
||||
e({"type": "tool_result", "id": f"c{n}", "name": name, "args": args, "content": "blocked: " + r["reason"], "is_error": True})
|
||||
continue
|
||||
with open("calls.txt", "a") as f:
|
||||
if name == "read":
|
||||
try: f.write("READ " + open(arg).read() + "\n")
|
||||
except OSError as x: f.write("READ-ERR " + str(x) + "\n")
|
||||
elif name == "write": open(arg, "w").write("x"); f.write("WROTE " + arg + "\n")
|
||||
else: f.write("RAN " + arg + "\n")
|
||||
e({"type": "tool_result", "id": f"c{n}", "name": name, "args": args, "content": "ok", "is_error": False})
|
||||
if "ENVDUMP" in body: open("env.txt", "w").write("\n".join(sorted(os.environ)))
|
||||
if "NOJSON" in body: sys.exit(1)
|
||||
if "HANG" in body:
|
||||
open(os.environ.get("PIDFILE", "/dev/null"), "w").write(str(os.getpid()))
|
||||
time.sleep(300)
|
||||
conc = os.environ.get("CONC")
|
||||
if "SLEEP=" in body:
|
||||
if conc: open(conc, "a").write("+")
|
||||
time.sleep(float(body.split("SLEEP=")[1].split()[0]))
|
||||
if conc: open(conc, "a").write("-")
|
||||
if "QUOTA" in body:
|
||||
e({"type": "stream", "event": {"type": "quota", "windows": [{"label": "5h", "used_pct": 100.0, "resets_at": int(time.time()) + 3600}], "credits": None}})
|
||||
for i in range(int(body.split("TURNS=")[1].split()[0]) if "TURNS=" in body else 1):
|
||||
e({"type": "turn_start"})
|
||||
e({"type": "stream", "event": {"type": "done", "usage": {"input_tokens": 100, "output_tokens": 50}}})
|
||||
if "WRITE" in body: open("out.txt", "w").write("x")
|
||||
if "--session" in sys.argv:
|
||||
open("retry-prompt.txt", "w").write(body)
|
||||
if "RETRYFIX" in os.environ.get("MODE", "RETRYFIX"): open("out.txt", "w").write("x")
|
||||
e({"type": "done", "error": "api 429: slow down" if "RATELIMIT" in body else "boom" if "FAIL" in body else None, "usage": {"input_tokens": 100, "output_tokens": 50}, "session": "/s.jsonl"})
|
||||
Reference in new issue
Block a user