fix(dashboard): collect Codex token usage - #423
Conversation
|
Blocking: the legacy Codex cumulative counter is scoped to a rollout file, not reliably to the TeamAI session. I can reproduce this with two real Codex Desktop rollout files created when the same session was reopened/materialized again. Both files have exactly the same So the second file starts only 78 seconds after the first completes, retains the same session id, but its legacy Running this PR's {
"first": {
"input": 283144,
"output": 27733,
"cacheRead": 8339712,
"cacheCreation": 0
},
"resumed": {
"input": 29828,
"output": 2936,
"cacheRead": 2490368,
"cacheCreation": 0
}
}Feeding those two Stop snapshots, in timestamp order, through {
"latestWins": {
"input": 29828,
"output": 2936,
"cacheRead": 2490368,
"cacheCreation": 0
}
}More importantly, if rollout A has already been reported, {
"deltaAfterResume": {
"input": 0,
"output": 0,
"cacheRead": 0,
"cacheCreation": 0
}
}This silently drops the first 2.5M tokens from the reopened segment (and temporarily makes the dashboard regress from 8.65M to 2.52M). A new prompt can then advance the persisted baseline to the smaller snapshot, making that omitted segment permanent. This grouping is consistent with Codex itself: the Stop hook receives I think the legacy A regression test should use one |
- parse Codex Desktop and CLI cumulative token records - wait for post-Stop usage flush without double counting cache tokens - preserve existing session metrics when merging usage stats
- distinguish thread-level token_usage_record from rollout-scoped token_count totals - sum the latest legacy snapshot per transcript and order background Stops by event time - add resume, mixed-format, delta, and idempotency regression coverage - document Codex snapshot semantics
0fa92be to
8c3a0d1
Compare
Summary
token_usage_recordand CLIevent_msg/token_counttranscripts.Type of Change
Test Plan
npm run typechecknpm run buildnpx vitest run src/__tests__/dashboard-collector.test.ts src/__tests__/conversation-token-metrics.test.ts(99 passed)npx vitest run --config vitest.e2e.config.ts src/__tests__/conversation-token-e2e.test.ts(2 passed)npx vitest run src/__tests__/usage-tracking.test.ts -t mergeStats(6 passed)Full
npm teston Windows completed with 2,552 passing and 91 pre-existing platform/environment failures (POSIX path, permission-mode, and bash assumptions); all tests covering the changed behavior pass.Notes for Reviewers
Codex reports inclusive
input_tokensand cumulative thread totals. The collector uses the latest cumulative snapshot and maps cached/cache-write subsets into TeamAI's disjoint buckets so the digest total is not inflated. The Stop collector runs in the existing detached background handler, so the bounded flush wait does not delay the agent UI.