Repository navigation
fix(codex): count a resumed session's tokens from when it opened - #174
Merged
Merged
Conversation
xxchan
pushed a commit
that referenced
this pull request
Oct 7, 2026
Review on #174: a resumed Session that never got codex's re-report kept reporting the thread's lifetime total, which the spec now calls wrong (totals count from when this Session opened). Don't know, don't report: if the first root turn of a resumed Session starts before any re-report, the baseline is unknown, the Session's root usage events carry `context` only and `usage().total` stays null. On the probed releases that means codex 0.151.0 and later report this Session's numbers after a resume, and 0.118.0 to 0.150.1 report no token totals (rechecked on 0.118.0, 0.130.0 and 0.149.0). New Sessions are unaffected. codex.md, the spec and the experiment say so; unit and adapter-wiring tests pin the missing re-report. Refs #169 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Token totals were meant to be this Session's (`usage()` already says "THIS session's token total"), but codex's `tokenUsage.total` spans the thread's life, so a resumed codex Session reported every earlier turn too and a host summing its Sessions over-counted (#169). As decided on the issue, every runtime's totals now count from when the Session opened. After a resume, before the Session's first turn starts, codex re-reports the thread's total once under the previous turn's id; the adapter records that report as the baseline and subtracts it from every later root total, cacheRead and cacheWrite included. It only subtracts codex's own number: a codex that sends no such report gets nothing subtracted. Probed with experiments/codex-resume-usage.ts against a scripted provider on 20 releases: 0.151.0 and later send the report; 0.131.0 to 0.150.1 send none, and 0.118.0 to 0.130.0 re-report it inside the first turn under that turn's id. On those releases a resumed Session still counts the thread's life; codex.md lists them. Ferry's real 0.155.1 recording (18,185 / 5, then 38,557 / 10) now reads 20,372 / 5 for the resumed run. - codex/token-usage.ts: the usage reading moved out of projection.ts, plus the baseline; the projection learns its open method - a recorded codex-aimock resume fixture (the recorder gains a `resume` scenario and keeps tokenUsage's turnId), unit, wiring and vendor tests - spec: "cumulative" becomes "counted from when this Session opened" Refs #169 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Review on #174: a resumed Session that never got codex's re-report kept reporting the thread's lifetime total, which the spec now calls wrong (totals count from when this Session opened). Don't know, don't report: if the first root turn of a resumed Session starts before any re-report, the baseline is unknown, the Session's root usage events carry `context` only and `usage().total` stays null. On the probed releases that means codex 0.151.0 and later report this Session's numbers after a resume, and 0.118.0 to 0.150.1 report no token totals (rechecked on 0.118.0, 0.130.0 and 0.149.0). New Sessions are unaffected. codex.md, the spec and the experiment say so; unit and adapter-wiring tests pin the missing re-report. Refs #169 Co-Authored-By: Claude Opus 5.5 <[email protected]>
xxchan
force-pushed
the
codex-token-baseline
branch
from
October 7, 2026 05:30
9ed6228 to
8c60b6d
Compare
Merged
xxchan
added a commit
that referenced
this pull request
Oct 7, 2026
Release OAR and CLI 0.30.0. This PR changes only the two package version fields. **Changed** - Token totals count from when this oar Session opened, on every runtime (#174, closes #169). `TokenTotals`, `UsageReport.tokens` and `usage()` now say so. Only codex changes: - codex 0.151.0 and later: after a resume, codex re-reports the thread's earlier total once before the Session's first turn; OAR subtracts it (with `cacheRead` / `cacheWrite`), so a resumed Session reports its own spend instead of the thread's whole life. - codex releases that send no such re-report before the first turn (0.118.0 to 0.150.1): a resumed Session reports no `tokens` (`usage().total` is null); `context` is unchanged. New sessions are unaffected. - Clearer refusals: kimi (#172) and antigravity (#173) still refuse `systemPrompt` / `appendSystemPrompt`, now naming the native reason (kimi's `acp` launcher does not forward `--agent-file` / `--agent`; antigravity's server has no prompt input in its protocol, launcher or configuration), with each session option's native channel documented. - grok prompts (#175): `systemPrompt` with `appendSystemPrompt` are sent as one override (the first, a blank line, the second) on new and resumed sessions, since grok 1.0.46 uses only the override when both are given. Resuming with only `appendSystemPrompt` is refused (`UnsupportedOptionError`): grok does not reapply rules on resume. A resumed `systemPrompt` keeps working. Pinned by `experiments/grok-prompt-options.ts`, which captures the request grok sends to a local provider. Validation: all 13 checks passed on the final heads of #172, #173, #174 and #175 (`8c60b6d`; evidence across 20 codex releases in `experiments/codex-resume-usage.ts`). After this PR passes CI and merges, tag its exact squash commit as `v0.30.0` to trigger npm trusted publishing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: exe.dev user <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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.
Implements #169 as decided by @cindy on the issue: token totals always count from when this oar Session opened, on every runtime.
What changes
thread/tokenUsage/updated { threadId, turnId: <previous turn's id>, tokenUsage: { total, last, modelContextWindow } }.The adapter takes the first root report that arrives before the first root
turn/startedof a resumed Session as the baseline. It subtracts that baseline from every later root total,cacheReadandcacheWriteincluded. The re-report itself readstokens0 and keeps itscontextreading. Only codex's own number is subtracted; nothing is estimated.Don't know, don't report (review): if a resumed Session's first root turn starts before any re-report, the baseline is unknown. Its root usage events then carry
contextonly andusage().totalstays null. They never show the thread's lifetime total. New Sessions are unaffected. The logic lives in a newruntimes/codex/token-usage.ts; the usage reading moved there fromprojection.ts, which now learns its open method.TokenTotals,UsageReport,usage(), the usage fold,attribution.md"Usage: one constraint", plus the record-stream and runtime-matrix mentions).Evidence
experiments/codex-resume-usage.ts(codex-aimock, no login, no tokens) ran on 20 releases:usage()turn/started, previous turn's idtotal: null)total: null)Ferry's real codex 0.155.1 recording, replayed through the new projection: run 1 is 18,185 / 5. The resumed run re-reports 18,185 / 5 and then reports 38,557 / 10, which it now shows as 20,372 / 5.
Tests
tests/replay/codex-resume.test.ts: a recorded codex-aimock resume (0.160.1). The recorder gained aresumescenario and now keeps tokenUsage'sturnId. The test checks the per-Session usage events,usageOfandviewOf.tests/codex/codex-token-usage.test.ts: Ferry's real numbers, cache parts, a resume without a re-report (notokens), a new thread, child threads not baselined.tests/codex/codex-session-resume-model.test.ts: adapter wiring,usage()on a resumed Session with and without the re-report.sea-trial/vendor/codex.vendor.test.ts: real codex resumed against aimock.pnpm run check(also withCI=1),OAR_TEST=codex-aimock pnpm sea-trial, and the codex-aimock vendor tests are green locally. Claude and pi resumed on aimock also report per Session (an ad-hoc check, not committed). No adapter other than codex changed.Refs #169
🤖 Generated with Claude Code