Repository navigation
Expand file tree
/
Copy pathusageTranscriptStreaming.test.ts
More file actions
421 lines (403 loc) · 14.8 KB
/
Copy pathusageTranscriptStreaming.test.ts
File metadata and controls
421 lines (403 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// @effect-diagnostics nodeBuiltinImport:off - exercise the real byte reader.
import * as NodeFSP from "node:fs/promises";
import * as NodeOS from "node:os";
import * as NodePath from "node:path";
import { afterEach, beforeEach, describe, expect, it } from "@effect/vitest";
import { TEST_FORMATS } from "./usageTestFormats.ts";
import {
readTranscriptRecords as readWithDefaultThreshold,
type TranscriptParsePosition,
} from "./usageTranscriptReader.ts";
const readTranscriptRecords = (
path: string,
provider: keyof typeof TEST_FORMATS,
position?: TranscriptParsePosition,
) =>
readWithDefaultThreshold(path, TEST_FORMATS[provider], position, {
streamingThresholdBytes: 256 * 1024,
});
let dir: string;
beforeEach(async () => {
dir = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "usage-stream-"));
});
afterEach(async () => {
await NodeFSP.rm(dir, { recursive: true, force: true });
});
const timestamp = "2026-08-01T10:00:00Z";
const content = '工具 output \\" usage token_count '.repeat(40_000);
const claude = (id = "m1", output = 99) => ({
type: "assistant",
timestamp,
sessionId: "s1",
requestId: `r-${id}`,
costUSD: 0.25,
message: {
content: [{ type: "tool_use", input: { text: content } }],
id,
model: "claude-fable-5",
usage: {
input_tokens: 100,
output_tokens: output,
cache_read_input_tokens: 20,
cache_creation_input_tokens: 5,
speed: "fast",
},
},
});
const codex = [
{ type: "session_meta", timestamp, payload: { id: "s1", model_provider: "openai" } },
{ type: "turn_context", timestamp, payload: { model: "gpt-5.6-sol", turn_id: "initial-turn" } },
{
type: "event_msg",
timestamp,
payload: {
type: "token_count",
info: {
last_token_usage: {
input_tokens: 100,
output_tokens: 99,
cached_input_tokens: 20,
cache_write_input_tokens: 5,
reasoning_output_tokens: 10,
},
},
},
},
];
const grok = {
timestamp: 1785578400,
params: {
sessionId: "s1",
_meta: { agentTimestampMs: 1785578400123 },
update: {
sessionUpdate: "turn_completed",
prompt_id: "p1",
usage: {
inputTokens: 100,
outputTokens: 99,
costUsdTicks: 2500000000,
modelUsage: {
"grok-4.5-build": {
inputTokens: 100,
outputTokens: 99,
cachedReadTokens: 20,
cacheCreationTokens: 5,
reasoningTokens: 10,
},
},
},
},
},
};
async function scan(
lines: readonly unknown[],
provider: keyof typeof TEST_FORMATS,
name = "history",
) {
const path = NodePath.join(dir, `${name}.jsonl`);
await NodeFSP.writeFile(path, lines.map((line) => JSON.stringify(line)).join("\n") + "\n");
const result = await readTranscriptRecords(path, provider);
expect(result).not.toBeNull();
return result!;
}
describe("large usage records", () => {
it("streams Muse completion envelopes and resumes a partial appended event", async () => {
const entry = {
schema_version: 1,
record_type: "event",
payload_type: "runtime.session",
payload_schema_version: 3,
id: "envelope-1",
recorded_at: 1_785_578_400_123_456,
stream: { kind: "session", id: "parent" },
payload: {
source_run_record_id: "inference-1",
event: {
kind: "automated_review_completed",
model: { model_id: "review-model" },
usage: {
input_tokens: 100,
cached_input_tokens: 60,
output_tokens: 20,
reasoning_tokens: 8,
},
},
},
};
const small = await scan([entry], "muse", "small-muse");
expect(small.records[0]?.totals).toMatchObject({
uncachedInputTokens: 40,
cachedInputTokens: 60,
});
const path = NodePath.join(dir, "large-muse.jsonl");
await NodeFSP.writeFile(path, JSON.stringify({ ...entry, discarded: { content } }) + "\n");
const large = await readTranscriptRecords(path, "muse");
expect(large?.records).toEqual(small.records);
const appended = JSON.stringify({
...entry,
id: "envelope-2",
payload: { ...entry.payload, source_run_record_id: "inference-2" },
});
await NodeFSP.appendFile(path, appended.slice(0, -7));
const partial = await readTranscriptRecords(path, "muse", large!.position);
expect(partial?.records).toEqual([]);
await NodeFSP.appendFile(path, appended.slice(-7) + "\n");
const completed = await readTranscriptRecords(path, "muse", partial!.position);
expect(completed?.records).toHaveLength(1);
expect(completed?.records[0]?.dedupeKey).not.toBe(large?.records[0]?.dedupeKey);
expect(completed?.records[0]?.totals).toEqual(small.records[0]?.totals);
});
it("keeps Pi model, cost, and fork identity after large assistant content", async () => {
const entry = {
type: "message",
id: "a123abcd",
timestamp,
message: {
role: "assistant",
provider: "anthropic",
model: "requested",
responseModel: "actual",
content: [{ type: "text", text: content }],
usage: {
input: 100,
output: 99,
cacheRead: 20,
cacheWrite: 5,
reasoning: 10,
cost: { total: 0.25 },
},
},
};
const result = await scan([{ type: "session", id: "pi-session" }, entry], "pi");
expect(result.records).toEqual([
{
provider: "pi",
model: "anthropic/actual",
rateModel: "actual",
sessionId: "pi-session",
timestampMs: Date.parse(timestamp),
totals: {
uncachedInputTokens: 100,
outputTokens: 99,
cachedInputTokens: 20,
cacheCreationTokens: 5,
reasoningTokens: 10,
},
reportedCostUsd: 0.25,
speed: "standard",
dedupeKey: JSON.stringify(["pi", entry.id, timestamp, "anthropic/actual"]),
},
]);
});
it("keeps usage after large Claude tool input, including fast-mode cost and dedupe metadata", async () => {
const result = await scan([claude()], "claude");
expect(result.records).toEqual([
{
provider: "claude",
timestampMs: Date.parse(timestamp),
sessionId: "s1",
model: "claude-fable-5",
totals: {
uncachedInputTokens: 100,
outputTokens: 99,
cachedInputTokens: 20,
cacheCreationTokens: 5,
reasoningTokens: 0,
},
reportedCostUsd: 0.25,
speed: "fast",
dedupeKey: "m1:r-m1",
},
]);
});
it.each(["claude", "codex", "grok"] as const)(
"matches ordinary %s records with large irrelevant fields in either order",
async (provider) => {
const small =
provider === "codex"
? codex
: provider === "grok"
? [grok]
: [{ ...claude(), message: { ...claude().message, content: [] } }];
const expected = await scan(small, provider, "small");
for (const first of [true, false]) {
const large = small.map((record) =>
first ? { padding: content, ...record } : { ...record, padding: content },
);
const actual = await scan(large, provider);
expect(actual.records).toEqual(expected.records);
expect(actual.position.state).toEqual(expected.position.state);
}
},
);
it("preserves Grok model allocation, precise timestamp and prompt identity", async () => {
const result = await scan([{ padding: content, ...grok }], "grok");
expect(result.records[0]).toMatchObject({
timestampMs: 1785578400123,
model: "grok-4.5-build",
sessionId: "s1",
totals: {
uncachedInputTokens: 75,
cachedInputTokens: 20,
cacheCreationTokens: 5,
outputTokens: 99,
reasoningTokens: 10,
},
dedupeKey: "s1:p1:grok-4.5-build",
reportedCostUsd: 0.25,
});
});
it("does not count usage-looking text or nested objects inside tool output", async () => {
const result = await scan(
[
{ type: "user", padding: content, toolOutput: claude() },
{ padding: content, message: { content: JSON.stringify(claude()) } },
{ type: "assistant", timestamp, message: { model: "claude-fable-5", content: [claude()] } },
],
"claude",
);
expect(result.records).toEqual([]);
});
it("replays partial UTF-8 and JSON tails, commits exact CRLF offsets, and replaces the tail once", async () => {
const path = NodePath.join(dir, "tail.jsonl");
const first = JSON.stringify(claude("first", 5)) + "\r\n";
const second = Buffer.from(JSON.stringify(claude("second", 7)));
const split = second.lastIndexOf(Buffer.from("工具")) + 1;
await NodeFSP.writeFile(path, Buffer.concat([Buffer.from(first), second.subarray(0, split)]));
const partial = await readTranscriptRecords(path, "claude");
expect(partial?.records.map((r) => r.totals.outputTokens)).toEqual([5]);
expect(partial?.tailRecords).toEqual([]);
expect(partial?.position.resumeOffset).toBe(Buffer.byteLength(first));
await NodeFSP.appendFile(path, second.subarray(split));
const complete = await readTranscriptRecords(path, "claude", partial!.position);
expect(complete?.resumed).toBe(true);
expect(complete?.records).toEqual([]);
expect(complete?.tailRecords.map((r) => r.totals.outputTokens)).toEqual([7]);
expect(complete?.position.resumeOffset).toBe(Buffer.byteLength(first));
await NodeFSP.appendFile(path, "\r\n" + JSON.stringify(claude("third", 11)) + "\n");
const appended = await readTranscriptRecords(path, "claude", complete!.position);
expect(appended?.records.map((r) => r.totals.outputTokens)).toEqual([7, 11]);
expect(appended?.tailRecords).toEqual([]);
expect(appended?.position.resumeOffset).toBe((await NodeFSP.stat(path)).size);
const full = await readTranscriptRecords(path, "claude");
expect([...partial!.records, ...appended!.records]).toEqual(full?.records);
});
it("preserves Codex model switches and duplicate suppression across streaming resumes", async () => {
const path = NodePath.join(dir, "history.jsonl");
const first = await scan(
codex.map((record) => ({ padding: "x".repeat(9 * 1024 * 1024), ...record })),
"codex",
);
expect(first.records[0]).toMatchObject({
sessionId: "s1",
model: "gpt-5.6-sol",
unresolvedServiceTierTurnId: "initial-turn",
});
await NodeFSP.appendFile(
path,
[
{ padding: content, ...codex[2] },
{ type: "turn_context", payload: { padding: content, model: "gpt-6" } },
{
type: "event_msg",
timestamp,
payload: {
type: "token_count",
padding: content,
info: { last_token_usage: { input_tokens: 200, output_tokens: 101 } },
},
},
]
.map((line) => JSON.stringify(line))
.join("\n") + "\n",
);
const result = await readTranscriptRecords(path, "codex", first.position);
expect(result?.resumed).toBe(true);
expect(result?.records).toHaveLength(1);
expect(result?.records[0]).toMatchObject({
model: "gpt-6",
sessionId: "s1",
totals: { outputTokens: 101 },
});
expect(result?.records[0]).not.toHaveProperty("unresolvedServiceTierTurnId");
const full = await readTranscriptRecords(path, "codex");
expect([...first.records, ...result!.records]).toEqual(full?.records);
});
it.each(["forked_from_id", "source"])(
"preserves Codex fork-copy suppression from %s",
async (field) => {
const fork =
field === "source"
? { source: { subagent: { thread_spawn: { parent_thread_id: "parent" } } } }
: { forked_from_id: "parent" };
const lines = [
{ type: "session_meta", timestamp, payload: { padding: content, id: "child", ...fork } },
codex[1],
codex[2],
{
...codex[2],
timestamp: "2026-08-01T10:00:05Z",
payload: {
type: "token_count",
info: { last_token_usage: { input_tokens: 100, output_tokens: 101 } },
},
},
];
const result = await scan(lines, "codex");
expect(result.records).toHaveLength(1);
expect(result.records[0]).toMatchObject({
sessionId: "child",
totals: { outputTokens: 101 },
});
},
);
it("rejects malformed large lines without accepting partial usage or losing following lines", async () => {
const valid = JSON.stringify(claude());
const path = NodePath.join(dir, "broken.jsonl");
await NodeFSP.writeFile(
path,
[valid + " junk", valid.slice(0, -1), valid + valid, JSON.stringify(claude("good", 7))].join(
"\n",
) + "\n",
);
const result = await readTranscriptRecords(path, "claude");
expect(result?.records.map((r) => r.totals.outputTokens)).toEqual([7]);
});
it("matches JSON.parse for reordered and escaped keys, duplicate fields, arrays and unknown key names", async () => {
const path = NodePath.join(dir, "odd.jsonl");
const small = JSON.stringify({ ...claude(), message: { ...claude().message, content: [] } });
const variants = [
small.replace('"message":', '"mess\\u0061ge":'),
small.replace('"costUSD":0.25', '"costUSD":5,"costUSD":0.25'),
small.replace('"type":"assistant"', '"type":"user","type":"assistant"'),
small.replace('"message":', '"__proto__":{"type":"user"},"message":'),
small.replace('"message":', '"message.usage":{"output_tokens":1234},"message":'),
];
for (const line of variants) {
await NodeFSP.writeFile(path, line + "\n");
const expected = await readTranscriptRecords(path, "claude");
await NodeFSP.writeFile(
path,
'{"padding":' + JSON.stringify(content) + "," + line.slice(1) + "\n",
);
const actual = await readTranscriptRecords(path, "claude");
expect(actual?.records).toEqual(expected?.records);
}
});
it("reads usage after deeply nested discarded tool content", async () => {
const path = NodePath.join(dir, "deep.jsonl");
const record = JSON.stringify(claude());
const nested = "[".repeat(300) + "0" + "]".repeat(300);
await NodeFSP.writeFile(path, '{"toolOutput":' + nested + "," + record.slice(1) + "\n");
const result = await readTranscriptRecords(path, "claude");
expect(result?.records[0]?.totals.outputTokens).toBe(99);
});
it("keeps JSON number semantics for non-finite values without serializing them into null", async () => {
const path = NodePath.join(dir, "numbers.jsonl");
const record = JSON.stringify(claude()).replace('"costUSD":0.25', '"costUSD":1e400');
await NodeFSP.writeFile(path, record + "\n");
const result = await readTranscriptRecords(path, "claude");
expect(result?.records[0]?.reportedCostUsd).toBeNull();
expect(result?.records[0]?.totals.outputTokens).toBe(99);
});
});