Skip to content

Commit 2676671

Browse files
committed
fix(codex): keep sessions distinct in grouped dedupe
Include the Codex session id hash and length in the grouped aggregation dedupe key so events with matching timestamp, model, and token counts from different sessions are not collapsed. This fixes the issue identified by cubic after the all-agent fast path started reusing grouped Codex aggregation. Added a regression test covering matching usage events from distinct session files. Validation: cargo fmt --manifest-path rust/Cargo.toml --all --check; cargo test --manifest-path rust/Cargo.toml -p ccusage; cargo clippy --manifest-path rust/Cargo.toml --workspace --all-targets -- -D warnings; fixture JSON parity; synthetic 50 MB Codex benchmark.
1 parent 45d122b commit 2676671

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

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

Lines changed: 17 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 {
@@ -339,8 +350,10 @@ fn codex_event_key(
339350
model: &str,
340351
) -> CodexEventKey {
341352
(
353+
hash_text(&event.session_id),
354+
event.session_id.len(),
342355
timestamp,
343-
hash_model_name(model),
356+
hash_text(model),
344357
model.len(),
345358
event.input_tokens,
346359
event.cached_input_tokens,
@@ -350,9 +363,9 @@ fn codex_event_key(
350363
)
351364
}
352365

353-
fn hash_model_name(model: &str) -> u64 {
366+
fn hash_text(value: &str) -> u64 {
354367
let mut hasher = FxHasher::default();
355-
model.hash(&mut hasher);
368+
value.hash(&mut hasher);
356369
hasher.finish()
357370
}
358371

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

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,35 @@ mod tests {
101101
assert_eq!(group.total_tokens, 280);
102102
}
103103

104+
#[test]
105+
fn keeps_matching_grouped_codex_usage_events_from_distinct_sessions() {
106+
let suffix = SystemTime::now()
107+
.duration_since(UNIX_EPOCH)
108+
.unwrap()
109+
.as_nanos();
110+
let root = std::env::temp_dir().join(format!("ccusage-codex-group-dedupe-{suffix}"));
111+
let sessions_dir = root.join("sessions");
112+
fs::create_dir_all(&sessions_dir).unwrap();
113+
let usage_line = r#"{"timestamp":"2026-01-02T00:00:00.000Z","type":"event_msg","payload":{"type":"token_count","info":{"model":"gpt-5","last_token_usage":{"input_tokens":100,"cached_input_tokens":10,"output_tokens":50,"reasoning_output_tokens":0,"total_tokens":150}}}}"#;
114+
fs::write(sessions_dir.join("session-a.jsonl"), usage_line).unwrap();
115+
fs::write(sessions_dir.join("session-b.jsonl"), usage_line).unwrap();
116+
let shared = SharedArgs {
117+
timezone: Some("UTC".to_string()),
118+
..SharedArgs::default()
119+
};
120+
121+
let groups =
122+
load_groups_from_directory(&sessions_dir, &shared, AgentReportKind::Daily).unwrap();
123+
fs::remove_dir_all(root).unwrap();
124+
125+
assert_eq!(groups.len(), 1);
126+
let group = groups.get("2026-01-02").unwrap();
127+
assert_eq!(group.input_tokens, 200);
128+
assert_eq!(group.cached_input_tokens, 20);
129+
assert_eq!(group.output_tokens, 100);
130+
assert_eq!(group.total_tokens, 300);
131+
}
132+
104133
#[test]
105134
fn reports_non_cached_codex_input_separately_from_cached_input() {
106135
let pricing = PricingMap::default();

0 commit comments

Comments
 (0)