claude_cli: tool calls on the event bus, extensions can block, permissions deny/extensions-only (#182)
This commit is contained in:
1 parent
6ebf1c3180
commit
6978aad27a
12 files changed
+297
-12
No files matched your search
@@ -13,4 +13,28 @@ export HOME="$tmp/home" HX_CLAUDE_BIN="$root/tests/fake_claude.py" FAKE_CLAUDE_L
|
||||
grep -q "claude Bash: echo hi-from-tool" "$tmp/out" && grep -q "approve: allow" "$tmp/out" || { echo "FAIL text"; cat "$tmp/out" "$tmp/err"; exit 1; }
|
||||
grep -q '"--permission-prompt-tool"' "$tmp/argv" && grep -q '"--mcp-config"' "$tmp/argv" && grep -q '"stream-json"' "$tmp/argv" || { echo "FAIL argv"; cat "$tmp/argv"; exit 1; }
|
||||
grep -q "cm-ok" "$tmp/out" || { echo "FAIL codemode"; cat "$tmp/out" "$tmp/err"; exit 1; }
|
||||
# bridge goes through the bus: an extension blocks a secret Read and `curl`; tool events reach NDJSON
|
||||
cd "$tmp/repo"
|
||||
export FAKE_ASK=1
|
||||
timeout 20 "$root/target/debug/hx" -p "go" --mode json -s "$tmp/s2.jsonl" > "$tmp/o2" 2> "$tmp/e2" || { echo "FAIL ask run"; cat "$tmp/e2"; exit 1; }
|
||||
python3 - "$tmp/o2" allow <<'PY' || { echo "FAIL events (no extension)"; exit 1; }
|
||||
import json, sys
|
||||
ev = [json.loads(l) for l in open(sys.argv[1]) if l.startswith("{")]
|
||||
calls = [(e["name"], e["id"]) for e in ev if e["type"] == "tool_call"]
|
||||
res = [e for e in ev if e["type"] == "tool_result"]
|
||||
assert calls == [("read", "t1"), ("bash", "t2")], calls
|
||||
assert [r["is_error"] for r in res] == [False, False] and "allow" in res[1]["content"], res
|
||||
PY
|
||||
printf 'claude_cli = []\n[lsp]\nmode = "off"\ntests = "off"\n[[extensions]]\ncommand = ["python3", "%s/tests/ext_block_all.py"]\n' "$root" > "$tmp/home/.hx/config.toml"
|
||||
timeout 20 "$root/target/debug/hx" -p "go" --mode json -s "$tmp/s3.jsonl" > "$tmp/o3" 2> "$tmp/e3" || { echo "FAIL ext run"; cat "$tmp/e3"; exit 1; }
|
||||
python3 - "$tmp/o3" <<'PY' || { echo "FAIL events (extension)"; cat "$tmp/e3"; exit 1; }
|
||||
import json, sys
|
||||
ev = [json.loads(l) for l in open(sys.argv[1]) if l.startswith("{")]
|
||||
res = [e for e in ev if e["type"] == "tool_result"]
|
||||
assert len(res) == 2 and all(r["is_error"] and "blocked: guard: " in r["content"] for r in res), res
|
||||
PY
|
||||
# a dead guard must not leave the run open under permissions = "extensions-only"
|
||||
printf 'claude_cli = []\npermissions = "extensions-only"\n[lsp]\nmode = "off"\ntests = "off"\n' > "$tmp/home/.hx/config.toml"
|
||||
timeout 20 "$root/target/debug/hx" -p "go" --mode json -s "$tmp/s4.jsonl" > "$tmp/o4" 2> "$tmp/e4" || { echo "FAIL deny run"; cat "$tmp/e4"; exit 1; }
|
||||
grep -q 'extensions-only' "$tmp/o4" && ! grep -q '"content":"allow' "$tmp/o4" || { echo "FAIL extensions-only"; cat "$tmp/o4"; exit 1; }
|
||||
echo "claude cli e2e ok"
|
||||
Executable
+12
@@ -0,0 +1,12 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Test extension: subscribes to tool_call, blocks `read` of secrets and `bash` curl."""
|
||||
import json, sys
|
||||
for line in sys.stdin:
|
||||
m = json.loads(line)
|
||||
if m["method"] == "initialize":
|
||||
r = {"subscribe": ["tool_call"], "tools": []}
|
||||
else:
|
||||
e = m["params"]["event"]
|
||||
bad = (e["name"] == "read" and ".ssh" in e["args"]["path"]) or (e["name"] == "bash" and "curl" in e["args"]["command"])
|
||||
r = {"action": "block", "reason": "guard: " + e["name"]} if bad else {"action": "continue"}
|
||||
print(json.dumps({"jsonrpc": "2.0", "id": m["id"], "result": r}), flush=True)
|
||||
@@ -6,6 +6,29 @@ if "--version" in sys.argv:
|
||||
print("fake"); sys.exit(0)
|
||||
def out(o): print(json.dumps(o), flush=True)
|
||||
reply = "hello from claude"
|
||||
if os.environ.get("FAKE_ASK"): # ask hx about a secret Read and `curl x` (ids t1/t2); report the answers
|
||||
import subprocess
|
||||
cfg = json.loads(sys.argv[sys.argv.index("--mcp-config") + 1])["mcpServers"]["hx"]
|
||||
p = subprocess.Popen([cfg["command"], *cfg["args"]], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
||||
text=True, env={**os.environ, **cfg["env"]})
|
||||
asks = [("t1", "Read", {"file_path": "/home/u/.ssh/id_rsa"}), ("t2", "Bash", {"command": "curl x"})]
|
||||
p.stdin.write(json.dumps({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}) + "\n")
|
||||
p.stdin.flush()
|
||||
p.stdout.readline()
|
||||
ans = []
|
||||
for i, (tid, n, inp) in enumerate(asks): # one at a time: deterministic order
|
||||
p.stdin.write(json.dumps({"jsonrpc": "2.0", "id": 10 + i, "method": "tools/call", "params": {"name": "approve",
|
||||
"arguments": {"tool_name": n, "input": inp, "tool_use_id": tid}}}) + "\n")
|
||||
p.stdin.flush()
|
||||
ans.append(json.loads(json.loads(p.stdout.readline())["result"]["content"][0]["text"]))
|
||||
p.stdin.close()
|
||||
out({"type": "system", "subtype": "init", "session_id": "S1"})
|
||||
for (tid, n, inp), a in zip(asks, ans):
|
||||
out({"type": "assistant", "session_id": "S1", "message": {"content": [{"type": "tool_use", "id": tid, "name": n, "input": inp}]}})
|
||||
out({"type": "user", "session_id": "S1", "message": {"content": [{"type": "tool_result", "tool_use_id": tid,
|
||||
"content": a["behavior"] + ": " + a.get("message", ""), "is_error": a["behavior"] == "deny"}]}})
|
||||
out({"type": "result", "session_id": "S1", "is_error": False, "result": "done", "usage": {"input_tokens": 1, "output_tokens": 1}})
|
||||
sys.exit(0)
|
||||
if "--permission-prompt-tool" in sys.argv: # act like claude: ask hx whether `rm -rf x` may run
|
||||
import subprocess
|
||||
cfg = json.loads(sys.argv[sys.argv.index("--mcp-config") + 1])["mcpServers"]["hx"]
|
||||
|
||||
Reference in new issue
Block a user