hx serve --acp: headless Agent Client Protocol mode
JSON-RPC on stdio with permission requests, hx/step|message|interrupt|compact in and hx/step_result|intent|edit|usage|decision|plan out. Sessions persist by id. Limits are soft: a limit stop is blocked with reason "limit" and no catch; catch is only a validated name from the step's catches list. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5408db83a9
commit
aec92ff5a5
7 files changed
+952
-1
No files matched your search
@@ -0,0 +1,83 @@
|
||||
"""Fake orchestrator for `hx serve --acp`: drives hx over stdio and checks the protocol.
|
||||
Usage: python3 tests/acp_client.py HX_BIN REPO SESSION_ID PORT
|
||||
Scenario (against tests/fake_anthropic.py with acp_turns.json):
|
||||
run 1: step with context limit 1 -> permission asked, edit done, step ends blocked (limit)
|
||||
step 2 -> done, decision + plan reported
|
||||
run 2 (new process, same session) -> step 3 sees the earlier conversation
|
||||
"""
|
||||
import json, subprocess, sys, urllib.request
|
||||
|
||||
hx, repo, sid, port = sys.argv[1:5]
|
||||
seen = [] # every notification and request from hx, in order
|
||||
|
||||
|
||||
class Hx:
|
||||
def __init__(self):
|
||||
self.p = subprocess.Popen([hx, "serve", "--acp", "--cwd", repo, "--session", sid],
|
||||
stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
|
||||
self.n = 0
|
||||
|
||||
def send(self, m):
|
||||
self.p.stdin.write(json.dumps({"jsonrpc": "2.0", **m}) + "\n")
|
||||
self.p.stdin.flush()
|
||||
|
||||
def call(self, method, params):
|
||||
self.n += 1
|
||||
self.send({"id": self.n, "method": method, "params": params})
|
||||
while True:
|
||||
line = self.p.stdout.readline()
|
||||
assert line, "hx closed stdout"
|
||||
m = json.loads(line)
|
||||
if "method" in m:
|
||||
seen.append(m)
|
||||
if "id" in m: # permission request: allow once
|
||||
assert m["method"] == "session/request_permission", m
|
||||
self.send({"id": m["id"], "result": {"outcome": {"outcome": "selected", "optionId": "allow_once"}}})
|
||||
elif m.get("id") == self.n:
|
||||
assert "error" not in m, m
|
||||
return m["result"]
|
||||
|
||||
def close(self):
|
||||
self.p.stdin.close()
|
||||
assert self.p.wait(timeout=30) == 0
|
||||
|
||||
|
||||
def notes(method):
|
||||
return [m["params"] for m in seen if m["method"] == method]
|
||||
|
||||
|
||||
a = Hx()
|
||||
init = a.call("initialize", {"protocolVersion": 1})
|
||||
assert init["agentInfo"]["name"] == "hx", init
|
||||
assert a.call("session/new", {"cwd": repo, "mcpServers": []})["sessionId"] == sid
|
||||
|
||||
r = a.call("hx/step", {"prompt": "write hello.txt", "instruction": "implement", "context": {"goal": "demo"},
|
||||
"limits": {"context_tokens": 1}})
|
||||
assert r["status"] == "blocked" and r["catch"] is None and r["reason"] == "limit" and "context limit" in r["summary"], r
|
||||
perm = [m for m in seen if m["method"] == "session/request_permission"]
|
||||
assert perm and perm[0]["params"]["options"][0]["optionId"] == "allow_once", perm
|
||||
assert open(f"{repo}/hello.txt").read() == "hi from hx\n"
|
||||
assert {"path": "hello.txt", "added": 1, "removed": 0} in notes("hx/edit"), notes("hx/edit")
|
||||
assert any("hello.txt" in i for i in notes("hx/intent")), notes("hx/intent")
|
||||
assert notes("hx/usage") and notes("hx/usage")[0]["used"] > 0, notes("hx/usage")
|
||||
assert notes("hx/step_result")[0] == r
|
||||
|
||||
a.call("hx/message", "prefer small commits")
|
||||
r = a.call("hx/step", {"prompt": "now summarize"})
|
||||
assert r["status"] == "done" and r["summary"] == "wrote hello.txt", r
|
||||
assert [d["text"] for d in notes("hx/decision")] == ["token bucket over sliding window"]
|
||||
assert notes("hx/plan") == [{"steps": ["write", "test"], "files": ["hello.txt"]}], notes("hx/plan")
|
||||
assert a.call("hx/interrupt", {}) == {}
|
||||
a.close()
|
||||
|
||||
b = Hx()
|
||||
b.call("initialize", {"protocolVersion": 1})
|
||||
r = b.call("hx/step", {"prompt": "what did you do before?"})
|
||||
assert r["status"] == "done", r
|
||||
b.close()
|
||||
|
||||
log = json.load(urllib.request.urlopen(f"http://127.0.0.1:{port}/"))
|
||||
text = json.dumps(log[-1]["body"]["messages"])
|
||||
assert "write hello.txt" in text and "wrote hello.txt" in text, "session not continued"
|
||||
assert "prefer small commits" in json.dumps(log[1]["body"]["messages"]), "hint not delivered"
|
||||
print("acp e2e ok")
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
#!/bin/sh
|
||||
# ACP e2e: `hx serve --acp` driven by a fake orchestrator (tests/acp_client.py) against a fake Anthropic API:
|
||||
# permission requests, hx/edit|usage|intent|decision|plan|step_result, limits, hx/message, session continuation.
|
||||
set -eu
|
||||
root=$(cd "$(dirname "$0")/.." && pwd)
|
||||
tmp=$(mktemp -d)
|
||||
trap 'kill $srv 2>/dev/null; rm -rf "$tmp"' EXIT
|
||||
port=$((20000 + $$ % 20000))
|
||||
python3 "$root/tests/fake_anthropic.py" $port "$root/tests/acp_turns.json" & srv=$!
|
||||
mkdir -p "$tmp/home/.hx" "$tmp/repo"
|
||||
echo '{"claude":{"access":"fake-token","refresh":"r","expires":99999999999999}}' > "$tmp/home/.hx/auth.json"
|
||||
printf 'claude_cli = false\n[lsp]\nmode = "off"\ntests = "off"\n' > "$tmp/home/.hx/config.toml"
|
||||
sleep 0.5
|
||||
export HOME="$tmp/home" HX_BASE_URL="http://127.0.0.1:$port"
|
||||
python3 "$root/tests/acp_client.py" "$root/target/debug/hx" "$tmp/repo" demo-1 $port
|
||||
@@ -0,0 +1,5 @@
|
||||
[
|
||||
[["tool_use", {"id": "t1", "name": "Write", "input": {"path": "hello.txt", "content": "hi from hx\n"}}]],
|
||||
[["text", "Done.\n```hx-step\n{\"status\":\"done\",\"summary\":\"wrote hello.txt\",\"decisions\":[\"token bucket over sliding window\"],\"plan\":{\"steps\":[\"write\",\"test\"],\"files\":[\"hello.txt\"]}}\n```"]],
|
||||
[["text", "```hx-step\n{\"status\":\"done\",\"summary\":\"as before\"}\n```"]]
|
||||
]
|
||||
Reference in new issue
Block a user