The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { describe, expect, it } from "@effect/vitest"; |
| 2 | import { totalTokens } from "@t3tools/provider-core/server/usage"; |
| 3 | |
| 4 | import { |
| 5 | type CodexScanState, |
| 6 | codexUsageFormat, |
| 7 | initialCodexScanState, |
| 8 | parseCodexLine as parseCodexRecords, |
| 9 | } from "./codexUsage.ts"; |
| 10 | |
| 11 | function parseCodexLine(line: string, state: CodexScanState) { |
| 12 | const records = parseCodexRecords(line, state); |
| 13 | expect(records.length).toBeLessThanOrEqual(1); |
| 14 | return records[0] ?? null; |
| 15 | } |
| 16 | |
| 17 | describe("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 | const at = (time: string, line: string) => line.replace("05:17:49.919Z", `${time}.000Z`); |
| 593 | const anonymousRequest = (time: string, usage: unknown) => |
| 594 | JSON.stringify({ |
| 595 | type: "token_usage_record", |
| 596 | timestamp: `2026-08-01T${time}.000Z`, |
| 597 | payload: { usage }, |
| 598 | }); |
| 599 | it.each([ |
| 600 | { |
| 601 | name: "does not bill an older snapshot re-emitted after requests without cumulative counters", |
| 602 | lines: [ |
| 603 | at("10:00:00", counted(counters(100, 20), counters(100, 20))), |
| 604 | at("10:00:05", counted(counters(20, 4))), |
| 605 | at("10:00:10", counted(counters(30, 6))), |
| 606 | at("10:00:05", counted(counters(20, 4), counters(120, 24))), |
| 607 | ], |
| 608 | processed: [120, 24, 36, 0], |
| 609 | skipped: 1, |
| 610 | }, |
| 611 | { |
| 612 | name: "bills a rewind that follows a request without cumulative counters", |
| 613 | lines: [ |
| 614 | at("10:00:00", counted(counters(100, 20))), |
| 615 | at("10:00:10", counted(counters(20, 4), counters(60, 12))), |
| 616 | ], |
| 617 | processed: [120, 24], |
| 618 | skipped: 0, |
| 619 | }, |
| 620 | { |
| 621 | name: "keeps the baseline when it rejects an older snapshot, so the replay that follows is not billed", |
| 622 | lines: [ |
| 623 | at("10:00:00", counted(counters(100, 20), counters(100, 20))), |
| 624 | at("10:00:05", counted(counters(20, 4), counters(120, 24))), |
| 625 | at("10:00:10", counted(counters(30, 6), counters(150, 30))), |
| 626 | at("10:00:05", counted(counters(20, 4), counters(120, 24))), |
| 627 | at("10:00:10", counted(counters(30, 6), counters(150, 30))), |
| 628 | ], |
| 629 | processed: [120, 24, 36, 0, 0], |
| 630 | skipped: 1, |
| 631 | }, |
| 632 | { |
| 633 | name: "keeps the baseline when it rejects a lower snapshot from the baseline's own time", |
| 634 | lines: [ |
| 635 | at("10:00:00", counted(counters(100, 20), counters(100, 20))), |
| 636 | at("10:00:00", counted(counters(20, 4), counters(120, 24))), |
| 637 | at("10:00:00", counted(counters(30, 6), counters(150, 30))), |
| 638 | at("10:00:00", counted(counters(20, 4), counters(120, 24))), |
| 639 | at("10:00:00", counted(counters(30, 6), counters(150, 30))), |
| 640 | ], |
| 641 | processed: [120, 24, 36, 0, 0], |
| 642 | skipped: 1, |
| 643 | }, |
| 644 | { |
| 645 | name: "does not bill replayed snapshots against a cached baseline of unknown age", |
| 646 | lines: [ |
| 647 | at("10:00:00", counted(counters(100, 20), counters(100, 20))), |
| 648 | at("10:00:05", counted(counters(20, 4), counters(120, 24))), |
| 649 | at("10:00:10", counted(counters(30, 6), counters(150, 30))), |
| 650 | at("10:00:00", counted(counters(100, 20), counters(100, 20))), |
| 651 | at("10:00:05", counted(counters(20, 4), counters(120, 24))), |
| 652 | ], |
| 653 | undatedAfter: 3, |
| 654 | processed: [120, 24, 36, 0, 0], |
| 655 | skipped: 1, |
| 656 | }, |
| 657 | { |
| 658 | name: "warns about the anonymous request a rewound snapshot does not report", |
| 659 | lines: [ |
| 660 | at("10:00:00", counted(counters(100, 20), counters(100, 20))), |
| 661 | anonymousRequest("10:00:05", counters(40, 8)), |
| 662 | anonymousRequest("10:00:06", counters(20, 4)), |
| 663 | at("10:00:07", counted(counters(20, 4), counters(60, 12))), |
| 664 | at("10:00:08", counted(counters(10, 2), counters(70, 14))), |
| 665 | ], |
| 666 | processed: [120, 0, 0, 24, 12], |
| 667 | skipped: 1, |
| 668 | }, |
| 669 | ])("$name", ({ lines, undatedAfter, processed, skipped }) => { |
| 670 | const state = initialCodexScanState(); |
| 671 | parseCodexLine(turnContext, state); |
| 672 | const billed = lines.map((line, index) => { |
| 673 | if (index === undatedAfter) delete state.latestCumulativeAtMs; |
| 674 | const record = parseCodexLine(line, state); |
| 675 | return record === null ? 0 : totalTokens(record.totals); |
| 676 | }); |
| 677 | |
| 678 | expect({ processed: billed, skipped: codexUsageFormat.readMalformedRecords?.(state) }).toEqual({ |
| 679 | processed, |
| 680 | skipped, |
| 681 | }); |
| 682 | }); |
| 683 | |
| 684 | it("ignores a replay of the current sequence's first request without rewinding the baseline", () => { |
| 685 | const state = initialCodexScanState(); |
| 686 | parseCodexLine(turnContext, state); |
| 687 | const request = counters(100, 20); |
| 688 | const processed = [ |
| 689 | counted(request, request), |
| 690 | counted(request, counters(200, 40)), |
| 691 | counted(request, request), |
| 692 | counted(request, counters(300, 60)), |
| 693 | ].map((line) => { |
| 694 | const record = parseCodexLine(line, state); |
| 695 | return record === null ? 0 : totalTokens(record.totals); |
| 696 | }); |
| 697 | |
| 698 | expect({ |
| 699 | processed, |
| 700 | skipped: codexUsageFormat.readMalformedRecords?.(state), |
| 701 | }).toEqual({ processed: [120, 120, 0, 120], skipped: 0 }); |
| 702 | }); |
| 703 | |
| 704 | it("bills a genuine restart with the same counters at a new timestamp", () => { |
| 705 | const state = initialCodexScanState(); |
| 706 | parseCodexLine(turnContext, state); |
| 707 | const request = counters(100, 20); |
| 708 | const lines = [ |
| 709 | counted(request, request), |
| 710 | counted(request, counters(200, 40)), |
| 711 | counted(request, request).replace("05:17:49.919Z", "05:18:49.919Z"), |
| 712 | counted(counters(30, 5), counters(130, 25)), |
| 713 | ]; |
| 714 | const processed = lines.map((line) => { |
| 715 | const record = parseCodexLine(line, state); |
| 716 | return record === null ? 0 : totalTokens(record.totals); |
| 717 | }); |
| 718 | expect({ processed, skipped: codexUsageFormat.readMalformedRecords?.(state) }).toEqual({ |
| 719 | processed: [120, 120, 120, 35], |
| 720 | skipped: 0, |
| 721 | }); |
| 722 | }); |
| 723 | |
| 724 | it.each([ |
| 725 | { |
| 726 | reset: true, |
| 727 | markerTotal: counters(0, 0), |
| 728 | nextTotal: counters(130, 25), |
| 729 | processed: [120, 120, 0, 120, 35], |
| 730 | }, |
| 731 | { |
| 732 | reset: false, |
| 733 | markerTotal: counters(200, 40), |
| 734 | nextTotal: counters(230, 45), |
| 735 | processed: [120, 120, 0, 0, 35], |
| 736 | }, |
| 737 | ])( |
| 738 | "bills a request equal to the old sequence start only after a context marker reset the counters (reset: $reset)", |
| 739 | ({ markerTotal, nextTotal, processed }) => { |
| 740 | const state = initialCodexScanState(); |
| 741 | parseCodexLine(turnContext, state); |
| 742 | const request = counters(100, 20); |
| 743 | const marker = counted({ ...counters(0, 0), total_tokens: 26387 }, markerTotal, { |
| 744 | model_context_window: 475000, |
| 745 | }); |
| 746 | const billed = [ |
| 747 | counted(request, request), |
| 748 | counted(request, counters(200, 40)), |
| 749 | marker, |
| 750 | counted(request, request), |
| 751 | counted(counters(30, 5), nextTotal), |
| 752 | ].map((line) => { |
| 753 | const record = parseCodexLine(line, state); |
| 754 | return record === null ? 0 : totalTokens(record.totals); |
| 755 | }); |
| 756 | |
| 757 | expect({ |
| 758 | processed: billed, |
| 759 | skipped: codexUsageFormat.readMalformedRecords?.(state), |
| 760 | }).toEqual({ processed, skipped: 0 }); |
| 761 | }, |
| 762 | ); |
| 763 | |
| 764 | it("forgets the old sequence start when a malformed pair zeroes the cumulative baseline", () => { |
| 765 | const state = initialCodexScanState(); |
| 766 | parseCodexLine(turnContext, state); |
| 767 | const request = counters(100, 20); |
| 768 | const processed = [ |
| 769 | counted(request, request), |
| 770 | counted(counters(30, 5), counters(0, 0)), |
| 771 | counted(request, request), |
| 772 | counted(counters(30, 5), counters(130, 25)), |
| 773 | ].map((line) => { |
| 774 | const record = parseCodexLine(line, state); |
| 775 | return record === null ? 0 : totalTokens(record.totals); |
| 776 | }); |
| 777 | |
| 778 | expect({ |
| 779 | processed, |
| 780 | skipped: codexUsageFormat.readMalformedRecords?.(state), |
| 781 | }).toEqual({ processed: [120, 0, 120, 35], skipped: 1 }); |
| 782 | }); |
| 783 | |
| 784 | it("retains initial tier evidence only for the own OpenAI session and exact context until native settings arrive", () => { |
| 785 | const meta = (id: string, model_provider?: string) => |
| 786 | JSON.stringify({ type: "session_meta", payload: { id, model_provider } }); |
| 787 | const context = (turn_id?: string, model?: string) => |
| 788 | JSON.stringify({ type: "turn_context", payload: { turn_id, model } }); |
| 789 | for (const provider of ["openai", "t3", "custom", undefined]) { |
| 790 | const state = initialCodexScanState(); |
| 791 | parseCodexLine(meta("own-session", provider), state); |
| 792 | parseCodexLine(meta("ancestor", provider === "openai" ? "custom" : "openai"), state); |
| 793 | parseCodexLine(context("first-turn", "gpt-6-astra"), state); |
| 794 | const first = parseCodexLine(tokenCount(100, 0, 1, 0), state); |
| 795 | expect(first).toMatchObject({ |
| 796 | sessionId: "own-session", |
| 797 | model: "gpt-6-astra", |
| 798 | speed: "standard", |
| 799 | }); |
| 800 | if (provider === "openai") { |
| 801 | expect(first).toHaveProperty("unresolvedServiceTierTurnId", "first-turn"); |
| 802 | } else { |
| 803 | expect(first).not.toHaveProperty("unresolvedServiceTierTurnId"); |
| 804 | } |
| 805 | |
| 806 | parseCodexLine(context(undefined, "gpt-6-astra"), state); |
| 807 | expect(parseCodexLine(tokenCount(100, 0, 2, 0), state)).not.toHaveProperty( |
| 808 | "unresolvedServiceTierTurnId", |
| 809 | ); |
| 810 | parseCodexLine(context("missing-model"), state); |
| 811 | expect(parseCodexLine(tokenCount(100, 0, 3, 0), state)).not.toHaveProperty( |
| 812 | "unresolvedServiceTierTurnId", |
| 813 | ); |
| 814 | parseCodexLine(context("next-turn", "gpt-6-astra"), state); |
| 815 | const next = parseCodexLine(tokenCount(100, 0, 4, 0), state); |
| 816 | if (provider === "openai") { |
| 817 | expect(next).toHaveProperty("unresolvedServiceTierTurnId", "next-turn"); |
| 818 | } else { |
| 819 | expect(next).not.toHaveProperty("unresolvedServiceTierTurnId"); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | for (const service_tier of ["standard", "", undefined, "priority", "ultrafast"]) { |
| 824 | const state = initialCodexScanState(); |
| 825 | parseCodexLine(meta("own-session", "openai"), state); |
| 826 | parseCodexLine(context("first-turn", "gpt-6-astra"), state); |
| 827 | expect(parseCodexLine(tokenCount(100, 0, 1, 0), state)).toHaveProperty( |
| 828 | "unresolvedServiceTierTurnId", |
| 829 | "first-turn", |
| 830 | ); |
| 831 | parseCodexLine( |
| 832 | JSON.stringify({ |
| 833 | type: "event_msg", |
| 834 | payload: { type: "thread_settings_applied", thread_settings: { service_tier } }, |
| 835 | }), |
| 836 | state, |
| 837 | ); |
| 838 | for (const output of [2, 3]) { |
| 839 | parseCodexLine(context(`native-turn-${output}`, "gpt-6-astra"), state); |
| 840 | const record = parseCodexLine(tokenCount(100, 0, output, 0), state); |
| 841 | expect(record?.speed).toBe( |
| 842 | service_tier === "priority" |
| 843 | ? "fast" |
| 844 | : service_tier === "ultrafast" |
| 845 | ? "ultrafast" |
| 846 | : "standard", |
| 847 | ); |
| 848 | expect(record).not.toHaveProperty("unresolvedServiceTierTurnId"); |
| 849 | } |
| 850 | } |
| 851 | }); |
| 852 | |
| 853 | // A forked/subagent rollout opens with the parent's history copied in and |
| 854 | // every line re-stamped to the fork instant, then the ancestors' session |
| 855 | // metas. Counting those again multiplied usage ~1.85x on real data. |
| 856 | describe("forked rollouts", () => { |
| 857 | const meta = (overrides: { |
| 858 | id: string; |
| 859 | timestamp: string; |
| 860 | forkedFromId?: string; |
| 861 | spawnParentId?: string; |
| 862 | }) => |
| 863 | JSON.stringify({ |
| 864 | type: "session_meta", |
| 865 | timestamp: overrides.timestamp, |
| 866 | payload: { |
| 867 | type: "session_meta", |
| 868 | id: overrides.id, |
| 869 | ...(overrides.forkedFromId === undefined |
| 870 | ? {} |
| 871 | : { forked_from_id: overrides.forkedFromId }), |
| 872 | ...(overrides.spawnParentId === undefined |
| 873 | ? {} |
| 874 | : { |
| 875 | source: { |
| 876 | subagent: { thread_spawn: { parent_thread_id: overrides.spawnParentId } }, |
| 877 | }, |
| 878 | }), |
| 879 | }, |
| 880 | }); |
| 881 | const stamped = (timestamp: string, line: string) => { |
| 882 | const parsed = JSON.parse(line) as { timestamp: string }; |
| 883 | parsed.timestamp = timestamp; |
| 884 | return JSON.stringify(parsed); |
| 885 | }; |
| 886 | |
| 887 | it("drops the re-stamped copied burst and keeps the first real event", () => { |
| 888 | const state = initialCodexScanState(); |
| 889 | const forkInstant = "2026-08-01T05:00:00.000Z"; |
| 890 | parseCodexLine(meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), state); |
| 891 | parseCodexLine(meta({ id: "parent", timestamp: forkInstant }), state); |
| 892 | parseCodexLine(stamped(forkInstant, turnContext), state); |
| 893 | |
| 894 | expect( |
| 895 | parseCodexLine( |
| 896 | stamped("2026-08-01T05:00:00.001Z", counted(counters(100, 10), counters(100, 10))), |
| 897 | state, |
| 898 | ), |
| 899 | ).toBeNull(); |
| 900 | expect( |
| 901 | parseCodexLine( |
| 902 | stamped("2026-08-01T05:00:00.002Z", counted(counters(200, 20), counters(300, 30))), |
| 903 | state, |
| 904 | ), |
| 905 | ).toBeNull(); |
| 906 | |
| 907 | const real = parseCodexLine( |
| 908 | stamped("2026-08-01T05:00:06.000Z", counted(counters(200, 20), counters(500, 50))), |
| 909 | state, |
| 910 | ); |
| 911 | expect(real).not.toBeNull(); |
| 912 | expect(real?.sessionId).toBe("child"); |
| 913 | expect(real?.totals.outputTokens).toBe(20); |
| 914 | |
| 915 | const next = parseCodexLine( |
| 916 | stamped("2026-08-01T05:00:06.100Z", counted(counters(400, 40), counters(900, 90))), |
| 917 | state, |
| 918 | ); |
| 919 | expect(next).not.toBeNull(); |
| 920 | expect(codexUsageFormat.readMalformedRecords?.(state)).toBe(0); |
| 921 | }); |
| 922 | |
| 923 | it("recognizes subagent spawns without forked_from_id", () => { |
| 924 | const state = initialCodexScanState(); |
| 925 | const spawnInstant = "2026-08-01T05:00:00.000Z"; |
| 926 | parseCodexLine( |
| 927 | meta({ id: "child", timestamp: spawnInstant, spawnParentId: "parent" }), |
| 928 | state, |
| 929 | ); |
| 930 | parseCodexLine(stamped(spawnInstant, turnContext), state); |
| 931 | expect( |
| 932 | parseCodexLine(stamped("2026-08-01T05:00:00.001Z", tokenCount(100, 0, 10, 0)), state), |
| 933 | ).toBeNull(); |
| 934 | }); |
| 935 | |
| 936 | it("does not suppress anything in a rollout that is not a fork", () => { |
| 937 | const state = initialCodexScanState(); |
| 938 | parseCodexLine(meta({ id: "root", timestamp: "2026-08-01T05:00:00.000Z" }), state); |
| 939 | parseCodexLine(stamped("2026-08-01T05:00:00.100Z", turnContext), state); |
| 940 | const record = parseCodexLine( |
| 941 | stamped("2026-08-01T05:00:00.200Z", tokenCount(100, 0, 10, 0)), |
| 942 | state, |
| 943 | ); |
| 944 | expect(record).not.toBeNull(); |
| 945 | }); |
| 946 | |
| 947 | it("forgets the old sequence start when a copied zero baseline rebases the counters", () => { |
| 948 | const state = initialCodexScanState(); |
| 949 | const forkInstant = "2026-08-01T05:00:00.000Z"; |
| 950 | const request = counters(100, 20); |
| 951 | const billed = (line: string) => { |
| 952 | const record = parseCodexLine(line, state); |
| 953 | return record === null ? 0 : totalTokens(record.totals); |
| 954 | }; |
| 955 | parseCodexLine(stamped("2026-08-01T04:59:00.000Z", turnContext), state); |
| 956 | const first = billed(stamped("2026-08-01T04:59:01.000Z", counted(request, request))); |
| 957 | parseCodexLine(meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), state); |
| 958 | const copied = billed( |
| 959 | stamped( |
| 960 | "2026-08-01T05:00:00.001Z", |
| 961 | counted({ ...counters(0, 0), total_tokens: 26387 }, counters(0, 0)), |
| 962 | ), |
| 963 | ); |
| 964 | const afterBurst = billed(stamped("2026-08-01T05:00:06.000Z", counted(request, request))); |
| 965 | const next = billed( |
| 966 | stamped("2026-08-01T05:00:06.100Z", counted(counters(30, 5), counters(130, 25))), |
| 967 | ); |
| 968 | |
| 969 | expect({ |
| 970 | processed: [first, copied, afterBurst, next], |
| 971 | skipped: codexUsageFormat.readMalformedRecords?.(state), |
| 972 | }).toEqual({ processed: [120, 0, 120, 35], skipped: 0 }); |
| 973 | }); |
| 974 | |
| 975 | it("bills the child's first request when its turn is interrupted before any snapshot", () => { |
| 976 | const state = initialCodexScanState(); |
| 977 | const forkInstant = "2026-08-01T05:00:00.000Z"; |
| 978 | const request = (timestamp: string, usage: unknown) => |
| 979 | JSON.stringify({ type: "token_usage_record", timestamp, payload: { usage } }); |
| 980 | const billed = [ |
| 981 | meta({ id: "child", timestamp: forkInstant, forkedFromId: "parent" }), |
| 982 | stamped(forkInstant, turnContext), |
| 983 | request("2026-08-01T05:00:00.001Z", counters(100, 20)), |
| 984 | stamped("2026-08-01T05:00:00.001Z", counted(counters(100, 20), counters(100, 20))), |
| 985 | request("2026-08-01T05:00:06.000Z", counters(150, 10)), |
| 986 | request("2026-08-01T05:00:10.000Z", counters(200, 30)), |
| 987 | stamped("2026-08-01T05:00:10.000Z", counted(counters(200, 30), counters(450, 60))), |
| 988 | ].flatMap((line) => parseCodexRecords(line, state)); |
| 989 | |
| 990 | expect({ |
| 991 | processed: billed.map((record) => totalTokens(record.totals)), |
| 992 | skipped: codexUsageFormat.readMalformedRecords?.(state), |
| 993 | }).toEqual({ processed: [160, 230], skipped: 0 }); |
| 994 | }); |
| 995 | }); |
| 996 | }); |