fix(memory): cap SessionStats turns at 200 with cost carryover#1628
Conversation
|
Both pieces of the original #1605 now filed cleanly (#1627 for AppendOnlyLog, this one for SessionStats) — appreciate the split. The carryover logic across Drift
The change flips What to do
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.
685e581 to
520010d
Compare
|
Rebased on latest main — drift cleaned up. Diff now only contains \src/telemetry/stats.ts. |
…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.
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
ecord()
Testing
Update
Drift issue fixed — rebased on latest main. Diff now only contains src/telemetry/stats.ts.