[codex] Forked sessions can undercount due dedupe collisions with forked_from_id
Summary
While parsing Codex session logs, forked sessions currently use forked_from_id in the cumulative token dedupe key:
const dedupKey = `codex:${forkedFromId || sessionId}:${cumulativeTotal}`
In src/providers/codex.ts this causes fork sessions to collide with their parent session's namespace. Forked sessions often replay the parent’s entire token history with similar cumulative totals, so parsing both sessions together can drop most forked token_count events as duplicates and undercount cost.
Repro
Compare event_msg entries with payload.type === 'token_count' and payload.info.total_token_usage.total_tokens.
Impact observed
- parent: 12,142
token_countentries withinfo - fork: 11,767
token_countentries withinfo - first 11,760 cumulative totals matched one-to-one before divergence.
- combined parse drops 10,337 fork events as duplicates under current keying.
This results in severe undercount in forked-session cost totals.
Why it happens
- Fork replay skipping (
forkCutoff) only skips first few seconds, not all replayed cumulative events. - Cumulative dedupe currently shares parent namespace via
forkedFromId.
Suggested fix
Use fork-local session id for cumulative dedupe, not parent id:
- replace
codex:${forkedFromId || sessionId}:${cumulativeTotal} - with
codex:${sessionId}:${cumulativeTotal}(or a similarly session-local discriminator).
I traced this on current main (3cdd13881fdbbbcb26ab9c54b41f8fac961952c5) and was able to turn it into a focused regression.
Fixture:
- parent session_id
parent-sessemits cumulative totals700,1100 - fork session_id
fork-sess,forked_from_id: parent-sess, emits matching cumulative totals after the existing 5s fork cutoff - both parsers share the same
seenKeys, as normal provider aggregation does
With the current key codex:${forkedFromId || sessionId}:${cumulativeTotal}, that regression returns 2 calls instead of the expected 4; the fork calls collide with the parent's dedupe keys. Changing cumulative token_count dedupe to codex:${sessionId}:${cumulativeTotal} keeps duplicate suppression local to the session while preserving the existing fork replay cutoff.
Local verification with that one-line change plus the fixture:
npm test -- tests/providers/codex.test.ts
PASS: 1 file, 15 tests
git diff --check
PASS
Patch shape:
src/providers/codex.ts: usesessionIdin the cumulative dedupe keytests/providers/codex.test.ts: add the parent/fork cumulative-total collision regression above
Happy to send this as a small PR if this matches the maintainer direction.
Thanks for the detailed writeup and repro. Dug into this deeply and opened #458 with a fix, but the approach differs from the suggested one-liner, so here is the reasoning.
The replays are not lost spend. A forked session copies the parent's entire token_count history (re-timestamped). The current key deliberately routes those replays into the parent's namespace (forkedFromId) so they collide and dedupe, because they represent the parent's already-counted spend, not new API calls. Keying on the fork's own sessionId (the suggested change) stops that collision and double-counts the whole replayed history instead. Reproduced with a parent + fork run through the real provider:
- parent does 1100 tokens; fork replays them past the 5s cutoff then adds 400 genuine new
- current key -> 1500 total (correct)
sessionIdkey -> 2600 total (overcounts by the replayed 1100)
But there is a real bug underneath. cumulativeTotal alone is too coarse a discriminator: a genuine post-divergence fork event whose running total coincidentally equals some parent total also collides and gets dropped -> undercount. That is the legitimate core of this issue.
Fix (#458): add the cumulative token breakdown (input / cached / output / reasoning) to the key. A replay copies those figures verbatim so it still collides (no double-count), while genuinely different work at the same total stays distinct (no undercount). It has to be the cumulative figures, not per-event deltas, otherwise the 5s replay cutoff skipping some events makes the delta diverge on a replay and double-count it in the total-only branch.
Validated across four key designs x three parent/fork scenarios; only the cumulative-breakdown key passes all three (no double-count, keeps divergent events, no overcount on a cutoff straddle). Three regression tests included.