Expand description
Task 33 — staged in-repo lexical memory index (spec §9.6 bounds; SQLite declined, see docs/decisions/T33-memory-index-no-sqlite.md).
The index is fully DERIVED state over the append-only JSONL store:
- immutable ts-desc-sorted segment files of doc summaries
(
{"id","ts","tags","terms"}). Lexical terms are CONTENT-DERIVED — they reveal the vocabulary of record bodies, so index files and directories are private-moded (0600/0700) like the store itself, andsecret-sensitivity bodies contribute NO terms at all (metadata-only indexing: id, timestamp, tags); - an atomically renamed
manifest.jsonrecording the consumed store offset, segment metadata (with min/max timestamps for range skipping), and the tombstone set; - queries stream segment lines through a k-way ts-desc merge with a
result-bounded collector — resident memory is proportional to the
requested limit plus at most
MAX_SEGMENTSbuffered docs (staging compacts segments whenever they exceed that bound), never to the total match count, and streaming early-terminates once a page is full; - crash safety + integrity: torn store tails are healed by the store’s
append path and skipped by the lenient reader; orphaned
*.tmpfiles are ignored; the manifest records byte length AND sha256 per segment, and any unparseable manifest, offset mismatch, or missing/truncated/ corrupted segment triggers a VERIFIED full rebuild from the store (the index can always be deleted safely).
No network: this module performs filesystem I/O only. Embeddings are DISABLED — no embedding hook exists here, so no implicit remote call is possible (spec §9.6; local embeddings would be a separate ask-first opt-in).
Structs§
- Index
Hit - A matching document reference (bodies stay in the store; fetch by id).
- Index
Page - One bounded result page.
- Index
Query - Index query. Terms use token-exact AND semantics over the lexical term sets (see the T33 decision record for the trade-off vs FTS).
- Index
Search Stats - Observability for the proportional-memory bound.
- Index
Status - Status returned by
ensure_index_in. - Page
Cursor - Exclusive pagination cursor: results strictly AFTER this position in
(timestamp desc, id desc)order.
Constants§
- MAX_
SEGMENTS - Hard bound on staged segments (and therefore on open readers per query). Staging compacts everything above this into fresh globally ts-ordered segments.
- MAX_
TERMS_ PER_ DOC - Lexical terms retained per document (deduped, sorted).
- SEGMENT_
MAX_ DOCS - Docs per immutable segment file.
Functions§
- ensure_
index_ in - Stage the index up to the current end of the store: consume unindexed store bytes into new immutable segments and an atomically replaced manifest. Crash-safe at every step; rebuilds from scratch when the manifest is unparseable or the store shrank (rewritten by retention).
- index_
dir_ in - Per-project index directory:
<base>/memory/index/<namespace>/. - search_
index_ in - Search the staged index (auto-staging the tail first). Bounded page, newest-first, resident memory proportional to the limit.