Skip to content

Commit 91b9881

Browse files
fix(codex): anchor replay bursts to session start
Preserve duplicate-only replay detection when the parent log is missing. Use the session start timestamp to distinguish inherited history from a real first response, retaining the existing pause threshold and parent matching. Restore the original replay regression and cover the duplicate response through daily and session JSON reports and table snapshots. Co-authored-by: parttimediarybehavior <[email protected]>
1 parent 9e179ed commit 91b9881

6 files changed

Lines changed: 212 additions & 6 deletions

File tree

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

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,6 +2336,99 @@ mod tests {
23362336
}
23372337
}
23382338

2339+
#[test]
2340+
fn skips_missing_parent_replay_when_duplicate_snapshot_is_suppressed() {
2341+
fn token_count(timestamp: &str, input: u64, total_input: u64) -> String {
2342+
json!({
2343+
"timestamp": timestamp,
2344+
"type": "event_msg",
2345+
"payload": {
2346+
"type": "token_count",
2347+
"info": {
2348+
"last_token_usage": {
2349+
"input_tokens": input,
2350+
"output_tokens": 1,
2351+
"total_tokens": input + 1,
2352+
},
2353+
"total_token_usage": {
2354+
"input_tokens": total_input,
2355+
"output_tokens": 1,
2356+
"total_tokens": total_input + 1,
2357+
},
2358+
"model": "gpt-5.5",
2359+
},
2360+
},
2361+
})
2362+
.to_string()
2363+
}
2364+
2365+
let fixture = fs_fixture!({
2366+
"child.jsonl": [
2367+
json!({
2368+
"type": "session_meta",
2369+
"payload": {"id": "child", "forked_from_id": "missing-parent"},
2370+
})
2371+
.to_string(),
2372+
// The replay repeats a snapshot, which must not end the burst.
2373+
token_count("2026-07-10T08:00:00.100Z", 100, 100),
2374+
token_count("2026-07-10T08:00:00.200Z", 100, 100),
2375+
// The child's own turn, after a real pause.
2376+
token_count("2026-07-10T08:00:08.000Z", 50, 50),
2377+
]
2378+
.join("\n"),
2379+
});
2380+
2381+
let events = load_codex_events_from_directory(fixture.root(), true).unwrap();
2382+
2383+
assert_eq!(events.len(), 1);
2384+
assert_eq!(events[0].input_tokens, 50);
2385+
}
2386+
2387+
#[test]
2388+
fn skips_missing_parent_duplicate_only_replay_at_known_session_start() {
2389+
let token_count = |timestamp: &str, input_tokens: u64| {
2390+
json!({
2391+
"timestamp": timestamp,
2392+
"type": "event_msg",
2393+
"payload": {
2394+
"type": "token_count",
2395+
"info": {
2396+
"last_token_usage": {
2397+
"input_tokens": input_tokens,
2398+
"output_tokens": 1,
2399+
"total_tokens": input_tokens + 1,
2400+
},
2401+
"total_token_usage": {
2402+
"input_tokens": input_tokens,
2403+
"output_tokens": 1,
2404+
"total_tokens": input_tokens + 1,
2405+
},
2406+
},
2407+
},
2408+
})
2409+
.to_string()
2410+
};
2411+
2412+
let fixture = fs_fixture!({
2413+
"child.jsonl": [
2414+
replay_metadata(
2415+
"2026-07-10T08:00:00.000Z",
2416+
"child",
2417+
Some("missing-parent"),
2418+
),
2419+
token_count("2026-07-10T08:00:00.100Z", 100),
2420+
token_count("2026-07-10T08:00:00.200Z", 100),
2421+
token_count("2026-07-10T08:00:08.000Z", 50),
2422+
]
2423+
.join("\n"),
2424+
});
2425+
2426+
let events = load_codex_events_from_directory(fixture.root(), true).unwrap();
2427+
2428+
assert_eq!(events.len(), 1);
2429+
assert_eq!(events[0].input_tokens, 50);
2430+
}
2431+
23392432
#[test]
23402433
fn skips_missing_parent_replay_with_duplicate_snapshots_and_advancing_usage() {
23412434
fn token_count(timestamp: &str, input: u64, total_input: u64) -> String {

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ fn detect_rewritten_burst(path: &Path) -> Option<TimestampMs> {
140140
};
141141
let mut line = Vec::new();
142142
let mut first: Option<TimestampMs> = None;
143-
let mut previous_totals = None;
143+
let mut session_start = None;
144144

145145
loop {
146146
line.clear();
@@ -150,6 +150,16 @@ fn detect_rewritten_burst(path: &Path) -> Option<TimestampMs> {
150150
if bytes_read == 0 {
151151
return None;
152152
}
153+
if memchr::memmem::find(&line, b"session_meta").is_some()
154+
&& let Ok(value) = serde_json::from_slice::<CodexSessionLogEntry<'_>>(&line)
155+
&& value.entry_type.as_deref() == Some("session_meta")
156+
{
157+
if session_start.is_none() {
158+
session_start = codex_session_timestamp(value.timestamp.as_ref())
159+
.and_then(|timestamp| parse_ts_timestamp(&timestamp));
160+
}
161+
continue;
162+
}
153163
let Some(CodexLineKind::Session) = codex_line_usage_kind(&line) else {
154164
continue;
155165
};
@@ -165,8 +175,9 @@ fn detect_rewritten_burst(path: &Path) -> Option<TimestampMs> {
165175
let Some(timestamp) = codex_session_timestamp(value.timestamp.as_ref()) else {
166176
continue;
167177
};
168-
let usage = codex_token_count_usage(info, &mut previous_totals);
169-
if !usage.delta.as_ref().is_some_and(has_codex_token_usage) {
178+
if info
179+
.is_none_or(|info| info.last_token_usage.is_none() && info.total_token_usage.is_none())
180+
{
170181
continue;
171182
}
172183
let Some(timestamp) = parse_ts_timestamp(&timestamp) else {
@@ -175,9 +186,14 @@ fn detect_rewritten_burst(path: &Path) -> Option<TimestampMs> {
175186
match first {
176187
None => first = Some(timestamp),
177188
Some(first) => {
178-
return (0..=CODEX_REWRITTEN_BURST_PAUSE_MS)
179-
.contains(&(timestamp.as_millis() - first.as_millis()))
180-
.then_some(first);
189+
let is_dense = (0..=CODEX_REWRITTEN_BURST_PAUSE_MS)
190+
.contains(&(timestamp.as_millis() - first.as_millis()));
191+
// A late first request can also produce a dense pair.
192+
let starts_at_fork = session_start.is_none_or(|start| {
193+
first.as_millis().abs_diff(start.as_millis())
194+
<= CODEX_REWRITTEN_BURST_PAUSE_MS as u64
195+
});
196+
return (is_dense && starts_at_fork).then_some(first);
181197
}
182198
}
183199
}

‎rust/crates/ccusage/tests/codex_cli.rs‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,75 @@ fn reports_compressed_codex_rollouts_alongside_plain_ones() {
118118
assert_eq!(session["totalTokens"], 325);
119119
}
120120

121+
#[test]
122+
fn reports_issue_1970_subagent_usage_in_daily_and_session_commands() {
123+
let fixture = issue_1970_codex_fixture();
124+
125+
let daily_json = run_cli(
126+
&fixture,
127+
&[
128+
"codex", "daily", "--since", "20260101", "--until", "20260131", "--json",
129+
],
130+
);
131+
let daily: Value = serde_json::from_str(&daily_json).unwrap();
132+
assert_eq!(daily["totals"]["totalTokens"], 1010);
133+
let daily_row = daily["daily"]
134+
.as_array()
135+
.unwrap()
136+
.iter()
137+
.find(|row| row["date"] == "2026-01-05")
138+
.expect("issue 1970 daily row should exist");
139+
assert_eq!(daily_row["totalTokens"], 1010);
140+
141+
let session_json = run_cli(
142+
&fixture,
143+
&[
144+
"codex", "session", "--since", "20260101", "--until", "20260131", "--json",
145+
],
146+
);
147+
let session: Value = serde_json::from_str(&session_json).unwrap();
148+
assert_eq!(session["totals"]["totalTokens"], 1010);
149+
let session_row = session["sessions"]
150+
.as_array()
151+
.unwrap()
152+
.iter()
153+
.find(|row| row["sessionFile"] == "child")
154+
.expect("issue 1970 session row should exist");
155+
assert_eq!(session_row["totalTokens"], 1010);
156+
157+
insta::assert_snapshot!(
158+
"issue_1970_daily_table",
159+
format!(
160+
"Daily\n{}",
161+
run_cli(
162+
&fixture,
163+
&[
164+
"codex", "daily", "--since", "20260101", "--until", "20260131",
165+
],
166+
),
167+
)
168+
);
169+
insta::assert_snapshot!(
170+
"issue_1970_session_table",
171+
format!(
172+
"Session\n{}",
173+
run_cli(
174+
&fixture,
175+
&[
176+
"codex", "session", "--since", "20260101", "--until", "20260131",
177+
],
178+
),
179+
)
180+
);
181+
}
182+
183+
fn issue_1970_codex_fixture() -> Fixture {
184+
fs_fixture!({
185+
"codex/sessions/2026/01/05/child.jsonl":
186+
include_str!("fixtures/codex/issue_1970.jsonl"),
187+
})
188+
}
189+
121190
fn codex_fixture() -> Fixture {
122191
fs_fixture!({
123192
"codex/sessions/2026/09/19/rollout-2026-09-19T23-22-40-01a0bb8c-c7c0-7630-9d82-860b875930f0.jsonl": usage("2026-09-19T23:22:40.000Z", 100, 50),
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{"timestamp":"2026-01-05T15:00:00.000Z","type":"session_meta","payload":{"id":"child","source":{"subagent":{"thread_spawn":{"parent_thread_id":"missing-parent"}}}}}
2+
{"timestamp":"2026-01-05T15:00:01.000Z","type":"turn_context","payload":{"model":"gpt-5","model_name":null,"metadata":null}}
3+
{"timestamp":"2026-01-05T15:00:02.000Z","type":"event_msg","payload":{"type":"token_count","info":{"last_token_usage":{"input_tokens":1000,"cached_input_tokens":400,"output_tokens":10,"reasoning_output_tokens":0,"total_tokens":1010},"total_token_usage":{"input_tokens":1000,"cached_input_tokens":400,"output_tokens":10,"reasoning_output_tokens":0,"total_tokens":1010}}}}
4+
{"timestamp":"2026-01-05T15:00:02.150Z","type":"event_msg","payload":{"type":"token_count","info":{"last_token_usage":{"input_tokens":1000,"cached_input_tokens":400,"output_tokens":10,"reasoning_output_tokens":0,"total_tokens":1010},"total_token_usage":{"input_tokens":1000,"cached_input_tokens":400,"output_tokens":10,"reasoning_output_tokens":0,"total_tokens":1010}}}}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/ccusage/tests/codex_cli.rs
3+
expression: "format!(\"Daily\\n{}\",\nrun_cli(&fixture,\n&[\"codex\", \"daily\", \"--since\", \"20260101\", \"--until\", \"20260131\",],),)"
4+
---
5+
Daily
6+
┌────────────┬───────────────┬───────────┬───────────┬────────────┬───────────────┬─────────────┬───────────────┬─────────────┐
7+
│ Date │ Models │ Input │ Output │ Reasoning │ Cache Create │ Cache Read │ Total Tokens │ Cost (USD) │
8+
├────────────┼───────────────┼───────────┼───────────┼────────────┼───────────────┼─────────────┼───────────────┼─────────────┤
9+
│ 2026-01-05 │ - gpt-5 │ 600 │ 10 │ 0 │ 0 │ 400 │ 1,010 │ $0.00 │
10+
├────────────┼───────────────┼───────────┼───────────┼────────────┼───────────────┼─────────────┼───────────────┼─────────────┤
11+
│ Total │ │ 600 │ 10 │ 0 │ 0 │ 400 │ 1,010 │ $0.00 │
12+
└────────────┴───────────────┴───────────┴───────────┴────────────┴───────────────┴─────────────┴───────────────┴─────────────┘
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
source: crates/ccusage/tests/codex_cli.rs
3+
expression: "format!(\"Session\\n{}\",\nrun_cli(&fixture,\n&[\"codex\", \"session\", \"--since\", \"20260101\", \"--until\", \"20260131\",],),)"
4+
---
5+
Session
6+
┌──────────────────┬───────────────┬───────────┬───────────┬────────────┬───────────────┬─────────────┬───────────────┬─────────────┐
7+
│ Session │ Models │ Input │ Output │ Reasoning │ Cache Create │ Cache Read │ Total Tokens │ Cost (USD) │
8+
├──────────────────┼───────────────┼───────────┼───────────┼────────────┼───────────────┼─────────────┼───────────────┼─────────────┤
9+
│ 2026/01/05/child │ - gpt-5 │ 600 │ 10 │ 0 │ 0 │ 400 │ 1,010 │ $0.00 │
10+
├──────────────────┼───────────────┼───────────┼───────────┼────────────┼───────────────┼─────────────┼───────────────┼─────────────┤
11+
│ Total │ │ 600 │ 10 │ 0 │ 0 │ 400 │ 1,010 │ $0.00 │
12+
└──────────────────┴───────────────┴───────────┴───────────┴────────────┴───────────────┴─────────────┴───────────────┴─────────────┘

0 commit comments

Comments
 (0)