memstead-base 0.26.1

Engine internals for Memstead — store, parser, validators, mem lifecycle, workspace policy. Published as a dependency of the memstead-cli and memstead-mcp products, not as a supported library — pre-1.0, no API stability promise.
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
#![cfg(test)]

use std::path::{Path, PathBuf};

use tempfile::TempDir;

use crate::backend::{BackendError, MemBackend};
use crate::engine::test_helpers::*;
use crate::engine::{DeleteEntityArgs, Engine, EngineError};
use crate::entity::EntityId;

use crate::provenance::Provenance;
use crate::storage::ArchiveBackend;
use crate::vcs::CommitContext;
use crate::workspace::{Mount, MountCapability, MountLifecycle, MountStorage};

#[test]
fn engine_diff_unknown_mem_returns_typed_error() {
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    let err = engine.diff("nope", "a", "b", None).unwrap_err();
    assert!(matches!(err, EngineError::UnknownMem { name: v, .. } if v == "nope"));
}

/// A `write_id` from a mutation response passed back as `since`
/// on a folder mem refuses with the typed `INVALID_CURSOR` code
/// and a message naming both the right cursor and the confusion —
/// before this guard it silently replayed the whole history.
#[test]
fn engine_changes_since_folder_refuses_write_token_as_cursor() {
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    let token = format!("{:032x}{:016x}", 1_766_000_000_000_000_000u128, 7u64);
    let err = engine.changes_since("specs", &token, None).unwrap_err();
    assert_eq!(err.code(), "INVALID_CURSOR");
    match &err {
        EngineError::InvalidTimestampCursor { mem, since } => {
            assert_eq!(mem, "specs");
            assert_eq!(since, &token);
        }
        other => panic!("expected InvalidTimestampCursor, got {other:?}"),
    }
    let msg = err.to_string();
    assert!(
        msg.contains("RFC3339"),
        "message names the cursor dialect: {msg}"
    );
    assert!(
        msg.contains("`write_id` is an identity, not a cursor"),
        "message names the confusion: {msg}"
    );
    // The sentinel and a real timestamp still read.
    assert!(
        engine
            .changes_since("specs", crate::ops::EMPTY_TREE_SHA, None)
            .is_ok()
    );
    assert!(engine.changes_since("specs", "", None).is_ok());
}

#[test]
fn engine_diff_folder_mount_refuses_with_invalid_input() {
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    // Folder backend has no git refs — refuse cleanly via the
    // typed `INVALID_INPUT` code rather than collapsing through
    // the backend layer.
    let err = engine.diff("specs", "a", "b", None).unwrap_err();
    assert_eq!(err.code(), "INVALID_INPUT");
    match err {
        EngineError::NotGitBacked { mem, operation } => {
            assert_eq!(mem.as_deref(), Some("specs"));
            assert_eq!(operation, "memstead_diff");
        }
        other => panic!("expected NotGitBacked, got {other:?}"),
    }
}

#[test]
fn engine_diff_rename_similarity_out_of_range_refuses() {
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    let bad = crate::ops::DiffConfig {
        rename_similarity: 2.0,
        ..Default::default()
    };
    let err = engine.diff("specs", "a", "b", Some(bad)).unwrap_err();
    assert!(matches!(
        err,
        EngineError::RenameSimilarityOutOfRange { .. }
    ));
}

#[test]
fn engine_changes_since_archive_mount_returns_empty_report() {
    // Archive backends have no diff surface; the engine wrapper
    // produces an empty `ChangesReport` with the cursor echoed.
    let tmp = TempDir::new().unwrap();
    let archive_path = build_archive(tmp.path(), "ext", &[("a.md", b"a")]);
    let mount = archive_mount("ext", archive_path.clone());
    let engine = Engine::from_mounts(vec![(
        mount,
        Box::new(ArchiveBackend::new(archive_path)) as Box<dyn MemBackend>,
    )])
    .unwrap();
    let report = engine.changes_since("ext", "abc", None).expect("known mem");
    assert_eq!(report.mem, "ext");
    assert_eq!(report.since, "abc");
    assert_eq!(report.head, "abc");
    assert!(report.changes.is_empty());
    assert!(report.warnings.is_empty());
}

#[test]
fn engine_changes_since_unknown_mem_returns_typed_error() {
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    let err = engine
        .changes_since("does-not-exist", "abc", None)
        .unwrap_err();
    assert!(matches!(err, EngineError::UnknownMem { .. }));
}

#[test]
fn engine_changes_since_refuses_rename_similarity_below_min() {
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    // 0.05 is below RENAME_SIMILARITY_MIN (0.1); typed refusal,
    // not a silent clamp.
    let err = engine
        .changes_since("specs", "abc", Some(0.05))
        .expect_err("out-of-range refuses");
    match err {
        EngineError::RenameSimilarityOutOfRange {
            requested,
            allowed_min,
            allowed_max,
        } => {
            assert!((requested - 0.05).abs() < f32::EPSILON);
            assert!((allowed_min - crate::ops::RENAME_SIMILARITY_MIN).abs() < f32::EPSILON);
            assert!((allowed_max - crate::ops::RENAME_SIMILARITY_MAX).abs() < f32::EPSILON);
        }
        other => panic!("expected RenameSimilarityOutOfRange, got {other:?}"),
    }
}

#[test]
fn engine_changes_since_refuses_rename_similarity_above_max() {
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    // 1.5 is above RENAME_SIMILARITY_MAX (1.0); typed refusal.
    let err = engine
        .changes_since("specs", "abc", Some(1.5))
        .expect_err("out-of-range refuses");
    match err {
        EngineError::RenameSimilarityOutOfRange { requested, .. } => {
            assert!((requested - 1.5).abs() < f32::EPSILON);
        }
        other => panic!("expected RenameSimilarityOutOfRange, got {other:?}"),
    }
}

#[test]
fn engine_changes_since_no_warning_when_rename_similarity_in_range() {
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    // 0.5 is comfortably inside the valid range; no warning.
    let report = engine
        .changes_since("specs", "", Some(0.5))
        .expect("known mem");
    assert!(report.warnings.is_empty());
}

#[test]
fn engine_changes_since_no_warning_when_rename_similarity_omitted() {
    // Caller passes None → wrapper falls back to the default;
    // no clamping, no warning.
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    let report = engine.changes_since("specs", "", None).expect("known mem");
    assert!(report.warnings.is_empty());
}

#[test]
fn engine_changes_since_enriches_envelope_title_and_type_from_store() {
    // `build_demo_engine` creates three entities via the engine's
    // mutation pipeline, which appends Create events to the folder
    // backend's changelog. `Engine::changes_since` synthesises
    // BackendChanges from the changelog (id-only envelopes), then
    // enriches title / entity_type from the in-memory store.
    let tmp = TempDir::new().unwrap();
    let engine = build_demo_engine(&tmp);
    let report = engine
        .changes_since("specs", crate::ops::EMPTY_TREE_SHA, None)
        .expect("known mem");

    // Three Create events → three Added envelopes, each enriched.
    assert_eq!(report.changes.len(), 3);
    for env in &report.changes {
        match env {
            crate::ops::ChangeEnvelope::Added {
                id,
                title,
                entity_type,
            } => {
                assert!(title.is_some(), "title enriched for {id}");
                assert_eq!(entity_type.as_deref(), Some("spec"), "type for {id}");
            }
            other => panic!("expected Added envelope, got {other:?}"),
        }
    }
}

#[test]
fn engine_changes_since_removed_envelope_keeps_title_and_type_none() {
    // Create-then-delete net effect = Removed. Even though the
    // store may still know the entity, the engine wrapper
    // unconditionally strips title / entity_type on Removed.
    let tmp = TempDir::new().unwrap();
    let mut engine = build_demo_engine(&tmp);
    let (actor, client) = cli_actor();
    let id = EntityId::new("specs", "lonely-three");
    let hash = engine
        .get_entity(&id)
        .expect("seeded entity present")
        .content_hash
        .clone();
    engine
        .delete_entity(
            DeleteEntityArgs {
                id: id.clone(),
                expected_hash: crate::ExpectedHash::Current(hash),
            },
            actor,
            Some(&client),
            None,
        )
        .unwrap();
    let report = engine
        .changes_since("specs", crate::ops::EMPTY_TREE_SHA, None)
        .unwrap();
    let removed = report
        .changes
        .iter()
        .find(|e| {
            matches!(e,
                crate::ops::ChangeEnvelope::Removed { id: rid, .. } if rid == &id)
        })
        .expect("removed envelope for lonely-three");
    match removed {
        crate::ops::ChangeEnvelope::Removed {
            title, entity_type, ..
        } => {
            assert!(title.is_none());
            assert!(entity_type.is_none());
        }
        other => panic!("expected Removed, got {other:?}"),
    }
}

// ---- Engine::cross_mem_link_allowed ---------------------------

#[test]
fn reload_if_stale_returns_empty_for_folder_only_engine() {
    // Folder mems now carry a changelog-derived drift cursor, so
    // this pins the QUIET case: no sibling wrote between probes,
    // so repeated checks stay warning-free (the first probe
    // captures the baseline silently, the second sees no advance).
    let tmp = TempDir::new().unwrap();
    let mut engine = build_demo_engine(&tmp);
    let warnings = engine.reload_if_stale(None);
    assert!(warnings.is_empty());
    let warnings = engine.reload_if_stale(Some("specs"));
    assert!(warnings.is_empty());
}

#[test]
fn reload_if_stale_short_circuits_for_unknown_mem_filter() {
    // Filtering by an unknown mem produces zero candidates;
    // the method returns an empty Vec without panicking.
    let tmp = TempDir::new().unwrap();
    let mut engine = build_demo_engine(&tmp);
    let warnings = engine.reload_if_stale(Some("does-not-exist"));
    assert!(warnings.is_empty());
}

/// Test fixture: a `MemBackend` whose `current_head` and
/// (read-side) entity surface are externally mutable so a test
/// can simulate a sibling writer advancing the head between
/// drift-check probes. Write methods are no-ops; the engine's
/// drift-check path never invokes them.
struct ManualHeadBackend {
    head: std::sync::Mutex<Option<String>>,
    entities: std::sync::Mutex<Vec<(PathBuf, Vec<u8>)>>,
}

impl ManualHeadBackend {
    fn new(initial_head: Option<&str>) -> Self {
        Self {
            head: std::sync::Mutex::new(initial_head.map(String::from)),
            entities: std::sync::Mutex::new(Vec::new()),
        }
    }

    fn set_head(&self, head: Option<&str>) {
        *self.head.lock().unwrap() = head.map(String::from);
    }
}

impl MemBackend for ManualHeadBackend {
    fn list_entities(&self) -> Result<Vec<PathBuf>, BackendError> {
        Ok(self
            .entities
            .lock()
            .unwrap()
            .iter()
            .map(|(p, _)| p.clone())
            .collect())
    }
    fn read_entity(&self, rel: &Path) -> Result<Option<Vec<u8>>, BackendError> {
        Ok(self
            .entities
            .lock()
            .unwrap()
            .iter()
            .find(|(p, _)| p == rel)
            .map(|(_, b)| b.clone()))
    }
    fn write_entity(&self, _: &Path, _: &[u8]) -> Result<(), BackendError> {
        Ok(())
    }
    fn delete_entity(&self, _: &Path) -> Result<(), BackendError> {
        Ok(())
    }
    fn move_entity(&self, _: &Path, _: &Path) -> Result<(), BackendError> {
        Ok(())
    }
    fn commit(
        &self,
        _: &str,
        _: &CommitContext<'_>,
    ) -> Result<crate::storage::CommitId, BackendError> {
        Ok("synthetic".to_string())
    }
    fn stage_provenance(&self, _: &Provenance) -> Result<(), BackendError> {
        Ok(())
    }
    fn append_provenance(&self, _: &Provenance) -> Result<(), BackendError> {
        Ok(())
    }
    fn read_provenance(&self, _: Option<&str>) -> Result<Vec<Provenance>, BackendError> {
        Ok(Vec::new())
    }
    fn current_head(&self) -> Result<Option<String>, BackendError> {
        Ok(self.head.lock().unwrap().clone())
    }
}

/// A `ManualHeadBackend` shared with the test so the head can move
/// after the engine took ownership of the mount.
struct SharedHeadBackend(std::sync::Arc<ManualHeadBackend>);
impl MemBackend for SharedHeadBackend {
    fn list_entities(&self) -> Result<Vec<PathBuf>, BackendError> {
        self.0.list_entities()
    }
    fn read_entity(&self, rel: &Path) -> Result<Option<Vec<u8>>, BackendError> {
        self.0.read_entity(rel)
    }
    fn write_entity(&self, p: &Path, b: &[u8]) -> Result<(), BackendError> {
        self.0.write_entity(p, b)
    }
    fn delete_entity(&self, p: &Path) -> Result<(), BackendError> {
        self.0.delete_entity(p)
    }
    fn move_entity(&self, f: &Path, t: &Path) -> Result<(), BackendError> {
        self.0.move_entity(f, t)
    }
    fn commit(
        &self,
        m: &str,
        c: &CommitContext<'_>,
    ) -> Result<crate::storage::CommitId, BackendError> {
        self.0.commit(m, c)
    }
    fn stage_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
        self.0.stage_provenance(r)
    }
    fn append_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
        self.0.append_provenance(r)
    }
    fn read_provenance(&self, c: Option<&str>) -> Result<Vec<Provenance>, BackendError> {
        self.0.read_provenance(c)
    }
    fn current_head(&self) -> Result<Option<String>, BackendError> {
        self.0.current_head()
    }
}

/// A mount whose branch was missing at boot loads empty and warns
/// `MOUNT_UNBACKED`; when the branch appears (born, fetched, pushed
/// into place by another process) the next probe reloads it, the
/// entities the branch holds are served, the warning is gone, and
/// the reload names the head the branch appeared at.
/// The folder feed through the engine, on a real ledger: an entity
/// created and then renamed arrives once; from a cursor between the
/// two writes it arrives as `renamed` with both ids; either way the
/// event carries the title and type of the entity a reader can
/// fetch, and no event names an id the store cannot serve.
#[test]
fn folder_feed_serves_a_renamed_entity_once_with_its_final_id() {
    use crate::engine::test_helpers::*;
    use crate::engine::{CreateEntityArgs, RenameEntityArgs};
    use crate::storage::FilesystemBackend;
    let tmp = tempfile::TempDir::new().unwrap();
    let mem_dir = tmp.path().to_path_buf();
    let writer = FilesystemBackend::new(mem_dir.clone());
    let mut engine = Engine::from_mounts(vec![(
        folder_mount("specs", mem_dir.clone()),
        Box::new(writer) as Box<dyn MemBackend>,
    )])
    .unwrap();
    engine.set_workspace_root(mem_dir.clone());
    let (actor, client) = cli_actor();

    let args: CreateEntityArgs = empty_create_args("specs", "Alpha One");
    let created = engine
        .create_entity(args, actor, Some(&client), Some("seed"))
        .unwrap();
    let old_id = created.id.clone();
    let keeper = engine
        .create_entity(
            empty_create_args("specs", "Keeper"),
            actor,
            Some(&client),
            Some("seed"),
        )
        .unwrap();
    let between = engine
        .changes_since("specs", "", None)
        .unwrap()
        .head
        .clone();
    // The ledger's millisecond clock must move past `between`.
    std::thread::sleep(std::time::Duration::from_millis(5));
    let renamed = engine
        .rename_entity(
            RenameEntityArgs {
                id: old_id.clone(),
                expected_hash: crate::ExpectedHash::Current(created.content_hash.clone()),
                new_title: "Alpha Two".to_string(),
            },
            actor,
            Some(&client),
            Some("rename"),
        )
        .unwrap();
    let new_id = renamed.new_id.clone();

    // Whole window: one added under the final id, one for keeper.
    let whole = engine.changes_since("specs", "", None).unwrap();
    let mut ids: Vec<(String, String)> = whole
        .changes
        .iter()
        .map(|c| (c.action().to_string(), c.primary_id().to_string()))
        .collect();
    ids.sort();
    assert_eq!(
        ids,
        vec![
            ("added".to_string(), new_id.0.clone()),
            ("added".to_string(), keeper.id.0.clone()),
        ],
        "{:?}",
        whole.changes
    );
    for c in &whole.changes {
        assert!(
            engine
                .store()
                .get(&EntityId(c.primary_id().to_string()))
                .is_some(),
            "every event names an id the store serves: {c:?}"
        );
        let titled = matches!(
            c,
            crate::ops::ChangeEnvelope::Added { title: Some(_), .. }
                | crate::ops::ChangeEnvelope::Updated { title: Some(_), .. }
                | crate::ops::ChangeEnvelope::Renamed { title: Some(_), .. }
        );
        assert!(titled && c.entity_type().is_some(), "{c:?}");
    }

    // From between the add and the rename: the renamed event alone.
    let later = engine.changes_since("specs", &between, None).unwrap();
    match later.changes.as_slice() {
        [
            crate::ops::ChangeEnvelope::Renamed {
                from_id,
                to_id,
                title,
                entity_type,
            },
        ] => {
            assert_eq!(from_id, &old_id);
            assert_eq!(to_id, &new_id);
            assert_eq!(title.as_deref(), Some("Alpha Two"));
            assert!(entity_type.is_some());
        }
        other => panic!("expected the renamed event alone, got {other:?}"),
    }
    // The head round-trips to silence.
    let quiet = engine.changes_since("specs", &later.head, None).unwrap();
    assert!(quiet.changes.is_empty(), "{:?}", quiet.changes);
}

#[test]
fn reload_if_stale_reloads_a_mem_whose_branch_appeared() {
    let shared = std::sync::Arc::new(ManualHeadBackend::new(None));
    let mount = Mount {
        mem: "specs".to_string(),
        schema: Some(pin("default")),
        storage: MountStorage::Folder {
            path: PathBuf::from("/dev/null"),
        },
        capability: MountCapability::Write,
        lifecycle: MountLifecycle::Eager,
        cross_linkable: true,
        migration_target: None,
    };
    let mut engine =
        Engine::from_mounts(vec![(mount, Box::new(SharedHeadBackend(shared.clone())))]).unwrap();
    assert!(
        engine
            .unbacked_mount_warnings()
            .iter()
            .any(|w| w.code() == "MOUNT_UNBACKED"),
        "an entity-less mount reads empty: {:?}",
        engine.unbacked_mount_warnings()
    );
    assert!(engine.reload_if_stale(Some("specs")).is_empty());

    // The branch appears with one entity, written by someone else.
    shared.entities.lock().unwrap().push((
        PathBuf::from("first.md"),
        b"---\ntype: spec\n---\n# First\n\n## Identity\n\nfirst.\n\n## Purpose\n\nexists.\n"
            .to_vec(),
    ));
    shared.set_head(Some("aaa"));

    let warnings = engine.reload_if_stale(Some("specs"));
    match warnings.as_slice() {
        [
            crate::ops::WarningHint::MemReloaded {
                mem,
                old_head,
                new_head,
                entities_loaded,
            },
        ] => {
            assert_eq!(mem, "specs");
            assert!(old_head.is_empty(), "no head was cached: {old_head}");
            assert_eq!(new_head, "aaa");
            assert_eq!(*entities_loaded, 1);
        }
        other => panic!("expected one MemReloaded, got {other:?}"),
    }
    assert!(
        engine
            .store()
            .get(&EntityId::new("specs", "first"))
            .is_some(),
        "the appeared branch's entity is served"
    );
    assert!(
        !engine
            .unbacked_mount_warnings()
            .iter()
            .any(|w| w.code() == "MOUNT_UNBACKED"),
        "a mem holding an entity no longer reads empty: {:?}",
        engine.unbacked_mount_warnings()
    );
    // Cached now; the next probe is quiet.
    assert!(engine.reload_if_stale(Some("specs")).is_empty());
}

#[test]
fn reload_if_stale_emits_mem_reloaded_when_head_advances() {
    // Use an Arc<ManualHeadBackend> so the test retains a handle
    // for mutation after the engine has taken ownership of a
    // Box<dyn MemBackend> wrapper around it.
    struct ArcBackend(std::sync::Arc<ManualHeadBackend>);
    impl MemBackend for ArcBackend {
        fn list_entities(&self) -> Result<Vec<PathBuf>, BackendError> {
            self.0.list_entities()
        }
        fn read_entity(&self, rel: &Path) -> Result<Option<Vec<u8>>, BackendError> {
            self.0.read_entity(rel)
        }
        fn write_entity(&self, p: &Path, b: &[u8]) -> Result<(), BackendError> {
            self.0.write_entity(p, b)
        }
        fn delete_entity(&self, p: &Path) -> Result<(), BackendError> {
            self.0.delete_entity(p)
        }
        fn move_entity(&self, f: &Path, t: &Path) -> Result<(), BackendError> {
            self.0.move_entity(f, t)
        }
        fn commit(
            &self,
            m: &str,
            c: &CommitContext<'_>,
        ) -> Result<crate::storage::CommitId, BackendError> {
            self.0.commit(m, c)
        }
        fn stage_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.0.stage_provenance(r)
        }
        fn append_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.0.append_provenance(r)
        }
        fn read_provenance(&self, c: Option<&str>) -> Result<Vec<Provenance>, BackendError> {
            self.0.read_provenance(c)
        }
        fn current_head(&self) -> Result<Option<String>, BackendError> {
            self.0.current_head()
        }
    }

    let shared = std::sync::Arc::new(ManualHeadBackend::new(Some("aaa")));
    let backend = Box::new(ArcBackend(shared.clone()));
    let mount = Mount {
        mem: "specs".to_string(),
        schema: Some(pin("default")),
        storage: MountStorage::Folder {
            path: PathBuf::from("/dev/null"),
        },
        capability: MountCapability::Write,
        lifecycle: MountLifecycle::Eager,
        cross_linkable: true,
        migration_target: None,
    };
    let mut engine = Engine::from_mounts(vec![(mount, backend)]).unwrap();

    // No drift on first probe — cached==new.
    let warnings = engine.reload_if_stale(Some("specs"));
    assert!(warnings.is_empty());

    // Sibling writer advances the head.
    shared.set_head(Some("bbb"));

    let warnings = engine.reload_if_stale(Some("specs"));
    assert_eq!(warnings.len(), 1);
    match &warnings[0] {
        crate::ops::WarningHint::MemReloaded {
            mem,
            old_head,
            new_head,
            ..
        } => {
            assert_eq!(mem, "specs");
            assert_eq!(old_head, "aaa");
            assert_eq!(new_head, "bbb");
        }
        other => panic!("expected MemReloaded, got {other:?}"),
    }

    // Drift cleared — the engine's cached head now matches the
    // backend's current head; another probe is a no-op.
    let warnings = engine.reload_if_stale(Some("specs"));
    assert!(warnings.is_empty());
}

#[test]
fn mem_drifted_tracks_sibling_advance_until_reload() {
    // The read-only drift probe (built for the retired macOS app's roster): it reports
    // `true` once a sibling writer advances the backend past the
    // engine's cached head, *without* itself reloading, and clears
    // after the engine re-reads.
    struct ArcBackend(std::sync::Arc<ManualHeadBackend>);
    impl MemBackend for ArcBackend {
        fn list_entities(&self) -> Result<Vec<PathBuf>, BackendError> {
            self.0.list_entities()
        }
        fn read_entity(&self, rel: &Path) -> Result<Option<Vec<u8>>, BackendError> {
            self.0.read_entity(rel)
        }
        fn write_entity(&self, p: &Path, b: &[u8]) -> Result<(), BackendError> {
            self.0.write_entity(p, b)
        }
        fn delete_entity(&self, p: &Path) -> Result<(), BackendError> {
            self.0.delete_entity(p)
        }
        fn move_entity(&self, f: &Path, t: &Path) -> Result<(), BackendError> {
            self.0.move_entity(f, t)
        }
        fn commit(
            &self,
            m: &str,
            c: &CommitContext<'_>,
        ) -> Result<crate::storage::CommitId, BackendError> {
            self.0.commit(m, c)
        }
        fn stage_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.0.stage_provenance(r)
        }
        fn append_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.0.append_provenance(r)
        }
        fn read_provenance(&self, c: Option<&str>) -> Result<Vec<Provenance>, BackendError> {
            self.0.read_provenance(c)
        }
        fn current_head(&self) -> Result<Option<String>, BackendError> {
            self.0.current_head()
        }
    }

    let shared = std::sync::Arc::new(ManualHeadBackend::new(Some("aaa")));
    let backend = Box::new(ArcBackend(shared.clone()));
    let mount = Mount {
        mem: "specs".to_string(),
        schema: Some(pin("default")),
        storage: MountStorage::Folder {
            path: PathBuf::from("/dev/null"),
        },
        capability: MountCapability::Write,
        lifecycle: MountLifecycle::Eager,
        cross_linkable: true,
        migration_target: None,
    };
    let mut engine = Engine::from_mounts(vec![(mount, backend)]).unwrap();

    // Fresh boot: cached == live, no drift.
    assert!(!engine.mem_drifted("specs").unwrap());

    // Sibling writer advances the head — drift is visible WITHOUT a reload.
    shared.set_head(Some("bbb"));
    assert!(engine.mem_drifted("specs").unwrap());
    // Probing did not reload — still drifted on a second read.
    assert!(engine.mem_drifted("specs").unwrap());

    // Re-reading through the engine clears it.
    let _ = engine.reload_if_stale(Some("specs"));
    assert!(!engine.mem_drifted("specs").unwrap());

    // Unknown mem errors rather than reporting a bogus `false`.
    assert!(matches!(
        engine.mem_drifted("nope"),
        Err(EngineError::UnknownMem { .. })
    ));
}

#[test]
fn reload_one_mem_report_head_before_is_prior_cursor_and_advances() {
    // Regression for the reload→changes_since recipe. `head_before`
    // must report the engine's PRIOR cursor (the SHA it last knew),
    // not the post-drift on-disk tip — otherwise
    // `changes_since(since=head_before)` spans an empty range in
    // exactly the sibling-drift case the recipe targets. The reload
    // must also advance the cursor to the new tip so the next
    // staleness probe is a no-op rather than a spurious reload.
    struct ArcBackend(std::sync::Arc<ManualHeadBackend>);
    impl MemBackend for ArcBackend {
        fn list_entities(&self) -> Result<Vec<PathBuf>, BackendError> {
            self.0.list_entities()
        }
        fn read_entity(&self, rel: &Path) -> Result<Option<Vec<u8>>, BackendError> {
            self.0.read_entity(rel)
        }
        fn write_entity(&self, p: &Path, b: &[u8]) -> Result<(), BackendError> {
            self.0.write_entity(p, b)
        }
        fn delete_entity(&self, p: &Path) -> Result<(), BackendError> {
            self.0.delete_entity(p)
        }
        fn move_entity(&self, f: &Path, t: &Path) -> Result<(), BackendError> {
            self.0.move_entity(f, t)
        }
        fn commit(
            &self,
            m: &str,
            c: &CommitContext<'_>,
        ) -> Result<crate::storage::CommitId, BackendError> {
            self.0.commit(m, c)
        }
        fn stage_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.0.stage_provenance(r)
        }
        fn append_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.0.append_provenance(r)
        }
        fn read_provenance(&self, c: Option<&str>) -> Result<Vec<Provenance>, BackendError> {
            self.0.read_provenance(c)
        }
        fn current_head(&self) -> Result<Option<String>, BackendError> {
            self.0.current_head()
        }
    }

    let shared = std::sync::Arc::new(ManualHeadBackend::new(Some("aaa")));
    let backend = Box::new(ArcBackend(shared.clone()));
    let mount = Mount {
        mem: "specs".to_string(),
        schema: Some(pin("default")),
        storage: MountStorage::Folder {
            path: PathBuf::from("/dev/null"),
        },
        capability: MountCapability::Write,
        lifecycle: MountLifecycle::Eager,
        cross_linkable: true,
        migration_target: None,
    };
    let mut engine = Engine::from_mounts(vec![(mount, backend)]).unwrap();

    // Sibling writer advances the head past the engine's cursor.
    shared.set_head(Some("bbb"));

    let report = engine.reload_one_mem_report("specs").unwrap();
    // head_before is the prior cursor "aaa", not the drifted tip.
    assert_eq!(report.head_before, "aaa");
    assert_eq!(report.head_after, "bbb");

    // Cursor advanced to "bbb": a follow-up staleness probe is a
    // no-op, not a spurious MEM_RELOADED.
    let warnings = engine.reload_if_stale(Some("specs"));
    assert!(
        warnings.is_empty(),
        "cursor should have advanced to bbb, got {warnings:?}"
    );
}

#[test]
fn reload_if_stale_fires_every_call_no_throttle() {
    // Two back-to-back probes with the head advancing between
    // them: the second must reload and warn. There is no throttle
    // window — the ref check is the correctness floor.
    let shared = std::sync::Arc::new(ManualHeadBackend::new(Some("aaa")));
    struct ArcBackend(std::sync::Arc<ManualHeadBackend>);
    impl MemBackend for ArcBackend {
        fn list_entities(&self) -> Result<Vec<PathBuf>, BackendError> {
            self.0.list_entities()
        }
        fn read_entity(&self, rel: &Path) -> Result<Option<Vec<u8>>, BackendError> {
            self.0.read_entity(rel)
        }
        fn write_entity(&self, p: &Path, b: &[u8]) -> Result<(), BackendError> {
            self.0.write_entity(p, b)
        }
        fn delete_entity(&self, p: &Path) -> Result<(), BackendError> {
            self.0.delete_entity(p)
        }
        fn move_entity(&self, f: &Path, t: &Path) -> Result<(), BackendError> {
            self.0.move_entity(f, t)
        }
        fn commit(
            &self,
            m: &str,
            c: &CommitContext<'_>,
        ) -> Result<crate::storage::CommitId, BackendError> {
            self.0.commit(m, c)
        }
        fn stage_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.0.stage_provenance(r)
        }
        fn append_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.0.append_provenance(r)
        }
        fn read_provenance(&self, c: Option<&str>) -> Result<Vec<Provenance>, BackendError> {
            self.0.read_provenance(c)
        }
        fn current_head(&self) -> Result<Option<String>, BackendError> {
            self.0.current_head()
        }
    }

    let backend = Box::new(ArcBackend(shared.clone()));
    let mount = Mount {
        mem: "specs".to_string(),
        schema: Some(pin("default")),
        storage: MountStorage::Folder {
            path: PathBuf::from("/dev/null"),
        },
        capability: MountCapability::Write,
        lifecycle: MountLifecycle::Eager,
        cross_linkable: true,
        migration_target: None,
    };
    let mut engine = Engine::from_mounts(vec![(mount, backend)]).unwrap();

    // First probe observes cached==new; no warning.
    let warnings = engine.reload_if_stale(Some("specs"));
    assert!(warnings.is_empty());

    // Sibling advances head — the very next probe reloads and
    // warns, with no throttle window to mask it.
    shared.set_head(Some("bbb"));
    let warnings = engine.reload_if_stale(Some("specs"));
    assert_eq!(
        warnings.len(),
        1,
        "no throttle window — the moved ref reloads on the next probe"
    );
}

/// An id deleted and created again is alive again for the folder feed: a
/// later rename of it pairs, and arrives as one renamed event, where the
/// earlier delete kept the old id marked gone and the rename surfaced as
/// a removed id plus an updated one.
#[test]
fn folder_feed_pairs_a_rename_of_an_id_that_was_deleted_and_created_again() {
    use crate::engine::test_helpers::*;
    use crate::engine::{CreateEntityArgs, DeleteEntityArgs, RenameEntityArgs};
    use crate::storage::FilesystemBackend;
    let tmp = tempfile::TempDir::new().unwrap();
    let mem_dir = tmp.path().to_path_buf();
    let writer = FilesystemBackend::new(mem_dir.clone());
    let mut engine = Engine::from_mounts(vec![(
        folder_mount("specs", mem_dir.clone()),
        Box::new(writer) as Box<dyn MemBackend>,
    )])
    .unwrap();
    engine.set_workspace_root(mem_dir.clone());
    let (actor, client) = cli_actor();
    let tick = || std::thread::sleep(std::time::Duration::from_millis(5));

    let first: CreateEntityArgs = empty_create_args("specs", "Alpha");
    let first = engine
        .create_entity(first, actor, Some(&client), None)
        .unwrap();
    tick();
    engine
        .delete_entity(
            DeleteEntityArgs {
                id: first.id.clone(),
                expected_hash: crate::ExpectedHash::Current(first.content_hash.clone()),
            },
            actor,
            Some(&client),
            None,
        )
        .unwrap();
    tick();
    let again = engine
        .create_entity(
            empty_create_args("specs", "Alpha"),
            actor,
            Some(&client),
            None,
        )
        .unwrap();
    let between = engine
        .changes_since("specs", "", None)
        .unwrap()
        .head
        .clone();
    tick();
    let renamed = engine
        .rename_entity(
            RenameEntityArgs {
                id: again.id.clone(),
                expected_hash: crate::ExpectedHash::Current(again.content_hash.clone()),
                new_title: "Beta".to_string(),
            },
            actor,
            Some(&client),
            None,
        )
        .unwrap();

    let later = engine.changes_since("specs", &between, None).unwrap();
    match later.changes.as_slice() {
        [crate::ops::ChangeEnvelope::Renamed { from_id, to_id, .. }] => {
            assert_eq!(from_id, &again.id);
            assert_eq!(to_id, &renamed.new_id);
        }
        other => panic!("expected one renamed event, got {other:?}"),
    }
}

/// Taking a moved head (after a pull or a reset) moves the cursor only
/// with a reload that went through. A reload that cannot read the mem
/// leaves the cursor where it was and marks the view behind, so the next
/// operation reloads again and a write refuses meanwhile; the cursor used
/// to move first, which made the stale view read as current.
#[test]
fn a_moved_head_whose_reload_fails_leaves_the_view_marked_behind() {
    struct Flaky {
        inner: std::sync::Arc<ManualHeadBackend>,
        unreadable: std::sync::Arc<std::sync::atomic::AtomicBool>,
    }
    impl MemBackend for Flaky {
        fn list_entities(&self) -> Result<Vec<PathBuf>, BackendError> {
            self.inner.list_entities()
        }
        fn read_entity(&self, rel: &Path) -> Result<Option<Vec<u8>>, BackendError> {
            self.inner.read_entity(rel)
        }
        fn read_all_entities(&self) -> Result<Vec<crate::backend::EntityRead>, BackendError> {
            if self.unreadable.load(std::sync::atomic::Ordering::SeqCst) {
                return Ok(vec![(
                    PathBuf::from("lost.md"),
                    Err(BackendError::Other("blob unreadable".to_string())),
                )]);
            }
            Ok(Vec::new())
        }
        fn write_entity(&self, p: &Path, b: &[u8]) -> Result<(), BackendError> {
            self.inner.write_entity(p, b)
        }
        fn delete_entity(&self, p: &Path) -> Result<(), BackendError> {
            self.inner.delete_entity(p)
        }
        fn move_entity(&self, f: &Path, t: &Path) -> Result<(), BackendError> {
            self.inner.move_entity(f, t)
        }
        fn commit(
            &self,
            m: &str,
            c: &CommitContext<'_>,
        ) -> Result<crate::storage::CommitId, BackendError> {
            self.inner.commit(m, c)
        }
        fn stage_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.inner.stage_provenance(r)
        }
        fn append_provenance(&self, r: &Provenance) -> Result<(), BackendError> {
            self.inner.append_provenance(r)
        }
        fn read_provenance(&self, c: Option<&str>) -> Result<Vec<Provenance>, BackendError> {
            self.inner.read_provenance(c)
        }
        fn current_head(&self) -> Result<Option<String>, BackendError> {
            self.inner.current_head()
        }
    }
    let inner = std::sync::Arc::new(ManualHeadBackend::new(Some("aaa")));
    let unreadable = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
    let mount = Mount {
        mem: "specs".to_string(),
        schema: Some(pin("default")),
        storage: MountStorage::Folder {
            path: PathBuf::from("/dev/null"),
        },
        capability: MountCapability::Write,
        lifecycle: MountLifecycle::Eager,
        cross_linkable: true,
        migration_target: None,
    };
    let backend = Box::new(Flaky {
        inner: inner.clone(),
        unreadable: unreadable.clone(),
    });
    let mut engine = Engine::from_mounts(vec![(mount, backend)]).unwrap();
    let _ = engine.reload_if_stale(Some("specs"));
    let before = engine.mounts[0].last_known_head.clone();

    inner.set_head(Some("bbb"));
    unreadable.store(true, std::sync::atomic::Ordering::SeqCst);
    assert!(engine.adopt_moved_head(0, "specs", "bbb").is_err());
    assert_eq!(
        engine.mounts[0].last_known_head, before,
        "the cursor did not move"
    );
    assert!(
        engine.mounts[0].reload_failure.is_some(),
        "the view is marked behind"
    );

    unreadable.store(false, std::sync::atomic::Ordering::SeqCst);
    engine.adopt_moved_head(0, "specs", "bbb").unwrap();
    assert_eq!(engine.mounts[0].last_known_head.as_deref(), Some("bbb"));
    assert!(engine.mounts[0].reload_failure.is_none());
}