scripts/ab/mirror-replay.py replays native harness data through opencode_mirror.py, agy_mirror.py and runner/cli.mjs (TranscriptWriter, fed by a stub pi) and dumps the JSONL they write: --base REF diffs base vs working tree byte for byte, --incremental feeds opencode / agy sources in steps (per op, seeded chunks with half-written lines, a fresh mirror per poll) and requires the hub's parse to match the one-shot output, --real adds read-only copies of the lab's opencode db and agy brain/. Committed synthetic fixtures (mirror_fixtures/) and sidecar/test_mirror_replay.py run without private data. Clock, output paths, git branch, run liveness and temp paths are frozen or normalised; no product code changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
5.7 KiB
Python
111 lines
5.7 KiB
Python
"""Recorded ``pi --mode json`` runs, replayed through ``runner/cli.mjs`` by a
|
|
stub ``pi`` (``pi-stub.sh``) that prints a scenario's events and exits with
|
|
its code. Each scenario: the runner's argv, the events pi prints, pi's exit
|
|
code.
|
|
|
|
Covered: inline ``<think>`` split (one and two blocks), native thinking blocks,
|
|
tool calls mapped to Claude's names (read, bash with its timeout → ms, a
|
|
one-edit and a two-edit ``edit``, grep -i, find, ls, an extension tool passed
|
|
through), tool results (text, an image placeholder, an error), ``costUSD``
|
|
present / zero / absent, an error turn held until pi settles and dropped by
|
|
``auto_retry_start``, an error flushed by the next message, a failed call's
|
|
empty shell skipped, ``agent_settled`` → ``turn_end``, a pi with no
|
|
``agent_settled`` (turn_end on exit), a pi that died before answering, and the
|
|
argv the runner gives pi for a lean / scoped / thinking run.
|
|
"""
|
|
|
|
SID = "5e55105e-0000-4000-8000-0000000000a1"
|
|
MODEL = "qwen/qwen3.8-27b"
|
|
# pi's provider registry for the replay: both models served by a provider
|
|
# that is neither OpenRouter (no key needed) nor evox2* (no wake-up).
|
|
MODELS_JSON = {"providers": {"replay": {"baseUrl": "http://127.0.0.1:9/v1", "models": [
|
|
{"id": MODEL}, {"id": "replay-small", "defaultThinking": "medium"}]}}}
|
|
|
|
|
|
def assistant(content, *, stop="toolUse", usage=None, error=None, model=MODEL):
|
|
m = {"role": "assistant", "content": content, "model": model, "provider": "replay",
|
|
"stopReason": stop, "usage": usage or {"input": 0, "output": 0}}
|
|
if error is not None:
|
|
m["errorMessage"] = error
|
|
return {"type": "message_end", "message": m}
|
|
|
|
|
|
def result(cid, *content, error=False):
|
|
return {"type": "message_end", "message": {"role": "toolResult", "toolCallId": cid,
|
|
"content": list(content), "isError": error}}
|
|
|
|
|
|
def text(s):
|
|
return {"type": "text", "text": s}
|
|
|
|
|
|
TURN = [
|
|
{"type": "agent_start"},
|
|
{"type": "message_end", "message": {"role": "user", "content": [text("fix it")]}},
|
|
assistant([{"type": "thinking", "thinking": "Read first, then edit."},
|
|
text("<think>check the file</think>Reading the file."),
|
|
{"type": "toolCall", "id": "call_1", "name": "read",
|
|
"arguments": {"path": "/w/a.py", "offset": 1}},
|
|
{"type": "toolCall", "id": "call_2", "name": "bash",
|
|
"arguments": {"command": "ls", "timeout": 30}}],
|
|
usage={"input": 1200, "output": 80, "cacheRead": 300, "cacheWrite": 10,
|
|
"cost": {"total": 0.0042}}),
|
|
result("call_1", text("def add(a, b):\n return a+b")),
|
|
result("call_2", text("ls: cannot open"), {"type": "image", "data": "AAAA"}, error=True),
|
|
assistant([{"type": "toolCall", "id": "call_3", "name": "edit", "arguments": {
|
|
"path": "/w/a.py", "edits": [{"oldText": "a+b", "newText": "a + b"}]}},
|
|
{"type": "toolCall", "id": "call_4", "name": "edit", "arguments": {
|
|
"path": "/w/b.py", "edits": [{"oldText": "x", "newText": "y"},
|
|
{"oldText": "1", "newText": "2"}]}},
|
|
{"type": "toolCall", "id": "call_5", "name": "grep",
|
|
"arguments": {"pattern": "add", "caseInsensitive": True}},
|
|
{"type": "toolCall", "id": "call_6", "name": "find",
|
|
"arguments": {"pattern": "*.py"}},
|
|
{"type": "toolCall", "id": "call_7", "name": "ls", "arguments": {"path": "/w"}},
|
|
{"type": "toolCall", "id": "call_8", "name": "web_fetch",
|
|
"arguments": {"url": "https://example.org"}}],
|
|
usage={"input": 1500, "output": 120, "cacheRead": 1200}),
|
|
*(result(f"call_{i}", text("ok")) for i in range(3, 9)),
|
|
assistant([text("<think>one</think>Fixed.<think>two</think> All tests pass.")],
|
|
stop="stop", usage={"input": 2000, "output": 30, "cost": {"total": 0}}),
|
|
{"type": "agent_settled"},
|
|
]
|
|
|
|
ERRORS = [
|
|
# a failure pi moves on from without a retry: flushed by the next message
|
|
assistant([text("Partial answer")], stop="error", usage={"input": 10, "output": 3},
|
|
error="500 internal error"),
|
|
# a failed call's empty shell: no assistant record, the error held…
|
|
assistant([], stop="error", error="terminated"),
|
|
# …and dropped: pi retries it
|
|
{"type": "auto_retry_start", "attempt": 1},
|
|
assistant([], stop="error",
|
|
error='429: {"error": {"message": "Rate limit exceeded for this key"}}'),
|
|
{"type": "agent_settled"},
|
|
]
|
|
|
|
LEAN = [
|
|
assistant([text("Done.")], stop="stop", usage={"input": 5, "output": 1},
|
|
model="replay-small"),
|
|
# no agent_settled: the runner closes the turn when pi exits
|
|
]
|
|
|
|
SCENARIOS: list[dict] = [
|
|
{"name": "turn", "argv": ["-p", "fix it", "--session-id", SID, "--model", MODEL],
|
|
"events": TURN, "exit": 0},
|
|
{"name": "errors", "argv": ["-p", "try", "--session-id", SID, "--model", MODEL,
|
|
"--thinking", "disabled"],
|
|
"events": ERRORS, "exit": 0},
|
|
{"name": "died", "argv": ["-p", "hi", "--resume", SID, "--model", MODEL],
|
|
"events": [], "exit": 3},
|
|
{"name": "lean", "argv": ["-p", "lean job", "--session-id", SID, "--model", "replay-small",
|
|
"--lean", "--system-prompt", "Be brief.", "--tools", "read,bash",
|
|
"--allow", "git", "--read", "/w:/s", "--thinking", "high",
|
|
"--output-format", "stream-json", "--remote-control"],
|
|
"events": LEAN, "exit": 0},
|
|
{"name": "scoped", "argv": ["-p", "guest job", "--session-id", SID, "--model", MODEL,
|
|
"--scoped", "--append-system-prompt", "Stay in /w.",
|
|
"--tools", "read"],
|
|
"events": LEAN, "exit": 0},
|
|
]
|