Files
ai-agent/backend/test_converter_tables.py
gabrielvidalandClaude Opus 5.5 65f3eb3468 feat(converters): pi, opencode and agy write v1 transcripts
runner/cli.mjs (pi), sidecar/opencode_mirror.py and sidecar/agy_mirror.py
write v1 events (docs/transcript-format.md) instead of Claude-Code-shaped
records: a header naming the harness, each model step whole — its call, its
blocks, its usage, in one append — tool calls under their neutral name with
the harness's own in `native`, the hub envelopes (author tag, inbox note)
unwrapped and the old text conventions said as events. Shared Python helpers
in sidecar/transcript_v1.py (Out: header, moved facts, dedup keys rebuilt
from the file, a v1 header before the first v1 line appended to an
old-format file); backend/test_converter_tables.py keeps its copies of the
hub's tables equal to the hub's.

- opencode: a step goes out once it has ended (its tokens come then, after
  its tools ran), so a step seen while it streamed no longer splits into
  content + a late usage record (quirk a). A step still running when its
  turn ends (idle, a new turn, the next step) goes out as it stands; a step
  that failed or was aborted and left nothing is not written (its error, or
  the interrupt, says it).
- agy: what closes a turn (turn_end, the interrupt, the interrupted results,
  the API-error call) carries the latest time agy recorded in it, never the
  mirror's clock (quirk c).
- pi: a fork copies its source's v1 history under the new id.
- gen_progress.tool_running reads v1 transcripts too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 11:13:21 +01:00

89 lines
4.0 KiB
Python

"""The tables ``sidecar/transcript_v1.py`` copies from the hub's reader of
the old format (tool names, API-error kinds, the text conventions a user
message can carry) — the converters write what that reader made of a record,
so the copies must not drift; ``prompt_events`` folds like the old user
record holding the same text did."""
import importlib.util
import json
import pathlib
import unittest
import conversations as c
from conversations.parse import errors
from conversations.parse.readers import LegacyReader, claudetext, hubtext, toolnames
HERE = pathlib.Path(__file__).resolve().parent
def canonical(obj) -> str:
return json.dumps(obj, sort_keys=True, ensure_ascii=False)
def _sidecar(name: str):
spec = importlib.util.spec_from_file_location(
f"_sidecar_{name}", HERE.parent / "sidecar" / f"{name}.py")
mod = importlib.util.module_from_spec(spec)
spec.loader.exec_module(mod)
return mod
class ConverterTablesAreTheHubs(unittest.TestCase):
"""``sidecar/transcript_v1.py`` copies what the hub's reader of the old
format knew; the copies must not drift."""
@classmethod
def setUpClass(cls):
cls.v1 = _sidecar("transcript_v1")
def test_tool_names(self):
self.assertEqual(self.v1.CLAUDE, toolnames.CLAUDE)
self.assertEqual(self.v1.LEGACY, toolnames.LEGACY)
self.assertEqual(self.v1.LEGACY_OWN, toolnames.LEGACY_OWN)
for name in [*toolnames.CLAUDE, *toolnames.LEGACY, "Execute", "mcp__s__t", "mcp_s_t",
"manage_task", "foo"]:
for h in ("opencode", "agy"):
self.assertEqual(self.v1.neutral(name, h), toolnames.claude_name(name, h),
(name, h))
def test_error_kinds(self):
self.assertEqual(self.v1._API_ERROR_FIELD, errors._API_ERROR_FIELD)
self.assertEqual([(k, r.pattern, r.flags) for k, r in self.v1._API_ERROR_TEXT],
[(k, r.pattern, r.flags) for k, r in errors._API_ERROR_TEXT])
def test_text_conventions(self):
for name, mod in (("_INBOX_RE", hubtext), ("_AUTHOR_RE", hubtext),
("_INTERRUPT_RE", claudetext), ("_SLASH_CMD_RE", claudetext),
("_SLASH_ARGS_RE", claudetext), ("_SKILL_BODY_RE", claudetext),
("_SKILL_NOTE_RE", claudetext), ("_COMPACT_LEAD_RE", claudetext),
("_COMPACT_REMINDER_RE", claudetext), ("_NO_RESPONSE_RE", claudetext)):
mine, hubs = getattr(self.v1, name), getattr(mod, name)
self.assertEqual((mine.pattern, mine.flags), (hubs.pattern, hubs.flags), name)
def test_a_prompt_reads_like_the_old_record_did(self):
"""``prompt_events`` == what the legacy reader made of a user record
holding the same text."""
texts = ["fix it", "[Request interrupted by user]", "<system-reminder>\nx\n</system-reminder>",
'<ai-agent-from user="u1" name="Ann" role="guest"/>\nhello',
"[Message from the ai-agent hub (child) — sent by the app, not typed by your user.]"
"\n\nreport", "<command-name>/greet</command-name>", "", "`! ls`\n\n```\na\n```"]
for t in texts:
r = LegacyReader(full=True)
r.feed({"type": "user", "sessionId": "s", "timestamp": "2026-11-01T10:00:00.000Z",
"message": {"role": "user", "content": "x"}})
old = r.feed({"type": "user", "sessionId": "s", "timestamp": "2026-11-01T10:00:01.000Z",
"message": {"role": "user", "content": t}})
new = self.v1.prompt_events(t, "2026-11-01T10:00:01.000Z")
st_old, st_new = c.ParserState(pathlib.Path("x"), full=True), \
c.ParserState(pathlib.Path("x"), full=True)
for ev in old:
st_old.apply(ev)
for ev in new:
st_new.apply(ev)
self.assertEqual(canonical(st_new.summary()), canonical(st_old.summary()), repr(t))
if __name__ == "__main__":
unittest.main()