mnemo_engine/store/crud.rs
1//! Public CRUD (remember, get, delete, embedder), and the handle caches (key slots, superseded set, thread layers).
2
3use super::*;
4
5/// R-004.2: where the thread layers and the onboarding briefing live,
6/// built by the handle's one lazy scan (`ensure_key_caches`, together with
7/// the 12.1 key caches) and then kept current by `put` and `delete`. It
8/// lets `about`, `essence`, `ingest_thread`, `thread_export` and
9/// thread-pinned recall read just their own records instead of decrypting
10/// the whole file each time.
11#[derive(Debug, Default)]
12pub(super) struct LayerCache {
13 /// Live essence item ids.
14 pub(super) essence: std::collections::HashSet<u128>,
15 /// Live ids tagged `metadata.area = "onboarding"` (superseded versions
16 /// included; `about` drops those when it reads them).
17 pub(super) onboarding: std::collections::HashSet<u128>,
18 /// Per `_mnemo.thread`: its records and archive state.
19 pub(super) threads: std::collections::HashMap<String, ThreadCache>,
20 /// Thread → (the thread it continues, the link record's id).
21 pub(super) links: std::collections::HashMap<String, (String, u128)>,
22 /// FMT-9e: on v9, thread-turn records the lazy scan didn't decode,
23 /// by their row's `thread_hash`. A thread's bucket is decoded into
24 /// `threads` the first time that thread is used
25 /// ([`Mnemo::warm_thread`]), so a cold handle never decodes (or
26 /// decompresses) the turns of threads it doesn't touch.
27 pub(super) cold_turns: std::collections::HashMap<u32, Vec<u128>>,
28}
29
30/// One thread's records (turns, essence items, link), the turn ids already
31/// archived (with how many records each has: long turns are chunked), and
32/// the highest `_mnemo.seq` seen.
33#[derive(Debug, Default)]
34pub(super) struct ThreadCache {
35 pub(super) records: std::collections::HashSet<u128>,
36 pub(super) turns: std::collections::HashMap<String, u32>,
37 pub(super) max_seq: Option<u64>,
38}
39
40pub(super) fn meta_str<'a>(m: &'a Memory, key: &str) -> Option<&'a str> {
41 m.metadata.get(key).and_then(|v| v.as_str())
42}
43
44pub(super) fn is_onboarding(m: &Memory) -> bool {
45 meta_str(m, "area").is_some_and(|s| s.eq_ignore_ascii_case("onboarding"))
46}
47
48impl LayerCache {
49 pub(super) fn add(&mut self, m: &Memory) {
50 let id = m.id.0;
51 let kind = meta_str(m, MNEMO_META_KIND);
52 if kind == Some(MNEMO_META_KIND_ESSENCE) {
53 self.essence.insert(id);
54 }
55 if is_onboarding(m) {
56 self.onboarding.insert(id);
57 }
58 let Some(thread) = meta_str(m, MNEMO_META_THREAD) else {
59 return;
60 };
61 let tc = self.threads.entry(thread.to_string()).or_default();
62 tc.records.insert(id);
63 if kind == Some(MNEMO_META_KIND_TURN) {
64 if let Some(turn_id) = meta_str(m, MNEMO_META_TURN_ID) {
65 *tc.turns.entry(turn_id.to_string()).or_insert(0) += 1;
66 }
67 if let Some(seq) = m.metadata.get(MNEMO_META_SEQ).and_then(|v| v.as_u64()) {
68 tc.max_seq = Some(tc.max_seq.map_or(seq, |s| s.max(seq)));
69 }
70 }
71 if kind == Some(MNEMO_META_KIND_THREAD_LINK) {
72 if let Some(from) = meta_str(m, MNEMO_META_CONTINUES) {
73 self.links
74 .entry(thread.to_string())
75 .or_insert_with(|| (from.to_string(), id));
76 }
77 }
78 }
79
80 /// Undo [`LayerCache::add`] for the stored version of a record that is
81 /// being overwritten or deleted. A thread with no records left is
82 /// dropped, so a forgotten thread's `_mnemo.seq` restarts at 0. Partial
83 /// deletes keep the old maximum (sequence gaps are harmless).
84 pub(super) fn remove(&mut self, m: &Memory) {
85 let id = m.id.0;
86 self.essence.remove(&id);
87 self.onboarding.remove(&id);
88 let Some(thread) = meta_str(m, MNEMO_META_THREAD) else {
89 return;
90 };
91 if self.links.get(thread).is_some_and(|(_, link)| *link == id) {
92 self.links.remove(thread);
93 }
94 let Some(tc) = self.threads.get_mut(thread) else {
95 return;
96 };
97 tc.records.remove(&id);
98 if meta_str(m, MNEMO_META_KIND) == Some(MNEMO_META_KIND_TURN) {
99 if let Some(turn_id) = meta_str(m, MNEMO_META_TURN_ID) {
100 if let Some(n) = tc.turns.get_mut(turn_id) {
101 *n -= 1;
102 if *n == 0 {
103 tc.turns.remove(turn_id);
104 }
105 }
106 }
107 }
108 if tc.records.is_empty() {
109 self.threads.remove(thread);
110 }
111 }
112}
113
114impl Mnemo {
115 /// Register a text embedder for this handle. Enables
116 /// [`Mnemo::remember_text`] and the text-side `RecallRequest`
117 /// convenience path. Does two checks before storing the embedder:
118 ///
119 /// 1. **Dimension check.** The embedder's `identity().dimensions`
120 /// must equal [`Mnemo::dimensions`] (the width this database
121 /// was created with). Mismatch returns
122 /// [`MnemoError::DimensionMismatch`].
123 /// 2. **Identity check (EMBED-ID-1).** Every file records the
124 /// identity (name, dimensions, normalize) of the first embedder
125 /// attached to it, in a reserved, encrypted record that isn't a
126 /// memory: `len`, `count`, `stats`, recall, `about` and `get`
127 /// never see it, and compaction, the rekeyed exports and the
128 /// passphrase import carry it. After that, any other identity,
129 /// even at the same width (another model, or another revision of
130 /// the same one), returns [`MnemoError::EmbedderMismatch`]: a
131 /// store is bound to the model that first wrote to it, so a new
132 /// model needs a new store.
133 ///
134 /// **Files with a manifest** (`mnemo init`, `seed.json`) also carry
135 /// the identity on the manifest at `metadata.embedder = { name,
136 /// dimensions, normalize }` (the legacy flat `embedder_name` /
137 /// `embedder_dimensions` / `embedder_normalize` keys are read as an
138 /// alias). The first registration upserts it there too, as before.
139 /// If the manifest and the record disagree, this returns
140 /// [`MnemoError::Invalid`] naming both.
141 ///
142 /// **Files from before EMBED-ID-1** have no record: the first
143 /// embedder attached after the upgrade is recorded. The check
144 /// protects from then on; it can't tell which model made the older
145 /// vectors.
146 ///
147 /// **Read-only handles.** `set_embedder` on an `open_read_only`
148 /// handle performs both checks and adopts the embedder in memory;
149 /// it never writes. Readers (CLI `--text`, agent-side `recall_text`)
150 /// can therefore use the text path next to a writer that holds
151 /// the exclusive lock.
152 ///
153 /// On every failure path the handle's embedder slot is left
154 /// unchanged. On success the embedder is wrapped in an `Arc<dyn
155 /// Embedder>` and consulted by every text-side call made through
156 /// this handle. The slot is per-handle and not persisted — a
157 /// fresh open must re-register (same identity, so the check
158 /// round-trips cleanly).
159 pub fn set_embedder<E: Embedder + 'static>(&mut self, embedder: E) -> Result<()> {
160 let identity = embedder.identity();
161 if identity.dimensions != self.dimensions {
162 return Err(MnemoError::DimensionMismatch {
163 expected: self.dimensions,
164 got: identity.dimensions,
165 });
166 }
167
168 let (onboarding, on_manifest, upsert_target, on_record) = self.embedder_records()?;
169
170 if let (Some(manifest), Some(record)) = (&on_manifest, &on_record) {
171 if manifest != record {
172 return Err(embed::identity_conflict_err(manifest, record));
173 }
174 }
175 if let Some(stored) = on_record.as_ref().or(on_manifest.as_ref()) {
176 if *stored != identity {
177 return Err(embed::identity_mismatch_err(stored, &identity));
178 }
179 }
180
181 // Record what's missing. A read-only handle writes nothing, but
182 // still adopts the embedder in memory.
183 if !self.read_only {
184 let mut wrote = false;
185 if on_manifest.is_none() {
186 if let Some(idx) = upsert_target {
187 let mut m = onboarding[idx].clone();
188 embed::write_identity_into_metadata(&mut m.metadata, &identity);
189 // Reuse the memory's existing id so this is an in-place
190 // update of the scaffold manifest, not a new entry.
191 self.put(m)?;
192 wrote = true;
193 }
194 }
195 if on_record.is_none() {
196 self.record_identity(&identity)?;
197 wrote = true;
198 }
199 if wrote {
200 self.flush()?;
201 }
202 }
203
204 self.embedder = Some(std::sync::Arc::new(embedder));
205 Ok(())
206 }
207
208 /// Where the file records an embedder identity (EMBED-ID-1): the
209 /// onboarding memories, the manifest's identity if it has one, the first
210 /// onboarding memory without one (the upsert target), and the reserved
211 /// record's identity.
212 #[allow(clippy::type_complexity)]
213 fn embedder_records(
214 &mut self,
215 ) -> Result<(
216 Vec<Memory>,
217 Option<embed::EmbedderIdentity>,
218 Option<usize>,
219 Option<embed::EmbedderIdentity>,
220 )> {
221 let onboarding = self.about()?;
222 let mut on_manifest: Option<embed::EmbedderIdentity> = None;
223 let mut upsert_target: Option<usize> = None;
224 for (i, m) in onboarding.iter().enumerate() {
225 if let Some(saw) = embed::read_identity_from_metadata(&m.metadata) {
226 on_manifest = Some(saw);
227 break;
228 }
229 if upsert_target.is_none() {
230 upsert_target = Some(i);
231 }
232 }
233 let on_record = self.recorded_identity()?;
234 Ok((onboarding, on_manifest, upsert_target, on_record))
235 }
236
237 /// Whether [`Mnemo::set_embedder`] with this identity would write to the
238 /// file (to record the identity where it's missing). A shared handle
239 /// attaches an already recorded embedder in a read window (RB-007.6).
240 pub(super) fn embedder_would_record(
241 &mut self,
242 identity: &embed::EmbedderIdentity,
243 ) -> Result<bool> {
244 if identity.dimensions != self.dimensions {
245 // `set_embedder` refuses it without writing.
246 return Ok(false);
247 }
248 let (_, on_manifest, upsert_target, on_record) = self.embedder_records()?;
249 Ok((on_manifest.is_none() && upsert_target.is_some()) || on_record.is_none())
250 }
251
252 /// The embedder identity this file records (EMBED-ID-1), or `None`
253 /// before the first `set_embedder` on a read-write handle. Reads the
254 /// reserved record without touching any counter.
255 pub fn embedder_identity(&mut self) -> Result<Option<embed::EmbedderIdentity>> {
256 self.recorded_identity()
257 }
258
259 /// Borrow the currently-registered embedder, if any. Returns
260 /// `None` when no embedder has been set via
261 /// [`Mnemo::set_embedder`]. Bindings use this to drive their own
262 /// text-side convenience methods without duplicating the identity
263 /// check.
264 pub fn embedder(&self) -> Option<&dyn Embedder> {
265 self.embedder.as_deref()
266 }
267
268 /// Shared handle to the registered embedder, cloned so the caller
269 /// can drop the borrow on this `Mnemo` before invoking `embed()`.
270 /// FIX-1 / PY-1 (R-001.11): the Python binding needs this so an
271 /// embedder callback that re-enters `Mnemo` (e.g. a Python
272 /// embedder that calls `db.dimensions()` or `db.cache_get(...)`)
273 /// doesn't hit PyO3's "Already borrowed" — we clone the Arc under
274 /// a short `&self` borrow, release that borrow, then invoke
275 /// `embed()` with no `&mut Mnemo` held.
276 pub fn embedder_arc(&self) -> Option<std::sync::Arc<dyn Embedder>> {
277 self.embedder.clone()
278 }
279
280 /// Embed `content` with the registered embedder and `remember` the
281 /// result. Returns the new memory's ULID. Convenience path for
282 /// callers who have a string, want it stored, and don't need any
283 /// of the per-memory options [`Memory`] exposes (agent, importance,
284 /// metadata, ttl, scope) — build a [`Memory`] by hand and call
285 /// [`Mnemo::remember`] for those.
286 ///
287 /// Fails with [`MnemoError::Invalid`] if no embedder has been
288 /// registered on this handle (call [`Mnemo::set_embedder`] first);
289 /// with [`MnemoError::DimensionMismatch`] if the embedder returned
290 /// the wrong width for this database.
291 pub fn remember_text(&mut self, content: &str, memory_type: MemoryType) -> Result<Ulid> {
292 if self.read_only {
293 return Err(MnemoError::ReadOnly);
294 }
295 let embedder = self.embedder.as_ref().ok_or_else(|| {
296 MnemoError::Invalid(
297 "no embedder registered on this handle; call set_embedder first".into(),
298 )
299 })?;
300 let mut vectors = embedder.embed(&[content])?;
301 let vector = vectors.pop().ok_or_else(|| {
302 MnemoError::Invalid("embedder returned no vector for the input text".into())
303 })?;
304 if vector.len() != self.dimensions {
305 return Err(MnemoError::DimensionMismatch {
306 expected: self.dimensions,
307 got: vector.len(),
308 });
309 }
310 let memory = Memory::new(content.to_string(), memory_type, vector);
311 self.remember(memory)
312 }
313
314 /// Store a memory. Returns its ULID. If the memory's ID is nil a fresh
315 /// one is assigned; an existing ID overwrites in place.
316 ///
317 /// Metadata keys starting with `_mnemo.` are the engine's (supersession,
318 /// keys, threads, kinds): a new memory carrying one is refused with
319 /// [`MnemoError::Invalid`]. An in-place overwrite may carry the stored
320 /// record's own `_mnemo.*` keys through unchanged (the usual `get`,
321 /// change a field, `remember`), but can't add, change or drop one.
322 pub fn remember(&mut self, memory: Memory) -> Result<Ulid> {
323 if self.read_only {
324 return Err(MnemoError::ReadOnly);
325 }
326 self.check_caller_metadata(&memory)?;
327 self.put(memory)
328 }
329
330 /// RB-001.24: the caller's metadata may not set engine state. For a
331 /// memory with a nil or unknown id, any `_mnemo.*` key is refused; for
332 /// an overwrite in place, the `_mnemo.*` keys must equal the stored
333 /// record's.
334 pub(super) fn check_caller_metadata(&mut self, memory: &Memory) -> Result<()> {
335 let reserved: Vec<(&String, &Value)> = memory
336 .metadata
337 .iter()
338 .filter(|(k, _)| k.starts_with(crate::memory::MNEMO_META_NS))
339 .collect();
340 if reserved.is_empty() {
341 return Ok(());
342 }
343 let stored = if memory.id.is_nil() {
344 None
345 } else {
346 match self.index.get(memory.id.0, &self.catalog) {
347 Some(idx) if !self.catalog[idx].deleted => {
348 let entry = self.catalog[idx].clone();
349 Some(self.read_memory(&entry)?)
350 }
351 _ => None,
352 }
353 };
354 if let Some(old) = stored {
355 let old_reserved: Vec<(&String, &Value)> = old
356 .metadata
357 .iter()
358 .filter(|(k, _)| k.starts_with(crate::memory::MNEMO_META_NS))
359 .collect();
360 if old_reserved == reserved {
361 return Ok(());
362 }
363 }
364 Err(reserved_key_error(reserved[0].0))
365 }
366
367 /// Fetch a memory by ID.
368 pub fn get(&mut self, id: &Ulid) -> Result<Memory> {
369 let idx = self
370 .index
371 .get(id.0, &self.catalog)
372 .ok_or_else(|| MnemoError::NotFound(id.to_string()))?;
373 let entry = self.catalog[idx].clone();
374 if entry.deleted {
375 return Err(MnemoError::NotFound(id.to_string()));
376 }
377 self.read_memory(&entry)
378 }
379
380 /// Soft-delete a memory (tombstoned; space reclaimed by `compact`).
381 pub fn delete(&mut self, id: &Ulid) -> Result<()> {
382 if self.read_only {
383 return Err(MnemoError::ReadOnly);
384 }
385 let idx = self
386 .index
387 .get(id.0, &self.catalog)
388 .ok_or_else(|| MnemoError::NotFound(id.to_string()))?;
389 // R-004.2 / 12.1: with the handle caches built, read the record
390 // being deleted (one decrypt) so the key slot, superseded set and
391 // thread layers drop exactly what it contributed.
392 let entry = self.catalog[idx].clone();
393 if self.layers.is_some() && !entry.deleted {
394 let old = self.read_memory(&entry)?;
395 self.note_removed(&old)?;
396 }
397 if !self.catalog[idx].deleted {
398 self.catalog[idx].deleted = true;
399 self.dirty_catalog = true;
400 self.touch(idx);
401 // FMT-9f: drop its document from the stats in place.
402 if let (Some(stats), Some(r)) = (self.bm25.stats.as_mut(), entry.v9) {
403 if r.doc != 0 {
404 stats.remove(r.doc);
405 }
406 }
407 if self.is_v9() {
408 self.kill_record(idx, &entry);
409 self.kill_vector(idx, &entry);
410 } else {
411 self.release_pages(entry.start_page, entry.page_count as u64);
412 }
413 }
414 if let Some(ann) = &mut self.ann {
415 if ann.contains(id.0) {
416 ann.remove(id.0);
417 self.dirty_index = true;
418 self.index_persist.changes += 1;
419 }
420 }
421 // FIX-1 R-001.8 part 2: we can't tell whether this delete
422 // removed the last memory of its agent without a decrypt
423 // (the catalog entry doesn't carry `agent_id` in format v8),
424 // so just invalidate the lazy set. The next `stats()` call
425 // rebuilds from live memories.
426 self.agent_set = None;
427 // Phase 12.1: delete of a keyed record removes it from the slot,
428 // and delete of a superseded record frees a `history` entry. When
429 // the caches are built, `note_removed` above did that exactly;
430 // otherwise they're built from scratch on first use anyway.
431 Ok(())
432 }
433
434 /// Keep the handle caches (12.1 key slots, superseded set, R-004.2
435 /// layers) current after `put` writes `m`. No-op until they're built.
436 /// FMT-9e: a cold thread is warmed first, so its layer entry is whole.
437 pub(super) fn note_added(&mut self, m: &Memory) -> Result<()> {
438 if let Some(thread) = meta_str(m, MNEMO_META_THREAD) {
439 self.warm_thread(thread)?;
440 }
441 let id = m.id.0;
442 if let (Some(keys), Some(sup)) = (self.key_index.as_mut(), self.superseded.as_mut()) {
443 if m.metadata.contains_key(MNEMO_META_SUPERSEDED_BY) {
444 sup.insert(id);
445 } else if let Some(Value::String(k)) = m.metadata.get(MNEMO_META_KEY) {
446 keys.insert(Self::key_slot_for(m, k), id);
447 }
448 }
449 if let Some(layers) = self.layers.as_mut() {
450 layers.add(m);
451 }
452 Ok(())
453 }
454
455 /// Undo [`Mnemo::note_added`] for the stored version of a record that's
456 /// about to be overwritten or deleted.
457 pub(super) fn note_removed(&mut self, m: &Memory) -> Result<()> {
458 if let Some(thread) = meta_str(m, MNEMO_META_THREAD) {
459 self.warm_thread(thread)?;
460 }
461 let id = m.id.0;
462 if let (Some(keys), Some(sup)) = (self.key_index.as_mut(), self.superseded.as_mut()) {
463 sup.remove(&id);
464 if let Some(Value::String(k)) = m.metadata.get(MNEMO_META_KEY) {
465 let slot = Self::key_slot_for(m, k);
466 if keys.get(&slot) == Some(&id) {
467 keys.remove(&slot);
468 }
469 }
470 }
471 if let Some(layers) = self.layers.as_mut() {
472 layers.remove(m);
473 }
474 Ok(())
475 }
476
477 /// FMT-9e: decode `thread`'s cold turn records (see
478 /// [`LayerCache::cold_turns`]) into the layer cache. Every record in
479 /// the thread's hash bucket is decoded and filed under its own thread,
480 /// so a hash collision just warms both threads. A no-op when the
481 /// caches aren't built or the bucket is already warm.
482 pub(super) fn warm_thread(&mut self, thread: &str) -> Result<()> {
483 self.warm_thread_decoded(thread).map(|_| ())
484 }
485
486 /// [`Mnemo::warm_thread`], returning the records it decoded (without
487 /// vectors), so a caller about to read the thread decodes each once.
488 pub(super) fn warm_thread_decoded(
489 &mut self,
490 thread: &str,
491 ) -> Result<std::collections::HashMap<u128, Memory>> {
492 let mut decoded = std::collections::HashMap::new();
493 let h = fnv1a32(&[thread.as_bytes()]);
494 let Some(ids) = self.layers.as_mut().and_then(|l| l.cold_turns.remove(&h)) else {
495 return Ok(decoded);
496 };
497 for e in self.live_entries(ids) {
498 let m = self.read_memory_meta(&e)?;
499 if let Some(layers) = self.layers.as_mut() {
500 layers.add(&m);
501 }
502 decoded.insert(e.id, m);
503 }
504 Ok(decoded)
505 }
506
507 /// Decode a record's memory without its vector or live access stats:
508 /// what the handle caches need (metadata, scope, agent), without the
509 /// v9 vector-column read [`Mnemo::read_memory`] adds.
510 pub(super) fn read_memory_meta(&mut self, e: &CatalogEntry) -> Result<Memory> {
511 let bytes = self.read_record(e)?;
512 self.decode_memory(&bytes)
513 }
514
515 /// The live catalog entries for `ids` (ids that are gone are skipped).
516 pub(super) fn live_entries(&self, ids: impl IntoIterator<Item = u128>) -> Vec<CatalogEntry> {
517 ids.into_iter()
518 .filter_map(|id| {
519 self.index
520 .get(id, &self.catalog)
521 .map(|i| self.catalog[i].clone())
522 })
523 .filter(|e| !e.deleted)
524 .collect()
525 }
526
527 /// Catalog entries of the essence items, from the handle caches.
528 pub(super) fn essence_entries(&mut self) -> Result<Vec<CatalogEntry>> {
529 self.ensure_key_caches()?;
530 let ids: Vec<u128> = self
531 .layers
532 .as_ref()
533 .map(|l| l.essence.iter().copied().collect())
534 .unwrap_or_default();
535 Ok(self.live_entries(ids))
536 }
537
538 /// Catalog entries of every record filed under `thread`, from the
539 /// handle caches.
540 pub(super) fn thread_entries(&mut self, thread: &str) -> Result<Vec<CatalogEntry>> {
541 Ok(self.thread_entries_decoded(thread)?.0)
542 }
543
544 /// [`Mnemo::thread_entries`], plus the records that warming a cold
545 /// thread just decoded (see [`Mnemo::warm_thread_decoded`]).
546 pub(super) fn thread_entries_decoded(
547 &mut self,
548 thread: &str,
549 ) -> Result<(Vec<CatalogEntry>, std::collections::HashMap<u128, Memory>)> {
550 self.ensure_key_caches()?;
551 let decoded = self.warm_thread_decoded(thread)?;
552 let ids: Vec<u128> = self
553 .layers
554 .as_ref()
555 .and_then(|l| l.threads.get(thread))
556 .map(|tc| tc.records.iter().copied().collect())
557 .unwrap_or_default();
558 Ok((self.live_entries(ids), decoded))
559 }
560
561 // --- Phase 12.1: edit in place + keyed supersession -----------------
562
563 /// Scope-aware key-slot identifier for `memory` and a caller key.
564 ///
565 /// * [`Scope::Private`] → owned by the memory's `agent_id`.
566 /// * [`Scope::Shared`] → one slot across the whole file.
567 pub(super) fn key_slot_for(memory: &Memory, key: &str) -> crate::store::KeySlot {
568 let scope_owner = match memory.scope {
569 Scope::Private => Some(memory.agent_id.clone()),
570 Scope::Shared => None,
571 };
572 crate::store::KeySlot {
573 scope_owner,
574 key: key.to_string(),
575 }
576 }
577
578 /// The handle's one lazy scan: builds the 12.1 `key_index` +
579 /// `superseded` caches and the R-004.2 layer cache (essence ids,
580 /// onboarding ids, threads, links) from live records, and prunes
581 /// non-fact ids from a loaded ANN index (files written before v0.10.0
582 /// indexed every kind). Decrypts every live record once per handle;
583 /// `put` and `delete` keep the result current after that.
584 pub(super) fn ensure_key_caches(&mut self) -> Result<()> {
585 if self.key_index.is_some() && self.superseded.is_some() && self.layers.is_some() {
586 return Ok(());
587 }
588 let entries: Vec<CatalogEntry> = self
589 .catalog
590 .iter()
591 .filter(|e| !e.deleted)
592 .cloned()
593 .collect();
594 let mut keys: std::collections::HashMap<crate::store::KeySlot, u128> =
595 std::collections::HashMap::new();
596 let mut superseded: std::collections::HashSet<u128> = std::collections::HashSet::new();
597 let mut layers = LayerCache::default();
598 let mut non_facts: Vec<u128> = Vec::new();
599 for e in &entries {
600 // FMT-9e: a v9 turn row with no key slot and no supersession
601 // contributes only to its thread's layer entry, so it's left
602 // undecoded in the thread's cold bucket (most of a
603 // thread-heavy file). Its row says it isn't a fact.
604 if let Some(r) = e.v9 {
605 if r.kind == ROW_KIND_TURN
606 && r.thread_hash != 0
607 && r.key_hash == 0
608 && r.flags & ROW_SUPERSEDED == 0
609 {
610 layers
611 .cold_turns
612 .entry(r.thread_hash)
613 .or_default()
614 .push(e.id);
615 non_facts.push(e.id);
616 continue;
617 }
618 }
619 let m = self.read_memory_meta(e)?;
620 layers.add(&m);
621 if !is_ann_kind(&m) {
622 non_facts.push(e.id);
623 }
624 if m.metadata.get(MNEMO_META_SUPERSEDED_BY).is_some() {
625 superseded.insert(e.id);
626 continue;
627 }
628 if let Some(Value::String(k)) = m.metadata.get(MNEMO_META_KEY) {
629 let slot = Self::key_slot_for(&m, k);
630 // Catalog order is insertion order; a later live record
631 // with the same slot would only exist if a prior build
632 // of this code allowed it. Keep the later id (newer
633 // ULID), in case of a tie.
634 keys.entry(slot)
635 .and_modify(|cur| {
636 if *cur < e.id {
637 *cur = e.id;
638 }
639 })
640 .or_insert(e.id);
641 }
642 }
643 self.key_index = Some(keys);
644 self.superseded = Some(superseded);
645 self.layers = Some(layers);
646 if let Some(ann) = &mut self.ann {
647 let mut pruned = 0usize;
648 for id in non_facts {
649 if ann.contains(id) {
650 ann.remove(id);
651 pruned += 1;
652 }
653 }
654 if pruned > 0 {
655 self.dirty_index = true;
656 self.index_persist.changes += pruned;
657 }
658 }
659 Ok(())
660 }
661}
662
663/// RB-001.24: the error for caller metadata that sets an engine key.
664pub(super) fn reserved_key_error(key: &str) -> MnemoError {
665 MnemoError::Invalid(format!(
666 "metadata key \"{key}\" is reserved: keys starting with \"{}\" are set by the engine",
667 crate::memory::MNEMO_META_NS
668 ))
669}