Files
gabrielvidalandClaude Opus 5.5 74041724aa test(ab): mirror-replay — the three transcript converters replayed, A/B and incremental
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>
2026-11-23 23:46:29 +01:00

247 lines
14 KiB
Python

"""A synthetic opencode V2 database, as the ordered writes opencode makes.
Replayed by ``mirror-replay.py`` into a fresh SQLite file holding the three
tables the mirror reads (``SCHEMA``). Ops:
{"op": "session", id, parent?, fork_of?, directory, title?, agent?, t}
{"op": "title", id, title} opencode names a session after its first turn
{"op": "put", sid, mid, type, data} insert a message row, or rewrite it in place
(a step streams, then settles: same row)
{"op": "forks", map} the fork route writes ids.json {hub uuid: ses_…}
Every write bumps the session's ``event_sequence`` (what the mirror polls).
Covered: user text + attachment, a title with ``<think>``, reasoning, inline
``<think>`` split (closed and unclosed), a step seen streaming then settled
(content first, tool result, usage last), a step already over when first seen
(one record), a multi-file ``patch`` (Write + MultiEdit + DeleteFile) and a
one-chunk ``patch`` (Edit), a tool error, a metered step's ``costUSD``, a skill
call, a subagent (child session, sidechain + meta.json, linked to its running
call), a background subagent's completion notice, a failed step + the idle row
repeating its error (one API-error turn, with its reset time), an aborted step
+ an interrupted turn, a compaction, a ``!`` shell row, a fork filed under the
hub's uuid (ids.json written after opencode created it), a terminal session
(uuid5 id) in a directory that exists (git branch).
"""
from . import FROZEN_NOW
ACCOUNT = "chatgpt-work"
SES = "ses_aaaaaaaa-0000-4000-8000-000000000001" # a hub run: ses_<uuid>
CHILD = "ses_child00000000000000001"
BG_CHILD = "ses_bgchild0000000000000002"
FORK = "ses_forkmintedbyopencode01" # an id opencode minted
FORK_HUB = "bbbbbbbb-0000-4000-8000-000000000002"
TERM = "ses_terminal0000000000000003" # started in a terminal
CWD = "/home/u/projects/demo"
T0 = 1_792_000_000_000 # 2026-10-14, ms
SUBAGENT_PREFIX = "You are a subagent spawned by another session.\n"
GPT = {"id": "gpt-5.5", "providerID": "openai", "variant": "high"}
OR_MODEL = {"id": "qwen/qwen3.8-27b", "providerID": "openrouter", "variant": "default"}
LOCAL = {"id": "inkling-2-mini", "providerID": "evox2-inkling"}
PATCH = """*** Begin Patch
*** Add File: docs/new.md
+# New
+hello
*** Update File: src/calc.py
@@ def add(a, b):
def add(a, b):
- return a+b
+ return a + b
@@
-x = 1
+x = 2
*** Delete File: old.txt
*** End Patch"""
ONE_CHUNK = "*** Begin Patch\n*** Update File: src/calc.py\n@@\n-y = 1\n+y = 3\n*** End Patch"
def t(ms: int) -> int:
return T0 + ms
def tokens(i, o, r=0, cr=0, cw=0):
return {"input": i, "output": o, "reasoning": r, "cache": {"read": cr, "write": cw}}
def step(content, *, created, streamed=None, completed=None, model=GPT, tok=None,
finish=None, cost=0, error=None, agent="build"):
tm = {"created": t(created)}
if streamed is not None:
tm["streamed"] = t(streamed)
if completed is not None:
tm["completed"] = t(completed)
d = {"agent": agent, "model": model, "content": content, "time": tm, "cost": cost}
if tok:
d["tokens"] = tok
if finish:
d["finish"] = finish
if error:
d["error"] = error
return d
def tool(cid, name, status, inp, *, ran, completed=None, content=None, error=None, meta=None):
st = {"status": status, "input": inp, "metadata": meta or {}}
if content is not None:
st["content"] = [{"type": "text", "text": content}]
if error is not None:
st["error"] = {"message": error}
tm = {"created": t(ran - 50), "ran": t(ran)}
if completed is not None:
tm["completed"] = t(completed)
return {"type": "tool", "id": cid, "name": name, "state": st, "time": tm}
RATE = {"type": "provider.rate-limit", "message": "ChatGPT usage limit reached.",
"status": 429, "response": {"body": '{"error": {"type": "usage_limit_reached", '
'"resets_at": 1792400000}}'}}
OPS: list[dict] = [
# ── turn 1: read, patch, a failing shell call, the answer ──
{"op": "session", "id": SES, "directory": CWD, "t": t(0)},
{"op": "put", "sid": SES, "mid": "msg_u01", "type": "user", "data": {
"text": "fix the add() bug\n", "time": {"created": t(100)},
"files": [{"mime": "image/png", "name": "shot.png"}],
"skills": [{"id": "dev-env"}]}},
{"op": "title", "id": SES, "title": "<think>a short title</think>Fix the add() bug"},
# streaming: reasoning + a running read → content goes out, no usage yet
{"op": "put", "sid": SES, "mid": "msg_a02", "type": "assistant", "data": step(
[{"type": "reasoning", "text": "Let me read calc.py first."},
tool("call_read", "read", "running", {"path": "src/calc.py"}, ran=300)],
created=200, streamed=290)},
# the read settles, the step ends: its result, then its usage
{"op": "put", "sid": SES, "mid": "msg_a02", "type": "assistant", "data": step(
[{"type": "reasoning", "text": "Let me read calc.py first."},
tool("call_read", "read", "completed", {"path": "src/calc.py"}, ran=300, completed=350,
content="def add(a, b):\n return a+b\n")],
created=200, streamed=290, completed=400, tok=tokens(1000, 50, 20, 9000),
finish="tool-calls")},
{"op": "put", "sid": SES, "mid": "msg_a03", "type": "assistant", "data": step(
[{"type": "text", "text": "<think>A patch fixes it.</think>I'll patch it."},
tool("call_patch", "patch", "running", {"patchText": PATCH}, ran=520),
tool("call_sh", "shell", "running", {"command": "pytest -q", "timeout": 60000},
ran=530)],
created=500, streamed=510)},
{"op": "put", "sid": SES, "mid": "msg_a03", "type": "assistant", "data": step(
[{"type": "text", "text": "<think>A patch fixes it.</think>I'll patch it."},
tool("call_patch", "patch", "completed", {"patchText": PATCH}, ran=520, completed=560,
content="Success. Updated the following files:\nA docs/new.md\nM src/calc.py\n"
"D old.txt"),
tool("call_sh", "shell", "error", {"command": "pytest -q", "timeout": 60000},
ran=530, completed=600, content="1 failed", error="exit status 1")],
created=500, streamed=510, completed=610, tok=tokens(2000, 300, 0, 10000, 500),
finish="tool-calls")},
# a metered step, already over when first seen: one record, with costUSD
{"op": "put", "sid": SES, "mid": "msg_a04", "type": "assistant", "data": step(
[{"type": "text", "text": "Fixed: `add` now returns `a + b`."},
tool("call_p2", "patch", "completed", {"patchText": ONE_CHUNK}, ran=650, completed=660,
content="Success.")],
created=640, streamed=670, completed=700, model=OR_MODEL, tok=tokens(500, 40),
finish="stop", cost=0.0123)},
{"op": "put", "sid": SES, "mid": "msg_i05", "type": "idle", "data": {
"outcome": "succeeded", "time": {"created": t(710)}}},
# ── turn 2: a skill, a subagent (foreground) and a background one ──
{"op": "put", "sid": SES, "mid": "msg_u06", "type": "user", "data": {
"text": "greet, then count the files with a helper", "time": {"created": t(1000)}}},
{"op": "put", "sid": SES, "mid": "msg_a07", "type": "assistant", "data": step(
[tool("call_skill", "skill", "completed", {"id": "greet"}, ran=1020, completed=1030,
content='<skill_content name="greet">\n# Skill: greet\n\nSay hi.\n\n'
'Base directory for this skill: /s/greet\nRelative paths…',
meta={"directory": "/s/greet"}),
tool("call_sub", "subagent", "running",
{"agent": "explore", "description": "Count files", "prompt": "count"}, ran=1100),
tool("call_bg", "subagent", "running",
{"agent": "explore", "description": "Watch CI", "prompt": "watch",
"background": True}, ran=1110)],
created=1010, streamed=1090)},
{"op": "session", "id": CHILD, "parent": SES, "directory": CWD, "title": "Count files",
"agent": "explore", "t": t(1150)},
{"op": "put", "sid": CHILD, "mid": "msg_c01", "type": "user", "data": {
"text": SUBAGENT_PREFIX + "count", "time": {"created": t(1151)}}},
{"op": "session", "id": BG_CHILD, "parent": SES, "directory": CWD, "title": "Watch CI",
"agent": "explore", "t": t(1160)},
{"op": "put", "sid": CHILD, "mid": "msg_c02", "type": "assistant", "data": step(
[{"type": "text", "text": "3 files."}], created=1160, streamed=1190, completed=1200,
model=LOCAL, tok=tokens(300, 10), finish="stop", agent="explore")},
{"op": "put", "sid": CHILD, "mid": "msg_c03", "type": "idle", "data": {
"outcome": "succeeded", "time": {"created": t(1210)}}},
{"op": "put", "sid": BG_CHILD, "mid": "msg_b01", "type": "user", "data": {
"text": SUBAGENT_PREFIX + "watch", "time": {"created": t(1161)}}},
{"op": "put", "sid": SES, "mid": "msg_a07", "type": "assistant", "data": step(
[tool("call_skill", "skill", "completed", {"id": "greet"}, ran=1020, completed=1030,
content='<skill_content name="greet">\n# Skill: greet\n\nSay hi.\n\n'
'Base directory for this skill: /s/greet\nRelative paths…',
meta={"directory": "/s/greet"}),
tool("call_sub", "subagent", "completed",
{"agent": "explore", "description": "Count files", "prompt": "count"},
ran=1100, completed=1220,
content=f'<subagent sessionID="{CHILD}" state="completed">\n3 files.\n</subagent>',
meta={"sessionID": CHILD}),
tool("call_bg", "subagent", "completed",
{"agent": "explore", "description": "Watch CI", "prompt": "watch",
"background": True}, ran=1110, completed=1120,
content=f'<subagent sessionID="{BG_CHILD}" state="running">\nStarted.\n</subagent>',
meta={"sessionID": BG_CHILD, "status": "running"})],
created=1010, streamed=1090, completed=1230, tok=tokens(3000, 100, 0, 12000),
finish="tool-calls")},
{"op": "put", "sid": BG_CHILD, "mid": "msg_b02", "type": "assistant", "data": step(
[{"type": "text", "text": "<think>poll the CI\nCI is green."}], created=1300,
streamed=1390, completed=1400, model=LOCAL, tok=tokens(200, 5), finish="stop",
agent="explore")},
{"op": "put", "sid": SES, "mid": "msg_s08", "type": "synthetic", "data": {
"text": "Background subagent Watch CI finished: CI is green.",
"metadata": {"source": "subagent", "childID": BG_CHILD, "state": "completed"},
"time": {"created": t(1410)}}},
# a failed step, then the idle row that repeats its error: one API-error turn
{"op": "put", "sid": SES, "mid": "msg_a09", "type": "assistant", "data": step(
[], created=1500, completed=1510, error=RATE, finish="error")},
{"op": "put", "sid": SES, "mid": "msg_i10", "type": "idle", "data": {
"outcome": "failed", "error": RATE, "time": {"created": t(1520)}}},
# ── turn 3: interrupted mid-tool, then a compaction and a `!` shell row ──
{"op": "put", "sid": SES, "mid": "msg_u11", "type": "user", "data": {
"text": "try again", "time": {"created": t(2000)}}},
{"op": "put", "sid": SES, "mid": "msg_a12", "type": "assistant", "data": step(
[tool("call_long", "shell", "running", {"command": "sleep 100"}, ran=2020)],
created=2010, streamed=2015)},
{"op": "put", "sid": SES, "mid": "msg_a12", "type": "assistant", "data": step(
[tool("call_long", "shell", "error", {"command": "sleep 100"}, ran=2020,
completed=2100, error="Tool execution aborted")],
created=2010, streamed=2015, completed=2100, tok=tokens(100, 3),
error={"type": "aborted", "message": "aborted"}, finish="aborted")},
{"op": "put", "sid": SES, "mid": "msg_i13", "type": "idle", "data": {
"outcome": "interrupted", "time": {"created": t(2110)}}},
{"op": "put", "sid": SES, "mid": "msg_k14", "type": "compaction", "data": {
"status": "running", "time": {"created": t(2200)}}},
{"op": "put", "sid": SES, "mid": "msg_k14", "type": "compaction", "data": {
"status": "completed", "reason": "manual", "summary": "We fixed add().",
"model": GPT, "tokens": tokens(40000, 800), "time": {"created": t(2200)}}},
{"op": "put", "sid": SES, "mid": "msg_w15", "type": "shell", "data": {
"command": "git status", "status": "exited",
"output": {"output": "On branch main\nnothing to commit"},
"time": {"created": t(2300)}}},
# ── a fork: opencode mints its id, the fork route maps it a moment later ──
{"op": "session", "id": FORK, "fork_of": SES, "directory": CWD,
"title": "Fix the add() bug (fork)", "t": int(FROZEN_NOW * 1000) - 5000},
{"op": "put", "sid": FORK, "mid": "msg_f01", "type": "user", "data": {
"text": "continue in the fork", "time": {"created": t(3000)}}},
{"op": "forks", "map": {FORK_HUB: FORK}},
{"op": "put", "sid": FORK, "mid": "msg_f02", "type": "assistant", "data": step(
[{"type": "text", "text": "Forked and continuing."}], created=3010, streamed=3020,
completed=3030, tok=tokens(50, 5), finish="stop")},
{"op": "put", "sid": FORK, "mid": "msg_f03", "type": "idle", "data": {
"outcome": "succeeded", "time": {"created": t(3040)}}},
# ── a terminal session (derived uuid5) in a directory that exists ──
{"op": "session", "id": TERM, "directory": "/", "title": "Hello", "t": t(4000)},
{"op": "put", "sid": TERM, "mid": "msg_t01", "type": "user", "data": {
"text": "hi", "time": {"created": t(4001)}}},
{"op": "put", "sid": TERM, "mid": "msg_t02", "type": "assistant", "data": step(
[{"type": "text", "text": "Hello!"}], created=4002, streamed=4003, completed=4004,
model=LOCAL, tok=tokens(10, 2), finish="stop")},
]