@@ -511,6 +511,7 @@ fn is_valid_usage_entry(data: &UsageEntry) -> bool {
511511}
512512
513513pub ( crate ) fn has_unsupported_null_field ( line : & [ u8 ] ) -> bool {
514+ let iterations = iterations_span ( 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;
@@ -525,17 +526,59 @@ pub(crate) fn has_unsupported_null_field(line: &[u8]) -> bool {
525526 while field_start > 0 && line[ field_start] != b'"' {
526527 field_start -= 1 ;
527528 }
528- if line. get ( field_start) == Some ( & b'"' )
529- && is_unsupported_nullable_field ( & line[ field_start + 1 ..field_end] )
530- {
531- return true ;
529+ if line. get ( field_start) == Some ( & b'"' ) {
530+ let field = & line[ field_start + 1 ..field_end] ;
531+ // `message.usage.iterations[].model` is `Option<String>` in `UsageIteration`,
532+ // so a null there is fine; only `message.model` (outside the array) is not.
533+ let nested_iteration_model = field == b"model"
534+ && iterations
535+ . as_ref ( )
536+ . is_some_and ( |span| span. contains ( & null_index) ) ;
537+ if !nested_iteration_model && is_unsupported_nullable_field ( field) {
538+ return true ;
539+ }
532540 }
533541 }
534542 offset = null_index + b":null" . len ( ) ;
535543 }
536544 false
537545}
538546
547+ /// Byte range of the `"iterations":[...]` array body, found without parsing the line: the
548+ /// scan skips string contents (with escapes) and tracks bracket depth until the array closes.
549+ /// Claude Code writes compact JSON, so the marker carries no whitespace, like `"usage":{`.
550+ fn iterations_span ( line : & [ u8 ] ) -> Option < std:: ops:: Range < usize > > {
551+ const MARKER : & [ u8 ] = br#""iterations":["# ;
552+ let start = memmem:: find ( line, MARKER ) ? + MARKER . len ( ) ;
553+ let mut depth = 1usize ;
554+ let mut in_string = false ;
555+ let mut escaped = false ;
556+ for ( index, & byte) in line[ start..] . iter ( ) . enumerate ( ) {
557+ if in_string {
558+ if escaped {
559+ escaped = false ;
560+ } else if byte == b'\\' {
561+ escaped = true ;
562+ } else if byte == b'"' {
563+ in_string = false ;
564+ }
565+ continue ;
566+ }
567+ match byte {
568+ b'"' => in_string = true ,
569+ b'[' | b'{' => depth += 1 ,
570+ b']' | b'}' => {
571+ depth -= 1 ;
572+ if depth == 0 {
573+ return Some ( start..start + index) ;
574+ }
575+ }
576+ _ => { }
577+ }
578+ }
579+ None
580+ }
581+
539582fn is_unsupported_nullable_field ( field : & [ u8 ] ) -> bool {
540583 // Match the raw field bytes directly against the known set of fields that
541584 // are not allowed to be `null`. Comparing byte slices lets rustc dispatch on
@@ -713,6 +756,55 @@ mod tests {
713756 ) ) ;
714757 }
715758
759+ #[ test]
760+ fn allows_null_iteration_model_but_still_rejects_other_nulls ( ) {
761+ // Claude Code 2.1.266+ writes `"model":null` for the main-model iteration of Fable 5.1
762+ // entries; `UsageIteration.model` is optional, so the line must be kept.
763+ assert ! ( !has_unsupported_null_field(
764+ br#"{"message":{"model":"claude-fable-5-1","usage":{"input_tokens":2,"output_tokens":289,"iterations":[{"type":"message","model":null,"input_tokens":2,"output_tokens":289}]}}}"#
765+ ) ) ;
766+ // A missing iteration model and an empty iterations array are unchanged.
767+ assert ! ( !has_unsupported_null_field(
768+ br#"{"message":{"model":"m","usage":{"input_tokens":2,"iterations":[{"type":"message","input_tokens":2}]}}}"#
769+ ) ) ;
770+ assert ! ( !has_unsupported_null_field(
771+ br#"{"message":{"model":"m","usage":{"input_tokens":2,"iterations":[]}}}"#
772+ ) ) ;
773+ // Strings inside the array may contain brackets or escaped quotes without ending the span.
774+ assert ! ( !has_unsupported_null_field(
775+ br#"{"message":{"model":"m","usage":{"iterations":[{"type":"ad]vi\"sor}","model":null,"input_tokens":1}]}}}"#
776+ ) ) ;
777+ // `message.model` null is still rejected, before or after the iterations array.
778+ assert ! ( has_unsupported_null_field(
779+ br#"{"message":{"model":null,"usage":{"iterations":[{"type":"message","model":"m"}]}}}"#
780+ ) ) ;
781+ assert ! ( has_unsupported_null_field(
782+ br#"{"message":{"usage":{"iterations":[{"type":"message","model":"m"}]},"model":null}}"#
783+ ) ) ;
784+ // Other unsupported nulls inside the array are still rejected.
785+ assert ! ( has_unsupported_null_field(
786+ br#"{"message":{"model":"m","usage":{"iterations":[{"type":"message","model":null,"cache_read_input_tokens":null}]}}}"#
787+ ) ) ;
788+ }
789+
790+ #[ test]
791+ fn counts_entries_whose_iteration_model_is_null ( ) {
792+ let fixture = fs_fixture ! ( {
793+ "projects/project-a/session-a/chat.jsonl" : r#"{"type":"assistant","timestamp":"2026-09-12T04:38:42.296Z","version":"2.1.268","sessionId":"session-a","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}]}}}"# ,
794+ } ) ;
795+
796+ let loaded = read_usage_file (
797+ & fixture. path ( "projects/project-a/session-a/chat.jsonl" ) ,
798+ None ,
799+ CostMode :: Calculate ,
800+ Some ( & PricingMap :: default ( ) ) ,
801+ ) ;
802+
803+ assert_eq ! ( loaded. entries. len( ) , 1 ) ;
804+ assert_eq ! ( loaded. entries[ 0 ] . model. as_deref( ) , Some ( "claude-fable-5-1" ) ) ;
805+ assert_eq ! ( loaded. entries[ 0 ] . data. message. usage. output_tokens, 289 ) ;
806+ }
807+
716808 #[ test]
717809 fn calculates_advisor_cost_with_the_advisor_model ( ) {
718810 let fixture = fs_fixture ! ( {
0 commit comments