mindfork 0.10.1

A terminal AI chat written in Rust: local models via llama.cpp or OpenAI, Anthropic, Gemini and Grok in the cloud, with persistent memory, notes, RAG and tools.
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
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
//! Note tests. See mod.rs.

use super::super::testkit::ctx_with_storage;
use super::*;
use crate::shared::api::EmbedRole;
use uuid::Uuid;

/// The reference locale (ru) — direct overview calls in tests pin the ru bundle.
fn ru() -> &'static crate::shared::i18n::Locale {
    crate::shared::i18n::locale(crate::shared::i18n::Lang::Ru)
}

/// Is there no Cyrillic in the string (a proxy for "translated to en").
fn no_cyrillic(s: &str) -> bool {
    !s.chars()
        .any(|c| ('а'..='я').contains(&c) || ('А'..='Я').contains(&c))
}

#[test]
fn note_tool_descriptions_are_localized() {
    // Every note tool returns DIFFERENT text on ru/en (catches a forgotten
    // `_loc`), and the en description has no Cyrillic. §3.5 docs/history/i18n.md.
    use crate::shared::i18n::{Lang, locale};
    let (ru, en) = (locale(Lang::Ru), locale(Lang::En));
    let pairs: Vec<(String, String)> = vec![
        (NoteSave.description(ru), NoteSave.description(en)),
        (NoteRecall.description(ru), NoteRecall.description(en)),
        (NoteRevise.description(ru), NoteRevise.description(en)),
        (NoteSupersede.description(ru), NoteSupersede.description(en)),
        (NoteMerge.description(ru), NoteMerge.description(en)),
        (NoteLink.description(ru), NoteLink.description(en)),
        (NoteNeighbors.description(ru), NoteNeighbors.description(en)),
        (
            ConsolidateNotes.description(ru),
            ConsolidateNotes.description(en),
        ),
        (
            NoteCiteSource.description(ru),
            NoteCiteSource.description(en),
        ),
    ];
    for (r, e) in pairs {
        assert_ne!(r, e, "description not localized (loc forgotten?): {r}");
        assert!(no_cyrillic(&e), "Cyrillic in en description: {e}");
    }
}

#[tokio::test]
async fn note_recall_result_localized_for_all_langs() {
    // The empty result and the "found" header render in every built-in language.
    use crate::shared::i18n::{Lang, locale};
    for &lang in Lang::ALL {
        let profile = Uuid::new_v4();
        let (_d, _s, mut ctx) = ctx_with_storage(profile);
        ctx.loc = locale(lang);
        let empty = NoteRecall
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert_eq!(
            empty.result,
            locale(lang).t("tool.note_recall.result.empty")
        );
        NoteSave
            .invoke(&ctx, serde_json::json!({"content": "hello world"}))
            .await
            .unwrap();
        let out = NoteRecall
            .invoke(&ctx, serde_json::json!({}))
            .await
            .unwrap();
        assert!(out.result.contains("hello world"), "{lang:?}");
        assert!(
            out.result
                .contains(&locale(lang).tf("tool.note_recall.result.header", &[("n", "1")])),
            "{lang:?}: {}",
            out.result
        );
    }
}

#[tokio::test]
async fn save_then_recall_isolated_by_profile() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);

    NoteSave
        .invoke(
            &ctx,
            serde_json::json!({"content": "любит чай", "tags": ["pref"]}),
        )
        .await
        .unwrap();
    // The note was actually recorded under this profile.
    assert_eq!(
        storage
            .db()
            .note_list(profile, None, &[], None)
            .unwrap()
            .len(),
        1
    );

    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({"query": "чай"}))
        .await
        .unwrap();
    assert!(out.result.contains("любит чай"));

    // A different profile doesn't see the note.
    let (_d2, _s2, other) = ctx_with_storage(Uuid::new_v4());
    // A different profile — different storage: checking isolation at the filter level
    let empty = NoteRecall
        .invoke(&other, serde_json::json!({}))
        .await
        .unwrap();
    assert!(empty.result.contains("не найдены"));
}

#[tokio::test]
async fn recall_by_tag() {
    let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "a", "tags": ["x"]}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "b", "tags": ["y"]}))
        .await
        .unwrap();
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({"tags": ["x"]}))
        .await
        .unwrap();
    assert!(out.result.contains("a"));
    assert!(!out.result.contains("- b"));
}

#[tokio::test]
async fn save_rejects_empty_content() {
    let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
    assert!(
        NoteSave
            .invoke(&ctx, serde_json::json!({"content": "  "}))
            .await
            .is_err()
    );
}

#[tokio::test]
async fn recall_excludes_self_notes() {
    // Tier 1 "narrative as notes": self-notes (@self) don't surface in user-facing
    // note_recall — neither the substring path nor the semantic one.
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "любит чай"}))
        .await
        .unwrap();
    storage
        .db()
        .note_insert(&Note::new(
            profile,
            "сам люблю чай",
            vec![SELF_NOTE_TAG.to_string()],
        ))
        .unwrap();

    // The substring path (no query).
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({}))
        .await
        .unwrap();
    assert!(out.result.contains("любит чай"));
    assert!(!out.result.contains("сам люблю чай"));

    // The semantic path (there's a query + an embedder).
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({"query": "чай"}))
        .await
        .unwrap();
    assert!(out.result.contains("любит чай"));
    assert!(!out.result.contains("сам люблю чай"));
}

#[tokio::test]
async fn recall_includes_self_notes_when_enabled() {
    // Tier 3, Path 2: with recall_includes_self, self-notes (@self) DO enter
    // general note_recall marked [about self] (both branches: substring and
    // semantic); the internal @self tag is hidden from the output.
    let profile = Uuid::new_v4();
    let (_d, storage, mut ctx) = ctx_with_storage(profile);
    ctx.recall_includes_self = true;
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "любит чай"}))
        .await
        .unwrap();
    storage
        .db()
        .note_insert(&Note::new(
            profile,
            "сам люблю чай",
            vec![SELF_NOTE_TAG.to_string()],
        ))
        .unwrap();

    // The substring path (no query).
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({}))
        .await
        .unwrap();
    assert!(out.result.contains("любит чай"));
    assert!(out.result.contains("[о себе] сам люблю чай"));
    // The internal @self tag is hidden from the tag display (the marker replaces it).
    assert!(!out.result.contains("@self"));

    // The semantic path (query + embedder).
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({"query": "чай"}))
        .await
        .unwrap();
    assert!(out.result.contains("[о себе] сам люблю чай"));
}

#[tokio::test]
async fn recall_shows_note_ids() {
    // Tier 3: note_recall prints note ids — otherwise the model can't reference
    // them in note_link/note_revise (including cross-organ).
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "любит чай"}))
        .await
        .unwrap();
    let id = storage.db().note_list(profile, None, &[], None).unwrap()[0].id;
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({}))
        .await
        .unwrap();
    assert!(out.result.contains(&format!("(id={id})")));
}

#[tokio::test]
async fn note_cite_source_links_and_recall_shows_it() {
    // Tier 3, Path 3: a note cites a RAG source; note_recall shows the "Source
    // citations" block.
    use crate::entities::rag::RagDocument;
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    storage
        .db()
        .rag_insert(&RagDocument::new(
            profile,
            "spec.md",
            "текст про X",
            vec![1.0, 0.0],
        ))
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "вывод про X"}))
        .await
        .unwrap();
    let id = storage.db().note_list(profile, None, &[], None).unwrap()[0].id;

    // An unknown source — a clear refusal, nothing created.
    let out = NoteCiteSource
        .invoke(
            &ctx,
            serde_json::json!({"note_id": id.to_string(), "source": "нет.md"}),
        )
        .await
        .unwrap();
    assert!(out.result.contains("не найден в базе знаний"));

    // An existing source — the link is created; a repeat — it already existed.
    let out = NoteCiteSource
        .invoke(
            &ctx,
            serde_json::json!({"note_id": id.to_string(), "source": "spec.md"}),
        )
        .await
        .unwrap();
    assert!(out.result.contains("Связь с источником создана"));
    let out = NoteCiteSource
        .invoke(
            &ctx,
            serde_json::json!({"note_id": id.to_string(), "source": "spec.md"}),
        )
        .await
        .unwrap();
    assert!(out.result.contains("уже существовала"));

    // note_recall shows the source citation.
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({}))
        .await
        .unwrap();
    assert!(out.result.contains("Ссылки на источники"));
    assert!(out.result.contains("spec.md"));
}

#[tokio::test]
async fn recall_surfaces_cross_organ_self_neighbor_marked() {
    // Tier 3 (cross-organ links): a user note EXPLICITLY linked to an "about
    // self" observation shows it in the "Related notes" block marked [about
    // self] — but regular search still doesn't pull in self-notes.
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(
            &ctx,
            serde_json::json!({"content": "пользователь любит краткость"}),
        )
        .await
        .unwrap();
    storage
        .db()
        .note_insert(&Note::new(
            profile,
            "я склонен к многословию",
            vec![SELF_NOTE_TAG.to_string()],
        ))
        .unwrap();
    let user_id = id_by_content(&storage, profile, "пользователь любит краткость");
    let self_id = id_by_content(&storage, profile, "я склонен к многословию");
    storage
        .db()
        .note_link_insert(profile, user_id, self_id, "contradicts")
        .unwrap();

    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({"query": "краткость"}))
        .await
        .unwrap();
    // The primary output — only the user note (self is hidden from search).
    assert!(out.result.contains("пользователь любит краткость"));
    // But the linked observation surfaces in the related-links block marked [about self].
    assert!(out.result.contains("Связанные заметки"));
    assert!(out.result.contains("[о себе]"));
    assert!(out.result.contains("я склонен к многословию"));
}

#[tokio::test]
async fn self_related_block_surfaces_cross_organ_user_note_marked() {
    // Tier 3: reading the "self-model" shows a user note that neighbors an
    // observation, marked [note] (a self↔user edge).
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    storage
        .db()
        .note_insert(&Note::new(
            profile,
            "я склонен к многословию",
            vec![SELF_NOTE_TAG.to_string()],
        ))
        .unwrap();
    NoteSave
        .invoke(
            &ctx,
            serde_json::json!({"content": "пользователь любит краткость"}),
        )
        .await
        .unwrap();
    let self_id = id_by_content(&storage, profile, "я склонен к многословию");
    let user_id = id_by_content(&storage, profile, "пользователь любит краткость");
    storage
        .db()
        .note_link_insert(profile, self_id, user_id, "contradicts")
        .unwrap();

    let block = self_related_block(&ctx, &[self_id]).expect("expected a links block");
    assert!(block.contains("[заметка]"));
    assert!(block.contains("пользователь любит краткость"));
    assert!(block.contains("contradicts"));
}

#[tokio::test]
async fn save_gate_excludes_self_notes() {
    // The note_save gate doesn't show semantically close self-notes — a regular
    // save shouldn't run into "self-model" observations.
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    storage
        .db()
        .note_insert(&Note::new(
            profile,
            "aaaa bbbb",
            vec![SELF_NOTE_TAG.to_string()],
        ))
        .unwrap();
    let out = NoteSave
        .invoke(&ctx, serde_json::json!({"content": "aaab"}))
        .await
        .unwrap();
    // The only close note is self → the "Similar notes" block doesn't appear.
    assert!(!out.result.contains("Похожие заметки"));
    assert!(!out.result.contains("aaaa bbbb"));
}

#[tokio::test]
async fn consolidation_overview_excludes_self_notes() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "обычная одна"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "обычная два"}))
        .await
        .unwrap();
    storage
        .db()
        .note_insert(&Note::new(
            profile,
            "наблюдение о себе",
            vec![SELF_NOTE_TAG.to_string()],
        ))
        .unwrap();

    let overview = build_consolidation_overview(&storage, profile, ru());
    // The self-note isn't in the active count and isn't in the overview's lists.
    assert!(overview.contains("Активных заметок: 2"));
    assert!(!overview.contains("наблюдение о себе"));
}

#[tokio::test]
async fn self_consolidation_overview_covers_self_only() {
    // Tier 3: the self-consolidation overview over observations (@self) — similar
    // pairs, contradicts, no links; user notes are excluded; None when < 2.
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    assert!(build_self_consolidation_overview(&storage, profile, ru()).is_none());
    create_note(&ctx, "aaaa bbbb".into(), vec![SELF_NOTE_TAG.to_string()])
        .await
        .unwrap();
    // 1 observation → still None.
    assert!(build_self_consolidation_overview(&storage, profile, ru()).is_none());
    create_note(&ctx, "aaab".into(), vec![SELF_NOTE_TAG.to_string()])
        .await
        .unwrap();
    create_note(&ctx, "wwww".into(), vec![SELF_NOTE_TAG.to_string()])
        .await
        .unwrap();
    // A user note shouldn't make it into the observation overview.
    NoteSave
        .invoke(
            &ctx,
            serde_json::json!({"content": "aaaa пользовательская"}),
        )
        .await
        .unwrap();

    let ov = build_self_consolidation_overview(&storage, profile, ru()).unwrap();
    assert!(ov.contains("Обзор наблюдений"));
    assert!(ov.contains("Наблюдений: 3")); // @self only
    assert!(!ov.contains("пользовательская"));
    // A similar pair among observations (aaaa bbbb ↔ aaab, cosine ≈ 0.89 ≥ 0.85).
    assert!(ov.contains("aaaa bbbb"));
    assert!(ov.contains("aaab"));

    // A contradicts link among observations — the overview shows it.
    let selves = storage
        .db()
        .note_list(profile, None, &[SELF_NOTE_TAG.to_string()], None)
        .unwrap();
    storage
        .db()
        .note_link_insert(profile, selves[0].id, selves[1].id, "contradicts")
        .unwrap();
    let ov = build_self_consolidation_overview(&storage, profile, ru()).unwrap();
    assert!(ov.contains("Связи contradicts среди наблюдений: 1"));
}

// ---------- the gates are positions in a scale, not absolute cosines ----------

/// Records `multilingual-e5-large-instruct`'s measured means (research §8.2) —
/// the model whose usable range is 2.6× narrower than bge-m3's, which is why the
/// raw constants would fire on unrelated pairs there.
fn calibrate_e5(storage: &crate::shared::storage::Storage) {
    storage
        .db()
        .set_embed_calibration(&crate::shared::embed_calibration::Calibration {
            unrelated: 0.7897,
            paraphrase: 0.9456,
        })
        .unwrap();
}

/// Two note pairs in orthogonal 2-d subspaces, so every cross pair is 0: one at
/// cosine 0.90 (above the raw 0.85 gate, below its 0.958 e5 mapping) and one at
/// 0.99 (above both). Hand-written vectors rather than embedded text — the point
/// is where the similarities land, not what the mock embedder happens to produce.
fn insert_two_pairs(storage: &crate::shared::storage::Storage, profile: Uuid, tags: Vec<String>) {
    let vectors = [
        vec![1.0, 0.0, 0.0, 0.0],
        vec![0.9, 0.435_889_9, 0.0, 0.0], // cos = 0.90 with the previous
        vec![0.0, 0.0, 1.0, 0.0],
        vec![0.0, 0.0, 0.99, 0.141_067_3], // cos = 0.99 with the previous
    ];
    for (i, v) in vectors.iter().enumerate() {
        let n = Note::new(profile, format!("note {i}"), tags.clone());
        storage.db().note_insert(&n).unwrap();
        storage.db().note_vector_upsert(n.id, profile, v).unwrap();
    }
}

#[test]
fn consolidation_overview_uses_the_calibrated_threshold() {
    let profile = Uuid::new_v4();
    let (_d, storage, _ctx) = ctx_with_storage(profile);
    insert_two_pairs(&storage, profile, vec![]);

    // Uncalibrated — the identity scale, i.e. exactly today's behaviour: both
    // pairs are duplicates and the model is told the constant as written.
    let ov = build_consolidation_overview(&storage, profile, ru());
    assert!(ov.contains("близость ≥ 0.85): 2"), "{ov}");
    assert!(ov.contains("- 0.99 (id="), "{ov}");
    assert!(ov.contains("- 0.90 (id="), "{ov}");

    // On a model whose range is 2.6× narrower, 0.85 means ~0.958 — so the 0.90
    // pair is no longer a duplicate, while the 0.99 one still is.
    calibrate_e5(&storage);
    let ov = build_consolidation_overview(&storage, profile, ru());
    assert!(ov.contains("- 0.99 (id="), "{ov}");
    assert!(!ov.contains("- 0.90 (id="), "{ov}");
    // ...and the number the model is shown is the one that actually selected the
    // pairs — telling it "≥ 0.85" next to a list built at 0.958 would be a lie.
    assert!(ov.contains("близость ≥ 0.96): 1"), "{ov}");
}

#[test]
fn self_consolidation_overview_uses_the_calibrated_threshold() {
    // The same gate over @self observations — its own call site, its own display.
    let profile = Uuid::new_v4();
    let (_d, storage, _ctx) = ctx_with_storage(profile);
    insert_two_pairs(&storage, profile, vec![SELF_NOTE_TAG.to_string()]);

    let ov = build_self_consolidation_overview(&storage, profile, ru()).unwrap();
    assert!(ov.contains("близость ≥ 0.85): 2"), "{ov}");
    assert!(ov.contains("- 0.90 (id="), "{ov}");

    calibrate_e5(&storage);
    let ov = build_self_consolidation_overview(&storage, profile, ru()).unwrap();
    assert!(ov.contains("близость ≥ 0.96): 1"), "{ov}");
    assert!(ov.contains("- 0.99 (id="), "{ov}");
    assert!(!ov.contains("- 0.90 (id="), "{ov}");
}

/// A unit vector at exactly `cos` from `v`: an arbitrary direction made
/// orthogonal to `v` (Gram-Schmidt), then `cos·v̂ + sin·û`. Lets a test place a
/// stored vector at a chosen similarity to an *embedded* text, instead of hoping
/// the mock embedder happens to land there.
fn at_cosine(v: &[f32], cos: f32) -> Vec<f32> {
    let norm = |x: &[f32]| x.iter().map(|a| a * a).sum::<f32>().sqrt();
    let vn: Vec<f32> = v.iter().map(|a| a / norm(v)).collect();
    // The coordinate where `v` is smallest is never parallel to it (a unit vector
    // has some coordinate ≤ 1/√dim), so the projection below cannot degenerate.
    let k = (0..vn.len())
        .min_by(|&a, &b| vn[a].abs().total_cmp(&vn[b].abs()))
        .expect("a non-empty vector");
    let mut e = vec![0.0_f32; vn.len()];
    e[k] = 1.0;
    let dot: f32 = e.iter().zip(&vn).map(|(a, b)| a * b).sum();
    let mut u: Vec<f32> = e.iter().zip(&vn).map(|(a, b)| a - dot * b).collect();
    let un = norm(&u);
    for x in &mut u {
        *x /= un;
    }
    let sin = (1.0 - cos * cos).sqrt();
    vn.iter().zip(&u).map(|(a, b)| cos * a + sin * b).collect()
}

#[tokio::test]
async fn summary_obs_overlap_uses_the_calibrated_threshold() {
    use crate::entities::self_model::SelfModel;
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);

    let paragraph = "a".repeat(50);
    let mut model = SelfModel::new(profile);
    model.summary = paragraph.clone();
    storage.db().self_model_upsert(&model).unwrap();
    let pv = ctx
        .embedder
        .embed(vec![paragraph], EmbedRole::Passage)
        .await
        .unwrap()
        .into_iter()
        .next()
        .unwrap();

    // An observation at cosine 0.80: above the raw 0.62 gate, below its 0.869 e5
    // mapping.
    let obs = Note::new(profile, "MARKER", vec![SELF_NOTE_TAG.to_string()]);
    storage.db().note_insert(&obs).unwrap();
    let set = |cos: f32| {
        storage
            .db()
            .note_vector_upsert(obs.id, profile, &at_cosine(&pv, cos))
            .unwrap()
    };
    set(0.80);

    let overlaps = || summary_observation_overlaps(&storage, ctx.embedder.as_ref(), profile, ru());
    // Uncalibrated: matched, as today.
    assert!(overlaps().await.is_some());

    // Calibrated to the narrower model: the same pair no longer counts...
    calibrate_e5(&storage);
    assert!(overlaps().await.is_none());
    // ...but one above the mapped threshold still does — the gate moved, it did
    // not switch off.
    set(0.95);
    let out = overlaps().await.expect("0.95 clears the mapped threshold");
    assert!(out.contains("MARKER"), "{out}");
}

#[tokio::test]
async fn summary_obs_overlap_surfaces_match_not_unrelated() {
    // A2: a self-description (summary) paragraph matching an observation is
    // surfaced as a pair; an unrelated paragraph/observation isn't. Observation
    // vectors are set manually, so the test is robust to the threshold (a match =
    // cosine 1.0, non-matches = 0.0).
    use crate::entities::self_model::SelfModel;
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);

    // Summary paragraphs (by blank line): P_match (all "a") + unrelated P_unrel
    // (all "b"); both ≥ 40 characters. MockEmbedder — a bag of characters, so
    // their embeddings are orthogonal.
    let p_match = "a".repeat(50);
    let p_unrel = "b".repeat(50);
    let mut model = SelfModel::new(profile);
    model.summary = format!("{p_match}\n\n{p_unrel}");
    storage.db().self_model_upsert(&model).unwrap();

    // The observation matching P_match: its vector = P_match's embedding (cosine = 1.0).
    let match_vec = ctx
        .embedder
        .embed(vec![p_match.clone()], EmbedRole::Passage)
        .await
        .unwrap()
        .into_iter()
        .next()
        .unwrap();
    let obs_match = Note::new(profile, "MARKER_MATCH", vec![SELF_NOTE_TAG.to_string()]);
    storage.db().note_insert(&obs_match).unwrap();
    storage
        .db()
        .note_vector_upsert(obs_match.id, profile, &match_vec)
        .unwrap();

    // An unrelated observation: a vector on an unused dimension (cosine = 0 with both).
    let mut other_vec = vec![0.0_f32; match_vec.len()];
    other_vec[5] = 1.0;
    let obs_other = Note::new(profile, "MARKER_OTHER", vec![SELF_NOTE_TAG.to_string()]);
    storage.db().note_insert(&obs_other).unwrap();
    storage
        .db()
        .note_vector_upsert(obs_other.id, profile, &other_vec)
        .unwrap();

    let out = summary_observation_overlaps(&storage, ctx.embedder.as_ref(), profile, ru())
        .await
        .expect("expected a summary↔observation match section");
    assert!(out.contains("совпадающие с наблюдениями")); // the section header
    assert!(out.contains("MARKER_MATCH"));
    assert!(out.contains(&obs_match.id.to_string())); // the observation's full id
    // The unrelated observation and unrelated paragraph don't surface.
    assert!(!out.contains("MARKER_OTHER"));
    assert!(!out.contains(&"b".repeat(10)));
    // Exactly one pair (one "\n- …" item line under the header).
    assert_eq!(out.matches("\n- ").count(), 1);
}

#[tokio::test]
async fn summary_obs_overlap_soft_degrades() {
    // A2, graceful degradation: no observations / an empty summary / an embedder
    // with a mismatched vector count → no section (None), no panic.
    use crate::entities::self_model::SelfModel;
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);

    // There's a summary, but no observations → None.
    let mut model = SelfModel::new(profile);
    model.summary = "a".repeat(50);
    storage.db().self_model_upsert(&model).unwrap();
    assert!(
        summary_observation_overlaps(&storage, ctx.embedder.as_ref(), profile, ru())
            .await
            .is_none()
    );

    // Add an observation with a vector — now there's something to compare.
    let match_vec = ctx
        .embedder
        .embed(vec!["a".repeat(50)], EmbedRole::Passage)
        .await
        .unwrap()
        .into_iter()
        .next()
        .unwrap();
    let obs = Note::new(profile, "obs", vec![SELF_NOTE_TAG.to_string()]);
    storage.db().note_insert(&obs).unwrap();
    storage
        .db()
        .note_vector_upsert(obs.id, profile, &match_vec)
        .unwrap();

    // The embedder returns the wrong number of vectors → None (a mismatch).
    struct BadCountEmbedder;
    #[async_trait::async_trait]
    impl crate::shared::api::Embedder for BadCountEmbedder {
        async fn embed(
            &self,
            _texts: Vec<String>,
            _role: EmbedRole,
        ) -> anyhow::Result<Vec<Vec<f32>>> {
            Ok(Vec::new()) // 0 vectors for any input — a mismatch
        }
    }
    assert!(
        summary_observation_overlaps(&storage, &BadCountEmbedder, profile, ru())
            .await
            .is_none()
    );

    // An empty summary → None (even with observations present and a working embedder).
    let mut empty = SelfModel::new(profile);
    empty.summary = String::new();
    storage.db().self_model_upsert(&empty).unwrap();
    assert!(
        summary_observation_overlaps(&storage, ctx.embedder.as_ref(), profile, ru())
            .await
            .is_none()
    );
}

#[test]
fn migrate_self_narrative_moves_and_is_idempotent() {
    use crate::entities::self_model::{NarrativeSegment, SelfModel};
    use chrono::{Duration, Utc};
    let profile = Uuid::new_v4();
    let (_d, storage, _ctx) = ctx_with_storage(profile);
    // An "old" model with a narrative blob (as before Tier 1).
    let mut m = SelfModel::new(profile);
    let old = Utc::now() - Duration::days(3);
    m.narrative = vec![
        NarrativeSegment {
            id: Uuid::new_v4(),
            text: "старое наблюдение".into(),
            created_at: old,
        },
        NarrativeSegment {
            id: Uuid::new_v4(),
            text: "ещё одно".into(),
            created_at: Utc::now(),
        },
    ];
    storage.db().self_model_upsert(&m).unwrap();

    migrate_self_narrative(&storage, profile);

    // The blob's narrative is cleared, observations became @self notes (created_at preserved).
    assert!(
        storage
            .db()
            .self_model_get(profile)
            .unwrap()
            .unwrap()
            .narrative
            .is_empty()
    );
    let self_notes = storage
        .db()
        .note_list(profile, None, &[SELF_NOTE_TAG.to_string()], None)
        .unwrap();
    assert_eq!(self_notes.len(), 2);
    assert!(
        self_notes
            .iter()
            .any(|n| n.content == "старое наблюдение" && n.created_at == old)
    );

    // A repeat pass — a no-op (the narrative is empty, no duplicates).
    migrate_self_narrative(&storage, profile);
    assert_eq!(
        storage
            .db()
            .note_list(profile, None, &[SELF_NOTE_TAG.to_string()], None)
            .unwrap()
            .len(),
        2
    );
}

#[tokio::test]
async fn self_notes_relevant_ranks_and_filters() {
    // Tier 2: relevance-based injection — self_notes_relevant ranks self-notes by
    // closeness to the query and doesn't return regular notes.
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    create_note(
        &ctx,
        "aaaa про краткость".into(),
        vec![SELF_NOTE_TAG.to_string()],
    )
    .await
    .unwrap();
    create_note(
        &ctx,
        "wwww про погоду".into(),
        vec![SELF_NOTE_TAG.to_string()],
    )
    .await
    .unwrap();
    // A regular note (not @self) — shouldn't make it into the observation selection.
    create_note(&ctx, "aaaa обычная".into(), vec![])
        .await
        .unwrap();

    let rel = self_notes_relevant(&storage, ctx.embedder.as_ref(), profile, "aaaa", 3).await;
    assert!(!rel.is_empty());
    assert!(rel[0].content.contains("краткость")); // closest to "aaaa"
    assert!(rel.iter().all(|n| n.content != "aaaa обычная")); // @self only
    // An empty query → empty (the caller's graceful degradation to recency).
    assert!(
        self_notes_relevant(&storage, ctx.embedder.as_ref(), profile, "  ", 3)
            .await
            .is_empty()
    );
}

#[tokio::test]
async fn save_surfaces_similar_notes_as_gate() {
    // MockEmbedder(16) — a bag of characters: texts sharing letters are close.
    let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "aaaa bbbb"}))
        .await
        .unwrap();
    // The second note is close by characters → the gate must show the first one.
    let out = NoteSave
        .invoke(&ctx, serde_json::json!({"content": "aaab"}))
        .await
        .unwrap();
    assert!(out.result.contains("Похожие заметки"));
    assert!(out.result.contains("aaaa bbbb"));
}

#[tokio::test]
async fn recall_semantic_finds_non_substring_match() {
    let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "aaaa"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "wwww"}))
        .await
        .unwrap();
    // The query "aaab" isn't a substring of any note, but is semantically closer
    // to "aaaa" → the semantic path finds it.
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({"query": "aaab", "limit": 1}))
        .await
        .unwrap();
    assert!(out.result.contains("aaaa"));
    assert!(!out.result.contains("wwww"));
}

#[tokio::test]
async fn save_gate_surfaces_legacy_note_without_vector() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    // An "old" note with no vector (inserted directly — as before the feature/on import).
    storage
        .db()
        .note_insert(&Note::new(profile, "aaaa bbbb", vec![]))
        .unwrap();
    assert_eq!(
        storage.db().notes_missing_vectors(profile).unwrap().len(),
        1
    );

    // Save a similar one — the gate must show the old one (backfill in note_save).
    let out = NoteSave
        .invoke(&ctx, serde_json::json!({"content": "aaab"}))
        .await
        .unwrap();
    assert!(out.result.contains("Похожие заметки"));
    assert!(out.result.contains("aaaa bbbb"));
    // Backfill indexed the old note.
    assert_eq!(
        storage.db().notes_missing_vectors(profile).unwrap().len(),
        0
    );
}

#[tokio::test]
async fn recall_backfills_legacy_notes_without_vectors() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    // "Old" notes with no vectors (inserted directly — as before the feature/on import).
    storage
        .db()
        .note_insert(&Note::new(profile, "aaaa", vec![]))
        .unwrap();
    storage
        .db()
        .note_insert(&Note::new(profile, "wwww", vec![]))
        .unwrap();
    assert_eq!(
        storage.db().notes_missing_vectors(profile).unwrap().len(),
        2
    );

    // Semantic recall pulls in vectors and finds a non-substring match.
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({"query": "aaab", "limit": 1}))
        .await
        .unwrap();
    assert!(out.result.contains("aaaa"));
    // Backfill was performed — no more notes without vectors.
    assert_eq!(
        storage.db().notes_missing_vectors(profile).unwrap().len(),
        0
    );
}

#[tokio::test]
async fn revise_rewrites_in_place() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "старое"}))
        .await
        .unwrap();
    let id = storage.db().note_list(profile, None, &[], None).unwrap()[0].id;

    let out = NoteRevise
        .invoke(
            &ctx,
            serde_json::json!({"id": id.to_string(), "content": "новое"}),
        )
        .await
        .unwrap();
    assert!(out.result.contains("переписана"));
    // Content is replaced in place (no new note added).
    let notes = storage.db().note_list(profile, None, &[], None).unwrap();
    assert_eq!(notes.len(), 1);
    assert_eq!(notes[0].content, "новое");
}

#[tokio::test]
async fn revise_bad_and_missing_id() {
    let (_d, _s, ctx) = ctx_with_storage(Uuid::new_v4());
    // An invalid uuid → an error.
    assert!(
        NoteRevise
            .invoke(&ctx, serde_json::json!({"id": "not-uuid", "content": "x"}))
            .await
            .is_err()
    );
    // Valid but nonexistent → clear text, not a panic.
    let out = NoteRevise
        .invoke(
            &ctx,
            serde_json::json!({"id": Uuid::new_v4().to_string(), "content": "x"}),
        )
        .await
        .unwrap();
    assert!(out.result.contains("не найдена"));
}

/// A note's id by content (for graph tests).
fn id_by_content(storage: &crate::shared::storage::Storage, profile: Uuid, content: &str) -> Uuid {
    storage
        .db()
        .note_list(profile, None, &[], None)
        .unwrap()
        .into_iter()
        .find(|n| n.content == content)
        .unwrap()
        .id
}

#[tokio::test]
async fn link_then_neighbors() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "альфа"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "бета"}))
        .await
        .unwrap();
    let a = id_by_content(&storage, profile, "альфа");
    let b = id_by_content(&storage, profile, "бета");

    let out = NoteLink
            .invoke(
                &ctx,
                serde_json::json!({"from_id": a.to_string(), "to_id": b.to_string(), "relation": "refines"}),
            )
            .await
            .unwrap();
    assert!(out.result.contains("Связь создана"));

    // Repeating the same link — an honest "already existed" answer (no duplicate
    // in the graph).
    let dup = NoteLink
            .invoke(
                &ctx,
                serde_json::json!({"from_id": a.to_string(), "to_id": b.to_string(), "relation": "refines"}),
            )
            .await
            .unwrap();
    assert!(dup.result.contains("уже существовала"));

    let nb = NoteNeighbors
        .invoke(&ctx, serde_json::json!({"id": a.to_string()}))
        .await
        .unwrap();
    assert!(nb.result.contains("бета"));
    assert!(nb.result.contains("refines"));

    // An unknown relation type and a self-link → errors.
    assert!(
            NoteLink
                .invoke(
                    &ctx,
                    serde_json::json!({"from_id": a.to_string(), "to_id": b.to_string(), "relation": "foo"}),
                )
                .await
                .is_err()
        );
    assert!(
            NoteLink
                .invoke(
                    &ctx,
                    serde_json::json!({"from_id": a.to_string(), "to_id": a.to_string(), "relation": "relates"}),
                )
                .await
                .is_err()
        );
}

#[tokio::test]
async fn revise_warns_when_note_has_links() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "узел"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "другой"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "одиночка"}))
        .await
        .unwrap();
    let a = id_by_content(&storage, profile, "узел");
    let b = id_by_content(&storage, profile, "другой");
    let lone = id_by_content(&storage, profile, "одиночка");
    NoteLink
            .invoke(
                &ctx,
                serde_json::json!({"from_id": b.to_string(), "to_id": a.to_string(), "relation": "contradicts"}),
            )
            .await
            .unwrap();

    // Revising a node with a link warns about note_supersede.
    let out = NoteRevise
        .invoke(
            &ctx,
            serde_json::json!({"id": a.to_string(), "content": "узел v2"}),
        )
        .await
        .unwrap();
    assert!(out.result.contains("переписана"));
    assert!(out.result.contains("note_supersede"));

    // A node with no links — no warning.
    let out2 = NoteRevise
        .invoke(
            &ctx,
            serde_json::json!({"id": lone.to_string(), "content": "одиночка v2"}),
        )
        .await
        .unwrap();
    assert!(!out2.result.contains("note_supersede"));
}

#[tokio::test]
async fn supersede_hides_old_shows_new() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "первая версия"}))
        .await
        .unwrap();
    let old = id_by_content(&storage, profile, "первая версия");

    let out = NoteSupersede
        .invoke(
            &ctx,
            serde_json::json!({"old_id": old.to_string(), "content": "вторая версия"}),
        )
        .await
        .unwrap();
    assert!(out.result.contains("замещена"));

    let active = storage.db().note_list(profile, None, &[], None).unwrap();
    assert_eq!(active.len(), 1);
    assert_eq!(active[0].content, "вторая версия");
    assert!(!storage.db().note_is_active(profile, old).unwrap());
}

#[tokio::test]
async fn merge_consolidates_sources() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "кусок один"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "кусок два"}))
        .await
        .unwrap();
    let ids: Vec<String> = storage
        .db()
        .note_list(profile, None, &[], None)
        .unwrap()
        .into_iter()
        .map(|n| n.id.to_string())
        .collect();

    let out = NoteMerge
        .invoke(
            &ctx,
            serde_json::json!({"ids": ids, "content": "единая заметка"}),
        )
        .await
        .unwrap();
    assert!(out.result.contains("Объединено заметок: 2"));

    let active = storage.db().note_list(profile, None, &[], None).unwrap();
    assert_eq!(active.len(), 1);
    assert_eq!(active[0].content, "единая заметка");

    // Fewer than two existing ones → an error.
    assert!(
        NoteMerge
            .invoke(
                &ctx,
                serde_json::json!({"ids": [Uuid::new_v4().to_string()], "content": "x"}),
            )
            .await
            .is_err()
    );
}

#[tokio::test]
async fn supersede_preserves_tags() {
    // Superseding a self-note preserves the @self tag — the new version stays
    // hidden from user-facing recall (otherwise it would "fall out" into the output).
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    storage
        .db()
        .note_insert(&Note::new(
            profile,
            "версия 1",
            vec![SELF_NOTE_TAG.to_string()],
        ))
        .unwrap();
    let old = storage
        .db()
        .note_list(profile, None, &[SELF_NOTE_TAG.to_string()], None)
        .unwrap()[0]
        .id;
    NoteSupersede
        .invoke(
            &ctx,
            serde_json::json!({"old_id": old.to_string(), "content": "версия 2"}),
        )
        .await
        .unwrap();
    let self_notes = storage
        .db()
        .note_list(profile, None, &[SELF_NOTE_TAG.to_string()], None)
        .unwrap();
    assert_eq!(self_notes.len(), 1);
    assert_eq!(self_notes[0].content, "версия 2");
    // And doesn't surface in regular recall.
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({}))
        .await
        .unwrap();
    assert!(out.result.contains("не найдены"));
}

#[tokio::test]
async fn merge_unions_tags() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    storage
        .db()
        .note_insert(&Note::new(profile, "aaa", vec!["x".into()]))
        .unwrap();
    storage
        .db()
        .note_insert(&Note::new(profile, "bbb", vec!["y".into()]))
        .unwrap();
    let ids: Vec<String> = storage
        .db()
        .note_list(profile, None, &[], None)
        .unwrap()
        .iter()
        .map(|n| n.id.to_string())
        .collect();
    NoteMerge
        .invoke(&ctx, serde_json::json!({"ids": ids, "content": "ccc"}))
        .await
        .unwrap();
    let merged = storage.db().note_list(profile, None, &[], None).unwrap();
    assert_eq!(merged.len(), 1);
    assert!(merged[0].tags.contains(&"x".to_string()));
    assert!(merged[0].tags.contains(&"y".to_string()));
}

#[tokio::test]
async fn recall_spreads_to_linked_notes() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "aaaa"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "zzzz"}))
        .await
        .unwrap();
    let a = id_by_content(&storage, profile, "aaaa");
    let z = id_by_content(&storage, profile, "zzzz");
    NoteLink
            .invoke(
                &ctx,
                serde_json::json!({"from_id": a.to_string(), "to_id": z.to_string(), "relation": "relates"}),
            )
            .await
            .unwrap();

    // The query is close to "aaaa"; "zzzz" isn't similar, but is linked → makes
    // it into "Related".
    let out = NoteRecall
        .invoke(&ctx, serde_json::json!({"query": "aaab", "limit": 1}))
        .await
        .unwrap();
    assert!(out.result.contains("aaaa"));
    assert!(out.result.contains("Связанные заметки"));
    assert!(out.result.contains("zzzz"));
}

#[tokio::test]
async fn consolidate_notes_reports_dups_and_dangling() {
    let profile = Uuid::new_v4();
    let (_d, _s, ctx) = ctx_with_storage(profile);
    // Two near-duplicates (shared characters → a high cosine on MockEmbedder).
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "aaaa bbbb"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "aaaa bbbbb"}))
        .await
        .unwrap();
    // An unrelated, dissimilar note.
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "zzzz"}))
        .await
        .unwrap();

    let out = ConsolidateNotes
        .invoke(&ctx, serde_json::json!({}))
        .await
        .unwrap();
    assert!(out.result.contains("Обзор базы знаний"));
    // A similar pair was found (at least one).
    assert!(out.result.contains("Похожие пары (возможные дубли"));
    assert!(out.result.contains("aaaa bbbb"));
    assert!(out.result.contains("без связей"));
    assert!(out.result.contains("note_merge"));
}

#[tokio::test]
async fn merge_transfers_links_to_new_note() {
    let profile = Uuid::new_v4();
    let (_d, storage, ctx) = ctx_with_storage(profile);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "часть один"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "часть два"}))
        .await
        .unwrap();
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "третья"}))
        .await
        .unwrap();
    // Ids taken BEFORE merging (afterward the sources are superseded and vanish
    // from the list).
    let s1 = id_by_content(&storage, profile, "часть один");
    let s2 = id_by_content(&storage, profile, "часть два");
    let other = id_by_content(&storage, profile, "третья");
    NoteLink
            .invoke(
                &ctx,
                serde_json::json!({"from_id": s1.to_string(), "to_id": other.to_string(), "relation": "relates"}),
            )
            .await
            .unwrap();

    NoteMerge
        .invoke(
            &ctx,
            serde_json::json!({"ids": [s1.to_string(), s2.to_string()], "content": "единая"}),
        )
        .await
        .unwrap();

    // The source's link is transferred onto the merged note: "third"'s neighbor
    // is "merged".
    let nb = NoteNeighbors
        .invoke(&ctx, serde_json::json!({"id": other.to_string()}))
        .await
        .unwrap();
    assert!(nb.result.contains("единая"));
    assert!(!nb.result.contains("часть один"));
}

/// The note writers report a write on the path that wrote and nothing on a
/// refusal or a no-op — a link that already existed changed nothing
/// (docs/research/acted-by-effect.md §3.1).
#[tokio::test]
async fn note_writers_report_a_write_only_when_they_wrote() {
    let (_d, storage, ctx) = ctx_with_storage(Uuid::new_v4());
    let profile = ctx.profile_id;
    let saved = NoteSave
        .invoke(&ctx, serde_json::json!({"content": "первая заметка"}))
        .await
        .unwrap();
    assert!(saved.wrote);
    NoteSave
        .invoke(&ctx, serde_json::json!({"content": "вторая заметка"}))
        .await
        .unwrap();
    let ids: Vec<Uuid> = storage
        .db()
        .note_list(profile, None, &[], None)
        .unwrap()
        .iter()
        .map(|n| n.id)
        .collect();
    let (a, b) = (ids[0], ids[1]);

    let revised = NoteRevise
        .invoke(
            &ctx,
            serde_json::json!({"id": a.to_string(), "content": "новое"}),
        )
        .await
        .unwrap();
    assert!(revised.wrote);
    let missing = NoteRevise
        .invoke(
            &ctx,
            serde_json::json!({"id": Uuid::new_v4().to_string(), "content": "x"}),
        )
        .await
        .unwrap();
    assert!(!missing.wrote, "a refusal: {}", missing.result);

    let link = serde_json::json!({"from_id": a.to_string(), "to_id": b.to_string(), "relation": "refines"});
    let linked = NoteLink.invoke(&ctx, link.clone()).await.unwrap();
    assert!(linked.wrote);
    let again = NoteLink.invoke(&ctx, link).await.unwrap();
    assert!(!again.wrote, "an existing link changes nothing");

    let superseded = NoteSupersede
        .invoke(
            &ctx,
            serde_json::json!({"old_id": b.to_string(), "content": "замена"}),
        )
        .await
        .unwrap();
    assert!(superseded.wrote);
}