mnem-core 0.1.7

Content-addressed versioned substrate for AI agent memory - the core of mnem.
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//! E2E agent-flow integration test for the 0.3 agent-support surface.
//!
//! Exercises the three primitives shipped in this track as a cohesive
//! workflow:
//!
//! - [`Transaction::commit_memory`] - ergonomic node write with
//!   auto-stamped temporal metadata.
//! - [`Retriever::where_created_after`] /
//!   [`Retriever::where_created_before`] - temporal-range gate on the
//!   reserved props.
//! - [`Transaction::tombstone_node`] +
//!   [`Retriever::include_tombstoned`] - forget / audit semantics.
//! - [`ReadonlyRepo::incoming_edges`] - dual-adjacency back-index
//!   lookup.
//!
//! These tests pin the contracts an agent would care about if it were
//! writing memory via `commit_memory`, reading via a filtered
//! `retrieve`, and later revoking or chasing provenance. Unit coverage
//! of each primitive lives on its own module; this file is the
//! stitched-together happy path plus the obvious forget / range-filter
//! fail modes.
//!
//! [`Transaction::commit_memory`]: mnem_core::repo::Transaction::commit_memory
//! [`Retriever::where_created_after`]: mnem_core::retrieve::Retriever::where_created_after
//! [`Retriever::where_created_before`]: mnem_core::retrieve::Retriever::where_created_before
//! [`Transaction::tombstone_node`]: mnem_core::repo::Transaction::tombstone_node
//! [`Retriever::include_tombstoned`]: mnem_core::retrieve::Retriever::include_tombstoned
//! [`ReadonlyRepo::incoming_edges`]: mnem_core::repo::ReadonlyRepo::incoming_edges

use std::sync::Arc;

use ipld_core::ipld::Ipld;

use mnem_core::id::{EdgeId, NodeId};
use mnem_core::objects::Edge;
use mnem_core::repo::ReadonlyRepo;
use mnem_core::store::{Blockstore, MemoryBlockstore, MemoryOpHeadsStore, OpHeadsStore};

fn fresh_repo() -> ReadonlyRepo {
    let bs: Arc<dyn Blockstore> = Arc::new(MemoryBlockstore::new());
    let ohs: Arc<dyn OpHeadsStore> = Arc::new(MemoryOpHeadsStore::new());
    ReadonlyRepo::init(bs, ohs).expect("fresh repo init")
}

#[test]
fn commit_memory_writes_node_with_auto_stamped_temporal_props() {
    // Contract: `commit_memory` returns a fresh NodeId, stores the
    // node under the given ntype + summary, and auto-stamps
    // `mnem:created_at` / `mnem:updated_at` with a positive
    // microseconds-since-epoch integer.
    let repo = fresh_repo();

    let mut tx = repo.start_transaction();
    let id = tx
        .commit_memory(
            "Note",
            "morning meeting with alice",
            [("topic".to_string(), Ipld::String("ops".into()))],
        )
        .expect("commit_memory ok");
    let repo = tx.commit("agent", "note").expect("commit ok");

    let node = repo
        .lookup_node(&id)
        .expect("lookup ok")
        .expect("node present");
    assert_eq!(node.ntype, "Note");
    assert_eq!(node.summary.as_deref(), Some("morning meeting with alice"));
    assert_eq!(
        node.props.get("topic"),
        Some(&Ipld::String("ops".into())),
        "caller-supplied prop must round-trip"
    );
    let created = match node.props.get("mnem:created_at") {
        Some(Ipld::Integer(n)) => *n,
        other => panic!("expected Integer for mnem:created_at, got {other:?}"),
    };
    let updated = match node.props.get("mnem:updated_at") {
        Some(Ipld::Integer(n)) => *n,
        other => panic!("expected Integer for mnem:updated_at, got {other:?}"),
    };
    assert!(
        created > 0 && updated > 0,
        "temporal stamps must be positive (got created={created} updated={updated})"
    );
    assert_eq!(
        created, updated,
        "on first write, created_at and updated_at coincide"
    );
}

#[test]
fn retrieve_surfaces_commit_memory_node_without_filter() {
    // Contract: a node written via commit_memory is visible through
    // the standard retrieve path (label gate) without any temporal
    // filter configured.
    let repo = fresh_repo();
    let mut tx = repo.start_transaction();
    let id = tx
        .commit_memory(
            "Note",
            "morning meeting with alice",
            [("topic".to_string(), Ipld::String("ops".into()))],
        )
        .unwrap();
    let repo = tx.commit("agent", "note").unwrap();

    let result = repo
        .retrieve()
        .label("Note")
        .execute()
        .expect("retrieve ok");
    let ids: Vec<_> = result.items.iter().map(|i| i.node.id).collect();
    assert!(
        ids.contains(&id),
        "unfiltered retrieve must surface the committed node, got ids={ids:?}"
    );
}

#[test]
fn temporal_filter_excludes_future_bound_past() {
    // Contract: `where_created_before(t_past)` with `t_past = 1`
    // (microsecond 1, effectively "before the epoch began for this
    // test") drops every node that has a real `mnem:created_at`.
    let repo = fresh_repo();
    let mut tx = repo.start_transaction();
    let id = tx
        .commit_memory(
            "Note",
            "morning meeting with alice",
            [("topic".to_string(), Ipld::String("ops".into()))],
        )
        .unwrap();
    let repo = tx.commit("agent", "note").unwrap();

    // created_before(1) is exclusive: every real-epoch timestamp is
    // >= 1, so the filter excludes every node with a stamp.
    let result = repo
        .retrieve()
        .label("Note")
        .where_created_before(1)
        .execute()
        .expect("retrieve ok");
    let ids: Vec<_> = result.items.iter().map(|i| i.node.id).collect();
    assert!(
        !ids.contains(&id),
        "where_created_before(past_t) must exclude the node, got ids={ids:?}"
    );
}

#[test]
fn temporal_filter_includes_anything_after_zero() {
    // Contract: `where_created_after(0)` inclusive lower bound at
    // zero surfaces every node that has any non-negative stamp (so,
    // every node `commit_memory` has written). This is the
    // "everything since the beginning of time" shape agents use for
    // unbounded-lower queries.
    let repo = fresh_repo();
    let mut tx = repo.start_transaction();
    let id = tx
        .commit_memory(
            "Note",
            "morning meeting with alice",
            [("topic".to_string(), Ipld::String("ops".into()))],
        )
        .unwrap();
    let repo = tx.commit("agent", "note").unwrap();

    let result = repo
        .retrieve()
        .label("Note")
        .where_created_after(0)
        .execute()
        .expect("retrieve ok");
    let ids: Vec<_> = result.items.iter().map(|i| i.node.id).collect();
    assert!(
        ids.contains(&id),
        "where_created_after(0) must surface the node, got ids={ids:?}"
    );
}

#[test]
fn tombstone_excludes_node_and_include_tombstoned_surfaces_it() {
    // Contract: `tombstone_node` + commit drops the node from a
    // default retrieve. `include_tombstoned(true)` restores it for
    // audit / debug.
    let repo = fresh_repo();
    let mut tx = repo.start_transaction();
    let id = tx
        .commit_memory(
            "Note",
            "morning meeting with alice",
            [("topic".to_string(), Ipld::String("ops".into()))],
        )
        .unwrap();
    let repo = tx.commit("agent", "note").unwrap();

    let mut tx2 = repo.start_transaction();
    tx2.tombstone_node(id, "user revoked").unwrap();
    let repo = tx2.commit("agent", "revoke").unwrap();

    // Default retrieve: the node must not appear.
    let result = repo
        .retrieve()
        .label("Note")
        .execute()
        .expect("retrieve ok");
    let ids: Vec<_> = result.items.iter().map(|i| i.node.id).collect();
    assert!(
        !ids.contains(&id),
        "tombstoned node must be excluded by default, got ids={ids:?}"
    );

    // Audit path: include_tombstoned(true) brings it back.
    let result = repo
        .retrieve()
        .label("Note")
        .include_tombstoned(true)
        .execute()
        .expect("retrieve ok");
    let ids: Vec<_> = result.items.iter().map(|i| i.node.id).collect();
    assert!(
        ids.contains(&id),
        "include_tombstoned(true) must surface the tombstoned node, got ids={ids:?}"
    );
}

#[test]
fn anchor_excluded_from_retrieve_by_default_and_surfaced_under_include_system() {
    // Contract: a node whose id matches the system anchor must NOT
    // appear in a default retrieve, no matter what other live nodes
    // are present. `include_system(true)` restores it for audit.
    //
    // Repros the user-reported bug where the `mnem init` anchor
    // (node id 00000000-0000-7000-8000-6d6e656d0001) showed up with
    // a low score in every retrieve after the first ingest.
    use mnem_core::anchor::anchor_node_id;
    use mnem_core::objects::Node;

    let repo = fresh_repo();
    let mut tx = repo.start_transaction();
    let anchor = Node::new(anchor_node_id(), "Meta");
    tx.add_node(&anchor).expect("add anchor");
    let real_id = tx
        .commit_memory("Note", "live content the user actually wrote", [])
        .unwrap();
    let repo = tx.commit("agent", "init + content").unwrap();

    // Default retrieve: anchor is hidden, real note is visible.
    let result = repo
        .retrieve()
        .label("Note")
        .execute()
        .expect("retrieve ok");
    let ids: Vec<_> = result.items.iter().map(|i| i.node.id).collect();
    assert!(
        !ids.contains(&anchor_node_id()),
        "anchor must be excluded by default, got ids={ids:?}"
    );
    assert!(
        ids.contains(&real_id),
        "live note must still surface, got ids={ids:?}"
    );

    // Also check the Meta-labelled retrieve directly: the anchor is a
    // Meta node so without the system filter it would dominate the
    // results. With the filter on (default) we expect zero hits.
    let result_meta = repo.retrieve().label("Meta").execute().expect("retrieve ok");
    let ids_meta: Vec<_> = result_meta.items.iter().map(|i| i.node.id).collect();
    assert!(
        ids_meta.is_empty(),
        "default retrieve of label=Meta must drop the anchor entirely, got ids={ids_meta:?}"
    );

    // Audit path: include_system(true) brings it back.
    let result = repo
        .retrieve()
        .label("Meta")
        .include_system(true)
        .execute()
        .expect("retrieve ok");
    let ids: Vec<_> = result.items.iter().map(|i| i.node.id).collect();
    assert!(
        ids.contains(&anchor_node_id()),
        "include_system(true) must surface the anchor for audit, got ids={ids:?}"
    );
}

#[test]
fn query_excludes_anchor_by_default_and_surfaces_under_include_system() {
    // Same contract as the retriever test above, but at the Query
    // level (the structured `mnem query` / MCP `mnem_list_nodes`
    // surface). Two filter sites in `Query::execute` need to agree:
    // the indexed-label cursor path and the streaming-fallback path.
    use mnem_core::anchor::anchor_node_id;
    use mnem_core::objects::Node;

    let repo = fresh_repo();
    let mut tx = repo.start_transaction();
    tx.add_node(&Node::new(anchor_node_id(), "Meta"))
        .expect("add anchor");
    let _real = tx
        .commit_memory("Note", "user-authored fact", [])
        .unwrap();
    let repo = tx.commit("agent", "init + content").unwrap();

    // Default Query for ntype=Meta: zero hits (only the anchor lives
    // under that label, and the system filter drops it).
    let hits = repo.query().label("Meta").execute().expect("query ok");
    let ids: Vec<_> = hits.iter().map(|h| h.node.id).collect();
    assert!(
        ids.is_empty(),
        "default Query for label=Meta must drop the anchor, got ids={ids:?}"
    );

    // include_system(true) restores it.
    let hits = repo
        .query()
        .label("Meta")
        .include_system(true)
        .execute()
        .expect("query ok");
    let ids: Vec<_> = hits.iter().map(|h| h.node.id).collect();
    assert!(
        ids.contains(&anchor_node_id()),
        "include_system(true) must surface the anchor, got ids={ids:?}"
    );

    // No-label streaming fallback: same guarantee on the alternate
    // execute branch.
    let hits = repo.query().execute().expect("query ok");
    let ids: Vec<_> = hits.iter().map(|h| h.node.id).collect();
    assert!(
        !ids.contains(&anchor_node_id()),
        "default no-label Query must also drop the anchor, got ids={ids:?}"
    );
}

#[test]
fn edge_between_two_notes_surfaces_on_incoming_edges() {
    // Contract: an edge added between two commit_memory-written
    // nodes is reachable via the dual-adjacency back-index
    // (`incoming_edges(dst)`), which is what provenance walks and
    // `graph_expand(direction = Incoming)` rely on.
    let repo = fresh_repo();
    let mut tx = repo.start_transaction();
    let src_id = tx
        .commit_memory(
            "Note",
            "source note",
            [("role".to_string(), Ipld::String("src".into()))],
        )
        .unwrap();
    let dst_id = tx
        .commit_memory(
            "Note",
            "dest note",
            [("role".to_string(), Ipld::String("dst".into()))],
        )
        .unwrap();
    let edge = Edge::new(EdgeId::new_v7(), "references", src_id, dst_id);
    let edge_id = edge.id;
    tx.add_edge(&edge).unwrap();
    let repo = tx.commit("agent", "notes + edge").unwrap();

    let incoming = repo
        .incoming_edges(&dst_id, None)
        .expect("incoming_edges ok");
    assert!(
        incoming.iter().any(|e| e.id == edge_id
            && e.src == src_id
            && e.dst == dst_id
            && e.etype == "references"),
        "incoming_edges(dst) must surface the src->dst edge, got {incoming:#?}"
    );
}

#[test]
fn temporal_filter_is_lenient_on_nodes_without_reserved_props() {
    // Contract: a node lacking both reserved temporal props (written
    // via the lower-level `add_node` path) passes every temporal
    // check - callers want to layer the filter onto legacy repos
    // without nuking the result set.
    let repo = fresh_repo();
    let mut tx = repo.start_transaction();
    // Bypass commit_memory so no auto-stamp happens.
    let legacy = mnem_core::objects::Node::new(NodeId::new_v7(), "Note").with_summary("no stamp");
    tx.add_node(&legacy).unwrap();
    let repo = tx.commit("agent", "legacy").unwrap();

    // Every bound: the node must still surface because it has no
    // reserved prop to gate against.
    let result = repo
        .retrieve()
        .label("Note")
        .where_created_after(10_000_000_000_000)
        .where_created_before(10_000_000_000_001)
        .where_updated_after(10_000_000_000_000)
        .where_updated_before(10_000_000_000_001)
        .execute()
        .expect("retrieve ok");
    let ids: Vec<_> = result.items.iter().map(|i| i.node.id).collect();
    assert!(
        ids.contains(&legacy.id),
        "legacy node without reserved props must pass every temporal check (lenient rule), got ids={ids:?}"
    );
}