Skip to content

Commit 92eb24f

Browse files
committed
fix(claude): keep the cache TTL split when deduping equal copies
Both Claude dedup comparators (daily and the session loader) settle a tie between duplicates with equal token totals, and in daily also equal cost, by preferring the copy that reports `speed`. Otherwise the first copy seen survives. When one copy of a message reports the 5m/1h `cache_creation` split and the other only the flat `cache_creation_input_tokens`, the split was dropped whenever the flat copy was read first, so `cacheCreation1hTokens` went missing or was undercounted in `modelBreakdowns`. Move the tie-break into one shared `carries_more_usage_detail` helper that ranks the speed tier first (it changes cost) and the TTL split second, and use it from both comparators. Sidechain, token-total and daily cost priorities are unchanged, and inputs without such a pair produce the same output. In calculate mode the split copy never costs less than the flat one for equal tokens, because 1h writes are priced above 5m ones. Daily's cost comparison therefore already kept it whenever 1h tokens were non-zero. The session loader does not compare cost and kept the flat copy, so the same data reported a lower session cost than daily. Preferring the split resolves that tie in the same direction as the cost order.
1 parent c17c591 commit 92eb24f

2 files changed

Lines changed: 83 additions & 5 deletions

File tree

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ use crate::{
2121

2222
use super::{
2323
DailySummaries, DedupeIndexVec, SessionDays, advisor_usages_from_line,
24-
chunk_file_indexes_by_size, deserialize_usage_line, is_anthropic_message_id, is_semver_prefix,
25-
main_thread_session_days,
24+
carries_more_usage_detail, chunk_file_indexes_by_size, deserialize_usage_line,
25+
is_anthropic_message_id, is_semver_prefix, main_thread_session_days,
2626
paths::{SinceFiles, claude_paths, extract_project, split_files_before_since, usage_files},
2727
push_deduped_index, push_deduped_session_alias, push_new_deduped_index,
2828
sidechain_entry_replay_dedupe_hash, sidechain_replay_dedupe_hash, usage_dedupe_hash,
@@ -652,7 +652,7 @@ fn should_replace_deduped_daily_entry(
652652
if candidate.cost != existing.cost {
653653
return candidate.cost > existing.cost;
654654
}
655-
candidate.usage.speed.is_some() && existing.usage.speed.is_none()
655+
carries_more_usage_detail(&candidate.usage, &existing.usage)
656656
}
657657

658658
fn is_sidechain_daily_entry(entry: &DailyLoadedEntry) -> bool {
@@ -992,6 +992,40 @@ mod tests {
992992
);
993993
}
994994

995+
#[test]
996+
fn keeps_the_cache_ttl_split_from_an_otherwise_equal_daily_duplicate() {
997+
for lines in [
998+
[
999+
r#"{"timestamp":"2026-05-22T02:34:40.000Z","sessionId":"session-a","requestId":"req-a","message":{"id":"msg_a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":100}}}"#,
1000+
r#"{"timestamp":"2026-05-22T02:34:40.000Z","sessionId":"session-a","requestId":"req-a","message":{"id":"msg_a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":100,"cache_creation":{"ephemeral_5m_input_tokens":40,"ephemeral_1h_input_tokens":60}}}}"#,
1001+
],
1002+
[
1003+
r#"{"timestamp":"2026-05-22T02:34:40.000Z","sessionId":"session-a","requestId":"req-a","message":{"id":"msg_a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":100,"cache_creation":{"ephemeral_5m_input_tokens":40,"ephemeral_1h_input_tokens":60}}}}"#,
1004+
r#"{"timestamp":"2026-05-22T02:34:40.000Z","sessionId":"session-a","requestId":"req-a","message":{"id":"msg_a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":100}}}"#,
1005+
],
1006+
] {
1007+
let fixture = fs_fixture!({
1008+
"projects/project-a/session-a.jsonl": lines.join("\n"),
1009+
});
1010+
let _env = EnvVarsGuard::set_many([(
1011+
"CLAUDE_CONFIG_DIR",
1012+
Some(fixture.root().as_os_str().to_os_string()),
1013+
)]);
1014+
let shared = SharedArgs {
1015+
mode: CostMode::Display,
1016+
timezone: Some("UTC".to_string()),
1017+
single_thread: true,
1018+
..SharedArgs::default()
1019+
};
1020+
1021+
let daily = load_daily_summaries_inner(&shared, None, false, session_days).unwrap();
1022+
1023+
let breakdown = &daily.summaries[0].model_breakdowns[0];
1024+
assert_eq!(breakdown.cache_creation_tokens, 100);
1025+
assert_eq!(breakdown.cache_creation_1h_tokens, Some(60));
1026+
}
1027+
}
1028+
9951029
#[test]
9961030
fn dedupes_anthropic_message_id_from_same_session_at_distinct_timestamps() {
9971031
let mut deduped_indexes = Default::default();

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

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,15 @@ fn should_replace_deduped_entry(candidate: &UsageEntry, existing: &UsageEntry) -
294294
return candidate_total > existing_total;
295295
}
296296

297-
candidate.message.usage.speed.is_some() && existing.message.usage.speed.is_none()
297+
carries_more_usage_detail(&candidate.message.usage, &existing.message.usage)
298+
}
299+
300+
/// Breaks a tie between duplicates with equal token totals by keeping the copy
301+
/// that reports more of the request: the speed tier, then the cache-write TTL
302+
/// split.
303+
fn carries_more_usage_detail(candidate: &TokenUsageRaw, existing: &TokenUsageRaw) -> bool {
304+
let detail = |usage: &TokenUsageRaw| (usage.speed.is_some(), usage.cache_creation.is_some());
305+
detail(candidate) > detail(existing)
298306
}
299307

300308
fn push_deduped_entry(
@@ -990,7 +998,8 @@ mod tests {
990998
push_deduped_entry, read_usage_file, usage_files,
991999
};
9921000
use crate::{
993-
LoadedEntry, PricingMap, TimestampMs, TokenUsageRaw, UsageEntry, UsageMessage,
1001+
LoadedEntry, ModelBreakdown, PricingMap, TimestampMs, TokenUsageRaw, UsageEntry,
1002+
UsageMessage,
9941003
cli::{CostMode, SharedArgs},
9951004
};
9961005
use ccusage_test_support::{EnvVarsGuard, fs_fixture};
@@ -1347,6 +1356,41 @@ null,"usage":{"input_tokens":1}}}"#
13471356
);
13481357
}
13491358

1359+
#[test]
1360+
fn keeps_the_cache_ttl_split_from_an_otherwise_equal_duplicate() {
1361+
for lines in [
1362+
[
1363+
r#"{"timestamp":"2026-05-22T02:34:40.000Z","sessionId":"session-a","requestId":"req-a","message":{"id":"msg_a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":100}}}"#,
1364+
r#"{"timestamp":"2026-05-22T02:34:40.000Z","sessionId":"session-a","requestId":"req-a","message":{"id":"msg_a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":100,"cache_creation":{"ephemeral_5m_input_tokens":40,"ephemeral_1h_input_tokens":60}}}}"#,
1365+
],
1366+
[
1367+
r#"{"timestamp":"2026-05-22T02:34:40.000Z","sessionId":"session-a","requestId":"req-a","message":{"id":"msg_a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":100,"cache_creation":{"ephemeral_5m_input_tokens":40,"ephemeral_1h_input_tokens":60}}}}"#,
1368+
r#"{"timestamp":"2026-05-22T02:34:40.000Z","sessionId":"session-a","requestId":"req-a","message":{"id":"msg_a","model":"claude-sonnet-4-20250514","usage":{"input_tokens":10,"output_tokens":5,"cache_creation_input_tokens":100}}}"#,
1369+
],
1370+
] {
1371+
let fixture = fs_fixture!({
1372+
"projects/project-a/session-a.jsonl": lines.join("\n"),
1373+
});
1374+
let _env = EnvVarsGuard::set_many([(
1375+
"CLAUDE_CONFIG_DIR",
1376+
Some(fixture.root().as_os_str().to_os_string()),
1377+
)]);
1378+
let shared = SharedArgs {
1379+
mode: CostMode::Display,
1380+
single_thread: true,
1381+
..SharedArgs::default()
1382+
};
1383+
1384+
let deduped = load_deduped_entries(&shared, None).unwrap();
1385+
1386+
assert_eq!(deduped.entries.len(), 1);
1387+
let mut breakdown = ModelBreakdown::default();
1388+
breakdown.add_usage(&deduped.entries[0].data.message.usage);
1389+
assert_eq!(breakdown.cache_creation_tokens, 100);
1390+
assert_eq!(breakdown.cache_creation_1h_tokens, Some(60));
1391+
}
1392+
}
1393+
13501394
#[test]
13511395
fn dedupes_anthropic_message_id_from_same_session_at_distinct_timestamps() {
13521396
let fixture = fs_fixture!({

0 commit comments

Comments
 (0)