fix(server): exclude inherited Codex child usage - #7238
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
ApprovabilityVerdict: Needs human review This PR changes how token usage is calculated and reported for Codex child agents by introducing baseline subtraction logic. Changes to metering/usage calculation have potential billing implications and warrant human review. You can customize Macroscope's approvability policy. Learn more. |
Closes #5793.
Problem
Codex child usage events contain a cumulative
totalcounter. A fork can copy prior cumulative history into that counter.CodexAdaptersent the raw total through the provider-neutral task-usage interface. The Agents panel then summed task snapshots that already contained parent history.The client fold and Agents panel did not create the inflation. They correctly max-merge and sum the task totals supplied by the adapter.
Change
CodexAdapterbefore runtime storage and UI consumption.total - lastframe.This keeps Codex counter rules inside the existing Codex adapter. It needs no contract, storage, or UI change.
Regression coverage
The provider-free adapter regression covers:
task.progress.typedUsageoutput.The test failed before the fix with inherited totals such as
5,800,000,100instead of100.Verification
pnpm exec vp test run apps/server/src/provider/Layers/CodexAdapter.test.ts— 27 passedgit diff --check— passedRepo-wide checks were not run, as repository guidance assigns the full suite to CI.
Compatibility
No public contract or stored shape changes. Existing inflated stored rows cannot be repaired from their stored shape alone. A later corrected snapshot replaces the active stable task-usage row.
Prepared with Codex. Luna Max subagents performed independent design and review checks.
Note
Fix Codex collab agent token usage to exclude inherited parent baseline
mapCollabAgentEventin CodexAdapter.ts now subtracts a per-agent baseline from cumulative token totals, sotask.progressevents reflect only child-generated usage rather than raw inherited totals.cumulative - current-turncounts (clamped to zero); subsequent events normalize against this stored baseline and clamp to the previously emitted maximum.collabUsageByAgentmap is introduced inadapter.startSessionto maintain baseline and latest state across events for each child agent.total.totalTokensorlast.totalTokensis absent.Macroscope summarized f9b6ed4.
Note
Medium Risk
Changes only Codex collab usage math in the server adapter; wrong baselines would skew agent token display/sums, but parent usage and public contracts are untouched and coverage is targeted.
Overview
Fixes inflated token totals in the Agents panel when Codex collab children inherit cumulative usage from a forked parent thread.
CodexAdapternow normalizescollabAgent/tokenUsagebefore emittingtask.progresstypedUsage. It keeps per-childCodexCollabUsageState(baseline + latest) for the session event stream. The first valid frame sets baseline fromtotal − lastso copied history is stripped; later frames subtract that fixed baseline from cumulativetotalwhile clamping each breakdown field to stay non-negative and non-decreasing. Events missingtotal.totalTokensorlast.totalTokensare dropped. Parentthread/tokenUsage/updatedmapping is unchanged.A lifecycle regression test drives parent baseline, two children, zero usage, resume/retry, duplicate frames, and asserts child
typedUsagestays turn-scoped (e.g. ~100 tokens, not billions).Reviewed by Cursor Bugbot for commit f9b6ed4. Bugbot is set up for automated code reviews on this repo. Configure here.