Skip to content

Commit e4f0ea9

Browse files
authored
fix(codex): count spawned subagent requests stamped at the fork instant (ccusage#2051)
* 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 ccusage#2046 * fix(codex): keep identical requests from one rollout distinct Codex deduplicates usage that one rollout copied from another, because replayed parent history repeats the parent's token_count events. Without a response ID to compare, both dedupe paths (the event loader and the report aggregation) keyed a usage event on its timestamp, model, and token counts alone. The key carried no record of which file the event came from, so two requests in the same rollout with identical usage at the same timestamp were treated as a copy and merged. That is exactly the shape from ccusage#2046: a spawned subagent whose every line carries the fork instant. Its two identical requests were counted once, reporting 8,000 non-cached input where 16,000 was spent. A rollout records each request once, so identical events within one file are distinct requests, while a copy is the same request seen in another file. Each dedupe path now numbers the identical usage events of a file in order and puts that ordinal in the key. A copy repeats the original's ordinals and still collapses onto it, so copied history is counted once, and a parent's repeated identical requests stay distinct when copied together. Response-ID keys are unchanged. In the report path the ordinal also keeps recorded service tiers per request, because each distinct request now owns its own dedupe record. The loader keeps events grouped by file until dedupe, so it has the same provenance as the aggregation. Measured with release builds over 2.0G of real Codex logs: daily runs in 3.58s against 3.88s on main (within noise). Totals rise by 29.7M tokens in 14 sessions, none fall. All 14 are spawned subagents with one timestamp throughout, and none of their response IDs appears in any of the other 3,980 rollouts, so the added usage is the child's own. Refs ccusage#2046 * test(codex): keep a spawned child's own usage before its first record A spawned subagent never inherits the parent's token_usage_record lines, so a record proves that everything after it is the child's own usage. It does not prove the converse. Codex releases before openai/codex#41912 wrote no records, and a child created by one of them and resumed by a later release holds its own record-less turns ahead of the first record. A real rollout has this shape: a thread_spawn child created by Codex 0.149 that spent 2.8M tokens over several minutes, then was resumed two days later by a release that writes records. Skipping everything before the first record would drop all of that usage, so a record may end a replay skip but never start one. This test pins that boundary. * docs(codex): document fork replay boundaries and copy dedupe The adapter README listed only the MultiAgent V2 markers and said replay boundaries never come from session_meta. Spawned subagents now end a replay skip at their first token_usage_record, which depends on knowing from session_meta that the rollout came from thread_spawn. That rule targets cli_version, which resume re-records. The fork source identifies how the rollout was created and does not change on resume, so the README now says the version rule covers version fields only. It also describes each fork boundary, why a record never starts a skip, and how usage dedupe keys on an event's ordinal within its own file. * test(codex): cover the ccusage#2046 rollout in every Codex report mode The adapter tests drive the event loader and the session aggregation, but each Codex report mode groups usage on its own path, and the copy dedupe runs inside that aggregation. A regression in how one mode keys or groups the two identical requests would pass the adapter tests. Run the issue's exact rollout through the CLI for daily, monthly, and session (Codex has no weekly report). Assert JSON rows and totals at 16,000 input, 4,000 cache read, and 200 output, and snapshot each table. * test(codex): keep the post-record event inside the burst window The resumed spawned-child test pins that a token_usage_record never starts a replay skip. Its post-record token_count sat 1.86s after the record, outside the 1s rewritten-burst window. A broken rule that started a burst skip at the record would therefore still count that event, and the test would pass. Move the event to 800ms after the record, so it falls inside that window. Checked against two deliberately broken parsers: one starting a burst skip at the record and one skipping all usage before the first record. The test now fails under both; with the original timestamp it passed under the first. Reported by Pullfrog on ccusage#2051.
1 parent 908daef commit e4f0ea9

11 files changed

Lines changed: 640 additions & 164 deletions

File tree

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,14 @@ Relevant MultiAgent V2 subagent replay markers:
5353
- `type === "event_msg"` with `payload.type === "task_started"` ends the replayed prefix.
5454
- `type === "inter_agent_communication_metadata"` with `payload.trigger_turn === true` starts the child turn. The event was named `inter_agent_communication` before Codex CLI 0.143.0-alpha.15; both spellings are matched. `trigger_turn` is also emitted as `false`, so the value must be checked.
5555
- Usage between those two markers belongs to the child; usage before them is inherited history.
56-
- Which replay boundary a rollout uses is decided by scanning it for the marker, never from `session_meta`. Resuming a session re-records the original session's `cli_version`, so that field cannot be used to gate behavior.
56+
- Which replay boundary a rollout uses is decided by scanning it for the marker, never from `session_meta` version fields. Resuming a session re-records the original session's `cli_version`, so that field cannot be used to gate behavior.
57+
58+
Forked rollouts without those markers:
59+
60+
- `session_meta.payload.forked_from_id` or `payload.source.subagent.thread_spawn.parent_thread_id` names the parent. This identifies how the rollout was forked, which resuming does not change, unlike `cli_version`.
61+
- A spawned subagent (`thread_spawn`) never inherits the parent's `token_usage_record` lines, so a record ends any skip of replayed history: everything from it on is the child's own, even when every line carries the fork timestamp. A record never starts a skip. A child created by a Codex release without records and resumed by a later one holds its own record-less turns before the first record.
62+
- A user fork (`forked_from_id` only) copies the parent's records, so they mark nothing there. Its replayed prefix is matched against the parent's usage stream, or, when the parent log is unavailable, skipped as the dense burst Codex writes at the fork instant.
63+
- Usage without a response ID is deduplicated across files on timestamp, model, token counts, and its ordinal among identical events in its own file. A copy repeats the original's ordinals, while identical requests within one rollout stay distinct.
5764

5865
Token mapping:
5966

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

Lines changed: 197 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,62 @@ struct CodexUsageKey {
3939
output_tokens: u64,
4040
reasoning_output_tokens: u64,
4141
total_tokens: u64,
42+
occurrence: u32,
43+
}
44+
45+
/// Numbers the identical usage events of one rollout file.
46+
///
47+
/// A rollout records each request once, so identical events in one file are
48+
/// distinct requests and get distinct ordinals. Usage copied into another file
49+
/// repeats the ordinals of the original, which is what lets the dedupe drop it.
50+
pub(super) struct CodexFileOccurrences<K>(FxHashMap<K, u32>);
51+
52+
impl<K> Default for CodexFileOccurrences<K> {
53+
fn default() -> Self {
54+
Self(FxHashMap::default())
55+
}
56+
}
57+
58+
impl<K: Hash + Eq + Clone> CodexFileOccurrences<K> {
59+
pub(super) fn next(&mut self, key: &K) -> u32 {
60+
if let Some(count) = self.0.get_mut(key) {
61+
let occurrence = *count;
62+
*count += 1;
63+
return occurrence;
64+
}
65+
self.0.insert(key.clone(), 1);
66+
0
67+
}
68+
}
69+
70+
/// Where an event was read from: the file's position in the run, and the
71+
/// ordinals of the identical usage events seen in it so far.
72+
struct CodexEventProvenance {
73+
file_index: usize,
74+
occurrences: CodexFileOccurrences<CodexUsageKey>,
75+
}
76+
77+
impl CodexEventProvenance {
78+
fn new(file_index: usize) -> Self {
79+
Self {
80+
file_index,
81+
occurrences: CodexFileOccurrences::default(),
82+
}
83+
}
84+
85+
fn event_key(
86+
&mut self,
87+
event: &CodexTokenUsageEvent,
88+
timestamp: crate::TimestampMs,
89+
model: &str,
90+
kind: AgentReportKind,
91+
) -> CodexEventKey {
92+
let mut key = codex_event_key(event, timestamp, model, kind);
93+
if let CodexEventKey::Usage(usage) = &mut key {
94+
usage.occurrence = self.occurrences.next(usage);
95+
}
96+
key
97+
}
4298
}
4399

44100
struct CodexDedupeRecord {
@@ -243,10 +299,11 @@ fn aggregate_file(
243299
groups: &mut BTreeMap<String, CodexGroup>,
244300
) -> Result<()> {
245301
let file = &run.files[file_index];
302+
let mut provenance = CodexEventProvenance::new(run.file_offset + file_index);
246303
parser::visit_codex_session_file(
247304
run.sessions_dir,
248305
file,
249-
run.replay_plan.replay_prefix(file),
306+
run.replay_plan.fork_replay(file),
250307
|event| {
251308
if run
252309
.replay_plan
@@ -261,7 +318,7 @@ fn aggregate_file(
261318
run.shared,
262319
seen,
263320
groups,
264-
run.file_offset + file_index,
321+
&mut provenance,
265322
)
266323
},
267324
)
@@ -293,10 +350,11 @@ fn aggregate_file_local(
293350
aggregation: &mut CodexAggregation,
294351
) -> Result<()> {
295352
let file = &run.files[file_index];
353+
let mut provenance = CodexEventProvenance::new(run.file_offset + file_index);
296354
parser::visit_codex_session_file(
297355
run.sessions_dir,
298356
file,
299-
run.replay_plan.replay_prefix(file),
357+
run.replay_plan.fork_replay(file),
300358
|event| {
301359
if run
302360
.replay_plan
@@ -310,7 +368,7 @@ fn aggregate_file_local(
310368
timezone,
311369
run.shared,
312370
aggregation,
313-
run.file_offset + file_index,
371+
&mut provenance,
314372
)
315373
},
316374
)
@@ -323,15 +381,15 @@ fn add_event_to_groups(
323381
shared: &SharedArgs,
324382
seen: &CodexDedupeShards,
325383
groups: &mut BTreeMap<String, CodexGroup>,
326-
file_index: usize,
384+
provenance: &mut CodexEventProvenance,
327385
) -> Result<()> {
328386
let Some(model) = event.model.as_deref().filter(|model| !model.is_empty()) else {
329387
return Ok(());
330388
};
331389
let model = crate::model_aliases::resolve_model_name(model);
332390
let timestamp = parse_ts_timestamp(&event.timestamp)
333391
.ok_or_else(|| crate::cli_error(format!("Invalid Codex timestamp: {}", event.timestamp)))?;
334-
if !insert_event_key(event, timestamp, model.as_ref(), kind, seen, file_index) {
392+
if !insert_event_key(event, timestamp, model.as_ref(), kind, seen, provenance) {
335393
return Ok(());
336394
}
337395
add_deduped_event_to_groups(
@@ -351,23 +409,23 @@ fn add_event_to_groups_local(
351409
timezone: Option<&JiffTimeZone>,
352410
shared: &SharedArgs,
353411
aggregation: &mut CodexAggregation,
354-
file_index: usize,
412+
provenance: &mut CodexEventProvenance,
355413
) -> Result<()> {
356414
let Some(model) = event.model.as_deref().filter(|model| !model.is_empty()) else {
357415
return Ok(());
358416
};
359417
let model = crate::model_aliases::resolve_model_name(model);
360418
let timestamp = parse_ts_timestamp(&event.timestamp)
361419
.ok_or_else(|| crate::cli_error(format!("Invalid Codex timestamp: {}", event.timestamp)))?;
362-
let key = codex_event_key(event, timestamp, model.as_ref(), kind);
420+
let key = provenance.event_key(event, timestamp, model.as_ref(), kind);
363421
if !insert_dedupe_record(
364422
&mut aggregation.seen,
365423
key,
366424
event,
367425
model.as_ref(),
368426
kind,
369427
timestamp,
370-
file_index,
428+
provenance.file_index,
371429
) {
372430
return Ok(());
373431
}
@@ -697,9 +755,9 @@ fn insert_event_key(
697755
model: &str,
698756
kind: AgentReportKind,
699757
seen: &CodexDedupeShards,
700-
file_index: usize,
758+
provenance: &mut CodexEventProvenance,
701759
) -> bool {
702-
let key = codex_event_key(event, timestamp, model, kind);
760+
let key = provenance.event_key(event, timestamp, model, kind);
703761
let mut hasher = FxHasher::default();
704762
key.hash(&mut hasher);
705763
let shard_index = hasher.finish() as usize % seen.len();
@@ -710,7 +768,7 @@ fn insert_event_key(
710768
model,
711769
kind,
712770
timestamp,
713-
file_index,
771+
provenance.file_index,
714772
)
715773
}
716774

@@ -782,6 +840,7 @@ fn codex_event_key(
782840
output_tokens: event.output_tokens,
783841
reasoning_output_tokens: event.reasoning_output_tokens,
784842
total_tokens: event.total_tokens,
843+
occurrence: 0,
785844
})
786845
}
787846

@@ -986,7 +1045,7 @@ mod tests {
9861045
&shared,
9871046
&seen,
9881047
&mut groups,
989-
file_index,
1048+
&mut CodexEventProvenance::new(file_index),
9901049
)
9911050
.unwrap();
9921051
}
@@ -1864,4 +1923,129 @@ mod tests {
18641923
assert_eq!(groups["2026-05-13"].input_tokens, 222);
18651924
}
18661925
}
1926+
1927+
/// The rollout from #2046: a spawned subagent whose every line carries the
1928+
/// fork instant, with two requests of identical usage.
1929+
const IDENTICAL_REQUESTS_ROLLOUT: &str = r#"{"timestamp":"2026-11-01T09:42:09.923Z","type":"session_meta","payload":{"id":"01a1967c-0000-7000-8000-00000000000a","forked_from_id":"01a1967b-0000-7000-8000-000000000001","timestamp":"2026-11-01T09:42:09.923Z","originator":"codex_vscode","cli_version":"0.164.0-alpha.11.1","source":{"subagent":{"thread_spawn":{"parent_thread_id":"01a1967b-0000-7000-8000-000000000001","depth":1}}},"model_provider":"openai"}}
1930+
{"timestamp":"2026-11-01T09:42:09.923Z","type":"compacted","payload":{"message":"","replacement_history":[]}}
1931+
{"timestamp":"2026-11-01T09:42:09.923Z","type":"turn_context","payload":{"model":"gpt-6-astra"}}
1932+
{"timestamp":"2026-11-01T09:42:09.923Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":990000,"cached_input_tokens":0,"output_tokens":10000,"reasoning_output_tokens":0,"total_tokens":1000000},"last_token_usage":{"input_tokens":990000,"cached_input_tokens":0,"output_tokens":10000,"reasoning_output_tokens":0,"total_tokens":1000000}}}}
1933+
{"timestamp":"2026-11-01T09:42:09.923Z","type":"token_usage_record","payload":{"response_id":"resp_child_1","usage":{"input_tokens":10000,"cached_input_tokens":2000,"output_tokens":100,"reasoning_output_tokens":0,"total_tokens":10100}}}
1934+
{"timestamp":"2026-11-01T09:42:09.923Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":1000000,"cached_input_tokens":2000,"output_tokens":10100,"reasoning_output_tokens":0,"total_tokens":1010100},"last_token_usage":{"input_tokens":10000,"cached_input_tokens":2000,"output_tokens":100,"reasoning_output_tokens":0,"total_tokens":10100}}}}
1935+
{"timestamp":"2026-11-01T09:42:09.923Z","type":"token_usage_record","payload":{"response_id":"resp_child_2","usage":{"input_tokens":10000,"cached_input_tokens":2000,"output_tokens":100,"reasoning_output_tokens":0,"total_tokens":10100}}}
1936+
{"timestamp":"2026-11-01T09:42:09.923Z","type":"event_msg","payload":{"type":"token_count","info":{"total_token_usage":{"input_tokens":1010000,"cached_input_tokens":4000,"output_tokens":10200,"reasoning_output_tokens":0,"total_tokens":1020200},"last_token_usage":{"input_tokens":10000,"cached_input_tokens":2000,"output_tokens":100,"reasoning_output_tokens":0,"total_tokens":10100}}}}"#;
1937+
1938+
fn identical_request_line() -> String {
1939+
json!({
1940+
"timestamp": "2026-11-01T09:00:00.000Z",
1941+
"type": "event_msg",
1942+
"payload": {
1943+
"type": "token_count",
1944+
"info": {
1945+
"model": "gpt-5.2",
1946+
"last_token_usage": {
1947+
"input_tokens": 1_000,
1948+
"cached_input_tokens": 100,
1949+
"output_tokens": 10,
1950+
"total_tokens": 1_010,
1951+
},
1952+
},
1953+
},
1954+
})
1955+
.to_string()
1956+
}
1957+
1958+
fn daily_input_tokens(sessions_dir: &Path, single_thread: bool) -> Vec<(String, u64)> {
1959+
let shared = SharedArgs {
1960+
single_thread,
1961+
timezone: Some("UTC".to_string()),
1962+
..SharedArgs::default()
1963+
};
1964+
load_groups_from_directory(sessions_dir, &shared, AgentReportKind::Daily)
1965+
.unwrap()
1966+
.into_iter()
1967+
.map(|(period, group)| (period, group.input_tokens))
1968+
.collect()
1969+
}
1970+
1971+
#[test]
1972+
fn counts_identical_requests_within_one_rollout() {
1973+
let fixture = fs_fixture!({
1974+
"sessions/2026/11/01/rollout-2026-11-01T09-42-09-01a1967c-0000-7000-8000-00000000000a.jsonl":
1975+
IDENTICAL_REQUESTS_ROLLOUT,
1976+
});
1977+
let sessions = fixture.path("sessions");
1978+
1979+
for single_thread in [true, false] {
1980+
let events = crate::load_codex_events_from_directory(&sessions, single_thread)
1981+
.unwrap()
1982+
.into_iter()
1983+
.map(|event| {
1984+
(
1985+
event.input_tokens,
1986+
event.cached_input_tokens,
1987+
event.output_tokens,
1988+
)
1989+
})
1990+
.collect::<Vec<_>>();
1991+
assert_eq!(
1992+
events,
1993+
[(10_000, 2_000, 100), (10_000, 2_000, 100)],
1994+
"single_thread={single_thread}"
1995+
);
1996+
1997+
let shared = SharedArgs {
1998+
single_thread,
1999+
timezone: Some("UTC".to_string()),
2000+
..SharedArgs::default()
2001+
};
2002+
let groups =
2003+
load_groups_from_directory(&sessions, &shared, AgentReportKind::Session).unwrap();
2004+
let totals = groups
2005+
.values()
2006+
.map(|group| {
2007+
(
2008+
group.input_tokens - group.cached_input_tokens,
2009+
group.cached_input_tokens,
2010+
group.output_tokens,
2011+
)
2012+
})
2013+
.collect::<Vec<_>>();
2014+
assert_eq!(
2015+
totals,
2016+
[(16_000, 4_000, 200)],
2017+
"single_thread={single_thread}"
2018+
);
2019+
}
2020+
}
2021+
2022+
#[test]
2023+
fn counts_each_copied_request_once_across_rollouts() {
2024+
let request = identical_request_line();
2025+
let both_requests = format!("{request}\n{request}");
2026+
let scenarios = [
2027+
("copy of both requests", both_requests.clone()),
2028+
("copy of the first request", request.clone()),
2029+
];
2030+
2031+
for (name, copy) in scenarios {
2032+
let fixture = fs_fixture!({
2033+
"sessions/a-original.jsonl": &both_requests,
2034+
"sessions/b-copy.jsonl": &copy,
2035+
});
2036+
let sessions = fixture.path("sessions");
2037+
2038+
for single_thread in [true, false] {
2039+
let events =
2040+
crate::load_codex_events_from_directory(&sessions, single_thread).unwrap();
2041+
assert_eq!(events.len(), 2, "{name}, single_thread={single_thread}");
2042+
2043+
assert_eq!(
2044+
daily_input_tokens(&sessions, single_thread),
2045+
[("2026-11-01".to_string(), 2_000)],
2046+
"{name}, single_thread={single_thread}"
2047+
);
2048+
}
2049+
}
2050+
}
18672051
}

‎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(())

0 commit comments

Comments
 (0)