Open History to see the last commit
1import { describe, expect, it } from "@effect/vitest";
2import { totalTokens } from "@t3tools/provider-core/server/usage";
3
4import {
5 type CodexScanState,
6 codexUsageFormat,
7 initialCodexScanState,
8 parseCodexLine as parseCodexRecords,
9} from "./codexUsage.ts";
10
11function parseCodexLine(line: string, state: CodexScanState) {
12 const records = parseCodexRecords(line, state);
13 expect(records.length).toBeLessThanOrEqual(1);
14 return records[0] ?? null;
15}
16
17describe("parseCodexLine", () => {
18 const sessionMeta = JSON.stringify({
19 type: "session_meta",
20 timestamp: "2026-08-01T05:17:41.289Z",
21 payload: { type: "session_meta", id: "019fbbc1-b12c-7360-a685-28c181f0025f" },
22 });
23 const turnContext = JSON.stringify({
24 type: "turn_context",
25 timestamp: "2026-08-01T05:17:42.694Z",
26 payload: { type: "turn_context", model: "gpt-5.6-sol" },
27 });
28 const tokenCount = (inputTokens: number, cached: number, output: number, reasoning: number) =>
29 JSON.stringify({
30 type: "event_msg",
31 timestamp: "2026-08-01T05:17:49.919Z",
32 payload: {
33 type: "token_count",
34 info: {
35 last_token_usage: {
36 input_tokens: inputTokens,
37 cached_input_tokens: cached,
38 cache_write_input_tokens: 0,
39 output_tokens: output,
40 reasoning_output_tokens: reasoning,
41 },
42 },
43 },
44 });
45 const counters = (input: number, output: number) => ({
46 input_tokens: input,
47 cached_input_tokens: 0,
48 cache_write_input_tokens: 0,
49 output_tokens: output,
50 reasoning_output_tokens: 0,
51 total_tokens: input + output,
52 });
53 const counted = (last: unknown, total?: unknown, extra = {}) =>
54 JSON.stringify({
55 type: "event_msg",
56 timestamp: "2026-08-01T05:17:49.919Z",
57 payload: {
58 type: "token_count",
59 info: { last_token_usage: last, total_token_usage: total, ...extra },
60 },
61 });
62
63 it("attributes usage to the model from the preceding turn context", () => {
64 const state = initialCodexScanState();
65 parseCodexLine(sessionMeta, state);
66 parseCodexLine(turnContext, state);
67 const record = parseCodexLine(tokenCount(19239, 11008, 299, 116), state);
68
69 expect(record?.provider).toBe("codex");
70 expect(record?.model).toBe("gpt-5.6-sol");
71 expect(record?.sessionId).toBe("019fbbc1-b12c-7360-a685-28c181f0025f");
72 expect(record?.totals.uncachedInputTokens).toBe(19239 - 11008);
73 expect(record?.totals.cachedInputTokens).toBe(11008);
74 expect(record?.totals.reasoningTokens).toBe(116);
75 });
76
77 it("skips a repeated token_count so deltas are not double counted", () => {
78 const state = initialCodexScanState();
79 parseCodexLine(turnContext, state);
80 const first = parseCodexLine(tokenCount(100, 0, 10, 0), state);
81 const repeat = parseCodexLine(tokenCount(100, 0, 10, 0), state);
82
83 expect(first).not.toBeNull();
84 expect(repeat).toBeNull();
85 });
86
87 it("does not consume counters before the model and timestamp are valid", () => {
88 const state = initialCodexScanState();
89 expect(parseCodexLine(tokenCount(100, 0, 10, 0), state)).toBeNull();
90 parseCodexLine(turnContext, state);
91 expect(parseCodexLine(tokenCount(100, 0, 10, 0), state)).not.toBeNull();
92
93 const cumulativeState = initialCodexScanState();
94 const line = counted(counters(100, 20), counters(100, 20));
95 expect(parseCodexLine(line, cumulativeState)).toBeNull();
96 parseCodexLine(turnContext, cumulativeState);
97 expect(
98 parseCodexLine(line.replace("2026-08-01T05:17:49.919Z", "invalid"), cumulativeState),
99 ).toBeNull();
100 expect(parseCodexLine(line, cumulativeState)?.totals.outputTokens).toBe(20);
101 expect(codexUsageFormat.readMalformedRecords?.(cumulativeState)).toBe(0);
102 });
103
104 it("ignores stale changed requests but counts equal requests when totals advance across contexts", () => {
105 const state = initialCodexScanState();
106 parseCodexLine(turnContext, state);
107 const request = counters(100, 20);
108 expect(parseCodexLine(counted(request, request), state)?.totals.outputTokens).toBe(20);
109 expect(parseCodexLine(counted(counters(20, 5), request), state)).toBeNull();
110 expect(
111 parseCodexLine(
112 counted({ ...counters(0, 0), total_tokens: 20330 }, request, {
113 model_context_window: 258400,
114 }),
115 state,
116 ),
117 ).toBeNull();
118 expect(parseCodexLine(counted(request, counters(200, 40)), state)?.totals.outputTokens).toBe(
119 20,
120 );
121
122 for (const [index, tier] of ["priority", "ultrafast"].entries()) {
123 parseCodexLine(
124 JSON.stringify({ type: "turn_context", payload: { model: "gpt-6-astra" } }),
125 state,
126 );
127 parseCodexLine(
128 JSON.stringify({
129 type: "event_msg",
130 payload: { type: "thread_settings_applied", thread_settings: { service_tier: tier } },
131 }),
132 state,
133 );
134 const record = parseCodexLine(
135 counted(request, counters(100 * (index + 3), 20 * (index + 3))),
136 state,
137 );
138 expect(record).toMatchObject({
139 model: "gpt-6-astra",
140 speed: tier === "priority" ? "fast" : "ultrafast",
141 totals: { uncachedInputTokens: 100, outputTokens: 20 },
142 });
143 }
144 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
145 });
146
147 it.each([false, true])(
148 "does not rebill legacy requests or drop the next cumulative delta (initial cumulative=%s)",
149 (initialCumulative) => {
150 const state = initialCodexScanState();
151 parseCodexLine(turnContext, state);
152 const first = counters(100, 20);
153 expect(
154 parseCodexLine(counted(first, initialCumulative ? first : undefined), state)?.totals
155 .outputTokens,
156 ).toBe(20);
157 expect(parseCodexLine(counted(first), state)).toBeNull();
158 expect(parseCodexLine(counted(counters(20, 5)), state)?.totals.outputTokens).toBe(5);
159 expect(parseCodexLine(counted(counters(20, 5), counters(120, 25)), state)).toBeNull();
160 expect(
161 parseCodexLine(counted(counters(10, 2), counters(130, 27)), state)?.totals.outputTokens,
162 ).toBe(2);
163 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
164 },
165 );
166
167 it("checks every cumulative component and recovers using an inconsistent pair's valid baseline", () => {
168 const request = {
169 ...counters(100, 20),
170 cached_input_tokens: 30,
171 cache_write_input_tokens: 10,
172 reasoning_output_tokens: 5,
173 };
174 const fields = Object.keys(request) as (keyof typeof request)[];
175 for (const field of fields) {
176 const state = initialCodexScanState();
177 parseCodexLine(turnContext, state);
178 expect(parseCodexLine(counted(request, request), state)?.totals).toEqual({
179 uncachedInputTokens: 60,
180 cachedInputTokens: 30,
181 cacheCreationTokens: 10,
182 outputTokens: 20,
183 reasoningTokens: 5,
184 });
185 const total = { ...request };
186 for (const key of fields) total[key] *= 2;
187 total[field] += 1;
188 if (field === "input_tokens" || field === "output_tokens") total.total_tokens += 1;
189 expect(parseCodexLine(counted(request, total), state), field).toBeNull();
190 expect(codexUsageFormat.readMalformedRecords?.(state), field).toBe(1);
191 for (const key of fields) total[key] += request[key];
192 expect(parseCodexLine(counted(request, total), state)?.totals.outputTokens, field).toBe(20);
193 expect(codexUsageFormat.readMalformedRecords?.(state), field).toBe(1);
194 }
195 });
196
197 it.each(["continuation", "fork", "subagent"])(
198 "bills only the latest request when a %s has no initial counter baseline",
199 (source) => {
200 const state = initialCodexScanState();
201 parseCodexLine(
202 JSON.stringify({
203 type: "session_meta",
204 timestamp: "2026-08-01T05:17:41.000Z",
205 payload: {
206 id: "child",
207 model_provider: "openai",
208 ...(source === "fork" ? { forked_from_id: "parent" } : {}),
209 ...(source === "subagent"
210 ? { source: { subagent: { thread_spawn: { parent_thread_id: "parent" } } } }
211 : { source: "vscode" }),
212 },
213 }),
214 state,
215 );
216 parseCodexLine(turnContext, state);
217 const request = {
218 ...counters(100, 20),
219 cached_input_tokens: 30,
220 cache_write_input_tokens: 10,
221 reasoning_output_tokens: 5,
222 };
223 const cumulative = {
224 ...counters(500, 60),
225 cached_input_tokens: 200,
226 cache_write_input_tokens: 20,
227 reasoning_output_tokens: 15,
228 };
229 const first = counted(request, cumulative);
230 expect(parseCodexLine(first, state)).toMatchObject({
231 sessionId: "child",
232 totals: {
233 uncachedInputTokens: 60,
234 cachedInputTokens: 30,
235 cacheCreationTokens: 10,
236 outputTokens: 20,
237 reasoningTokens: 5,
238 },
239 });
240 expect(parseCodexLine(first, state)).toBeNull();
241 expect(parseCodexLine(counted(counters(10, 2), cumulative), state)).toBeNull();
242 expect(
243 parseCodexLine(
244 counted(counters(10, 2), {
245 ...cumulative,
246 input_tokens: 510,
247 output_tokens: 62,
248 total_tokens: 572,
249 }),
250 state,
251 )?.totals.outputTokens,
252 ).toBe(2);
253 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
254 },
255 );
256
257 it.each([
258 {
259 ...counters(90, 40),
260 cached_input_tokens: 60,
261 cache_write_input_tokens: 20,
262 reasoning_output_tokens: 10,
263 },
264 {
265 ...counters(200, 10),
266 cached_input_tokens: 60,
267 cache_write_input_tokens: 20,
268 reasoning_output_tokens: 5,
269 },
270 {
271 ...counters(200, 40),
272 cached_input_tokens: 20,
273 cache_write_input_tokens: 20,
274 reasoning_output_tokens: 10,
275 },
276 {
277 ...counters(200, 40),
278 cached_input_tokens: 60,
279 cache_write_input_tokens: 5,
280 reasoning_output_tokens: 10,
281 },
282 {
283 ...counters(200, 40),
284 cached_input_tokens: 60,
285 cache_write_input_tokens: 20,
286 reasoning_output_tokens: 3,
287 },
288 {
289 ...counters(110, 40),
290 cached_input_tokens: 60,
291 cache_write_input_tokens: 20,
292 reasoning_output_tokens: 10,
293 },
294 {
295 ...counters(200, 30),
296 cached_input_tokens: 60,
297 cache_write_input_tokens: 20,
298 reasoning_output_tokens: 20,
299 },
300 ])("rejects an initial snapshot with an impossible inherited baseline: %j", (cumulative) => {
301 const state = initialCodexScanState();
302 parseCodexLine(turnContext, state);
303 const request = {
304 ...counters(100, 20),
305 cached_input_tokens: 30,
306 cache_write_input_tokens: 10,
307 reasoning_output_tokens: 5,
308 };
309 expect(parseCodexLine(counted(request, cumulative), state)).toBeNull();
310 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(1);
311 expect(state.lastCumulativeUsage).toEqual(cumulative);
312 });
313
314 it.each(["request", "cumulative"])("skips malformed %s counters and recovers", (malformed) => {
315 const state = initialCodexScanState();
316 parseCodexLine(turnContext, state);
317 const request = counters(100, 20);
318 parseCodexLine(counted(request, request), state);
319 expect(
320 parseCodexLine(
321 counted(
322 malformed === "request" ? { ...request, cached_input_tokens: -1 } : request,
323 malformed === "cumulative"
324 ? { ...counters(200, 40), output_tokens: "40" }
325 : counters(200, 40),
326 ),
327 state,
328 ),
329 ).toBeNull();
330 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(1);
331 expect(
332 parseCodexLine(
333 counted(request, malformed === "request" ? counters(300, 60) : counters(200, 40)),
334 state,
335 )?.totals.outputTokens,
336 ).toBe(20);
337 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(1);
338 });
339
340 it("bills only the reported request when usage records sit between reconciling snapshots", () => {
341 const state = initialCodexScanState();
342 const usageRecord = (usage: unknown) =>
343 JSON.stringify({
344 type: "token_usage_record",
345 timestamp: "2026-08-01T05:17:45.000Z",
346 payload: { usage },
347 });
348 parseCodexLine(turnContext, state);
349 parseCodexLine(counted(counters(100, 20), counters(100, 20)), state);
350 for (const usage of [counters(40, 5), counters(60, 15), counters(30, 3)]) {
351 expect(parseCodexLine(usageRecord(usage), state)).toBeNull();
352 }
353 expect(
354 parseCodexLine(counted(counters(100, 20), counters(200, 40)), state)?.totals.outputTokens,
355 ).toBe(20);
356 // Records explained by an earlier snapshot cannot bridge a later gap.
357 parseCodexLine(usageRecord(counters(10, 2)), state);
358 expect(parseCodexLine(counted(counters(10, 2), counters(340, 65)), state)).toBeNull();
359 expect(state.malformedRecords).toBe(1);
360 });
361
362 it("bills each interrupted turn's request with the model, tier and time it was read under", () => {
363 const state = initialCodexScanState();
364 const line = (type: string, time: string, payload: Record<string, unknown>) =>
365 JSON.stringify({ type, timestamp: `2026-11-0${time}Z`, payload });
366 const context = (time: string, turn_id: string, model: string) =>
367 line("turn_context", time, { turn_id, model });
368 const tier = (time: string, service_tier: string) =>
369 line("event_msg", time, {
370 type: "thread_settings_applied",
371 thread_settings: { service_tier },
372 });
373 const request = (time: string, usage: unknown) => line("token_usage_record", time, { usage });
374 const snapshot = (time: string, last: unknown, total: unknown) =>
375 line("event_msg", time, {
376 type: "token_count",
377 info: { last_token_usage: last, total_token_usage: total },
378 });
379 const parsed = [
380 line("session_meta", "1T23:59:50", { id: "session-a", model_provider: "openai" }),
381 context("1T23:59:50", "turn-1", "gpt-6-sol"),
382 request("1T23:59:51", counters(100, 20)),
383 snapshot("1T23:59:52", counters(100, 20), counters(100, 20)),
384 // One interrupted turn whose requests straddle midnight.
385 request("1T23:59:59", counters(150, 10)),
386 request("2T00:00:01", counters(50, 5)),
387 tier("2T00:00:02", "priority"),
388 context("2T00:00:02", "turn-2", "gpt-6-astra"),
389 request("2T00:00:03", counters(50, 5)),
390 request("2T00:00:04", counters(30, 5)),
391 tier("2T00:00:05", "default"),
392 context("2T00:00:05", "turn-3", "gpt-6-sol"),
393 request("2T00:00:06", counters(200, 30)),
394 snapshot("2T00:00:07", counters(200, 30), counters(580, 75)),
395 ].flatMap((value) => parseCodexRecords(value, state));
396
397 const at = (time: string) => Date.parse(`2026-11-0${time}Z`);
398 expect(
399 parsed.map((record) => ({
400 tokens: totalTokens(record.totals),
401 model: record.model,
402 speed: record.speed,
403 timestampMs: record.timestampMs,
404 unresolvedServiceTierTurnId: record.unresolvedServiceTierTurnId,
405 })),
406 ).toEqual([
407 {
408 tokens: 120,
409 model: "gpt-6-sol",
410 speed: "standard",
411 timestampMs: at("1T23:59:52"),
412 unresolvedServiceTierTurnId: "turn-1",
413 },
414 {
415 tokens: 160,
416 model: "gpt-6-sol",
417 speed: "standard",
418 timestampMs: at("1T23:59:59"),
419 unresolvedServiceTierTurnId: "turn-1",
420 },
421 {
422 tokens: 55,
423 model: "gpt-6-sol",
424 speed: "standard",
425 timestampMs: at("2T00:00:01"),
426 unresolvedServiceTierTurnId: "turn-1",
427 },
428 {
429 tokens: 55,
430 model: "gpt-6-astra",
431 speed: "fast",
432 timestampMs: at("2T00:00:03"),
433 unresolvedServiceTierTurnId: undefined,
434 },
435 {
436 tokens: 35,
437 model: "gpt-6-astra",
438 speed: "fast",
439 timestampMs: at("2T00:00:04"),
440 unresolvedServiceTierTurnId: undefined,
441 },
442 {
443 tokens: 230,
444 model: "gpt-6-sol",
445 speed: "standard",
446 timestampMs: at("2T00:00:07"),
447 unresolvedServiceTierTurnId: undefined,
448 },
449 ]);
450 expect(state.malformedRecords).toBe(0);
451 });
452
453 it("does not bill a context-window estimate or lose the following real request", () => {
454 const state = initialCodexScanState();
455 parseCodexLine(turnContext, state);
456 parseCodexLine(counted(counters(100, 20), counters(100, 20)), state);
457 const estimate = { ...counters(0, 0), total_tokens: 1000 };
458 expect(
459 parseCodexLine(
460 counted({ ...estimate, total_tokens: 880 }, estimate, { model_context_window: 1000 }),
461 state,
462 ),
463 ).toBeNull();
464 expect(
465 parseCodexLine(counted(counters(10, 2), { ...counters(10, 2), total_tokens: 1012 }), state)
466 ?.totals.outputTokens,
467 ).toBe(2);
468 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
469 });
470
471 it.each([false, true])(
472 "accepts the post-compaction context marker without a malformed record (prior request: %s)",
473 (priorRequest) => {
474 const state = initialCodexScanState();
475 parseCodexLine(turnContext, state);
476 if (priorRequest) parseCodexLine(counted(counters(100, 20), counters(100, 20)), state);
477 const marker = counted({ ...counters(0, 0), total_tokens: 26387 }, counters(0, 0), {
478 model_context_window: 475000,
479 });
480 expect(parseCodexLine(marker, state)).toBeNull();
481 expect(
482 parseCodexLine(counted(counters(30, 5), counters(30, 5)), state)?.totals.outputTokens,
483 ).toBe(5);
484 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
485 },
486 );
487
488 it("bills the first request after a resume restarts the cumulative counters", () => {
489 const state = initialCodexScanState();
490 parseCodexLine(turnContext, state);
491 parseCodexLine(counted(counters(100, 20), counters(100, 20)), state);
492 expect(
493 parseCodexLine(counted(counters(30, 5), counters(30, 5)), state)?.totals.outputTokens,
494 ).toBe(5);
495 expect(
496 parseCodexLine(counted(counters(10, 2), counters(40, 7)), state)?.totals.outputTokens,
497 ).toBe(2);
498 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
499 });
500
501 it("ignores a replay of the current sequence's first request without rewinding the baseline", () => {
502 const state = initialCodexScanState();
503 parseCodexLine(turnContext, state);
504 const request = counters(100, 20);
505 const processed = [
506 counted(request, request),
507 counted(request, counters(200, 40)),
508 counted(request, request),
509 counted(request, counters(300, 60)),
510 ].map((line) => {
511 const record = parseCodexLine(line, state);
512 return record === null ? 0 : totalTokens(record.totals);
513 });
514
515 expect({
516 processed,
517 skipped: codexUsageFormat.readMalformedRecords?.(state),
518 }).toEqual({ processed: [120, 120, 0, 120], skipped: 0 });
519 });
520
521 it("bills a genuine restart with the same counters at a new timestamp", () => {
522 const state = initialCodexScanState();
523 parseCodexLine(turnContext, state);
524 const request = counters(100, 20);
525 const lines = [
526 counted(request, request),
527 counted(request, counters(200, 40)),
528 counted(request, request).replace("05:17:49.919Z", "05:18:49.919Z"),
529 counted(counters(30, 5), counters(130, 25)),
530 ];
531 const processed = lines.map((line) => {
532 const record = parseCodexLine(line, state);
533 return record === null ? 0 : totalTokens(record.totals);
534 });
535 expect({ processed, skipped: codexUsageFormat.readMalformedRecords?.(state) }).toEqual({
536 processed: [120, 120, 120, 35],
537 skipped: 0,
538 });
539 });
540
541 it.each([
542 {
543 reset: true,
544 markerTotal: counters(0, 0),
545 nextTotal: counters(130, 25),
546 processed: [120, 120, 0, 120, 35],
547 },
548 {
549 reset: false,
550 markerTotal: counters(200, 40),
551 nextTotal: counters(230, 45),
552 processed: [120, 120, 0, 0, 35],
553 },
554 ])(
555 "bills a request equal to the old sequence start only after a context marker reset the counters (reset: $reset)",
556 ({ markerTotal, nextTotal, processed }) => {
557 const state = initialCodexScanState();
558 parseCodexLine(turnContext, state);
559 const request = counters(100, 20);
560 const marker = counted({ ...counters(0, 0), total_tokens: 26387 }, markerTotal, {
561 model_context_window: 475000,
562 });
563 const billed = [
564 counted(request, request),
565 counted(request, counters(200, 40)),
566 marker,
567 counted(request, request),
568 counted(counters(30, 5), nextTotal),
569 ].map((line) => {
570 const record = parseCodexLine(line, state);
571 return record === null ? 0 : totalTokens(record.totals);
572 });
573
574 expect({
575 processed: billed,
576 skipped: codexUsageFormat.readMalformedRecords?.(state),
577 }).toEqual({ processed, skipped: 0 });
578 },
579 );
580
581 it("forgets the old sequence start when a malformed pair zeroes the cumulative baseline", () => {
582 const state = initialCodexScanState();
583 parseCodexLine(turnContext, state);
584 const request = counters(100, 20);
585 const processed = [
586 counted(request, request),
587 counted(counters(30, 5), counters(0, 0)),
588 counted(request, request),
589 counted(counters(30, 5), counters(130, 25)),
590 ].map((line) => {
591 const record = parseCodexLine(line, state);
592 return record === null ? 0 : totalTokens(record.totals);
593 });
594
595 expect({
596 processed,
597 skipped: codexUsageFormat.readMalformedRecords?.(state),
598 }).toEqual({ processed: [120, 0, 120, 35], skipped: 1 });
599 });
600
601 it("retains initial tier evidence only for the own OpenAI session and exact context until native settings arrive", () => {
602 const meta = (id: string, model_provider?: string) =>
603 JSON.stringify({ type: "session_meta", payload: { id, model_provider } });
604 const context = (turn_id?: string, model?: string) =>
605 JSON.stringify({ type: "turn_context", payload: { turn_id, model } });
606 for (const provider of ["openai", "t3", "custom", undefined]) {
607 const state = initialCodexScanState();
608 parseCodexLine(meta("own-session", provider), state);
609 parseCodexLine(meta("ancestor", provider === "openai" ? "custom" : "openai"), state);
610 parseCodexLine(context("first-turn", "gpt-6-astra"), state);
611 const first = parseCodexLine(tokenCount(100, 0, 1, 0), state);
612 expect(first).toMatchObject({
613 sessionId: "own-session",
614 model: "gpt-6-astra",
615 speed: "standard",
616 });
617 if (provider === "openai") {
618 expect(first).toHaveProperty("unresolvedServiceTierTurnId", "first-turn");
619 } else {
620 expect(first).not.toHaveProperty("unresolvedServiceTierTurnId");
621 }
622
623 parseCodexLine(context(undefined, "gpt-6-astra"), state);
624 expect(parseCodexLine(tokenCount(100, 0, 2, 0), state)).not.toHaveProperty(
625 "unresolvedServiceTierTurnId",
626 );
627 parseCodexLine(context("missing-model"), state);
628 expect(parseCodexLine(tokenCount(100, 0, 3, 0), state)).not.toHaveProperty(
629 "unresolvedServiceTierTurnId",
630 );
631 parseCodexLine(context("next-turn", "gpt-6-astra"), state);
632 const next = parseCodexLine(tokenCount(100, 0, 4, 0), state);
633 if (provider === "openai") {
634 expect(next).toHaveProperty("unresolvedServiceTierTurnId", "next-turn");
635 } else {
636 expect(next).not.toHaveProperty("unresolvedServiceTierTurnId");
637 }
638 }
639
640 for (const service_tier of ["standard", "", undefined, "priority", "ultrafast"]) {
641 const state = initialCodexScanState();
642 parseCodexLine(meta("own-session", "openai"), state);
643 parseCodexLine(context("first-turn", "gpt-6-astra"), state);
644 expect(parseCodexLine(tokenCount(100, 0, 1, 0), state)).toHaveProperty(
645 "unresolvedServiceTierTurnId",
646 "first-turn",
647 );
648 parseCodexLine(
649 JSON.stringify({
650 type: "event_msg",
651 payload: { type: "thread_settings_applied", thread_settings: { service_tier } },
652 }),
653 state,
654 );
655 for (const output of [2, 3]) {
656 parseCodexLine(context(`native-turn-${output}`, "gpt-6-astra"), state);
657 const record = parseCodexLine(tokenCount(100, 0, output, 0), state);
658 expect(record?.speed).toBe(
659 service_tier === "priority"
660 ? "fast"
661 : service_tier === "ultrafast"
662 ? "ultrafast"
663 : "standard",
664 );
665 expect(record).not.toHaveProperty("unresolvedServiceTierTurnId");
666 }
667 }
668 });
669
670 // A forked/subagent rollout opens with the parent's history copied in and
671 // every line re-stamped to the fork instant, then the ancestors' session
672 // metas. Counting those again multiplied usage ~1.85x on real data.
673 describe("forked rollouts", () => {
674 const meta = (overrides: {
675 id: string;
676 timestamp: string;
677 forkedFromId?: string;
678 spawnParentId?: string;
679 }) =>
680 JSON.stringify({
681 type: "session_meta",
682 timestamp: overrides.timestamp,
683 payload: {
684 type: "session_meta",
685 id: overrides.id,
686 ...(overrides.forkedFromId === undefined
687 ? {}
688 : { forked_from_id: overrides.forkedFromId }),
689 ...(overrides.spawnParentId === undefined
690 ? {}
691 : {
692 source: {
693 subagent: { thread_spawn: { parent_thread_id: overrides.spawnParentId } },
694 },
695 }),
696 },
697 });
698 const stamped = (timestamp: string, line: string) => {
699 const parsed = JSON.parse(line) as { timestamp: string };
700 parsed.timestamp = timestamp;
701 return JSON.stringify(parsed);
702 };
703
704 it("drops the re-stamped copied burst and keeps the first real event", () => {
705 const state = initialCodexScanState();
706 const forkInstant = "2026-08-01T05:00:00.000Z";
707 parseCodexLine(meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), state);
708 parseCodexLine(meta({ id: "parent", timestamp: forkInstant }), state);
709 parseCodexLine(stamped(forkInstant, turnContext), state);
710
711 expect(
712 parseCodexLine(
713 stamped("2026-08-01T05:00:00.001Z", counted(counters(100, 10), counters(100, 10))),
714 state,
715 ),
716 ).toBeNull();
717 expect(
718 parseCodexLine(
719 stamped("2026-08-01T05:00:00.002Z", counted(counters(200, 20), counters(300, 30))),
720 state,
721 ),
722 ).toBeNull();
723
724 const real = parseCodexLine(
725 stamped("2026-08-01T05:00:06.000Z", counted(counters(200, 20), counters(500, 50))),
726 state,
727 );
728 expect(real).not.toBeNull();
729 expect(real?.sessionId).toBe("child");
730 expect(real?.totals.outputTokens).toBe(20);
731
732 const next = parseCodexLine(
733 stamped("2026-08-01T05:00:06.100Z", counted(counters(400, 40), counters(900, 90))),
734 state,
735 );
736 expect(next).not.toBeNull();
737 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
738 });
739
740 it("recognizes subagent spawns without forked_from_id", () => {
741 const state = initialCodexScanState();
742 const spawnInstant = "2026-08-01T05:00:00.000Z";
743 parseCodexLine(
744 meta({ id: "child", timestamp: spawnInstant, spawnParentId: "parent" }),
745 state,
746 );
747 parseCodexLine(stamped(spawnInstant, turnContext), state);
748 expect(
749 parseCodexLine(stamped("2026-08-01T05:00:00.001Z", tokenCount(100, 0, 10, 0)), state),
750 ).toBeNull();
751 });
752
753 it("does not suppress anything in a rollout that is not a fork", () => {
754 const state = initialCodexScanState();
755 parseCodexLine(meta({ id: "root", timestamp: "2026-08-01T05:00:00.000Z" }), state);
756 parseCodexLine(stamped("2026-08-01T05:00:00.100Z", turnContext), state);
757 const record = parseCodexLine(
758 stamped("2026-08-01T05:00:00.200Z", tokenCount(100, 0, 10, 0)),
759 state,
760 );
761 expect(record).not.toBeNull();
762 });
763
764 it("forgets the old sequence start when a copied zero baseline rebases the counters", () => {
765 const state = initialCodexScanState();
766 const forkInstant = "2026-08-01T05:00:00.000Z";
767 const request = counters(100, 20);
768 const billed = (line: string) => {
769 const record = parseCodexLine(line, state);
770 return record === null ? 0 : totalTokens(record.totals);
771 };
772 parseCodexLine(stamped("2026-08-01T04:59:00.000Z", turnContext), state);
773 const first = billed(stamped("2026-08-01T04:59:01.000Z", counted(request, request)));
774 parseCodexLine(meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), state);
775 const copied = billed(
776 stamped(
777 "2026-08-01T05:00:00.001Z",
778 counted({ ...counters(0, 0), total_tokens: 26387 }, counters(0, 0)),
779 ),
780 );
781 const afterBurst = billed(stamped("2026-08-01T05:00:06.000Z", counted(request, request)));
782 const next = billed(
783 stamped("2026-08-01T05:00:06.100Z", counted(counters(30, 5), counters(130, 25))),
784 );
785
786 expect({
787 processed: [first, copied, afterBurst, next],
788 skipped: codexUsageFormat.readMalformedRecords?.(state),
789 }).toEqual({ processed: [120, 0, 120, 35], skipped: 0 });
790 });
791
792 it("bills the child's first request when its turn is interrupted before any snapshot", () => {
793 const state = initialCodexScanState();
794 const forkInstant = "2026-08-01T05:00:00.000Z";
795 const request = (timestamp: string, usage: unknown) =>
796 JSON.stringify({ type: "token_usage_record", timestamp, payload: { usage } });
797 const billed = [
798 meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }),
799 stamped(forkInstant, turnContext),
800 request("2026-08-01T05:00:00.001Z", counters(100, 20)),
801 stamped("2026-08-01T05:00:00.001Z", counted(counters(100, 20), counters(100, 20))),
802 request("2026-08-01T05:00:06.000Z", counters(150, 10)),
803 request("2026-08-01T05:00:10.000Z", counters(200, 30)),
804 stamped("2026-08-01T05:00:10.000Z", counted(counters(200, 30), counters(450, 60))),
805 ].flatMap((line) => parseCodexRecords(line, state));
806
807 expect({
808 processed: billed.map((record) => totalTokens(record.totals)),
809 skipped: codexUsageFormat.readMalformedRecords?.(state),
810 }).toEqual({ processed: [160, 230], skipped: 0 });
811 });
812 });
813});