Files
jaredandClaude Opus 5.5 1ef68f12fb
tests / unittest (push) Failing after 28s
feat: local drafter (pi against an OpenAI-compatible server on your own hardware)
drafter: local runs the pi agent against the model already loaded on a self-hosted server,
as an unprivileged user, in the same sandbox as the Claude drafter. It fails soft when the
server is unreachable or has no model loaded. Both drafters now refuse to continue if
git metadata changed during drafting, and strip project pi configuration from the sandbox.
Tested live against a self-hosted server: one run, 12 turns, docs PR opened.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-12 17:12:42 -05:00

104 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Stand-in for the pi binary. Prints a JSON event stream like `pi -p --mode json`.
Settings come from fake_pi.json beside this script, because the drafter's environment
is an allowlist. Keys:
sentinel (file touched when invoked), env_file (file receiving the env var names),
argv_file (file receiving argv as JSON), cwd_file (file receiving the working
directory), home_file (file receiving $HOME), models_copy (file receiving the
models.json pi would read), task_copy (file receiving the @file task), system_copy
(file receiving the --append-system-prompt file), edit (path to append a line to,
relative to the working directory or absolute), write (text to append instead),
fail (end on an assistant error), exit_code (exit with this code), no_text (end
with an empty assistant message), list_file (file receiving the names at the top of the
working directory).
Stdin must be /dev/null: a real pi waits on an open stdin until EOF. When it is
not, this exits 3 without printing anything.
"""
import json
import os
import sys
from pathlib import Path
config_path = Path(__file__).resolve().with_name("fake_pi.json")
config = json.loads(config_path.read_text()) if config_path.exists() else {}
stdin, devnull = os.fstat(0), os.stat(os.devnull)
if (stdin.st_dev, stdin.st_ino, stdin.st_rdev) != (devnull.st_dev, devnull.st_ino,
devnull.st_rdev):
print("stdin is not /dev/null", file=sys.stderr)
sys.exit(3)
def emit(event):
print(json.dumps(event))
def assistant(text, stop="stop", error=None):
msg = {"role": "assistant", "content": [{"type": "text", "text": text}] if text else [],
"stopReason": stop, "usage": {"input": 100, "output": 20}}
if error:
msg["errorMessage"] = error
return msg
if config.get("sentinel"):
Path(config["sentinel"]).write_text("invoked\n", encoding="utf-8")
if config.get("env_file"):
Path(config["env_file"]).write_text("\n".join(sorted(os.environ)), encoding="utf-8")
if config.get("argv_file"):
Path(config["argv_file"]).write_text(json.dumps(sys.argv), encoding="utf-8")
if config.get("cwd_file"):
Path(config["cwd_file"]).write_text(os.getcwd(), encoding="utf-8")
if config.get("list_file"):
Path(config["list_file"]).write_text("\n".join(sorted(os.listdir("."))),
encoding="utf-8")
if config.get("home_file"):
Path(config["home_file"]).write_text(os.environ.get("HOME", ""), encoding="utf-8")
if config.get("models_copy"):
models = Path(os.environ["HOME"]) / ".pi" / "agent" / "models.json"
Path(config["models_copy"]).write_text(models.read_text(encoding="utf-8"),
encoding="utf-8")
args = sys.argv[sys.argv.index("--") + 1:] if "--" in sys.argv else []
task = next((a[1:] for a in args if a.startswith("@")), None)
if config.get("task_copy") and task:
Path(config["task_copy"]).write_text(Path(task).read_text(encoding="utf-8"),
encoding="utf-8")
if config.get("system_copy") and "--append-system-prompt" in sys.argv:
source = Path(sys.argv[sys.argv.index("--append-system-prompt") + 1])
Path(config["system_copy"]).write_text(source.read_text(encoding="utf-8"),
encoding="utf-8")
emit({"type": "session", "version": 3, "id": "s", "cwd": os.getcwd()})
emit({"type": "agent_start"})
emit({"type": "turn_start"})
emit({"type": "message_end", "message": {"role": "user", "content": [
{"type": "text", "text": "task"}]}})
edits = []
target = config.get("edit")
if target:
path = Path(target)
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("a", encoding="utf-8") as fh:
fh.write(config.get("write", "Edited by the local drafter.") + "\n")
edits.append({"path": target, "why": "updated for the change"})
tool = assistant("", stop="toolUse")
emit({"type": "message_end", "message": tool})
emit({"type": "turn_end", "message": tool, "toolResults": []})
emit({"type": "turn_start"})
if config.get("fail"):
final = assistant("", stop="error", error="Connection error.\nsecond line")
elif config.get("no_text"):
final = assistant("")
else:
summary = {"edits": edits, "declined": []}
final = assistant("Done.\n\n```json\n" + json.dumps(summary) + "\n```")
emit({"type": "message_end", "message": final})
emit({"type": "turn_end", "message": final, "toolResults": []})
emit({"type": "agent_end", "messages": [final]})
sys.exit(int(config.get("exit_code", 0)))