@@ -14,7 +14,7 @@ use std::{
1414use jiff:: tz:: TimeZone as JiffTimeZone ;
1515use memchr:: memmem;
1616use rustc_hash:: FxHasher ;
17- use serde:: Deserialize ;
17+ use serde:: { Deserialize , de :: DeserializeOwned } ;
1818
1919use crate :: {
2020 LoadedEntry , LoadedFile , PricingMap , Result , Speed , TimestampMs , TokenUsageRaw , UsageEntry ,
@@ -312,10 +312,7 @@ fn read_usage_file(
312312 if usage_marker. find ( line) . is_none ( ) {
313313 continue ;
314314 }
315- if has_unsupported_null_field ( line) {
316- continue ;
317- }
318- let Ok ( data) = serde_json:: from_slice :: < UsageEntry > ( line) else {
315+ let Some ( data) = deserialize_usage_line :: < UsageEntry > ( line) else {
319316 continue ;
320317 } ;
321318 let Some ( timestamp) = parse_ts_timestamp ( & data. timestamp ) else {
@@ -514,21 +511,40 @@ fn is_valid_usage_entry(data: &UsageEntry) -> bool {
514511/// is skipped before deserialisation. The direct `model` member of an element in either
515512/// assistant or AgentProgress `usage.iterations` is the one exception, because
516513/// `UsageIteration.model` is optional and Claude Code writes it as `null`.
517- pub ( crate ) fn has_unsupported_null_field ( line : & [ u8 ] ) -> bool {
514+ #[ cfg( test) ]
515+ fn has_unsupported_null_field ( line : & [ u8 ] ) -> bool {
518516 if memmem:: find ( line, b"null" ) . is_none ( ) {
519517 return false ;
520518 }
521519 let Ok ( root) = serde_json:: from_slice :: < serde_json:: Value > ( line) else {
522520 return false ;
523521 } ;
522+ has_unsupported_null_field_in_value ( & root)
523+ }
524+
525+ /// Deserializes a transcript line while enforcing Claude's nullable-field contract.
526+ /// Lines containing `null` are parsed into a value once so structural validation and
527+ /// typed deserialization can share the same parse.
528+ pub ( crate ) fn deserialize_usage_line < T : DeserializeOwned > ( line : & [ u8 ] ) -> Option < T > {
529+ if memmem:: find ( line, b"null" ) . is_none ( ) {
530+ return serde_json:: from_slice ( line) . ok ( ) ;
531+ }
532+ let root = serde_json:: from_slice :: < serde_json:: Value > ( line) . ok ( ) ?;
533+ if has_unsupported_null_field_in_value ( & root) {
534+ return None ;
535+ }
536+ serde_json:: from_value ( root) . ok ( )
537+ }
538+
539+ fn has_unsupported_null_field_in_value ( root : & serde_json:: Value ) -> bool {
524540 let iteration_arrays = [
525541 root. pointer ( "/message/usage/iterations" ) ,
526542 root. pointer ( "/data/message/message/usage/iterations" ) ,
527543 ]
528544 . into_iter ( )
529545 . flatten ( )
530546 . collect :: < Vec < _ > > ( ) ;
531- has_unsupported_null_value ( & root, false , & iteration_arrays)
547+ has_unsupported_null_value ( root, false , & iteration_arrays)
532548}
533549
534550fn has_unsupported_null_value (
0 commit comments