@@ -511,31 +511,128 @@ fn is_valid_usage_entry(data: &UsageEntry) -> bool {
511511}
512512
513513pub ( crate ) fn has_unsupported_null_field ( line : & [ u8 ] ) -> bool {
514+ let excluded = iterations_array_ranges ( line) ;
514515 let mut offset = 0 ;
515516 while let Some ( relative_index) = memmem:: find ( & line[ offset..] , b":null" ) {
516517 let null_index = offset + relative_index;
517- let mut field_end = null_index. saturating_sub ( 1 ) ;
518- if line. get ( field_end) != Some ( & b'"' ) {
519- while field_end > 0 && line[ field_end] != b'"' {
520- field_end -= 1 ;
521- }
522- }
523- if line. get ( field_end) == Some ( & b'"' ) {
524- let mut field_start = field_end. saturating_sub ( 1 ) ;
525- while field_start > 0 && line[ field_start] != b'"' {
526- field_start -= 1 ;
518+ if !is_in_excluded_ranges ( null_index, & excluded) {
519+ let mut field_end = null_index. saturating_sub ( 1 ) ;
520+ if line. get ( field_end) != Some ( & b'"' ) {
521+ while field_end > 0 && line[ field_end] != b'"' {
522+ field_end -= 1 ;
523+ }
527524 }
528- if line. get ( field_start) == Some ( & b'"' )
529- && is_unsupported_nullable_field ( & line[ field_start + 1 ..field_end] )
530- {
531- return true ;
525+ if line. get ( field_end) == Some ( & b'"' ) {
526+ let mut field_start = field_end. saturating_sub ( 1 ) ;
527+ while field_start > 0 && line[ field_start] != b'"' {
528+ field_start -= 1 ;
529+ }
530+ if line. get ( field_start) == Some ( & b'"' )
531+ && is_unsupported_nullable_field ( & line[ field_start + 1 ..field_end] )
532+ {
533+ return true ;
534+ }
532535 }
533536 }
534537 offset = null_index + b":null" . len ( ) ;
535538 }
536539 false
537540}
538541
542+ /// Byte ranges of every `"iterations": [...]` array in the line.
543+ ///
544+ /// `message.usage.iterations[].model` is `Option<String>` in the typed structs
545+ /// (Fable 5.1 writes it as explicit `null`), so nulls in there must not trip
546+ /// the top-level `has_unsupported_null_field` pre-check. The fields that check
547+ /// exists to mirror all live outside the array.
548+ fn iterations_array_ranges ( line : & [ u8 ] ) -> Vec < ( usize , usize ) > {
549+ // Fast path: most lines carry no iterations array at all.
550+ if memmem:: find ( line, br#""iterations""# ) . is_none ( ) {
551+ return Vec :: new ( ) ;
552+ }
553+ let mut ranges = Vec :: new ( ) ;
554+ let mut cursor = 0 ;
555+ let mut in_string = false ;
556+ let mut escaped = false ;
557+ while cursor < line. len ( ) {
558+ let byte = line[ cursor] ;
559+ if in_string {
560+ if escaped {
561+ escaped = false ;
562+ } else if byte == b'\\' {
563+ escaped = true ;
564+ } else if byte == b'"' {
565+ in_string = false ;
566+ }
567+ cursor += 1 ;
568+ continue ;
569+ }
570+ if byte == b'"' {
571+ if line[ cursor..] . starts_with ( br#""iterations""# ) {
572+ let key_start = cursor;
573+ let mut after = cursor + br#""iterations""# . len ( ) ;
574+ while after < line. len ( ) && line[ after] . is_ascii_whitespace ( ) {
575+ after += 1 ;
576+ }
577+ if line. get ( after) == Some ( & b':' ) {
578+ after += 1 ;
579+ while after < line. len ( ) && line[ after] . is_ascii_whitespace ( ) {
580+ after += 1 ;
581+ }
582+ if line. get ( after) == Some ( & b'[' ) {
583+ let array_start = after;
584+ let mut depth = 1usize ;
585+ let mut inner_string = false ;
586+ let mut inner_escaped = false ;
587+ after += 1 ;
588+ while after < line. len ( ) {
589+ let inner = line[ after] ;
590+ if inner_string {
591+ if inner_escaped {
592+ inner_escaped = false ;
593+ } else if inner == b'\\' {
594+ inner_escaped = true ;
595+ } else if inner == b'"' {
596+ inner_string = false ;
597+ }
598+ } else if inner == b'"' {
599+ inner_string = true ;
600+ } else if inner == b'[' {
601+ depth += 1 ;
602+ } else if inner == b']' {
603+ depth -= 1 ;
604+ if depth == 0 {
605+ ranges. push ( ( array_start, after + 1 ) ) ;
606+ break ;
607+ }
608+ }
609+ after += 1 ;
610+ }
611+ if after >= line. len ( ) {
612+ break ;
613+ }
614+ cursor = after;
615+ continue ;
616+ }
617+ }
618+ cursor = key_start + 1 ;
619+ continue ;
620+ }
621+ in_string = true ;
622+ cursor += 1 ;
623+ continue ;
624+ }
625+ cursor += 1 ;
626+ }
627+ ranges
628+ }
629+
630+ fn is_in_excluded_ranges ( index : usize , ranges : & [ ( usize , usize ) ] ) -> bool {
631+ ranges
632+ . iter ( )
633+ . any ( |& ( start, end) | index >= start && index < end)
634+ }
635+
539636fn is_unsupported_nullable_field ( field : & [ u8 ] ) -> bool {
540637 // Match the raw field bytes directly against the known set of fields that
541638 // are not allowed to be `null`. Comparing byte slices lets rustc dispatch on
@@ -713,6 +810,60 @@ mod tests {
713810 ) ) ;
714811 }
715812
813+ #[ test]
814+ fn allows_null_model_inside_iterations ( ) {
815+ assert ! ( !has_unsupported_null_field(
816+ br#"{"message":{"model":"m","usage":{"input_tokens":0,"iterations":[{"type":"message","model":null,"input_tokens":0,"output_tokens":0}]}}}"#
817+ ) ) ;
818+ }
819+
820+ #[ test]
821+ fn still_rejects_top_level_null_model_next_to_iterations ( ) {
822+ assert ! ( has_unsupported_null_field(
823+ br#"{"message":{"model":null,"usage":{"input_tokens":0,"iterations":[{"type":"message","model":null,"input_tokens":0,"output_tokens":0}]}}}"#
824+ ) ) ;
825+ assert ! ( has_unsupported_null_field(
826+ br#"{"message":{"model":"m","usage":{"input_tokens":0,"speed":null,"iterations":[{"type":"message","model":null,"input_tokens":0,"output_tokens":0}]}}}"#
827+ ) ) ;
828+ }
829+
830+ #[ test]
831+ fn ignores_iterations_key_inside_strings ( ) {
832+ assert ! ( has_unsupported_null_field(
833+ br#"{"message":{"model":null,"content":"\"iterations\":[0]","usage":{"input_tokens":0}}}"#
834+ ) ) ;
835+ assert ! ( !has_unsupported_null_field(
836+ br#"{"message":{"model":"m","content":"\"iterations\":[0]","usage":{"input_tokens":0,"iterations":[{"type":"message","model":null,"input_tokens":0,"output_tokens":0}]}}}"#
837+ ) ) ;
838+ }
839+
840+ #[ test]
841+ fn keeps_entry_with_null_model_inside_iterations ( ) {
842+ let fixture = fs_fixture ! ( {
843+ "projects/p/s/chat.jsonl" : r#"{"type":"assistant","timestamp":"2026-09-12T04:38:42.296Z","version":"2.1.268","sessionId":"s","requestId":"req_1","message":{"id":"msg_1","model":"claude-fable-5-1","role":"assistant","usage":{"input_tokens":2,"cache_creation_input_tokens":55866,"cache_read_input_tokens":0,"output_tokens":289,"iterations":[{"type":"message","model":null,"input_tokens":2,"output_tokens":289,"cache_creation_input_tokens":55866,"cache_read_input_tokens":0}]}}}"# ,
844+ } ) ;
845+
846+ let loaded = read_usage_file (
847+ & fixture. path ( "projects/p/s/chat.jsonl" ) ,
848+ None ,
849+ CostMode :: Display ,
850+ None ,
851+ ) ;
852+
853+ assert_eq ! ( loaded. entries. len( ) , 1 ) ;
854+ assert_eq ! ( loaded. entries[ 0 ] . model. as_deref( ) , Some ( "claude-fable-5-1" ) ) ;
855+ assert_eq ! ( loaded. entries[ 0 ] . data. message. usage. input_tokens, 2 ) ;
856+ assert_eq ! ( loaded. entries[ 0 ] . data. message. usage. output_tokens, 289 ) ;
857+ assert_eq ! (
858+ loaded. entries[ 0 ]
859+ . data
860+ . message
861+ . usage
862+ . cache_creation_input_tokens,
863+ 55866
864+ ) ;
865+ }
866+
716867 #[ test]
717868 fn calculates_advisor_cost_with_the_advisor_model ( ) {
718869 let fixture = fs_fixture ! ( {
0 commit comments