Skip to content

fix(codex): count a resumed session's tokens from when it opened - #174

Merged
xxchan merged 2 commits into
mainfrom
codex-token-baseline
Oct 7, 2026
Merged

xxchan merged 2 commits into
mainfrom
codex-token-baseline

Conversation

@xxchan

@xxchan xxchan commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

Implements #169 as decided by @cindy on the issue: token totals always count from when this oar Session opened, on every runtime.

What changes

  • codex adapter. After a resume, before the Session's first turn starts, codex re-reports the thread's total once:
    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/started of a resumed Session as the baseline. It subtracts that baseline from every later root total, cacheRead and cacheWrite included. The re-report itself reads tokens 0 and keeps its context reading. 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 context only and usage().total stays null. They never show the thread's lifetime total. New Sessions are unaffected. The logic lives in a new runtimes/codex/token-usage.ts; the usage reading moved there from projection.ts, which now learns its open method.
  • Spec. "cumulative" becomes "counted from when this Session opened" (TokenTotals, UsageReport, usage(), the usage fold, attribution.md "Usage: one constraint", plus the record-stream and runtime-matrix mentions).
  • codex.md. Documents the frame, the baseline, Ferry's real-login numbers, and which releases send the re-report.

Evidence

experiments/codex-resume-usage.ts (codex-aimock, no login, no tokens) ran on 20 releases:

codex releases run re-report of the earlier total resumed usage()
0.151.0, 0.153.0, 0.154.0, 0.155.1, 0.160.1 one frame before the first turn/started, previous turn's id this Session's own (1200 / 7)
0.131.0 to 0.137.0, 0.141.0, 0.144.6, 0.149.0 to 0.150.1 none no token totals (total: null)
0.118.0 (login floor), 0.130.0 inside the first turn, under that turn's own id no token totals (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 a resume scenario and now keeps tokenUsage's turnId. The test checks the per-Session usage events, usageOf and viewOf.
  • tests/codex/codex-token-usage.test.ts: Ferry's real numbers, cache parts, a resume without a re-report (no tokens), 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 with CI=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

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]>
exe.dev user and others added 2 commits October 7, 2026 05:29
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
xxchan force-pushed the codex-token-baseline branch from 9ed6228 to 8c60b6d Compare October 7, 2026 05:30
@xxchan
xxchan merged commit 9883582 into main Oct 7, 2026
13 checks passed
@xxchan
xxchan deleted the codex-token-baseline branch October 7, 2026 05:33
@xxchan xxchan mentioned this pull request Oct 7, 2026
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]>
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