Skip to content

fix(stats): carry totalCompletionTokens across resumes (#1667 follow-up)#1680

Merged
esengine merged 1 commit into
mainfrom
chore/completion-tokens-carryover
May 24, 2026
Merged

fix(stats): carry totalCompletionTokens across resumes (#1667 follow-up)#1680
esengine merged 1 commit into
mainfrom
chore/completion-tokens-carryover

Conversation

@esengine

Copy link
Copy Markdown
Owner

Summary

Follow-up to #1667. That PR persisted totalCompletionTokens to session meta after every turn, but the carryover plumbing in SessionStats was incomplete:

  • seedCarryover (src/loop.ts:227) didn't read meta.totalCompletionTokens
  • SessionStats had no _carryoverCompletion field
  • the write call summed only this.stats.turns (no carryover term), and trimOldTurns (src/telemetry/stats.ts:194) dropped completion tokens into the void when older turns rolled off

Net effect: on every resume, the first turn's patchSessionMeta call overwrote the previously-saved totalCompletionTokens with just the new turn's tally — counter reset to ~0 across restarts. Cost / cache hit / miss don't have this problem because they each have proper carryover plumbing.

Currently zero user-visible impact since no UI reads the field yet, but the data was already flowing through UsageStats in both dashboard/src/App.tsx:168 and desktop/src/App.tsx:198, so the next surface that exposes it would inherit the bug.

Fix shape

Mirror the cacheHit / cacheMiss pattern exactly:

  • New _carryoverCompletion field, seeded from meta.totalCompletionTokens in seedCarryover
  • cumulativeCompletionTokens getter that returns carryover + sum of current turns
  • trimOldTurns folds completion tokens into carryover when oldest turns roll off, matching the other counters
  • loop.ts switches the patchSessionMeta write to use the new getter (so the persisted value always includes carryover and trimmed-turn contributions)

Test plan

  • npx tsc --noEmit
  • npx vitest run tests/telemetry.test.ts tests/loop.test.ts — 99 passed / 1 skipped (added 2 cases: carryover sums + zero/negative input guard)

#1667 added totalCompletionTokens to SessionMeta and wrote it after every
turn via patchSessionMeta, but the carryover plumbing in SessionStats
was incomplete:

- seedCarryover didn't read meta.totalCompletionTokens
- SessionStats had no _carryoverCompletion field
- the write call summed only this.turns (no carryover), and trimOldTurns
  dropped completion tokens into the void

Net effect: on every resume, the first turn's patchSessionMeta call
overwrote the previously-saved totalCompletionTokens with just the new
turn's tally — counter reset to ~0 across restarts. (Cost / cache hit /
miss don't have this problem because they each have proper carryover
plumbing.)

Currently zero user-visible impact since no UI reads the field yet, but
the data was already flowing through UsageStats in both dashboard and
desktop, so the next surface that exposes it would inherit the bug.

Fix mirrors the cacheHit/Miss pattern: new _carryoverCompletion field
seeded from meta, a cumulativeCompletionTokens getter, trimOldTurns
folds completion tokens into carryover like the other counters, and
loop.ts switches the write to use the new getter.
@esengine esengine merged commit f297fe0 into main May 24, 2026
3 checks passed
@esengine esengine deleted the chore/completion-tokens-carryover branch May 24, 2026 11:11
esengine pushed a commit that referenced this pull request May 24, 2026
…moved, persisted usage stats, plan dispatch gate

Headline themes:
- Desktop: bundle the CLI-hosted React dashboard, retire Tauri+Preact duplicate (#1418)
- Config: drop preset abstraction; flash/pro are direct model selections (#1657, #1630)
- Stats: persist cumulative usage to session meta + auto-restore on startup (#1667, #1680, #1643, #1628)
- Plans: editMode="plan" enforced at the ToolRegistry dispatch gate (#1681); step advance fix (#1629)
- Context: fold once at turn start, drop pre-flight + byte-ceiling (#1642, #1646); collapsible compacted card (#1649)
- Subagents: per-skill flash/pro override + Settings UI (#1632)
- Desktop polish: sidebar drag-resize (#1688), responsive collapse (#1585), copy/edit overlay + msg-history nav (#1645), Esc closes modal not turn (#1685), QQ tab isolation (#1672), DiffCard for edits (#1662), theme-aware highlighting (#1655), system events toggle (#1654/#1650), macOS TCC inheritance (#1614), dashboard.enabled (#1612)
- Dashboard polish: persistent session URL (#1586, #1589, #1599), theme-aware highlighting (#1664), IME confirm-enter guard (#1689), code-fence lang fix (#1677), vendor chunk split (#1587), markdown table h-scroll (#1562)
- TUI: Alt+S input stash/recall; static history isolated from input rerenders (#1635); legacy mouse drop (#1637, #1648); multi-edit gated in review (#1647)
- Diff: SplitDiff column border holds under CJK (#1686)
- MCP: workspace roots passed to servers (#1625); codeCommand honors mcpServers (#1603)
- Config plumbing: (baseUrl, apiKey) resolved as a tuple (#1658); stale model id self-heal (#1663)

See CHANGELOG for the full list.
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