trusty-memory 0.28.1

MCP server (stdio + Unix socket) for trusty-memory
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
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
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
//! Deterministic KG triple extraction from drawer content.
//!
//! Why: Issue #97 — `memory_remember` should populate the knowledge graph
//! automatically so palaces with drawers always have a non-empty KG. Calling an
//! LLM on every write would blow up latency and require network access; a
//! deterministic heuristic stays fast and offline while still producing useful
//! triples for tag membership, key-phrase mentions, and obvious is-a / has-a /
//! works-at patterns. The visual graph view (the other half of #97) renders
//! whatever shows up here, so this pass is the data source for "every palace
//! has a graph".
//! What: A pure function `extract_triples` that takes drawer content + tags +
//! drawer id and returns a `Vec<Triple>` with `provenance = "auto:remember"`.
//! The current heuristics are tag→drawer, room→drawer, hashtag→drawer, and a
//! short pattern table (`X is a Y`, `X works at Y`, `X uses Y`, `X depends on
//! Y`). Drawer ids are encoded as `drawer:<uuid>` so the subject keeps a
//! stable, palace-unique identity that the graph view can dereference back
//! to the source drawer.
//! Test: `extract_triples_emits_tag_triples`,
//! `extract_triples_emits_hashtag_mentions`,
//! `extract_triples_extracts_is_a_pattern`,
//! `extract_triples_never_panics_on_empty_input`.

use crate::wordnet_pos::{self, WordNetPos};
use chrono::Utc;
use std::collections::HashSet;
use trusty_common::memory_core::store::kg::Triple;
use uuid::Uuid;

/// Default tags that cause a drawer to be skipped during auto-extraction.
///
/// Why: Drawers tagged with these labels are by definition non-factual project
/// knowledge (test fixtures, QA scaffolding, synthetic content) and should not
/// pollute the KG with noise triples.
/// What: A static slice of lowercase tag strings; matched case-insensitively
/// during extraction.
/// Test: `extract_triples_skips_denied_tags`.
pub const DEFAULT_DENY_TAGS: &[&str] = &["cross-project-qa", "test", "fixture"];

/// Configuration for a single extraction pass.
///
/// Why: Bundles per-run configuration so `extract_triples` can be called with
/// different deny-lists (e.g. the default prod list vs. an empty list in
/// integration tests) without changing the function signature. #5399 added the
/// POS table here rather than to a process-wide `OnceLock`, which CLAUDE.md
/// forbids: the table is a 16-byte `Copy` handle over `&'static str` with no
/// build step, so passing it costs nothing and every caller can substitute a
/// different one.
/// What: Contains a `deny_tags` slice and a [`WordNetPos`] lookup. The
/// extractor skips any drawer whose tags intersect the deny set, and consults
/// the lookup when deciding which token in a noun phrase is its head.
/// `#[non_exhaustive]` so the next field is not another breaking change.
/// Test: `extract_triples_skips_denied_tags`, `extract_triples_empty_deny_list`,
/// `a_caller_supplied_pos_table_is_the_one_consulted`.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct KgExtractConfig<'a> {
    /// Tags that cause extraction to be skipped entirely. Compared
    /// case-insensitively against the drawer's tag list.
    pub deny_tags: &'a [&'a str],
    /// Part-of-speech membership used to find the head of a noun phrase.
    pub pos: WordNetPos,
}

impl Default for KgExtractConfig<'_> {
    fn default() -> Self {
        Self {
            deny_tags: DEFAULT_DENY_TAGS,
            pos: WordNetPos::shipped(),
        }
    }
}

/// Provenance tag stamped on every auto-extracted triple.
///
/// Why: Operators need a stable string to filter / retract the auto-extracted
/// subset without scanning content. Centralising the constant keeps every
/// emitter and the back-fill CLI in sync.
/// What: A `&'static str` containing the literal `auto:remember`.
/// Test: `extract_triples_stamps_provenance`.
pub const AUTO_PROVENANCE: &str = "auto:remember";

/// Confidence applied to auto-extracted triples.
///
/// Why: Heuristic extraction is not authoritative; downstream rankers can use
/// the confidence to prefer explicit `kg_assert` triples over auto-extracted
/// noise.
/// What: `0.6` — high enough to surface in queries, low enough to be
/// over-ridden by a manual `kg_assert` of the same `(subject, predicate)`.
/// Test: `extract_triples_uses_reduced_confidence`.
pub const AUTO_CONFIDENCE: f32 = 0.6;

/// Subject prefix used for drawer-identity triples.
///
/// Why: A stable, palace-unique identifier lets the graph view dereference a
/// node back to the source drawer (and the back-fill CLI dedupe by drawer).
/// What: `drawer:` — concatenated with the drawer UUID hyphenated form.
/// Test: every test in this module asserts the prefix.
pub const DRAWER_SUBJECT_PREFIX: &str = "drawer:";

/// Subject prefix used for tag entities.
///
/// Why: The KG enforces at most one active triple per `(subject, predicate)`,
/// so we can't emit `drawer:X has-tag t1; drawer:X has-tag t2` — the second
/// assert would close the first. By promoting each tag to its own subject
/// (`tag:t1`, `tag:t2`) we keep multiple tags as distinct edges and the graph
/// view gets natural tag-clusters around each drawer.
/// What: `tag:` — concatenated with the lower-cased tag string.
/// Test: `extract_triples_emits_tag_triples`.
pub const TAG_SUBJECT_PREFIX: &str = "tag:";

/// Subject prefix used for free-text mention entities.
///
/// Why: Same temporal-invariant reasoning as `TAG_SUBJECT_PREFIX`. Hashtag
/// mentions and other discovered topical terms become their own subjects so
/// multiple mentions per drawer survive the assert pipeline.
/// What: `topic:` — concatenated with the lower-cased term.
/// Test: `extract_triples_emits_hashtag_mentions`.
pub const TOPIC_SUBJECT_PREFIX: &str = "topic:";

/// Subject prefix used for room entities.
///
/// Why: A drawer can only sit in one room, but encoding the room as its own
/// subject keeps the graph topology consistent (all "discovered metadata"
/// entities live under prefixed namespaces) and lets multiple drawers from
/// the same room cluster around a shared room node.
/// What: `room:` — concatenated with the room label.
/// Test: `extract_triples_emits_tag_triples`.
pub const ROOM_SUBJECT_PREFIX: &str = "room:";

/// Build the drawer subject string used as the (s) for every per-drawer
/// triple emitted by this module.
///
/// Why: Centralises the `drawer:<uuid>` encoding so call sites cannot drift.
/// What: Returns `format!("{DRAWER_SUBJECT_PREFIX}{id}")`.
/// Test: covered by every extractor test.
pub fn drawer_subject(id: Uuid) -> String {
    format!("{DRAWER_SUBJECT_PREFIX}{id}")
}

/// Inputs to a single extraction pass.
///
/// Why: Bundling the inputs keeps `extract_triples` signature small and lets
/// us add new fields (e.g. drawer_type) without breaking call sites.
/// What: Plain data struct; all fields are borrowed so the caller keeps
/// ownership.
/// Test: indirectly via every test that constructs one.
#[derive(Debug, Clone)]
pub struct ExtractInput<'a> {
    pub drawer_id: Uuid,
    pub content: &'a str,
    pub tags: &'a [String],
    pub room: Option<&'a str>,
}

/// Run the deterministic heuristic extractor with default config.
///
/// Why: Convenience wrapper that uses [`KgExtractConfig::default`] (the
/// production deny-list) so call sites that do not need a custom config
/// remain unchanged.
/// What: Delegates to [`extract_triples_with_config`] with a default config.
/// Test: All existing tests call this helper and implicitly exercise the default
/// deny-list path.
pub fn extract_triples(input: &ExtractInput<'_>) -> Vec<Triple> {
    extract_triples_with_config(input, &KgExtractConfig::default())
}

/// Run the deterministic heuristic extractor.
///
/// Why: Single entry point so `memory_remember`, `memory_note`, and the
/// back-fill CLI all share the same logic. Pure function — no I/O, no async —
/// so it can be unit-tested cheaply. Accepts a [`KgExtractConfig`] so callers
/// can override the deny-list without touching the function signature.
/// What: First checks whether any of the drawer's tags appear in
/// `config.deny_tags` (case-insensitive); when a match is found the function
/// returns immediately with an empty vec and logs a debug message. Otherwise
/// walks `tags`, content tokens, and a small pattern list to emit `Triple`s;
/// deduplicates so the same `(subject, predicate, object)` never appears twice
/// in a single pass.
/// Test: `extract_triples_skips_denied_tags`, `extract_triples_emits_tag_triples`,
/// plus all other tests in this file.
pub fn extract_triples_with_config(
    input: &ExtractInput<'_>,
    config: &KgExtractConfig<'_>,
) -> Vec<Triple> {
    // Deny-list check: if any tag on this drawer is in the deny set, skip
    // extraction entirely. The check is case-insensitive to tolerate mixed-
    // case tags from different clients.
    let denied = input.tags.iter().any(|t| {
        let lower = t.trim().to_lowercase();
        config.deny_tags.contains(&lower.as_str())
    });
    if denied {
        tracing::debug!(
            drawer_id = %input.drawer_id,
            tags = ?input.tags,
            "kg_extract: skipping drawer — tag matches deny-list"
        );
        return Vec::new();
    }
    let now = Utc::now();
    let subject = drawer_subject(input.drawer_id);
    let mut out: Vec<Triple> = Vec::new();
    let mut seen: HashSet<(String, String, String)> = HashSet::new();

    let push = |out: &mut Vec<Triple>,
                seen: &mut HashSet<(String, String, String)>,
                s: String,
                p: String,
                o: String| {
        let key = (s.clone(), p.clone(), o.clone());
        if seen.insert(key) {
            out.push(Triple {
                subject: s,
                predicate: p,
                object: o,
                valid_from: now,
                valid_to: None,
                confidence: AUTO_CONFIDENCE,
                provenance: Some(AUTO_PROVENANCE.to_string()),
            });
        }
    };

    // Tag membership — each tag becomes its own subject so multiple tags on
    // the same drawer don't collide under the "one active triple per
    // (s, p)" invariant. Edge direction is `tag:<t> tags drawer:<id>` so the
    // graph clusters drawers under their shared tag nodes.
    for tag in input.tags {
        let clean = tag.trim();
        if clean.is_empty() {
            continue;
        }
        push(
            &mut out,
            &mut seen,
            format!("{TAG_SUBJECT_PREFIX}{}", clean.to_lowercase()),
            "tags".to_string(),
            subject.clone(),
        );
    }

    // Room membership — `room:<r> contains drawer:<id>` for the same reason
    // (multiple drawers per room must coexist).
    if let Some(room) = input.room {
        let clean = room.trim();
        if !clean.is_empty() {
            push(
                &mut out,
                &mut seen,
                format!("{ROOM_SUBJECT_PREFIX}{clean}"),
                "contains".to_string(),
                subject.clone(),
            );
        }
    }

    // Hashtag-style mentions — `topic:<term> mentioned-in drawer:<id>` so
    // multiple terms per drawer can coexist as distinct active edges.
    for term in extract_hashtags(input.content) {
        push(
            &mut out,
            &mut seen,
            format!("{TOPIC_SUBJECT_PREFIX}{term}"),
            "mentioned-in".to_string(),
            subject.clone(),
        );
    }

    // Simple natural-language patterns. Each yields a free-form
    // `<subject> <predicate> <object>` triple anchored to entities found in
    // the content (not the drawer subject), so the graph develops topical
    // edges over time.
    for (s, p, o) in extract_patterns(input.content, &config.pos) {
        push(&mut out, &mut seen, s, p, o);
    }

    out
}

/// Pull `#hashtag`-style tokens out of free-form content.
///
/// Why: Hashtags are a cheap, intentional signal — when a user writes `#rust`
/// or `#design-doc` we should record the mention so the graph picks it up.
/// What: Walks the string, captures runs of `[a-zA-Z0-9_-]` following a `#`,
/// lower-cases and deduplicates. Skips empty captures (a lone `#`).
/// Test: `extract_triples_emits_hashtag_mentions`.
fn extract_hashtags(content: &str) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    let mut seen: HashSet<String> = HashSet::new();
    let mut iter = content.char_indices().peekable();
    while let Some((_, c)) = iter.next() {
        if c != '#' {
            continue;
        }
        let mut term = String::new();
        while let Some(&(_, nc)) = iter.peek() {
            if nc.is_ascii_alphanumeric() || nc == '_' || nc == '-' {
                term.push(nc.to_ascii_lowercase());
                iter.next();
            } else {
                break;
            }
        }
        if term.is_empty() {
            continue;
        }
        if seen.insert(term.clone()) {
            out.push(term);
        }
    }
    out
}

/// Pattern dictionary used by `extract_patterns`.
///
/// Why: A small, predictable set of (predicate, marker phrases) keeps the
/// extractor explicable and deterministic. Each entry maps a predicate to one
/// or more space-padded marker phrases; when the marker appears in the lower-
/// cased content we split on it and read the entity tokens immediately to
/// each side.
/// What: A static slice of `(predicate, &[marker, ...])`. Markers must be
/// lower-case and surrounded by whatever whitespace the input has — we add
/// the padding ourselves.
/// Test: `extract_triples_extracts_is_a_pattern`.
const PATTERN_TABLE: &[(&str, &[&str])] = &[
    ("is-a", &[" is a ", " is an "]),
    ("works-at", &[" works at "]),
    ("uses", &[" uses ", " using "]),
    ("depends-on", &[" depends on ", " requires "]),
];

/// Function words that can never be a KG entity.
///
/// Why: #4678 — a [`PATTERN_TABLE`] marker hit says a marker phrase appeared,
/// not that the tokens either side of it name anything. "calling them is a
/// no-op" put `them --is-a--> no-op` into the live palace. Grouped by part of
/// speech because the boundary is grammatical, not statistical: these are the
/// closed word classes English never coins new members of, so the list is
/// finite and stable rather than a frequency cut-off that needs re-tuning.
/// What: a flat lower-case slice, matched exactly against a normalised token
/// by [`is_stop_token`]. Membership is checked with a linear scan — it runs at
/// most twice per pattern hit and at most four hits exist per drawer.
/// Test: `stopwords_are_unique`, `is_stop_token_rejects_every_stopword`.
const STOPWORDS: &[&str] = &[
    // Articles and determiners.
    "a",
    "an",
    "the",
    "this",
    "that",
    "these",
    "those",
    "some",
    "any",
    "each",
    "every",
    "all",
    "both",
    "either",
    "neither",
    "another",
    "such",
    "same",
    "no",
    "none",
    // Pronouns.
    "i",
    "me",
    "my",
    "mine",
    "myself",
    "we",
    "us",
    "our",
    "ours",
    "ourselves",
    "you",
    "your",
    "yours",
    "yourself",
    "he",
    "him",
    "his",
    "himself",
    "she",
    "her",
    "hers",
    "herself",
    "it",
    "its",
    "itself",
    "they",
    "them",
    "their",
    "theirs",
    "themselves",
    "who",
    "whom",
    "whose",
    "which",
    "what",
    "one",
    "ones",
    "someone",
    "something",
    "anyone",
    "anything",
    "everyone",
    "everything",
    "nobody",
    "nothing",
    "others",
    // Prepositions.
    "of",
    "in",
    "on",
    "at",
    "to",
    "for",
    "with",
    "by",
    "from",
    "about",
    "into",
    "onto",
    "over",
    "under",
    "below",
    "above",
    "between",
    "among",
    "through",
    "during",
    "before",
    "after",
    "across",
    "against",
    "within",
    "without",
    "upon",
    "per",
    "via",
    "than",
    "toward",
    "towards",
    "off",
    "out",
    "up",
    "down",
    "near",
    "around",
    "behind",
    "beyond",
    "beside",
    "along",
    "past",
    "throughout",
    // #5399: WordNet lists these two as nouns ("the inside of the box"), so
    // without the closed-class list they pass the POS check and continue a
    // noun phrase they actually close.
    "inside",
    "outside",
    // Conjunctions and subordinators.
    "and",
    "or",
    "but",
    "nor",
    "so",
    "yet",
    "if",
    "then",
    "else",
    "because",
    "while",
    "when",
    "where",
    "whether",
    "though",
    "although",
    "since",
    "as",
    "unless",
    "until",
    "whereas",
    // Copulas, auxiliaries and modals.
    "is",
    "are",
    "was",
    "were",
    "be",
    "been",
    "being",
    "am",
    "do",
    "does",
    "did",
    "done",
    "doing",
    "has",
    "have",
    "had",
    "having",
    "can",
    "could",
    "will",
    "would",
    "shall",
    "should",
    "may",
    "might",
    "must",
    "ought",
    "need",
    "needs",
    "let",
    "lets",
    "get",
    "gets",
    "got",
    "gotten",
    // Degree, negation and discourse adverbs — closed-class fillers that sit
    // next to a marker often and name nothing.
    "not",
    "only",
    "just",
    "also",
    "very",
    "too",
    "still",
    "already",
    "always",
    "never",
    "often",
    "again",
    "here",
    "there",
    "now",
    "actually",
    "really",
    "simply",
    "merely",
    "quite",
    "rather",
    "even",
    "ever",
    "once",
    "yes",
    "well",
    "much",
    "many",
    "more",
    "most",
    "less",
    "least",
    "few",
    "several",
    "enough",
    "almost",
    "perhaps",
    "maybe",
    "instead",
    "otherwise",
    "hence",
    "therefore",
    "however",
    "thus",
    "moreover",
];

/// Minimum character length for a token to be accepted as an entity.
///
/// Why: one- and two-character tokens are overwhelmingly punctuation debris,
/// initials, or list markers rather than entities.
/// What: `3`. Tokens shorter than this are rejected unless they appear in
/// [`SHORT_ENTITY_ALLOWLIST`].
/// Test: `extract_triples_rejects_short_token_off_allowlist`.
pub const MIN_ENTITY_TOKEN_LEN: usize = 3;

/// Short tokens that are real entities in this workspace's subject matter.
///
/// Why: [`MIN_ENTITY_TOKEN_LEN`] would otherwise reject the languages, crate
/// aliases, and infrastructure abbreviations these palaces actually discuss —
/// a precision filter that silently drops `Go` and `C` costs recall for
/// nothing. The crate aliases (`tm`, `ts`, `tc`, `ta`) come from this repo's
/// own abbreviation table.
/// What: lower-case tokens of fewer than [`MIN_ENTITY_TOKEN_LEN`] characters
/// that bypass the length floor. The stopword check still applies first, so an
/// entry here cannot resurrect a function word.
/// Test: `extract_triples_keeps_allowlisted_go`,
/// `extract_triples_keeps_allowlisted_c`,
/// `short_entity_allowlist_entries_survive_the_length_floor`.
pub const SHORT_ENTITY_ALLOWLIST: &[&str] = &[
    // Languages and language-shaped tokens.
    "go", "c", "c#", "js", "ts", "py", "ml", // Domains and infrastructure.
    "ai", "kg", "db", "ui", "os", "io", "vm", "ip", // Process and workspace aliases.
    "pr", "ci", "qa", "pm", "tm", "tc", "ta",
];

/// Punctuation stripped from a token's edges before it is classified.
///
/// Why: `last_token` / `first_token` only strip a trailing run, so a token can
/// still arrive as `("the` or `` `redb` ``. Comparing that against
/// [`STOPWORDS`] would miss, and the filter would leak exactly the tokens it
/// exists to catch.
/// What: the edge characters [`is_stop_token`] trims before matching. Interior
/// characters are untouched, so `no-op` and `c#` survive intact.
/// Test: `is_stop_token_normalises_surrounding_punctuation`.
const TOKEN_EDGE_PUNCT: &[char] = &[
    '(', ')', '[', ']', '{', '}', '<', '>', '"', '\'', '`', ',', '.', ';', ':', '!', '?', '*', '_',
    '-', '/', '\\', '|', '—', '–', '…', '“', '”', '‘', '’',
];

/// Whether `tok` must be refused as a KG entity.
///
/// Why: #4678 — the pattern pass treated any whitespace-delimited token beside
/// a marker as an entity, which is how `them --is-a--> no-op`,
/// `exhaustiveness --is-a--> hard`, and `squash --is-a--> ancestor` reached the
/// live graph. This is the single gate both the extractor and the
/// `--purge-stale-subjects` back-fill consult, so forward extraction and
/// historical clean-up can never disagree about what counts as garbage.
/// What: normalises `tok` through [`clean_token`], lower-cases it, and rejects
/// it when the result is empty, appears in [`STOPWORDS`], or is shorter than
/// [`MIN_ENTITY_TOKEN_LEN`] without being in [`SHORT_ENTITY_ALLOWLIST`].
/// Length is counted in `char`s, not bytes, so a multi-byte token is not
/// mis-measured. Purely lexical: it judges the token alone and knows nothing of
/// the sentence, so it cannot catch a triple whose tokens are both ordinary
/// words (see #4678 for the residue).
///
/// The normalisation step is NOT redundant with `clean_token`'s use in
/// `first_token` / `last_token`. Those clean tokens on the way IN, which only
/// helps content extracted from now on. The `--purge-stale-subjects` path calls
/// this on subjects read back out of redb, and those were written by the old
/// extractor with the punctuation already welded on — normalising here is what
/// lets a stored `("the` be recognised as the stopword it is.
/// Test: `is_stop_token_rejects_every_stopword`,
/// `is_stop_token_accepts_ordinary_entities`,
/// `is_stop_token_normalises_surrounding_punctuation`,
/// `purge_selects_a_legacy_subject_with_welded_punctuation`.
pub fn is_stop_token(tok: &str) -> bool {
    let norm = clean_token(tok).to_lowercase();
    if norm.is_empty() {
        return true;
    }
    if STOPWORDS.contains(&norm.as_str()) {
        return true;
    }
    norm.chars().count() < MIN_ENTITY_TOKEN_LEN && !SHORT_ENTITY_ALLOWLIST.contains(&norm.as_str())
}

/// The cleaned spelling a stored entity term must be merged onto, or `None`
/// when the term is already canonical.
///
/// Why: #4678's edge trim fixed extraction going forward and split every
/// pre-fix entity in two — a palace holds both `` `redb` `` and `redb`, and
/// each `kg-rebuild` over the same drawer content widens the gap (#5401).
/// Deciding the canonical spelling next to the filter that produced the split
/// is what stops the merge pass from inventing a second normalisation that can
/// drift away from the extractor's.
/// What: `Some(cleaned)` when [`clean_token`] changes `term`; `None` when it
/// does not, or when [`is_stop_token`] rejects the term. That second arm is
/// what keeps this disjoint from `--purge-stale-subjects`: a punctuated
/// stopword such as `("the` is garbage the purge deletes, never a twin some
/// real node should absorb.
///
/// The merge therefore inherits [`clean_token`]'s normalisation whole,
/// dotfiles included: `.env` names `env`, `--verbose` names `verbose`, and the
/// leading punctuation that IS the identity is collapsed with the decorative
/// kind. That is the point of reusing `clean_token` — the extractor already
/// emits `env` for content saying `.env`, so the merge aligns stored nodes
/// with current extraction instead of preserving a spelling nothing produces
/// any more. `close_active_row` keeps the punctuated row under its `hist:`
/// key, so the original spelling is not lost.
/// Test: `canonical_entity_names_the_cleaned_twin`.
pub fn canonical_entity(term: &str) -> Option<&str> {
    // #5401: the merge pass and the extractor must agree on one spelling.
    if is_stop_token(term) {
        return None;
    }
    let cleaned = clean_token(term);
    (cleaned != term).then_some(cleaned)
}

/// Apply the pattern table to a single content blob.
///
/// Why: Keeps the matching loop out of `extract_triples` so the dispatcher
/// stays readable.
/// What: For every `(predicate, markers)` row, scan every marker against the
/// lower-cased content; on the first hit emit `(left_token, predicate,
/// right_token)` and move on to the next predicate. Only the first hit per
/// predicate is taken to avoid combinatorial output on long texts. A hit whose
/// subject or object fails [`is_stop_token`] emits nothing and still consumes
/// the predicate's turn, so one rejected hit never cascades into a scan for a
/// second, lower-quality match later in the blob.
/// Test: `extract_triples_extracts_is_a_pattern`,
/// `extract_triples_rejects_pronoun_subject`,
/// `extract_triples_rejects_stopword_object`.
fn extract_patterns(content: &str, pos: &WordNetPos) -> Vec<(String, String, String)> {
    let lower = content.to_lowercase();
    let mut out: Vec<(String, String, String)> = Vec::new();
    for (predicate, markers) in PATTERN_TABLE {
        for marker in *markers {
            if let Some(idx) = lower.find(marker) {
                // #5399: production hands this whole multi-line drawer bodies
                // (`auto_extract_and_assert`, `kg_rebuild`), so an unbounded
                // walk joins two unrelated sentences across a line break. A
                // newline closes the phrase exactly as a period does.
                let line_start = lower[..idx].rfind('\n').map_or(0, |p| p + 1);
                let left = lower[line_start..idx].trim();
                let right_start = idx + marker.len();
                let line_end = lower[right_start..]
                    .find('\n')
                    .map_or(lower.len(), |p| right_start + p);
                let right = lower[right_start..line_end].trim();
                // #4678: a marker hit is not evidence of two entities — reject
                // the whole triple when either side is a function word or too
                // short, never half of it. #5399: and take the HEAD of each
                // noun phrase rather than the token nearest the marker.
                if let (Some(subject_tok), Some(object_tok)) =
                    (select_subject(left, pos), select_object(right, pos))
                {
                    out.push((subject_tok, (*predicate).to_string(), object_tok));
                }
                break;
            }
        }
    }
    out
}

/// Prepositions that continue a noun phrase rather than closing it.
///
/// Why: #5399 — `ancestor of origin/main` and `member of the process group`
/// are single noun phrases, so grabbing `ancestor` or `member` alone truncates
/// a relation into a bogus type. The stopping token is what reveals it. The set
/// is deliberately just `of`: it is the genitive linker and is almost always
/// NP-internal, whereas every other preposition attaches ambiguously —
/// `trusty-memory uses redb for persistence` has `for` closing the object and
/// opening a purpose adjunct on the verb, and that triple must survive.
/// What: matched against the cleaned token immediately after the extracted
/// noun-phrase run; a hit rejects the whole triple.
/// Test: `row2_rejects_ancestor_truncated_before_of`,
/// `row6_rejects_member_of_the_process_group`,
/// `row5_keeps_uses_object_before_a_non_genitive_preposition`.
const NP_CONTINUING_PREPOSITIONS: &[&str] = &["of"];

/// Characters that close a noun phrase when welded to a token's trailing edge.
///
/// Why: `is a parser, and ...` must not walk the run into the next clause.
/// What: consulted on the RAW token, through [`ends_noun_phrase`], before
/// [`clean_token`] strips it.
/// Test: `stops_the_noun_phrase_run_at_a_comma`,
/// `stops_the_run_at_a_terminator_behind_markdown_emphasis`.
const NP_TERMINATING_PUNCT: &[char] = &['.', ',', ';', ':', '!', '?', ')'];

/// Longest noun-phrase run either walk will consider.
///
/// Why: a bound keeps a pathological line (a long unpunctuated list of unknown
/// tokens) from letting the head drift far from the marker. Four covers
/// `[adj] [noun] [noun]` plus one, which is past the length of any real
/// technical compound in drawer prose.
/// Test: `caps_the_noun_phrase_run`.
const NP_RUN_MAX: usize = 4;

/// Whether a raw token's trailing punctuation closes the noun phrase.
///
/// Why: #5399 — the check used to read only the token's LAST character, so
/// `**MCP is a thin proxy.**` (raw token `proxy.**`) ended in `*`, missed the
/// `.`, and let the run walk into the next clause. Markdown emphasis is the
/// normal shape of drawer content, not an edge case, so the terminator is
/// almost always behind one or two more punctuation characters.
/// What: scans the whole trailing punctuation run — every trailing character
/// that [`clean_token`] would strip — and reports whether any of it terminates
/// a sentence or clause. Interior punctuation is not consulted, so `Node.js`
/// and `src/main.rs` still read as one unterminated token.
/// Test: `stops_the_run_at_a_terminator_behind_markdown_emphasis`,
/// `interior_punctuation_does_not_terminate_the_run`.
fn ends_noun_phrase(raw: &str) -> bool {
    raw.trim_end()
        .chars()
        .rev()
        .take_while(|c| TOKEN_EDGE_PUNCT.contains(c))
        .any(|c| NP_TERMINATING_PUNCT.contains(&c))
}

/// Which way a noun-phrase walk moves away from the marker.
///
/// Why: the two sides are mirror images in one respect that matters. Walking
/// RIGHT, a token's trailing `.` closes the phrase we are building, so the
/// token belongs to it. Walking LEFT, that same `.` ended the PREVIOUS
/// sentence, so the token belongs to that one and must not be taken.
/// Test: `stops_the_noun_phrase_run_at_a_comma`,
/// `subject_walk_stops_before_a_previous_sentence`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Walk {
    /// Away from the marker into the object phrase.
    Right,
    /// Away from the marker into the subject phrase.
    Left,
}

/// Collect the noun-phrase run adjacent to a marker.
///
/// Why: both sides of a pattern hit need the same walk — "which tokens belong
/// to this phrase" is one question with one answer, and writing it twice is how
/// the two sides drift.
/// What: consumes `toks` in walk order (the caller reverses them for
/// [`Walk::Left`]) and stops at a function word, a verb-only or adverb-only
/// token, sentence punctuation, an unknown token, or [`NP_RUN_MAX`]. Returns
/// the accepted tokens in READING order regardless of direction, whether
/// punctuation closed the phrase, and how many raw tokens were consumed — the
/// caller needs the last two to decide about a following genitive.
/// Test: `caps_the_noun_phrase_run`, `stops_the_noun_phrase_run_at_a_comma`,
/// `subject_walk_stops_before_a_previous_sentence`.
fn noun_phrase_run<'a>(
    toks: &[&'a str],
    pos: &WordNetPos,
    walk: Walk,
) -> (Vec<&'a str>, bool, usize) {
    let mut run: Vec<&str> = Vec::new();
    let mut idx = 0usize;
    let mut terminated = false;
    while idx < toks.len() && run.len() < NP_RUN_MAX {
        let raw = toks[idx];
        let tok = clean_token(raw);
        if tok.is_empty() || is_stop_token(tok) {
            break;
        }
        let closes = ends_noun_phrase(raw);
        // Walking left, the punctuation sits between this token and the phrase
        // we are collecting, so the token is on the far side of the boundary.
        if closes && walk == Walk::Left {
            terminated = true;
            break;
        }
        let mask = pos.mask(tok);
        // A word WordNet knows only as a verb or only as an adverb cannot sit
        // inside a noun phrase, so the phrase ended before it.
        if mask != 0 && mask & (wordnet_pos::NOUN | wordnet_pos::ADJ) == 0 {
            break;
        }
        run.push(tok);
        idx += 1;
        if closes {
            terminated = true;
            break;
        }
        // A name heads its phrase rather than modifying the next word.
        if mask == 0 {
            break;
        }
    }
    if walk == Walk::Left {
        run.reverse();
    }
    (run, terminated, idx)
}

/// Pick the head of a noun-phrase run.
///
/// Why: #5399 — English compounds are right-headed, so the head is the
/// rightmost token of the phrase that can name a thing. An adjective-only token
/// names a property, so it is skipped rather than taken. This RE-WALK is the
/// whole policy: the earlier spike instead REJECTED the triple when the token
/// nearest the marker was adjective-only, and that dropped 304 pairs over 306k
/// lines of repo markdown, 196 of them (64%) with a perfectly good head noun
/// sitting one token further along. See the fixture comment on
/// `row1_rewalks_past_an_adjective_only_modifier` for why the reject rule was
/// wrong on its own terms and not merely expensive.
/// What: `run` is in reading order, so this returns its last non-adjective-only
/// token — or `None` when every token in it is adjective-only (`a robust` names
/// nothing) or the run is empty.
/// Test: `row1_rewalks_past_an_adjective_only_modifier`,
/// `row4_rewalks_past_the_adjective_to_the_head_noun`,
/// `an_all_adjective_subject_yields_no_triple`.
fn phrase_head(run: &[&str], pos: &WordNetPos) -> Option<String> {
    run.iter()
        .rev()
        .find(|t| !pos.is_adjective_only(t))
        .map(|t| (*t).to_string())
}

/// Pick the subject entity out of the text preceding a pattern marker.
///
/// Why: the head of the subject phrase is normally the token immediately before
/// the marker — `a fast parser is a tool` — so this agrees with the old
/// `last_token` in every ordinary case. It differs only when that token cannot
/// head a phrase, where `last_token` emitted it as an entity anyway. That is
/// the #5399 defect (`exhaustiveness --is-a--> hard`) mirrored onto the other
/// side of the marker, so it gets the same re-walk rather than a reject rule.
/// What: walks leftward from the marker through [`noun_phrase_run`] and takes
/// the phrase head. Returns `None` when the phrase has no head — `anything
/// robust is a compiler` names no entity to be the subject of anything.
/// Test: `subject_side_rewalks_past_an_adjective_only_token`,
/// `an_all_adjective_subject_yields_no_triple`,
/// `extract_triples_rejects_pronoun_subject`.
fn select_subject(left: &str, pos: &WordNetPos) -> Option<String> {
    let mut toks: Vec<&str> = left.split_whitespace().collect();
    toks.reverse();
    let (run, _, _) = noun_phrase_run(&toks, pos, Walk::Left);
    phrase_head(&run, pos)
}

/// Pick the object entity out of the text following a pattern marker.
///
/// Why: #5399 — taking one token is wrong three ways at once. It takes the
/// modifier instead of the head (`a fast parser` -> `fast`), it emits a bare
/// property as a type (`a hard requirement` -> `hard`), and it silently
/// truncates a relation into a type (`an ancestor of origin main` ->
/// `ancestor`). All three need to see more than one token and need to know each
/// token's part of speech.
/// What: walks rightward from the marker through [`noun_phrase_run`], rejects
/// the triple when the token that STOPPED the run is in
/// [`NP_CONTINUING_PREPOSITIONS`] (the phrase was not finished, so its head is
/// not in what we collected), and otherwise returns the phrase head. Every
/// WordNet miss widens the accepted set rather than narrowing it, so an unknown
/// crate name is never rejected for being unknown.
/// Test: `row4_rewalks_past_the_adjective_to_the_head_noun`,
/// `row2_rejects_ancestor_truncated_before_of`,
/// `unknown_subject_and_object_both_fail_open`.
fn select_object(right: &str, pos: &WordNetPos) -> Option<String> {
    let toks: Vec<&str> = right.split_whitespace().collect();
    let (run, terminated, consumed) = noun_phrase_run(&toks, pos, Walk::Right);
    if !terminated
        && toks
            .get(consumed)
            .is_some_and(|next| NP_CONTINUING_PREPOSITIONS.contains(&clean_token(next)))
    {
        return None;
    }
    phrase_head(&run, pos)
}

/// Strip surrounding punctuation from one raw token.
///
/// Why: #4678 — `first_token` trimmed a TRAILING run while its doc promised a
/// leading one, and both helpers used a set that omitted the characters drawer
/// content actually wraps names in (backticks, brackets, asterisks). So
/// `` `redb` `` reached the graph verbatim and became a second node for an
/// entity that already had one. Which SIDE of the marker a token sits on says
/// nothing about which edge carries punctuation, so both helpers clean both
/// edges through this one function rather than each guessing.
/// What: trims whitespace, then [`TOKEN_EDGE_PUNCT`] from both ends. Interior
/// characters are untouched, so `no-op`, `c#`, and `src/main.rs` survive whole.
/// Test: `extract_triples_strips_punctuation_from_both_token_edges`.
fn clean_token(raw: &str) -> &str {
    raw.trim().trim_matches(TOKEN_EDGE_PUNCT)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn input_for(content: &str, tags: &[&str], room: Option<&str>) -> (Uuid, Vec<String>) {
        let id = Uuid::new_v4();
        let owned_tags: Vec<String> = tags.iter().map(|s| s.to_string()).collect();
        let _ = content; // silence unused warning if test ignores content
        let _ = room;
        (id, owned_tags)
    }

    /// Why: Tag-derived triples are the lowest-hanging extraction and the
    /// graph view's first signal when no patterns fire. The KG's temporal
    /// model only allows one active triple per `(subject, predicate)`, so
    /// each tag becomes its own subject (`tag:<name>`) with a `tags`
    /// predicate pointing at the drawer.
    /// What: One `tag:<t> tags drawer:<id>` per non-empty tag, plus
    /// `room:<r> contains drawer:<id>` when a room is supplied.
    /// Test: This test.
    #[test]
    fn extract_triples_emits_tag_triples() {
        let (id, tags) = input_for("hello world", &["rust", "design"], Some("Backend"));
        let triples = extract_triples(&ExtractInput {
            drawer_id: id,
            content: "hello world",
            tags: &tags,
            room: Some("Backend"),
        });
        let object = drawer_subject(id);
        assert!(triples
            .iter()
            .any(|t| t.subject == "tag:rust" && t.predicate == "tags" && t.object == object));
        assert!(triples
            .iter()
            .any(|t| t.subject == "tag:design" && t.predicate == "tags" && t.object == object));
        assert!(triples.iter().any(|t| t.subject == "room:Backend"
            && t.predicate == "contains"
            && t.object == object));
    }

    /// Why: Hashtag tokens are a cheap user signal; the extractor must catch
    /// them so the graph picks up topical entities.
    /// What: `#rust` and `#design-doc` both become `topic:<term>
    /// mentioned-in drawer:<id>` triples, lower-cased and deduplicated.
    /// Test: This test.
    #[test]
    fn extract_triples_emits_hashtag_mentions() {
        let (id, tags) = input_for("see #Rust and #design-doc and #rust again", &[], None);
        let triples = extract_triples(&ExtractInput {
            drawer_id: id,
            content: "see #Rust and #design-doc and #rust again",
            tags: &tags,
            room: None,
        });
        let mention_subjects: Vec<&str> = triples
            .iter()
            .filter(|t| t.predicate == "mentioned-in")
            .map(|t| t.subject.as_str())
            .collect();
        assert!(mention_subjects.contains(&"topic:rust"));
        assert!(mention_subjects.contains(&"topic:design-doc"));
        // Dedupe — `#rust` and `#Rust` collapse.
        assert_eq!(
            mention_subjects
                .iter()
                .filter(|s| **s == "topic:rust")
                .count(),
            1
        );
    }

    /// Why: `is a` is the simplest NL pattern and the most common idiom in
    /// quick notes ("rustc is a compiler").
    /// What: Pattern fires once per content blob; subject and object are the
    /// nouns either side of the marker.
    /// Test: This test.
    #[test]
    fn extract_triples_extracts_is_a_pattern() {
        let (id, _) = input_for("rustc is a compiler for rust", &[], None);
        let triples = extract_triples(&ExtractInput {
            drawer_id: id,
            content: "rustc is a compiler for rust",
            tags: &[],
            room: None,
        });
        assert!(triples
            .iter()
            .any(|t| t.subject == "rustc" && t.predicate == "is-a" && t.object == "compiler"));
    }

    /// Why: Confidence and provenance are guard-rails — extracted triples
    /// must be recognisable and over-ridable.
    /// What: Every triple carries `provenance = Some("auto:remember")` and
    /// `confidence == AUTO_CONFIDENCE`.
    /// Test: This test.
    #[test]
    fn extract_triples_stamps_provenance() {
        let (id, tags) = input_for("anything", &["x"], None);
        let triples = extract_triples(&ExtractInput {
            drawer_id: id,
            content: "anything",
            tags: &tags,
            room: None,
        });
        assert!(!triples.is_empty());
        for t in &triples {
            assert_eq!(t.provenance.as_deref(), Some(AUTO_PROVENANCE));
            assert!((t.confidence - AUTO_CONFIDENCE).abs() < f32::EPSILON);
        }
    }

    /// Why: Reduced confidence is the contract a manual `kg_assert` of the
    /// same `(subject, predicate)` needs in order to "win" against the
    /// auto-extracted edge.
    /// What: Every triple carries `confidence == AUTO_CONFIDENCE` (currently
    /// 0.6); the constant is asserted to stay strictly below 1.0 so manual
    /// asserts always rank higher.
    /// Test: This test.
    #[test]
    #[allow(clippy::assertions_on_constants)]
    fn extract_triples_uses_reduced_confidence() {
        // Why: both bounds are static facts about the AUTO_CONFIDENCE
        // constant; the assertion is documentation for future tweakers.
        assert!(AUTO_CONFIDENCE < 1.0);
        assert!(AUTO_CONFIDENCE > 0.0);
    }

    /// Why: Empty / whitespace-only content must not panic or emit garbage.
    /// What: No tags, no room, no content → empty vec.
    /// Test: This test.
    #[test]
    fn extract_triples_never_panics_on_empty_input() {
        let id = Uuid::new_v4();
        let triples = extract_triples(&ExtractInput {
            drawer_id: id,
            content: "",
            tags: &[],
            room: None,
        });
        assert!(triples.is_empty());
    }

    /// Why: Edge-case test — content with no patterns but tags should still
    /// produce the tag triples (the graph view's primary signal).
    /// What: Single tag, no room, prose with no pattern hits → exactly one
    /// triple shaped as `tag:meeting tags drawer:<id>`.
    /// Test: This test.
    #[test]
    fn extract_triples_tags_only_path() {
        let id = Uuid::new_v4();
        let tags = vec!["meeting".to_string()];
        let triples = extract_triples(&ExtractInput {
            drawer_id: id,
            content: "Discussed roadmap.",
            tags: &tags,
            room: None,
        });
        assert_eq!(triples.len(), 1);
        assert_eq!(triples[0].subject, "tag:meeting");
        assert_eq!(triples[0].predicate, "tags");
        assert_eq!(triples[0].object, drawer_subject(id));
    }

    /// Why: Drawers tagged with deny-listed labels (test fixtures, QA scaffolding)
    /// must not pollute the KG with non-factual content.
    /// What: A drawer with the `test` tag must produce zero triples even when
    /// it also has a room and content with extractable patterns.
    /// Test: This test.
    #[test]
    fn extract_triples_skips_denied_tags() {
        let id = Uuid::new_v4();
        let tags = vec!["test".to_string(), "rust".to_string()];
        let triples = extract_triples(&ExtractInput {
            drawer_id: id,
            content: "rustc is a compiler",
            tags: &tags,
            room: Some("Backend"),
        });
        assert!(
            triples.is_empty(),
            "a drawer with a deny-list tag must produce zero triples, got {triples:?}"
        );
    }

    /// Why: Deny-list matching is case-insensitive so `TEST` and `Test` are
    /// blocked the same as `test`.
    /// What: A drawer tagged `FIXTURE` (upper-case) must still produce zero
    /// triples.
    /// Test: This test.
    #[test]
    fn extract_triples_deny_list_is_case_insensitive() {
        let id = Uuid::new_v4();
        let tags = vec!["FIXTURE".to_string()];
        let triples = extract_triples(&ExtractInput {
            drawer_id: id,
            content: "some content",
            tags: &tags,
            room: None,
        });
        assert!(
            triples.is_empty(),
            "upper-cased deny tag must still be blocked"
        );
    }

    /// Collect only the triples produced by the [`PATTERN_TABLE`] pass.
    ///
    /// Why: every assertion about extraction precision is about the pattern
    /// pass; the tag / room / hashtag passes always fire and would otherwise
    /// mask a "zero triples" assertion.
    /// What: filters by predicate against [`PATTERN_TABLE`].
    /// Test: used by every precision test below.
    fn pattern_triples(triples: &[Triple]) -> Vec<(String, String, String)> {
        let predicates: Vec<&str> = PATTERN_TABLE.iter().map(|(p, _)| *p).collect();
        triples
            .iter()
            .filter(|t| predicates.contains(&t.predicate.as_str()))
            .map(|t| (t.subject.clone(), t.predicate.clone(), t.object.clone()))
            .collect()
    }

    /// Run the extractor over bare content with no tags and no room.
    ///
    /// Why: the precision fixtures care only about content; tags and rooms
    /// would add noise triples to every assertion.
    /// What: builds an `ExtractInput` with empty tags and no room.
    /// Test: used by every precision test below.
    fn patterns_for(content: &str) -> Vec<(String, String, String)> {
        let triples = extract_triples(&ExtractInput {
            drawer_id: Uuid::new_v4(),
            content,
            tags: &[],
            room: None,
        });
        pattern_triples(&triples)
    }

    /// Why: `them --is-a--> no-op` is live in the real trusty-tools palace
    /// (asserted 2026-08-04). A marker hit is not evidence that the tokens
    /// either side of it are entities; a pronoun never is one.
    /// What: "calling them is a no-op ..." must yield zero pattern triples.
    /// Test: This test.
    #[test]
    fn extract_triples_rejects_pronoun_subject() {
        let got = patterns_for("calling them is a no-op when the flag is off");
        assert!(
            got.is_empty(),
            "pronoun subject must reject the whole triple, got {got:?}"
        );
    }

    /// Why: a stopword can land on either side of a marker, so filtering only
    /// the subject would still admit `libpq --depends-on--> the`.
    /// What: "libpq depends on the license" must yield zero pattern triples —
    /// the triple is rejected whole, never truncated to a half-triple.
    /// Test: This test.
    #[test]
    fn extract_triples_rejects_stopword_object() {
        let got = patterns_for("libpq depends on the license header");
        assert!(
            got.is_empty(),
            "stopword object must reject the whole triple, got {got:?}"
        );
    }

    /// Why: a one- or two-character token is almost never an entity, and the
    /// extractor had no length floor at all.
    /// What: "x is a thing" must yield zero pattern triples.
    /// Test: This test.
    #[test]
    fn extract_triples_rejects_short_token_off_allowlist() {
        let got = patterns_for("x is a thing worth recording");
        assert!(
            got.is_empty(),
            "short token off the allowlist must be rejected, got {got:?}"
        );
    }

    /// Why: the length floor must not swallow the short names this repo
    /// genuinely discusses — without the allowlist, `Go` and `C` would be
    /// rejected as noise and the filter would cost real recall.
    /// What: "Go is a language" still extracts `go --is-a--> language`.
    /// Test: This test.
    #[test]
    fn extract_triples_keeps_allowlisted_go() {
        let got = patterns_for("Go is a language with green threads");
        assert!(
            got.contains(&("go".into(), "is-a".into(), "language".into())),
            "allowlisted `Go` must survive the length floor, got {got:?}"
        );
    }

    /// Why: `C` is the single-character case — the length floor's worst
    /// false positive if the allowlist is not consulted.
    /// What: "C is a language" still extracts `c --is-a--> language`.
    /// Test: This test.
    #[test]
    fn extract_triples_keeps_allowlisted_c() {
        let got = patterns_for("C is a language without a runtime");
        assert!(
            got.contains(&("c".into(), "is-a".into(), "language".into())),
            "allowlisted `C` must survive the length floor, got {got:?}"
        );
    }

    /// Why: the filter must not cost recall on ordinary, well-formed content.
    /// A rejection filter that also rejects the good cases is a regression,
    /// so every predicate in [`PATTERN_TABLE`] keeps a worked example.
    /// What: table-driven — each row is `(content, expected triple)` and must
    /// appear in the extracted pattern set.
    /// Test: This test.
    #[test]
    fn extract_triples_keeps_real_entities_across_all_predicates() {
        let cases: &[(&str, (&str, &str, &str))] = &[
            (
                "tokio is an executor for async rust",
                ("tokio", "is-a", "executor"),
            ),
            (
                "alice works at initech today",
                ("alice", "works-at", "initech"),
            ),
            (
                "trusty-memory uses redb for persistence",
                ("trusty-memory", "uses", "redb"),
            ),
            (
                "trusty-search depends on trusty-common for shared helpers",
                ("trusty-search", "depends-on", "trusty-common"),
            ),
            (
                "the embedder requires onnxruntime at startup",
                ("embedder", "depends-on", "onnxruntime"),
            ),
        ];
        for (content, (s, p, o)) in cases {
            let got = patterns_for(content);
            let want = ((*s).to_string(), (*p).to_string(), (*o).to_string());
            assert!(
                got.contains(&want),
                "content {content:?} must still extract {want:?}, got {got:?}"
            );
        }
    }

    /// Why: `first_token` trimmed only a TRAILING run while its doc promised it
    /// stripped the leading one, so an entity quoted the way drawer content
    /// actually quotes things — markdown backticks, brackets, an opening
    /// quote — entered the graph with the punctuation welded on: `` `redb ``
    /// rather than `redb`. Two spellings of one entity are two nodes that never
    /// join up.
    /// What: pins the emitted strings. Both helpers strip punctuation from BOTH
    /// edges, so the subject side (`last_token`) is covered too, and no emitted
    /// token may retain an edge character.
    /// Test: This test.
    #[test]
    fn extract_triples_strips_punctuation_from_both_token_edges() {
        let cases: &[(&str, (&str, &str, &str))] = &[
            // Object side, markdown backticks — the common shape in drawers.
            (
                "trusty-memory uses `redb` for persistence",
                ("trusty-memory", "uses", "redb"),
            ),
            // Object side, parenthesised.
            (
                "the daemon uses (tantivy) for search",
                ("daemon", "uses", "tantivy"),
            ),
            // Subject side, opening quote with no closing one before the marker.
            (
                "he said \"rustc is a compiler for rust",
                ("rustc", "is-a", "compiler"),
            ),
            // Object side, bold markdown.
            (
                "trusty-search depends on **trusty-common** for helpers",
                ("trusty-search", "depends-on", "trusty-common"),
            ),
        ];
        for (content, (s, p, o)) in cases {
            let got = patterns_for(content);
            let want = ((*s).to_string(), (*p).to_string(), (*o).to_string());
            assert!(
                got.contains(&want),
                "content {content:?} must emit {want:?}, got {got:?}"
            );
            for (subject, _, object) in &got {
                for tok in [subject, object] {
                    assert!(
                        !tok.starts_with(TOKEN_EDGE_PUNCT) && !tok.ends_with(TOKEN_EDGE_PUNCT),
                        "emitted token {tok:?} still carries edge punctuation"
                    );
                }
            }
        }
    }

    /// Why: a duplicated entry is dead weight and a sign the categories drifted
    /// apart during editing.
    /// What: every [`STOPWORDS`] entry appears exactly once and is already
    /// lower-case, which is what [`is_stop_token`] compares against.
    /// Test: This test.
    #[test]
    fn stopwords_are_unique() {
        let mut seen: HashSet<&str> = HashSet::new();
        for w in STOPWORDS {
            assert!(seen.insert(w), "duplicate stopword {w:?}");
            assert_eq!(*w, w.to_lowercase(), "stopword {w:?} must be lower-case");
        }
    }

    /// Why: the rejection contract is only as good as the list behind it.
    /// What: every [`STOPWORDS`] entry is rejected, in its own case and
    /// upper-cased, since content reaches the filter lower-cased but the purge
    /// path reads subjects straight out of the store.
    /// Test: This test.
    #[test]
    fn is_stop_token_rejects_every_stopword() {
        for w in STOPWORDS {
            assert!(is_stop_token(w), "{w:?} must be rejected");
            assert!(
                is_stop_token(&w.to_uppercase()),
                "{w:?} must be rejected case-insensitively"
            );
        }
    }

    /// Why: an over-broad filter costs recall, which is the failure mode that
    /// would make this change a net loss.
    /// What: ordinary multi-character entity names are accepted.
    /// Test: This test.
    #[test]
    fn is_stop_token_accepts_ordinary_entities() {
        for tok in [
            "rustc",
            "compiler",
            "trusty-memory",
            "redb",
            "no-op",
            "onnxruntime",
            "initech",
        ] {
            assert!(!is_stop_token(tok), "{tok:?} must be accepted");
        }
    }

    /// Why: `first_token` / `last_token` strip only a trailing run, so a token
    /// can still carry an opening bracket or backtick. Comparing that raw
    /// against the list would miss the stopword it is meant to catch.
    /// What: edge punctuation is stripped before matching, interior characters
    /// are not, and a token that is nothing but punctuation is rejected.
    /// Test: This test.
    #[test]
    fn is_stop_token_normalises_surrounding_punctuation() {
        assert!(
            is_stop_token("(the"),
            "leading bracket must not hide a stopword"
        );
        assert!(is_stop_token("`it`"), "backticks must not hide a stopword");
        assert!(
            is_stop_token("---"),
            "punctuation-only token must be rejected"
        );
        assert!(
            is_stop_token("   "),
            "whitespace-only token must be rejected"
        );
        assert!(
            !is_stop_token("`redb`"),
            "interior name must survive trimming"
        );
        assert!(
            !is_stop_token("no-op"),
            "interior hyphen must survive trimming"
        );
    }

    /// Why: #5401's merge asks this function which node a stored term belongs
    /// under, and its three answers are the pass's whole selection rule.
    /// What: a punctuated real entity names its cleaned twin; an already-clean
    /// term and a punctuated stopword both name nothing — the second because it
    /// is `--purge-stale-subjects`'s to delete, not this pass's to re-point. A
    /// name whose leading punctuation IS the identity — `.env`, `--verbose` —
    /// names the same twin as decorative punctuation does, because the merge
    /// inherits [`clean_token`] rather than re-deciding.
    /// Test: This test.
    #[test]
    fn canonical_entity_names_the_cleaned_twin() {
        assert_eq!(canonical_entity("`redb`"), Some("redb"));
        assert_eq!(canonical_entity("(sled)"), Some("sled"));
        assert_eq!(canonical_entity("*trusty-memory*"), Some("trusty-memory"));
        assert_eq!(
            canonical_entity("src/main.rs,"),
            Some("src/main.rs"),
            "only edge punctuation moves; the interior path must survive"
        );

        assert_eq!(canonical_entity("redb"), None, "a clean term is canonical");
        assert_eq!(canonical_entity("no-op"), None);

        assert_eq!(
            canonical_entity("(\"the"),
            None,
            "a punctuated stopword belongs to the purge, not the merge"
        );
        assert_eq!(canonical_entity("`it`"), None);
        assert_eq!(canonical_entity("---"), None);

        // The dotfile class: the leading punctuation is part of the name, and
        // it is collapsed anyway. Deliberate — today's extractor already emits
        // `env` for a drawer that says `.env`, so the merge aligns the stored
        // node with what extraction now produces rather than inventing a
        // second normalisation that could drift from it.
        assert_eq!(canonical_entity(".env"), Some("env"));
        assert_eq!(canonical_entity(".git"), Some("git"));
        assert_eq!(canonical_entity(".gitignore"), Some("gitignore"));
        assert_eq!(canonical_entity("_private"), Some("private"));
        assert_eq!(canonical_entity("--verbose"), Some("verbose"));
    }

    /// Why: the allowlist is the length floor's escape hatch; if an entry stops
    /// working the floor silently starts eating real entities.
    /// What: every [`SHORT_ENTITY_ALLOWLIST`] entry is accepted, and no entry
    /// collides with [`STOPWORDS`] (which is checked first and would win).
    /// Test: This test.
    #[test]
    fn short_entity_allowlist_entries_survive_the_length_floor() {
        for tok in SHORT_ENTITY_ALLOWLIST {
            assert!(
                !STOPWORDS.contains(tok),
                "{tok:?} is on both lists; the stopword check runs first and wins"
            );
            assert!(!is_stop_token(tok), "allowlisted {tok:?} must be accepted");
        }
    }

    /// Why: #4678 named three live regressions and could only reach the first.
    /// Its filter is lexical — it judges one token at a time — and
    /// `exhaustiveness`, `hard`, `squash` and `ancestor` are all ordinary
    /// content words, indistinguishable token-wise from the `rustc --is-a-->
    /// compiler` the extractor is supposed to keep. So #4678 shipped this test
    /// ASSERTING the residue, under the name
    /// `lexical_filter_does_not_reach_the_two_content_word_regressions`, as the
    /// standing record of an open gate.
    ///
    /// #5399 closed that gate with WordNet head-noun selection, so the
    /// assertion is INVERTED rather than deleted and the name is kept
    /// recognisable — `git log -S` on either name finds both states. The
    /// lexical half is unchanged and still passes: it is what proves the fix
    /// landed in the POS pass and not by quietly widening the stopword list,
    /// which would have broken `rustc --is-a--> compiler` too.
    /// What: asserts the four words still survive the lexical filter, AND that
    /// neither #4678 residue triple is produced any more.
    /// Test: This test.
    #[test]
    fn lexical_filter_still_cannot_reach_the_two_content_word_regressions_but_wordnet_does() {
        assert!(
            !is_stop_token("exhaustiveness")
                && !is_stop_token("hard")
                && !is_stop_token("squash")
                && !is_stop_token("ancestor"),
            "these are ordinary words; no lexical filter may reject them"
        );
        let exhaustiveness = patterns_for("match exhaustiveness is a hard requirement here");
        assert!(
            !exhaustiveness.contains(&("exhaustiveness".into(), "is-a".into(), "hard".into())),
            "#4678 residue must be gone; got {exhaustiveness:?}"
        );
        let squash = patterns_for("confirm the squash is an ancestor of origin main");
        assert!(
            !squash.contains(&("squash".into(), "is-a".into(), "ancestor".into())),
            "#4678 residue must be gone; got {squash:?}"
        );
    }

    /// Why: An empty deny-list (e.g. in integration tests that want to exercise
    /// extraction regardless of tags) must not suppress any triples.
    /// What: Calling `extract_triples_with_config` with `deny_tags = &[]` on a
    /// drawer tagged `test` must produce the normal tag triple.
    /// Test: This test.
    #[test]
    fn extract_triples_empty_deny_list_passes_through() {
        let id = Uuid::new_v4();
        let tags = vec!["test".to_string()];
        let config = KgExtractConfig {
            deny_tags: &[],
            ..Default::default()
        };
        let triples = extract_triples_with_config(
            &ExtractInput {
                drawer_id: id,
                content: "anything",
                tags: &tags,
                room: None,
            },
            &config,
        );
        // "test" tag should produce a tag triple when the deny-list is empty.
        assert!(
            !triples.is_empty(),
            "empty deny-list must not suppress extraction"
        );
    }
}

/// The #5399 evaluation set for the shipped WordNet policy, plus the cases that
/// probe where it behaves surprisingly.
///
/// Why: these rows decided the bake-off between the WordNet lane and a spaCy
/// lane on identical inputs, so they are the record of what was chosen. The
/// `surprising_*` tests are deliberately included even where they record a
/// WRONG answer — an eval set built only from passing rows tells the next
/// reader nothing about where the approach runs out.
/// What: each test drives `extract_triples` end to end and asserts on the
/// pattern-derived triples only (tag/room/hashtag triples are unrelated).
#[cfg(test)]
mod wordnet_eval {
    use super::*;

    /// Pattern triples only, as `(subject, predicate, object)`.
    fn pattern_triples(content: &str) -> Vec<(String, String, String)> {
        extract_triples(&ExtractInput {
            drawer_id: Uuid::new_v4(),
            content,
            tags: &[],
            room: None,
        })
        .into_iter()
        .filter(|t| PATTERN_TABLE.iter().any(|(p, _)| *p == t.predicate))
        .map(|t| (t.subject, t.predicate, t.object))
        .collect()
    }

    fn assert_none(content: &str) {
        let got = pattern_triples(content);
        assert!(
            got.is_empty(),
            "expected no triple from {content:?}, got {got:?}"
        );
    }

    fn assert_one(content: &str, s: &str, p: &str, o: &str) {
        let got = pattern_triples(content);
        assert_eq!(
            got,
            vec![(s.to_string(), p.to_string(), o.to_string())],
            "wrong extraction from {content:?}"
        );
    }

    // ---- Row 1: adjective-only modifier. `hard` is ADJ|ADV, never NOUN. ----
    //
    // 🔴 THIS ROW'S EXPECTATION WAS CHANGED, AND THE OLD ONE WAS WRONG. The
    // lane-A spike specified "NO triple" here, on the theory that a phrase
    // whose modifier is adjective-only is unsalvageable. That misread the
    // defect. #5399 is about extracting the ADJECTIVE `hard` as though it were
    // a type; it is not about the noun `requirement`, and
    // `exhaustiveness --is-a--> requirement` is a TRUE statement about this
    // sentence. Rejecting the whole triple threw away a good fact to avoid a
    // bad one.
    //
    // The corpus settled it: over 306k lines of repo markdown the reject rule
    // dropped 304 pairs, and 196 of them (64%) had a recoverable head noun one
    // token further along. 43 of 78 ordinary technical adjectives — `robust`,
    // `concurrent`, `embedded`, `distributed`, `immutable`, `scalable` — are
    // adjective-only in WordNet, so the rule fired constantly, and every time
    // it fired it destroyed a usable triple.
    //
    // Do NOT "fix" this back to `assert_none`. Re-walking to the head noun IS
    // the shipped policy; a test asserting no triple here asserts the bug.
    #[test]
    fn row1_rewalks_past_an_adjective_only_modifier() {
        assert_one(
            "match exhaustiveness is a hard requirement here",
            "exhaustiveness",
            "is-a",
            "requirement",
        );
    }

    // ---- Row 2: genitive boundary. The NP is `ancestor of origin main`. ----
    #[test]
    fn row2_rejects_ancestor_truncated_before_of() {
        assert_none("confirm the squash is an ancestor of origin main");
    }

    // ---- Row 3: the baseline good triple must survive untouched. ----
    #[test]
    fn row3_keeps_rustc_is_a_compiler() {
        assert_one("rustc is a compiler", "rustc", "is-a", "compiler");
    }

    // ---- Row 4: head-noun re-walk past a modifier. ----
    #[test]
    fn row4_rewalks_past_the_adjective_to_the_head_noun() {
        assert_one("librs is a fast parser", "librs", "is-a", "parser");
    }

    // ---- Row 5: `for` closes the object; it is a verb adjunct, not a NP. ----
    #[test]
    fn row5_keeps_uses_object_before_a_non_genitive_preposition() {
        assert_one(
            "trusty-memory uses redb for persistence",
            "trusty-memory",
            "uses",
            "redb",
        );
    }

    // ---- Row 6: same genitive shape as row 2, different noun. ----
    #[test]
    fn row6_rejects_member_of_the_process_group() {
        assert_none("the daemon is a member of the process group");
    }

    // ---- Row 7: right-headed compound; the head is the LAST noun. ----
    #[test]
    fn row7_takes_the_head_of_a_noun_noun_compound() {
        assert_one("tantivy is a search library", "tantivy", "is-a", "library");
    }

    // ================== the re-walk, on the cases that drove it =============

    /// The adjectives with no WordNet noun sense. Under the spike's reject rule
    /// every one of these produced nothing; under the shipped policy each
    /// re-walks to its head noun.
    #[test]
    fn adjective_only_modifiers_re_walk_instead_of_destroying_the_triple() {
        assert_one("librs is a robust parser", "librs", "is-a", "parser");
        assert_one(
            "tantivy is an embedded library",
            "tantivy",
            "is-a",
            "library",
        );
        assert_one("sled is a concurrent database", "sled", "is-a", "database");
        // A modifier WordNet DOES give a noun sense now behaves identically,
        // which is the point: the lexicographic accident no longer changes the
        // outcome, only which token the re-walk skips.
        assert_one("librs is a fast parser", "librs", "is-a", "parser");
    }

    /// The lexicographic split the re-walk removed, measured rather than
    /// asserted by feel.
    ///
    /// Why: 43 of these 78 ordinary technical adjectives have no noun sense in
    /// WordNet 3.1. That is a property of Princeton's lexicography, not of
    /// anything tunable here, and it is the size of the blast radius the reject
    /// rule had. Pinned so a WordNet upgrade that moves it is visible.
    #[test]
    fn measures_the_adjective_only_population() {
        let wn = WordNetPos::shipped();
        let sample: &[&str] = &[
            "fast",
            "small",
            "simple",
            "modern",
            "lightweight",
            "portable",
            "generic",
            "old",
            "good",
            "great",
            "better",
            "new",
            "robust",
            "minimal",
            "tiny",
            "huge",
            "concurrent",
            "embedded",
            "reliable",
            "lazy",
            "secure",
            "rusty",
            "async",
            "asynchronous",
            "synchronous",
            "distributed",
            "persistent",
            "immutable",
            "mutable",
            "functional",
            "relational",
            "hierarchical",
            "incremental",
            "deterministic",
            "idempotent",
            "scalable",
            "extensible",
            "pluggable",
            "configurable",
            "optional",
            "required",
            "deprecated",
            "experimental",
            "stable",
            "unstable",
            "legacy",
            "native",
            "remote",
            "local",
            "static",
            "dynamic",
            "public",
            "private",
            "internal",
            "external",
            "open",
            "closed",
            "free",
            "paid",
            "commercial",
            "fancy",
            "neat",
            "clean",
            "dirty",
            "quick",
            "slow",
            "heavy",
            "light",
            "cheap",
            "expensive",
            "strict",
            "lenient",
            "safe",
            "unsafe",
            "correct",
            "incorrect",
            "complete",
            "partial",
        ];
        let adj_only = sample.iter().filter(|w| wn.is_adjective_only(w)).count();
        assert_eq!(
            (sample.len(), adj_only),
            (78, 43),
            "adjective-only population moved; re-measure before trusting the #5399 numbers"
        );
    }

    /// A phrase with no head names nothing, so no triple — and this is the
    /// re-walk returning `None`, not a surviving reject rule.
    #[test]
    fn an_all_adjective_subject_yields_no_triple() {
        assert_none("anything robust is a compiler");
    }

    /// The subject side gets the same re-walk. Without it, the old `last_token`
    /// would emit whichever token sat against the marker.
    #[test]
    fn subject_side_rewalks_past_an_adjective_only_token() {
        assert_one("the fast parser is a tool", "parser", "is-a", "tool");
    }

    // ====================== markdown and phrase boundaries ==================

    /// #5399: the terminator check used to read only the raw token's LAST
    /// character. `**MCP is a thin proxy.**` ends in `*`, so the `.` was missed
    /// and the run walked on into the next sentence, yielding
    /// `mcp --is-a--> sessions`. Markdown emphasis is the ordinary shape of
    /// drawer content, so this was never an edge case.
    #[test]
    fn stops_the_run_at_a_terminator_behind_markdown_emphasis() {
        assert_one(
            "**MCP is a thin proxy.** Sessions are cheap here",
            "mcp",
            "is-a",
            "proxy",
        );
    }

    /// The trailing-punctuation scan must not fire on interior punctuation, or
    /// every dotted name would close its own phrase.
    #[test]
    fn interior_punctuation_does_not_terminate_the_run() {
        for raw in ["parser", "node.js", "src/main.rs", "**bold**", "v1.2"] {
            assert!(!ends_noun_phrase(raw), "{raw:?} must not terminate");
        }
        for raw in ["proxy.**", "compiler,", "thing.", "group)", "list;", "end?"] {
            assert!(ends_noun_phrase(raw), "{raw:?} must terminate");
        }
    }

    /// Walking LEFT, a trailing `.` ended the PREVIOUS sentence, so the token
    /// carrying it belongs to that sentence and cannot join this phrase.
    /// Without the direction rule the head jumps the boundary and asserts
    /// `tantivy --is-a--> compiler` from two unrelated clauses.
    #[test]
    fn subject_walk_stops_before_a_previous_sentence() {
        assert_none("we shipped tantivy. robust is a compiler");
    }

    // ================= multi-line bodies, the production shape ==============
    // 🔴 Every other fixture in this module is a SINGLE LINE, and so is the
    // corpus harness (`examples/kg_dump.rs` reads one line per extraction).
    // Production is not: `tools::helpers::auto_extract_and_assert` and
    // `commands::kg_rebuild::rebuild_one` both pass a whole drawer body. The
    // walk had no newline boundary, so it ran off the end of its own sentence
    // and took a head from the next line — a correct-to-wrong regression that
    // no single-line fixture could see. Use `--whole-file` when measuring.

    /// A bare newline closes the phrase exactly as a period does.
    ///
    /// Against `8402bd8b` every row here produced the token from line two:
    /// `builds`, `builds`, `runs`, `sled`.
    #[test]
    fn the_object_walk_stops_at_a_line_break() {
        assert_one(
            "trusty-search is a daemon\ncargo builds it",
            "trusty-search",
            "is-a",
            "daemon",
        );
        assert_one(
            "rustc is a compiler\ncargo builds it",
            "rustc",
            "is-a",
            "compiler",
        );
        assert_one(
            "the parser is a tool\ncargo runs fine",
            "parser",
            "is-a",
            "tool",
        );
        assert_one(
            "redb is a database\nsled is another one",
            "redb",
            "is-a",
            "database",
        );
    }

    /// The subject side takes the same boundary: a preceding line is a
    /// previous sentence, whether or not it ended in punctuation.
    #[test]
    fn the_subject_walk_stops_at_a_line_break() {
        assert_one(
            "we shipped tantivy\nrobust compilers are a myth\nredb is a database",
            "redb",
            "is-a",
            "database",
        );
    }

    /// A trailing period already worked; it must keep working.
    #[test]
    fn a_terminated_line_is_unaffected_by_the_newline_rule() {
        assert_one(
            "trusty-search is a daemon.\ncargo builds it",
            "trusty-search",
            "is-a",
            "daemon",
        );
    }

    // ============ an inflected or unknown token must not take the head ======

    /// A participle is not a noun, and WordNet indexes only its base form.
    ///
    /// Against `8402bd8b` `containing` was unknown, and "unknown" meant
    /// "eligible to head a phrase", so this asserted
    /// `skill --is-a--> containing`. Resolving `containing` to `contain`
    /// (VERB-only) ends the phrase before it, as it always should have.
    #[test]
    fn a_participle_does_not_head_the_phrase() {
        assert_one(
            "each skill is a directory containing:",
            "skill",
            "is-a",
            "directory",
        );
        assert_one(
            "trusty-search is a daemon parsing every file",
            "trusty-search",
            "is-a",
            "daemon",
        );
    }

    /// An unknown token may OPEN a phrase but never joins one that already has
    /// a head — against `8402bd8b` this asserted the whole file path as a type.
    ///
    /// The closed-class `inside` is what stops the run here: it is a
    /// preposition, so the phrase ends before the path regardless of whether
    /// WordNet has heard of the path.
    #[test]
    fn an_unknown_token_does_not_displace_an_established_head() {
        assert_one(
            "tree is a comment inside crates/trusty-search/src/allowlist/tests.rs",
            "tree",
            "is-a",
            "comment",
        );
    }

    /// A plural subject survives, including one whose singular ends in `e`.
    ///
    /// Both of these yielded a triple at `8402bd8b` and nothing at `ba579925`:
    /// `base_form_candidates` chopped `es` before `s`, so `notes` resolved to
    /// the adverb `not` and `sites` to the verb `sit`, and a verb-or-adverb
    /// token ends the phrase. The sibilant rule in
    /// [`crate::wordnet_pos::base_form_candidates`] is what restores them.
    #[test]
    fn a_plural_subject_whose_singular_ends_in_e_still_heads_its_phrase() {
        assert_one("notes is a drawer", "notes", "is-a", "drawer");
        assert_one("sites is a directory", "sites", "is-a", "directory");
    }

    /// An unknown identifier still heads its phrase when the token before it is
    /// a modifier WordNet also lists as a noun.
    ///
    /// 🔴 DO NOT "fix" this by stopping the walk at an unknown token once the
    /// run is non-empty. That rule was written and then removed here, measured
    /// over 1,277 markdown files: it changed 156 heads, and 83 of those
    /// replaced the real head with an adjective-capable modifier —
    /// `tcode|is-a|local`, `persistence|is-a|redb` -> `single`,
    /// `file|is-a|no-op` -> `true`. That is the #5399 defect itself arriving by
    /// a different route, and [`phrase_head`]'s adjective-only skip cannot
    /// catch it, because WordNet lists every one of those words as a noun as
    /// well. It also dropped 76 triples outright, several of them real facts
    /// (`server|uses|rocksdb`, `codebase|uses|redb`).
    /// Test: this is the isolating test — reintroducing the rule fails it.
    #[test]
    fn an_unknown_token_heads_its_phrase_over_an_adjective_capable_modifier() {
        // `local` is NOUN|ADJ, so it passes both the POS check and the
        // adjective-only skip; only walking on to `app` finds the real head.
        assert_one("tcode is a local app", "tcode", "is-a", "app");
        assert_one(
            "persistence is a single redb",
            "persistence",
            "is-a",
            "redb",
        );
    }

    #[test]
    fn stops_the_noun_phrase_run_at_a_comma() {
        assert_one(
            "rustc is a compiler, and cargo is the build tool",
            "rustc",
            "is-a",
            "compiler",
        );
    }

    #[test]
    fn caps_the_noun_phrase_run() {
        // Five noun/adjective tokens; the head must come from the first four.
        assert_one(
            "librs is a fast small modular parser core",
            "librs",
            "is-a",
            "parser",
        );
    }

    // ============================ fail-open =================================

    #[test]
    fn unknown_subject_and_object_both_fail_open() {
        assert_one("tantivy uses fst", "tantivy", "uses", "fst");
    }

    /// WordNet knows ordinary English, and ordinary English words are also
    /// language and tool names. Nothing here rejects a known word, so these
    /// work — but the fail-open rule is what saves them, and any future
    /// "reject known common nouns" tightening would break exactly these.
    #[test]
    fn common_english_words_that_are_real_entity_names_survive() {
        assert_one("rust is a language", "rust", "is-a", "language");
        assert_one("go is a language", "go", "is-a", "language");
        assert_one("python is a language", "python", "is-a", "language");
    }

    /// The caller's table is the one consulted — the property that replaced the
    /// `OnceLock`. Against a table that knows nothing, every token fails open,
    /// so the walk stops at the first one and reproduces the #4678 residue on
    /// demand. That is the proof the shipped table is what removes it.
    #[test]
    fn a_caller_supplied_pos_table_is_the_one_consulted() {
        let config = KgExtractConfig {
            pos: WordNetPos::from_table("zzz\t1\n"),
            ..Default::default()
        };
        let got: Vec<_> = extract_triples_with_config(
            &ExtractInput {
                drawer_id: Uuid::new_v4(),
                content: "match exhaustiveness is a hard requirement here",
                tags: &[],
                room: None,
            },
            &config,
        )
        .into_iter()
        .filter(|t| PATTERN_TABLE.iter().any(|(p, _)| *p == t.predicate))
        .map(|t| (t.subject, t.predicate, t.object))
        .collect();
        assert_eq!(
            got,
            vec![(
                "exhaustiveness".to_string(),
                "is-a".to_string(),
                "hard".to_string()
            )]
        );
    }

    // ================= where this approach behaves surprisingly =============
    // Each of these is a KNOWN limitation, filed separately and deliberately
    // NOT fixed in #5399. They are pinned so that a future change altering them
    // does so on purpose.

    /// Only `of` is treated as NP-continuing, so the same truncation slips
    /// through with every other preposition. Widening the set breaks row 5
    /// (`uses redb for persistence`); what actually separates the two is
    /// relational-noun complement requirements, which WordNet does not encode.
    #[test]
    fn surprising_non_genitive_relational_phrase_still_truncates() {
        assert_one(
            "the daemon is a participant in the process group",
            "daemon",
            "is-a",
            "participant",
        );
    }

    /// A regular inflection is POS-checked through its base form.
    ///
    /// 🔴 THIS EXPECTATION WAS CHANGED, AND THE OLD ONE DESCRIBED A DEFECT.
    /// It used to assert `mask("parsers") == 0` and file that under "known
    /// limitations, benign" — WordNet indexes base forms, so an inflected word
    /// simply took the fail-open path. That reading missed what fail-open
    /// MEANS inside the walk: unknown is what makes a token eligible to head a
    /// phrase, so every participle became a candidate head and won the slot by
    /// sitting rightmost. `a directory containing:` asserted `containing` as
    /// the type. So the limitation was not benign, and the fix is to resolve
    /// the base form rather than to refuse unknown tokens — refusing them
    /// outright would take `parsers` with it, and `parsers` is a real head.
    #[test]
    fn inflected_forms_resolve_through_their_base_form() {
        let wn = WordNetPos::shipped();
        assert!(
            wn.is_noun("parsers"),
            "a regular plural keeps its noun sense"
        );
        assert_eq!(wn.mask("containing") & wordnet_pos::NOUN, 0);
        assert_one("librs is a fast parsers", "librs", "is-a", "parsers");
    }

    /// A gerund that WordNet lists as a noun in its own right still takes the
    /// head, because membership cannot tell a nominalisation from a participle.
    ///
    /// `running` is NOUN|ADJ and `clearing` is NOUN, so no base-form retry
    /// fires and both read as ordinary nouns. Stopping the run at any `-ing`
    /// word whose base is a verb WOULD fix these two, and it was measured over
    /// this repo's markdown before being rejected. The count first recorded
    /// here — "repairs ~25, breaks ~18" — was wrong and flattered the decision;
    /// the break side is the larger one. Over the 1,277-file corpus the rule
    /// reaches 71 head positions ending in `-ing` (47 distinct), and most of
    /// them are ordinary nouns it would destroy: `running`, `mapping`,
    /// `warning`, `tooling`, `ranking`, `understanding`. Telling a
    /// nominalisation from a participle is syntax, not lexical membership, so
    /// it needs a tagger rather than a wider suffix table.
    #[test]
    fn surprising_a_gerund_noun_still_takes_the_head() {
        assert_one(
            "session creation depends on daemon running",
            "creation",
            "depends-on",
            "running",
        );
        assert_one(
            "budget_tokens is an estimate clearing a budget",
            "budget_tokens",
            "is-a",
            "clearing",
        );
    }

    /// Two unknown tokens in a row: the run stops at the first, so the head is
    /// the first name rather than the last. Right for `uses redb for X`, wrong
    /// for a genuine unknown-unknown compound.
    #[test]
    fn surprising_run_stops_at_the_first_unknown_token() {
        assert_one(
            "trusty-memory uses redb sled",
            "trusty-memory",
            "uses",
            "redb",
        );
    }

    /// A single-token head truncates a compound.
    #[test]
    fn surprising_single_token_head_truncates_a_compound() {
        assert_one(
            "brew is a system package managers",
            "brew",
            "is-a",
            "managers",
        );
    }
}