SIGN IN SIGN UP

fix: consolidation retry storm, idle curation frequency, and session memory leak (#473)

## Summary

Fixes three user-reported issues: excessive Sonnet API usage, 5GB RAM
consumption, and knowledge consolidation stuck in an infinite retry
loop. All three are interconnected — the consolidation loop was a major
driver of the Sonnet overhead.

## Changes

### Consolidation retry storm (Issue 3 + Issue 1)
- **Consolidation cooldown** (`idle.ts`): tracks per-project
`{attemptedAt, entryCount}`. When consolidation runs but produces no
changes (LLM correctly concludes all entries are unique), enters a
1-hour cooldown. Cooldown clears when entry count changes (curation
adds/removes entries). Previously retried every 30-60s indefinitely —
15-30 wasted Sonnet calls per 30-minute idle period.
- **Stronger consolidation prompt** (`prompt.ts`): added a "FORCED
EVICTION" step — when merging/trimming isn't enough, the LLM MUST delete
least-valuable entries to reach the target. The user prompt now states
"must remove at least N entries."
- **Curation creation gate** (`curator.ts`): when entry count is at or
above `maxEntries`, curation runs with `skipCreate: true`, preventing
the ratchet effect where entries grow monotonically.

### Excessive Sonnet API usage (Issue 1)
- **Cost-aware idle curation** (`idle.ts`): the idle path was using raw
`afterTurns=3` while the inline path uses `afterTurns *
curationMultiplier` (=6 for Sonnet, =9 for Opus). Idle curation was
firing 2x more often than intended for Sonnet-class models.

### Session memory leak (Issue 2)
- **Session eviction** (`idle.ts`, `pipeline.ts`, `gradient.ts`,
`index.ts`): sessions idle > 1 hour are evicted from all in-memory Maps.
Persists final cost/gradient state to SQLite before cleanup. Cleans up:
gradient state, curation tracker, cost tracking, auth, billing prefix,
warmup auth, and pipeline satellite Maps (headerSessionIndex,
ltmSessionCache, ltmPinnedText, stableLtmCache, cwdWarned). New
`evictSession()` exported from core for clean single-session gradient
cleanup.
B
Burak Yigit Kaya committed
48cc25babaa8c19a8e8caa0888ebea42c50a91bc
Parent: 6f10e4b
Committed by GitHub <noreply@github.com> on 5/27/2026, 10:44:58 AM