#12766: fix(memory): bootstrap session indexing on cold start
stale
Cluster:
Memory Management Fixes
## Problem
Two related bugs prevent session memory from working correctly:
### 1. Cold-start deadlock in `shouldSyncSessions()` (src/memory/manager.ts)
After a restart, `sessionsDirty` is `false` and `sessionsDirtyFiles` is empty (both are only set by runtime delta events). If `needsFullReindex` is also `false` (DB exists with valid fingerprint), the method always returns `false` and session files are never indexed.
**Observed:** 115 session files on disk, only 53 indexed. The 62 missing files are permanently invisible.
### 2. `extraDirs` hooks overridden by bundled hooks (src/hooks/workspace.ts)
The hook merge order places `extraDirs` *before* bundled hooks:
```
extra → bundled → managed → workspace
```
Since later entries overwrite earlier ones in the Map, bundled hooks always win over `extraDirs` with the same name. This makes it impossible to patch a bundled hook (e.g. `session-memory`) via `extraDirs`.
## Fixes
### Commit 1: `fix(memory): bootstrap session indexing on cold start`
- Add a one-time cold-start check in `shouldSyncSessions()` comparing indexed session rows vs files on disk
- When files outnumber indexed rows, force a catch-up sync
- Check runs at most once per manager lifetime (cached in `sessionsColdStartNeeded`)
- Uses sync `readdirSync` to stay cheap
- Logs `sessions cold-start: N files on disk, M indexed — will sync` for observability
### Commit 2: `fix(hooks): extraDirs should override bundled hooks, not the reverse`
- Swap merge order: bundled loads first, then `extraDirs` overwrites
- Managed and workspace hooks still take highest priority
- One-line change + comment update
## Scope
2 files changed: `src/memory/manager.ts` (+40 lines), `src/hooks/workspace.ts` (+3/-2 lines)
## Not addressed (future work)
- Bounded/chunked bootstrap sync for very large session directories (timeout mitigation)
- `--verbose` skip-reason logging for `shouldSyncSessions`
Ref: #12633
Most Similar PRs
#17639: fix: Memory indexer skips session files
by MisterGuy420 · 2026-02-16
70.3%
#20148: fix(memory): persist session dirty state and fix reindex gate
by togotago · 2026-02-18
69.6%
#11749: fix(memory): prioritize full reindex over session-start skip in sho...
by QDenka · 2026-02-08
68.4%
#10591: feat(hooks): add session-start-memory bundled hook
by morningstar-daemon · 2026-02-06
67.2%
#20183: fix(memory): index reset/deleted session transcripts
by VACInc · 2026-02-18
65.6%
#22220: feat(bootstrap): cache session's bootstrap files so we don't invali...
by anisoptera · 2026-02-20
64.5%
#20526: fix(session-memory): search state-dir sessions path when workspace ...
by 7Sageer · 2026-02-19
64.3%
#18756: fix the memory manager class hierarchy declared at the wrong level
by leoh · 2026-02-17
64.0%
#18103: fix: session-memory hook reads reset file after /new or /reset
by MisterGuy420 · 2026-02-16
62.9%
#19313: fix(session-memory): preserve rotated transcript selection on /new
by ayanesakura · 2026-02-17
61.9%