@@ -450,6 +450,61 @@ describe("parseCodexLine", () => {
450450 expect ( state . malformedRecords ) . toBe ( 0 ) ;
451451 } ) ;
452452
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+
453508 it ( "does not bill a context-window estimate or lose the following real request" , ( ) => {
454509 const state = initialCodexScanState ( ) ;
455510 parseCodexLine ( turnContext , state ) ;
0 commit comments