feat: local drafter (pi against a self-run model server)
drafter: local runs the pi coding agent against the model resident on an OpenAI-compatible server at the new local_url input, keyed by the LOCAL_MODEL_API_KEY secret. It asks the server which model is loaded and names exactly that one, so it never evicts the resident model. An unreachable server or no loaded model posts the stage 1 flags with a note and exits 0; a drafter that ran and failed still exits 1. pi runs in the existing sandbox with read, ls, edit and write, every project-local pi file and AGENTS.md ignored, stdin from /dev/null, the task passed as an @file, and an allowlisted environment. As root with DOCS_REVIEW_DRAFTER_USER set, it runs as that unprivileged user, which owns only the sandbox and pi's temporary home. Both drafters now hash the checkout's .git/config and .git/hooks before drafting and stop with no further git command if either changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9dea56ce98
commit
28b993cb23
14 files changed
+1042
-30
No files matched your search
@@ -0,0 +1,99 @@
|
||||
#!/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).
|
||||
|
||||
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("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)))
|
||||
Reference in new issue
Block a user