Repository navigation
Description
Summary
The index cache evicts in insertion order and a hit does not refresh the entry. A library search walks every index in the same order each time, so with more than 64 indexes the hit rate drops to zero and every query re-reads, re-parses and re-masks every index synchronously.
Where
src/utils/recordingIndex.ts:374-395src/utils/recordingIndex.ts:451-464src/tools/searchRecording.ts:56-60src/utils/retention.ts:13
Details
const INDEX_CACHE_MAX = 64; ... if (indexCache.size >= INDEX_CACHE_MAX) indexCache.delete(indexCache.keys().next().value!);. This is insertion-order (FIFO) eviction, and a hit does not move its entry to the back. A library search (search_recording with no input) runs listLibraryIndexes(homeRoot()).flatMap(p => readIndexCached(p)) over every index in the same directory order each time. With N > 64 indexes that cyclic scan evicts each entry just before it is needed again, so the hit rate is 0. Each miss is a synchronous readFileSync + JSON.parse (recordingIndex.ts:363-365), and it creates a new index object, so the maskedCache WeakMap (keyed by that object, 451-464) also misses. Masking every item was named "most of a search's cost" in #346. Below the limit, the cache holds up to 64 fully parsed indexes plus their masked/normalized copies, with no byte budget. One index carries up to 600 segments of OCR lines plus events, transcript, and up to 50,000 log lines (logTail.ts:20), so a 1 h index can run to several MB. Retention is off by default (retention.ts:13), so indexes keep accumulating.
Impact
Once a user has indexed more than 64 recordings, each library search blocks the event loop while it reads, parses, and masks every index: hundreds of MB of JSON for 100 multi-MB indexes, so seconds to tens of seconds during which no other MCP request, cancel, or stop is handled. That is the #346 cost again, just above a fixed count. Below 64, the resident memory of the cache can reach hundreds of MB.
Steps to reproduce
Create 65+ *.index.json files under SCREENCAST_HOME/edits/indexes (index_recording on 65 clips), then call search_recording { query: "error" } twice. The second call takes as long as the first, and every file is read again.
Suggested fix
Make the cache LRU (on a hit, delete then set to move the entry to the back) and bound it by total bytes (st.size), for example 64 MB, instead of entry count. Better, keep a small per-index search entry (pre-masked norm strings and item metadata only) instead of the full parsed index. Reading the files with fs/promises would keep a cold library search from stalling the server.
Acceptance criteria
- Cache hits move the entry to the back (true LRU), or the cache is bounded by total bytes instead of count
- A test with more than 64 indexes shows repeated library searches hit the cache
Getting started
- Relevant files:
src/utils/recordingIndex.ts,src/tools/searchRecording.ts,src/utils/retention.ts - Run tests:
npm test(alsonpm run typecheckandnpm run lint); see CONTRIBUTING.md - If you change a tool's schema or description, run
npm run docs
Filed by repo-audit/full-audit at f0e5ebd. Severity: medium. Effort: S. audit-key: performance|src/utils/recordingIndex.ts|readIndexCached
Activity