Files
orch/tests/fake_acp_hx.py
Johannes KresnerandClaude Opus 5.5 db80d15ba2
build / version (push) Successful in 12s
build / build (aarch64-apple-darwin) (push) Successful in 5m20s
build / build (x86_64-unknown-linux-musl) (push) Successful in 4m45s
build / release (push) Successful in 24s
fix: an hx error asks the user instead of taking the state's first catch
hx reports its own failures (API, login, ...) as blocked with reason
"error" and no catch. orch then fell back to the state's first catch,
so with code's ToolDenied = "review" a failed agent looked like "review
ready" with no code written. Now the error lands in Needs-you; a reply
runs the step again with the reply as hint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 15:52:40 +01:00

125 lines
6.0 KiB
Python

"""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; finishes when the retried step has a context.hint
permission asks session/request_permission first and records the answer in permission.txt
slow works until hx/interrupt (records hx/message and hx/compact in messages.txt / compact.txt), then
answers done; a retried step with context.hint finishes at once
limit step_result blocked, no catch, reason "limit" (hx hit a limit from hx/step)
error step_result blocked, no catch, reason "error"; finishes when the retried step has a context.hint
crash exits without a step_result
FAKE_UNDERSCORE=1 sends every custom method as `_hx/...`.
FAKE_USAGE="ctx_used,ctx_limit,cost" makes every step report that usage first.
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):
if os.environ.get("FAKE_UNDERSCORE") and method.startswith("hx/"):
method = "_" + method
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}"})
usage = os.environ.get("FAKE_USAGE") # "ctx_used,ctx_limit,cost"
if usage:
used, limit, cost = usage.split(",")
note("hx/usage", {"ctx_used": int(used), "ctx_limit": int(limit), "cost": float(cost)})
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
# slow keeps working until the orchestrator interrupts it; it records what it is told meanwhile.
# A retried step (context.hint set, e.g. the user's reply after an interrupt) finishes at once.
if mode == "slow" and not params.get("context", {}).get("hint"):
while True:
m = recv()
md = m.get("method")
if md == "hx/message":
with open(os.path.join(cwd, "messages.txt"), "a") as f:
f.write(m["params"]["text"] + "\n")
elif md == "hx/compact":
with open(os.path.join(cwd, "compact.txt"), "a") as f:
f.write("compact\n")
elif md == "hx/interrupt":
note("hx/step_result", {"status": "done", "summary": "interrupted"})
return
# needs_input asks once; with the user's answer in context.hint the same step finishes
if mode == "needs_input" and not params.get("context", {}).get("hint"):
note("hx/step_result", {"status": "needs_input", "question": "Which provider?"})
return
if mode == "limit" or mode == "error" and not params.get("context", {}).get("hint"): # blocked without a catch, only the reason differs
note("hx/step_result", {"status": "blocked", "reason": mode, "summary": f"hit the {mode}"})
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"}})