#!/usr/bin/env python3 """Fake `claude -p` for the claude_cli provider (orch tests). It is the process that "executes tools": it reads the file named in $FAKE_CLAUDE_READ the way the real claude would (directly, no hx involved) and writes what it saw to ./claude-read.txt, then answers. Logs argv to $FAKE_CLAUDE_LOG.""" import json, os, sys if "--version" in sys.argv: print("fake") sys.exit(0) open(os.environ.get("FAKE_CLAUDE_LOG", "/dev/null"), "a").write(json.dumps(sys.argv[1:]) + "\n") out = lambda o: print(json.dumps(o), flush=True) out({"type": "system", "subtype": "init", "session_id": "S1"}) if os.environ.get("FAKE_CLAUDE_READ"): # like claude with --permission-prompt-tool: ask hx first, run the tool only when allowed import subprocess target = os.environ["FAKE_CLAUDE_READ"] answer = {"behavior": "allow"} if "--permission-prompt-tool" in sys.argv and not os.environ.get("FAKE_CLAUDE_NOASK"): cfg = json.loads(sys.argv[sys.argv.index("--mcp-config") + 1])["mcpServers"]["hx"] pr = subprocess.Popen([cfg["command"], *cfg["args"]], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True, env={**os.environ, **cfg["env"]}) def rpc(i, m, p): pr.stdin.write(json.dumps({"jsonrpc": "2.0", "id": i, "method": m, "params": p}) + "\n") pr.stdin.flush() # (a lambda `write(..) or flush()` never reaches flush: write returns a truthy count) rpc(1, "initialize", {}) pr.stdout.readline() rpc(2, "tools/call", {"name": "approve", "arguments": {"tool_name": "Read", "input": {"file_path": target}, "tool_use_id": "t1"}}) answer = json.loads(json.loads(pr.stdout.readline())["result"]["content"][0]["text"]) pr.stdin.close() out({"type": "assistant", "session_id": "S1", "message": {"content": [ {"type": "tool_use", "id": "t1", "name": "Read", "input": {"file_path": target}}]}}) if answer["behavior"] == "allow": try: seen = open(target).read() except OSError as e: seen = "ERR " + str(e) open("claude-read.txt", "w").write(seen) res, err = seen, False else: res, err = "deny: " + answer.get("message", ""), True out({"type": "user", "session_id": "S1", "message": {"content": [ {"type": "tool_result", "tool_use_id": "t1", "content": res, "is_error": err}]}}) out({"type": "stream_event", "session_id": "S1", "event": {"type": "content_block_delta", "delta": {"type": "text_delta", "text": "done"}}}) out({"type": "result", "session_id": "S1", "is_error": False, "result": "done", "usage": {"input_tokens": 3, "output_tokens": 4}})