aidaemon-memory 0.27.1

Swappable long-term memory for aidaemon agents: the MemoryStore traits plus SQLite and embedding implementations
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
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
//! Memory store conformance kit (feature `conformance`).
//!
//! Exercises the documented [`MemoryStore`] contract against fresh stores
//! made by a factory, so a third-party backend (pgvector, Qdrant, a document
//! DB) can prove it behaves like the bundled stores:
//!
//! ```
//! # #[tokio::main(flavor = "current_thread")]
//! # async fn main() {
//! use std::sync::Arc;
//! use aidaemon_memory::conformance::check_store;
//! use aidaemon_memory::{InMemoryMemoryStore, MemoryStore};
//!
//! check_store(|| async { Arc::new(InMemoryMemoryStore::new()) as Arc<dyn MemoryStore> })
//!     .await
//!     .assert_all_passed();
//! # }
//! ```
//!
//! Every check gets its own store from the factory. Facts arrive already
//! normalized (as [`crate::Memory`] sends them) and all timestamps are whole
//! seconds, so stores that keep microseconds compare exactly. Check names
//! are the constants in [`store_checks`], each documenting its rule.

use std::collections::HashMap;
use std::future::Future;
use std::sync::Arc;

use aidaemon_core::conformance::ConformanceReport;
use chrono::{DateTime, Duration, TimeZone, Utc};

use crate::store::MemoryStore;
use crate::types::{
    DecayReport, Episode, Fact, FactAccess, NewEpisode, NewFact, RecordKind, RetentionConfig,
    StoredEmbedding, UpsertOutcome,
};

/// Stable names of the checks run by [`check_store`].
pub mod store_checks {
    /// `upsert_fact` of a new key returns `Inserted` with a non-empty id;
    /// the fact is active, keeps its fields, and starts with zero recalls.
    pub const UPSERT_INSERTS_NEW_FACT: &str = "store.upsert_inserts_new_fact";
    /// Upserting the same value (modulo case and whitespace, see
    /// [`crate::same_value`]) returns `Reinforced` with the same id, adds no
    /// version, refreshes `updated_at` and keeps the higher confidence.
    pub const SAME_VALUE_REINFORCES: &str = "store.same_value_reinforces";
    /// Upserting a different value returns `Superseded` (new id, previous id
    /// and value); the old value is only in `fact_history` (oldest first,
    /// with `superseded_at` set) and never in `active_facts` or searches.
    pub const DIFFERENT_VALUE_SUPERSEDES: &str = "store.different_value_supersedes";
    /// Scopes are hard boundaries: facts, history, lexical scores, searches,
    /// episodes and supersession never cross scopes.
    pub const SCOPE_ISOLATION: &str = "store.scope_isolation";
    /// `supersede_fact` (forget) hides the active fact, keeps it in history,
    /// returns `true` once and `false` when nothing is active.
    pub const FORGET_SUPERSEDES: &str = "store.forget_supersedes";
    /// `insert_episode` returns the stored episode (non-empty id, fields
    /// kept, zero recalls); `episodes` lists it; `episode_lexical_scores`
    /// scores matching episodes in `(0, 1]` and omits non-matching ones.
    pub const EPISODES_ADD_LIST_SEARCH: &str = "store.episodes_add_list_search";
    /// `fact_lexical_scores` returns scores in `(0, 1]` only for active facts
    /// that match the query; non-matching facts are absent (or 0).
    pub const LEXICAL_SCORES_ONLY_MATCHING: &str = "store.lexical_scores_only_matching";
    /// When `search_facts` / `search_episodes` return `Some`, results are in
    /// scope, active (facts), and at most `limit` long. `None` passes.
    pub const NATIVE_SEARCH_CONTRACT: &str = "store.native_search_contract";
    /// `record_fact_recalls` bumps `recall_count` by one per call and sets
    /// `last_recalled_at`; unknown ids are ignored; other facts unchanged.
    pub const FACT_RECALL_BOOKKEEPING: &str = "store.fact_recall_bookkeeping";
    /// `record_episode_recalls` bumps `recall_count` and sets
    /// `last_recalled_at`, like facts (episode retention depends on it).
    pub const EPISODE_RECALL_BOOKKEEPING: &str = "store.episode_recall_bookkeeping";
    /// Fact and episode embeddings come back exactly as stored: same model
    /// string, same length, every `f32` equal (including subnormals and
    /// extremes); `None` stays `None`.
    pub const EMBEDDINGS_ROUND_TRIP: &str = "store.embeddings_round_trip";
    /// `stats` counts active facts, superseded facts, episodes and distinct
    /// scopes.
    pub const STATS_COUNTS: &str = "store.stats_counts";
    /// `decay` with every [`crate::RetentionConfig`] window `None` changes
    /// nothing and reports nothing.
    pub const DECAY_DISABLED_IS_NOOP: &str = "store.decay_disabled_is_noop";
    /// `decay` with the default config either leaves everything alone (the
    /// trait default, for backends that expire data themselves; noted) or
    /// applies each documented step exactly: superseded versions past the
    /// retention window deleted, never-recalled old episodes deleted, stale
    /// fact recalls lowered by a tenth (at least one), stale episode recalls
    /// lowered by one. The report matches what changed, and active facts or
    /// recently recalled rows are never touched.
    pub const DECAY_FOLLOWS_RETENTION: &str = "store.decay_follows_retention";
    /// `fact_access` / `episode_access` are either empty (the trait default:
    /// records stay private; noted) or return exactly what was written, for
    /// the asked scope only. Restating a fact never widens its access, and a
    /// new version takes the access it was written with.
    pub const ACCESS_LEVELS_ROUND_TRIP: &str = "store.access_levels_round_trip";
    /// `fact_graph_scores` is either empty for related facts (the trait
    /// default: no graph signal; noted) or scores in `(0, 1]`, for active
    /// facts of the asked scope only, and nothing for unknown seeds.
    pub const GRAPH_SCORES_CONTRACT: &str = "store.graph_scores_contract";
    /// `stale_embeddings` is either empty (the trait default: old rows stay
    /// lexical-only; noted) or lists the active records with no vector or
    /// another model's, in ascending id order, bounded by the limit, after
    /// `after_id`. `replace_embedding` stores the vector, leaves `updated_at`
    /// alone, and returns `false` when the record's model has since changed.
    pub const EMBEDDING_REFRESH_CONTRACT: &str = "store.embedding_refresh_contract";
}

/// Run every store check (see [`store_checks`]); each check uses a fresh
/// store from `make`.
pub async fn check_store<F, Fut>(make: F) -> ConformanceReport
where
    F: Fn() -> Fut,
    Fut: Future<Output = Arc<dyn MemoryStore>>,
{
    use store_checks::*;

    let mut report = ConformanceReport::new("memory store");
    report.record(UPSERT_INSERTS_NEW_FACT, upsert_inserts(make().await).await);
    report.record(SAME_VALUE_REINFORCES, reinforces(make().await).await);
    report.record(DIFFERENT_VALUE_SUPERSEDES, supersedes(make().await).await);
    report.record(SCOPE_ISOLATION, scope_isolation(make().await).await);
    report.record(FORGET_SUPERSEDES, forget(make().await).await);
    report.record(EPISODES_ADD_LIST_SEARCH, episodes(make().await).await);
    report.record(LEXICAL_SCORES_ONLY_MATCHING, lexical(make().await).await);
    match native_search(make().await).await {
        Ok(notes) => {
            report.pass(NATIVE_SEARCH_CONTRACT);
            for note in notes {
                report.note(format!("{NATIVE_SEARCH_CONTRACT}: {note}"));
            }
        }
        Err(e) => report.fail(NATIVE_SEARCH_CONTRACT, e),
    }
    report.record(FACT_RECALL_BOOKKEEPING, fact_recalls(make().await).await);
    report.record(
        EPISODE_RECALL_BOOKKEEPING,
        episode_recalls(make().await).await,
    );
    report.record(EMBEDDINGS_ROUND_TRIP, embeddings(make().await).await);
    report.record(STATS_COUNTS, stats(make().await).await);
    report.record(DECAY_DISABLED_IS_NOOP, decay_disabled(make().await).await);
    match decay_default(make().await).await {
        Ok(true) => report.pass(DECAY_FOLLOWS_RETENTION),
        Ok(false) => {
            report.pass(DECAY_FOLLOWS_RETENTION);
            report.note(format!(
                "{DECAY_FOLLOWS_RETENTION}: decay changed nothing (the trait default); \
                 make sure the backend expires data itself"
            ));
        }
        Err(e) => report.fail(DECAY_FOLLOWS_RETENTION, e),
    }
    match access_levels(make().await).await {
        Ok(true) => report.pass(ACCESS_LEVELS_ROUND_TRIP),
        Ok(false) => {
            report.pass(ACCESS_LEVELS_ROUND_TRIP);
            report.note(format!(
                "{ACCESS_LEVELS_ROUND_TRIP}: the store keeps no access levels, so its records \
                 are hidden from shared channels"
            ));
        }
        Err(e) => report.fail(ACCESS_LEVELS_ROUND_TRIP, e),
    }
    match graph_scores(make().await).await {
        Ok(true) => report.pass(GRAPH_SCORES_CONTRACT),
        Ok(false) => {
            report.pass(GRAPH_SCORES_CONTRACT);
            report.note(format!(
                "{GRAPH_SCORES_CONTRACT}: the store has no graph, so the graph signal stays 0"
            ));
        }
        Err(e) => report.fail(GRAPH_SCORES_CONTRACT, e),
    }
    match embedding_refresh(make().await).await {
        Ok(true) => report.pass(EMBEDDING_REFRESH_CONTRACT),
        Ok(false) => {
            report.pass(EMBEDDING_REFRESH_CONTRACT);
            report.note(format!(
                "{EMBEDDING_REFRESH_CONTRACT}: the store cannot refresh vectors, so rows from \
                 an old model stay lexical-only"
            ));
        }
        Err(e) => report.fail(EMBEDDING_REFRESH_CONTRACT, e),
    }
    report
}

const SCOPE: &str = "conformance:synthetic-user-1";
const OTHER_SCOPE: &str = "conformance:synthetic-user-2";

type Check<T = ()> = Result<T, String>;

fn t0() -> DateTime<Utc> {
    Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0)
        .single()
        .expect("valid timestamp")
}

fn at(seconds: i64) -> DateTime<Utc> {
    t0() + Duration::seconds(seconds)
}

fn fact(scope: &str, category: &str, key: &str, value: &str) -> NewFact {
    NewFact::new(scope, category, key, value)
        .normalized()
        .expect("kit facts are valid")
}

fn episode(scope: &str, summary: &str, topics: &[&str]) -> NewEpisode {
    NewEpisode {
        scope: scope.to_string(),
        session_id: format!("{scope}:session"),
        summary: summary.to_string(),
        topics: topics.iter().map(|t| t.to_string()).collect(),
        importance: 0.5,
    }
}

/// `?` for store calls, naming the call that failed.
macro_rules! call {
    ($what:expr, $fut:expr) => {
        $fut.await.map_err(|e| format!("{} failed: {e}", $what))?
    };
}

fn ensure(cond: bool, message: impl FnOnce() -> String) -> Check {
    if cond { Ok(()) } else { Err(message()) }
}

fn close(a: f32, b: f32) -> bool {
    (a - b).abs() < 1e-5
}

async fn active(store: &dyn MemoryStore, scope: &str) -> Check<Vec<Fact>> {
    Ok(call!("active_facts", store.active_facts(scope))
        .into_iter()
        .map(|r| r.fact)
        .collect())
}

async fn upsert_inserts(store: Arc<dyn MemoryStore>) -> Check {
    let new = fact(SCOPE, "person", "daughters", "Mia and Zoe").confidence(0.8);
    let outcome = call!("upsert_fact", store.upsert_fact(new, None, at(0)));
    let UpsertOutcome::Inserted { id } = outcome else {
        return Err(format!("expected Inserted, got {outcome:?}"));
    };
    ensure(!id.is_empty(), || "Inserted id is empty".into())?;
    let facts = active(&*store, SCOPE).await?;
    ensure(facts.len() == 1, || {
        format!("expected 1 active fact, got {}", facts.len())
    })?;
    let f = &facts[0];
    ensure(f.id == id, || {
        format!("active fact id {:?} != {id:?}", f.id)
    })?;
    ensure(
        (
            f.scope.as_str(),
            f.category.as_str(),
            f.key.as_str(),
            f.value.as_str(),
        ) == (SCOPE, "person", "daughters", "Mia and Zoe"),
        || format!("fields not preserved: {f:?}"),
    )?;
    ensure(close(f.confidence, 0.8), || {
        format!("confidence {} != 0.8", f.confidence)
    })?;
    ensure(
        f.is_active() && f.recall_count == 0 && f.last_recalled_at.is_none(),
        || format!("a new fact must be active with no recalls: {f:?}"),
    )
}

async fn reinforces(store: Arc<dyn MemoryStore>) -> Check {
    let key = ("preference", "theme");
    let first = call!(
        "upsert_fact",
        store.upsert_fact(
            fact(SCOPE, key.0, key.1, "Dark mode").confidence(0.6),
            None,
            at(0)
        )
    );
    let id = first.id().to_string();
    let again = call!(
        "upsert_fact (same value)",
        store.upsert_fact(
            fact(SCOPE, key.0, key.1, "dark  MODE").confidence(0.9),
            None,
            at(60)
        )
    );
    ensure(
        again == UpsertOutcome::Reinforced { id: id.clone() },
        || format!("expected Reinforced {{ id: {id:?} }}, got {again:?}"),
    )?;
    let lower = call!(
        "upsert_fact (same value, lower confidence)",
        store.upsert_fact(
            fact(SCOPE, key.0, key.1, "Dark mode").confidence(0.3),
            None,
            at(120)
        )
    );
    ensure(
        lower == UpsertOutcome::Reinforced { id: id.clone() },
        || format!("expected Reinforced again, got {lower:?}"),
    )?;
    let facts = active(&*store, SCOPE).await?;
    ensure(facts.len() == 1, || {
        format!("reinforcing created versions: {} active facts", facts.len())
    })?;
    let history = call!("fact_history", store.fact_history(SCOPE, key.0, key.1));
    ensure(history.len() == 1, || {
        format!(
            "reinforcing must not add versions; history has {}",
            history.len()
        )
    })?;
    let f = &facts[0];
    ensure(f.updated_at == at(120), || {
        format!(
            "updated_at {} was not refreshed to {}",
            f.updated_at,
            at(120)
        )
    })?;
    ensure(close(f.confidence, 0.9), || {
        format!(
            "confidence {} should be the highest seen (0.9)",
            f.confidence
        )
    })
}

async fn supersedes(store: Arc<dyn MemoryStore>) -> Check {
    let first = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "work", "employer", "Acme Corp"), None, at(0))
    );
    let old_id = first.id().to_string();
    let second = call!(
        "upsert_fact (new value)",
        store.upsert_fact(fact(SCOPE, "work", "employer", "Globex"), None, at(60))
    );
    let UpsertOutcome::Superseded {
        id,
        previous_id,
        previous_value,
    } = &second
    else {
        return Err(format!("expected Superseded, got {second:?}"));
    };
    ensure(*id != old_id && !id.is_empty(), || {
        format!("the new version needs a new id, got {id:?} (old {old_id:?})")
    })?;
    ensure(
        *previous_id == old_id && previous_value == "Acme Corp",
        || {
            format!(
                "previous should be ({old_id:?}, \"Acme Corp\"), got ({previous_id:?}, {previous_value:?})"
            )
        },
    )?;
    let facts = active(&*store, SCOPE).await?;
    let values: Vec<&str> = facts.iter().map(|f| f.value.as_str()).collect();
    ensure(values == ["Globex"], || {
        format!("only the new value may be active, got {values:?}")
    })?;
    let history = call!(
        "fact_history",
        store.fact_history(SCOPE, "work", "employer")
    );
    let summary: Vec<(&str, bool)> = history
        .iter()
        .map(|f| (f.value.as_str(), f.superseded_at.is_some()))
        .collect();
    ensure(summary == [("Acme Corp", true), ("Globex", false)], || {
        format!("history must be oldest first with the old value superseded, got {summary:?}")
    })?;
    let scores = call!(
        "fact_lexical_scores",
        store.fact_lexical_scores(SCOPE, "Acme Corp")
    );
    ensure(scores.get(&old_id).is_none_or(|s| *s <= 0.0), || {
        "a superseded fact received a lexical score".into()
    })?;
    if let Some(hits) = call!(
        "search_facts",
        store.search_facts(SCOPE, "Acme Corp employer", None, 10)
    ) {
        ensure(hits.iter().all(|h| h.fact.id != old_id), || {
            "search_facts returned a superseded fact".into()
        })?;
    }
    Ok(())
}

async fn scope_isolation(store: Arc<dyn MemoryStore>) -> Check {
    let mine = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "pet", "dog_name", "Rex"), None, at(0))
    );
    let theirs = call!(
        "upsert_fact (other scope)",
        store.upsert_fact(
            fact(OTHER_SCOPE, "pet", "dog_name", "Biscuit"),
            None,
            at(10)
        )
    );
    ensure(matches!(theirs, UpsertOutcome::Inserted { .. }), || {
        format!("the same key in another scope must be Inserted, got {theirs:?}")
    })?;
    let facts = active(&*store, SCOPE).await?;
    ensure(
        facts.len() == 1 && facts[0].id == mine.id() && facts[0].scope == SCOPE,
        || format!("active_facts leaked across scopes: {facts:?}"),
    )?;
    let history = call!("fact_history", store.fact_history(SCOPE, "pet", "dog_name"));
    ensure(
        history.iter().all(|f| f.scope == SCOPE) && history.len() == 1,
        || format!("fact_history leaked across scopes: {history:?}"),
    )?;
    let scores = call!(
        "fact_lexical_scores",
        store.fact_lexical_scores(SCOPE, "Biscuit dog")
    );
    ensure(!scores.contains_key(theirs.id()), || {
        "fact_lexical_scores scored another scope's fact".into()
    })?;
    if let Some(hits) = call!(
        "search_facts",
        store.search_facts(SCOPE, "Biscuit dog", None, 10)
    ) {
        ensure(hits.iter().all(|h| h.fact.scope == SCOPE), || {
            "search_facts returned another scope's fact".into()
        })?;
    }
    call!(
        "insert_episode",
        store.insert_episode(
            episode(OTHER_SCOPE, "Planned the dog's vet visit", &[]),
            None,
            at(20)
        )
    );
    let eps = call!("episodes", store.episodes(SCOPE));
    ensure(eps.is_empty(), || "episodes leaked across scopes".into())?;
    let ep_scores = call!(
        "episode_lexical_scores",
        store.episode_lexical_scores(SCOPE, "vet visit")
    );
    ensure(ep_scores.is_empty(), || {
        "episode_lexical_scores scored another scope's episode".into()
    })?;
    let forgot = call!(
        "supersede_fact",
        store.supersede_fact(SCOPE, "pet", "dog_name", at(30))
    );
    ensure(forgot, || {
        "supersede_fact returned false for an active fact".into()
    })?;
    let other = active(&*store, OTHER_SCOPE).await?;
    ensure(other.len() == 1 && other[0].value == "Biscuit", || {
        format!("forgetting in one scope affected another: {other:?}")
    })
}

async fn forget(store: Arc<dyn MemoryStore>) -> Check {
    call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "user", "city", "Springfield"), None, at(0))
    );
    let first = call!(
        "supersede_fact",
        store.supersede_fact(SCOPE, "user", "city", at(60))
    );
    ensure(first, || {
        "supersede_fact returned false for an active fact".into()
    })?;
    ensure(active(&*store, SCOPE).await?.is_empty(), || {
        "a forgotten fact is still active".into()
    })?;
    let history = call!("fact_history", store.fact_history(SCOPE, "user", "city"));
    ensure(
        history.len() == 1 && history[0].superseded_at.is_some(),
        || format!("a forgotten fact must stay in history as superseded: {history:?}"),
    )?;
    let again = call!(
        "supersede_fact (again)",
        store.supersede_fact(SCOPE, "user", "city", at(120))
    );
    ensure(!again, || {
        "supersede_fact returned true with nothing active".into()
    })?;
    let unknown = call!(
        "supersede_fact (unknown key)",
        store.supersede_fact(SCOPE, "user", "never_set", at(120))
    );
    ensure(!unknown, || {
        "supersede_fact returned true for an unknown key".into()
    })
}

async fn episodes(store: Arc<dyn MemoryStore>) -> Check {
    let new = episode(
        SCOPE,
        "Fixed the deployment pipeline for Acme Corp",
        &["deploy", "ci"],
    );
    let stored = call!(
        "insert_episode",
        store.insert_episode(new.clone(), None, at(0))
    );
    ensure(!stored.id.is_empty(), || "episode id is empty".into())?;
    ensure(
        stored.scope == new.scope
            && stored.session_id == new.session_id
            && stored.summary == new.summary
            && stored.topics == new.topics
            && close(stored.importance, new.importance)
            && stored.recall_count == 0,
        || format!("insert_episode did not keep the fields: {stored:?}"),
    )?;
    let other = call!(
        "insert_episode",
        store.insert_episode(
            episode(SCOPE, "Chose a birthday cake for Mia", &[]),
            None,
            at(10)
        )
    );
    let listed = call!("episodes", store.episodes(SCOPE));
    let ids: Vec<&str> = listed.iter().map(|r| r.episode.id.as_str()).collect();
    ensure(
        listed.len() == 2 && ids.contains(&stored.id.as_str()),
        || format!("episodes should list both episodes, got {ids:?}"),
    )?;
    let listed_stored = listed
        .iter()
        .find(|r| r.episode.id == stored.id)
        .map(|r| &r.episode);
    ensure(listed_stored == Some(&stored), || {
        format!("episodes returned {listed_stored:?}, insert_episode returned {stored:?}")
    })?;
    let scores = call!(
        "episode_lexical_scores",
        store.episode_lexical_scores(SCOPE, "deployment pipeline")
    );
    check_scores(&scores, &[stored.id.as_str()], &[other.id.as_str()])
}

fn check_scores(scores: &HashMap<String, f32>, matching: &[&str], not: &[&str]) -> Check {
    for id in matching {
        match scores.get(*id) {
            Some(s) if *s > 0.0 && *s <= 1.0 => {}
            other => return Err(format!("matching row {id} scored {other:?}, want (0, 1]")),
        }
    }
    for id in not {
        if scores.get(*id).is_some_and(|s| *s > 0.0) {
            return Err(format!("non-matching row {id} scored {:?}", scores[*id]));
        }
    }
    if let Some((id, s)) = scores.iter().find(|(_, s)| !(0.0..=1.0).contains(*s)) {
        return Err(format!("score {s} for {id} is outside 0..=1"));
    }
    Ok(())
}

async fn lexical(store: Arc<dyn MemoryStore>) -> Check {
    let fruit = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "food", "favorite_fruit", "mango"), None, at(0))
    );
    let dog = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "pet", "dog_name", "Rex"), None, at(0))
    );
    let scores = call!(
        "fact_lexical_scores",
        store.fact_lexical_scores(SCOPE, "mango")
    );
    check_scores(&scores, &[fruit.id()], &[dog.id()])?;
    let none = call!(
        "fact_lexical_scores",
        store.fact_lexical_scores(SCOPE, "zeppelin")
    );
    ensure(none.values().all(|s| *s <= 0.0), || {
        format!("a query matching nothing produced scores: {none:?}")
    })
}

async fn native_search(store: Arc<dyn MemoryStore>) -> Check<Vec<String>> {
    let mut notes = Vec::new();
    for (i, value) in ["mango smoothie", "mango salad", "mango lassi"]
        .iter()
        .enumerate()
    {
        call!(
            "upsert_fact",
            store.upsert_fact(
                fact(SCOPE, "food", &format!("dish_{i}"), value),
                None,
                at(0)
            )
        );
    }
    call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "food", "dish_0", "papaya"), None, at(10))
    );
    call!(
        "upsert_fact",
        store.upsert_fact(
            fact(OTHER_SCOPE, "food", "dish_9", "mango pie"),
            None,
            at(0)
        )
    );
    match call!("search_facts", store.search_facts(SCOPE, "mango", None, 1)) {
        None => notes.push("search_facts uses the scorer (None)".to_string()),
        Some(hits) => {
            ensure(hits.len() <= 1, || {
                format!("search_facts returned {} hits for limit 1", hits.len())
            })?;
            ensure(
                hits.iter()
                    .all(|h| h.fact.scope == SCOPE && h.fact.is_active()),
                || format!("search_facts returned out-of-scope or superseded facts: {hits:?}"),
            )?;
        }
    }
    for summary in ["Mango tasting", "Mango market trip"] {
        call!(
            "insert_episode",
            store.insert_episode(episode(SCOPE, summary, &[]), None, at(0))
        );
    }
    call!(
        "insert_episode",
        store.insert_episode(episode(OTHER_SCOPE, "Mango farm", &[]), None, at(0))
    );
    match call!(
        "search_episodes",
        store.search_episodes(SCOPE, "mango", None, 1)
    ) {
        None => notes.push("search_episodes uses the scorer (None)".to_string()),
        Some(hits) => {
            ensure(hits.len() <= 1, || {
                format!("search_episodes returned {} hits for limit 1", hits.len())
            })?;
            ensure(hits.iter().all(|h| h.episode.scope == SCOPE), || {
                "search_episodes returned another scope's episode".into()
            })?;
        }
    }
    Ok(notes)
}

async fn fact_recalls(store: Arc<dyn MemoryStore>) -> Check {
    let a = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "user", "city", "Springfield"), None, at(0))
    );
    let b = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "user", "team", "Platform"), None, at(0))
    );
    let ids = vec![a.id().to_string(), "conformance-unknown-id".to_string()];
    call!(
        "record_fact_recalls",
        store.record_fact_recalls(&ids, at(100))
    );
    call!(
        "record_fact_recalls",
        store.record_fact_recalls(&ids[..1], at(200))
    );
    let facts = active(&*store, SCOPE).await?;
    let get = |id: &str| facts.iter().find(|f| f.id == id).cloned();
    let fa = get(a.id()).ok_or("recalled fact disappeared")?;
    ensure(
        fa.recall_count == 2 && fa.last_recalled_at == Some(at(200)),
        || {
            format!(
                "after two recalls expected count 2 at {}, got {} at {:?}",
                at(200),
                fa.recall_count,
                fa.last_recalled_at
            )
        },
    )?;
    let fb = get(b.id()).ok_or("untouched fact disappeared")?;
    ensure(
        fb.recall_count == 0 && fb.last_recalled_at.is_none(),
        || format!("a fact that was not recalled changed: {fb:?}"),
    )
}

async fn episode_recalls(store: Arc<dyn MemoryStore>) -> Check {
    let a = call!(
        "insert_episode",
        store.insert_episode(
            episode(SCOPE, "Reviewed the quarterly plan", &[]),
            None,
            at(0)
        )
    );
    let b = call!(
        "insert_episode",
        store.insert_episode(episode(SCOPE, "Booked flights", &[]), None, at(0))
    );
    let ids = vec![a.id.clone(), "conformance-unknown-id".to_string()];
    call!(
        "record_episode_recalls",
        store.record_episode_recalls(&ids, at(100))
    );
    call!(
        "record_episode_recalls",
        store.record_episode_recalls(&ids[..1], at(200))
    );
    let eps: Vec<Episode> = call!("episodes", store.episodes(SCOPE))
        .into_iter()
        .map(|r| r.episode)
        .collect();
    let get = |id: &str| eps.iter().find(|e| e.id == id).cloned();
    let ea = get(&a.id).ok_or("recalled episode disappeared")?;
    ensure(
        ea.recall_count == 2 && ea.last_recalled_at == Some(at(200)),
        || {
            format!(
                "after two recalls expected count 2 at {}, got {} at {:?}",
                at(200),
                ea.recall_count,
                ea.last_recalled_at
            )
        },
    )?;
    let eb = get(&b.id).ok_or("untouched episode disappeared")?;
    ensure(
        eb.recall_count == 0 && eb.last_recalled_at.is_none(),
        || format!("an episode that was not recalled changed: {eb:?}"),
    )
}

fn tricky_vector() -> Vec<f32> {
    vec![
        0.1,
        -0.0,
        1.0 / 3.0,
        f32::MAX,
        f32::MIN,
        f32::MIN_POSITIVE,
        1.0e-45, // smallest subnormal
        -123.456,
        f32::EPSILON,
        7.0,
    ]
}

fn same_embedding(got: &Option<StoredEmbedding>, want: &Option<StoredEmbedding>) -> Check {
    match (got, want) {
        (None, None) => Ok(()),
        (Some(g), Some(w)) => {
            ensure(g.model == w.model, || {
                format!("model {:?} != {:?}", g.model, w.model)
            })?;
            ensure(g.vector.len() == w.vector.len(), || {
                format!("length {} != {}", g.vector.len(), w.vector.len())
            })?;
            match g.vector.iter().zip(&w.vector).position(|(a, b)| a != b) {
                None => Ok(()),
                Some(i) => Err(format!(
                    "component {i}: got {:e}, stored {:e}",
                    g.vector[i], w.vector[i]
                )),
            }
        }
        _ => Err(format!(
            "embedding presence changed: stored {}, got {}",
            want.is_some(),
            got.is_some()
        )),
    }
}

async fn embeddings(store: Arc<dyn MemoryStore>) -> Check {
    let embedding = Some(StoredEmbedding {
        model: "conformance-embedder-v1".to_string(),
        vector: tricky_vector(),
    });
    let with = call!(
        "upsert_fact",
        store.upsert_fact(
            fact(SCOPE, "user", "city", "Springfield"),
            embedding.clone(),
            at(0)
        )
    );
    let without = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "user", "team", "Platform"), None, at(0))
    );
    let records = call!("active_facts", store.active_facts(SCOPE));
    for (id, want) in [(with.id(), &embedding), (without.id(), &None)] {
        let got = records
            .iter()
            .find(|r| r.fact.id == id)
            .map(|r| r.embedding.clone())
            .ok_or_else(|| format!("fact {id} missing"))?;
        same_embedding(&got, want).map_err(|e| format!("fact embedding: {e}"))?;
    }
    let ep = call!(
        "insert_episode",
        store.insert_episode(
            episode(SCOPE, "Embedded episode", &[]),
            embedding.clone(),
            at(0)
        )
    );
    let eps = call!("episodes", store.episodes(SCOPE));
    let got = eps
        .iter()
        .find(|r| r.episode.id == ep.id)
        .map(|r| r.embedding.clone())
        .ok_or("episode missing")?;
    same_embedding(&got, &embedding).map_err(|e| format!("episode embedding: {e}"))
}

async fn stats(store: Arc<dyn MemoryStore>) -> Check {
    call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "work", "employer", "Acme Corp"), None, at(0))
    );
    call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "work", "employer", "Globex"), None, at(10))
    );
    call!(
        "upsert_fact",
        store.upsert_fact(fact(OTHER_SCOPE, "pet", "dog_name", "Rex"), None, at(0))
    );
    call!(
        "insert_episode",
        store.insert_episode(episode(SCOPE, "Changed jobs", &[]), None, at(20))
    );
    let s = call!("stats", store.stats());
    let got = (s.active_facts, s.superseded_facts, s.episodes, s.scopes);
    ensure(got == (2, 1, 1, 2), || {
        format!(
            "expected (active 2, superseded 1, episodes 1, scopes 2), got (active {}, \
             superseded {}, episodes {}, scopes {})",
            got.0, got.1, got.2, got.3
        )
    })
}

/// Rows the decay checks set up, and a snapshot of their observable state.
struct DecayFixture {
    recalled_old: String,
    recalled_recent: String,
    superseded: String,
    replacement: String,
    episode_never: String,
    episode_old: String,
    episode_recent: String,
}

#[derive(Debug, PartialEq)]
struct Snapshot {
    active: Vec<(String, u32)>,
    history: Vec<String>,
    episodes: Vec<(String, u32)>,
}

fn now() -> DateTime<Utc> {
    t0() + Duration::days(400)
}

async fn decay_setup(store: &dyn MemoryStore) -> Check<DecayFixture> {
    let recalled_old = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "user", "city", "Springfield"), None, t0())
    )
    .id()
    .to_string();
    let ids = [recalled_old.clone()];
    for _ in 0..10 {
        call!("record_fact_recalls", store.record_fact_recalls(&ids, t0()));
    }
    let recalled_recent = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "user", "team", "Platform"), None, t0())
    )
    .id()
    .to_string();
    let ids = [recalled_recent.clone()];
    for _ in 0..5 {
        call!(
            "record_fact_recalls",
            store.record_fact_recalls(&ids, now() - Duration::days(1))
        );
    }
    let superseded = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "work", "employer", "Acme Corp"), None, t0())
    )
    .id()
    .to_string();
    let replacement = call!(
        "upsert_fact",
        store.upsert_fact(fact(SCOPE, "work", "employer", "Globex"), None, t0())
    )
    .id()
    .to_string();
    let episode_never = call!(
        "insert_episode",
        store.insert_episode(episode(SCOPE, "Never recalled", &[]), None, t0())
    )
    .id;
    let episode_old = call!(
        "insert_episode",
        store.insert_episode(episode(SCOPE, "Recalled long ago", &[]), None, t0())
    )
    .id;
    let ids = [episode_old.clone()];
    for _ in 0..3 {
        call!(
            "record_episode_recalls",
            store.record_episode_recalls(&ids, t0())
        );
    }
    let episode_recent = call!(
        "insert_episode",
        store.insert_episode(episode(SCOPE, "Recalled yesterday", &[]), None, t0())
    )
    .id;
    let ids = [episode_recent.clone()];
    call!(
        "record_episode_recalls",
        store.record_episode_recalls(&ids, now() - Duration::days(1))
    );
    Ok(DecayFixture {
        recalled_old,
        recalled_recent,
        superseded,
        replacement,
        episode_never,
        episode_old,
        episode_recent,
    })
}

async fn snapshot(store: &dyn MemoryStore) -> Check<Snapshot> {
    let mut active: Vec<(String, u32)> = call!("active_facts", store.active_facts(SCOPE))
        .into_iter()
        .map(|r| (r.fact.id, r.fact.recall_count))
        .collect();
    active.sort();
    let mut history: Vec<String> = call!(
        "fact_history",
        store.fact_history(SCOPE, "work", "employer")
    )
    .into_iter()
    .map(|f| f.id)
    .collect();
    history.sort();
    let mut episodes: Vec<(String, u32)> = call!("episodes", store.episodes(SCOPE))
        .into_iter()
        .map(|r| (r.episode.id, r.episode.recall_count))
        .collect();
    episodes.sort();
    Ok(Snapshot {
        active,
        history,
        episodes,
    })
}

async fn decay_disabled(store: Arc<dyn MemoryStore>) -> Check {
    decay_setup(&*store).await?;
    let before = snapshot(&*store).await?;
    let disabled = RetentionConfig {
        fact_recall_decay_after: None,
        episode_recall_decay_after: None,
        superseded_retention: None,
        episode_retention: None,
    };
    let report = call!("decay", store.decay(now(), &disabled));
    ensure(report == DecayReport::default(), || {
        format!("disabled decay reported changes: {report:?}")
    })?;
    let after = snapshot(&*store).await?;
    ensure(before == after, || {
        format!("disabled decay changed state: before {before:?}, after {after:?}")
    })
}

/// `Ok(true)` when decay applied the documented steps, `Ok(false)` when it
/// was a complete no-op.
async fn decay_default(store: Arc<dyn MemoryStore>) -> Check<bool> {
    let f = decay_setup(&*store).await?;
    let report = call!("decay", store.decay(now(), &RetentionConfig::default()));
    let s = snapshot(&*store).await?;
    let fact_count = |id: &str| s.active.iter().find(|(i, _)| i == id).map(|(_, c)| *c);
    let episode_count = |id: &str| s.episodes.iter().find(|(i, _)| i == id).map(|(_, c)| *c);
    let mut problems = Vec::new();

    // Always: active and recently recalled rows are untouched.
    if fact_count(&f.recalled_recent) != Some(5) {
        problems.push(format!(
            "recently recalled fact changed: {:?}",
            fact_count(&f.recalled_recent)
        ));
    }
    if fact_count(&f.replacement) != Some(0) {
        problems.push("the active replacement fact was deleted or changed".to_string());
    }
    if episode_count(&f.episode_recent) != Some(1) {
        problems.push(format!(
            "recently recalled episode changed: {:?}",
            episode_count(&f.episode_recent)
        ));
    }

    // Superseded retention: untouched, or exactly the old version deleted.
    let old_present = s.history.contains(&f.superseded);
    match (report.superseded_deleted, old_present) {
        (0, true) | (1, false) => {}
        (n, present) => problems.push(format!(
            "superseded retention: reported {n} deleted but the 400-day-old superseded \
             version is {}",
            if present { "still present" } else { "gone" }
        )),
    }
    // Episode retention: untouched, or exactly the never-recalled one gone.
    let never_present = episode_count(&f.episode_never).is_some();
    match (report.episodes_deleted, never_present) {
        (0, true) | (1, false) => {}
        (n, present) => problems.push(format!(
            "episode retention: reported {n} deleted but the never-recalled old episode is {}",
            if present { "still present" } else { "gone" }
        )),
    }
    if episode_count(&f.episode_old).is_none() {
        problems.push("a recalled episode was deleted by retention".to_string());
    }
    // Fact recall decay: 10 -> 9 (a tenth, at least one), or untouched.
    match (report.facts_decayed, fact_count(&f.recalled_old)) {
        (0, Some(10)) | (1, Some(9)) => {}
        (n, count) => problems.push(format!(
            "fact recall decay: reported {n} decayed, stale fact count is {count:?} \
             (expected 10 untouched or 9 decayed)"
        )),
    }
    // Episode recall decay: 3 -> 2, or untouched.
    match (report.episodes_decayed, episode_count(&f.episode_old)) {
        (0, Some(3)) | (1, Some(2)) => {}
        (n, count) => problems.push(format!(
            "episode recall decay: reported {n} decayed, stale episode count is {count:?} \
             (expected 3 untouched or 2 decayed)"
        )),
    }
    if problems.is_empty() {
        Ok(report != DecayReport::default())
    } else {
        Err(problems.join("; "))
    }
}

/// `Ok(true)` when the store keeps access levels, `Ok(false)` for the trait
/// default.
async fn access_levels(store: Arc<dyn MemoryStore>) -> Check<bool> {
    let shared = FactAccess::channel("conformance-channel");
    let first = call!(
        "upsert_fact_with_access",
        store.upsert_fact_with_access(
            fact(SCOPE, "work", "employer", "Acme Corp"),
            None,
            &FactAccess::global(),
            at(0)
        )
    );
    let id = first.id().to_string();
    let ids = [id.clone()];
    let access = call!("fact_access", store.fact_access(SCOPE, &ids));
    if access.is_empty() {
        return Ok(false);
    }
    ensure(access.get(&id) == Some(&FactAccess::global()), || {
        format!("expected global access for {id}, got {access:?}")
    })?;
    let other = call!("fact_access", store.fact_access(OTHER_SCOPE, &ids));
    ensure(other.is_empty(), || {
        format!("fact_access answered for another scope: {other:?}")
    })?;

    call!(
        "upsert_fact_with_access (restated, narrower)",
        store.upsert_fact_with_access(
            fact(SCOPE, "work", "employer", "Acme Corp"),
            None,
            &shared,
            at(10)
        )
    );
    call!(
        "upsert_fact_with_access (restated, wider)",
        store.upsert_fact_with_access(
            fact(SCOPE, "work", "employer", "Acme Corp"),
            None,
            &FactAccess::global(),
            at(20)
        )
    );
    let access = call!("fact_access", store.fact_access(SCOPE, &ids));
    ensure(access.get(&id) == Some(&shared), || {
        format!("restating must keep the narrowest access, got {access:?}")
    })?;

    let next = call!(
        "upsert_fact_with_access (new value)",
        store.upsert_fact_with_access(
            fact(SCOPE, "work", "employer", "Globex"),
            None,
            &FactAccess::global(),
            at(30)
        )
    );
    let ids = [next.id().to_string()];
    let access = call!("fact_access", store.fact_access(SCOPE, &ids));
    ensure(access.get(next.id()) == Some(&FactAccess::global()), || {
        format!("a new version takes the access it was written with, got {access:?}")
    })?;

    let stored = call!(
        "insert_episode_with_access",
        store.insert_episode_with_access(
            episode(SCOPE, "Reviewed the roadmap", &[]),
            None,
            &shared,
            at(40)
        )
    );
    let ids = [stored.id.clone()];
    let access = call!("episode_access", store.episode_access(SCOPE, &ids));
    ensure(access.get(&stored.id) == Some(&shared), || {
        format!("expected the channel access back for the episode, got {access:?}")
    })?;
    let other = call!("episode_access", store.episode_access(OTHER_SCOPE, &ids));
    ensure(other.is_empty(), || {
        format!("episode_access answered for another scope: {other:?}")
    })?;
    Ok(true)
}

/// `Ok(true)` when the store relates facts, `Ok(false)` for the trait default.
async fn graph_scores(store: Arc<dyn MemoryStore>) -> Check<bool> {
    let seed = call!(
        "upsert_fact",
        store.upsert_fact(
            fact(SCOPE, "person", "sister_name", "Alice Rivera"),
            None,
            at(0)
        )
    );
    let related = call!(
        "upsert_fact",
        store.upsert_fact(
            fact(SCOPE, "person", "alice_rivera_employer", "Acme Corp"),
            None,
            at(0)
        )
    );
    let replaced = call!(
        "upsert_fact",
        store.upsert_fact(
            fact(SCOPE, "person", "alice_rivera_city", "Lisbon"),
            None,
            at(0)
        )
    );
    call!(
        "upsert_fact",
        store.upsert_fact(
            fact(SCOPE, "person", "alice_rivera_city", "Porto"),
            None,
            at(10)
        )
    );
    let theirs = call!(
        "upsert_fact (other scope)",
        store.upsert_fact(
            fact(OTHER_SCOPE, "person", "alice_rivera_pet", "Biscuit"),
            None,
            at(0)
        )
    );
    let seeds = [seed.id().to_string()];
    let scores = call!("fact_graph_scores", store.fact_graph_scores(SCOPE, &seeds));
    if scores.is_empty() {
        return Ok(false);
    }
    ensure(scores.values().all(|s| *s > 0.0 && *s <= 1.0), || {
        format!("graph scores must be in (0, 1]: {scores:?}")
    })?;
    ensure(scores.contains_key(related.id()), || {
        format!("a fact naming the seed's subject got no score: {scores:?}")
    })?;
    ensure(
        !scores.contains_key(replaced.id()) && !scores.contains_key(theirs.id()),
        || format!("superseded or other-scope facts got a score: {scores:?}"),
    )?;
    let unknown = ["conformance-unknown-id".to_string()];
    let none = call!(
        "fact_graph_scores",
        store.fact_graph_scores(SCOPE, &unknown)
    );
    ensure(none.is_empty(), || {
        format!("an unknown seed produced scores: {none:?}")
    })?;
    Ok(true)
}

fn vector_from(model: &str) -> StoredEmbedding {
    StoredEmbedding {
        model: model.to_string(),
        vector: vec![0.25, 0.5],
    }
}

/// `Ok(true)` when the store refreshes vectors, `Ok(false)` for the default.
async fn embedding_refresh(store: Arc<dyn MemoryStore>) -> Check<bool> {
    const CURRENT: &str = "conformance-embedder-v2";
    let upsert = |key: &'static str, embedding: Option<StoredEmbedding>| {
        let store = store.clone();
        async move {
            store
                .upsert_fact(fact(SCOPE, "note", key, key), embedding, at(0))
                .await
        }
    };
    let old = call!("upsert_fact", upsert("old", Some(vector_from("v1"))));
    let none = call!("upsert_fact", upsert("none", None));
    call!("upsert_fact", upsert("fresh", Some(vector_from(CURRENT))));
    let replaced = call!("upsert_fact", upsert("gone", Some(vector_from("v1"))));
    call!(
        "supersede_fact",
        store.supersede_fact(SCOPE, "note", "gone", at(1))
    );
    let page = call!(
        "stale_embeddings",
        store.stale_embeddings(CURRENT, RecordKind::Fact, None, 10)
    );
    if page.is_empty() {
        return Ok(false);
    }
    let mut want = vec![old.id().to_string(), none.id().to_string()];
    want.sort();
    let got: Vec<String> = page.iter().map(|s| s.id.clone()).collect();
    ensure(got == want, || {
        format!("expected the stale active facts {want:?} in id order, got {got:?}")
    })?;
    ensure(!got.contains(&replaced.id().to_string()), || {
        "a superseded fact was listed as stale".into()
    })?;
    let first = call!(
        "stale_embeddings",
        store.stale_embeddings(CURRENT, RecordKind::Fact, None, 1)
    );
    ensure(first.len() == 1 && first[0].id == want[0], || {
        format!("limit 1 should return only {:?}, got {first:?}", want[0])
    })?;
    let rest = call!(
        "stale_embeddings",
        store.stale_embeddings(CURRENT, RecordKind::Fact, Some(&want[0]), 10)
    );
    ensure(rest.len() == 1 && rest[0].id == want[1], || {
        format!("after_id should skip to {:?}, got {rest:?}", want[1])
    })?;

    let target = page
        .iter()
        .find(|s| s.id == old.id())
        .ok_or("the old-model fact was not listed")?;
    ensure(
        target.model.as_deref() == Some("v1") && target.text.contains("old"),
        || format!("the stale row should carry its model and text: {target:?}"),
    )?;
    let replaced_now = call!(
        "replace_embedding",
        store.replace_embedding(target, vector_from(CURRENT))
    );
    ensure(replaced_now, || {
        "replace_embedding returned false for a stale record".into()
    })?;
    let again = call!(
        "replace_embedding (model has changed)",
        store.replace_embedding(target, vector_from("conformance-other"))
    );
    ensure(!again, || {
        "replace_embedding overwrote a vector that changed since it was read".into()
    })?;
    let records = call!("active_facts", store.active_facts(SCOPE));
    let record = records
        .iter()
        .find(|r| r.fact.id == old.id())
        .ok_or("the refreshed fact disappeared")?;
    same_embedding(&record.embedding, &Some(vector_from(CURRENT)))?;
    ensure(record.fact.updated_at == at(0), || {
        format!(
            "replace_embedding moved updated_at to {}",
            record.fact.updated_at
        )
    })?;

    let stored = call!(
        "insert_episode",
        store.insert_episode(
            episode(SCOPE, "Old episode", &["topic"]),
            Some(vector_from("v1")),
            at(0)
        )
    );
    let episodes = call!(
        "stale_embeddings (episodes)",
        store.stale_embeddings(CURRENT, RecordKind::Episode, None, 10)
    );
    ensure(
        episodes.len() == 1
            && episodes[0].id == stored.id
            && episodes[0].kind == RecordKind::Episode,
        || format!("expected the one stale episode, got {episodes:?}"),
    )?;
    Ok(true)
}