"""Fake `hx serve --acp --cwd DIR --session ID`: newline-delimited JSON-RPC 2.0 on stdio. Behaviour per `hx/step`, chosen with FAKE_MODE (orch passes FAKE_* through the profile's `env`): ok intent, tool call, edit, usage, decision, plan, then step_result done (default) blocked step_result blocked, catch FAKE_CATCH (default PlanInvalid) block_once blocked the first time (marker file .fake_blocked in cwd), done afterwards needs_input step_result needs_input with a question permission asks session/request_permission first and records the answer in permission.txt crash exits without a step_result Every received message is appended to FAKE_LOG when set. Each step also appends its prompt kind to steps.txt. """ import json import os import sys args = sys.argv[1:] cwd = args[args.index("--cwd") + 1] if "--cwd" in args else os.getcwd() session = args[args.index("--session") + 1] if "--session" in args else "sess-1" mode = os.environ.get("FAKE_MODE", "ok") log = os.environ.get("FAKE_LOG") def send(o): sys.stdout.write(json.dumps({"jsonrpc": "2.0", **o}) + "\n") sys.stdout.flush() def note(method, params): send({"method": method, "params": params}) def recv(): line = sys.stdin.readline() if not line: sys.exit(0) m = json.loads(line) if log: with open(log, "a") as f: f.write(json.dumps(m) + "\n") return m def step(params): kind = params["prompt"] with open(os.path.join(cwd, "steps.txt"), "a") as f: f.write(kind + "\n") if mode == "crash": sys.exit(3) note("hx/intent", {"text": f"working on {kind}"}) note("session/update", {"sessionId": session, "update": {"sessionUpdate": "tool_call", "toolCallId": "t1", "title": "edit src/lib.rs", "kind": "edit", "status": "in_progress"}}) if mode == "permission": send({"id": 900, "method": "session/request_permission", "params": { "sessionId": session, "toolCall": {"toolCallId": "t2", "title": "docker compose up -d redis"}, "options": [{"optionId": "allow", "name": "Allow once", "kind": "allow_once"}, {"optionId": "deny", "name": "Deny", "kind": "reject_once"}]}}) while True: m = recv() if m.get("id") == 900: with open(os.path.join(cwd, "permission.txt"), "w") as f: f.write(json.dumps(m["result"])) break if mode == "needs_input": note("hx/step_result", {"status": "needs_input", "question": "Which provider?"}) return blocked = mode == "blocked" if mode == "block_once" and not os.path.exists(os.path.join(cwd, ".fake_blocked")): open(os.path.join(cwd, ".fake_blocked"), "w").close() blocked = True if blocked: note("hx/step_result", {"status": "blocked", "catch": os.environ.get("FAKE_CATCH", "PlanInvalid"), "summary": "cannot continue"}) return with open(os.path.join(cwd, f"agent_{kind}.txt"), "a") as f: f.write("work\n") note("hx/edit", {"path": f"agent_{kind}.txt", "added": 4, "removed": 1}) note("hx/usage", {"ctx_used": 40000, "ctx_limit": 200000, "cost": 0.25}) note("hx/decision", {"text": "token bucket over sliding window", "state": kind, "at": 1}) if kind == "plan": note("hx/plan", {"steps": ["write the limiter", "add tests"], "files": ["src/limiter.rs"]}) note("hx/step_result", {"status": "done", "summary": f"{kind} finished"}) while True: m = recv() method, mid = m.get("method"), m.get("id") if method == "initialize": send({"id": mid, "result": {"protocolVersion": 1, "agentCapabilities": {}}}) elif method == "session/new": send({"id": mid, "result": {"sessionId": session}}) elif method == "hx/step": send({"id": mid, "result": {}}) # ack; the outcome comes as hx/step_result step(m["params"]) elif mid is not None: send({"id": mid, "error": {"code": -32601, "message": "unknown method"}})