Repository navigation
fix(insights): stop recounting a Codex fork's copied parent history - #131
Merged
Merged
Conversation
A forked Codex thread (a subagent) starts its rollout file with a copy of the parent's records. The first copied token_count had no previous total, so the parent's whole cumulative usage was counted again in every fork, and copied records before any turn_context landed under model "unknown". In a forked session, records before the child's first own turn now only set the cumulative baseline. Codex turn ids are UUIDv7, so a turn older than the forked thread id is copied history; copied turn_context records can precede the copied counters, so the first turn_context alone is not a safe boundary. Non-fork sessions are unchanged. The Codex parser version is bumped so stored scans are recomputed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The fork boundary treated any turn id that was not UUIDv7 as the child's own turn. Copied parent history can contain goal-continuation turns with UUIDv4 ids, so the copy ended early and most of the parent's usage was still counted in every fork. Only a UUIDv7 turn created after the forked thread now ends the copy; a fork whose own thread id is not UUIDv7 keeps the previous behavior. The Codex parser version is bumped again so scans made with the earlier boundary are recomputed. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Owner
Codex Review
Update: Addressed in |
Owner
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Codex usage was overcounted for every subagent (forked thread). A forked rollout file starts with a copy of the parent's history, and Insights counted that copy again in each fork. On one real machine this inflated lifetime Codex usage from about 42 B to 628 B tokens, and put 275 B of it under model "unknown".
What
core/adapters.mjs(Codex parser): in a forked session, records before the child's first own turn only set the cumulative baseline; they no longer add usage. Applies totoken_countandtoken_usage_record.core/scan.mjs: Codex parser version 5 to 7, so stored scans are recomputed.turn_contextbefore the copied counters, copied counters with noturn_context, a copied UUIDv4 turn inside the copy, and a non-fork session.Why
Subagent rollouts (
session_meta.payload.forked_from_id,thread_source: "subagent") replay the parent's records with timestamps at the fork moment. The first copiedtoken_counthad no previous total, so the parent's cumulative usage was counted once per fork, and copied records before anyturn_contexthad no model. The parent's usage is already counted from its own file.A simpler boundary does not hold on real data. "Until the first
turn_context" fails because the copy often includes the parent'sturn_contextbefore its counters. A timestamp window fails because a large copy can take seconds to write. UUIDv7 turn ids carry their creation time, which separates the two cleanly.Related issues
None
Testing
npm run test:insights: 251 pass, 0 fail, 3 skipped (247 before; 4 new tests). The fork tests failed before the fix.npx eslint herdr-insights tests/herdr-insights --max-warnings=0: clean.Compatibility
None. Existing stores are recomputed once on the next scan because the Codex parser version changed.
Risk
Checklist
🤖 Generated with Claude Code