Skip to content

Commit 8e826b4

Browse files
committed
fix(codex): count spawned subagent requests stamped at the fork instant
When a forked rollout's parent log is unavailable, the parser falls back to skipping the "rewritten burst": the run of usage events Codex writes back to back at the fork instant when it copies the parent's history. The burst only ends at a pause of more than a second, because the heuristic assumes the child's own turns follow the replay after a real delay. Some Codex VS Code rollouts (0.164.0-alpha.11.1) carry the session_meta timestamp on every line, including the child's own token_count events. With no pause anywhere, the burst runs to the end of the file and the whole rollout counts as zero, even though its token_usage_record lines have response IDs that exist in no other rollout. Timestamps cannot separate replay from the child's own usage in such a file, but the records can. Since TokenUsageRecord was introduced (openai/codex#41912), Codex drops the parent's records from the history it hands a spawned subagent ("child threads inherit model context, not the parent's cumulative usage state"). So in a session whose session_meta names a source.subagent.thread_spawn parent, a token_usage_record is a request the child sent itself, and nothing after it can be replayed history. The replay filter now ends at that record, whether it was matching the parent prefix or skipping a rewritten burst. User forks (thread/fork) copy every rollout item, records included, so for them a record proves nothing and the existing timestamp handling is unchanged. A regression test pins that double-count protection. To carry the spawn signal, the replay plan now hands the parser a CodexForkReplay (prefix plus whether records end the replay), and the replay state machine moves into a small filter so the main loop can observe records alongside usage events. Closes #2046
1 parent 3297d33 commit 8e826b4

5 files changed

Lines changed: 231 additions & 80 deletions

File tree

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ fn aggregate_file(
246246
parser::visit_codex_session_file(
247247
run.sessions_dir,
248248
file,
249-
run.replay_plan.replay_prefix(file),
249+
run.replay_plan.fork_replay(file),
250250
|event| {
251251
if run
252252
.replay_plan
@@ -296,7 +296,7 @@ fn aggregate_file_local(
296296
parser::visit_codex_session_file(
297297
run.sessions_dir,
298298
file,
299-
run.replay_plan.replay_prefix(file),
299+
run.replay_plan.fork_replay(file),
300300
|event| {
301301
if run
302302
.replay_plan

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::{
66
aggregate::{aggregate_events, load_groups_from_directory},
77
cli::{AgentReportKind, CodexSpeed, CostMode, SharedArgs},
88
load_codex_events_from_directory,
9-
parser::visit_codex_session_file,
9+
parser::{CodexForkReplay, visit_codex_session_file},
1010
report::report_from_groups,
1111
types::{
1212
CodexRawUsage,
@@ -53,7 +53,10 @@ fn compaction_identity_does_not_consume_the_normal_replay_prefix() {
5353
visit_codex_session_file(
5454
fixture.root(),
5555
&fixture.path("child.jsonl"),
56-
Some(&prefix),
56+
Some(CodexForkReplay {
57+
prefix: &prefix,
58+
ends_at_usage_record: false,
59+
}),
5760
|event| {
5861
events.push(event);
5962
Ok(())

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

Lines changed: 114 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,13 @@ fn read_codex_session_file(
222222
replay_plan: &CodexReplayPlan,
223223
) -> Vec<CodexTokenUsageEvent> {
224224
let mut events = Vec::new();
225-
let _ = visit_codex_session_file(
226-
sessions_dir,
227-
path,
228-
replay_plan.replay_prefix(path),
229-
|event| {
230-
if replay_plan.is_replayed_compaction(path, event.response_id.as_deref()) {
231-
return Ok(());
232-
}
233-
events.push(event);
234-
Ok(())
235-
},
236-
);
225+
let _ = visit_codex_session_file(sessions_dir, path, replay_plan.fork_replay(path), |event| {
226+
if replay_plan.is_replayed_compaction(path, event.response_id.as_deref()) {
227+
return Ok(());
228+
}
229+
events.push(event);
230+
Ok(())
231+
});
237232
events
238233
}
239234

@@ -288,7 +283,7 @@ mod tests {
288283
use super::*;
289284

290285
use ccusage_test_support::fs_fixture;
291-
use serde_json::json;
286+
use serde_json::{Value, json};
292287

293288
use crate::paths::CodexUsageSource;
294289

@@ -2242,6 +2237,112 @@ mod tests {
22422237
);
22432238
}
22442239

2240+
#[test]
2241+
fn counts_spawned_child_requests_stamped_with_the_fork_instant() {
2242+
let at_fork = "2026-11-01T09:42:09.923Z";
2243+
let usage = |input: u64, cached: u64, output: u64| {
2244+
json!({
2245+
"input_tokens": input,
2246+
"cached_input_tokens": cached,
2247+
"output_tokens": output,
2248+
"reasoning_output_tokens": 0,
2249+
"total_tokens": input + output,
2250+
})
2251+
};
2252+
let token_count = |last: Value, total: Value| {
2253+
json!({
2254+
"timestamp": at_fork,
2255+
"type": "event_msg",
2256+
"payload": {
2257+
"type": "token_count",
2258+
"info": {"last_token_usage": last, "total_token_usage": total},
2259+
},
2260+
})
2261+
.to_string()
2262+
};
2263+
let usage_record = |response_id: &str, usage: Value| {
2264+
json!({
2265+
"timestamp": at_fork,
2266+
"type": "token_usage_record",
2267+
"payload": {
2268+
"response_id": response_id,
2269+
"usage": usage,
2270+
},
2271+
})
2272+
.to_string()
2273+
};
2274+
let fixture = fs_fixture!({
2275+
// Every line carries the fork instant, so timestamps cannot separate
2276+
// the inherited baseline from the child's own requests.
2277+
"child.jsonl": [
2278+
json!({
2279+
"timestamp": at_fork,
2280+
"type": "session_meta",
2281+
"payload": {
2282+
"id": "child",
2283+
"forked_from_id": "missing-parent",
2284+
"source": {"subagent": {"thread_spawn": {"parent_thread_id": "missing-parent"}}},
2285+
},
2286+
})
2287+
.to_string(),
2288+
json!({"timestamp": at_fork, "type": "compacted", "payload": {"message": "", "replacement_history": []}}).to_string(),
2289+
json!({"timestamp": at_fork, "type": "turn_context", "payload": {"model": "gpt-5.2"}}).to_string(),
2290+
token_count(usage(990_000, 0, 10_000), usage(990_000, 0, 10_000)),
2291+
usage_record("resp_child_1", usage(10_000, 2_000, 100)),
2292+
token_count(
2293+
usage(10_000, 2_000, 100),
2294+
usage(1_000_000, 2_000, 10_100),
2295+
),
2296+
usage_record("resp_child_2", usage(20_000, 3_000, 200)),
2297+
token_count(
2298+
usage(20_000, 3_000, 200),
2299+
usage(1_020_000, 5_000, 10_300),
2300+
),
2301+
]
2302+
.join("\n"),
2303+
});
2304+
2305+
let events = load_codex_events_from_directory(fixture.root(), true).unwrap();
2306+
2307+
assert_eq!(
2308+
events
2309+
.iter()
2310+
.map(|event| (
2311+
event.input_tokens,
2312+
event.cached_input_tokens,
2313+
event.output_tokens
2314+
))
2315+
.collect::<Vec<_>>(),
2316+
[(10_000, 2_000, 100), (20_000, 3_000, 200)]
2317+
);
2318+
}
2319+
2320+
#[test]
2321+
fn skips_usage_records_copied_into_a_rewritten_user_fork_burst() {
2322+
let fixture = fs_fixture!({
2323+
// A user fork copies the parent's usage records along with the rest
2324+
// of its history, so a record does not end the replay here.
2325+
"child.jsonl": [
2326+
replay_metadata("2026-11-01T09:00:00.000Z", "child", Some("missing-parent")),
2327+
replay_token_count("2026-11-01T09:00:00.100Z", 100),
2328+
json!({
2329+
"timestamp": "2026-11-01T09:00:00.150Z",
2330+
"type": "token_usage_record",
2331+
"payload": {"response_id": "resp_parent", "usage": {"input_tokens": 200, "output_tokens": 1}},
2332+
})
2333+
.to_string(),
2334+
replay_token_count("2026-11-01T09:00:00.200Z", 200),
2335+
replay_token_count("2026-11-01T09:00:08.000Z", 500),
2336+
]
2337+
.join("\n"),
2338+
});
2339+
2340+
assert_eq!(
2341+
replay_input_tokens_by_session(fixture.root()),
2342+
[("child".to_string(), 500)]
2343+
);
2344+
}
2345+
22452346
#[test]
22462347
fn keeps_fork_local_usage_recorded_after_a_rewritten_burst() {
22472348
let fixture = fs_fixture!({

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

Lines changed: 86 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ struct CodexTokenCountUsage {
101101
delta: Option<CodexRawUsage>,
102102
}
103103

104+
/// History a forked session replayed from its parent.
105+
#[derive(Clone, Copy)]
106+
pub(super) struct CodexForkReplay<'a> {
107+
/// The parent's usage up to the fork; empty when the parent log is unavailable.
108+
pub(super) prefix: &'a [CodexRawUsage],
109+
/// Codex drops the parent's `token_usage_record` lines when it spawns a
110+
/// subagent, so every record in such a child is a request the child sent
111+
/// itself and nothing after it can be replayed history. A user fork copies
112+
/// them with the rest of the parent's history, so they prove nothing there.
113+
pub(super) ends_at_usage_record: bool,
114+
}
115+
104116
/// Tracks how far a forked session's leading events still match the history it
105117
/// replayed from its parent.
106118
enum CodexReplayState<'a> {
@@ -205,50 +217,24 @@ fn detect_rewritten_burst(path: &Path) -> Option<TimestampMs> {
205217
}
206218
}
207219

208-
/// Visits every usage event in a Codex session log.
209-
///
210-
/// `replayed_prefix` carries the usage a forked session copied from its parent so
211-
/// it is not counted twice: `None` for sessions that are not forks, and an empty
212-
/// slice for forks whose parent log is unavailable.
213-
pub(super) fn visit_codex_session_file(
214-
sessions_dir: &Path,
215-
path: &Path,
216-
replayed_prefix: Option<&[CodexRawUsage]>,
217-
visit: impl FnMut(CodexTokenUsageEvent) -> Result<()>,
218-
) -> Result<()> {
219-
visit_codex_session_file_with_compaction_history(
220-
sessions_dir,
221-
path,
222-
replayed_prefix,
223-
None,
224-
visit,
225-
)
220+
struct CodexReplayFilter<'a> {
221+
path: &'a Path,
222+
state: CodexReplayState<'a>,
223+
ends_at_usage_record: bool,
226224
}
227225

228-
/// Visits usage while optionally retaining matched compaction identities,
229-
/// including requests already covered by cumulative usage in a parent log.
230-
pub(super) fn visit_codex_session_file_with_compaction_history(
231-
sessions_dir: &Path,
232-
path: &Path,
233-
replayed_prefix: Option<&[CodexRawUsage]>,
234-
compaction_history: Option<&mut HashMap<String, Option<TimestampMs>>>,
235-
mut visit: impl FnMut(CodexTokenUsageEvent) -> Result<()>,
236-
) -> Result<()> {
237-
let Ok((mut reader, opened_path)) = open_rollout_reader(path) else {
238-
return Ok(());
239-
};
240-
let mut line = Vec::new();
241-
let session_id = codex_session_id(sessions_dir, path);
242-
let mut state = CodexSessionUsageState::default();
243-
if compaction_history.is_some() {
244-
state.compaction_usage.recorded_usage_timestamps = Some(HashMap::new());
226+
impl CodexReplayFilter<'_> {
227+
fn observe_usage_record(&mut self) {
228+
if self.ends_at_usage_record {
229+
self.state = CodexReplayState::Done;
230+
}
245231
}
246-
let fallback_timestamp = file_modified_timestamp(&opened_path);
247-
let mut replay = match replayed_prefix {
248-
Some(prefix) => CodexReplayState::MatchingParent { prefix, index: 0 },
249-
None => CodexReplayState::Done,
250-
};
251-
let mut visit_filtered = |event: CodexTokenUsageEvent| {
232+
233+
fn visit(
234+
&mut self,
235+
event: CodexTokenUsageEvent,
236+
visit: &mut impl FnMut(CodexTokenUsageEvent) -> Result<()>,
237+
) -> Result<()> {
252238
// Compaction requests have their own response identity. Comparing them
253239
// with normal request totals could consume an unrelated replay entry;
254240
// copied compactions are deduplicated by ID after parsing instead.
@@ -258,11 +244,11 @@ pub(super) fn visit_codex_session_file_with_compaction_history(
258244
// Each arm either returns or advances the state toward `Done`, so this
259245
// loop only re-runs to apply the event to the state it switched to.
260246
loop {
261-
match replay {
247+
match self.state {
262248
CodexReplayState::MatchingParent { prefix, index } => {
263249
let usage = event.raw_usage();
264250
if prefix.get(index) == Some(&usage) {
265-
replay = CodexReplayState::MatchingParent {
251+
self.state = CodexReplayState::MatchingParent {
266252
prefix,
267253
index: index + 1,
268254
};
@@ -271,8 +257,8 @@ pub(super) fn visit_codex_session_file_with_compaction_history(
271257
// Nothing matched, so the parent stream cannot anchor this
272258
// replay: the log is unavailable, or Codex rewrote the copied
273259
// history. Fall back to the rewritten burst instead.
274-
replay = (index == 0)
275-
.then(|| detect_rewritten_burst(path))
260+
self.state = (index == 0)
261+
.then(|| detect_rewritten_burst(self.path))
276262
.flatten()
277263
.map_or(
278264
CodexReplayState::Done,
@@ -284,14 +270,59 @@ pub(super) fn visit_codex_session_file_with_compaction_history(
284270
&& (0..=CODEX_REWRITTEN_BURST_PAUSE_MS)
285271
.contains(&(timestamp.as_millis() - previous.as_millis()))
286272
{
287-
replay = CodexReplayState::SkippingRewrittenBurst(timestamp);
273+
self.state = CodexReplayState::SkippingRewrittenBurst(timestamp);
288274
return Ok(());
289275
}
290-
replay = CodexReplayState::Done;
276+
self.state = CodexReplayState::Done;
291277
}
292278
CodexReplayState::Done => return visit(event),
293279
}
294280
}
281+
}
282+
}
283+
284+
/// Visits every usage event in a Codex session log.
285+
///
286+
/// `fork_replay` carries the usage a forked session copied from its parent so it
287+
/// is not counted twice, and is `None` for sessions that are not forks.
288+
pub(super) fn visit_codex_session_file(
289+
sessions_dir: &Path,
290+
path: &Path,
291+
fork_replay: Option<CodexForkReplay<'_>>,
292+
visit: impl FnMut(CodexTokenUsageEvent) -> Result<()>,
293+
) -> Result<()> {
294+
visit_codex_session_file_with_compaction_history(sessions_dir, path, fork_replay, None, visit)
295+
}
296+
297+
/// Visits usage while optionally retaining matched compaction identities,
298+
/// including requests already covered by cumulative usage in a parent log.
299+
pub(super) fn visit_codex_session_file_with_compaction_history(
300+
sessions_dir: &Path,
301+
path: &Path,
302+
fork_replay: Option<CodexForkReplay<'_>>,
303+
compaction_history: Option<&mut HashMap<String, Option<TimestampMs>>>,
304+
mut visit: impl FnMut(CodexTokenUsageEvent) -> Result<()>,
305+
) -> Result<()> {
306+
let Ok((mut reader, opened_path)) = open_rollout_reader(path) else {
307+
return Ok(());
308+
};
309+
let mut line = Vec::new();
310+
let session_id = codex_session_id(sessions_dir, path);
311+
let mut state = CodexSessionUsageState::default();
312+
if compaction_history.is_some() {
313+
state.compaction_usage.recorded_usage_timestamps = Some(HashMap::new());
314+
}
315+
let fallback_timestamp = file_modified_timestamp(&opened_path);
316+
let mut replay = CodexReplayFilter {
317+
path,
318+
state: fork_replay.map_or(CodexReplayState::Done, |fork_replay| {
319+
CodexReplayState::MatchingParent {
320+
prefix: fork_replay.prefix,
321+
index: 0,
322+
}
323+
}),
324+
ends_at_usage_record: fork_replay
325+
.is_some_and(|fork_replay| fork_replay.ends_at_usage_record),
295326
};
296327

297328
loop {
@@ -310,7 +341,12 @@ pub(super) fn visit_codex_session_file_with_compaction_history(
310341
let Ok(value) = serde_json::from_slice::<CodexSessionLogEntry<'_>>(&line) else {
311342
continue;
312343
};
313-
visit_codex_session_entry(&session_id, value, &mut state, &mut visit_filtered)?;
344+
if value.entry_type.as_deref() == Some("token_usage_record") {
345+
replay.observe_usage_record();
346+
}
347+
visit_codex_session_entry(&session_id, value, &mut state, &mut |event| {
348+
replay.visit(event, &mut visit)
349+
})?;
314350
}
315351
CodexLineKind::Headless => {
316352
if let Ok(value) = serde_json::from_slice::<CodexLogEntry<'_>>(&line) {
@@ -320,7 +356,7 @@ pub(super) fn visit_codex_session_file_with_compaction_history(
320356
&fallback_timestamp,
321357
&mut state.current_model,
322358
&mut state.current_model_is_fallback,
323-
&mut visit_filtered,
359+
&mut |event| replay.visit(event, &mut visit),
324360
)?;
325361
} else {
326362
add_codex_exec_event_from_value(
@@ -329,7 +365,7 @@ pub(super) fn visit_codex_session_file_with_compaction_history(
329365
&fallback_timestamp,
330366
&mut state.current_model,
331367
&mut state.current_model_is_fallback,
332-
&mut visit_filtered,
368+
&mut |event| replay.visit(event, &mut visit),
333369
)?;
334370
};
335371
}

0 commit comments

Comments
 (0)