second-brain-sync 0.13.2

Bidirectional sync for second-brain: SSH transport, JSONL change log, conflict resolution
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
use std::io::BufReader;

use chrono::{Duration, Utc};

use second_brain_core::{
    schema::{
        Conversation, Entity, Memory, MemoryType, Relation, RelationType, SyncNodeType, SyncOp,
        SyncRecord,
    },
    sqlite_store::SqliteStore,
    store::{Store, StoreAdmin},
};
use second_brain_sync::{export::export_changes, import::import_changes};

fn make_memory(content: &str, machine_id: &str) -> Memory {
    Memory {
        machine_id: machine_id.to_string(),
        ..Memory::new(
            content.to_string(),
            MemoryType::Semantic,
            "test".to_string(),
            String::new(),
        )
    }
}

fn export_to_string(store: &SqliteStore, after_seq: u64) -> String {
    let mut buf = Vec::new();
    export_changes(store, after_seq, &mut buf).unwrap();
    String::from_utf8(buf).unwrap()
}

fn import_from_str(
    store: &SqliteStore,
    data: &str,
) -> (second_brain_sync::import::ImportStats, u64) {
    let reader = BufReader::new(data.as_bytes());
    import_changes(store, reader).unwrap()
}

fn apply_update(
    store: &SqliteStore,
    memory: &Memory,
    origin: &str,
    origin_seq: u64,
) -> second_brain_core::store::ApplyOutcome {
    let record = SyncRecord {
        local_seq: origin_seq,
        origin_machine_id: origin.to_string(),
        origin_seq,
        op: SyncOp::Update,
        node_type: SyncNodeType::Memory,
        node_id: memory.id.to_string(),
        timestamp: Utc::now(),
        data: Some(serde_json::to_value(memory).unwrap()),
    };
    store.apply_record(&record).unwrap()
}

#[test]
fn newer_updated_at_wins() {
    let store_a = SqliteStore::in_memory("machine-a".to_string()).unwrap();
    let store_b = SqliteStore::in_memory("machine-b".to_string()).unwrap();

    let mut mem = make_memory("original content", "machine-a");
    mem.updated_at = Utc::now() - Duration::hours(2);
    store_a.store_memory(&mem).unwrap();

    let exported = export_to_string(&store_a, 0);
    import_from_str(&store_b, &exported);

    let mem_on_b = store_b.get_memory(mem.id).unwrap().unwrap();
    assert_eq!(mem_on_b.content, "original content");

    let newer_updated_at = Utc::now();
    let mut updated = mem_on_b.clone();
    updated.content = "newer content wins".to_string();
    updated.updated_at = newer_updated_at;

    let outcome = apply_update(&store_b, &updated, "machine-b", 99);
    assert!(matches!(
        outcome,
        second_brain_core::store::ApplyOutcome::Updated
    ));
    let after_newer = store_b.get_memory(mem.id).unwrap().unwrap();
    assert_eq!(after_newer.content, "newer content wins");

    let mut older = after_newer.clone();
    older.content = "older content should lose".to_string();
    older.updated_at = Utc::now() - Duration::hours(1);

    let outcome2 = apply_update(&store_b, &older, "machine-a", 100);
    assert!(matches!(
        outcome2,
        second_brain_core::store::ApplyOutcome::Skipped(
            second_brain_core::store::SkipReason::StaleWrite
        )
    ));
    let final_state = store_b.get_memory(mem.id).unwrap().unwrap();
    assert_eq!(final_state.content, "newer content wins");
}

#[test]
fn reinforce_does_not_win_conflict() {
    let store_a = SqliteStore::in_memory("machine-a".to_string()).unwrap();
    let store_b = SqliteStore::in_memory("machine-b".to_string()).unwrap();

    let base_time = Utc::now() - Duration::hours(3);
    let mut mem = make_memory("base content", "machine-a");
    mem.updated_at = base_time;
    store_a.store_memory(&mem).unwrap();

    let exported = export_to_string(&store_a, 0);
    import_from_str(&store_b, &exported);

    let mut b_copy = store_b.get_memory(mem.id).unwrap().unwrap();
    b_copy.content = "b genuine edit".to_string();
    b_copy.updated_at = Utc::now();
    apply_update(&store_b, &b_copy, "machine-b", 10);

    let b_state_after_edit = store_b.get_memory(mem.id).unwrap().unwrap();
    let b_edit_time = b_state_after_edit.updated_at;

    let mut a_reinforced = mem.clone();
    a_reinforced.updated_at = base_time;
    a_reinforced.access_count += 5;
    a_reinforced.last_accessed = Utc::now();

    let outcome = apply_update(&store_b, &a_reinforced, "machine-a", 20);
    assert!(matches!(
        outcome,
        second_brain_core::store::ApplyOutcome::Skipped(
            second_brain_core::store::SkipReason::StaleWrite
        )
    ));

    let final_b = store_b.get_memory(mem.id).unwrap().unwrap();
    assert_eq!(final_b.content, "b genuine edit");
    assert_eq!(final_b.updated_at, b_edit_time);
}

#[test]
fn tombstone_blocks_resurrection() {
    let store_a = SqliteStore::in_memory("machine-a".to_string()).unwrap();
    let store_b = SqliteStore::in_memory("machine-b".to_string()).unwrap();

    let mem = make_memory("memory to be deleted", "machine-a");
    let mem_id = mem.id;
    store_a.store_memory(&mem).unwrap();

    let exported_create = export_to_string(&store_a, 0);
    let (create_stats, _) = import_from_str(&store_b, &exported_create);
    assert_eq!(create_stats.created, 1);

    store_a.delete_memory(mem_id).unwrap();

    let after_create_seq = {
        let entries = store_a.sync_log_since(0).unwrap();
        entries
            .iter()
            .filter(|e| e.op == SyncOp::Create)
            .map(|e| e.local_seq as u64)
            .max()
            .unwrap_or(0)
    };

    let exported_delete = export_to_string(&store_a, after_create_seq);
    let (delete_stats, _) = import_from_str(&store_b, &exported_delete);
    assert_eq!(delete_stats.deleted, 1);

    assert!(
        store_b.tombstone_exists(mem_id).unwrap(),
        "tombstone must be on B after delete sync"
    );
    assert!(
        store_b.get_memory(mem_id).unwrap().is_none(),
        "memory must be gone from B"
    );

    let create_record_jsonl = {
        let entries = store_a.sync_log_since(0).unwrap();
        let create_entry = entries.iter().find(|e| e.op == SyncOp::Create).unwrap();
        let data: Option<serde_json::Value> = create_entry
            .data
            .as_ref()
            .and_then(|d| serde_json::from_str(d).ok());
        let record = serde_json::json!({
            "local_seq": create_entry.local_seq,
            "origin_machine_id": create_entry.origin_machine_id,
            "origin_seq": create_entry.origin_seq,
            "op": create_entry.op,
            "node_type": create_entry.node_type,
            "node_id": create_entry.node_id,
            "timestamp": create_entry.timestamp,
            "data": data,
        });
        format!("{}\n", serde_json::to_string(&record).unwrap())
    };

    let reader = BufReader::new(create_record_jsonl.as_bytes());
    let (resurrection_stats, _) = import_changes(&store_b, reader).unwrap();
    assert_eq!(
        resurrection_stats.skipped, 1,
        "create on tombstoned id must be skipped"
    );
    assert!(
        store_b.get_memory(mem_id).unwrap().is_none(),
        "tombstone must block resurrection"
    );
}

#[test]
fn transitive_relay() {
    let store_a = SqliteStore::in_memory("machine-a".to_string()).unwrap();
    let store_b = SqliteStore::in_memory("machine-b".to_string()).unwrap();
    let store_c = SqliteStore::in_memory("machine-c".to_string()).unwrap();

    let mem = make_memory("transitive relay test", "machine-a");
    let mem_id = mem.id;
    store_a.store_memory(&mem).unwrap();

    let a_export = export_to_string(&store_a, 0);
    import_from_str(&store_b, &a_export);

    assert!(
        store_b.get_memory(mem_id).unwrap().is_some(),
        "B must have the memory"
    );

    let b_export = export_to_string(&store_b, 0);
    import_from_str(&store_c, &b_export);

    assert!(
        store_c.get_memory(mem_id).unwrap().is_some(),
        "C must have the memory via relay"
    );

    let c_log = store_c.sync_log_since(0).unwrap();
    let create_entry = c_log
        .iter()
        .find(|e| e.node_id == mem_id.to_string() && e.op == SyncOp::Create)
        .unwrap();
    assert_eq!(
        create_entry.origin_machine_id, "machine-a",
        "C's log must carry A's origin_machine_id, not B's"
    );
}

#[test]
fn import_is_idempotent() {
    let store = SqliteStore::in_memory("machine-a".to_string()).unwrap();

    let mem = make_memory("idempotent test", "machine-a");
    store.store_memory(&mem).unwrap();

    let exported = export_to_string(&store, 0);

    let target = SqliteStore::in_memory("machine-b".to_string()).unwrap();

    let (first_stats, _) = import_from_str(&target, &exported);
    let count_after_first = target.memory_count().unwrap();
    let log_after_first = target.sync_log_since(0).unwrap().len();

    let (second_stats, _) = import_from_str(&target, &exported);
    let count_after_second = target.memory_count().unwrap();
    let log_after_second = target.sync_log_since(0).unwrap().len();

    assert_eq!(first_stats.created, 1);
    assert_eq!(second_stats.created, 0);
    assert_eq!(second_stats.errors, 0);
    assert_eq!(second_stats.skipped, 1);
    assert_eq!(count_after_first, count_after_second);
    assert_eq!(log_after_first, log_after_second);
}

#[test]
fn entity_and_conversation_ops_apply() {
    let store_src = SqliteStore::in_memory("machine-src".to_string()).unwrap();
    let store_dst = SqliteStore::in_memory("machine-dst".to_string()).unwrap();

    let conv = Conversation {
        id: uuid::Uuid::new_v4(),
        source: "test".to_string(),
        machine_id: "machine-src".to_string(),
        started_at: Utc::now(),
        project_path: None,
    };
    store_src.store_conversation(&conv).unwrap();

    let entity = Entity::new("TestEntity".to_string(), "person".to_string());
    let entity_id = entity.id;
    store_src.store_entity(&entity).unwrap();

    let conv_id = conv.id;
    let exported = export_to_string(&store_src, 0);
    let (stats, _) = import_from_str(&store_dst, &exported);
    assert_eq!(
        stats.created, 2,
        "conversation and entity must both be created"
    );
    assert!(store_dst.get_conversation(conv_id).unwrap().is_some());
    assert!(store_dst.get_entity(entity_id).unwrap().is_some());

    let mut updated_entity = entity.clone();
    updated_entity.name = "UpdatedEntity".to_string();
    let update_jsonl = {
        let data = serde_json::to_value(&updated_entity).unwrap();
        let record = serde_json::json!({
            "local_seq": 100u64,
            "origin_machine_id": "machine-src",
            "origin_seq": 100u64,
            "op": SyncOp::Update,
            "node_type": SyncNodeType::Entity,
            "node_id": entity_id.to_string(),
            "timestamp": Utc::now(),
            "data": data,
        });
        format!("{}\n", serde_json::to_string(&record).unwrap())
    };
    let (update_stats, _) = import_from_str(&store_dst, &update_jsonl);
    assert_eq!(update_stats.updated, 1);

    let delete_conv_jsonl = {
        let record = serde_json::json!({
            "local_seq": 101u64,
            "origin_machine_id": "machine-src",
            "origin_seq": 101u64,
            "op": SyncOp::Delete,
            "node_type": SyncNodeType::Conversation,
            "node_id": conv_id.to_string(),
            "timestamp": Utc::now(),
            "data": null,
        });
        format!("{}\n", serde_json::to_string(&record).unwrap())
    };
    let (delete_stats, _) = import_from_str(&store_dst, &delete_conv_jsonl);
    assert_eq!(delete_stats.deleted, 1);
    assert!(store_dst.get_conversation(conv_id).unwrap().is_none());
}

#[test]
fn error_holds_watermark() {
    let store = SqliteStore::in_memory("machine-a".to_string()).unwrap();

    let mem1 = make_memory("memory one", "machine-a");
    let mem3 = make_memory("memory three", "machine-a");

    let record1 = serde_json::json!({
        "local_seq": 1u64,
        "origin_machine_id": "machine-a",
        "origin_seq": 1u64,
        "op": SyncOp::Create,
        "node_type": SyncNodeType::Memory,
        "node_id": mem1.id.to_string(),
        "timestamp": Utc::now(),
        "data": serde_json::to_value(&mem1).unwrap(),
    });

    let record2_malformed = serde_json::json!({
        "local_seq": 2u64,
        "origin_machine_id": "machine-a",
        "origin_seq": 2u64,
        "op": SyncOp::Create,
        "node_type": SyncNodeType::Memory,
        "node_id": "some-id",
        "timestamp": Utc::now(),
        "data": null,
    });

    let record3 = serde_json::json!({
        "local_seq": 3u64,
        "origin_machine_id": "machine-a",
        "origin_seq": 3u64,
        "op": SyncOp::Create,
        "node_type": SyncNodeType::Memory,
        "node_id": mem3.id.to_string(),
        "timestamp": Utc::now(),
        "data": serde_json::to_value(&mem3).unwrap(),
    });

    let jsonl = format!(
        "{}\n{}\n{}\n",
        serde_json::to_string(&record1).unwrap(),
        serde_json::to_string(&record2_malformed).unwrap(),
        serde_json::to_string(&record3).unwrap(),
    );

    let reader = BufReader::new(jsonl.as_bytes());
    let (stats, watermark) = import_changes(&store, reader).unwrap();

    assert_eq!(stats.errors, 1);
    assert!(
        watermark < 2,
        "watermark must be below the errored record's local_seq=2, got {watermark}"
    );
}

#[test]
fn retire_consolidated_raws_archives_and_logs_an_update() {
    let store = SqliteStore::in_memory("machine-a".to_string()).unwrap();

    let raw = make_memory("raw memory to consolidate", "machine-a");
    let distilled = make_memory("distilled result", "machine-a");

    let raw_id = raw.id;
    store.store_memory(&raw).unwrap();
    store.store_memory(&distilled).unwrap();
    store
        .mark_consolidated(raw_id, distilled.id, "test-model")
        .unwrap();

    let retired = store.retire_consolidated_raws().unwrap();
    assert_eq!(retired, 1);

    let row = store.get_memory(raw_id).unwrap().unwrap();
    assert_eq!(
        row.tier,
        second_brain_core::schema::Tier::Archived,
        "raw memory must be archived, not deleted"
    );
    assert!(
        !store.tombstone_exists(raw_id).unwrap(),
        "retirement must not tombstone the raw"
    );

    let log = store.sync_log_since(0).unwrap();
    let update_entries: Vec<_> = log
        .iter()
        .filter(|e| e.op == SyncOp::Update && e.node_id == raw_id.to_string())
        .collect();
    assert_eq!(
        update_entries.len(),
        1,
        "sync log must carry the tier change so peers retire the raw too"
    );
}

#[test]
fn synced_update_preserves_relationships() {
    let store = SqliteStore::in_memory("machine-a".to_string()).unwrap();

    let mem = make_memory("original content", "machine-a");
    let mem_id = mem.id;
    store.store_memory(&mem).unwrap();

    let entity = Entity::new("TestEntity".to_string(), "person".to_string());
    let entity_id = entity.id;
    store.store_entity(&entity).unwrap();

    let rel = Relation {
        from_id: mem_id,
        to_id: entity_id,
        relation_type: RelationType::Mentions,
        strength: 1.0,
        context: None,
    };
    store.store_relation(&rel).unwrap();

    let relations_before = store
        .get_relations(mem_id, Some(RelationType::Mentions))
        .unwrap();
    assert!(
        !relations_before.is_empty(),
        "MENTIONS relation must exist before the update"
    );

    let mut updated = mem.clone();
    updated.content = "updated content".to_string();
    updated.updated_at = Utc::now() + Duration::seconds(1);

    let outcome = apply_update(&store, &updated, "machine-b", 99);
    assert!(
        matches!(outcome, second_brain_core::store::ApplyOutcome::Updated),
        "a synced memory update must report Updated"
    );

    let relations_after = store
        .get_relations(mem_id, Some(RelationType::Mentions))
        .unwrap();
    assert!(
        !relations_after.is_empty(),
        "MENTIONS relation must still exist after a synced update — DETACH DELETE destroys it"
    );
    assert_eq!(
        relations_after[0].to_id, entity_id,
        "MENTIONS relation must still point to the original entity"
    );
}

#[test]
fn batched_import_matches_per_record_state() {
    let source = SqliteStore::in_memory("machine-src".to_string()).unwrap();

    let mut ids = Vec::new();
    for i in 0..50 {
        let mem = make_memory(&format!("batched memory {i}"), "machine-src");
        ids.push(mem.id);
        source.store_memory(&mem).unwrap();
    }
    let exported = export_to_string(&source, 0);

    let store_batched = SqliteStore::in_memory("machine-b".to_string()).unwrap();
    let (stats, _) = {
        let reader = BufReader::new(exported.as_bytes());
        second_brain_sync::import::import_changes(&store_batched, reader).unwrap()
    };
    assert_eq!(
        stats.created, 50,
        "all 50 unique memory creates must be applied in the batched import"
    );

    for id in &ids {
        assert!(
            store_batched.get_memory(*id).unwrap().is_some(),
            "memory {id} must be present after batched import"
        );
    }

    let (stats2, _) = {
        let reader = BufReader::new(exported.as_bytes());
        second_brain_sync::import::import_changes(&store_batched, reader).unwrap()
    };
    assert_eq!(
        stats2.created, 0,
        "re-importing the same records must create nothing (idempotent skip)"
    );
    assert_eq!(stats2.skipped, 50, "the second import must skip all 50");
}

struct EveryLineAt(chrono::DateTime<Utc>);

impl second_brain_core::session_dates::SessionEvidence for EveryLineAt {
    fn line_time(&self, _transcript: &str, _text: &str) -> Option<chrono::DateTime<Utc>> {
        Some(self.0)
    }
    fn first_time(&self, _transcript: &str) -> Option<chrono::DateTime<Utc>> {
        None
    }
    fn note_modified_at(&self, _path: &str) -> Option<chrono::DateTime<Utc>> {
        None
    }
}

/// The Pi supersedes a memory haruka has not pulled yet while haruka
/// backfills its date. The backfill must not beat the supersede, and the
/// supersede must not erase the date, whichever order the peers pull in.
#[test]
fn a_peer_supersede_and_a_session_date_backfill_both_survive_the_exchange() {
    let haruka = SqliteStore::in_memory("haruka".to_string()).unwrap();
    let pi = SqliteStore::in_memory("pi".to_string()).unwrap();

    let mut x = Memory::new(
        "raw from a transcript".to_string(),
        MemoryType::Semantic,
        "claude-code".to_string(),
        "/t.jsonl".to_string(),
    );
    x.updated_at = Utc::now() - Duration::hours(1);
    haruka.store_memory(&x).unwrap();
    import_from_str(&pi, &export_to_string(&haruka, 0));
    let haruka_seq = haruka.max_sync_seq().unwrap();
    let pi_seq = pi.max_sync_seq().unwrap();

    let y = make_memory("the newer fact", "pi");
    pi.store_memory(&y).unwrap();
    pi.supersede_memory(x.id, y.id).unwrap();

    let session: chrono::DateTime<Utc> = "2026-07-23T09:00:00Z".parse().unwrap();
    let report = haruka
        .backfill_session_at(&EveryLineAt(session), false, false)
        .unwrap();
    assert_eq!(report.written, 1);

    import_from_str(&pi, &export_to_string(&haruka, haruka_seq));
    import_from_str(&haruka, &export_to_string(&pi, pi_seq));

    for (name, store) in [("haruka", &haruka), ("pi", &pi)] {
        let got = store.get_memory(x.id).unwrap().unwrap();
        assert_eq!(got.superseded_by, Some(y.id), "{name} keeps the supersede");
        assert_eq!(got.session_at, Some(session), "{name} keeps the date");
    }
}

struct UntrustedClock;

impl second_brain_core::sqlite_store::WallClock for UntrustedClock {
    fn now_micros(&self) -> Option<i64> {
        None
    }
}

#[test]
fn an_import_the_store_cannot_mint_for_fails_instead_of_counting_errors() {
    let source = SqliteStore::in_memory("source".to_string()).unwrap();
    for n in 0..3 {
        let memory = make_memory(&format!("relayed to an untrusted clock {n}"), "source");
        source.store_memory(&memory).unwrap();
    }
    let stream = export_to_string(&source, 0);
    let target = SqliteStore::in_memory("target".to_string())
        .unwrap()
        .with_wall_clock(std::sync::Arc::new(UntrustedClock));

    let error = import_changes(&target, BufReader::new(stream.as_bytes()))
        .err()
        .expect("an import that cannot mint fails");

    assert!(
        error
            .chain()
            .any(|cause| cause.is::<second_brain_core::sqlite_store::ClockUntrusted>()),
        "{error:#}"
    );
    assert_eq!(target.memory_count().unwrap(), 0);
}