Skip to content

Commit dbac9e6

Browse files
committed
fix(codex): retain counted compaction IDs in replay history
Keep matched parent response IDs independently of usage emission, so a retained child log missing the intervening cumulative snapshot cannot count an already-accounted compaction again in a date-bounded report. Collect identities during the existing parent pass. Normal report parsing does not allocate the optional history map, and ordinary replay matching continues to use only token-count deltas. Add a regression covering the loader and serial/parallel reports with a missing child snapshot. This addresses Cubic's review finding on #1821.
1 parent ad2d201 commit dbac9e6

4 files changed

Lines changed: 109 additions & 16 deletions

File tree

‎rust/adapters/codex/src/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Automatic compaction usage is recorded separately:
2626
- Keep its request timestamp and recorded service tier. Missing model metadata uses the active `turn_context` and marks the attribution as a fallback.
2727
- Compaction usage does not advance the normal cumulative baseline or change the active turn model.
2828
- Deduplicate copied compactions by response ID, independently of normal token-count replay matching. Retain the first file's attribution even when files are parsed in parallel.
29-
- Preserve parent compaction IDs in the replay plan, so a date-bounded child report does not count a copied request when its parent lies outside the report range.
29+
- Preserve all matched parent compaction IDs in the replay plan, including usage already covered by a cumulative snapshot, so a date-bounded child report does not count a copied request when its parent lies outside the report range.
3030

3131
Relevant speed-setting event in Codex CLI 0.144.0 and later:
3232

‎rust/adapters/codex/src/compaction_tests.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,49 @@ fn bounded_reports_exclude_parent_compactions_but_keep_child_requests() {
132132
}
133133
}
134134

135+
#[test]
136+
fn bounded_reports_exclude_counted_parent_compactions_without_a_copied_snapshot() {
137+
let fixture = fs_fixture!({
138+
"2026/09/01/parent.jsonl": [
139+
r#"{"type":"session_meta","payload":{"id":"parent"}}"#,
140+
r#"{"type":"turn_context","payload":{"model":"gpt-5"}}"#,
141+
r#"{"timestamp":"2026-09-01T00:00:01Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":100,"output_tokens":20}}}}"#,
142+
r#"{"timestamp":"2026-09-01T00:00:02Z","type":"token_usage_record","payload":{"response_id":"response-1","usage":{"input_tokens":300,"output_tokens":20}}}"#,
143+
r#"{"timestamp":"2026-09-01T00:00:03Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":400,"output_tokens":40},"last_token_usage":{"input_tokens":300,"output_tokens":20}}}}"#,
144+
r#"{"type":"compacted","payload":{"compaction_response_id":"response-1"}}"#,
145+
].join("\n"),
146+
"2026/09/02/child.jsonl": [
147+
json!({"timestamp": "2026-09-02T00:00:00Z", "type": "session_meta", "payload": {"id": "child", "forked_from_id": "parent"}}).to_string(),
148+
token_count("2026-09-02T00:00:01Z", 100),
149+
compaction_log("2026-09-02T00:00:02Z", "gpt-5", 300),
150+
compaction_log("2026-09-02T00:01:00Z", "gpt-5", 50).replace("response-1", "child-response"),
151+
token_count("2026-09-02T00:02:00Z", 30),
152+
].join("\n"),
153+
});
154+
crate::paths::set_file_modified(
155+
&fixture.path("2026/09/01/parent.jsonl"),
156+
crate::parse_ts_timestamp("2026-09-01T00:03:00Z").unwrap(),
157+
);
158+
let _guard = ccusage_test_support::EnvVarGuard::set("CODEX_HOME", fixture.root());
159+
for single_thread in [true, false] {
160+
let shared = SharedArgs {
161+
since: Some("20260902".into()),
162+
timezone: Some("UTC".into()),
163+
single_thread,
164+
..SharedArgs::default()
165+
};
166+
let groups =
167+
load_groups_from_directory(fixture.root(), &shared, AgentReportKind::Daily).unwrap();
168+
assert_eq!(groups["2026-09-02"].total_tokens, 120);
169+
let (events, _) = crate::load_codex_events_with_detection(&shared).unwrap();
170+
assert_eq!(
171+
events.iter().map(|event| event.total_tokens).sum::<u64>(),
172+
120
173+
);
174+
assert_eq!(events[0].response_id.as_deref(), Some("child-response"));
175+
}
176+
}
177+
135178
#[test]
136179
fn all_report_modes_keep_the_first_compaction_copy_in_serial_and_parallel() {
137180
let fixture = fs_fixture!({

‎rust/adapters/codex/src/parser.rs‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct CodexCompactionUsageState {
7676
pending_usage: HashMap<String, CodexPendingUsage>,
7777
emitted_response_ids: HashSet<String>,
7878
latest_usage_response_id: Option<String>,
79+
recorded_usage_timestamps: Option<HashMap<String, Option<TimestampMs>>>,
7980
}
8081

8182
struct CodexPendingUsage {
@@ -184,6 +185,24 @@ pub(super) fn visit_codex_session_file(
184185
sessions_dir: &Path,
185186
path: &Path,
186187
replayed_prefix: Option<&[CodexRawUsage]>,
188+
visit: impl FnMut(CodexTokenUsageEvent) -> Result<()>,
189+
) -> Result<()> {
190+
visit_codex_session_file_with_compaction_history(
191+
sessions_dir,
192+
path,
193+
replayed_prefix,
194+
None,
195+
visit,
196+
)
197+
}
198+
199+
/// Visits usage while optionally retaining matched compaction identities,
200+
/// including requests already covered by cumulative usage in a parent log.
201+
pub(super) fn visit_codex_session_file_with_compaction_history(
202+
sessions_dir: &Path,
203+
path: &Path,
204+
replayed_prefix: Option<&[CodexRawUsage]>,
205+
compaction_history: Option<&mut HashMap<String, Option<TimestampMs>>>,
187206
mut visit: impl FnMut(CodexTokenUsageEvent) -> Result<()>,
188207
) -> Result<()> {
189208
let Ok(file) = fs::File::open(path) else {
@@ -193,6 +212,9 @@ pub(super) fn visit_codex_session_file(
193212
let mut line = Vec::new();
194213
let session_id = codex_session_id(sessions_dir, path);
195214
let mut state = CodexSessionUsageState::default();
215+
if compaction_history.is_some() {
216+
state.compaction_usage.recorded_usage_timestamps = Some(HashMap::new());
217+
}
196218
let fallback_timestamp = file_modified_timestamp(path);
197219
let mut replay = match replayed_prefix {
198220
Some(prefix) => CodexReplayState::MatchingParent { prefix, index: 0 },
@@ -247,7 +269,7 @@ pub(super) fn visit_codex_session_file(
247269
loop {
248270
line.clear();
249271
let Ok(bytes_read) = reader.read_until(b'\n', &mut line) else {
250-
return Ok(());
272+
break;
251273
};
252274
if bytes_read == 0 {
253275
break;
@@ -286,6 +308,20 @@ pub(super) fn visit_codex_session_file(
286308
}
287309
}
288310

311+
if let Some(history) = compaction_history
312+
&& let Some(mut timestamps) = state.compaction_usage.recorded_usage_timestamps
313+
{
314+
// Parent identity survives accounting suppression. A copied child can
315+
// retain a compaction pair while its cumulative snapshot is missing.
316+
timestamps.retain(|response_id, _| {
317+
state
318+
.compaction_usage
319+
.compacted_response_ids
320+
.contains(response_id)
321+
});
322+
history.extend(timestamps);
323+
}
324+
289325
Ok(())
290326
}
291327

@@ -357,6 +393,12 @@ fn visit_codex_session_entry(
357393
return Ok(());
358394
}
359395

396+
if let Some(timestamps) = compaction_usage.recorded_usage_timestamps.as_mut() {
397+
timestamps
398+
.entry(response_id.clone())
399+
.or_insert_with(|| parse_ts_timestamp(&timestamp));
400+
}
401+
360402
let parsed_model = codex_model_from_payload(payload);
361403
let model_was_inferred = parsed_model.is_none();
362404
// A request record can name a different model, or never match a

‎rust/adapters/codex/src/replay.rs‎

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use serde_json::Value;
1313

1414
use crate::{CodexRawUsage, TimestampMs, chunk_file_indexes_by_size, parse_ts_timestamp};
1515

16-
use super::parser::{codex_value_timestamp, visit_codex_session_file};
16+
use super::parser::{codex_value_timestamp, visit_codex_session_file_with_compaction_history};
1717

1818
#[cfg(test)]
1919
#[derive(Debug, Eq, PartialEq)]
@@ -357,19 +357,27 @@ fn read_usage_events(sessions_dir: &Path, path: &Path) -> ParentUsage {
357357
usage: Vec::new(),
358358
compactions: HashMap::new(),
359359
};
360-
let _ = visit_codex_session_file(sessions_dir, path, None, |event| {
361-
// Response IDs deduplicate compactions independently of this normal
362-
// token-count prefix, including when a fork omits compaction records.
363-
if let Some(response_id) = event.response_id {
364-
stream
365-
.compactions
366-
.insert(response_id, parse_ts_timestamp(&event.timestamp));
367-
return Ok(());
368-
}
369-
stream.timestamps.push(parse_ts_timestamp(&event.timestamp));
370-
stream.usage.push(event.raw_usage());
371-
Ok(())
372-
});
360+
let ParentUsage {
361+
timestamps,
362+
usage,
363+
compactions,
364+
} = &mut stream;
365+
let _ = visit_codex_session_file_with_compaction_history(
366+
sessions_dir,
367+
path,
368+
None,
369+
Some(compactions),
370+
|event| {
371+
// Response IDs deduplicate compactions independently of this normal
372+
// token-count prefix, including when a fork omits compaction records.
373+
if event.response_id.is_some() {
374+
return Ok(());
375+
}
376+
timestamps.push(parse_ts_timestamp(&event.timestamp));
377+
usage.push(event.raw_usage());
378+
Ok(())
379+
},
380+
);
373381
stream
374382
}
375383

0 commit comments

Comments
 (0)