1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//! Phase 12.1: keyed upserts, edit in place (update), and supersession history.
use super::*;
impl Mnemo {
/// Stage-identical check for a 12.1 keyed no-op. The incoming write
/// is byte-equal to the live record iff every user-visible field
/// (content, type, importance, vector, user metadata) matches. The
/// `_mnemo.*` reserved keys on the live record are **ignored** for
/// this comparison — a repeat of an identical write re-issued by an
/// extractor must be a NOOP even if the live record already carries
/// supersession bookkeeping from a prior op.
pub(super) fn is_identical_keyed_write(incoming: &Memory, live: &Memory) -> bool {
if incoming.content != live.content {
return false;
}
if incoming.memory_type != live.memory_type {
return false;
}
if (incoming.importance - live.importance).abs() > f32::EPSILON {
return false;
}
if incoming.vector != live.vector {
return false;
}
// Strip `_mnemo.*` from both sides before comparing user metadata.
let strip = |m: &Map<String, Value>| -> Map<String, Value> {
m.iter()
.filter(|(k, _)| !k.starts_with(crate::memory::MNEMO_META_NS))
.map(|(k, v)| (k.clone(), v.clone()))
.collect()
};
strip(&incoming.metadata) == strip(&live.metadata)
}
/// Store a memory under a keyed upsert slot. Returns the live record's
/// id and a [`WriteOutcome`] saying whether this write created a new
/// slot (`Added`), replaced a prior record (`Superseded`), or matched
/// the existing live record byte-for-byte (`Unchanged`, no file write).
///
/// The slot is identified by `(scope_owner, key)`:
/// * `Scope::Private` memories share a slot per `(agent_id, key)`.
/// * `Scope::Shared` memories share one slot per `key` across the file.
///
/// On supersession the prior record is **not** deleted — it remains
/// retrievable through [`Mnemo::history`] — but it is excluded from
/// default [`Mnemo::recall`] and [`Mnemo::about`]. A caller that wants
/// the full lineage of a key walks `history(key)`.
///
/// This is the atomic 12.1 "supersede-or-noop" write path. Plain
/// [`Mnemo::remember`] without a key is unchanged.
pub fn remember_keyed(
&mut self,
mut memory: Memory,
key: &str,
) -> Result<(Ulid, WriteOutcome)> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
if key.is_empty() {
return Err(MnemoError::Invalid(
"remember_keyed: 'key' must not be empty".into(),
));
}
self.ensure_key_caches()?;
let slot = Self::key_slot_for(&memory, key);
let existing = self.key_index.as_ref().and_then(|k| k.get(&slot).copied());
// Caller may already have set `_mnemo.key` on the memory; rewrite
// it to our canonical value regardless so the record is
// self-identifying.
memory
.metadata
.insert(MNEMO_META_KEY.into(), Value::String(key.to_string()));
// NOOP check: if a live record exists for this slot and its
// user-visible fields match, return the existing id without a
// write.
if let Some(prev_id_u) = existing {
let prev_idx = self
.index
.get(prev_id_u, &self.catalog)
.ok_or_else(|| MnemoError::Invalid("key_index drift".into()))?;
let prev_entry = self.catalog[prev_idx].clone();
let prev_mem = self.read_memory(&prev_entry)?;
if Self::is_identical_keyed_write(&memory, &prev_mem) {
return Ok((Ulid(prev_id_u), WriteOutcome::Unchanged));
}
// SUPERSESSION: insert the new record, then rewrite the old
// with `_mnemo.superseded_by` + `_mnemo.invalid_at`.
let now = memory::now_secs();
memory.id = Ulid::nil(); // force a fresh ULID in `put`
memory.metadata.insert(
MNEMO_META_SUPERSEDES.into(),
Value::String(Ulid(prev_id_u).to_string()),
);
let new_id = self.put(memory)?;
// Mark the prior record.
let mut prev_mut = prev_mem;
prev_mut.metadata.insert(
MNEMO_META_SUPERSEDED_BY.into(),
Value::String(new_id.to_string()),
);
prev_mut
.metadata
.insert(MNEMO_META_INVALID_AT.into(), Value::Number(now.into()));
// keep prev_mut.id so `put` overwrites in place.
self.put(prev_mut)?;
// Update caches incrementally.
if let Some(k) = self.key_index.as_mut() {
k.insert(slot, new_id.0);
}
if let Some(s) = self.superseded.as_mut() {
s.insert(prev_id_u);
}
return Ok((new_id, WriteOutcome::Superseded));
}
// ADDED: no prior record for this slot.
memory.id = Ulid::nil();
let new_id = self.put(memory)?;
if let Some(k) = self.key_index.as_mut() {
k.insert(slot, new_id.0);
}
Ok((new_id, WriteOutcome::Added))
}
/// Text-side keyed write: embed `content` through the registered
/// embedder, then [`Mnemo::remember_keyed`]. Same returns.
pub fn remember_keyed_text(
&mut self,
content: &str,
memory_type: MemoryType,
key: &str,
) -> Result<(Ulid, WriteOutcome)> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let embedder = self.embedder.as_ref().ok_or_else(|| {
MnemoError::Invalid(
"no embedder registered on this handle; call set_embedder first".into(),
)
})?;
let mut vectors = embedder.embed(&[content])?;
let vector = vectors.pop().ok_or_else(|| {
MnemoError::Invalid("embedder returned no vector for the input text".into())
})?;
if vector.len() != self.dimensions {
return Err(MnemoError::DimensionMismatch {
expected: self.dimensions,
got: vector.len(),
});
}
let memory = Memory::new(content.to_string(), memory_type, vector);
self.remember_keyed(memory, key)
}
/// Update an existing memory in place, preserving its ULID,
/// `created_at` and access stats. Returns [`WriteOutcome::Updated`]
/// on a real change, [`WriteOutcome::Unchanged`] when every field in
/// the patch already matches the live record.
///
/// Reserved `_mnemo.*` metadata keys on the existing record are
/// preserved across a `metadata` replacement; a `metadata` of
/// `Some(map)` replaces every **user-visible** key with `map`'s
/// entries. A `content` change requires a matching `vector`
/// (use [`Mnemo::update_text`] to re-embed) — a bare `content` patch
/// is rejected to stop text and embedding drifting apart silently.
pub fn update(&mut self, id: &Ulid, patch: MemoryPatch) -> Result<WriteOutcome> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
if patch.content.is_some() && patch.vector.is_none() {
return Err(MnemoError::Invalid(
"update: a content change needs a matching vector (or call update_text \
to re-embed through the registered embedder)"
.into(),
));
}
let idx = self
.index
.get(id.0, &self.catalog)
.ok_or_else(|| MnemoError::NotFound(id.to_string()))?;
if self.catalog[idx].deleted {
return Err(MnemoError::NotFound(id.to_string()));
}
let entry = self.catalog[idx].clone();
let existing = self.read_memory(&entry)?;
// Apply patch into a candidate, detect no-op after the fact.
let mut cand = existing.clone();
if let Some(c) = &patch.content {
cand.content = c.clone();
}
if let Some(v) = &patch.vector {
if v.len() != self.dimensions {
return Err(MnemoError::DimensionMismatch {
expected: self.dimensions,
got: v.len(),
});
}
cand.vector = v.clone();
}
if let Some(i) = patch.importance {
cand.importance = i.clamp(0.0, 1.0);
}
if let Some(mt) = patch.memory_type {
cand.memory_type = mt;
}
if let Some(new_meta) = patch.metadata {
// Keep reserved `_mnemo.*` keys from the existing record.
let reserved: Vec<(String, Value)> = existing
.metadata
.iter()
.filter(|(k, _)| k.starts_with(crate::memory::MNEMO_META_NS))
.map(|(k, v)| (k.clone(), v.clone()))
.collect();
cand.metadata = new_meta;
for (k, v) in reserved {
cand.metadata.insert(k, v);
}
}
if let Some(ttl) = patch.ttl_secs {
cand.ttl_secs = ttl;
}
// No-op detection: compare all user-visible fields.
if cand.content == existing.content
&& cand.vector == existing.vector
&& (cand.importance - existing.importance).abs() <= f32::EPSILON
&& cand.memory_type == existing.memory_type
&& cand.metadata == existing.metadata
&& cand.ttl_secs == existing.ttl_secs
{
return Ok(WriteOutcome::Unchanged);
}
// Stamp `_mnemo.updated_at` and preserve id + created_at.
let now = memory::now_secs();
cand.metadata
.insert(MNEMO_META_UPDATED_AT.into(), Value::Number(now.into()));
cand.id = existing.id;
cand.created_at = existing.created_at;
// put() preserves accessed_at/access_count via the index lookup.
self.put(cand)?;
// `put` kept the key slot and the other handle caches current.
Ok(WriteOutcome::Updated)
}
/// In-place update that re-embeds `new_text` through the registered
/// embedder. Shorthand for `update(id, patch.with_content_and_vector)`.
pub fn update_text(
&mut self,
id: &Ulid,
new_text: &str,
mut patch: MemoryPatch,
) -> Result<WriteOutcome> {
if self.read_only {
return Err(MnemoError::ReadOnly);
}
let embedder = self.embedder.as_ref().ok_or_else(|| {
MnemoError::Invalid(
"no embedder registered on this handle; call set_embedder first".into(),
)
})?;
let mut vectors = embedder.embed(&[new_text])?;
let vector = vectors.pop().ok_or_else(|| {
MnemoError::Invalid("embedder returned no vector for the input text".into())
})?;
if vector.len() != self.dimensions {
return Err(MnemoError::DimensionMismatch {
expected: self.dimensions,
got: vector.len(),
});
}
patch.content = Some(new_text.to_string());
patch.vector = Some(vector);
self.update(id, patch)
}
/// Walk the full chain for a keyed slot: every record this key has
/// ever held, newest first. Entry 0 is the **live** record (unless
/// the slot is empty, in which case the vector is empty); subsequent
/// entries are the superseded history in reverse-supersession order.
///
/// **Slot resolution (v0.8.1 bugfix, lane A).** The lookup slot
/// matches a keyed `remember` with the same arguments, so the
/// obvious `history(key, None)` walks the private slot of the
/// default agent — the same slot
/// `remember_keyed(Memory::new(...), key)` writes to when the
/// caller omits an explicit `agent_id` / `scope`. Pre-v0.8.1
/// `agent_id = None` incorrectly looked up the shared slot and
/// returned an empty chain for the common case; the old behavior
/// is still reachable via `history_in(key, KeyScope::Shared)`.
///
/// * `agent_id = Some(a)` → private slot for that agent.
/// * `agent_id = None` → private slot for the default agent (same
/// as [`KeyScope::private_default`]).
///
/// Use [`Mnemo::history_in`] with [`KeyScope::Shared`] for the
/// shared slot.
pub fn history(&mut self, key: &str, agent_id: Option<&str>) -> Result<Vec<Memory>> {
let scope = match agent_id {
Some(a) => KeyScope::Private(a.to_string()),
None => KeyScope::private_default(),
};
self.history_in(key, scope)
}
/// Walk the full chain for a keyed slot identified by an explicit
/// [`KeyScope`]. Returns the same shape as [`Mnemo::history`] —
/// live record first, then the superseded chain newest-first.
///
/// Use this when you need the **shared** slot (`KeyScope::Shared`),
/// which `history(key, None)` no longer reaches as of v0.8.1.
pub fn history_in(&mut self, key: &str, scope: KeyScope) -> Result<Vec<Memory>> {
self.ensure_key_caches()?;
let slot = crate::store::KeySlot {
scope_owner: match scope {
KeyScope::Private(a) => Some(a),
KeyScope::Shared => None,
},
key: key.to_string(),
};
let live_id = self.key_index.as_ref().and_then(|k| k.get(&slot).copied());
let mut out = Vec::new();
let mut cursor = live_id;
while let Some(id_u) = cursor {
let idx = self.index.get(id_u, &self.catalog).ok_or_else(|| {
MnemoError::Invalid("history: superseded chain references missing id".into())
})?;
let entry = self.catalog[idx].clone();
if entry.deleted {
break;
}
let m = self.read_memory(&entry)?;
let next = m
.metadata
.get(MNEMO_META_SUPERSEDES)
.and_then(|v| v.as_str())
.and_then(|s| Ulid::from_string(s).ok())
.map(|u| u.0);
out.push(m);
cursor = next;
}
Ok(out)
}
// --- Phase 12.7a: thread archive --------------------------------
}