Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

codexUsage.test.ts

904 lines33.7 KB

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;
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("bills request-ledger usage through resumed context counters and compaction", () => {
454 const state = initialCodexScanState();
455 const request = (response_id: string, usage: unknown) =>
456 JSON.stringify({
457 type: "token_usage_record",
458 timestamp: "2026-08-01T05:17:45.000Z",
459 payload: { response_id, usage },
460 });
461 parseCodexLine(sessionMeta, state);
462 parseCodexLine(turnContext, state);
463 parseCodexLine(counted(counters(100, 20), counters(1000, 200)), state);
464 // Resume restores older context counters, while the request ledger continues.
465 expect(
466 parseCodexLine(request("response-1", counters(80, 10)), state)?.totals.outputTokens,
467 ).toBe(10);
468 expect(parseCodexLine(counted(counters(80, 10), counters(580, 110)), state)).toBeNull();
469 // Rate-limit refreshes repeat the snapshot without another request.
470 expect(parseCodexLine(counted(counters(80, 10), counters(580, 110)), state)).toBeNull();
471 expect(
472 parseCodexLine(request("compaction", counters(90, 15)), state)?.totals.outputTokens,
473 ).toBe(15);
474 parseCodexLine(turnContext, state);
475 expect(
476 parseCodexLine(counted({ ...counters(0, 0), total_tokens: 40 }, counters(580, 110)), state),
477 ).toBeNull();
478 expect(parseCodexLine(request("response-2", counters(30, 5)), state)?.totals.outputTokens).toBe(
479 5,
480 );
481 expect(parseCodexLine(counted(counters(30, 5), counters(610, 115)), state)).toBeNull();
482 expect(state.malformedRecords).toBe(0);
483 // A later turn from an older CLI can still report usage through snapshots alone.
484 parseCodexLine(turnContext, state);
485 expect(
486 parseCodexLine(counted(counters(20, 2), counters(630, 117)), state)?.totals.outputTokens,
487 ).toBe(2);
488 expect(
489 parseCodexLine(request("interrupted", counters(40, 4)), state)?.totals.outputTokens,
490 ).toBe(4);
491 parseCodexLine(turnContext, state);
492 expect(
493 parseCodexLine(counted(counters(10, 1), counters(680, 122)), state)?.totals.outputTokens,
494 ).toBe(1);
495 expect(parseCodexLine(request("", counters(15, 3)), state)).toBeNull();
496 parseCodexLine(turnContext, state);
497 const transition = parseCodexRecords(request("upgraded", counters(25, 5)), state);
498 expect(transition.map((record) => record.totals.outputTokens)).toEqual([3, 5]);
499 expect(parseCodexLine(counted(counters(25, 5), counters(720, 130)), state)).toBeNull();
500 expect(parseCodexLine(request("", counters(10, 1)), state)).toBeNull();
501 expect(
502 parseCodexLine(counted(counters(10, 1), counters(730, 131)), state)?.totals.outputTokens,
503 ).toBe(1);
504 expect(parseCodexLine(counted(counters(10, 1), counters(730, 131)), state)).toBeNull();
505 expect(state.malformedRecords).toBe(0);
506 });
507​
508 it("does not bill a context-window estimate or lose the following real request", () => {
509 const state = initialCodexScanState();
510 parseCodexLine(turnContext, state);
511 parseCodexLine(counted(counters(100, 20), counters(100, 20)), state);
512 const estimate = { ...counters(0, 0), total_tokens: 1000 };
513 expect(
514 parseCodexLine(
515 counted({ ...estimate, total_tokens: 880 }, estimate, { model_context_window: 1000 }),
516 state,
517 ),
518 ).toBeNull();
519 expect(
520 parseCodexLine(counted(counters(10, 2), { ...counters(10, 2), total_tokens: 1012 }), state)
521 ?.totals.outputTokens,
522 ).toBe(2);
523 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
524 });
525​
526 it.each([false, true])(
527 "accepts the post-compaction context marker without a malformed record (prior request: %s)",
528 (priorRequest) => {
529 const state = initialCodexScanState();
530 parseCodexLine(turnContext, state);
531 if (priorRequest) parseCodexLine(counted(counters(100, 20), counters(100, 20)), state);
532 const marker = counted({ ...counters(0, 0), total_tokens: 26387 }, counters(0, 0), {
533 model_context_window: 475000,
534 });
535 expect(parseCodexLine(marker, state)).toBeNull();
536 expect(
537 parseCodexLine(counted(counters(30, 5), counters(30, 5)), state)?.totals.outputTokens,
538 ).toBe(5);
539 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
540 },
541 );
542​
543 it("bills the first request after a resume restarts the cumulative counters", () => {
544 const state = initialCodexScanState();
545 parseCodexLine(turnContext, state);
546 parseCodexLine(counted(counters(100, 20), counters(100, 20)), state);
547 expect(
548 parseCodexLine(counted(counters(30, 5), counters(30, 5)), state)?.totals.outputTokens,
549 ).toBe(5);
550 expect(
551 parseCodexLine(counted(counters(10, 2), counters(40, 7)), state)?.totals.outputTokens,
552 ).toBe(2);
553 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
554 });
555​
556 const later = (line: string) => line.replace("05:17:49.919Z", "07:17:49.919Z");
557 it.each([
558 {
559 name: "bills a later request that continues from an earlier snapshot",
560 tail: [
561 later(counted(counters(20, 4), counters(130, 26))),
562 later(counted(counters(5, 1), counters(135, 27))),
563 ],
564 processed: [120, 12, 35, 24, 6],
565 skipped: 0,
566 },
567 {
568 name: "does not bill a re-emitted earlier snapshot again",
569 tail: [counted(counters(10, 2), counters(110, 22))],
570 processed: [120, 12, 35, 0],
571 skipped: 1,
572 },
573 ])("$name when cumulative counters rewind", ({ tail, processed, skipped }) => {
574 const state = initialCodexScanState();
575 parseCodexLine(turnContext, state);
576 const billed = [
577 counted(counters(100, 20), counters(100, 20)),
578 counted(counters(10, 2), counters(110, 22)),
579 counted(counters(30, 5), counters(140, 27)),
580 ...tail,
581 ].map((line) => {
582 const record = parseCodexLine(line, state);
583 return record === null ? 0 : totalTokens(record.totals);
584 });
585​
586 expect({ processed: billed, skipped: codexUsageFormat.readMalformedRecords?.(state) }).toEqual({
587 processed,
588 skipped,
589 });
590 });
591​
592 it("ignores a replay of the current sequence's first request without rewinding the baseline", () => {
593 const state = initialCodexScanState();
594 parseCodexLine(turnContext, state);
595 const request = counters(100, 20);
596 const processed = [
597 counted(request, request),
598 counted(request, counters(200, 40)),
599 counted(request, request),
600 counted(request, counters(300, 60)),
601 ].map((line) => {
602 const record = parseCodexLine(line, state);
603 return record === null ? 0 : totalTokens(record.totals);
604 });
605​
606 expect({
607 processed,
608 skipped: codexUsageFormat.readMalformedRecords?.(state),
609 }).toEqual({ processed: [120, 120, 0, 120], skipped: 0 });
610 });
611​
612 it("bills a genuine restart with the same counters at a new timestamp", () => {
613 const state = initialCodexScanState();
614 parseCodexLine(turnContext, state);
615 const request = counters(100, 20);
616 const lines = [
617 counted(request, request),
618 counted(request, counters(200, 40)),
619 counted(request, request).replace("05:17:49.919Z", "05:18:49.919Z"),
620 counted(counters(30, 5), counters(130, 25)),
621 ];
622 const processed = lines.map((line) => {
623 const record = parseCodexLine(line, state);
624 return record === null ? 0 : totalTokens(record.totals);
625 });
626 expect({ processed, skipped: codexUsageFormat.readMalformedRecords?.(state) }).toEqual({
627 processed: [120, 120, 120, 35],
628 skipped: 0,
629 });
630 });
631​
632 it.each([
633 {
634 reset: true,
635 markerTotal: counters(0, 0),
636 nextTotal: counters(130, 25),
637 processed: [120, 120, 0, 120, 35],
638 },
639 {
640 reset: false,
641 markerTotal: counters(200, 40),
642 nextTotal: counters(230, 45),
643 processed: [120, 120, 0, 0, 35],
644 },
645 ])(
646 "bills a request equal to the old sequence start only after a context marker reset the counters (reset: $reset)",
647 ({ markerTotal, nextTotal, processed }) => {
648 const state = initialCodexScanState();
649 parseCodexLine(turnContext, state);
650 const request = counters(100, 20);
651 const marker = counted({ ...counters(0, 0), total_tokens: 26387 }, markerTotal, {
652 model_context_window: 475000,
653 });
654 const billed = [
655 counted(request, request),
656 counted(request, counters(200, 40)),
657 marker,
658 counted(request, request),
659 counted(counters(30, 5), nextTotal),
660 ].map((line) => {
661 const record = parseCodexLine(line, state);
662 return record === null ? 0 : totalTokens(record.totals);
663 });
664​
665 expect({
666 processed: billed,
667 skipped: codexUsageFormat.readMalformedRecords?.(state),
668 }).toEqual({ processed, skipped: 0 });
669 },
670 );
671​
672 it("forgets the old sequence start when a malformed pair zeroes the cumulative baseline", () => {
673 const state = initialCodexScanState();
674 parseCodexLine(turnContext, state);
675 const request = counters(100, 20);
676 const processed = [
677 counted(request, request),
678 counted(counters(30, 5), counters(0, 0)),
679 counted(request, request),
680 counted(counters(30, 5), counters(130, 25)),
681 ].map((line) => {
682 const record = parseCodexLine(line, state);
683 return record === null ? 0 : totalTokens(record.totals);
684 });
685​
686 expect({
687 processed,
688 skipped: codexUsageFormat.readMalformedRecords?.(state),
689 }).toEqual({ processed: [120, 0, 120, 35], skipped: 1 });
690 });
691​
692 it("retains initial tier evidence only for the own OpenAI session and exact context until native settings arrive", () => {
693 const meta = (id: string, model_provider?: string) =>
694 JSON.stringify({ type: "session_meta", payload: { id, model_provider } });
695 const context = (turn_id?: string, model?: string) =>
696 JSON.stringify({ type: "turn_context", payload: { turn_id, model } });
697 for (const provider of ["openai", "t3", "custom", undefined]) {
698 const state = initialCodexScanState();
699 parseCodexLine(meta("own-session", provider), state);
700 parseCodexLine(meta("ancestor", provider === "openai" ? "custom" : "openai"), state);
701 parseCodexLine(context("first-turn", "gpt-6-astra"), state);
702 const first = parseCodexLine(tokenCount(100, 0, 1, 0), state);
703 expect(first).toMatchObject({
704 sessionId: "own-session",
705 model: "gpt-6-astra",
706 speed: "standard",
707 });
708 if (provider === "openai") {
709 expect(first).toHaveProperty("unresolvedServiceTierTurnId", "first-turn");
710 } else {
711 expect(first).not.toHaveProperty("unresolvedServiceTierTurnId");
712 }
713​
714 parseCodexLine(context(undefined, "gpt-6-astra"), state);
715 expect(parseCodexLine(tokenCount(100, 0, 2, 0), state)).not.toHaveProperty(
716 "unresolvedServiceTierTurnId",
717 );
718 parseCodexLine(context("missing-model"), state);
719 expect(parseCodexLine(tokenCount(100, 0, 3, 0), state)).not.toHaveProperty(
720 "unresolvedServiceTierTurnId",
721 );
722 parseCodexLine(context("next-turn", "gpt-6-astra"), state);
723 const next = parseCodexLine(tokenCount(100, 0, 4, 0), state);
724 if (provider === "openai") {
725 expect(next).toHaveProperty("unresolvedServiceTierTurnId", "next-turn");
726 } else {
727 expect(next).not.toHaveProperty("unresolvedServiceTierTurnId");
728 }
729 }
730​
731 for (const service_tier of ["standard", "", undefined, "priority", "ultrafast"]) {
732 const state = initialCodexScanState();
733 parseCodexLine(meta("own-session", "openai"), state);
734 parseCodexLine(context("first-turn", "gpt-6-astra"), state);
735 expect(parseCodexLine(tokenCount(100, 0, 1, 0), state)).toHaveProperty(
736 "unresolvedServiceTierTurnId",
737 "first-turn",
738 );
739 parseCodexLine(
740 JSON.stringify({
741 type: "event_msg",
742 payload: { type: "thread_settings_applied", thread_settings: { service_tier } },
743 }),
744 state,
745 );
746 for (const output of [2, 3]) {
747 parseCodexLine(context(`native-turn-${output}`, "gpt-6-astra"), state);
748 const record = parseCodexLine(tokenCount(100, 0, output, 0), state);
749 expect(record?.speed).toBe(
750 service_tier === "priority"
751 ? "fast"
752 : service_tier === "ultrafast"
753 ? "ultrafast"
754 : "standard",
755 );
756 expect(record).not.toHaveProperty("unresolvedServiceTierTurnId");
757 }
758 }
759 });
760​
761 // A forked/subagent rollout opens with the parent's history copied in and
762 // every line re-stamped to the fork instant, then the ancestors' session
763 // metas. Counting those again multiplied usage ~1.85x on real data.
764 describe("forked rollouts", () => {
765 const meta = (overrides: {
766 id: string;
767 timestamp: string;
768 forkedFromId?: string;
769 spawnParentId?: string;
770 }) =>
771 JSON.stringify({
772 type: "session_meta",
773 timestamp: overrides.timestamp,
774 payload: {
775 type: "session_meta",
776 id: overrides.id,
777 ...(overrides.forkedFromId === undefined
778 ? {}
779 : { forked_from_id: overrides.forkedFromId }),
780 ...(overrides.spawnParentId === undefined
781 ? {}
782 : {
783 source: {
784 subagent: { thread_spawn: { parent_thread_id: overrides.spawnParentId } },
785 },
786 }),
787 },
788 });
789 const stamped = (timestamp: string, line: string) => {
790 const parsed = JSON.parse(line) as { timestamp: string };
791 parsed.timestamp = timestamp;
792 return JSON.stringify(parsed);
793 };
794​
795 it("drops the re-stamped copied burst and keeps the first real event", () => {
796 const state = initialCodexScanState();
797 const forkInstant = "2026-08-01T05:00:00.000Z";
798 parseCodexLine(meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), state);
799 parseCodexLine(meta({ id: "parent", timestamp: forkInstant }), state);
800 parseCodexLine(stamped(forkInstant, turnContext), state);
801​
802 expect(
803 parseCodexLine(
804 stamped("2026-08-01T05:00:00.001Z", counted(counters(100, 10), counters(100, 10))),
805 state,
806 ),
807 ).toBeNull();
808 expect(
809 parseCodexLine(
810 stamped("2026-08-01T05:00:00.002Z", counted(counters(200, 20), counters(300, 30))),
811 state,
812 ),
813 ).toBeNull();
814​
815 const real = parseCodexLine(
816 stamped("2026-08-01T05:00:06.000Z", counted(counters(200, 20), counters(500, 50))),
817 state,
818 );
819 expect(real).not.toBeNull();
820 expect(real?.sessionId).toBe("child");
821 expect(real?.totals.outputTokens).toBe(20);
822​
823 const next = parseCodexLine(
824 stamped("2026-08-01T05:00:06.100Z", counted(counters(400, 40), counters(900, 90))),
825 state,
826 );
827 expect(next).not.toBeNull();
828 expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0);
829 });
830​
831 it("recognizes subagent spawns without forked_from_id", () => {
832 const state = initialCodexScanState();
833 const spawnInstant = "2026-08-01T05:00:00.000Z";
834 parseCodexLine(
835 meta({ id: "child", timestamp: spawnInstant, spawnParentId: "parent" }),
836 state,
837 );
838 parseCodexLine(stamped(spawnInstant, turnContext), state);
839 expect(
840 parseCodexLine(stamped("2026-08-01T05:00:00.001Z", tokenCount(100, 0, 10, 0)), state),
841 ).toBeNull();
842 });
843​
844 it("does not suppress anything in a rollout that is not a fork", () => {
845 const state = initialCodexScanState();
846 parseCodexLine(meta({ id: "root", timestamp: "2026-08-01T05:00:00.000Z" }), state);
847 parseCodexLine(stamped("2026-08-01T05:00:00.100Z", turnContext), state);
848 const record = parseCodexLine(
849 stamped("2026-08-01T05:00:00.200Z", tokenCount(100, 0, 10, 0)),
850 state,
851 );
852 expect(record).not.toBeNull();
853 });
854​
855 it("forgets the old sequence start when a copied zero baseline rebases the counters", () => {
856 const state = initialCodexScanState();
857 const forkInstant = "2026-08-01T05:00:00.000Z";
858 const request = counters(100, 20);
859 const billed = (line: string) => {
860 const record = parseCodexLine(line, state);
861 return record === null ? 0 : totalTokens(record.totals);
862 };
863 parseCodexLine(stamped("2026-08-01T04:59:00.000Z", turnContext), state);
864 const first = billed(stamped("2026-08-01T04:59:01.000Z", counted(request, request)));
865 parseCodexLine(meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), state);
866 const copied = billed(
867 stamped(
868 "2026-08-01T05:00:00.001Z",
869 counted({ ...counters(0, 0), total_tokens: 26387 }, counters(0, 0)),
870 ),
871 );
872 const afterBurst = billed(stamped("2026-08-01T05:00:06.000Z", counted(request, request)));
873 const next = billed(
874 stamped("2026-08-01T05:00:06.100Z", counted(counters(30, 5), counters(130, 25))),
875 );
876​
877 expect({
878 processed: [first, copied, afterBurst, next],
879 skipped: codexUsageFormat.readMalformedRecords?.(state),
880 }).toEqual({ processed: [120, 0, 120, 35], skipped: 0 });
881 });
882​
883 it("bills the child's first request when its turn is interrupted before any snapshot", () => {
884 const state = initialCodexScanState();
885 const forkInstant = "2026-08-01T05:00:00.000Z";
886 const request = (timestamp: string, usage: unknown) =>
887 JSON.stringify({ type: "token_usage_record", timestamp, payload: { usage } });
888 const billed = [
889 meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }),
890 stamped(forkInstant, turnContext),
891 request("2026-08-01T05:00:00.001Z", counters(100, 20)),
892 stamped("2026-08-01T05:00:00.001Z", counted(counters(100, 20), counters(100, 20))),
893 request("2026-08-01T05:00:06.000Z", counters(150, 10)),
894 request("2026-08-01T05:00:10.000Z", counters(200, 30)),
895 stamped("2026-08-01T05:00:10.000Z", counted(counters(200, 30), counters(450, 60))),
896 ].flatMap((line) => parseCodexRecords(line, state));
897​
898 expect({
899 processed: billed.map((record) => totalTokens(record.totals)),
900 skipped: codexUsageFormat.readMalformedRecords?.(state),
901 }).toEqual({ processed: [160, 230], skipped: 0 });
902 });
903 });
904});