Skip to content

MUL-4921: fix(codex): report resumed usage as task delta#5569

Open
wjueyao wants to merge 5 commits into
multica-ai:mainfrom
wjueyao:agent/fix-codex-resume-usage-delta
Open

MUL-4921: fix(codex): report resumed usage as task delta#5569
wjueyao wants to merge 5 commits into
multica-ai:mainfrom
wjueyao:agent/fix-codex-resume-usage-delta

Conversation

@wjueyao

@wjueyao wjueyao commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes Codex usage accounting for tasks that resume an existing app-server session.

Codex JSONL total_token_usage values 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_usage fallback semantics, and finds resumed rollouts that remain in an earlier date directory.

Thinking path:

  1. Confirmed the issue/workspace SQL aggregation correctly sums task-scoped rows.
  2. Traced the semantic mismatch to scanCodexSessionUsage selecting a session-scoped cumulative total.
  3. Added regression fixtures around the resumed task boundary, including aliases, resets, fallback ordering, and cross-day rollouts.
  4. Applied the conversion at the Codex JSONL boundary so all downstream usage consumers keep task-scoped semantics.

Related Issue

Closes #5568

Type of Change

  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Refactor / code improvement (no behavior change)
  • Documentation update
  • Tests (adding or improving test coverage)
  • CI / infrastructure

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_usage ordering, both cached-input aliases, counter resets, cache-only usage, and cross-day resumes.

How to Test

  1. cd server
  2. go test ./pkg/agent -run 'Test(ParseCodexSessionFile|ScanCodexSessionUsage|CodexSessionRoot)' -count=1
  3. Observe the resumed-session regression test reports {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/agent passed; the timing-heavy full pkg/agent run 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

  • The delta path depends on normal Codex RFC3339 event timestamps. Events without timestamps retain the previous whole-file behavior rather than dropping usage.
  • Historical usage rows are not rewritten; the fix applies to newly completed tasks.

Checklist

  • I have included a thinking path that traces from project context to this change
  • I have run tests locally and they pass
  • I have added or updated tests where applicable
  • If this change affects the UI, I have included before/after screenshots (not applicable)
  • I have updated relevant documentation to reflect my changes (not applicable; internal accounting semantics only)
  • If I added a new runtime / coding tool / UI tab, I synced the change to landing copy and relevant docs (not applicable)
  • If this PR touches Chinese product copy, I checked it against the convention (not applicable)
  • I have considered and documented any risks above
  • I will address all reviewer comments before requesting merge

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.

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
@vercel

vercel Bot commented Jul 17, 2026

Copy link
Copy Markdown

@wjueyao is attempting to deploy a commit to the IndexLabs Team on Vercel.

A member of the Team first needs to authorize it.

@wjueyao
wjueyao marked this pull request as ready for review July 17, 2026 07:48
Co-authored-by: multica-agent <github@multica.ai>
@multica-eve multica-eve changed the title fix(codex): report resumed usage as task delta MUL-4921: fix(codex): report resumed usage as task delta Jul 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Codex resumed tasks double-count cumulative token usage

1 participant