@@ -511,151 +511,48 @@ fn is_valid_usage_entry(data: &UsageEntry) -> bool {
511511}
512512
513513/// Mirrors the TypeScript loader's schema: a line whose known non-nullable field is `null`
514- /// is skipped before deserialisation. `message.usage.iterations[].model` is the one exception,
515- /// because `UsageIteration.model` is optional and Claude Code writes it as `null`.
514+ /// is skipped before deserialisation. The direct `model` member of an element in either
515+ /// assistant or AgentProgress `usage.iterations` is the one exception, because
516+ /// `UsageIteration.model` is optional and Claude Code writes it as `null`.
516517pub ( crate ) fn has_unsupported_null_field ( line : & [ u8 ] ) -> bool {
517- let iterations = iterations_span ( line) ;
518- let mut offset = 0 ;
519- while let Some ( relative_index) = memmem:: find ( & line[ offset..] , b":null" ) {
520- let null_index = offset + relative_index;
521- let mut field_end = null_index. saturating_sub ( 1 ) ;
522- if line. get ( field_end) != Some ( & b'"' ) {
523- while field_end > 0 && line[ field_end] != b'"' {
524- field_end -= 1 ;
525- }
526- }
527- if line. get ( field_end) == Some ( & b'"' ) {
528- let mut field_start = field_end. saturating_sub ( 1 ) ;
529- while field_start > 0 && line[ field_start] != b'"' {
530- field_start -= 1 ;
531- }
532- if line. get ( field_start) == Some ( & b'"' ) {
533- let field = & line[ field_start + 1 ..field_end] ;
534- // `message.usage.iterations[].model` is `Option<String>` in `UsageIteration`,
535- // so a null there is fine; only `message.model` (outside the array) is not.
536- let nested_iteration_model = field == b"model"
537- && iterations
538- . as_ref ( )
539- . is_some_and ( |span| span. contains ( & null_index) ) ;
540- if !nested_iteration_model && is_unsupported_nullable_field ( field) {
541- return true ;
542- }
543- }
544- }
545- offset = null_index + b":null" . len ( ) ;
546- }
547- false
548- }
549-
550- /// Byte range of the `message.usage.iterations` array body, found without deserialising the line.
551- /// Each object is walked at its own level, so same-named members in unrelated objects or string
552- /// contents cannot provide the exemption.
553- fn iterations_span ( line : & [ u8 ] ) -> Option < std:: ops:: Range < usize > > {
554- let root = expect_after_whitespace ( line, 0 , b'{' ) ?;
555- let message = container_member_body ( line, root, b"message" , b'{' ) ?;
556- let usage = container_member_body ( line, message, b"usage" , b'{' ) ?;
557- let iterations = container_member_body ( line, usage, b"iterations" , b'[' ) ?;
558- Some ( iterations..container_end ( line, iterations) ?)
559- }
560-
561- /// Walks one object level and returns the body start of a named container member.
562- fn container_member_body (
563- line : & [ u8 ] ,
564- start : usize ,
565- wanted_key : & [ u8 ] ,
566- opening : u8 ,
567- ) -> Option < usize > {
568- let mut index = skip_whitespace ( line, start) ;
569- loop {
570- match line. get ( index) ? {
571- b'}' => return None ,
572- b'"' => { }
573- _ => return None ,
574- }
575- let key_end = string_end ( line, index + 1 ) ?;
576- let key = & line[ index + 1 ..key_end] ;
577- let value = expect_after_whitespace ( line, key_end + 1 , b':' ) ?;
578- let value = skip_whitespace ( line, value) ;
579- if key == wanted_key {
580- return ( line. get ( value) == Some ( & opening) ) . then_some ( value + 1 ) ;
581- }
582- let value_end = json_value_end ( line, value) ?;
583- index = skip_whitespace ( line, value_end + 1 ) ;
584- match line. get ( index) ? {
585- b',' => index = skip_whitespace ( line, index + 1 ) ,
586- b'}' => return None ,
587- _ => return None ,
588- }
589- }
590- }
591-
592- /// Index of the last byte in the JSON value beginning at `start`.
593- fn json_value_end ( line : & [ u8 ] , start : usize ) -> Option < usize > {
594- match line. get ( start) ? {
595- b'[' | b'{' => container_end ( line, start + 1 ) ,
596- b'"' => string_end ( line, start + 1 ) ,
597- _ => {
598- let mut end = start;
599- while line
600- . get ( end)
601- . is_some_and ( |byte| !matches ! ( byte, b',' | b'}' ) )
602- {
603- end += 1 ;
604- }
605- ( end > start) . then_some ( end - 1 )
606- }
518+ if memmem:: find ( line, b"null" ) . is_none ( ) {
519+ return false ;
607520 }
521+ let Ok ( root) = serde_json:: from_slice :: < serde_json:: Value > ( line) else {
522+ return false ;
523+ } ;
524+ let iteration_arrays = [
525+ root. pointer ( "/message/usage/iterations" ) ,
526+ root. pointer ( "/data/message/message/usage/iterations" ) ,
527+ ]
528+ . into_iter ( )
529+ . flatten ( )
530+ . collect :: < Vec < _ > > ( ) ;
531+ has_unsupported_null_value ( & root, false , & iteration_arrays)
608532}
609533
610- /// Index of the bracket that closes the array or object whose body starts at `start`.
611- fn container_end ( line : & [ u8 ] , start : usize ) -> Option < usize > {
612- let mut depth = 1usize ;
613- let mut index = start;
614- while let Some ( & byte) = line. get ( index) {
615- match byte {
616- b'"' => index = string_end ( line, index + 1 ) ?,
617- b'[' | b'{' => depth += 1 ,
618- b']' | b'}' => {
619- depth -= 1 ;
620- if depth == 0 {
621- return Some ( index) ;
622- }
534+ fn has_unsupported_null_value (
535+ value : & serde_json:: Value ,
536+ allow_iteration_model : bool ,
537+ iteration_arrays : & [ & serde_json:: Value ] ,
538+ ) -> bool {
539+ match value {
540+ serde_json:: Value :: Object ( fields) => fields. iter ( ) . any ( |( field, value) | {
541+ if value. is_null ( ) && is_unsupported_nullable_field ( field. as_bytes ( ) ) {
542+ return !( allow_iteration_model && field == "model" ) ;
623543 }
624- _ => { }
625- }
626- index += 1 ;
627- }
628- None
629- }
630-
631- /// Index of the quote that closes the string whose contents start at `start`.
632- fn string_end ( line : & [ u8 ] , start : usize ) -> Option < usize > {
633- let mut index = start;
634- while let Some ( & byte) = line. get ( index) {
635- match byte {
636- b'\\' => index += 1 ,
637- b'"' => return Some ( index) ,
638- _ => { }
544+ has_unsupported_null_value ( value, false , iteration_arrays)
545+ } ) ,
546+ serde_json:: Value :: Array ( values) => {
547+ let is_iteration_array = iteration_arrays
548+ . iter ( )
549+ . any ( |array| std:: ptr:: eq ( * array, value) ) ;
550+ values. iter ( ) . any ( |value| {
551+ has_unsupported_null_value ( value, is_iteration_array, iteration_arrays)
552+ } )
639553 }
640- index += 1 ;
641- }
642- None
643- }
644-
645- fn skip_whitespace ( line : & [ u8 ] , mut index : usize ) -> usize {
646- while line
647- . get ( index)
648- . is_some_and ( |byte| matches ! ( byte, b' ' | b'\t' | b'\n' | b'\r' ) )
649- {
650- index += 1 ;
554+ _ => false ,
651555 }
652- index
653- }
654-
655- /// Index just past `expected` when it is the next non-whitespace byte at or after `start`.
656- fn expect_after_whitespace ( line : & [ u8 ] , start : usize , expected : u8 ) -> Option < usize > {
657- let index = skip_whitespace ( line, start) ;
658- ( line. get ( index) == Some ( & expected) ) . then_some ( index + 1 )
659556}
660557
661558fn is_unsupported_nullable_field ( field : & [ u8 ] ) -> bool {
@@ -823,6 +720,9 @@ mod tests {
823720 assert ! ( has_unsupported_null_field(
824721 br#"{"message":{"model":null,"usage":{"input_tokens":0}}}"#
825722 ) ) ;
723+ assert ! ( has_unsupported_null_field(
724+ br#"{"message":{"model": null,"usage":{"input_tokens":0}}}"#
725+ ) ) ;
826726 assert ! ( has_unsupported_null_field(
827727 br#"{"sessionId":null,"message":{"usage":{"input_tokens":0}}}"#
828728 ) ) ;
@@ -876,6 +776,10 @@ mod tests {
876776 assert ! ( !has_unsupported_null_field(
877777 br#"{"message": {"model": "m", "usage" : { "input_tokens": 2, "iterations" : [ {"type": "message", "model": null} ] }}}"#
878778 ) ) ;
779+ // Escaped property names are decoded before the structural path check.
780+ assert ! ( !has_unsupported_null_field(
781+ br#"{"message":{"model":"m","usage":{"iter\u0061tions" : [{"type":"message","model" : null}]}}}"#
782+ ) ) ;
879783 // An earlier `iterations` array in another object does not stand in for the real one.
880784 assert ! ( !has_unsupported_null_field(
881785 br#"{"toolUseResult":{"iterations":[{"model":"x"}]},"message":{"model":"m","usage":{"iterations":[{"type":"message","model":null}]}}}"#
@@ -891,6 +795,10 @@ mod tests {
891795 assert ! ( has_unsupported_null_field(
892796 br#"{"message":{"model":"m","usage":{"server_tool_use":{"iterations":[{"model":null}]},"input_tokens":1}}}"#
893797 ) ) ;
798+ // A nested object's model is not the optional model of the iteration itself.
799+ assert ! ( has_unsupported_null_field(
800+ br#"{"message":{"model":"m","usage":{"iterations":[{"type":"message","metadata":{"model":null}}]}}}"#
801+ ) ) ;
894802 // The key as text inside a string value is not a member.
895803 assert ! ( has_unsupported_null_field(
896804 br#"{"message":{"content":"\"usage\":{\"iterations\":[","model":null,"usage":{"input_tokens":1}}}"#
@@ -903,11 +811,15 @@ mod tests {
903811 assert ! ( !has_unsupported_null_field(
904812 br#"{"usage":{"iterations":[{"model":"decoy"}]},"message":{"model":"m","usage":{"iterations":[{"type":"message","model":null}]}}}"#
905813 ) ) ;
814+ // AgentProgress records wrap the assistant message under `data.message.message`.
815+ assert ! ( !has_unsupported_null_field(
816+ br#"{"type":"progress","data":{"message":{"message":{"model":"m","usage":{"iterations":[{"type":"message","model":null}]}}}}}"#
817+ ) ) ;
906818 }
907819
908820 #[ test]
909821 fn malformed_usage_members_do_not_panic_the_null_precheck ( ) {
910- assert ! ( has_unsupported_null_field(
822+ assert ! ( ! has_unsupported_null_field(
911823 br#"{"message":{"model":"m","usage":{"input_tokens":,"iterations":[{"type":"message","model":null}]}}}"#
912824 ) ) ;
913825 }
0 commit comments