"""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")