1 parent
25a96a8c65
commit
29c08130c2
3 files changed
+146
No files matched your search
@@ -0,0 +1,46 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Self-check for the O0 spike: python3 spike/check.py"""
|
||||||
|
import json, pathlib, subprocess, sys, tempfile
|
||||||
|
|
||||||
|
here = pathlib.Path(__file__).parent.resolve()
|
||||||
|
tmp = pathlib.Path(tempfile.mkdtemp())
|
||||||
|
repo = tmp / "repo"
|
||||||
|
repo.mkdir()
|
||||||
|
for c in (["init", "-q", "-b", "main"], ["-c", "user.name=t", "-c", "user.email=t@t", "commit", "-q", "--allow-empty", "-m", "i"]):
|
||||||
|
subprocess.run(["git", *c], cwd=repo, check=True)
|
||||||
|
tele = tmp / "t.jsonl"
|
||||||
|
hx = f"{sys.executable} {here / 'fake_hx.py'}"
|
||||||
|
|
||||||
|
|
||||||
|
def run(name, body, timeout=30):
|
||||||
|
task = tmp / f"{name}.md"
|
||||||
|
task.write_text(f"# Task {name}\n{body}\n")
|
||||||
|
p = subprocess.run([sys.executable, str(here / "orch.py"), str(task), "--repo", str(repo), "--hx", hx,
|
||||||
|
"--gate", "test -f out.txt", "--timeout", str(timeout), "--telemetry", str(tele)],
|
||||||
|
capture_output=True, text=True)
|
||||||
|
return p.returncode, json.loads(p.stdout.splitlines()[-1])
|
||||||
|
|
||||||
|
|
||||||
|
rc, r = run("ok", "write it")
|
||||||
|
assert (rc, r["result"], r["gate_exit"]) == (0, "done", 0), r
|
||||||
|
assert r["usage"] == {"input_tokens": 10, "output_tokens": 5}, r
|
||||||
|
files = subprocess.run(["git", "ls-tree", "--name-only", "orch/ok"], cwd=repo, capture_output=True, text=True).stdout
|
||||||
|
assert "out.txt" in files, files # hx's work committed on the branch
|
||||||
|
|
||||||
|
rc, r = run("hxfail", "FAIL")
|
||||||
|
assert (rc, r["result"]) == (1, "hx-failed") and r["gate_exit"] is None, r
|
||||||
|
rc, r = run("crash", "CRASH")
|
||||||
|
assert r["result"] == "hx-failed" and r["hx_exit"] == 2, r
|
||||||
|
rc, r = run("slow", "HANG", timeout=1)
|
||||||
|
assert r["result"] == "timeout", r
|
||||||
|
|
||||||
|
# gate failure: hx writes nothing relevant because gate wants a file that never exists
|
||||||
|
task = tmp / "gate.md"
|
||||||
|
task.write_text("# Gate\nwrite it\n")
|
||||||
|
p = subprocess.run([sys.executable, str(here / "orch.py"), str(task), "--repo", str(repo), "--hx", hx,
|
||||||
|
"--gate", "test -f nope", "--telemetry", str(tele)], capture_output=True, text=True)
|
||||||
|
assert json.loads(p.stdout)["result"] == "gate-failed", p.stdout
|
||||||
|
|
||||||
|
lines = [json.loads(l) for l in tele.read_text().splitlines()]
|
||||||
|
assert [l["result"] for l in lines] == ["done", "hx-failed", "hx-failed", "timeout", "gate-failed"], lines
|
||||||
|
print("check ok")
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""Fake hx for the O0 spike: `fake_hx.py -p BODY --mode json`. BODY keywords: FAIL (error), CRASH (exit 2), HANG (sleep)."""
|
||||||
|
import json, sys, time
|
||||||
|
|
||||||
|
body = sys.argv[sys.argv.index("-p") + 1]
|
||||||
|
|
||||||
|
|
||||||
|
def emit(o):
|
||||||
|
print(json.dumps(o), flush=True)
|
||||||
|
|
||||||
|
|
||||||
|
emit({"type": "session_start"})
|
||||||
|
if "HANG" in body:
|
||||||
|
time.sleep(60)
|
||||||
|
if "CRASH" in body:
|
||||||
|
sys.exit(2)
|
||||||
|
emit({"type": "turn_start"})
|
||||||
|
if "FAIL" not in body:
|
||||||
|
open("out.txt", "w").write(body)
|
||||||
|
emit({"type": "tool_result", "name": "write", "args": {"path": "out.txt"}, "content": "ok", "is_error": False})
|
||||||
|
emit({"type": "done", "error": "boom" if "FAIL" in body else None,
|
||||||
|
"usage": {"input_tokens": 10, "output_tokens": 5}, "session": "/tmp/fake-session.jsonl"})
|
||||||
|
sys.exit(1 if "FAIL" in body else 0)
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
"""O0 spike: one task -> worktree -> `hx -p ... --mode json` -> gate -> JSONL telemetry. Throwaway; Rust rewrite is O1.
|
||||||
|
|
||||||
|
usage: orch.py TASK.md --repo DIR [--hx CMD] [--gate CMD] [--timeout SECS] [--telemetry FILE]
|
||||||
|
Result: done | gate-failed | hx-failed | timeout. Exit code 0 only for done.
|
||||||
|
"""
|
||||||
|
import argparse, json, pathlib, shlex, subprocess, sys, time
|
||||||
|
|
||||||
|
|
||||||
|
def git(cwd, *a):
|
||||||
|
return subprocess.run(["git", "-c", "user.name=orch", "-c", "user.email=orch@localhost", *a],
|
||||||
|
cwd=cwd, capture_output=True, text=True, check=True).stdout
|
||||||
|
|
||||||
|
|
||||||
|
def read_task(p):
|
||||||
|
lines = pathlib.Path(p).read_text().splitlines()
|
||||||
|
title = next((l.lstrip("# ").strip() for l in lines if l.startswith("#")), pathlib.Path(p).stem)
|
||||||
|
body = "\n".join(l for l in lines if not l.startswith("#")).strip() or title
|
||||||
|
return title, body
|
||||||
|
|
||||||
|
|
||||||
|
def run_hx(hx, body, cwd, timeout):
|
||||||
|
"""Returns (result, done_event, rc, events). result is None when hx finished normally."""
|
||||||
|
try:
|
||||||
|
p = subprocess.run([*shlex.split(hx), "-p", body, "--mode", "json"], cwd=cwd,
|
||||||
|
capture_output=True, text=True, timeout=timeout)
|
||||||
|
except subprocess.TimeoutExpired: # subprocess.run kills the child
|
||||||
|
return "timeout", None, None, 0
|
||||||
|
events = []
|
||||||
|
for l in p.stdout.splitlines():
|
||||||
|
try:
|
||||||
|
events.append(json.loads(l))
|
||||||
|
except ValueError:
|
||||||
|
pass # ponytail: non-JSON stdout lines are dropped; keep them in a log from O1 on
|
||||||
|
done = next((e for e in reversed(events) if e.get("type") == "done"), None)
|
||||||
|
if p.returncode != 0 or done is None or done.get("error"):
|
||||||
|
return "hx-failed", done, p.returncode, len(events)
|
||||||
|
return None, done, p.returncode, len(events)
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv=None):
|
||||||
|
a = argparse.ArgumentParser()
|
||||||
|
a.add_argument("task")
|
||||||
|
a.add_argument("--repo", required=True)
|
||||||
|
a.add_argument("--hx", default="hx")
|
||||||
|
a.add_argument("--gate", default="sh tests/all.sh")
|
||||||
|
a.add_argument("--timeout", type=float, default=1800)
|
||||||
|
a.add_argument("--telemetry", default="telemetry.jsonl")
|
||||||
|
a = a.parse_args(argv)
|
||||||
|
|
||||||
|
repo = pathlib.Path(a.repo).resolve()
|
||||||
|
tid = pathlib.Path(a.task).stem
|
||||||
|
title, body = read_task(a.task)
|
||||||
|
branch, wt = f"orch/{tid}", repo.parent / ".orch" / tid
|
||||||
|
git(repo, "worktree", "add", "-b", branch, str(wt), "HEAD")
|
||||||
|
|
||||||
|
t0 = time.time()
|
||||||
|
result, done, hx_rc, n_events = run_hx(a.hx, body, wt, a.timeout)
|
||||||
|
gate_rc = None
|
||||||
|
if git(wt, "status", "--porcelain"): # keep hx's work on the branch even if the gate fails
|
||||||
|
git(wt, "add", "-A")
|
||||||
|
git(wt, "commit", "-qm", f"orch: {title}")
|
||||||
|
if result is None:
|
||||||
|
gate_rc = subprocess.run(a.gate, shell=True, cwd=wt).returncode
|
||||||
|
result = "done" if gate_rc == 0 else "gate-failed"
|
||||||
|
|
||||||
|
rec = {"task": tid, "title": title, "branch": branch, "result": result, "hx_exit": hx_rc,
|
||||||
|
"gate_exit": gate_rc, "events": n_events, "usage": (done or {}).get("usage"),
|
||||||
|
"session": (done or {}).get("session"), "seconds": round(time.time() - t0, 2)}
|
||||||
|
with open(a.telemetry, "a") as f:
|
||||||
|
f.write(json.dumps(rec) + "\n")
|
||||||
|
print(json.dumps(rec))
|
||||||
|
return 0 if result == "done" else 1
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
sys.exit(main())
|
||||||
Reference in new issue
Block a user