refactor(usage): transcript readers come from their drivers (#17576)

Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Julius MarmingeandClaude Opus 5.5 authored and GitHub committed 2026-10-09 15:26:18 -07:00
1 parent 507361bc73
commit e5dca2332f
34 files changed
+1731 -1411

No files matched your search

@@ -8,6 +8,7 @@ import * as NodeSqlite from "node:sqlite";
import { afterEach, assert, beforeEach, describe, it } from "@effect/vitest";
import { TEST_FORMATS } from "./usageTestFormats.ts";
import { readTranscriptRecords } from "./usageTranscriptReader.ts";
import { readOpenCodeUsage } from "./opencodeUsageReader.ts";
import { readCursorAccountUsage } from "./cursorUsageReader.ts";
@@ -99,20 +100,20 @@ describe("readTranscriptRecords resume", () => {
it("parses only appended lines when resuming a grown file", async () => {
const path = NodePath.join(dir, "claude.jsonl");
await NodeFSP.writeFile(path, claudeLine(1, 5) + claudeLine(2, 7));
const first = await readTranscriptRecords(path, "claude");
const first = await readTranscriptRecords(path, TEST_FORMATS.claude);
assert.isNotNull(first);
assert.strictEqual(first.records.length, 2);
assert.isFalse(first.resumed);
await NodeFSP.appendFile(path, claudeLine(3, 11));
const second = await readTranscriptRecords(path, "claude", first.position);
const second = await readTranscriptRecords(path, TEST_FORMATS.claude, first.position);
assert.isNotNull(second);
assert.isTrue(second.resumed);
assert.strictEqual(second.records.length, 1);
assert.strictEqual(second.records[0]?.totals.outputTokens, 11);
// The stitched result matches a from-scratch parse of the whole file.
const full = await readTranscriptRecords(path, "claude");
const full = await readTranscriptRecords(path, TEST_FORMATS.claude);
assert.isNotNull(full);
assert.deepStrictEqual([...first.records, ...second.records], [...full.records]);
});
@@ -123,7 +124,7 @@ describe("readTranscriptRecords resume", () => {
path,
codexMetaLine() + codexModelLine("gpt-5.2-codex") + codexTierLine("ultrafast"),
);
const first = await readTranscriptRecords(path, "codex");
const first = await readTranscriptRecords(path, TEST_FORMATS.codex);
assert.isNotNull(first);
assert.strictEqual(first.records.length, 0);
@@ -131,7 +132,7 @@ describe("readTranscriptRecords resume", () => {
// session_meta of its own; model, tier, and session must come from the
// state captured before the boundary.
await NodeFSP.appendFile(path, codexUsageLine(9, 5));
const second = await readTranscriptRecords(path, "codex", first.position);
const second = await readTranscriptRecords(path, TEST_FORMATS.codex, first.position);
assert.isNotNull(second);
assert.isTrue(second.resumed);
assert.strictEqual(second.records.length, 1);
@@ -146,14 +147,14 @@ describe("readTranscriptRecords resume", () => {
path,
codexMetaLine() + codexModelLine("gpt-5.2-codex") + codexUsageLine(9, 5),
);
const first = await readTranscriptRecords(path, "codex");
const first = await readTranscriptRecords(path, TEST_FORMATS.codex);
assert.isNotNull(first);
assert.strictEqual(first.records.length, 1);
// Codex re-emits an unchanged token_count on stream boundaries; the copy
// lands after the resume point and must still be dropped.
await NodeFSP.appendFile(path, codexUsageLine(9, 5) + codexUsageLine(21, 8));
const second = await readTranscriptRecords(path, "codex", first.position);
const second = await readTranscriptRecords(path, TEST_FORMATS.codex, first.position);
assert.isNotNull(second);
assert.isTrue(second.resumed);
assert.deepStrictEqual(
@@ -166,7 +167,7 @@ describe("readTranscriptRecords resume", () => {
const path = NodePath.join(dir, "claude.jsonl");
const unterminated = claudeLine(2, 7).trimEnd();
await NodeFSP.writeFile(path, claudeLine(1, 5) + unterminated);
const first = await readTranscriptRecords(path, "claude");
const first = await readTranscriptRecords(path, TEST_FORMATS.claude);
assert.isNotNull(first);
assert.strictEqual(first.records.length, 1);
assert.strictEqual(first.tailRecords.length, 1);
@@ -175,7 +176,7 @@ describe("readTranscriptRecords resume", () => {
// Completing the line and appending another re-reads from the resume
// point, so the once-tail record arrives exactly once as a line record.
await NodeFSP.appendFile(path, `\n${claudeLine(3, 11)}`);
const second = await readTranscriptRecords(path, "claude", first.position);
const second = await readTranscriptRecords(path, TEST_FORMATS.claude, first.position);
assert.isNotNull(second);
assert.isTrue(second.resumed);
assert.deepStrictEqual(
@@ -188,12 +189,12 @@ describe("readTranscriptRecords resume", () => {
it("re-parses from the start when the guard bytes no longer match", async () => {
const path = NodePath.join(dir, "claude.jsonl");
await NodeFSP.writeFile(path, claudeLine(1, 5));
const first = await readTranscriptRecords(path, "claude");
const first = await readTranscriptRecords(path, TEST_FORMATS.claude);
assert.isNotNull(first);
// Same path, larger size, different content: a replaced file, not growth.
await NodeFSP.writeFile(path, claudeLine(4, 13) + claudeLine(5, 17));
const second = await readTranscriptRecords(path, "claude", first.position);
const second = await readTranscriptRecords(path, TEST_FORMATS.claude, first.position);
assert.isNotNull(second);
assert.isFalse(second.resumed);
assert.deepStrictEqual(
@@ -205,11 +206,11 @@ describe("readTranscriptRecords resume", () => {
it("re-parses from the start when the file shrank below the resume point", async () => {
const path = NodePath.join(dir, "claude.jsonl");
await NodeFSP.writeFile(path, claudeLine(1, 5) + claudeLine(2, 7));
const first = await readTranscriptRecords(path, "claude");
const first = await readTranscriptRecords(path, TEST_FORMATS.claude);
assert.isNotNull(first);
await NodeFSP.writeFile(path, claudeLine(3, 11));
const second = await readTranscriptRecords(path, "claude", first.position);
const second = await readTranscriptRecords(path, TEST_FORMATS.claude, first.position);
assert.isNotNull(second);
assert.isFalse(second.resumed);
assert.deepStrictEqual(
@@ -236,7 +237,7 @@ describe("readTranscriptRecords resume", () => {
})}\n`;
await NodeFSP.writeFile(path, bigLine + claudeLine(2, 7));
const parsed = await readTranscriptRecords(path, "claude");
const parsed = await readTranscriptRecords(path, TEST_FORMATS.claude);
assert.isNotNull(parsed);
assert.deepStrictEqual(
parsed.records.map((record) => record.totals.outputTokens),
@@ -245,7 +246,9 @@ describe("readTranscriptRecords resume", () => {
});
it("returns null for an unreadable file", async () => {
assert.isNull(await readTranscriptRecords(NodePath.join(dir, "missing.jsonl"), "claude"));
assert.isNull(
await readTranscriptRecords(NodePath.join(dir, "missing.jsonl"), TEST_FORMATS.claude),
);
});
});