MUL-4921: fix(codex): report resumed usage as task delta#5569
Open
wjueyao wants to merge 5 commits into
Open
Conversation
Codex session logs expose cumulative total_token_usage values. Subtract the pre-task baseline so issue usage does not count earlier turns again. Refs multica-ai#5568
|
@wjueyao is attempting to deploy a commit to the IndexLabs Team on Vercel. A member of the Team first needs to authorize it. |
wjueyao
marked this pull request as ready for review
July 17, 2026 07:48
Co-authored-by: multica-agent <github@multica.ai>
Co-authored-by: multica-agent <github@multica.ai>
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.
What does this PR do?
Fixes Codex usage accounting for tasks that resume an existing app-server session.
Codex JSONL
total_token_usagevalues are cumulative across the whole resumed thread, but Multica persists one usage row per task. The fallback scanner previously stored the final cumulative value for every task, so issue/workspace aggregation counted earlier turns again on each resume.This change reads the JSONL event timestamps and accumulates only the counter deltas after the task start. It normalizes Codex's two cached-input field names before subtraction, handles counter resets, preserves final-event
last_token_usagefallback semantics, and finds resumed rollouts that remain in an earlier date directory.Thinking path:
scanCodexSessionUsageselecting a session-scoped cumulative total.Related Issue
Closes #5568
Type of Change
Changes Made
server/pkg/agent/codex.go: accumulate task-scoped deltas from Codex session JSONL, canonicalize cached-input aliases, tolerate counter resets, and scan modified rollouts across date directories.server/pkg/agent/codex_test.go: cover resumed-session deltas,total_token_usage/last_token_usageordering, both cached-input aliases, counter resets, cache-only usage, and cross-day resumes.How to Test
cd servergo test ./pkg/agent -run 'Test(ParseCodexSessionFile|ScanCodexSessionUsage|CodexSessionRoot)' -count=1{InputTokens:100 OutputTokens:65 CacheReadTokens:700}rather than the final session total, and all edge-case fixtures pass.Local focused result: PASS.
I also ran the full server suite. Packages outside
pkg/agentpassed; the timing-heavy fullpkg/agentrun hit unrelated 5-second fake-process timeouts under local host load. Re-running the affected usage tests in isolation is stable and passes. CI should provide the clean full-suite signal.Risks
Checklist
AI Disclosure
AI tool used: OpenAI Codex
Prompt / approach: Reproduced the cumulative-vs-task semantic mismatch from production-shaped JSONL totals, wrote failing regressions first, implemented the conversion at the session-log boundary, reviewed reset/fallback/cross-day edge cases, and verified the focused package tests before publishing.
Screenshots (optional)
Not applicable.