Skip to content

Commit 95ebe37

Browse files
committed
fix(claude): rank Fast above Standard when deduping equal copies
`carries_more_usage_detail` ranked the speed tier with `speed.is_some()`, the same check the comparators used on main. That treats `Standard` and `Fast` as equal, so a Standard copy that also carried the cache TTL split could replace a Fast copy, or a Standard copy seen first could survive a later Fast one. Only `Fast` carries the fast-mode price multiplier, so the session loader (which does not compare cost) could undercount the request's cost. Rank the tiers explicitly as None < Standard < Fast, before the TTL split tie-break. Reported by Cubic on #2060.
1 parent 92eb24f commit 95ebe37

1 file changed

Lines changed: 48 additions & 4 deletions

File tree

  • rust/adapters/claude/src

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,18 @@ fn should_replace_deduped_entry(candidate: &UsageEntry, existing: &UsageEntry) -
298298
}
299299

300300
/// 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.
301+
/// that reports more of the request: the speed tier, with `Fast` above
302+
/// `Standard` because only `Fast` carries the fast-mode price multiplier, then
303+
/// the cache-write TTL split.
303304
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+
let detail = |usage: &TokenUsageRaw| {
306+
let speed_rank = match usage.speed {
307+
None => 0,
308+
Some(Speed::Standard) => 1,
309+
Some(Speed::Fast) => 2,
310+
};
311+
(speed_rank, usage.cache_creation.is_some())
312+
};
305313
detail(candidate) > detail(existing)
306314
}
307315

@@ -998,7 +1006,7 @@ mod tests {
9981006
push_deduped_entry, read_usage_file, usage_files,
9991007
};
10001008
use crate::{
1001-
LoadedEntry, ModelBreakdown, PricingMap, TimestampMs, TokenUsageRaw, UsageEntry,
1009+
LoadedEntry, ModelBreakdown, PricingMap, Speed, TimestampMs, TokenUsageRaw, UsageEntry,
10021010
UsageMessage,
10031011
cli::{CostMode, SharedArgs},
10041012
};
@@ -1391,6 +1399,42 @@ null,"usage":{"input_tokens":1}}}"#
13911399
}
13921400
}
13931401

1402+
#[test]
1403+
fn keeps_the_fast_tier_from_an_otherwise_equal_duplicate() {
1404+
for lines in [
1405+
[
1406+
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,"speed":"standard","cache_creation":{"ephemeral_5m_input_tokens":40,"ephemeral_1h_input_tokens":60}}}}"#,
1407+
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,"speed":"fast"}}}"#,
1408+
],
1409+
[
1410+
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,"speed":"fast"}}}"#,
1411+
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,"speed":"standard","cache_creation":{"ephemeral_5m_input_tokens":40,"ephemeral_1h_input_tokens":60}}}}"#,
1412+
],
1413+
] {
1414+
let fixture = fs_fixture!({
1415+
"projects/project-a/session-a.jsonl": lines.join("\n"),
1416+
});
1417+
let mut deduped_indexes = Default::default();
1418+
let mut deduped = Vec::new();
1419+
1420+
let loaded = read_usage_file(
1421+
&fixture.path("projects/project-a/session-a.jsonl"),
1422+
None,
1423+
CostMode::Display,
1424+
None,
1425+
);
1426+
for entry in loaded.entries {
1427+
push_deduped_entry(entry, &mut deduped_indexes, &mut deduped);
1428+
}
1429+
1430+
assert_eq!(deduped.len(), 1);
1431+
assert!(matches!(
1432+
deduped[0].data.message.usage.speed,
1433+
Some(Speed::Fast)
1434+
));
1435+
}
1436+
}
1437+
13941438
#[test]
13951439
fn dedupes_anthropic_message_id_from_same_session_at_distinct_timestamps() {
13961440
let fixture = fs_fixture!({

0 commit comments

Comments
 (0)