Skip to content

Commit 166a813

Browse files
committed
test(claude): cover growing requestless usage across days and sessions
Document that the msg_ prefix is a heuristic and keep the same-timestamp test on a gateway ID so it still exercises the timestamp-scoped path.
1 parent 659aa55 commit 166a813

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ fn usage_dedupe_hash(
367367

368368
/// Real Anthropic message IDs are unique per response, unlike the placeholder IDs gateways
369369
/// reuse for every response.
370+
///
371+
/// The prefix is a heuristic: only a request ID proves two lines belong to one response, so a
372+
/// gateway that reused a `msg_`-shaped placeholder would collapse to one entry per session.
370373
pub(crate) fn is_anthropic_message_id(message_id: &str) -> bool {
371374
message_id.starts_with("msg_")
372375
}

‎rust/crates/ccusage/src/main.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,8 +360,8 @@ mod tests {
360360
fn dedupes_repeated_requestless_writes_from_same_session_at_the_same_timestamp() {
361361
let fixture = fs_fixture!({
362362
"projects/project1/session1/chat.jsonl": [
363-
r#"{"timestamp":"2025-01-10T10:00:00.000Z","message":{"id":"msg_123","model":"claude-opus-4-6","usage":{"input_tokens":100,"output_tokens":25,"cache_creation_input_tokens":10,"cache_read_input_tokens":5}},"costUSD":0.001}"#,
364-
r#"{"timestamp":"2025-01-10T10:00:00.000Z","message":{"id":"msg_123","model":"claude-opus-4-6","usage":{"input_tokens":100,"output_tokens":250,"cache_creation_input_tokens":10,"cache_read_input_tokens":5,"speed":"standard"}},"costUSD":0.01}"#,
363+
r#"{"timestamp":"2025-01-10T10:00:00.000Z","message":{"id":"gateway_123","model":"claude-opus-4-6","usage":{"input_tokens":100,"output_tokens":25,"cache_creation_input_tokens":10,"cache_read_input_tokens":5}},"costUSD":0.001}"#,
364+
r#"{"timestamp":"2025-01-10T10:00:00.000Z","message":{"id":"gateway_123","model":"claude-opus-4-6","usage":{"input_tokens":100,"output_tokens":250,"cache_creation_input_tokens":10,"cache_read_input_tokens":5,"speed":"standard"}},"costUSD":0.01}"#,
365365
]
366366
.join("\n"),
367367
});

‎rust/crates/ccusage/tests/claude_cli.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,3 +713,29 @@ fn requestless_anthropic_message_id_content_blocks_are_counted_once_in_every_rep
713713
assert_eq!(total, 102, "claude {command}");
714714
}
715715
}
716+
717+
#[test]
718+
fn requestless_anthropic_message_id_keeps_largest_usage_across_days_and_sessions() {
719+
let fixture = Fixture::new();
720+
let line = |timestamp: &str, session: &str, output_tokens: u64| {
721+
format!(
722+
r#"{{"timestamp":"{timestamp}","sessionId":"{session}","message":{{"id":"msg_01AbC2DeF3","model":"claude-sonnet-4-20250514","usage":{{"input_tokens":100,"output_tokens":{output_tokens}}}}}}}"#
723+
)
724+
};
725+
// Content-block lines of one streamed response straddle midnight with growing output, so
726+
// every report must keep only the final, largest line rather than one line per day.
727+
let session_a = [
728+
line("2026-09-11T23:59:59.900Z", "session-a", 5),
729+
line("2026-09-12T00:00:00.100Z", "session-a", 50),
730+
];
731+
let _ = fixture.write_file("projects/project-a/session-a.jsonl", session_a.join("\n"));
732+
// The same message ID in another session is a separate copy and stays counted.
733+
let _ = fixture.write_file(
734+
"projects/project-a/session-b.jsonl",
735+
line("2026-09-12T01:00:00.000Z", "session-b", 5),
736+
);
737+
738+
for (command, total) in claude_report_totals(&fixture) {
739+
assert_eq!(total, 150 + 105, "claude {command}");
740+
}
741+
}

0 commit comments

Comments
 (0)