Skip to content

Commit 474874d

Browse files
committed
perf(claude): deserialize nullable lines once
Share the parsed JSON value between null-field validation and typed deserialization for both direct and daily Claude loaders.
1 parent 503f2b8 commit 474874d

2 files changed

Lines changed: 25 additions & 12 deletions

File tree

‎rust/adapters/claude/src/daily.rs‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020

2121
use super::{
2222
advisor_usages_from_line, chunk_file_indexes_by_size, daily_usage_dedupe_hash,
23-
has_unsupported_null_field, is_semver_prefix,
23+
deserialize_usage_line, is_semver_prefix,
2424
paths::{claude_paths, extract_project, usage_files},
2525
sidechain_replay_dedupe_hash,
2626
};
@@ -264,10 +264,7 @@ fn read_daily_usage_file(
264264
if usage_marker.find(line).is_none() {
265265
continue;
266266
}
267-
if has_unsupported_null_field(line) {
268-
continue;
269-
}
270-
let Ok(data) = serde_json::from_slice::<DailyUsageLine>(line) else {
267+
let Some(data) = deserialize_usage_line::<DailyUsageLine>(line) else {
271268
continue;
272269
};
273270
let data = data.into_entry();

‎rust/adapters/claude/src/lib.rs‎

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::{
1414
use jiff::tz::TimeZone as JiffTimeZone;
1515
use memchr::memmem;
1616
use rustc_hash::FxHasher;
17-
use serde::Deserialize;
17+
use serde::{Deserialize, de::DeserializeOwned};
1818

1919
use 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

534550
fn has_unsupported_null_value(

0 commit comments

Comments
 (0)