--verify (read-only, the default) rewrites every lab transcript and its hub archive copy in a temp dir and parses both with this tree's backend: with no fix the copy must parse exactly like the original (summary, full, chunked); each fix alone lists the parse paths it changes. --dry-run, --apply (skips a session with a live run, a file modified in the last 10 min or one that changes meanwhile; originals to ~/.cache/ai-agent/transcript-backups/ v1-rewrite-<date>/ with a manifest; mtime + 1 s), --restore (a file a run appended to since keeps its v1 tail behind a header), --prune (30 days). The workers' synced copies are left in the old format and counted: the FeedMirror syncs them by size, a rewrite here would be undone or corrupted. The v1 reader no longer times a call from a session event (a fact that changed, not what unblocked the call), so a session event keeps its time (the resource miner dates a worktree's branch with it); a converter's header takes the first moment it writes. A mirror that loaded a file in the old format re-checks its head before appending (it may have been rewritten). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
72 lines
3.0 KiB
Python
72 lines
3.0 KiB
Python
"""``V1Reader``: a transcript whose lines already are v1 events (its first
|
|
line ``{"t": "session", "v": 1, …}``) — what the converters write from step
|
|
1c on (``docs/transcript-format.md``).
|
|
|
|
Each line is validated (``events.validate``; an invalid one is dropped) and
|
|
handed on. The only work is grouping a model step: the fold folds a ``call``
|
|
and its blocks as one batch, with the call's ``usage`` first when the
|
|
converter knew it by then. So the reader holds a step's events until its
|
|
``usage`` arrives (the step is then complete: usage moved right after the
|
|
call) or any other event does (the step is flushed without usage; a later
|
|
``usage`` for that call is priced on its own, like opencode's late usage).
|
|
A step whose usage never comes is therefore shown once the next event
|
|
arrives. It also fills a call's ``elapsedMs`` from the previous event's
|
|
time when the converter left it out — what the claude reader does (a
|
|
``session`` event aside: a fact that changed, written with the event it
|
|
came with, is not what unblocked the call)."""
|
|
|
|
from ..events import validate
|
|
from ..records import _elapsed_ms
|
|
|
|
_BLOCKS = ("text", "thinking", "tool_call", "error")
|
|
|
|
|
|
class V1Reader:
|
|
def __init__(self, full: bool = True):
|
|
self.full = full
|
|
self.prev_at = None
|
|
self._open: list[dict] | None = None # the step being collected
|
|
self._calls: dict = {} # call id → its call event
|
|
|
|
def _flush(self) -> list[dict]:
|
|
step, self._open = self._open, None
|
|
return step or []
|
|
|
|
def _of_open(self, ev: dict) -> bool:
|
|
return self._open is not None and ev.get("call") in (None, self._open[0].get("id"))
|
|
|
|
def feed(self, rec: dict) -> list[dict]:
|
|
try:
|
|
ev = validate(rec)
|
|
except ValueError:
|
|
return []
|
|
t, at = ev["t"], ev.get("at")
|
|
if t == "call":
|
|
out = self._flush()
|
|
if ev.get("elapsedMs") is None:
|
|
ev = {**ev, "elapsedMs": _elapsed_ms(self.prev_at, at)}
|
|
self._open = [ev]
|
|
if ev.get("id") is not None:
|
|
self._calls[ev["id"]] = ev
|
|
elif t in _BLOCKS and self._of_open(ev):
|
|
self._open.append(ev)
|
|
out = []
|
|
elif t == "usage" and self._of_open(ev):
|
|
step = self._flush()
|
|
out = [step[0], ev, *step[1:]]
|
|
elif t == "usage":
|
|
# Usage for a call whose blocks were already folded.
|
|
out = self._flush()
|
|
c = self._calls.get(ev.get("call")) or {}
|
|
out.append({"t": "call", "at": at, "id": ev.get("call"),
|
|
"request": c.get("request"), "model": c.get("model"),
|
|
"effort": c.get("effort"), "tier": c.get("tier"),
|
|
"elapsedMs": _elapsed_ms(self.prev_at, at), "empty": True})
|
|
out.append(ev)
|
|
else:
|
|
out = self._flush()
|
|
out.append(ev)
|
|
if at and t != "session":
|
|
self.prev_at = at
|
|
return out
|