Skip to content

Fix Codex cumulative-as-delta usage; add subtractUsage helper - #6

Merged
michael-tomala merged 1 commit into
mainfrom
fix-codex-cumulative-usage-delta
May 9, 2026
Merged

michael-tomala merged 1 commit into
mainfrom
fix-codex-cumulative-usage-delta

Conversation

@michael-tomala

Copy link
Copy Markdown
Contributor

Summary

  • Codex adapter was leaking cumulative session totals through result.usage (the unified contract requires per-execute() delta). codex exec --experimental-json emits cumulative usage on turn.completed — see Include per-API-call token usage (last) in turn.completed JSONL events openai/codex#17539. Adapter now tracks last-seen cumulative per threadId (LRU-capped to 256) and yields current − prior.
  • New subtractUsage helper in src/usage.ts, exported from src/index.ts.
  • assertResumeUsageIndependence relaxed to a cumulative-leak bound (max(2× t1, t1+100)) so adapters with single-digit inputTokens (cache-heavy SDKs) aren't flaky from natural variance, while still catching the 2× cumulative regression.
  • New src/adapters/codex.usage.test.ts mocks @openai/codex-sdk and asserts delta across three sequential turns on the same thread.

Test plan

  • npm run typecheck passes
  • npm test — 145/145 unit tests pass (incl. new codex.usage.test.ts)
  • Codex e2e resume scenario re-validated downstream

🤖 Generated with Claude Code

- codex adapter: track last cumulative per threadId (LRU-capped) and emit
  delta = current_cumulative - prior in result.usage. Underlying
  `codex exec --experimental-json` reports cumulative session totals
  (openai/codex#17539); unified contract requires per-execute() delta.
- usage: add subtractUsage helper, exported from index.
- testing: relax assertResumeUsageIndependence to a cumulative-leak bound
  so cache-heavy adapters (single-digit inputTokens) aren't flaky while
  still catching the 2x cumulative regression.
- new codex.usage.test.ts mocks the SDK and asserts delta across 3 turns.

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@michael-tomala
michael-tomala merged commit ccae562 into main May 9, 2026
@michael-tomala
michael-tomala deleted the fix-codex-cumulative-usage-delta branch August 26, 2026 18:22
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.

1 participant