@@ -359,70 +359,91 @@ describe("parseCodexLine", () => {
359359 expect ( state . malformedRecords ) . toBe ( 1 ) ;
360360 } ) ;
361361
362- it ( "bills each interrupted turn's request with the model and tier it was read under" , ( ) => {
362+ it ( "bills each interrupted turn's request with the model, tier and time it was read under" , ( ) => {
363363 const state = initialCodexScanState ( ) ;
364- const line = ( type : string , second : number , payload : Record < string , unknown > ) =>
365- JSON . stringify ( { type, timestamp : `2026-08-01T05:17: 0${ second } .000Z ` , payload } ) ;
366- const context = ( second : number , turn_id : string , model : string ) =>
367- line ( "turn_context" , second , { turn_id, model } ) ;
368- const tier = ( second : number , service_tier : string ) =>
369- line ( "event_msg" , second , {
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 , {
370370 type : "thread_settings_applied" ,
371371 thread_settings : { service_tier } ,
372372 } ) ;
373- const request = ( second : number , usage : unknown ) =>
374- line ( "token_usage_record" , second , { usage } ) ;
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+ } ) ;
375379 const parsed = [
376- line ( "session_meta" , 0 , { id : "session-a" , model_provider : "openai" } ) ,
377- context ( 0 , "turn-1" , "gpt-6-sol" ) ,
378- request ( 1 , counters ( 100 , 20 ) ) ,
379- counted ( counters ( 100 , 20 ) , counters ( 100 , 20 ) ) ,
380- request ( 2 , counters ( 150 , 10 ) ) ,
381- tier ( 3 , "priority" ) ,
382- context ( 3 , "turn-2" , "gpt-6-astra" ) ,
383- request ( 4 , counters ( 50 , 5 ) ) ,
384- request ( 5 , counters ( 30 , 5 ) ) ,
385- tier ( 6 , "default" ) ,
386- context ( 6 , "turn-3" , "gpt-6-sol" ) ,
387- request ( 7 , counters ( 200 , 30 ) ) ,
388- counted ( counters ( 200 , 30 ) , counters ( 530 , 70 ) ) ,
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 ) ) ,
389395 ] . flatMap ( ( value ) => parseCodexRecords ( value , state ) ) ;
390396
397+ const at = ( time : string ) => Date . parse ( `2026-11-0${ time } Z` ) ;
391398 expect (
392399 parsed . map ( ( record ) => ( {
393400 tokens : totalTokens ( record . totals ) ,
394401 model : record . model ,
395402 speed : record . speed ,
396- second : Math . floor ( record . timestampMs / 1000 ) % 60 ,
403+ timestampMs : record . timestampMs ,
397404 unresolvedServiceTierTurnId : record . unresolvedServiceTierTurnId ,
398405 } ) ) ,
399406 ) . toEqual ( [
400407 {
401408 tokens : 120 ,
402409 model : "gpt-6-sol" ,
403410 speed : "standard" ,
404- second : 49 ,
411+ timestampMs : at ( "1T23:59:52" ) ,
405412 unresolvedServiceTierTurnId : "turn-1" ,
406413 } ,
407414 {
408415 tokens : 160 ,
409416 model : "gpt-6-sol" ,
410417 speed : "standard" ,
411- second : 2 ,
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" ) ,
412426 unresolvedServiceTierTurnId : "turn-1" ,
413427 } ,
414428 {
415- tokens : 90 ,
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 ,
416437 model : "gpt-6-astra" ,
417438 speed : "fast" ,
418- second : 4 ,
439+ timestampMs : at ( "2T00:00:04" ) ,
419440 unresolvedServiceTierTurnId : undefined ,
420441 } ,
421442 {
422443 tokens : 230 ,
423444 model : "gpt-6-sol" ,
424445 speed : "standard" ,
425- second : 49 ,
446+ timestampMs : at ( "2T00:00:07" ) ,
426447 unresolvedServiceTierTurnId : undefined ,
427448 } ,
428449 ] ) ;
@@ -767,5 +788,26 @@ describe("parseCodexLine", () => {
767788 skipped : codexUsageFormat . readMalformedRecords ?.( state ) ,
768789 } ) . toEqual ( { processed : [ 120 , 0 , 120 , 35 ] , skipped : 0 } ) ;
769790 } ) ;
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+ } ) ;
770812 } ) ;
771813} ) ;
0 commit comments