Skip to content

Commit e0697ce

Browse files
committed
fix(codex): skip forked session replay history
1 parent a98e374 commit e0697ce

2 files changed

Lines changed: 176 additions & 6 deletions

File tree

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

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -972,6 +972,174 @@ mod tests {
972972
}
973973
}
974974

975+
#[test]
976+
fn skips_replayed_parent_token_history_in_forked_session_files() {
977+
let fixture = fs_fixture!({
978+
"2026-05-12T08-00-00-parent.jsonl": [
979+
json!({
980+
"timestamp": "2026-05-12T08:00:00.000Z",
981+
"type": "turn_context",
982+
"payload": {"model": "gpt-5.2"},
983+
})
984+
.to_string(),
985+
json!({
986+
"timestamp": "2026-05-12T08:01:00.000Z",
987+
"type": "event_msg",
988+
"payload": {
989+
"type": "token_count",
990+
"info": {
991+
"last_token_usage": {
992+
"input_tokens": 1_000,
993+
"cached_input_tokens": 100,
994+
"output_tokens": 200,
995+
"total_tokens": 1_200,
996+
},
997+
"total_token_usage": {
998+
"input_tokens": 1_000,
999+
"cached_input_tokens": 100,
1000+
"output_tokens": 200,
1001+
"total_tokens": 1_200,
1002+
},
1003+
},
1004+
},
1005+
})
1006+
.to_string(),
1007+
json!({
1008+
"timestamp": "2026-05-12T08:02:00.000Z",
1009+
"type": "event_msg",
1010+
"payload": {
1011+
"type": "token_count",
1012+
"info": {
1013+
"last_token_usage": {
1014+
"input_tokens": 500,
1015+
"cached_input_tokens": 50,
1016+
"output_tokens": 100,
1017+
"total_tokens": 600,
1018+
},
1019+
"total_token_usage": {
1020+
"input_tokens": 1_500,
1021+
"cached_input_tokens": 150,
1022+
"output_tokens": 300,
1023+
"total_tokens": 1_800,
1024+
},
1025+
},
1026+
},
1027+
})
1028+
.to_string(),
1029+
]
1030+
.join("\n"),
1031+
"2026-05-12T08-03-00-fork.jsonl": [
1032+
json!({
1033+
"timestamp": "2026-05-12T08:03:00.000Z",
1034+
"type": "session_meta",
1035+
"payload": {
1036+
"id": "fork-abc",
1037+
"forked_from_id": "parent-xyz",
1038+
},
1039+
})
1040+
.to_string(),
1041+
json!({
1042+
"timestamp": "2026-05-12T08:03:00.000Z",
1043+
"type": "session_meta",
1044+
"payload": {"id": "parent-xyz"},
1045+
})
1046+
.to_string(),
1047+
// replayed parent history with timestamps rewritten to fork creation time
1048+
json!({
1049+
"timestamp": "2026-05-12T08:03:00.000Z",
1050+
"type": "event_msg",
1051+
"payload": {
1052+
"type": "token_count",
1053+
"info": {
1054+
"last_token_usage": {
1055+
"input_tokens": 1_000,
1056+
"cached_input_tokens": 100,
1057+
"output_tokens": 200,
1058+
"total_tokens": 1_200,
1059+
},
1060+
"total_token_usage": {
1061+
"input_tokens": 1_000,
1062+
"cached_input_tokens": 100,
1063+
"output_tokens": 200,
1064+
"total_tokens": 1_200,
1065+
},
1066+
},
1067+
},
1068+
})
1069+
.to_string(),
1070+
json!({
1071+
"timestamp": "2026-05-12T08:03:00.000Z",
1072+
"type": "event_msg",
1073+
"payload": {
1074+
"type": "token_count",
1075+
"info": {
1076+
"last_token_usage": {
1077+
"input_tokens": 500,
1078+
"cached_input_tokens": 50,
1079+
"output_tokens": 100,
1080+
"total_tokens": 600,
1081+
},
1082+
"total_token_usage": {
1083+
"input_tokens": 1_500,
1084+
"cached_input_tokens": 150,
1085+
"output_tokens": 300,
1086+
"total_tokens": 1_800,
1087+
},
1088+
},
1089+
},
1090+
})
1091+
.to_string(),
1092+
// fork's own entry
1093+
json!({
1094+
"timestamp": "2026-05-12T08:04:00.000Z",
1095+
"type": "event_msg",
1096+
"payload": {
1097+
"type": "token_count",
1098+
"info": {
1099+
"last_token_usage": {
1100+
"input_tokens": 100,
1101+
"cached_input_tokens": 10,
1102+
"output_tokens": 20,
1103+
"total_tokens": 120,
1104+
},
1105+
"total_token_usage": {
1106+
"input_tokens": 100,
1107+
"cached_input_tokens": 10,
1108+
"output_tokens": 20,
1109+
"total_tokens": 120,
1110+
},
1111+
"model": "gpt-5.2",
1112+
},
1113+
},
1114+
})
1115+
.to_string(),
1116+
]
1117+
.join("\n"),
1118+
});
1119+
1120+
for single_thread in [true, false] {
1121+
let events = load_codex_events_from_directory(fixture.root(), single_thread).unwrap();
1122+
1123+
assert_eq!(
1124+
events.len(),
1125+
3,
1126+
"expected 3 events (2 parent + 1 fork real), got {} with single_thread={}",
1127+
events.len(),
1128+
single_thread
1129+
);
1130+
1131+
let fork_events: Vec<_> = events
1132+
.iter()
1133+
.filter(|event| event.session_id.contains("fork"))
1134+
.collect();
1135+
assert_eq!(fork_events.len(), 1);
1136+
assert_eq!(fork_events[0].input_tokens, 100);
1137+
assert_eq!(fork_events[0].cached_input_tokens, 10);
1138+
assert_eq!(fork_events[0].output_tokens, 20);
1139+
assert_eq!(fork_events[0].total_tokens, 120);
1140+
}
1141+
}
1142+
9751143
#[test]
9761144
fn keeps_cumulative_baseline_when_skipping_subagent_replay() {
9771145
let fixture = fs_fixture!({

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ static PROMPT_TOKENS_FIELD_FINDER: LazyLock<Finder<'static>> =
3434
LazyLock::new(|| Finder::new(br#""prompt_tokens":"#));
3535
static THREAD_SPAWN_FINDER: LazyLock<Finder<'static>> =
3636
LazyLock::new(|| Finder::new(b"thread_spawn"));
37+
static FORKED_FROM_ID_FINDER: LazyLock<Finder<'static>> =
38+
LazyLock::new(|| Finder::new(b"forked_from_id"));
3739

3840
const CODEX_AUTO_REVIEW_MODEL: &str = "codex-auto-review";
3941
const CODEX_AUTO_REVIEW_FALLBACKS_JSON: &str = include_str!("codex-auto-review-fallbacks.json");
@@ -62,18 +64,18 @@ struct CodexExecTimestamps {
6264
model: String,
6365
}
6466

65-
fn is_codex_subagent_session(path: &Path) -> bool {
67+
fn is_codex_replay_session(path: &Path) -> bool {
6668
let Ok(mut file) = fs::File::open(path) else {
6769
return false;
6870
};
6971
let mut buf = [0u8; 16 * 1024];
7072
let Ok(n) = file.read(&mut buf) else {
7173
return false;
7274
};
73-
THREAD_SPAWN_FINDER.find(&buf[..n]).is_some()
75+
THREAD_SPAWN_FINDER.find(&buf[..n]).is_some() || FORKED_FROM_ID_FINDER.find(&buf[..n]).is_some()
7476
}
7577

76-
fn detect_subagent_replay_second(path: &Path) -> Option<[u8; 19]> {
78+
fn detect_replay_second(path: &Path) -> Option<[u8; 19]> {
7779
let Ok(file) = fs::File::open(path) else {
7880
return None;
7981
};
@@ -138,9 +140,9 @@ pub(super) fn visit_codex_session_file(
138140
path: &Path,
139141
mut visit: impl FnMut(CodexTokenUsageEvent) -> Result<()>,
140142
) -> Result<()> {
141-
let is_subagent = is_codex_subagent_session(path);
142-
let replay_second = is_subagent
143-
.then(|| detect_subagent_replay_second(path))
143+
let is_replay_session = is_codex_replay_session(path);
144+
let replay_second = is_replay_session
145+
.then(|| detect_replay_second(path))
144146
.flatten();
145147
let Ok(file) = fs::File::open(path) else {
146148
return Ok(());

0 commit comments

Comments
 (0)