oxibrain-cli 0.5.0

Command-line interface and MCP server for oxibrain
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
//! End-to-end curation round-trip tests (Plan C Task 3).
//!
//! These tests drive the Brain facade directly (no `oxibrain` binary) to prove
//! that every curation operation — merge, split, alias, retract, declare,
//! source policy, predicate add — produces the expected store side-effects,
//! and that a full reproject preserves curation state.
//!

use oxibrain::{Brain, BrainConfig};
use oxibrain_core::{BeliefStatus, EntityMerge};
use oxibrain_store::project::{DeclObject, Declaration, EntityRef};

async fn fresh_brain() -> (tempfile::TempDir, Brain) {
    let dir = tempfile::tempdir().expect("tempdir");
    let brain = Brain::open(BrainConfig::at(dir.path()))
        .await
        .expect("open");
    (dir, brain)
}

fn add_statement(surface: &str, predicate: &str, object_surface: &str) -> Declaration {
    Declaration::AddStatement {
        subject: EntityRef {
            surface: surface.to_string(),
            ty: "Person".to_string(),
        },
        predicate: predicate.to_string(),
        object: DeclObject::Entity {
            surface: object_surface.to_string(),
            ty: "Organization".to_string(),
        },
        polarity: "affirm".to_string(),
        valid_from: 0,
        valid_to: i64::MAX,
    }
}

#[tokio::test]
async fn merge_then_split_round_trip() {
    let (_dir, brain) = fresh_brain().await;
    let space = brain.ensure_space("personal").await.expect("space");

    // Seed two independent entities (so the test doesn't depend on merge
    // initialising an empty loser). Without statements, Merge would still
    // create the entities, but seeding a belief first makes the merge's
    // post-state observable through `beliefs`.
    brain
        .declare(&space, add_statement("Bob", "employed_by", "Initech"))
        .await
        .expect("declare bob");
    brain
        .declare(&space, add_statement("Alice", "employed_by", "Initech"))
        .await
        .expect("declare alice");

    // Merge Bob -> Alice.
    brain
        .declare(
            &space,
            Declaration::Merge {
                loser: EntityRef {
                    surface: "Bob".to_string(),
                    ty: "Person".to_string(),
                },
                winner: EntityRef {
                    surface: "Alice".to_string(),
                    ty: "Person".to_string(),
                },
            },
        )
        .await
        .expect("merge");

    // Post-merge: a single merge record exists and is active (undone_at is None).
    let merges_post = brain.list_merges(&space).await.expect("list merges post");
    assert_eq!(merges_post.len(), 1, "exactly one merge record");
    let merge: &EntityMerge = &merges_post[0];
    assert!(
        merge.undone_at.is_none(),
        "merge is active (undone_at None)"
    );

    // Now split Bob. According to the design this finds the most recent
    // active merge where Bob is the loser and marks it undone.
    brain
        .declare(
            &space,
            Declaration::Split {
                entity: EntityRef {
                    surface: "Bob".to_string(),
                    ty: "Person".to_string(),
                },
            },
        )
        .await
        .expect("split");

    // Post-split: merge record's undone_at is now set.
    let merges_post_split = brain
        .list_merges(&space)
        .await
        .expect("list merges post-split");
    assert_eq!(merges_post_split.len(), 1, "merge record still present");
    let merge_after: &EntityMerge = &merges_post_split[0];
    assert!(
        merge_after.undone_at.is_some(),
        "split must set undone_at on the merge record"
    );

    // Sanity: splitting again with no active merge must fail (the merge is
    // already undone). This double-safety net proves we didn't accidentally
    // leave the merge active.
    let repeat = brain
        .declare(
            &space,
            Declaration::Split {
                entity: EntityRef {
                    surface: "Bob".to_string(),
                    ty: "Person".to_string(),
                },
            },
        )
        .await;
    assert!(
        repeat.is_err(),
        "second split on already-undone merge must fail"
    );
}

#[tokio::test]
async fn alias_makes_entity_findable_by_alias() {
    let (_dir, brain) = fresh_brain().await;
    let space = brain.ensure_space("personal").await.expect("space");

    // Declare a statement whose subject surface is "Alice".
    brain
        .declare(&space, add_statement("Alice", "employed_by", "Acme"))
        .await
        .expect("declare alice");

    // Confirm a baseline lookup by surface.
    let baseline = brain
        .resolve_entity_id(&space, "Person", "Alice")
        .await
        .expect("resolve")
        .expect("alice exists");

    // Add an alias "Al" on Alice.
    brain
        .declare(
            &space,
            Declaration::Alias {
                entity: EntityRef {
                    surface: "Alice".to_string(),
                    ty: "Person".to_string(),
                },
                surface: "Al".to_string(),
            },
        )
        .await
        .expect("alias");

    // The alias resolves to the same entity. This is the proof that the
    // user's surface form participates in the same lookup machinery as the
    // canonical surface (design: Alias adds a UserDeclared EntityKey).
    let via_alias = brain
        .resolve_entity_id(&space, "Person", "Al")
        .await
        .expect("resolve alias")
        .expect("alias resolves");
    assert_eq!(
        via_alias, baseline,
        "alias surface must resolve to the canonical entity"
    );

    // Beliefs asked through the alias-attached entity id still return Alice's
    // beliefs.
    let beliefs = brain.beliefs(&space, &via_alias).await.expect("beliefs");
    assert!(
        !beliefs.is_empty(),
        "Alice's beliefs still reachable via alias"
    );
}

#[tokio::test]
async fn retract_by_statement_id() {
    let (_dir, brain) = fresh_brain().await;
    let space = brain.ensure_space("personal").await.expect("space");

    // Declare the statement.
    let decl = add_statement("Alice", "employed_by", "Acme");
    brain.declare(&space, decl.clone()).await.expect("declare");

    // Find Alice's entity id, then the statement id from her beliefs.
    let alice_id = brain
        .resolve_entity_id(&space, "Person", "Alice")
        .await
        .expect("resolve")
        .expect("alice");
    let beliefs = brain.beliefs(&space, &alice_id).await.expect("beliefs");
    assert_eq!(beliefs.len(), 1, "exactly one belief before retract");
    let statement_id = beliefs[0].statement.clone();
    let original_status = beliefs[0].status;
    assert_eq!(original_status, BeliefStatus::Active, "starts Active");

    // Snapshot pre-retract, isolating the assertions section. The snapshot
    // helper renders NULL columns as empty strings between `|` separators.
    let pre_snap = brain.snapshot_truth(&space).await.expect("pre snap");
    let pre_assertions = section(&pre_snap, "assertions");
    assert!(
        pre_assertions.contains('|'),
        "pre-retract assertions section must contain at least one row; got: \
         {pre_assertions}"
    );
    // The trailing field (retracted_at) is NULL before retract — render as
    // empty between pipes. So a row ends with `|<empty>|` followed by \n.
    // Find rows that end with a double-pipe or trailing-pipe+newline.
    let pre_row_count = pre_assertions.lines().filter(|l| !l.is_empty()).count();
    assert_eq!(pre_row_count, 1, "exactly one assertion row pre-retract");

    // Retract via retract_parts + declare(Retract), mirroring the CLI path.
    let (subject, predicate, object) = brain
        .retract_parts(&space, &statement_id)
        .await
        .expect("retract_parts");
    brain
        .declare(
            &space,
            Declaration::Retract {
                subject,
                predicate,
                object,
                episode: String::new(),
            },
        )
        .await
        .expect("declare retract");

    // Post-retract: the assertion row's retracted_at column is now set to a
    // numeric ms timestamp. The retract produced an auditable episode, so
    // the truth snapshot must change.
    let post_snap = brain.snapshot_truth(&space).await.expect("post snap");
    let post_assertions = section(&post_snap, "assertions");
    assert_ne!(
        pre_assertions, post_assertions,
        "assertions section must change after retract"
    );

    // Take the first/only assertion row and inspect its trailing field.
    // Before retract, `retracted_at` rendered as empty (NULL). After, it is
    // a positive integer (the millisecond timestamp). Concretely, the post
    // row's last `|` segment contains a digit-only string; the pre row's
    // last segment is empty.
    let pre_row = pre_assertions
        .lines()
        .find(|l| !l.is_empty())
        .expect("pre row");
    let post_row = post_assertions
        .lines()
        .find(|l| !l.is_empty())
        .expect("post row");
    let pre_last = pre_row.rsplit('|').next().expect("pre tail");
    let post_last = post_row.rsplit('|').next().expect("post tail");
    assert!(
        pre_last.is_empty(),
        "pre-retract trailing field (retracted_at) must be empty/NULL; got: \
         `{pre_last}`"
    );
    assert!(
        !post_last.is_empty() && post_last.chars().all(|c| c.is_ascii_digit()),
        "post-retract trailing field must be a numeric ms timestamp; got: \
         `{post_last}`"
    );

    // Belt-and-braces: belief() lookups after retract return no rows because
    // the engine re-folds over visible (non-retracted) assertions, which now
    // contain zero. This is the documented engine behaviour — retracted
    // beliefs don't appear in `belief()` lists. The retracted state lives on
    // the assertion's retracted_at column instead, verifiable through the
    // truth snapshot above.
    let post_beliefs = brain
        .beliefs(&space, &alice_id)
        .await
        .expect("post beliefs");
    assert!(
        post_beliefs.is_empty(),
        "post-retract beliefs() returns empty (retracted dropped from fold)"
    );
}

/// Extract a single `---label---...---next-label---` slice from a snapshot.
fn section(snapshot: &str, label: &str) -> String {
    let start_header = format!("---{label}---");
    let Some(start) = snapshot.find(&start_header) else {
        return String::new();
    };
    let after = &snapshot[start + start_header.len()..];
    // Next `\n` after the header line.
    let body_start = after.find('\n').map(|i| i + 1).unwrap_or(0);
    let body = &after[body_start..];
    // Up to the next `---` marker (start of next section) or end of string.
    let end = body.find("\n---").unwrap_or(body.len());
    body[..end].to_string()
}

#[tokio::test]
async fn declare_raw_json() {
    let (_dir, brain) = fresh_brain().await;
    let space = brain.ensure_space("personal").await.expect("space");

    // Build a Declaration::AddStatement via serde_json round-trip.
    let decl = add_statement("Alice", "employed_by", "Acme");
    let json = serde_json::to_string(&decl).expect("serialize");
    let parsed: Declaration = serde_json::from_str(&json).expect("parse");

    let before = brain.episode_count().await.expect("count before");
    let ep_id = brain.declare(&space, parsed).await.expect("declare");
    let after = brain.episode_count().await.expect("count after");

    assert!(
        after > before,
        "declaring an episode must increase the count (before={before}, after={after})"
    );
    assert!(!ep_id.is_empty(), "declare returns non-empty episode id");
}

#[tokio::test]
async fn source_policy_changes_trust() {
    let (_dir, brain) = fresh_brain().await;
    let space = brain.ensure_space("personal").await.expect("space");
    // First, register the source.
    brain
        .declare(
            &space,
            Declaration::RegisterSource {
                name: "daily_news".to_string(),
                kind: "rss".to_string(),
                mode: "push".to_string(),
                claims_json: "{}".to_string(),
            },
        )
        .await
        .expect("register source");

    let before = brain.episode_count().await.expect("count before policy");
    brain
        .declare(
            &space,
            Declaration::SetSourcePolicy {
                source_name: "daily_news".to_string(),
                trust: "trusted".to_string(),
                effective_from: 0,
                effective_to: None,
            },
        )
        .await
        .expect("set policy");
    let after = brain.episode_count().await.expect("count after policy");

    assert!(
        after > before,
        "SetSourcePolicy must produce an episode (before={before}, after={after})"
    );

    // Re-declaring the same policy is idempotent at the engine level (content
    // hash collision). The episode count should not grow.
    let pre_dup = after;
    let second = brain
        .declare(
            &space,
            Declaration::SetSourcePolicy {
                source_name: "daily_news".to_string(),
                trust: "trusted".to_string(),
                effective_from: 0,
                effective_to: None,
            },
        )
        .await
        .expect("duplicate policy");
    let post_dup = brain.episode_count().await.expect("count after dup");
    assert_eq!(
        post_dup, pre_dup,
        "duplicate SetSourcePolicy is content-deterministic (ep={second})"
    );
}

#[tokio::test]
async fn predicate_add_and_list() {
    let (_dir, brain) = fresh_brain().await;
    let space = brain.ensure_space("personal").await.expect("space");

    // RegisterPredicate accepts raw def_json; the projection extracts
    // major/minor version fields and stores the row.
    let def_json = serde_json::json!({
        "name": "custom_likes",
        "major_version": 1,
        "minor_version": 0,
        "args": [{"name": "thing", "type": "Entity"}],
        "profile_relevant": true,
    })
    .to_string();

    let before = brain.snapshot_truth(&space).await.expect("snap before");
    brain
        .declare(
            &space,
            Declaration::RegisterPredicate {
                name: "custom_likes".to_string(),
                def_json: def_json.clone(),
            },
        )
        .await
        .expect("register predicate");
    let after = brain.snapshot_truth(&space).await.expect("snap after");

    assert_ne!(
        before, after,
        "RegisterPredicate must change the truth snapshot"
    );
    assert!(
        after.contains("custom_likes"),
        "snapshot truth must contain the new predicate name; got: {after}"
    );

    // Reprojection is still valid after introducing a new predicate: same
    // snapshot text across two reprojects.
    brain.reproject().await.expect("reproject 1");
    let snap1 = brain.snapshot_truth(&space).await.expect("snap1");
    brain.reproject().await.expect("reproject 2");
    let snap2 = brain.snapshot_truth(&space).await.expect("snap2");
    assert_eq!(snap1, snap2, "reprojection is deterministic");
}

#[tokio::test]
async fn reproject_preserves_curation() {
    let (_dir, brain) = fresh_brain().await;
    let space = brain.ensure_space("personal").await.expect("space");

    // Sequence of curation ops: merge, alias, split. Net effect: merge is
    // undone but the alias and the underlying entities remain.
    brain
        .declare(&space, add_statement("Bob", "employed_by", "Initech"))
        .await
        .expect("declare bob");
    brain
        .declare(&space, add_statement("Alice", "employed_by", "Initech"))
        .await
        .expect("declare alice");

    // Merge Bob -> Alice.
    brain
        .declare(
            &space,
            Declaration::Merge {
                loser: EntityRef {
                    surface: "Bob".to_string(),
                    ty: "Person".to_string(),
                },
                winner: EntityRef {
                    surface: "Alice".to_string(),
                    ty: "Person".to_string(),
                },
            },
        )
        .await
        .expect("merge");

    // Alias on the winner.
    brain
        .declare(
            &space,
            Declaration::Alias {
                entity: EntityRef {
                    surface: "Alice".to_string(),
                    ty: "Person".to_string(),
                },
                surface: "Ali".to_string(),
            },
        )
        .await
        .expect("alias");

    // Split Bob back out.
    brain
        .declare(
            &space,
            Declaration::Split {
                entity: EntityRef {
                    surface: "Bob".to_string(),
                    ty: "Person".to_string(),
                },
            },
        )
        .await
        .expect("split");

    // Snapshot before reproject.
    let before = brain.snapshot_truth(&space).await.expect("snap before");
    let merges_before = brain.list_merges(&space).await.expect("merges before");
    let alice_beliefs_before = brain
        .beliefs(
            &space,
            &brain
                .resolve_entity_id(&space, "Person", "Alice")
                .await
                .expect("resolve")
                .expect("alice"),
        )
        .await
        .expect("alice beliefs before");
    let ali_id_before = brain
        .resolve_entity_id(&space, "Person", "Ali")
        .await
        .expect("resolve ali")
        .expect("ali resolves");

    // Reproject everything from scratch.
    brain.reproject().await.expect("reproject");

    // After reproject: byte-identical truth snapshot, same merge records,
    // Alice's beliefs still reachable, alias still resolves.
    let after = brain.snapshot_truth(&space).await.expect("snap after");
    assert_eq!(
        before, after,
        "truth snapshot must be invariant under reproject"
    );

    let merges_after = brain.list_merges(&space).await.expect("merges after");
    assert_eq!(
        merges_before.len(),
        merges_after.len(),
        "merge record count must be invariant"
    );
    assert!(
        merges_after[0].undone_at.is_some(),
        "the single merge must still be undone (split state preserved)"
    );

    let alice_beliefs_after = brain
        .beliefs(
            &space,
            &brain
                .resolve_entity_id(&space, "Person", "Alice")
                .await
                .expect("resolve 2")
                .expect("alice still there"),
        )
        .await
        .expect("alice beliefs after");
    assert_eq!(
        alice_beliefs_before.len(),
        alice_beliefs_after.len(),
        "Alice's belief count must be invariant"
    );

    let ali_id_after = brain
        .resolve_entity_id(&space, "Person", "Ali")
        .await
        .expect("resolve ali after")
        .expect("ali still resolves");
    assert_eq!(
        ali_id_before, ali_id_after,
        "alias surface must still resolve to the same entity"
    );
}