sara-tasks 1.7.0

Sara — folder-aware task manager
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
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
use anyhow::{Context, Result};
use rusqlite::Connection;
use serde_json::{Value, json};
use std::path::{Path, PathBuf};
use uuid::Uuid;

use crate::infrastructure::config::Config;
use crate::infrastructure::db;
use crate::infrastructure::model::Item;
use crate::infrastructure::project::{detect_current_project, resolve_file_link_here};

/// Size threshold in characters above which we warn that the text is probably
/// not a distilled paragraph. A full conversation paste defeats the
/// token-minimization premise of the whole feature.
/// `sara learn "<text>" [--tag] [-p] [--task] [--file] [--auto-files] [--supersedes <label>]`
pub fn run(
    conn: &Connection,
    cfg: &Config,
    text: &str,
    tags: &[String],
    projects: &[String],
    tasks: &[String],
    files: &[String],
    auto_files: bool,
    force: bool,
    supersedes: &[String],
    derived_from: &[String],
    similar_to: &[String],
) -> Result<()> {
    let v = learn_value(
        conn,
        cfg,
        text,
        tags,
        projects,
        tasks,
        files,
        auto_files,
        force,
        supersedes,
        derived_from,
        similar_to,
    )?;
    let label = v["label"].as_str().unwrap_or("m?");
    let uuid = v["uuid"].as_str().unwrap_or("");
    let body = v["text"].as_str().unwrap_or("");

    let file_suffix = {
        let fs: Vec<&str> = v["files"]
            .as_array()
            .map(|a| a.iter().filter_map(|x| x.as_str()).collect())
            .unwrap_or_default();
        if fs.is_empty() {
            String::new()
        } else {
            format!(" (files: {})", fs.join(", "))
        }
    };
    let task_suffix = {
        let ts = v["linked_tasks"].as_array();
        let parts: Vec<String> = ts
            .map(|a| {
                a.iter()
                    .filter_map(|t| {
                        let id = t["id"].as_i64()?;
                        let desc = t["description"].as_str()?;
                        let src = t["source"].as_str().unwrap_or("auto");
                        Some(format!("#{id} {desc} [{src}]"))
                    })
                    .collect()
            })
            .unwrap_or_default();
        if parts.is_empty() {
            String::new()
        } else {
            format!(" (tasks: {})", parts.join(", "))
        }
    };

    println!("Learned {label} ({uuid}): {body}{file_suffix}{task_suffix}");
    if let Some(links) = v["superseded"].as_array() {
        for link in links {
            if let Some(old_label) = link.as_str() {
                println!("  ↳ supersedes {old_label}");
            }
        }
    }
    if let Some(links) = v["derived_from"].as_array() {
        for link in links {
            if let Some(canon_label) = link.as_str() {
                println!("  ↳ derived from {canon_label}");
            }
        }
    }
    if let Some(links) = v["auto_derived_from"].as_array() {
        for link in links {
            if let Some(canon_label) = link.as_str() {
                println!(
                    "  ↳ auto-linked as an application of canonical pattern {canon_label} \
                     (unlink with `sara unlink-memory {label} derived_from {canon_label}`)"
                );
            }
        }
    }
    if let Some(links) = v["similar_to"].as_array() {
        for link in links {
            if let Some(other_label) = link.as_str() {
                println!("  ↳ similar to {other_label}");
            }
        }
    }
    Ok(())
}

/// Print-free core shared by the CLI `learn` command and the MCP `learn` tool.
pub fn learn_value(
    conn: &Connection,
    cfg: &Config,
    text: &str,
    tags: &[String],
    projects: &[String],
    tasks: &[String],
    files: &[String],
    auto_files: bool,
    force: bool,
    supersedes: &[String],
    derived_from: &[String],
    similar_to: &[String],
) -> Result<Value> {
    let text = text.trim();
    let resolved_files = collect_files(files, auto_files)?;
    // Birth-time canonical attachment: canonical patterns this memory's tags
    // place it inside. Computed before the write (needs the pre-insert tag view)
    // and linked after, so a new instance auto-attaches to its pattern.
    let mut auto_canonical: Vec<uuid::Uuid> = Vec::new();
    if !force {
        crate::infrastructure::safety::check_size(text)?;
        crate::infrastructure::safety::check_secrets(text)?;
        // Scope the mandatory overlap band to the province this memory lands in
        // (explicit -p, else the current project) so common tags don't flag
        // other provinces' memories as must-resolve.
        let primary_project = projects.first().cloned().or_else(|| {
            crate::infrastructure::project::detect_current_project(conn, cfg)
                .ok()
                .map(|(p, _)| p)
        });
        check_overlap(conn, tags, &resolved_files, primary_project.as_deref())?;
        auto_canonical = canonical_overlap_candidates(conn, tags)?;
    }

    let item = save(conn, cfg, text, tags, projects, tasks, &resolved_files)?;

    let new_uuid = item.uuid.to_string();

    // Resolve and insert typed links atomically with the learn. `supersedes` and
    // `derived_from` point FROM the new memory TO the existing one; `similar_to`
    // is symmetric intent but stored new→existing. An unresolvable label warns
    // and is skipped — it never aborts the learn.
    let resolve_and_link =
        |handles: &[String], relation: &str, flag: &str| -> Result<Vec<String>> {
            let mut labels: Vec<String> = Vec::new();
            for handle in handles {
                match db::get_item_by_handle(conn, handle) {
                    Ok(target) => {
                        db::insert_memory_link(
                            conn,
                            &new_uuid,
                            &target.uuid.to_string(),
                            relation,
                            1.0,
                        )?;
                        let target_label = target
                            .display_id
                            .map(|id| format!("m{id}"))
                            .unwrap_or_else(|| handle.clone());
                        // Superseding a canonical (one with derived children) can
                        // orphan those children on a now-corrected pattern — warn,
                        // never auto-archive (mirrors `sara forget`'s behaviour).
                        if relation == "supersedes" {
                            let derived: Vec<String> =
                                db::get_memory_links_to(conn, &target.uuid.to_string())
                                    .unwrap_or_default()
                                    .into_iter()
                                    .filter(|l| l.relation == "derived_from")
                                    .filter_map(|l| db::get_item_by_uuid(conn, &l.from_uuid).ok())
                                    .map(|i| format!("m{}", i.display_id.unwrap_or(0)))
                                    .collect();
                            if !derived.is_empty() {
                                eprintln!(
                                    "Warning: {target_label} is a canonical pattern memory with \
                                     {} derived {} ({}) — review with `sara dream <label>` or \
                                     archive with `sara forget <label>`; they are not \
                                     auto-archived by this supersede.",
                                    derived.len(),
                                    if derived.len() == 1 {
                                        "memory"
                                    } else {
                                        "memories"
                                    },
                                    derived.join(", ")
                                );
                            }
                        }
                        labels.push(target_label);
                    }
                    Err(e) => {
                        eprintln!(
                            "warning: could not resolve memory '{}' for {flag}: {e}",
                            handle
                        );
                    }
                }
            }
            Ok(labels)
        };

    let superseded_labels = resolve_and_link(supersedes, "supersedes", "--supersedes")?;
    let derived_from_labels = resolve_and_link(derived_from, "derived_from", "--derived-from")?;
    let similar_to_labels = resolve_and_link(similar_to, "similar_to", "--similar-to")?;

    // Birth-time canonical attachment: auto-link `derived_from` to each canonical
    // pattern this memory's tags landed it inside, unless the author already
    // named that canonical in an explicit --derived-from/--supersedes/--similar-to
    // (an explicit intent — including a deliberate supersede — always wins). The
    // pattern strengthens as instances are learned, without a later `reflect`.
    // Reversible with `sara unlink-memory <this> derived_from <canonical>`.
    let mut auto_derived_labels: Vec<String> = Vec::new();
    {
        let already: std::collections::HashSet<&String> = superseded_labels
            .iter()
            .chain(derived_from_labels.iter())
            .chain(similar_to_labels.iter())
            .collect();
        for u in &auto_canonical {
            if let Ok(target) = db::get_item_by_uuid(conn, &u.to_string()) {
                let label = format!("m{}", target.display_id.unwrap_or(0));
                if already.contains(&label) || auto_derived_labels.contains(&label) {
                    continue;
                }
                db::insert_memory_link(conn, &new_uuid, &u.to_string(), "derived_from", 1.0)?;
                auto_derived_labels.push(label);
            }
        }
    }

    let files_json: Vec<Value> = resolved_files.iter().map(|f| json!(f)).collect();
    let tasks_json: Vec<Value> = item
        .linked_tasks
        .iter()
        .map(|(id, desc, src)| {
            json!({
                "id": id.parse::<i64>().unwrap_or(0),
                "description": desc,
                "source": src,
            })
        })
        .collect();

    Ok(json!({
        "label": format!("m{}", item.display_id.unwrap_or(0)),
        "uuid": &new_uuid[..8],
        "text": summarize(text),
        "tags": item.tags,
        "files": files_json,
        "linked_tasks": tasks_json,
        "superseded": superseded_labels,
        "derived_from": derived_from_labels,
        "similar_to": similar_to_labels,
        "auto_derived_from": auto_derived_labels,
    }))
}

/// Build the copy-pasteable typed-link suggestion for a near-duplicate overlap
/// (existing memory shares ALL of the new memory's tags). A near-dupe is either
/// a specialisation (→ `derived_from`) or a replacement (→ `supersedes`).
pub(crate) fn near_dupe_suggestion(label: &str) -> String {
    format!(
        "→ Link it typed: re-run with `--derived-from {label}` if this specialises it, \
         or `--supersedes {label}` if it replaces it (or --force to keep both)."
    )
}

/// Build the copy-pasteable typed-link suggestion for a partial overlap
/// (shares ≥50% but not all tags). A partial overlap is a lateral relation
/// (→ `similar_to`).
pub(crate) fn partial_overlap_suggestion(label: &str) -> String {
    format!(
        "→ Link it typed: re-run with `--similar-to {label}` to connect them in the memory graph."
    )
}

/// Number of `derived_from` children pointing at `uuid` — non-zero means
/// `uuid` is a canonical pattern memory, not just "another similar memory".
pub(crate) fn canonical_derived_count(conn: &Connection, uuid: &uuid::Uuid) -> usize {
    db::get_memory_links_to(conn, &uuid.to_string())
        .unwrap_or_default()
        .into_iter()
        .filter(|l| l.relation == "derived_from")
        .count()
}

/// Build the copy-pasteable hint for a candidate that is itself a canonical
/// pattern memory. Upgrades the generic near-dupe/partial suggestion into one
/// that specifically proposes `--derived-from` (register as another
/// application of the pattern) or `sara relearn` (enrich the canonical in
/// place) instead of silently accumulating another near-duplicate.
pub(crate) fn canonical_hint(label: &str, derived_count: usize) -> String {
    format!(
        "→ {label} is a canonical pattern memory with {derived_count} derived application{} — \
         consider `sara learn --derived-from {label}` to register this as another application, \
         or `sara relearn {label}` to enrich the canonical instead of creating a new memory.",
        if derived_count == 1 { "" } else { "s" }
    )
}

/// Canonical pattern memories the new memory's tags place it squarely inside:
/// near-duplicates (shares ALL tags) or canonical partial-tag matches (shares
/// ≥50% of tags with a memory that already has derived children). These are the
/// birth-time auto-attach targets — a new memory landing on an established
/// pattern is registered as another application (`derived_from`) automatically,
/// so the pattern strengthens as instances are learned instead of only during a
/// later `reflect` sweep.
///
/// Unlike `check_overlap`'s *mandatory warning* band, this deliberately does NOT
/// restrict to the current project: a canonical pattern is inherently
/// cross-project (the same fault recurring across repos is exactly what makes it
/// canonical), so a new repo's instance must be allowed to attach to it. The
/// confidence gate is instead "the overlap target is already a canonical".
/// Returns candidate uuids (deduped, order-stable).
pub(crate) fn canonical_overlap_candidates(
    conn: &Connection,
    tags: &[String],
) -> Result<Vec<uuid::Uuid>> {
    let normalized: Vec<String> = tags
        .iter()
        .map(|t| t.trim().to_lowercase())
        .filter(|t| !t.is_empty())
        .collect();
    if normalized.is_empty() {
        return Ok(vec![]);
    }

    let mut any_union: std::collections::HashSet<uuid::Uuid> = std::collections::HashSet::new();
    let mut all_intersection: Option<std::collections::HashSet<uuid::Uuid>> = None;
    let mut per_tag_sets: Vec<std::collections::HashSet<uuid::Uuid>> = Vec::new();
    for tag in &normalized {
        let uuids: std::collections::HashSet<uuid::Uuid> = db::find_items_by_tag(conn, tag)?
            .into_iter()
            .filter(|i| i.kind == "memory")
            .map(|i| i.uuid)
            .collect();
        any_union.extend(&uuids);
        all_intersection = Some(match all_intersection {
            Some(existing) => existing.intersection(&uuids).copied().collect(),
            None => uuids.clone(),
        });
        per_tag_sets.push(uuids);
    }

    let near_dupes: std::collections::HashSet<uuid::Uuid> = all_intersection.unwrap_or_default();
    let partial: std::collections::HashSet<uuid::Uuid> =
        any_union.difference(&near_dupes).copied().collect();
    let threshold = ((normalized.len() as f64) * 0.5).ceil() as usize;
    let significant_partial: Vec<uuid::Uuid> = if normalized.len() > 1 {
        partial
            .into_iter()
            .filter(|u| per_tag_sets.iter().filter(|set| set.contains(u)).count() >= threshold)
            .collect()
    } else {
        vec![]
    };

    // Candidates = near-dupes ∪ significant partials, filtered to established
    // canonicals (those with derived children).
    let mut out: Vec<uuid::Uuid> = Vec::new();
    for u in near_dupes.into_iter().chain(significant_partial) {
        if !out.contains(&u) && canonical_derived_count(conn, &u) > 0 {
            out.push(u);
        }
    }
    Ok(out)
}

/// Detect existing memories whose tags overlap significantly with the new one,
/// and memories that share any of the same file links (even with different tags).
/// Two tag-overlap bands:
///   - Near-duplicate: existing memory shares ALL given tags → warn prominently.
///   - Partial overlap: shares ≥50% (but not all) tags → softer note.
///
/// File-overlap: any memory already linked to one of the new memory's files → warn.
/// Prints warnings to stderr; never blocks the write (use --force to silence).
pub(crate) fn check_overlap(
    conn: &Connection,
    tags: &[String],
    files: &[String],
    current_project: Option<&str>,
) -> Result<()> {
    let normalized: Vec<String> = tags
        .iter()
        .map(|t| t.trim().to_lowercase())
        .filter(|t| !t.is_empty())
        .collect();

    // Build UUID sets per tag, then intersect (near-dupe) and union (any overlap).
    let mut any_union: std::collections::HashSet<uuid::Uuid> = std::collections::HashSet::new();
    let mut all_intersection: Option<std::collections::HashSet<uuid::Uuid>> = None;
    let mut per_tag_sets: Vec<std::collections::HashSet<uuid::Uuid>> = Vec::new();

    for tag in &normalized {
        let uuids: std::collections::HashSet<uuid::Uuid> = db::find_items_by_tag(conn, tag)?
            .into_iter()
            .filter(|i| i.kind == "memory")
            .map(|i| i.uuid)
            .collect();
        any_union.extend(&uuids);
        all_intersection = Some(match all_intersection {
            Some(existing) => existing.intersection(&uuids).copied().collect(),
            None => uuids.clone(),
        });
        per_tag_sets.push(uuids);
    }

    let near_dupes: std::collections::HashSet<uuid::Uuid> = all_intersection.unwrap_or_default();
    let partial: std::collections::HashSet<uuid::Uuid> =
        any_union.difference(&near_dupes).copied().collect();

    // Only surface partial overlaps if they share ≥50% of the given tags.
    let threshold = ((normalized.len() as f64) * 0.5).ceil() as usize;
    let significant_partial: Vec<uuid::Uuid> = if normalized.len() > 1 {
        partial
            .into_iter()
            .filter(|u| {
                let count = per_tag_sets.iter().filter(|set| set.contains(u)).count();
                count >= threshold
            })
            .collect()
    } else {
        vec![]
    };

    // Canonical detection: a candidate that already has derived children is an
    // established pattern, not just "another similar memory". A partial-tag
    // match against a canonical is upgraded to the near-dupe band, and any
    // canonical hit (near-dupe or promoted-partial) gets a specific
    // `--derived-from`/`relearn` hint instead of the generic suggestion.
    let (canonical_partial, plain_partial): (Vec<uuid::Uuid>, Vec<uuid::Uuid>) =
        significant_partial
            .into_iter()
            .partition(|u| canonical_derived_count(conn, u) > 0);

    // p3: the mandatory-resolve band is only actionable for SAME-province
    // overlaps — a common tag (auth/api/db) otherwise floods it with other
    // provinces' memories the author cannot resolve. Keep same-project hits in
    // the mandatory band; demote cross-project ones to the soft "related" note.
    // With no project context (current_project = None) the old global behaviour
    // is preserved.
    let mut plain_partial = plain_partial;
    let same_project = |u: &uuid::Uuid| -> bool {
        match current_project {
            None => true,
            Some(p) => {
                db::get_item_by_uuid(conn, &u.to_string())
                    .ok()
                    .and_then(|i| i.project)
                    .as_deref()
                    == Some(p)
            }
        }
    };
    let (mandatory, cross_project): (Vec<uuid::Uuid>, Vec<uuid::Uuid>) = near_dupes
        .iter()
        .copied()
        .chain(canonical_partial.iter().copied())
        .partition(&same_project);
    plain_partial.extend(cross_project);

    if !mandatory.is_empty() {
        let all_dupes: Vec<uuid::Uuid> = mandatory;
        eprintln!(
            "Warning: {} existing {} with identical or canonical-matching tags [{}]:",
            all_dupes.len(),
            if all_dupes.len() == 1 {
                "memory"
            } else {
                "memories"
            },
            normalized.join(", ")
        );
        let mut labels: Vec<String> = Vec::new();
        let mut canonical_hit: Option<(String, usize)> = None;
        for u in &all_dupes {
            if let Ok(item) = db::get_item_by_uuid(conn, &u.to_string()) {
                let label = format!("m{}", item.display_id.unwrap_or(0));
                let snippet: String = item.body.chars().take(80).collect();
                let derived_count = canonical_derived_count(conn, u);
                let suffix = if derived_count > 0 {
                    format!(" [canonical, {derived_count} derived]")
                } else {
                    String::new()
                };
                eprintln!("  {label}{suffix} — {}", snippet.trim());
                if derived_count > 0 && canonical_hit.is_none() {
                    canonical_hit = Some((label.clone(), derived_count));
                }
                labels.push(label);
            }
        }
        // A canonical hit gets the specific --derived-from/relearn hint;
        // otherwise fall back to the generic copy-pasteable typed link.
        match (&canonical_hit, labels.first()) {
            (Some((label, count)), _) => eprintln!("{}", canonical_hint(label, *count)),
            (None, Some(first)) => eprintln!("{}", near_dupe_suggestion(first)),
            (None, None) => {}
        }
    }

    if !plain_partial.is_empty() {
        eprintln!(
            "Note: {} potentially related {} (partial tag overlap):",
            plain_partial.len(),
            if plain_partial.len() == 1 {
                "memory"
            } else {
                "memories"
            }
        );
        let mut labels: Vec<String> = Vec::new();
        for u in &plain_partial {
            if let Ok(item) = db::get_item_by_uuid(conn, &u.to_string()) {
                let label = format!("m{}", item.display_id.unwrap_or(0));
                let snippet: String = item.body.chars().take(80).collect();
                eprintln!("  {} — {}", label, snippet.trim());
                labels.push(label);
            }
        }
        if let Some(first) = labels.first() {
            eprintln!("{}", partial_overlap_suggestion(first));
        }
    }

    // File-overlap check: warn for any existing memory sharing a file path.
    // Runs whether or not tags were given, so a `--file`-only learn still
    // surfaces "this file already has a memory".
    if !files.is_empty() {
        let file_overlap = file_overlaps(conn, files, &near_dupes)?;
        if !file_overlap.is_empty() {
            eprintln!(
                "Note: {} existing {} linked to the same file(s) — consider --supersedes or sara relearn:",
                file_overlap.len(),
                if file_overlap.len() == 1 {
                    "memory"
                } else {
                    "memories"
                }
            );
            for (path, u) in &file_overlap {
                if let Ok(item) = db::get_item_by_uuid(conn, &u.to_string()) {
                    let label = format!("m{}", item.display_id.unwrap_or(0));
                    let snippet: String = item.body.chars().take(80).collect();
                    let short_path = std::path::Path::new(path)
                        .file_name()
                        .and_then(|n| n.to_str())
                        .unwrap_or(path.as_str());
                    eprintln!("  {} [{short_path}] — {}", label, snippet.trim());
                }
            }
        }
    }

    Ok(())
}

/// Existing *memory* items linked to any of `files`, excluding those in
/// `exclude` (already reported as tag near-dupes). Each memory appears once,
/// paired with the first file path that matched it. Pure detection — no I/O
/// beyond the DB — so it runs regardless of whether tags were supplied.
pub(crate) fn file_overlaps(
    conn: &Connection,
    files: &[String],
    exclude: &std::collections::HashSet<uuid::Uuid>,
) -> Result<Vec<(String, uuid::Uuid)>> {
    let mut out: Vec<(String, uuid::Uuid)> = Vec::new();
    for path in files {
        for item in db::find_items_by_file(conn, path, false)? {
            if item.kind == "memory"
                && !out.iter().any(|(_, u)| *u == item.uuid)
                && !exclude.contains(&item.uuid)
            {
                out.push((path.clone(), item.uuid));
            }
        }
    }
    Ok(out)
}

/// Collect file paths from explicit --file flags and optionally --auto-files.
/// All paths are resolved to absolute. Returns an error if --auto-files is
/// requested but no git root can be found and no --file was given.
fn collect_files(explicit: &[String], auto_files: bool) -> Result<Vec<String>> {
    let mut paths: Vec<String> = explicit.iter().map(|p| resolve_file_link_here(p)).collect();

    if auto_files {
        match find_git_root(&std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."))) {
            Some(root) => {
                let diff = git_diff_files(&root)?;
                paths.extend(diff);
            }
            None => {
                if explicit.is_empty() {
                    anyhow::bail!(
                        "No git root found — cannot use --auto-files outside a git repository.\n\
                         Pass --file <path> explicitly instead."
                    );
                }
                // git root not found but --file was given — skip auto-files silently
                // (the explicit files are still stored).
            }
        }
    }

    // Deduplicate while preserving order.
    let mut seen = std::collections::HashSet::new();
    paths.retain(|p| seen.insert(p.clone()));
    Ok(paths)
}

/// Walk parent directories looking for a `.git` entry.
fn find_git_root(start: &Path) -> Option<PathBuf> {
    let mut dir = start.to_path_buf();
    loop {
        if dir.join(".git").exists() {
            return Some(dir);
        }
        if !dir.pop() {
            return None;
        }
    }
}

/// Run `git diff --name-only HEAD` in `git_root` and return absolute paths.
fn git_diff_files(git_root: &Path) -> Result<Vec<String>> {
    let output = std::process::Command::new("git")
        .args(["diff", "--name-only", "HEAD"])
        .current_dir(git_root)
        .output()
        .context("running git diff --name-only HEAD")?;
    let stdout = String::from_utf8_lossy(&output.stdout);
    Ok(stdout
        .lines()
        .map(|l| l.trim())
        .filter(|l| !l.is_empty())
        .map(|l| git_root.join(l).to_string_lossy().into_owned())
        .collect())
}

fn save(
    conn: &Connection,
    cfg: &Config,
    text: &str,
    tags: &[String],
    projects: &[String],
    task_prefixes: &[String],
    files: &[String],
) -> Result<Item> {
    if text.is_empty() {
        anyhow::bail!("Memory text cannot be empty");
    }

    // Use the first explicit --task as source_task_uuid (backward compat with
    // item_strength). All tasks (explicit + auto-detected) go into item_task_links.
    let first_task_uuid: Option<Uuid> = match task_prefixes.first() {
        Some(prefix) => Some(
            db::resolve_task(conn, prefix)
                .with_context(|| format!("looking up --task '{prefix}'"))?
                .uuid,
        ),
        None => None,
    };

    let projects: Vec<String> = if projects.is_empty() {
        let (name, _) = detect_current_project(conn, cfg)?;
        vec![name]
    } else {
        projects.to_vec()
    };

    let mut item = Item::new_memory(summarize(text), text.to_string(), first_task_uuid);
    item.tags = tags.to_vec();
    item.path = Some(String::new());

    db::insert_item(conn, &mut item)?;
    db::set_item_projects(conn, &item.uuid, &projects)?;

    // Keep the semantic index current: semantic recall is ALWAYS enabled
    // (SemanticOpts::from_cfg), so every learned memory must be embedded or it
    // could never match by meaning. Always index — do not gate on the
    // deprecated `recall.semantic` toggle, which left semantic recall silently
    // dead in every project that never flipped it. Best-effort — never blocks
    // learning.
    crate::infrastructure::embedding::index_memory(conn, &item);

    // Store file associations.
    if !files.is_empty() {
        db::set_item_files(conn, &item.uuid, files)?;
    }

    // Build task links: auto-detect from file reverse-lookup, then merge explicit.
    let mut task_links: Vec<(Uuid, &'static str)> = vec![];

    for file in files {
        let found = db::find_tasks_by_file(conn, file, false)?;
        for t in found {
            if !task_links.iter().any(|(u, _)| *u == t.uuid) {
                task_links.push((t.uuid, "auto"));
            }
        }
    }

    // Resolve all explicit --task prefixes and merge (explicit wins). Resolution
    // is display-id-first (db::resolve_task), consistent with every other
    // task-referencing command; a bare number is a display id, not a uuid prefix.
    for prefix in task_prefixes {
        if let Ok(t) = db::resolve_task(conn, prefix) {
            if let Some(pos) = task_links.iter().position(|(u, _)| *u == t.uuid) {
                task_links[pos].1 = "explicit";
            } else {
                task_links.push((t.uuid, "explicit"));
            }
        }
    }

    if !task_links.is_empty() {
        // Convert &'static str slice to owned for set_item_task_links.
        let owned: Vec<(Uuid, &str)> = task_links.iter().map(|(u, s)| (*u, *s)).collect();
        db::set_item_task_links(conn, &item.uuid, &owned)?;

        // Populate item.linked_tasks for the caller to render.
        let links = db::get_item_task_links(conn, &item.uuid)?;
        item.linked_tasks = links
            .into_iter()
            .map(|(t, src)| {
                (
                    t.id.map(|i| i.to_string()).unwrap_or_default(),
                    t.description,
                    src,
                )
            })
            .collect();
    }

    Ok(item)
}

/// A short title for display, taken from the start of the memory text.
fn summarize(text: &str) -> String {
    const MAX: usize = 80;
    let trimmed = text.trim();
    if trimmed.chars().count() <= MAX {
        trimmed.to_string()
    } else {
        let truncated: String = trimmed.chars().take(MAX).collect();
        format!("{}…", truncated.trim_end())
    }
}

#[cfg(test)]
mod tests {
    use crate::infrastructure::safety;

    #[test]
    fn size_check_passes_under_threshold() {
        assert!(safety::check_size("short text").is_ok());
        assert!(safety::check_size(&"x".repeat(safety::SIZE_LIMIT_CHARS)).is_ok());
    }

    #[test]
    fn size_check_fails_over_threshold() {
        let long = "x".repeat(safety::SIZE_LIMIT_CHARS + 1);
        assert!(safety::check_size(&long).is_err());
    }

    #[test]
    fn secret_check_flags_api_key_assignment() {
        assert!(safety::detect_secret("api_key=abc123secret").is_some());
        assert!(safety::detect_secret("apikey: supersecret").is_some());
        assert!(safety::detect_secret("client_secret: my-secret-value").is_some());
    }

    #[test]
    fn secret_check_does_not_flag_normal_prose() {
        assert!(safety::detect_secret("sara recall uses item_tags for exact tag lookup").is_none());
        assert!(safety::detect_secret("the token field is optional").is_none());
        assert!(safety::detect_secret("AuthAppUri (required string)").is_none());
    }

    #[test]
    fn secret_check_flags_aws_style_key() {
        assert!(safety::detect_secret("key: AKIAIOSFODNN7EXAMPLE").is_some());
    }

    #[test]
    fn secret_check_flags_high_entropy_hex_token() {
        assert!(
            safety::detect_secret(
                "token: a3f1b2c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2"
            )
            .is_some()
        );
    }

    #[test]
    fn secret_check_does_not_flag_uuid_in_prose() {
        assert!(
            safety::detect_secret(
                "AuthAppUri is a UUID like a2923ccd-a496-4cbd-9673-f40156552a92 in the config"
            )
            .is_none()
        );
    }

    #[test]
    fn learn_task_prefers_display_id_over_uuid_collision() {
        use crate::infrastructure::{config::Config, db, model::Task};
        use uuid::Uuid;

        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        // Task A: the intended target. Its UUID deliberately does NOT start with "1".
        let mut task_a = Task::new("intended target".into(), "proj".into());
        task_a.uuid = Uuid::parse_str("aaaaaaaa-0000-0000-0000-000000000001").unwrap();
        db::insert_task(&conn, &mut task_a).unwrap();
        let a_id = task_a.id.unwrap(); // display id 1

        // Task B: an unrelated task whose UUID starts with the same digit as A's
        // display id. A raw uuid-prefix lookup on "1" would wrongly match this.
        let mut task_b = Task::new("unrelated task".into(), "proj".into());
        task_b.uuid = Uuid::parse_str("1bbbbbbb-0000-0000-0000-000000000002").unwrap();
        db::insert_task(&conn, &mut task_b).unwrap();

        // Learn a memory with --task <A's display id>.
        let v = super::learn_value(
            &conn,
            &cfg,
            "finding tied to task A",
            &["tag-r".to_string()],
            &[],
            &[a_id.to_string()],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();

        // The memory must be linked to task A (by display id), never task B.
        let item = db::get_item_by_handle(&conn, v["label"].as_str().unwrap()).unwrap();
        let linked = db::get_item_task_links(&conn, &item.uuid).unwrap();
        assert_eq!(linked.len(), 1, "expected exactly one task link");
        assert_eq!(
            linked[0].0.uuid, task_a.uuid,
            "memory linked to the wrong task (uuid-prefix collision instead of display id)"
        );
    }

    #[test]
    fn file_overlaps_detects_tagless_memory_sharing_a_file() {
        use crate::infrastructure::{config::Config, db};
        use std::collections::HashSet;
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();
        let path = "/repo/src/auth.rs".to_string();

        // A memory saved with a file but NO tags — the exact case the overlap
        // check used to skip via an early return, hiding the file collision.
        super::learn_value(
            &conn,
            &cfg,
            "auth finding",
            &[],
            &[],
            &[],
            std::slice::from_ref(&path),
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();

        let overlaps =
            super::file_overlaps(&conn, std::slice::from_ref(&path), &HashSet::new()).unwrap();
        assert_eq!(
            overlaps.len(),
            1,
            "a tagless memory sharing the file must still be detected"
        );
        assert_eq!(overlaps[0].0, path);
    }

    #[test]
    fn learn_value_supersedes_inserts_link() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        // Learn a first memory to supersede.
        let old = super::learn_value(
            &conn,
            &cfg,
            "old finding",
            &["tag-a".to_string()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();
        let old_label = old["label"].as_str().unwrap().to_string();

        // Learn a new memory that supersedes the old one.
        let new_v = super::learn_value(
            &conn,
            &cfg,
            "new finding supersedes old",
            &["tag-a".to_string()],
            &[],
            &[],
            &[],
            false,
            true,
            std::slice::from_ref(&old_label),
            &[],
            &[],
        )
        .unwrap();

        // The superseded array should contain the old label.
        let superseded = new_v["superseded"].as_array().unwrap();
        assert_eq!(superseded.len(), 1);
        assert_eq!(superseded[0].as_str().unwrap(), old_label);
    }

    #[test]
    fn learn_value_supersedes_a_canonical_does_not_orphan_or_auto_archive_children() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        // Canonical memory.
        let canonical = super::learn_value(
            &conn,
            &cfg,
            "canonical pattern",
            &["pat".to_string()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();
        let canonical_label = canonical["label"].as_str().unwrap().to_string();

        // A derived child, linked via --derived-from.
        super::learn_value(
            &conn,
            &cfg,
            "an application of the pattern",
            &[],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            std::slice::from_ref(&canonical_label),
            &[],
        )
        .unwrap();
        assert_eq!(
            super::canonical_derived_count(
                &conn,
                &db::get_item_by_handle(&conn, &canonical_label)
                    .unwrap()
                    .uuid
            ),
            1,
            "canonical must have exactly one derived child before superseding"
        );

        // Superseding the canonical must succeed and must not touch the
        // derived child's status — it stays active (never auto-archived).
        let new_v = super::learn_value(
            &conn,
            &cfg,
            "corrected canonical pattern",
            &["pat".to_string()],
            &[],
            &[],
            &[],
            false,
            true,
            std::slice::from_ref(&canonical_label),
            &[],
            &[],
        )
        .unwrap();
        assert_eq!(
            new_v["superseded"].as_array().unwrap()[0].as_str().unwrap(),
            canonical_label
        );
    }

    #[test]
    fn canonical_derived_count_zero_for_a_plain_memory() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();
        let v = super::learn_value(
            &conn,
            &cfg,
            "plain note",
            &[],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();
        let label = v["label"].as_str().unwrap();
        let uuid = db::get_item_by_handle(&conn, label).unwrap().uuid;
        assert_eq!(super::canonical_derived_count(&conn, &uuid), 0);
    }

    #[test]
    fn check_overlap_warns_on_file_overlap() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        let file_path = "/tmp/test_sara_overlap_check.rs".to_string();

        // Learn first memory tied to the file.
        super::learn_value(
            &conn,
            &cfg,
            "first memory about this file",
            &["tag-x".to_string()],
            &[],
            &[],
            std::slice::from_ref(&file_path),
            false,
            true, // force — skip safety guardrails
            &[],
            &[],
            &[],
        )
        .unwrap();

        // Learning a second memory on the same file with DIFFERENT tags should
        // still succeed (file-overlap is advisory, not blocking). The test just
        // verifies check_overlap() itself doesn't error out.
        let result = super::check_overlap(
            &conn,
            &["tag-y".to_string()],
            std::slice::from_ref(&file_path),
            None,
        );
        assert!(
            result.is_ok(),
            "check_overlap should not error on file overlap"
        );
    }

    #[test]
    fn learn_creates_typed_links() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        // A canonical memory and a lateral one to link against.
        let canon = super::learn_value(
            &conn,
            &cfg,
            "canonical pattern",
            &["pat".to_string()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();
        let canon_label = canon["label"].as_str().unwrap().to_string();

        let sibling = super::learn_value(
            &conn,
            &cfg,
            "sibling note",
            &["side".to_string()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();
        let sibling_label = sibling["label"].as_str().unwrap().to_string();

        // Learn a new memory that is derived_from the canonical AND similar_to the sibling.
        let new_v = super::learn_value(
            &conn,
            &cfg,
            "applied specialisation of the pattern",
            &["apply".to_string()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            std::slice::from_ref(&canon_label),
            std::slice::from_ref(&sibling_label),
        )
        .unwrap();

        let derived = new_v["derived_from"].as_array().unwrap();
        assert_eq!(derived.len(), 1);
        assert_eq!(derived[0].as_str().unwrap(), canon_label);

        let similar = new_v["similar_to"].as_array().unwrap();
        assert_eq!(similar.len(), 1);
        assert_eq!(similar[0].as_str().unwrap(), sibling_label);

        // The typed edges must actually exist in the graph.
        let new_uuid = db::get_item_by_handle(&conn, new_v["label"].as_str().unwrap())
            .unwrap()
            .uuid
            .to_string();
        let out = db::get_memory_links_from(&conn, &new_uuid).unwrap();
        assert!(
            out.iter().any(|l| l.relation == "derived_from"),
            "derived_from edge exists"
        );
        assert!(
            out.iter().any(|l| l.relation == "similar_to"),
            "similar_to edge exists"
        );
    }

    #[test]
    fn learn_unresolvable_link_warns_not_aborts() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        // Reference a memory label that does not exist — learn must still succeed.
        let v = super::learn_value(
            &conn,
            &cfg,
            "a memory with a dangling link",
            &["solo".to_string()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &["m9999".to_string()],
            &[],
        );
        assert!(
            v.is_ok(),
            "unresolvable --derived-from must not abort the learn"
        );
        let v = v.unwrap();
        // The link was skipped, so the reported array is empty.
        assert_eq!(v["derived_from"].as_array().unwrap().len(), 0);
    }

    #[test]
    fn overlap_suggests_typed_link() {
        // Near-duplicate → derived-from / supersedes; partial → similar-to.
        let near = super::near_dupe_suggestion("m26");
        assert!(
            near.contains("--derived-from m26"),
            "near-dupe offers --derived-from: {near}"
        );
        assert!(
            near.contains("--supersedes m26"),
            "near-dupe offers --supersedes: {near}"
        );

        let partial = super::partial_overlap_suggestion("m30");
        assert!(
            partial.contains("--similar-to m30"),
            "partial offers --similar-to: {partial}"
        );
        // No longer the vague untyped "possible contradiction" wording.
        assert!(!partial.to_lowercase().contains("possible contradiction"));
    }

    #[test]
    fn canonical_hint_names_derived_from_and_relearn() {
        let hint = super::canonical_hint("m7", 3);
        assert!(hint.contains("m7"));
        assert!(hint.contains("3 derived applications"));
        assert!(hint.contains("--derived-from m7"));
        assert!(hint.contains("sara relearn m7"));
    }

    #[test]
    fn check_overlap_detects_canonical_via_derived_from_link() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        // Canonical memory with two tags.
        let canonical = super::learn_value(
            &conn,
            &cfg,
            "CodeQL config pattern",
            &["codeql".into(), "config".into()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();
        let canonical_label = canonical["label"].as_str().unwrap().to_string();
        let canonical_uuid = db::get_item_by_handle(&conn, &canonical_label)
            .unwrap()
            .uuid;

        // Before any derived child exists, it isn't canonical yet.
        assert_eq!(super::canonical_derived_count(&conn, &canonical_uuid), 0);

        // A derived application, linked via --derived-from.
        super::learn_value(
            &conn,
            &cfg,
            "applied CodeQL config to repo X",
            &["codeql".into()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            std::slice::from_ref(&canonical_label),
            &[],
        )
        .unwrap();

        // Now the canonical has one derived child — the detection
        // `check_overlap` relies on to upgrade a partial-tag match into the
        // near-dupe band with a `--derived-from`/`relearn` hint.
        assert_eq!(super::canonical_derived_count(&conn, &canonical_uuid), 1);

        // A new memory sharing only one of the canonical's two tags (partial
        // overlap) must not error when check_overlap runs against it.
        super::check_overlap(&conn, &["codeql".into()], &[], None).unwrap();
    }

    #[test]
    fn learn_auto_attaches_new_instance_to_canonical_pattern() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        // Canonical pattern memory with two tags.
        let canonical = super::learn_value(
            &conn,
            &cfg,
            "CANONICAL nsubstitute dependabot restore fix",
            &["nsubstitute".into(), "ci".into()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();
        let canon_label = canonical["label"].as_str().unwrap().to_string();
        let canon_uuid = db::get_item_by_handle(&conn, &canon_label).unwrap().uuid;

        // One explicit derived application turns it into a canonical.
        super::learn_value(
            &conn,
            &cfg,
            "applied the nsubstitute pin to repo A",
            &["nsubstitute".into()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            std::slice::from_ref(&canon_label),
            &[],
        )
        .unwrap();
        assert_eq!(super::canonical_derived_count(&conn, &canon_uuid), 1);

        // A NEW instance sharing all the canonical's tags, learned WITHOUT an
        // explicit --derived-from (force=false so overlap detection runs): it
        // must auto-attach to the canonical pattern.
        let new_v = super::learn_value(
            &conn,
            &cfg,
            "applied the nsubstitute pin to repo B under warnings-as-errors",
            &["nsubstitute".into(), "ci".into()],
            &[],
            &[],
            &[],
            false,
            false,
            &[],
            &[],
            &[],
        )
        .unwrap();

        let auto: Vec<String> = new_v["auto_derived_from"]
            .as_array()
            .unwrap()
            .iter()
            .map(|x| x.as_str().unwrap().to_string())
            .collect();
        assert!(
            auto.contains(&canon_label),
            "new instance must auto-attach to canonical, got {auto:?}"
        );
        assert_eq!(
            super::canonical_derived_count(&conn, &canon_uuid),
            2,
            "canonical should now count the auto-attached instance"
        );
    }

    #[test]
    fn learn_auto_attach_does_not_duplicate_explicit_derived_from() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        let canonical = super::learn_value(
            &conn,
            &cfg,
            "CANONICAL pattern",
            &["p".into()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();
        let canon_label = canonical["label"].as_str().unwrap().to_string();
        let canon_uuid = db::get_item_by_handle(&conn, &canon_label).unwrap().uuid;
        super::learn_value(
            &conn,
            &cfg,
            "seed application",
            &["p".into()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            std::slice::from_ref(&canon_label),
            &[],
        )
        .unwrap();

        // Author explicitly names the canonical: the explicit link wins, and the
        // auto-attach must NOT create a second, duplicate derived_from edge.
        let new_v = super::learn_value(
            &conn,
            &cfg,
            "another application, explicitly linked",
            &["p".into()],
            &[],
            &[],
            &[],
            false,
            false,
            &[],
            std::slice::from_ref(&canon_label),
            &[],
        )
        .unwrap();
        let explicit: Vec<String> = new_v["derived_from"]
            .as_array()
            .unwrap()
            .iter()
            .map(|x| x.as_str().unwrap().to_string())
            .collect();
        assert!(explicit.contains(&canon_label));
        assert!(
            new_v["auto_derived_from"].as_array().unwrap().is_empty(),
            "explicit derived_from must suppress the auto-link for the same canonical"
        );
        // Exactly two applications (seed + this one), not three.
        assert_eq!(super::canonical_derived_count(&conn, &canon_uuid), 2);
    }

    #[test]
    fn learn_does_not_auto_attach_to_a_plain_non_canonical_overlap() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        let cfg = Config::default();

        // A plain memory (no derived children) — not a pattern.
        super::learn_value(
            &conn,
            &cfg,
            "a plain finding",
            &["solo".into()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();

        // A near-duplicate by tag, but the overlap target is not canonical.
        let new_v = super::learn_value(
            &conn,
            &cfg,
            "another finding on the same topic",
            &["solo".into()],
            &[],
            &[],
            &[],
            false,
            false,
            &[],
            &[],
            &[],
        )
        .unwrap();
        assert!(
            new_v["auto_derived_from"].as_array().unwrap().is_empty(),
            "auto-attach must fire only for canonical patterns, not plain overlaps"
        );
    }

    #[test]
    fn learn_always_embeds_even_with_default_config() {
        use crate::infrastructure::{config::Config, db};
        let conn = db::open_in_memory_for_test();
        // The default config leaves the legacy `recall.semantic` toggle OFF.
        // Semantic *querying* is always enabled (SemanticOpts::from_cfg), so the
        // write side must ALWAYS embed too — otherwise recall silently degrades
        // to keyword-only in every project that never flipped the deprecated flag.
        let cfg = Config::default();
        assert!(
            !cfg.recall.semantic,
            "guard: default config keeps the deprecated toggle off"
        );

        let v = super::learn_value(
            &conn,
            &cfg,
            "dependabot bump broke the restore step; pin the lockfile version",
            &["ci".to_string()],
            &[],
            &[],
            &[],
            false,
            true,
            &[],
            &[],
            &[],
        )
        .unwrap();

        let item = db::get_item_by_handle(&conn, v["label"].as_str().unwrap()).unwrap();
        let stored = db::get_embedding(&conn, &item.uuid.to_string()).unwrap();
        assert!(
            stored.is_some(),
            "every learned memory must be embedded so semantic recall always \
             works, regardless of the deprecated recall.semantic toggle"
        );
    }
}