Skip to content

fix: codex real token usage + resolve codex outside the daemon PATH#145

Merged
saucam merged 2 commits into
mainfrom
fix/codex-usage-path
Jul 10, 2026
Merged

fix: codex real token usage + resolve codex outside the daemon PATH#145
saucam merged 2 commits into
mainfrom
fix/codex-usage-path

Conversation

@saucam

@saucam saucam commented Jul 10, 2026

Copy link
Copy Markdown
Owner

What

Two bugs the live ChatGPT-subscription smoke exposed — both make codex look broken on a real machine, neither is catchable offline:

  1. Usage was always zero. codex's turn/completed carries no token usage (confirmed by probing @openai/codex@0.144.1 live). Usage arrives on the separate thread/tokenUsage/updated notification, with tokenUsage.last being the just-finished turn's counts. The provider now captures that per turn and folds it into turn_done, matching the claude convention (inputTokens = new tokens, cache reads excluded: last.inputTokens - last.cachedInputTokens; cache reads and output mapped through). Smoke went from in=0 out=0 to in=12474 out=8.

  2. codex resolved to "not installed" when it plainly was. codex is a Node-managed binary, and the daemon frequently runs with a PATH that omits the user's nvm / npm-global bin (a systemd/launchd service, or a shell that never sourced nvm — exactly this machine). After PATH, resolution now scans the standard per-user Node bin dirs (nvm versions newest-first, NVM_BIN/VOLTA_HOME/npm_config_prefix, ~/.npm-global, ~/.local/bin), new source: "node-bin". Verified live: codex resolves with an empty daemon PATH.

Tests

fake-codex now emits the real tokenUsage.last notification (and its turn/completed no longer carries the fictional usage field). C1 asserts the cache-excluded numbers. The resolution test adds the node-bin-fallback case and isolates HOME so the machine's own codex can't leak into the "no codex → null" assertion.

bun run test, typecheck, lint green; both fixes confirmed against real codex.

🤖 Generated with Claude Code

Two fixes surfaced by the live subscription smoke (both previously showed
as zeros / not-installed on a real machine):

- Usage: codex's turn/completed carries NO token usage (verified against
  @openai/codex 0.144.1). It arrives on the separate
  thread/tokenUsage/updated notification (tokenUsage.last). Capture that
  per turn and fold it into turn_done — inputTokens excludes cache reads
  (claude convention: last.inputTokens - last.cachedInputTokens), cache
  reads and outputTokens mapped through. Smoke now reports real
  in=12474/out=8 instead of 0/0.
- Resolution: codex is a Node-managed binary, and the daemon often runs
  with a PATH that omits the user's nvm / npm-global bin (systemd/launchd
  service, or a shell that never sourced nvm) — so a clearly-installed
  codex showed as unavailable. After PATH, scan the standard per-user
  Node bin dirs (nvm versions newest-first, NVM_BIN/VOLTA_HOME/
  npm_config_prefix, ~/.npm-global, ~/.local/bin). New source: 'node-bin'.
  Verified live: codex resolves with an EMPTY daemon PATH.

Tests: fake-codex emits the real tokenUsage.last notification; C1 asserts
the cache-excluded usage; resolution test adds the node-bin-fallback case
and isolates HOME so the machine's own codex can't leak into the
no-codex assertion.
@codecov

codecov Bot commented Jul 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.86%. Comparing base (d88adf5) to head (ca7eb48).
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #145      +/-   ##
==========================================
+ Coverage   82.82%   82.86%   +0.03%     
==========================================
  Files         104      104              
  Lines       17734    17775      +41     
==========================================
+ Hits        14688    14729      +41     
  Misses       3046     3046              
Flag Coverage Δ
daemon 82.86% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/daemon/providers/codex/index.ts 100.00% <100.00%> (ø)
src/daemon/providers/codex/resolve.ts 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

… vars)

codecov patch gap was the nodeBinDirs scanning. Add tests for the nvm
newest-version-first glob, each version-manager env source (NVM_BIN /
VOLTA_HOME / npm_config_prefix), and export compareNodeVersionsDesc to
unit-test its ordering + equal-version tie. resolve.ts + index.ts now
100% line coverage.
@saucam saucam merged commit 1693fd3 into main Jul 10, 2026
4 checks passed
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