Skip to content

fix(memory): cap SessionStats turns at 200 with cost carryover#1628

Merged
esengine merged 1 commit into
esengine:mainfrom
Bernardxu123:fix/session-stats-rolling-window
May 23, 2026
Merged

fix(memory): cap SessionStats turns at 200 with cost carryover#1628
esengine merged 1 commit into
esengine:mainfrom
Bernardxu123:fix/session-stats-rolling-window

Conversation

@Bernardxu123

@Bernardxu123 Bernardxu123 commented May 23, 2026

Copy link
Copy Markdown
Collaborator

Memory Leak Fix — SessionStats Rolling Window

Fixes parts of #1571

Problem

Per-turn stats array (SessionStats.turns) grows indefinitely — iterated on every summary() call. Over long sessions this causes both memory growth and O(n) cost on each summary.

Fix

Add MAX_TURNS=200 cap: when the array exceeds this, oldest turns are dropped and their costs/cache tokens folded into carryover fields. Session totals remain accurate ( otalCost, cacheHitRatio, �ggregateCacheHitRatio) while the array stays bounded.

The cap is generous — 200 turns covers a full day of active use. The carryover aggregation means resuming a session after trim still reports correct cumulative totals.

Changes

  • src/telemetry/stats.ts: Added MAX_TURNS = 200 constant and rimOldTurns() method called from
    ecord()

Testing

  • npm run lint ✅
  • npm run typecheck ✅
  • npm run test ✅ (3588 passed)

Update

Drift issue fixed — rebased on latest main. Diff now only contains src/telemetry/stats.ts.

@esengine

Copy link
Copy Markdown
Owner

Both pieces of the original #1605 now filed cleanly (#1627 for AppendOnlyLog, this one for SessionStats) — appreciate the split. The carryover logic across _carryoverCost / _carryoverCacheHit / _carryoverCacheMiss / _carryoverTurns keeps the totalCost and cacheHitRatio getters accurate post-trim, and the record() insertion point is right. One blocker before merge: same drift pattern hitting today's PRs.

Drift

git diff origin/main..HEAD shows two extra files in the diff that aren't part of your fix:

src/cli/ui/PromptInput.tsx     |  6 +++---
src/cli/ui/cards/UserCard.tsx  |  2 +-

The change flips TONE.brand / SURFACE.bgInput back to hardcoded #0153e5 / #1e1e1e — that's #1623 (light-theme composer bg) which merged earlier today. GH says mergeStateStatus: CLEAN because git's 3-way merge sees absence-of-lines as deletion against main's additions; there are no textual conflicts to flag. Semantically merging this would undo the light-theme fix.

What to do

git fetch origin main && git rebase origin/main — your diff should narrow back to the one src/telemetry/stats.ts file. The PromptInput.tsx and UserCard.tsx lines should disappear from the diff entirely after rebase.

Same advice for #1627 — once both are rebased I'll merge them.

Per-turn stats array grows indefinitely — iterated on every
summary() call. Over long sessions this causes both memory growth
and O(n) cost on each summary.

Add MAX_TURNS=200 cap: when the array exceeds this, oldest turns
are dropped and their costs/cache tokens folded into carryover
fields. Session totals remain accurate (totalCost, cacheHitRatio)
while the array stays bounded.

The cap is generous — 200 turns covers a full day of active use.
The carryover aggregation means resuming a session after trim still
reports correct cumulative totals.
@Bernardxu123 Bernardxu123 force-pushed the fix/session-stats-rolling-window branch from 685e581 to 520010d Compare May 23, 2026 16:13
@Bernardxu123

Copy link
Copy Markdown
Collaborator Author

Rebased on latest main — drift cleaned up. Diff now only contains \src/telemetry/stats.ts.

@esengine esengine merged commit f12e10d into esengine:main May 23, 2026
4 checks passed
@Bernardxu123 Bernardxu123 deleted the fix/session-stats-rolling-window branch May 24, 2026 01:08
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.

2 participants