Files
orch/tests/fake_hx.py
T

62 lines
3.8 KiB
Python

import json, sys, time, os
if "--version" in sys.argv:
print("hx " + os.environ.get("FAKE_VERSION", "9.9.9"))
sys.exit(0)
body = sys.argv[sys.argv.index("-p") + 1]
e = lambda o: print(json.dumps(o), flush=True)
e({"type": "session_start"})
import subprocess, tomllib
ext = None
def guard(name, args):
"""Speak the hx extension protocol to whatever .hx/config.toml configures (like real hx)."""
global ext
if "NOGUARD" in body or not os.path.exists(".hx/config.toml"): return None
if ext is None:
cmd = tomllib.load(open(".hx/config.toml", "rb"))["extensions"][0]["command"]
ext = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
ext.stdin.write(json.dumps({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {}}) + "\n"); ext.stdin.flush()
ext.stdout.readline()
ext.stdin.write(json.dumps({"jsonrpc": "2.0", "id": 2, "method": "event", "params": {"event": {"type": "tool_call", "name": name, "args": args}}}) + "\n"); ext.stdin.flush()
return json.loads(ext.stdout.readline())["result"]
for n, l in enumerate(x for x in body.splitlines() if x.startswith("CALL ")):
_, name, arg = l.split(" ", 2)
args = {"command": arg} if name == "bash" else {"path": arg}
e({"type": "tool_call", "id": f"c{n}", "name": name, "args": args})
r = guard(name, args)
if r and r["action"] == "block":
e({"type": "tool_result", "id": f"c{n}", "name": name, "args": args, "content": "blocked: " + r["reason"], "is_error": True})
continue
with open("calls.txt", "a") as f:
if name == "read":
try: f.write("READ " + open(arg).read() + "\n")
except OSError as x: f.write("READ-ERR " + str(x) + "\n")
elif name == "write": open(arg, "w").write("x"); f.write("WROTE " + arg + "\n")
else: f.write("RAN " + arg + "\n")
e({"type": "tool_result", "id": f"c{n}", "name": name, "args": args, "content": "ok", "is_error": False})
if os.environ.get("SOLVE_DIR"):
# eval cases: solve by overlaying the case's solution/ unless the task title is listed in SOLVE_SKIP
for c in (d for d in os.listdir(os.environ["SOLVE_DIR"]) if os.path.isfile(os.path.join(os.environ["SOLVE_DIR"], d, "task.md"))):
t = open(os.path.join(os.environ["SOLVE_DIR"], c, "task.md")).read().split("\n", 2)[2].strip()
if t == body.strip() and c not in os.environ.get("SOLVE_SKIP", "").split(","):
subprocess.run(["cp", "-R", os.path.join(os.environ["SOLVE_DIR"], c, "solution") + "/.", "."], check=True)
if "ENVDUMP" in body: open("env.txt", "w").write("\n".join(sorted(os.environ)))
if "NOJSON" in body: sys.exit(1)
if "HANG" in body:
open(os.environ.get("PIDFILE", "/dev/null"), "w").write(str(os.getpid()))
time.sleep(300)
conc = os.environ.get("CONC")
if "SLEEP=" in body:
if conc: open(conc, "a").write("+")
time.sleep(float(body.split("SLEEP=")[1].split()[0]))
if conc: open(conc, "a").write("-")
if "QUOTA" in body:
e({"type": "stream", "event": {"type": "quota", "windows": [{"label": "5h", "used_pct": 100.0, "resets_at": int(time.time()) + 3600}], "credits": None}})
for i in range(int(body.split("TURNS=")[1].split()[0]) if "TURNS=" in body else 1):
e({"type": "turn_start"})
e({"type": "stream", "event": {"type": "done", "usage": {"input_tokens": 100, "output_tokens": 50, "cache_read_tokens": 1000, "cache_write_tokens": 10}}})
if "WRITE" in body: open("out.txt", "w").write("x")
if "--session" in sys.argv:
open("retry-prompt.txt", "w").write(body)
if "RETRYFIX" in os.environ.get("MODE", "RETRYFIX"): open("out.txt", "w").write("x")
e({"type": "done", "error": "api 429: slow down" if "RATELIMIT" in body else "boom" if "FAIL" in body else None, "usage": {"input_tokens": 100, "output_tokens": 50, "cache_read_tokens": 1000, "cache_write_tokens": 10}, "session": "/s.jsonl"})