Skip to content

Commit c7ffd05

Browse files
committed
fix(codex): preserve session report dedupe scope
Keep Codex rollout token dedupe across files for daily, weekly, and monthly reports while restoring session-scoped dedupe keys for session reports. The previous dedupe key intentionally removed session_id to collapse copied root/goal rollout token_count rows, but that same global key ran before session grouping and could drop a distinct session bucket when two session files had identical token usage rows. Session reports now include the session hash in the key, while aggregate period reports use a zero session component to retain cross-session dedupe. Adds a regression test that identical token usage rows in two session files remain visible as two session groups across both single-threaded and parallel aggregation.
1 parent d319394 commit c7ffd05

1 file changed

Lines changed: 68 additions & 4 deletions

File tree

‎rust/crates/ccusage/src/adapter/codex/aggregate.rs‎

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ use crate::{
1818

1919
use super::{parser, paths};
2020

21-
type CodexEventKey = (crate::TimestampMs, u64, usize, u64, u64, u64, u64, u64);
21+
type CodexEventKey = (
22+
u64,
23+
usize,
24+
crate::TimestampMs,
25+
u64,
26+
usize,
27+
u64,
28+
u64,
29+
u64,
30+
u64,
31+
u64,
32+
);
2233
type CodexDedupeShards = [Mutex<FxHashSet<CodexEventKey>>];
2334

2435
struct CodexAggregation {
@@ -226,7 +237,7 @@ fn add_event_to_groups(
226237
};
227238
let timestamp = parse_ts_timestamp(&event.timestamp)
228239
.ok_or_else(|| crate::cli_error(format!("Invalid Codex timestamp: {}", event.timestamp)))?;
229-
if !insert_event_key(event, timestamp, model, seen) {
240+
if !insert_event_key(event, timestamp, model, kind, seen) {
230241
return Ok(());
231242
}
232243
add_deduped_event_to_groups(event, model, timestamp, kind, timezone, shared, groups)
@@ -246,7 +257,7 @@ fn add_event_to_groups_local(
246257
.ok_or_else(|| crate::cli_error(format!("Invalid Codex timestamp: {}", event.timestamp)))?;
247258
if !aggregation
248259
.seen
249-
.insert(codex_event_key(event, timestamp, model))
260+
.insert(codex_event_key(event, timestamp, model, kind))
250261
{
251262
return Ok(());
252263
}
@@ -330,9 +341,10 @@ fn insert_event_key(
330341
event: &CodexTokenUsageEvent,
331342
timestamp: crate::TimestampMs,
332343
model: &str,
344+
kind: AgentReportKind,
333345
seen: &CodexDedupeShards,
334346
) -> bool {
335-
let key = codex_event_key(event, timestamp, model);
347+
let key = codex_event_key(event, timestamp, model, kind);
336348
let mut hasher = FxHasher::default();
337349
key.hash(&mut hasher);
338350
let shard_index = hasher.finish() as usize % seen.len();
@@ -343,8 +355,16 @@ fn codex_event_key(
343355
event: &CodexTokenUsageEvent,
344356
timestamp: crate::TimestampMs,
345357
model: &str,
358+
kind: AgentReportKind,
346359
) -> CodexEventKey {
360+
let (session_hash, session_len) = if kind == AgentReportKind::Session {
361+
(hash_text(&event.session_id), event.session_id.len())
362+
} else {
363+
(0, 0)
364+
};
347365
(
366+
session_hash,
367+
session_len,
348368
timestamp,
349369
hash_text(model),
350370
model.len(),
@@ -498,6 +518,50 @@ mod tests {
498518
}
499519
}
500520

521+
#[test]
522+
fn keeps_matching_token_usage_in_distinct_session_groups() {
523+
let usage_line = json!({
524+
"timestamp": "2026-05-29T08:01:00.000Z",
525+
"type": "event_msg",
526+
"payload": {
527+
"type": "token_count",
528+
"info": {
529+
"model": "gpt-5.2",
530+
"last_token_usage": {
531+
"input_tokens": 1_000,
532+
"cached_input_tokens": 100,
533+
"output_tokens": 200,
534+
"reasoning_output_tokens": 20,
535+
"total_tokens": 1_200,
536+
},
537+
},
538+
},
539+
})
540+
.to_string();
541+
let fixture = fs_fixture!({
542+
"sessions/root.jsonl": &usage_line,
543+
"sessions/goal.jsonl": &usage_line,
544+
});
545+
for single_thread in [true, false] {
546+
let shared = SharedArgs {
547+
single_thread,
548+
timezone: Some("UTC".to_string()),
549+
..SharedArgs::default()
550+
};
551+
552+
let groups = load_groups_from_directory(
553+
&fixture.path("sessions"),
554+
&shared,
555+
AgentReportKind::Session,
556+
)
557+
.unwrap();
558+
559+
assert_eq!(groups.len(), 2);
560+
assert_eq!(groups["root"].input_tokens, 1_000);
561+
assert_eq!(groups["goal"].input_tokens, 1_000);
562+
}
563+
}
564+
501565
#[test]
502566
fn aggregates_active_copy_when_archived_file_has_same_relative_path() {
503567
let active_usage = [

0 commit comments

Comments
 (0)