|
4 | 4 | import * as NodeFSP from "node:fs/promises"; |
5 | 5 | import * as NodeOS from "node:os"; |
6 | 6 | import * as NodePath from "node:path"; |
| 7 | +import * as NodeZlib from "node:zlib"; |
7 | 8 |
|
8 | 9 | import { afterEach, assert, beforeEach, describe, it } from "@effect/vitest"; |
9 | 10 | import type { TranscriptUsageFormat } from "@t3tools/provider-core/server/usage"; |
10 | 11 | import * as Schema from "effect/Schema"; |
11 | 12 |
|
12 | 13 | import { TEST_FORMATS } from "./usageTestFormats.ts"; |
13 | | -import { readTranscriptRecords, transcriptLineGate } from "./usageTranscriptReader.ts"; |
| 14 | +import { |
| 15 | + listTranscriptFiles, |
| 16 | + readTranscriptRecords, |
| 17 | + transcriptLineGate, |
| 18 | +} from "./usageTranscriptReader.ts"; |
14 | 19 |
|
15 | 20 | let dir: string; |
16 | 21 |
|
@@ -71,6 +76,103 @@ function codexUsageLine(outputTokens: number, secondsOffset: number): string { |
71 | 76 | })}\n`; |
72 | 77 | } |
73 | 78 |
|
| 79 | +describe("compressed transcripts", () => { |
| 80 | + it("discovers nested plain and zstd JSONL files within the requested window", async () => { |
| 81 | + const nested = NodePath.join(dir, "nested"); |
| 82 | + await NodeFSP.mkdir(nested); |
| 83 | + const plain = NodePath.join(dir, "active.jsonl"); |
| 84 | + const compressed = NodePath.join(nested, "archived.jsonl.zst"); |
| 85 | + const old = NodePath.join(nested, "old.jsonl.zst"); |
| 86 | + await NodeFSP.writeFile(plain, claudeLine(1, 5)); |
| 87 | + await NodeFSP.writeFile(compressed, NodeZlib.zstdCompressSync(claudeLine(2, 7))); |
| 88 | + await NodeFSP.writeFile(old, NodeZlib.zstdCompressSync(claudeLine(3, 11))); |
| 89 | + await NodeFSP.writeFile(NodePath.join(nested, "unrelated.zst"), "ignored"); |
| 90 | + await NodeFSP.writeFile(NodePath.join(nested, "partial.jsonl.zst.tmp"), "ignored"); |
| 91 | + await NodeFSP.utimes(old, 1, 1); |
| 92 | + |
| 93 | + const listed = await listTranscriptFiles(dir, Date.parse("2026-01-01T00:00:00Z")); |
| 94 | + assert.deepStrictEqual( |
| 95 | + listed.files.map((file) => file.path).sort(), |
| 96 | + [plain, compressed].sort(), |
| 97 | + ); |
| 98 | + assert.strictEqual(listed.failedPaths, 0); |
| 99 | + assert.isAbove(listed.files.find((file) => file.path === compressed)?.size ?? 0, 0); |
| 100 | + }); |
| 101 | + |
| 102 | + it.each([ |
| 103 | + { parser: "native", streamingThresholdBytes: undefined }, |
| 104 | + { parser: "projected", streamingThresholdBytes: 1024 }, |
| 105 | + ])( |
| 106 | + "matches plain Codex records and unfinished tails through long lines ($parser)", |
| 107 | + async ({ streamingThresholdBytes }) => { |
| 108 | + const plain = NodePath.join(dir, "rollout.jsonl"); |
| 109 | + const compressed = plain + ".zst"; |
| 110 | + const large = codexUsageLine(9, 5).replace( |
| 111 | + '"payload":', |
| 112 | + '"padding":' + JSON.stringify("日本語 é".repeat(128 * 1024)) + ',"payload":', |
| 113 | + ); |
| 114 | + const content = |
| 115 | + codexMetaLine() + |
| 116 | + codexModelLine("gpt-6-astra") + |
| 117 | + codexTierLine("ultrafast") + |
| 118 | + large + |
| 119 | + codexUsageLine(13, 6).replace('"output_tokens":13', '"output_tokens":"invalid"') + |
| 120 | + codexUsageLine(21, 8).trimEnd(); |
| 121 | + await NodeFSP.writeFile(plain, content); |
| 122 | + await NodeFSP.writeFile(compressed, NodeZlib.zstdCompressSync(content)); |
| 123 | + |
| 124 | + const options = |
| 125 | + streamingThresholdBytes === undefined ? undefined : { streamingThresholdBytes }; |
| 126 | + const expected = await readTranscriptRecords(plain, TEST_FORMATS.codex, undefined, options); |
| 127 | + const actual = await readTranscriptRecords( |
| 128 | + compressed, |
| 129 | + TEST_FORMATS.codex, |
| 130 | + undefined, |
| 131 | + options, |
| 132 | + ); |
| 133 | + assert.isNotNull(expected); |
| 134 | + assert.isNotNull(actual); |
| 135 | + assert.strictEqual(expected.records.length, 1); |
| 136 | + assert.strictEqual(expected.tailRecords.length, 1); |
| 137 | + assert.deepStrictEqual(actual.records, expected.records); |
| 138 | + assert.deepStrictEqual(actual.tailRecords, expected.tailRecords); |
| 139 | + assert.strictEqual(expected.malformedRecords, 1); |
| 140 | + assert.strictEqual(actual.malformedRecords, expected.malformedRecords); |
| 141 | + assert.strictEqual(actual.records[0]?.speed, "ultrafast"); |
| 142 | + assert.isFalse(actual.resumed); |
| 143 | + assert.strictEqual(actual.position.resumeOffset, 0); |
| 144 | + }, |
| 145 | + ); |
| 146 | + |
| 147 | + it("ignores a prior resume position and rereads compressed rollouts from their start", async () => { |
| 148 | + const plain = NodePath.join(dir, "rollout.jsonl"); |
| 149 | + const content = codexMetaLine() + codexModelLine("gpt-6-astra") + codexUsageLine(9, 5); |
| 150 | + await NodeFSP.writeFile(plain, content); |
| 151 | + const first = await readTranscriptRecords(plain, TEST_FORMATS.codex); |
| 152 | + assert.isNotNull(first); |
| 153 | + |
| 154 | + const compressed = plain + ".zst"; |
| 155 | + await NodeFSP.writeFile(compressed, NodeZlib.zstdCompressSync(content + codexUsageLine(21, 8))); |
| 156 | + const actual = await readTranscriptRecords(compressed, TEST_FORMATS.codex, first.position); |
| 157 | + assert.isNotNull(actual); |
| 158 | + assert.isFalse(actual.resumed); |
| 159 | + assert.deepStrictEqual( |
| 160 | + actual.records.map((record) => record.totals.outputTokens), |
| 161 | + [9, 21], |
| 162 | + ); |
| 163 | + const repeated = await readTranscriptRecords(compressed, TEST_FORMATS.codex, actual.position); |
| 164 | + assert.isNotNull(repeated); |
| 165 | + assert.isFalse(repeated.resumed); |
| 166 | + assert.deepStrictEqual(repeated.records, actual.records); |
| 167 | + }); |
| 168 | + |
| 169 | + it("returns null for zstd decoder errors rather than caching an empty parse", async () => { |
| 170 | + const path = NodePath.join(dir, "corrupt.jsonl.zst"); |
| 171 | + await NodeFSP.writeFile(path, "not a zstd frame"); |
| 172 | + assert.isNull(await readTranscriptRecords(path, TEST_FORMATS.claude)); |
| 173 | + }); |
| 174 | +}); |
| 175 | + |
74 | 176 | describe("readTranscriptRecords resume", () => { |
75 | 177 | it.each([ |
76 | 178 | { mode: "buffered", streamingThresholdBytes: Infinity }, |
|
0 commit comments