Skip to content

Commit 6278ec3

Browse files
committed
fix(codex): anchor replay dedupe per session file
The replay plan stored the missing-parent fallback prefix in `usage_by_parent` under the child's own path. When that file was also another session's parent, the insert replaced the parent's full usage stream with just its first replayed second, so the grandchild stopped matching after two events and counted the rest of the replayed history again. Reproduced with a missing grandparent, a parent, and a child: the child re-reported the parent's 300 and 400 token events. Keep the fallback out of `usage_by_parent` entirely. `ParentReplay::path` is now optional, and `replay_prefix` returns an empty slice for a fork whose parent log is unavailable, so no child path is ever used as a parent key. Move the same-second fallback into the parser as a second state of the replay match. Prefix matching that fails on the very first event means the parent stream cannot anchor the replay - the log is unavailable, or Codex rewrote the copied history - and before this change such files were left completely undeduped, which the same-second heuristic had handled. A replay that starts mid-parent-stream now falls back instead of double counting. Also: - Skip a parent that resolves to the child itself. A session listing itself in `forked_from_id` would match its own stream and lose every event it recorded. - Read session metadata on all workers instead of serially on the main thread. On a 1880 file history the plan phase drops from ~105ms to ~78ms warm, and cold runs no longer serialise one open per file. - Resolve duplicate session ids to the first file so the plan does not depend on source ordering. - Parse the fork timestamp from RFC3339 only. Codex writes `session_meta.timestamp` as a string, and the numeric branch guessed seconds against milliseconds with an unexplained threshold.
1 parent 021f663 commit 6278ec3

3 files changed

Lines changed: 285 additions & 113 deletions

File tree

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

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ fn read_codex_session_file(
137137
let _ = visit_codex_session_file(
138138
sessions_dir,
139139
path,
140-
replay_plan.parent_usage(path),
140+
replay_plan.replay_prefix(path),
141141
|event| {
142142
events.push(event);
143143
Ok(())
@@ -281,6 +281,129 @@ mod tests {
281281
assert_eq!(events[0].input_tokens, 50);
282282
}
283283

284+
fn replay_metadata(timestamp: &str, id: &str, parent: Option<&str>) -> String {
285+
json!({
286+
"timestamp": timestamp,
287+
"type": "session_meta",
288+
"payload": {"id": id, "forked_from_id": parent},
289+
})
290+
.to_string()
291+
}
292+
293+
fn replay_token_count(timestamp: &str, input_tokens: u64) -> String {
294+
json!({
295+
"timestamp": timestamp,
296+
"type": "event_msg",
297+
"payload": {
298+
"type": "token_count",
299+
"info": {
300+
"model": "gpt-5.2",
301+
"last_token_usage": {
302+
"input_tokens": input_tokens,
303+
"output_tokens": 1,
304+
"total_tokens": input_tokens + 1,
305+
},
306+
},
307+
},
308+
})
309+
.to_string()
310+
}
311+
312+
fn replay_input_tokens_by_session(dir: &Path) -> Vec<(String, u64)> {
313+
load_codex_events_from_directory(dir, true)
314+
.unwrap()
315+
.iter()
316+
.map(|event| (event.session_id.clone(), event.input_tokens))
317+
.collect()
318+
}
319+
320+
#[test]
321+
fn keeps_full_parent_stream_when_the_parent_itself_replayed_a_missing_session() {
322+
let fixture = fs_fixture!({
323+
// The grandparent log is gone, so this session falls back to skipping
324+
// its own rewritten second.
325+
"01-parent.jsonl": [
326+
replay_metadata("2026-07-10T08:00:00.000Z", "parent", Some("missing-grandparent")),
327+
replay_token_count("2026-07-10T08:00:00.100Z", 100),
328+
replay_token_count("2026-07-10T08:00:00.200Z", 200),
329+
replay_token_count("2026-07-10T08:01:00.000Z", 300),
330+
replay_token_count("2026-07-10T08:02:00.000Z", 400),
331+
]
332+
.join("\n"),
333+
"02-child.jsonl": [
334+
replay_metadata("2026-07-10T09:00:00.000Z", "child", Some("parent")),
335+
replay_token_count("2026-07-10T09:00:00.100Z", 100),
336+
replay_token_count("2026-07-10T09:00:00.200Z", 200),
337+
replay_token_count("2026-07-10T09:00:00.300Z", 300),
338+
replay_token_count("2026-07-10T09:00:00.400Z", 400),
339+
replay_token_count("2026-07-10T09:05:00.000Z", 500),
340+
]
341+
.join("\n"),
342+
});
343+
344+
assert_eq!(
345+
replay_input_tokens_by_session(fixture.root()),
346+
[
347+
("01-parent".to_string(), 300),
348+
("01-parent".to_string(), 400),
349+
("02-child".to_string(), 500),
350+
]
351+
);
352+
}
353+
354+
#[test]
355+
fn falls_back_to_rewritten_second_when_the_replay_starts_mid_parent_stream() {
356+
let fixture = fs_fixture!({
357+
"01-parent.jsonl": [
358+
replay_metadata("2026-07-10T08:00:00.000Z", "parent", None),
359+
replay_token_count("2026-07-10T08:01:00.000Z", 100),
360+
replay_token_count("2026-07-10T08:02:00.000Z", 200),
361+
replay_token_count("2026-07-10T08:03:00.000Z", 300),
362+
]
363+
.join("\n"),
364+
// Codex replayed a compacted history, so it does not line up with the
365+
// start of the parent stream.
366+
"02-child.jsonl": [
367+
replay_metadata("2026-07-10T09:00:00.000Z", "child", Some("parent")),
368+
replay_token_count("2026-07-10T09:00:00.100Z", 200),
369+
replay_token_count("2026-07-10T09:00:00.200Z", 300),
370+
replay_token_count("2026-07-10T09:05:00.000Z", 400),
371+
]
372+
.join("\n"),
373+
});
374+
375+
assert_eq!(
376+
replay_input_tokens_by_session(fixture.root()),
377+
[
378+
("01-parent".to_string(), 100),
379+
("01-parent".to_string(), 200),
380+
("01-parent".to_string(), 300),
381+
("02-child".to_string(), 400),
382+
]
383+
);
384+
}
385+
386+
#[test]
387+
fn keeps_usage_of_a_session_that_lists_itself_as_its_own_parent() {
388+
let fixture = fs_fixture!({
389+
"self.jsonl": [
390+
json!({
391+
"type": "session_meta",
392+
"payload": {"id": "self", "forked_from_id": "self"},
393+
})
394+
.to_string(),
395+
replay_token_count("2026-07-10T08:01:00.000Z", 100),
396+
replay_token_count("2026-07-10T08:02:00.000Z", 200),
397+
]
398+
.join("\n"),
399+
});
400+
401+
assert_eq!(
402+
replay_input_tokens_by_session(fixture.root()),
403+
[("self".to_string(), 100), ("self".to_string(), 200)]
404+
);
405+
}
406+
284407
#[test]
285408
fn skips_nested_replays_against_immutable_parent_streams() {
286409
fn metadata(id: &str, parent: Option<&str>) -> String {

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

Lines changed: 59 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,22 @@ struct CodexExecTimestamps {
5959
model: String,
6060
}
6161

62-
pub(super) fn detect_replay_second(path: &Path) -> Option<[u8; 19]> {
62+
/// Tracks how far a forked session's leading events still match the history it
63+
/// replayed from its parent.
64+
enum CodexReplayState<'a> {
65+
/// Comparing the child's leading usage against the parent's usage prefix.
66+
MatchingParent {
67+
prefix: &'a [CodexRawUsage],
68+
index: usize,
69+
},
70+
/// The parent stream could not anchor the replay, so skip the leading burst
71+
/// that Codex rewrote into a single second.
72+
SkippingSecond([u8; 19]),
73+
/// Past the replayed history: every remaining event is the child's own usage.
74+
Done,
75+
}
76+
77+
fn detect_replay_second(path: &Path) -> Option<[u8; 19]> {
6378
let Ok(file) = fs::File::open(path) else {
6479
return None;
6580
};
@@ -109,6 +124,11 @@ pub(super) fn detect_replay_second(path: &Path) -> Option<[u8; 19]> {
109124
}
110125
}
111126

127+
/// Visits every usage event in a Codex session log.
128+
///
129+
/// `replayed_prefix` carries the usage a forked session copied from its parent so
130+
/// it is not counted twice: `None` for sessions that are not forks, and an empty
131+
/// slice for forks whose parent log is unavailable.
112132
pub(super) fn visit_codex_session_file(
113133
sessions_dir: &Path,
114134
path: &Path,
@@ -125,24 +145,47 @@ pub(super) fn visit_codex_session_file(
125145
let mut current_model: Option<String> = None;
126146
let mut current_model_is_fallback = false;
127147
let fallback_timestamp = file_modified_timestamp(path);
128-
let mut replay_index = 0;
129-
let mut matching_replay = replayed_prefix.is_some();
148+
let mut replay = match replayed_prefix {
149+
Some(prefix) => CodexReplayState::MatchingParent { prefix, index: 0 },
150+
None => CodexReplayState::Done,
151+
};
130152
let mut visit_filtered = |event: CodexTokenUsageEvent| {
131-
if matching_replay {
132-
let usage = CodexRawUsage {
133-
input_tokens: event.input_tokens,
134-
cached_input_tokens: event.cached_input_tokens,
135-
output_tokens: event.output_tokens,
136-
reasoning_output_tokens: event.reasoning_output_tokens,
137-
total_tokens: event.total_tokens,
138-
};
139-
if replayed_prefix.and_then(|prefix| prefix.get(replay_index)) == Some(&usage) {
140-
replay_index += 1;
141-
return Ok(());
153+
// Each arm either returns or advances the state toward `Done`, so this
154+
// loop only re-runs to apply the event to the state it switched to.
155+
loop {
156+
match replay {
157+
CodexReplayState::MatchingParent { prefix, index } => {
158+
let usage = CodexRawUsage {
159+
input_tokens: event.input_tokens,
160+
cached_input_tokens: event.cached_input_tokens,
161+
output_tokens: event.output_tokens,
162+
reasoning_output_tokens: event.reasoning_output_tokens,
163+
total_tokens: event.total_tokens,
164+
};
165+
if prefix.get(index) == Some(&usage) {
166+
replay = CodexReplayState::MatchingParent {
167+
prefix,
168+
index: index + 1,
169+
};
170+
return Ok(());
171+
}
172+
// Nothing matched, so the parent stream cannot anchor this
173+
// replay: the log is unavailable, or Codex rewrote the copied
174+
// history. Fall back to the same-second burst instead.
175+
replay = (index == 0)
176+
.then(|| detect_replay_second(path))
177+
.flatten()
178+
.map_or(CodexReplayState::Done, CodexReplayState::SkippingSecond);
179+
}
180+
CodexReplayState::SkippingSecond(second) => {
181+
if event.timestamp.as_bytes().get(..19) == Some(second.as_slice()) {
182+
return Ok(());
183+
}
184+
replay = CodexReplayState::Done;
185+
}
186+
CodexReplayState::Done => return visit(event),
142187
}
143-
matching_replay = false;
144188
}
145-
visit(event)
146189
};
147190

148191
loop {

0 commit comments

Comments
 (0)