velesdb-memory 0.14.1

VelesDB-memory: local-first MCP memory server for AI agents (remember/recall/relate/forget/why + deterministic context compiler).
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
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
//! Every MCP tool must be reachable from every language binding — or its
//! absence must be a DECLARED decision, with a reason.
//!
//! Why this file exists: the `entity` tool shipped on the MCP server and was
//! silently missing from all three bindings (`velesdb-node`,
//! `velesdb-python`, `velesdb-wasm`) for its entire life. Nobody noticed
//! because nothing ever compared the surfaces. Two other absences
//! (`feedback`, `remember_extracted` on WASM) were deliberate and documented
//! in the WASM module header — but a reader had no way to tell a documented
//! decision from an oversight, since both looked identical from the outside:
//! a method that isn't there.
//!
//! The `remember_extracted` half of that pair is gone: its reason named
//! `OllamaExtractor` as the crate's only `Extractor`, and a deterministic,
//! dependency-free one annulled it (issue #1692). Worth recording, because
//! the exemption check never read that prose — it only asks whether the
//! binding started publishing the tool. An exemption whose REASON has become
//! false can therefore sit green indefinitely; only a reader catches it.
//!
//! The invariant is therefore not "every binding implements every tool" — it
//! is "every (tool, binding) pair is either implemented or explicitly
//! exempted with a reason". A new MCP tool added without touching the
//! bindings turns this test red on the spot.
//!
//! ## Where the truth comes from
//!
//! The tool list is read from the REAL [`McpServer`] over an in-memory
//! duplex (the idiom of `mcp_schema_bdd.rs`), so it can never drift from
//! what a harness actually sees. The binding surfaces are read from their
//! SOURCE files rather than from a hand-maintained manifest, deliberately: a
//! manifest is a second copy of the truth and can lie (someone adds a tool,
//! updates the manifest, and forgets the code — and the test stays green on
//! a promise nobody kept). Source is the only copy that cannot.
//!
//! Reading source is precise here because each binding publishes its whole
//! surface from exactly ONE `impl` block whose closing brace sits at column
//! 0 — so the scan is bounded to that block, never to a test module or a
//! private helper elsewhere in the file. Method names are the tool names,
//! verbatim, in all three bindings (JS/`camelCase` renaming happens in the
//! `#[napi]` / `#[wasm_bindgen]` attribute, not in the Rust identifier).
//!
//! ## The second invariant: return SHAPE, not just presence
//!
//! Name parity alone was blind by construction, and that blindness had a
//! cost. `load_working_context` served a three-field envelope (`found`,
//! `working`, `other_sessions`) from the MCP server since V2a-1 while all
//! three bindings kept handing back a bare `WorkingContext | null` — so a
//! caller going through a binding could not tell "nothing was ever saved"
//! from "a typo in `session` missed a session that exists". This file was
//! green the whole time: the method WAS there, under the right name, and
//! nothing here ever looked at what it returned.
//!
//! So the second guard reads each tool's `output_schema` ROOT KEYS from the
//! live server and requires every one of them to reach the binding. A field
//! the server publishes and no binding ever names is exactly the defect
//! above. Three ways to satisfy it, in the order they are tried:
//!
//! 1. **The binding names the server's own output type** (read out of the
//!    server source's `output_schema = wire_safe_output_schema::<T>()`).
//!    That is the complete answer: relaying `T` itself cannot lose a field,
//!    today or when `T` grows one. Where a binding relays wholesale, the
//!    type is written down as an annotation for exactly this reason —
//!    `let compiled: CompiledContext = …` in the WASM binding is not
//!    decoration, it is the relay made checkable.
//! 2. **The field is named in the method region**, or in the body of a
//!    binding-local struct that region names (ONE hop — the Node idiom of a
//!    mirror DTO, `CompiledContextJs`). The hop is what turns "Node never
//!    names `sections`" into the one statement that is true: Node never
//!    relays `warnings`.
//! 3. **A declared [`ShapeDivergence`]**, with its reason.
//!
//! ### What this guard does NOT prove — read this before trusting it
//!
//! **It is a text search over source, nothing more.** The region it scans is
//! the whole method region: doc comment, attributes (`#[napi(ts_return_type
//! = ...)]`, `#[wasm_bindgen]`, `#[pyo3]`) and body. A field named ONLY in a
//! prose comment therefore PASSES — as does a field named in a
//! `ts_return_type` string while the body returns something else entirely,
//! or a type annotation naming `T` on a value that is then reshaped by hand.
//!
//! It cannot be otherwise here: the bindings serialize whole values in one
//! `serde` call, so the field names legitimately never appear as identifiers
//! in their bodies. Requiring them in the body would force a hand-written
//! field-by-field copy — more code, more places to diverge — to satisfy a
//! test.
//!
//! Consequence, stated plainly: **this guard proves DECLARATION, never
//! MARSHALLING.** That a binding actually emits the field with the right
//! value is proved by the bindings' own round-trip tests
//! (`crates/velesdb-node/__test__/index.spec.mjs`,
//! `crates/velesdb-python/tests/test_context_compiler.py`,
//! `crates/velesdb-wasm/tests/memory_wedge_web.rs`), which call the real
//! binding and read the real result. What this file adds is the thing none
//! of those can do: notice that the SERVER grew a field nobody relayed.
//!
//! ### `SHAPE_DIVERGENCES` holds explicit differences, on purpose
//!
//! Most entries are deliberate unwraps (`recall` → a bare array, `forget` →
//! a bare bool, an id twin collapsing to one form). #1839 adds a deliberate
//! transport split: MCP extraction is a durable background receipt, while the
//! in-process bindings keep the synchronous domain call. A few entries carried
//! [`KNOWN_GAP`]: fields a binding really does lose. Writing a real gap down
//! is not blessing it — it is the only way the guard can be green on the 19
//! other tools while the gap stays visible in one place instead of being
//! rediscovered by a user. An entry is deleted by the fix, never renewed.
//!
//! As of issues #1690/#1691/#1692 the known-gap kind is EMPTY: every entry left
//! is a deliberate unwrap or contract split. That is the state this list is meant to reach, so
//! [`KNOWN_GAP`] now sits unused on purpose — see its own doc comment. Note
//! what it does NOT mean: nothing here caps how many gaps may be declared,
//! and adding a seventh would have been exactly as green as fixing six. The
//! pressure to close them was never mechanical.

#![cfg(all(feature = "mcp", feature = "context", feature = "persistence"))]

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

use rmcp::service::RunningService;
use rmcp::{RoleClient, ServiceExt};
use tempfile::TempDir;
use velesdb_memory::mcp::McpServer;
use velesdb_memory::{DynEmbedder, HashEmbedder, MemoryService, DEFAULT_DIMENSION};

/// One language binding of `velesdb-memory`, and where its published surface
/// lives.
struct Binding {
    /// Crate name, as it appears in a failure message.
    name: &'static str,
    /// Source file holding the published surface, relative to the workspace root.
    path: &'static str,
    /// The `impl` line that opens that surface. Everything up to the next
    /// column-0 `}` is the published block; nothing outside it counts.
    surface_impl: &'static str,
}

const BINDINGS: &[Binding] = &[
    Binding {
        name: "velesdb-node",
        path: "crates/velesdb-node/src/lib.rs",
        surface_impl: "impl MemoryStore {",
    },
    Binding {
        name: "velesdb-python",
        path: "crates/velesdb-python/src/agent_memory_service.rs",
        surface_impl: "impl PyMemoryService {",
    },
    Binding {
        name: "velesdb-wasm",
        path: "crates/velesdb-wasm/src/memory_service.rs",
        surface_impl: "impl WasmMemoryService {",
    },
];

/// A (tool, binding) pair that is deliberately NOT implemented.
///
/// An entry here is a decision on the record, not a to-do: it says the
/// absence was weighed and kept. The reason is mandatory and is printed
/// back by the stale-exemption check, so an entry that stops being true
/// cannot quietly survive.
struct Exemption {
    binding: &'static str,
    tool: &'static str,
    reason: &'static str,
}

const DAEMON_MIGRATION_ONLY: &str = "online migration owns the native daemon's live generation, process lock, durable control directory and environment-backed target factory; an in-process language binding owns none of that control plane, so exposing this MCP operation there would create a second unsafe migration authority";

const EXEMPTIONS: &[Exemption] = &[
    Exemption {
        binding: "velesdb-node",
        tool: "migration_start",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-node",
        tool: "migration_status",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-node",
        tool: "migration_cancel",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-node",
        tool: "migration_recover",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-python",
        tool: "migration_start",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-python",
        tool: "migration_status",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-python",
        tool: "migration_cancel",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-python",
        tool: "migration_recover",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-wasm",
        tool: "migration_start",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-wasm",
        tool: "migration_status",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-wasm",
        tool: "migration_cancel",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-wasm",
        tool: "migration_recover",
        reason: DAEMON_MIGRATION_ONLY,
    },
    Exemption {
        binding: "velesdb-node",
        tool: "extraction_status",
        reason: "this status surface exists for the MCP transport's durable background receipt; \
                 the Node binding keeps MemoryService::remember_extracted synchronous and returns \
                 its final ids directly, so it has no background request_id to query",
    },
    Exemption {
        binding: "velesdb-python",
        tool: "extraction_status",
        reason: "this status surface exists for the MCP transport's durable background receipt; \
                 the Python binding keeps MemoryService::remember_extracted synchronous and \
                 returns its final ids directly, so it has no background request_id to query",
    },
    Exemption {
        binding: "velesdb-wasm",
        tool: "extraction_status",
        reason: "WASM rememberExtracted is synchronous and in-memory, while durable extraction \
                 receipts require the native persistence directory that wasm32 deliberately omits",
    },
    Exemption {
        binding: "velesdb-wasm",
        tool: "feedback",
        reason: "a durable learned confidence is meaningless on the in-memory WASM backend: \
                 MemoryService::feedback lives in the `persistence`-gated `reinforce` module and \
                 is not compiled for wasm32 at all — exposing it would mean pulling \
                 NativeStore/filesystem code into the very bundle this binding exists to avoid",
    },
    Exemption {
        binding: "velesdb-wasm",
        tool: "list_memories",
        reason: "the WASM backend keeps MemoryStore::list default refusal - its in-memory \
                 store lives and dies with the page, so what-does-my-agent-know is answered \
                 by the caller own state, not by an audit walk",
    },
    Exemption {
        binding: "velesdb-wasm",
        tool: "memory_status",
        reason: "the WASM backend is in-memory and persistence-free: no provenance record, no \
                 store directory, no autograph worker — every block of the status except the \
                 fact count is structurally absent from that build",
    },
];

/// One `output_schema` root field a binding deliberately does NOT relay.
///
/// Twin of [`Exemption`], for the second invariant. Same contract: an entry
/// is a decision on the record, the reason is mandatory, and
/// [`no_shape_divergence_is_stale`] deletes it the moment it stops being
/// true. Unwrapping an envelope down to its one useful member is a
/// legitimate binding choice — LOSING a member is not, and only a declared
/// list can tell the two apart.
struct ShapeDivergence {
    binding: &'static str,
    tool: &'static str,
    field: &'static str,
    reason: &'static str,
}

/// The `<id>` / `<id>_str` twin pair collapses in a typed binding.
const ID_TWIN: &str = "deliberate unwrap of the id twin: the MCP wire carries an id BOTH as a \
     JSON number and as its decimal-string copy, because a u64 above 2^53 is lossy on a \
     float-lossy JSON client. A typed binding return has no such problem — it hands back one \
     form (a decimal string on JS, a native int on Python) and the twin has nothing to add";

/// A single-member envelope exists for the MCP transport, not for the domain.
const SINGLE_MEMBER: &str = "deliberate unwrap: the envelope carries exactly one useful member \
     and exists only because the MCP spec requires an object at the output-schema root — a \
     constraint of the transport, not of the domain. Nothing is lost";

/// `forget` answers with the one bit its envelope carries.
const FORGET_BOOL: &str = "deliberate unwrap: the binding returns the bare boolean, which IS \
     this envelope's `found` — whether a memory existed under that id and was deleted. The \
     echoed id adds nothing to a call the caller made with that id in hand";

/// The dated half of fused recall is a SEPARATE binding method.
const DATED_SPLIT: &str = "deliberate split, not a drop: the dated half of fused recall is a \
     SECOND binding method (`recallFusedDated`), which returns the timeline and the clock. \
     This method is the undated one, and returns the bare memories array";

/// MCP accepts a durable background job; in-process bindings finish inline.
const MCP_ASYNC_SPLIT: &str = "deliberate transport split, not an omitted implementation: the \
     MCP tool returns a durable background receipt because a model call can outlive its client \
     transport, while this in-process binding calls the transport-neutral MemoryService \
     synchronously and returns the committed ids plus skipped count directly. It therefore has \
     no MCP request_id/state/reused receipt; extraction_status is separately exempted for the \
     same reason (#1839)";

/// A field the server publishes and this binding really does lose. NOT a
/// deliberate unwrap — an entry here is an admission, kept honest by
/// [`no_shape_divergence_is_stale`], and it must be deleted by the fix, not
/// renewed.
///
/// **Currently unused, and that is the point.** The six entries that carried
/// it were deleted by their fixes (issues #1690, #1691, #1692): every field
/// the server publishes now reaches every binding, or is a declared,
/// motivated unwrap/contract split. The constant stays so the next honest admission has this
/// exact wording to reach for — deleting it would leave the next author to
/// invent a looser reason of their own, which is how a gap stops being
/// visible.
#[allow(
    dead_code,
    reason = "zero known gaps is the target state, not a reason to drop the word"
)]
const KNOWN_GAP: &str = "KNOWN GAP — NOT a deliberate unwrap: this binding really does lose \
     the field, and the loss predates the guard that found it. Declared so the guard can be \
     green on everything else while the gap stays visible in one place; delete this entry \
     with the fix, never renew it";

const SHAPE_DIVERGENCES: &[ShapeDivergence] = &[
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "entity",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "feedback",
        field: "id_str",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "forget",
        field: "found",
        reason: FORGET_BOOL,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "forget",
        field: "id_str",
        reason: FORGET_BOOL,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "recall_fused",
        field: "dated_context",
        reason: DATED_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "recall_fused",
        field: "memories",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "recall_where",
        field: "memories",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "relate",
        field: "edge_id",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "relate",
        field: "edge_id_str",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "remember",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "remember_extracted",
        field: "request_id",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "remember_extracted",
        field: "state",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "remember_extracted",
        field: "reused",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-node",
        tool: "save_working_context",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "entity",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "feedback",
        field: "id_str",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "forget",
        field: "found",
        reason: FORGET_BOOL,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "forget",
        field: "id_str",
        reason: FORGET_BOOL,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "recall_where",
        field: "memories",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "relate",
        field: "edge_id",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "relate",
        field: "edge_id_str",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "remember",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "remember_extracted",
        field: "request_id",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "remember_extracted",
        field: "state",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "remember_extracted",
        field: "reused",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-python",
        tool: "save_working_context",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "entity",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "remember_extracted",
        field: "request_id",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "remember_extracted",
        field: "state",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "remember_extracted",
        field: "reused",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "forget",
        field: "found",
        reason: FORGET_BOOL,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "forget",
        field: "id_str",
        reason: FORGET_BOOL,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "recall_fused",
        field: "dated_context",
        reason: DATED_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "recall_fused",
        field: "memories",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "recall_fused",
        field: "now",
        reason: DATED_SPLIT,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "recall_where",
        field: "memories",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "relate",
        field: "edge_id",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "relate",
        field: "edge_id_str",
        reason: SINGLE_MEMBER,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "remember",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: "velesdb-wasm",
        tool: "save_working_context",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: TYPESCRIPT_SDK,
        tool: "entity",
        field: "id_str",
        reason: ID_TWIN,
    },
    ShapeDivergence {
        binding: TYPESCRIPT_SDK,
        tool: "remember_extracted",
        field: "request_id",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: TYPESCRIPT_SDK,
        tool: "remember_extracted",
        field: "state",
        reason: MCP_ASYNC_SPLIT,
    },
    ShapeDivergence {
        binding: TYPESCRIPT_SDK,
        tool: "remember_extracted",
        field: "reused",
        reason: MCP_ASYNC_SPLIT,
    },
];

/// The shape divergence covering `(binding, tool, field)`, if one is declared.
fn shape_divergence_for(
    binding: &str,
    tool: &str,
    field: &str,
) -> Option<&'static ShapeDivergence> {
    SHAPE_DIVERGENCES
        .iter()
        .find(|d| d.binding == binding && d.tool == tool && d.field == field)
}

/// Boot the real `McpServer` over an in-memory duplex pipe and complete the
/// MCP handshake — same fixture as `mcp_schema_bdd.rs`, so both files read
/// the surface a real harness is served.
async fn connected() -> (TempDir, RunningService<RoleClient, ()>) {
    let store_dir = tempfile::tempdir().expect("create scratch store dir");
    let embedder: DynEmbedder = Box::new(HashEmbedder::new(DEFAULT_DIMENSION));
    let service =
        MemoryService::open(store_dir.path(), embedder).expect("open scratch memory store");
    let (server_side, client_side) = tokio::io::duplex(1 << 20);
    tokio::spawn(async move {
        if let Ok(running) = McpServer::new(service).serve(server_side).await {
            let _ = running.waiting().await;
        }
    });
    let client = ().serve(client_side).await.expect("MCP initialize handshake over duplex");
    (store_dir, client)
}

/// The workspace root: this crate is `<root>/crates/velesdb-memory`.
fn workspace_root() -> PathBuf {
    let manifest = Path::new(env!("CARGO_MANIFEST_DIR"));
    manifest
        .ancestors()
        .nth(2)
        .expect("velesdb-memory sits two levels under the workspace root")
        .to_path_buf()
}

/// The body of the ONE `impl` block that publishes `binding`'s surface.
///
/// Bounded by the block's own column-0 closing brace, which `rustfmt`
/// guarantees — so a `fn` in a nested test module or a free helper further
/// down the file is never mistaken for a published method.
fn surface_block(binding: &Binding) -> String {
    let path = workspace_root().join(binding.path);
    let source = std::fs::read_to_string(&path)
        .unwrap_or_else(|err| panic!("read {} ({}): {err}", binding.path, path.display()));
    let mut lines = source
        .lines()
        .skip_while(|l| l.trim() != binding.surface_impl);
    assert!(
        lines.next().is_some(),
        "{} no longer contains `{}` — the parity guard reads that block to know what the \
         binding publishes; point `Binding::surface_impl` at the renamed block",
        binding.path,
        binding.surface_impl,
    );
    lines
        .take_while(|l| *l != "}")
        .collect::<Vec<_>>()
        .join("\n")
}

/// Method names published by `binding`, taken from its surface block.
fn published_methods(binding: &Binding) -> BTreeSet<String> {
    let block = surface_block(binding);
    let methods: BTreeSet<String> = block
        .lines()
        .filter_map(|line| method_name(line.trim()))
        .collect();
    assert!(
        methods.contains("remember"),
        "{}: the surface block parsed to {} method(s) and none of them is `remember` — the \
         scan is broken, not the binding",
        binding.name,
        methods.len(),
    );
    methods
}

/// The identifier of a method declaration line, if the line is one.
fn method_name(trimmed: &str) -> Option<String> {
    let rest = trimmed
        .strip_prefix("pub fn ")
        .or_else(|| trimmed.strip_prefix("fn "))?;
    let (name, _) = rest.split_once('(')?;
    (!name.is_empty()).then(|| name.to_owned())
}

// --- The one published surface that is not Rust ------------------------------
//
// The TypeScript SDK wraps the WASM binding and shipped 17 of its methods for
// its whole life, missing `entity` and `unrelate` (issue #1721). Nothing saw
// it: `BINDINGS` holds three Rust crates and the reader above is a Rust
// parser — `surface_impl` looks for `impl X {`, `method_name` for `pub fn`,
// and `published_methods` asserts its way out of a scan that finds nothing.
// Handing it a `.ts` file would PANIC, not report, so "just add a fourth
// BINDINGS entry" is not available.
//
// What IS available is the chain. The SDK cannot expose a tool the WASM
// binding does not — it calls into it. So the invariant is not "the SDK
// implements every MCP tool" (that would duplicate wasm's `feedback`
// exemption into a second list, and a duplicated decision is a decision that
// will drift); it is "the SDK relays every tool that REACHES the WASM
// binding". Composed with the guard above, which holds wasm against the live
// tool list, the two links prove the whole path from server to SDK.

/// Where the TypeScript SDK declares what it publishes.
///
/// Two blocks in one file, and BOTH matter: `ensureCapability` is typed
/// `keyof WasmMemoryServiceInstance`, so a method added to the class without
/// the interface does not compile, and one added to the interface alone is a
/// promise no caller can reach.
const SDK_SOURCE: &str = "sdks/typescript/src/memory.ts";
const SDK_CLASS: &str = "export class MemoryService {";
const SDK_INTERFACE: &str = "interface WasmMemoryServiceInstance {";
const TYPESCRIPT_SDK: &str = "typescript-sdk";

/// One SDK method whose return is not a local interface, so root-field
/// inspection is impossible. The exact type and the reason are both pinned:
/// changing the method to a local interface makes the entry stale and red.
struct SdkShapeSkip {
    tool: &'static str,
    return_type: &'static str,
    reason: &'static str,
}

const SDK_SHAPE_SKIPS: &[SdkShapeSkip] = &[
    SdkShapeSkip {
        tool: "forget",
        return_type: "boolean",
        reason: FORGET_BOOL,
    },
    SdkShapeSkip {
        tool: "recall",
        return_type: "MemoryRecollection[]",
        reason: SINGLE_MEMBER,
    },
    SdkShapeSkip {
        tool: "recall_fused",
        return_type: "MemoryRecollection[]",
        reason: DATED_SPLIT,
    },
    SdkShapeSkip {
        tool: "recall_where",
        return_type: "MemoryRecollection[]",
        reason: SINGLE_MEMBER,
    },
    SdkShapeSkip {
        tool: "relate",
        return_type: "string",
        reason: ID_TWIN,
    },
    SdkShapeSkip {
        tool: "remember",
        return_type: "string",
        reason: ID_TWIN,
    },
    SdkShapeSkip {
        tool: "save_working_context",
        return_type: "string",
        reason: ID_TWIN,
    },
];

/// The identifier of a TypeScript method declaration, if the line is one.
///
/// Deliberately shallow: exactly one indent level (so a call inside a body
/// never counts) and not `private`. It over-collects — `init`, `close` and
/// `constructor` are not tools — which is harmless, because every caller
/// below iterates over TOOL names and asks whether the SDK has them, never
/// the reverse.
fn typescript_method(line: &str) -> Option<&str> {
    let declaration = line.strip_prefix("  ")?;
    if declaration.starts_with(' ') || declaration.starts_with("private ") {
        return None;
    }
    let (name, _) = declaration.split_once('(')?;
    if name.is_empty() || !name.chars().all(|c| c.is_alphanumeric() || c == '_') {
        return None;
    }
    Some(name)
}

/// The methods declared between `opening` and the next column-0 `}`.
fn sdk_methods(opening: &str) -> BTreeSet<String> {
    let path = workspace_root().join(SDK_SOURCE);
    let source = std::fs::read_to_string(&path)
        .unwrap_or_else(|err| panic!("read {SDK_SOURCE} ({}): {err}", path.display()));
    let mut lines = source.lines().skip_while(|l| l.trim() != opening);
    assert!(
        lines.next().is_some(),
        "{SDK_SOURCE} no longer contains `{opening}` — the SDK parity check reads that block to \
         know what the SDK publishes; point the constant at the renamed block",
    );
    let methods: BTreeSet<String> = lines
        .take_while(|l| *l != "}")
        .filter_map(typescript_method)
        .map(str::to_owned)
        .collect();
    assert!(
        methods.contains("remember"),
        "{SDK_SOURCE} `{opening}` parsed to {} method(s) and none of them is `remember` — the \
         scan is broken, not the SDK",
        methods.len(),
    );
    methods
}

/// A `snake_case` tool name as every JavaScript surface spells it.
///
/// The Rust bindings name their methods after the tool verbatim and rename in
/// an attribute (`#[napi(js_name = ...)]`, `#[wasm_bindgen(js_name = ...)]`);
/// the SDK is written in the target language, so the `camelCase` happens in
/// the identifier itself and the comparison has to cross that gap.
fn js_name(tool: &str) -> String {
    let mut out = String::with_capacity(tool.len());
    let mut capitalize = false;
    for c in tool.chars() {
        if c == '_' {
            capitalize = true;
        } else if capitalize {
            out.extend(c.to_uppercase());
            capitalize = false;
        } else {
            out.push(c);
        }
    }
    out
}

/// Tools the WASM binding publishes that the SDK never declares, named.
///
/// Pure, so the refusal itself is testable without a server: see
/// [`the_sdk_check_names_the_method_a_stripped_sdk_lost`].
fn sdk_gaps(
    tools: &BTreeSet<String>,
    upstream: &BTreeSet<String>,
    class: &BTreeSet<String>,
    interface: &BTreeSet<String>,
) -> Vec<String> {
    let mut gaps = Vec::new();
    for tool in tools {
        if !upstream.contains(tool) {
            // The tool does not reach the WASM binding either. That is the
            // first guard's business, and wasm's exemption governs both.
            continue;
        }
        let published = js_name(tool);
        let in_class = class.contains(&published);
        let in_interface = interface.contains(&published);
        if in_class && in_interface {
            continue;
        }
        let where_missing = match (in_class, in_interface) {
            (false, false) => "neither the class nor the interface",
            (true, false) => "the class but NOT the interface (`ensureCapability` keys off it)",
            (false, true) => "the interface but NOT the class (no caller can reach it)",
            (true, true) => unreachable!("handled above"),
        };
        gaps.push(format!(
            "  `{tool}` reaches velesdb-wasm and the SDK declares `{published}` in \
             {where_missing}"
        ));
    }
    gaps
}

/// Public SDK method regions, starting at the declaration and ending before
/// the next public declaration. Private helpers are intentionally absorbed
/// into their caller's region; only the signature is inspected below.
fn sdk_method_regions() -> BTreeMap<String, String> {
    let path = workspace_root().join(SDK_SOURCE);
    let source = std::fs::read_to_string(&path)
        .unwrap_or_else(|err| panic!("read {SDK_SOURCE} ({}): {err}", path.display()));
    let mut lines = source.lines().skip_while(|line| line.trim() != SDK_CLASS);
    assert!(
        lines.next().is_some(),
        "{SDK_SOURCE} no longer contains `{SDK_CLASS}`"
    );
    let block: Vec<&str> = lines.take_while(|line| *line != "}").collect();
    let starts: Vec<(usize, String)> = block
        .iter()
        .enumerate()
        .filter_map(|(index, line)| typescript_method(line).map(|name| (index, name.to_owned())))
        .collect();
    let mut regions = BTreeMap::new();
    for (position, (start, name)) in starts.iter().enumerate() {
        let end = starts
            .get(position + 1)
            .map_or(block.len(), |(index, _)| *index);
        regions.insert(name.clone(), block[*start..end].join("\n"));
    }
    regions
}

/// The `X` inside one method declaration's `Promise<X>`, including nested
/// generic brackets. Non-Promise methods return `None`.
fn promise_return_type(region: &str) -> Option<String> {
    let flat = region.split_whitespace().collect::<Vec<_>>().join(" ");
    let (_, tail) = flat.split_once("): Promise<")?;
    let mut depth = 1_u32;
    for (index, character) in tail.char_indices() {
        match character {
            '<' => depth += 1,
            '>' => depth = depth.saturating_sub(1),
            _ => {}
        }
        if depth == 0 {
            return Some(tail[..index].trim().to_owned());
        }
    }
    None
}

/// Declared Promise return type of every public SDK method.
fn sdk_return_types() -> BTreeMap<String, String> {
    sdk_method_regions()
        .into_iter()
        .filter_map(|(name, region)| {
            promise_return_type(&region).map(|return_type| (name, return_type))
        })
        .collect()
}

fn typescript_interface_name(line: &str) -> Option<&str> {
    let declaration = line
        .strip_prefix("export interface ")
        .or_else(|| line.strip_prefix("interface "))?;
    let name = declaration
        .split(|character: char| character.is_whitespace() || character == '{')
        .next()?;
    (!name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_')).then_some(name)
}

fn typescript_field(line: &str) -> Option<String> {
    let declaration = line.strip_prefix("  ")?;
    if declaration.starts_with(' ') {
        return None;
    }
    let (name, _) = declaration.split_once(':')?;
    let name = name.trim_end_matches('?');
    name.chars()
        .all(|c| c.is_alphanumeric() || c == '_')
        .then(|| name.to_owned())
}

/// Root fields of every interface declared locally in `memory.ts`.
fn sdk_interfaces() -> BTreeMap<String, BTreeSet<String>> {
    let path = workspace_root().join(SDK_SOURCE);
    let source = std::fs::read_to_string(&path)
        .unwrap_or_else(|err| panic!("read {SDK_SOURCE} ({}): {err}", path.display()));
    let lines: Vec<&str> = source.lines().collect();
    let declarations: Vec<(usize, &str)> = lines
        .iter()
        .enumerate()
        .filter_map(|(index, line)| typescript_interface_name(line).map(|name| (index, name)))
        .collect();
    declarations
        .into_iter()
        .map(|(start, name)| {
            let fields = lines[start + 1..]
                .iter()
                .take_while(|line| **line != "}")
                .filter_map(|line| typescript_field(line))
                .collect();
            (name.to_owned(), fields)
        })
        .collect()
}

fn sdk_shape_skip_for(tool: &str, return_type: &str) -> Option<&'static SdkShapeSkip> {
    SDK_SHAPE_SKIPS
        .iter()
        .find(|skip| skip.tool == tool && skip.return_type == return_type)
}

fn sdk_names_field(fields: &BTreeSet<String>, wire_field: &str) -> bool {
    fields.contains(wire_field) || fields.contains(&js_name(wire_field))
}

fn sdk_output_gaps(
    tool: &str,
    output_fields: &BTreeSet<String>,
    sdk_fields: &BTreeSet<String>,
) -> Vec<String> {
    output_fields
        .iter()
        .filter(|field| {
            !sdk_names_field(sdk_fields, field)
                && shape_divergence_for(TYPESCRIPT_SDK, tool, field).is_none()
        })
        .map(|field| {
            format!("  {tool}.{field} is not named by the TypeScript SDK return interface")
        })
        .collect()
}

fn sdk_shape_findings(tools: &[rmcp::model::Tool], upstream: &BTreeSet<String>) -> Vec<String> {
    let returns = sdk_return_types();
    let interfaces = sdk_interfaces();
    let mut findings = Vec::new();
    for tool in tools {
        let name = tool.name.as_ref();
        if !upstream.contains(name) {
            continue;
        }
        let Some(return_type) = returns.get(&js_name(name)) else {
            continue; // Method absence is the name guard's responsibility.
        };
        if let Some(fields) = interfaces.get(return_type) {
            findings.extend(sdk_output_gaps(name, &output_root_fields(tool), fields));
        } else if sdk_shape_skip_for(name, return_type).is_none() {
            findings.push(format!(
                "  {name} returns non-local `{return_type}` without an explicit SDK_SHAPE_SKIPS decision"
            ));
        }
    }
    findings
}

fn stale_sdk_skip_reason(
    skip: &SdkShapeSkip,
    tools: &BTreeSet<String>,
    upstream: &BTreeSet<String>,
    returns: &BTreeMap<String, String>,
    interfaces: &BTreeMap<String, BTreeSet<String>>,
) -> Option<String> {
    if skip.reason.trim().is_empty() {
        return Some("the reason is empty".to_owned());
    }
    if !tools.contains(skip.tool) || !upstream.contains(skip.tool) {
        return Some("the tool no longer reaches the TypeScript SDK".to_owned());
    }
    let method = js_name(skip.tool);
    let Some(actual) = returns.get(&method) else {
        return Some(format!(
            "the SDK no longer declares `{method}` with a Promise return"
        ));
    };
    if actual != skip.return_type {
        return Some(format!(
            "the return changed from `{}` to `{actual}`",
            skip.return_type
        ));
    }
    interfaces
        .contains_key(actual)
        .then(|| format!("`{actual}` is now a local interface and must be checked"))
}

/// The source region publishing each method of `binding`, keyed by method
/// name: everything from the end of the previous method through this one's
/// closing brace — so the doc comment and the `#[napi]` / `#[wasm_bindgen]`
/// / `#[pyo3]` attributes that carry the declared return type are included,
/// not just the body.
///
/// Bounded by the 4-space-indented `}` that `rustfmt` puts at the end of a
/// method inside an `impl` block.
fn method_regions(binding: &Binding) -> BTreeMap<String, String> {
    let block = surface_block(binding);
    let lines: Vec<&str> = block.lines().collect();
    let mut regions = BTreeMap::new();
    let mut start = 0usize;
    for (index, line) in lines.iter().enumerate() {
        let Some(name) = method_name(line.trim()) else {
            continue;
        };
        if index < start {
            // A nested `fn` inside the method already consumed — a closure
            // or a local helper, not a published method.
            continue;
        }
        let end = lines[index..]
            .iter()
            .position(|l| *l == "    }")
            .map_or(lines.len(), |offset| index + offset + 1);
        regions.insert(name, lines[start..end].join("\n"));
        start = end;
    }
    assert!(
        regions.contains_key("remember"),
        "{}: the region scan produced {} method(s) and none of them is `remember` — the \
         scan is broken, not the binding",
        binding.name,
        regions.len(),
    );
    regions
}

/// `region` with the declared method's PARAMETER LIST cut out — the window
/// route 2 must read.
///
/// A region runs from the end of the previous method to this one's closing
/// brace, so it carries the signature, and the signature carries the
/// parameter NAMES. Route 2 is a text search: an output field called `handle`
/// was therefore satisfied by an INPUT parameter called `handle`, which
/// proves the binding accepts that name, not that it relays it (#1704). The
/// return type sits outside the parentheses and is deliberately kept.
///
/// Falls back to the whole region when the declaration cannot be located —
/// a parser that silently narrows its own window would be the same class of
/// defect one level up.
fn output_window(region: &str, method: &str) -> String {
    cut_parameter_list(&cut_signature_attribute(region), method)
}

/// `region` without the parameter echo of a `#[pyo3(signature = (...))]`
/// attribute.
///
/// The same defect by another road: the attribute sits BEFORE the `fn` line,
/// so cutting the parentheses of the declaration alone leaves every parameter
/// name spelled a second time, and route 2 reads it just as happily.
fn cut_signature_attribute(region: &str) -> String {
    let mut text = region.to_owned();
    while let Some(at) = text.find("signature = (") {
        let open = at + "signature = ".len();
        let Some(close) = matching_paren(&text[open..]).map(|offset| open + offset) else {
            break;
        };
        text.replace_range(open..=close, "");
    }
    text
}

/// `region` with the parameter list of `method`'s declaration cut out.
fn cut_parameter_list(region: &str, method: &str) -> String {
    let mut at = 0usize;
    let mut declaration = None;
    for line in region.split_inclusive('\n') {
        // The same parser that built the regions, not a text search: a doc
        // comment mentioning `fn recall(` must not be taken for the header.
        if method_name(line.trim()).as_deref() == Some(method) {
            declaration = Some(at);
            break;
        }
        at += line.len();
    }
    let Some(header) = declaration else {
        return region.to_owned();
    };
    let Some(open) = region[header..].find('(').map(|offset| header + offset) else {
        return region.to_owned();
    };
    let Some(close) = matching_paren(&region[open..]).map(|offset| open + offset) else {
        return region.to_owned();
    };
    let mut window = region[..open].to_owned();
    window.push_str(&region[close + 1..]);
    window
}

/// `region` with every `///` doc-comment line removed.
///
/// Route 2's window keeps doc comments — deliberately, per the module
/// header's "What this guard does NOT prove": a field named only in prose is
/// an accepted weak pass for "is the field DECLARED". The stale check (below)
/// asks a different question, "did the RENDERED form change", which prose
/// cannot answer either way — so it reads this stricter window instead
/// (#1760: documenting `recall_where`'s contract as "Returns caller memories
/// ONLY" made a still-true [`ShapeDivergence`] for the field `memories` look
/// stale, because the sentence, not the return value, named it).
fn without_doc_comments(region: &str) -> String {
    region
        .lines()
        .filter(|line| !line.trim_start().starts_with("///"))
        .collect::<Vec<_>>()
        .join("\n")
}

/// The offset, within `text`, of the `)` closing the `(` that `text` opens
/// with — `None` when it never closes.
///
/// Depth-counted, not `find(')')`: a parameter typed `Wrapped<(u8, u8)>`
/// closes an inner pair first, and cutting there would leave half a
/// parameter list in the window.
fn matching_paren(text: &str) -> Option<usize> {
    let mut depth = 0usize;
    for (offset, character) in text.char_indices() {
        match character {
            '(' => depth += 1,
            ')' => {
                depth -= 1;
                if depth == 0 {
                    return Some(offset);
                }
            }
            _ => {}
        }
    }
    None
}

/// The ROOT property names of `tool`'s advertised `output_schema`, empty
/// when the tool advertises none.
fn output_root_fields(tool: &rmcp::model::Tool) -> BTreeSet<String> {
    tool.output_schema
        .as_ref()
        .and_then(|schema| schema.get("properties"))
        .and_then(serde_json::Value::as_object)
        .map(|props| props.keys().cloned().collect())
        .unwrap_or_default()
}

/// Root input property names from the live MCP schema.
fn input_root_fields(tool: &rmcp::model::Tool) -> BTreeSet<String> {
    tool.input_schema
        .get("properties")
        .and_then(serde_json::Value::as_object)
        .map(|props| props.keys().cloned().collect())
        .unwrap_or_default()
}

/// The parameter list of `method`'s Rust declaration inside its source region.
fn input_window(region: &str, method: &str) -> String {
    let Some(header) = region
        .lines()
        .position(|line| method_name(line.trim()).as_deref() == Some(method))
    else {
        return String::new();
    };
    let declaration = region.lines().skip(header).collect::<Vec<_>>().join("\n");
    let Some(open) = declaration.find('(') else {
        return String::new();
    };
    let Some(close) = matching_paren(&declaration[open..]).map(|offset| open + offset) else {
        return String::new();
    };
    declaration[open + 1..close].to_owned()
}

#[tokio::test]
async fn recall_count_input_is_k_on_the_server_and_every_binding() {
    let (_store, client) = connected().await;
    let tools = client.list_all_tools().await.expect("list tools");
    let recall_tools = ["recall", "recall_where", "recall_fused"];
    let mut gaps = Vec::new();
    for name in recall_tools {
        let tool = tools
            .iter()
            .find(|tool| tool.name.as_ref() == name)
            .unwrap_or_else(|| panic!("live server no longer advertises `{name}`"));
        let fields = input_root_fields(tool);
        if !fields.contains("k") || fields.contains("limit") {
            gaps.push(format!(
                "  {name} advertises {fields:?}, expected canonical `k`"
            ));
        }
        for binding in BINDINGS {
            let regions = method_regions(binding);
            let params = regions
                .get(name)
                .map_or_else(String::new, |region| input_window(region, name));
            if !names_identifier(&params, "k") {
                gaps.push(format!("  {}.{name} does not accept `k`", binding.name));
            }
        }
    }
    assert!(
        gaps.is_empty(),
        "shared recall count input drifted across surfaces:\n{}",
        gaps.join("\n")
    );
    client.cancel().await.expect("close the MCP session");
}

/// The server source files declaring the tools, scanned for the Rust type
/// each tool's `output_schema` is derived from.
const SERVER_TOOL_SOURCES: &[&str] = &[
    "crates/velesdb-memory/src/mcp.rs",
    "crates/velesdb-memory/src/mcp/advanced_tools.rs",
    "crates/velesdb-memory/src/mcp/context_tools.rs",
];

/// Tool name → the Rust type the server publishes as that tool's output
/// shape, read from its `output_schema = wire_safe_output_schema::<T>()`
/// declaration in the server source.
///
/// Why the TYPE and not only the field list: a binding that hands back the
/// server's own type cannot lose a field — not now, and not when the type
/// grows one. Naming the type is therefore a complete answer to "do you
/// relay this shape?", where naming today's fields would only be an answer
/// for today.
fn server_output_types() -> BTreeMap<String, String> {
    let mut types = BTreeMap::new();
    for relative in SERVER_TOOL_SOURCES {
        let path = workspace_root().join(relative);
        let source = std::fs::read_to_string(&path)
            .unwrap_or_else(|err| panic!("read {} ({}): {err}", relative, path.display()));
        collect_output_types(&source, &mut types);
    }
    assert!(
        types.len() >= 20,
        "only {} tool output type(s) parsed out of the server source — the scan is broken, \
         not the server (it publishes 27 tools)",
        types.len(),
    );
    types
}

/// Pair each `name = "<tool>"` line of a server source file with the
/// `wire_safe_output_schema::<T>()` that follows it inside the same `#[tool(
/// ... )]` attribute.
fn collect_output_types(source: &str, types: &mut BTreeMap<String, String>) {
    let mut pending: Option<String> = None;
    for line in source.lines() {
        let trimmed = line.trim();
        if let Some(tool) = between(trimmed, "name = \"", "\"") {
            pending = Some(tool.to_owned());
        } else if let Some(ty) = between(trimmed, "wire_safe_output_schema::<", ">") {
            if let Some(tool) = pending.take() {
                types.insert(tool, ty.to_owned());
            }
        }
    }
}

/// The text between the first `open` and the next `close` after it.
fn between<'a>(haystack: &'a str, open: &str, close: &str) -> Option<&'a str> {
    let rest = haystack.split_once(open)?.1;
    let (inner, _) = rest.split_once(close)?;
    (!inner.is_empty()).then_some(inner)
}

/// Whether `region` names `needle` as a WHOLE identifier — never as a
/// prefix, a suffix or an inner fragment.
///
/// Whole-word matters both ways here: `CompiledContext` must NOT be
/// satisfied by the Node binding's own mirror DTO `CompiledContextJs`, which
/// is a different type that really does drop a field (`warnings`); and
/// `found` must not be satisfied by the word `founded` in a comment.
fn names_identifier(region: &str, needle: &str) -> bool {
    region.match_indices(needle).any(|(at, _)| {
        !bounded_by_ident_char(&region[..at], true)
            && !bounded_by_ident_char(&region[at + needle.len()..], false)
    })
}

/// Whether `text` touches the match on an identifier character — looking
/// backwards from its end when `before`, forwards from its start otherwise.
fn bounded_by_ident_char(text: &str, before: bool) -> bool {
    let adjacent = if before {
        text.chars().next_back()
    } else {
        text.chars().next()
    };
    adjacent.is_some_and(|c| c.is_alphanumeric() || c == '_')
}

/// Every `struct <Name> { ... }` declared in `binding`'s crate, name → body.
///
/// Used for ONE hop out of the method region: a binding that returns its own
/// mirror DTO (the Node idiom — `CompiledContextJs`, `EntityProfileJs`)
/// declares the relayed shape in that struct, not at the method. Following
/// that hop is what turns "Node never names `sections`" into the one finding
/// that is actually true: Node never relays `warnings`.
fn binding_structs(binding: &Binding) -> BTreeMap<String, String> {
    let crate_src = workspace_root()
        .join(binding.path)
        .parent()
        .expect("a binding surface file lives in the crate's src/")
        .to_path_buf();
    let mut structs = BTreeMap::new();
    for entry in std::fs::read_dir(&crate_src).expect("read the binding crate's src/") {
        let path = entry.expect("read a src/ entry").path();
        if path.extension().is_some_and(|ext| ext == "rs") {
            let source = std::fs::read_to_string(&path).expect("read a binding source file");
            collect_structs(&source, &mut structs);
        }
    }
    structs
}

/// Pair each `struct X {` line with the lines up to its column-0 closing
/// brace — `rustfmt` guarantees that brace for a top-level item.
fn collect_structs(source: &str, structs: &mut BTreeMap<String, String>) {
    let lines: Vec<&str> = source.lines().collect();
    for (index, line) in lines.iter().enumerate() {
        let Some(name) = between(line, "struct ", " {") else {
            continue;
        };
        if !name.chars().all(|c| c.is_alphanumeric() || c == '_') {
            continue; // A generic or `where`-bounded declaration; not a wire DTO.
        }
        let end = lines[index..]
            .iter()
            .position(|l| *l == "}")
            .map_or(lines.len(), |offset| index + offset);
        structs.insert(name.to_owned(), lines[index..end].join("\n"));
    }
}

/// `region`, plus the body of every binding-local struct it names — the one
/// hop described on [`binding_structs`].
fn region_with_named_structs(region: &str, structs: &BTreeMap<String, String>) -> String {
    let mut text = region.to_owned();
    for (name, body) in structs {
        if names_identifier(region, name) {
            text.push('\n');
            text.push_str(body);
        }
    }
    text
}

/// The exemption covering `(binding, tool)`, if one is declared.
fn exemption_for(binding: &str, tool: &str) -> Option<&'static Exemption> {
    EXEMPTIONS
        .iter()
        .find(|e| e.binding == binding && e.tool == tool)
}

/// THE guard: no MCP tool may reach a binding without either an
/// implementation or a declared, motivated exemption.
#[tokio::test]
async fn every_mcp_tool_is_implemented_or_exempted_in_every_binding() {
    let (_store, client) = connected().await;
    let tools = client.list_all_tools().await.expect("list tools");
    assert!(!tools.is_empty(), "the server advertises at least one tool");

    let mut gaps: Vec<String> = Vec::new();
    for binding in BINDINGS {
        let methods = published_methods(binding);
        for tool in &tools {
            let name = tool.name.as_ref();
            if methods.contains(name) || exemption_for(binding.name, name).is_some() {
                continue;
            }
            gaps.push(format!("  {name} is missing from {}", binding.name));
        }
    }

    assert!(
        gaps.is_empty(),
        "{} MCP tool(s) unreachable from a binding:\n{}\n\nEvery MCP tool must be either \
         implemented in the binding (relay `MemoryService`, add no logic — follow the idiom of \
         a neighbouring tool such as `why`) or declared in `EXEMPTIONS` in this file WITH the \
         reason it does not apply there. Silence is not a decision: the `entity` tool was \
         invisible from all three bindings for its whole life precisely because nothing \
         compared the surfaces.",
        gaps.len(),
        gaps.join("\n"),
    );
    client.cancel().await.expect("close the MCP session");
}

/// An exemption that stopped being true must be deleted, not left to rot:
/// a stale entry is a hole in the guard that looks like a decision.
#[tokio::test]
async fn no_exemption_is_stale() {
    let (_store, client) = connected().await;
    let tools = client.list_all_tools().await.expect("list tools");
    let advertised: BTreeSet<&str> = tools.iter().map(|t| t.name.as_ref()).collect();

    let mut stale: Vec<String> = Vec::new();
    for exemption in EXEMPTIONS {
        let Some(binding) = BINDINGS.iter().find(|b| b.name == exemption.binding) else {
            stale.push(format!(
                "  unknown binding `{}` (exempting `{}`)",
                exemption.binding, exemption.tool
            ));
            continue;
        };
        if !advertised.contains(exemption.tool) {
            stale.push(format!(
                "  `{}` is no longer an MCP tool (exempted on {}: {})",
                exemption.tool, exemption.binding, exemption.reason
            ));
        } else if published_methods(binding).contains(exemption.tool) {
            stale.push(format!(
                "  {} now implements `{}` — drop the exemption (it claimed: {})",
                exemption.binding, exemption.tool, exemption.reason
            ));
        }
    }

    assert!(
        stale.is_empty(),
        "{} stale exemption(s) in EXEMPTIONS:\n{}",
        stale.len(),
        stale.join("\n"),
    );
    client.cancel().await.expect("close the MCP session");
}

/// THE second guard: every ROOT field of a tool's advertised
/// `output_schema` must reach the binding — either because the binding
/// names the SERVER'S OWN output type (then it cannot lose a field, today
/// or after the type grows one), or because it names that field, or because
/// the drop is a declared, motivated [`ShapeDivergence`].
///
/// Read the module header's "What this guard does NOT prove" before relying
/// on a green run: this is a text search, and a field named only in a
/// comment satisfies it.
#[tokio::test]
async fn every_output_field_is_relayed_or_divergence_is_declared_in_every_binding() {
    let (_store, client) = connected().await;
    let tools = client.list_all_tools().await.expect("list tools");
    let server_types = server_output_types();

    let mut gaps: Vec<String> = Vec::new();
    for binding in BINDINGS {
        let regions = method_regions(binding);
        let structs = binding_structs(binding);
        for tool in &tools {
            let name = tool.name.as_ref();
            let Some(region) = regions.get(name) else {
                continue; // Absence is the FIRST guard's business, not this one.
            };
            if server_types
                .get(name)
                .is_some_and(|ty| names_identifier(region, ty))
            {
                continue; // Relays the server's own type: shape-complete by construction.
            }
            // Route 2 reads the region MINUS the parameter list: an input
            // named like an output field is not a relay of it (#1704).
            // Route 1 above keeps the whole region — it matches a type name,
            // and a type named only in the parameters is an input type.
            let window = output_window(region, name);
            let declared = region_with_named_structs(&window, &structs);
            for field in output_root_fields(tool) {
                if names_identifier(&declared, &field)
                    || shape_divergence_for(binding.name, name, &field).is_some()
                {
                    continue;
                }
                gaps.push(format!(
                    "  {}.{field} is not relayed by {}",
                    name, binding.name
                ));
            }
        }
    }

    assert!(
        gaps.is_empty(),
        "{} output field(s) the server publishes but a binding never names:\n{}\n\nEvery \
         root field of a tool's output_schema must reach the binding: name the server's \
         output type, or name the field in the method region (doc comment, attributes or \
         body), or declare the drop in SHAPE_DIVERGENCES in this file WITH its reason. This \
         is the invariant that was missing when `load_working_context` served \
         `{{found, working, other_sessions}}` for months while all three bindings returned a \
         bare `WorkingContext | null`: the name guard was green, and nobody was looking at \
         the shape.",
        gaps.len(),
        gaps.join("\n"),
    );
    client.cancel().await.expect("close the MCP session");
}

/// Tools whose bindings must satisfy the shape guard by ROUTE 1 — naming the
/// server's own output type — and never by the text-search fallback.
///
/// Route 2 is a text search: prose in a doc comment satisfies it, and so does
/// a `ts_return_type` string whose body returns something else. That is an
/// accepted limit for the file at large (see the module header), but it is
/// NOT acceptable for the tool whose silent shape drift is the entire reason
/// this guard exists. `load_working_context` served `{found, working,
/// other_sessions}` for months while all three bindings handed back a bare
/// `WorkingContext | null`; every one of those bindings carried a doc comment
/// describing the tool, so a text search would have been green throughout.
///
/// Route 1 is different in kind: the annotation `let loaded:
/// LoadedWorkingContext = …` does not describe the relay, it IS the relay,
/// and the compiler rejects it the moment the binding stops returning that
/// type. Prose cannot lie its way past a type check.
const RELAY_BY_TYPE_ONLY: &[&str] = &["load_working_context"];

/// Route 1 is applicable — and applied — for every tool in
/// [`RELAY_BY_TYPE_ONLY`], in every binding that implements it.
///
/// Fails when a binding relays the envelope without naming its type, even
/// though the field-by-field fallback would have passed it on the strength of
/// its doc comment alone.
#[tokio::test]
async fn envelope_tools_are_relayed_by_type_never_by_prose_alone() {
    let server_types = server_output_types();

    let mut weak: Vec<String> = Vec::new();
    for binding in BINDINGS {
        let regions = method_regions(binding);
        for tool in RELAY_BY_TYPE_ONLY {
            let Some(region) = regions.get(*tool) else {
                continue; // Absence is the first guard's business, not this one.
            };
            let ty = server_types
                .get(*tool)
                .unwrap_or_else(|| panic!("no server output type parsed for `{tool}`"));
            if !names_identifier(region, ty) {
                weak.push(format!(
                    "  {}.{tool} never names `{ty}` — it relays the envelope without \
                     declaring its type",
                    binding.name
                ));
            }
        }
    }

    assert!(
        weak.is_empty(),
        "{} binding(s) satisfy the shape guard only by text search:\n{}\n\nThese tools must \
         name the server's own output type (route 1), because a doc comment describing the \
         envelope is enough to satisfy route 2 while the body returns the bare form — which \
         is exactly the drift that went unnoticed for months. Bind the value with an explicit \
         annotation, e.g. `let loaded: LoadedWorkingContext = \
         svc.resume_working_context(..)?;`, so the compiler enforces what this test reads.",
        weak.len(),
        weak.join("\n"),
    );
}

/// A shape divergence that stopped being true must be deleted, not left to
/// rot — same reason as [`no_exemption_is_stale`]: a stale entry is a hole
/// in the guard that looks like a decision.
#[tokio::test]
async fn no_shape_divergence_is_stale() {
    let (_store, client) = connected().await;
    let tools = client.list_all_tools().await.expect("list tools");

    let mut stale: Vec<String> = Vec::new();
    for divergence in SHAPE_DIVERGENCES {
        if let Some(reason) = stale_shape_reason(&tools, divergence) {
            stale.push(format!(
                "  {} / {} / {}: {reason} (it claimed: {})",
                divergence.binding, divergence.tool, divergence.field, divergence.reason
            ));
        }
    }

    assert!(
        stale.is_empty(),
        "{} stale entry(ies) in SHAPE_DIVERGENCES:\n{}",
        stale.len(),
        stale.join("\n"),
    );
    client.cancel().await.expect("close the MCP session");
}

/// Why `divergence` no longer describes reality, or `None` when it still
/// does. Split out of [`no_shape_divergence_is_stale`] to keep both under
/// the repository's cyclomatic-complexity gate.
fn stale_shape_reason(tools: &[rmcp::model::Tool], divergence: &ShapeDivergence) -> Option<String> {
    if divergence.binding == TYPESCRIPT_SDK {
        return stale_sdk_shape_reason(tools, divergence);
    }
    if !BINDINGS.iter().any(|b| b.name == divergence.binding) {
        return Some(format!("unknown binding `{}`", divergence.binding));
    }
    let Some(tool) = tools.iter().find(|t| t.name.as_ref() == divergence.tool) else {
        return Some(format!("`{}` is no longer an MCP tool", divergence.tool));
    };
    if !output_root_fields(tool).contains(divergence.field) {
        return Some(format!(
            "`{}` no longer has a root output field `{}`",
            divergence.tool, divergence.field
        ));
    }
    let binding = BINDINGS.iter().find(|b| b.name == divergence.binding)?;
    let regions = method_regions(binding);
    let region = regions.get(divergence.tool)?;
    let server_type = server_output_types().get(divergence.tool).cloned();
    if server_type
        .as_ref()
        .is_some_and(|ty| names_identifier(region, ty))
    {
        return Some(format!(
            "the binding now relays the server type `{}` wholesale",
            server_type.unwrap_or_default()
        ));
    }
    // Route 2's window, minus doc comments (#1760) — the parameter-list cut
    // is still shared with route 2 for the #1704 reason (a divergence must
    // not go stale because a PARAMETER names the field), but doc comments
    // are stripped ON TOP of that here, not in route 2: see
    // `without_doc_comments`.
    let window = without_doc_comments(&output_window(region, divergence.tool));
    let declared = region_with_named_structs(&window, &binding_structs(binding));
    names_identifier(&declared, divergence.field)
        .then(|| format!("the binding now names `{}`", divergence.field))
}

fn stale_sdk_shape_reason(
    tools: &[rmcp::model::Tool],
    divergence: &ShapeDivergence,
) -> Option<String> {
    let Some(tool) = tools
        .iter()
        .find(|tool| tool.name.as_ref() == divergence.tool)
    else {
        return Some(format!("`{}` is no longer an MCP tool", divergence.tool));
    };
    if !output_root_fields(tool).contains(divergence.field) {
        return Some(format!(
            "`{}` no longer has a root output field `{}`",
            divergence.tool, divergence.field
        ));
    }
    let method = js_name(divergence.tool);
    let returns = sdk_return_types();
    let Some(return_type) = returns.get(&method) else {
        return Some(format!(
            "the SDK no longer declares `{method}` with a Promise return"
        ));
    };
    let interfaces = sdk_interfaces();
    let Some(fields) = interfaces.get(return_type) else {
        return Some(format!(
            "`{method}` now returns non-local `{return_type}`; use SDK_SHAPE_SKIPS"
        ));
    };
    sdk_names_field(fields, divergence.field)
        .then(|| format!("the SDK now names `{}`", divergence.field))
}

// ===========================================================================
// The window of #1704, pinned on synthetic text.
//
// Measured on develop and again after the cut: NO binding is green today on
// the strength of a parameter name alone, so the bindings cannot demonstrate
// this refusal. That is a reason to pin it here, not a reason to skip it —
// the channel is open until something closes it, and a guard nobody has seen
// refuse is the defect this whole campaign is about.
// ===========================================================================

/// A region shaped like the real ones: doc comment, `#[pyo3(signature)]`
/// echo, declaration with a nested-paren parameter type, body, closing brace.
const SYNTHETIC_REGION: &str = "\
    /// Fetch the source back.
    #[pyo3(signature = (handle, media = None))]
    fn retrieve_context_source(&self, py: Python<'_>, handle: &str, media: Option<(u8, u8)>) -> PyResult<Py<PyAny>> {
        let source: ContextSource = fetch();
        Ok(source)
    }
";

#[test]
fn the_raw_region_is_satisfied_by_a_parameter_name() {
    // The false green of #1704, stated before it is fixed: `handle` is a root
    // field of `retrieve_context_source`'s output_schema, and the region
    // names it three times without relaying it once.
    assert!(
        names_identifier(SYNTHETIC_REGION, "handle"),
        "fixture precondition: the raw region names `handle`",
    );
}

#[test]
fn the_output_window_refuses_a_field_named_only_by_an_input() {
    let window = output_window(SYNTHETIC_REGION, "retrieve_context_source");
    assert!(
        !names_identifier(&window, "handle"),
        "an input named `handle` still satisfies the window:\n{window}",
    );
    assert!(
        !names_identifier(&window, "media"),
        "the `signature` attribute still spells the parameters:\n{window}",
    );
}

#[test]
fn the_output_window_keeps_everything_that_describes_the_return() {
    let window = output_window(SYNTHETIC_REGION, "retrieve_context_source");
    for kept in ["PyResult", "ContextSource", "Fetch"] {
        assert!(
            names_identifier(&window, kept),
            "the cut swallowed `{kept}`, which describes the return:\n{window}",
        );
    }
}

#[test]
fn the_cut_falls_back_to_the_whole_region_when_the_declaration_is_absent() {
    // A cut that narrowed silently on a parse miss would be the defect one
    // level up: the guard would stop seeing relays it used to see. Only the
    // parameter-list cut can miss — the `signature` echo is cut either way,
    // since finding it does not depend on finding the declaration.
    let missed = cut_parameter_list(SYNTHETIC_REGION, "a_method_that_is_not_here");
    assert_eq!(missed, SYNTHETIC_REGION);
}

#[test]
fn the_cut_stops_at_the_paren_that_closes_the_parameter_list() {
    // `Option<(u8, u8)>` closes an inner pair first; a `find(')')` would cut
    // there and leave `-> PyResult<…>` amputated of everything before it.
    let window = output_window(SYNTHETIC_REGION, "retrieve_context_source");
    assert!(
        window.contains("fn retrieve_context_source -> PyResult<Py<PyAny>> {"),
        "the declaration did not survive the cut intact:\n{window}",
    );
}

// ===========================================================================
// The false stale of #1760, pinned on synthetic text.
//
// Reproduced on develop before the fix: documenting `recall_where`'s
// contract as "Returns caller memories ONLY" in the three bindings' doc
// comments made `no_shape_divergence_is_stale` report their real,
// still-needed `memories` `ShapeDivergence` entries as stale — the sentence
// explaining what the binding does NOT return was read as proof that it now
// does.
// ===========================================================================

/// A region shaped like the real regression: a doc comment that spells a
/// field name in prose, on a method whose body never returns it.
const PROSE_ONLY_REGION: &str = "\
    /// Returns caller memories ONLY: entity hubs and the context compiler's\n\
    /// artefacts are internal scaffolding and never come back through this call.\n\
    fn recall_where(&self, py: Python<'_>) -> PyResult<Vec<PyObject>> {\n\
        Ok(vec![])\n\
    }\n\
";

#[test]
fn the_raw_window_is_satisfied_by_prose_alone() {
    // The false green this fixture exists to state before it is fixed:
    // `memories` never appears outside the doc comment, and route 2's window
    // (which keeps doc comments, by design) matches it anyway.
    let window = output_window(PROSE_ONLY_REGION, "recall_where");
    assert!(
        names_identifier(&window, "memories"),
        "fixture precondition: the raw window names `memories` only in prose",
    );
}

#[test]
fn stripping_doc_comments_refuses_a_field_named_only_in_prose() {
    let window = without_doc_comments(&output_window(PROSE_ONLY_REGION, "recall_where"));
    assert!(
        !names_identifier(&window, "memories"),
        "a field named only in a doc comment must not survive the strip:\n{window}",
    );
}

#[test]
fn stripping_doc_comments_keeps_everything_that_describes_the_return() {
    let window = without_doc_comments(&output_window(PROSE_ONLY_REGION, "recall_where"));
    for kept in ["PyResult", "PyObject", "recall_where"] {
        assert!(
            names_identifier(&window, kept),
            "the strip swallowed `{kept}`, which is not a doc comment:\n{window}",
        );
    }
}

// --- The SDK link of the chain (issue #1721) ---------------------------------

/// THE guard for the one surface written in the target language: every tool
/// that reaches the WASM binding must reach the SDK that wraps it.
///
/// The SDK shipped 17 of 19 for its whole life. The reason nothing caught it
/// is the same one that let `entity` go missing from three bindings: the list
/// of what is checked did not include it. Widening a guard's declared
/// perimeter is the fix; writing a second, parallel one would only move the
/// blind spot.
#[tokio::test]
async fn the_typescript_sdk_relays_every_tool_that_reaches_the_wasm_binding() {
    let (_store, client) = connected().await;
    let tools: BTreeSet<String> = client
        .list_all_tools()
        .await
        .expect("list tools")
        .iter()
        .map(|t| t.name.to_string())
        .collect();
    let wasm = BINDINGS
        .iter()
        .find(|b| b.name == "velesdb-wasm")
        .expect("velesdb-wasm is a declared binding");

    let gaps = sdk_gaps(
        &tools,
        &published_methods(wasm),
        &sdk_methods(SDK_CLASS),
        &sdk_methods(SDK_INTERFACE),
    );

    assert!(
        gaps.is_empty(),
        "{} tool(s) reach velesdb-wasm but stop at the TypeScript SDK:\n{}\n\nThe SDK calls \
         into the WASM binding, so it can never publish more than that binding does — but it \
         can publish LESS, silently, and it did: `entity` and `unrelate` were absent for the \
         SDK's whole life while wasm exposed both. Add the method to BOTH the class and the \
         `WasmMemoryServiceInstance` interface in {}.",
        gaps.len(),
        gaps.join("\n"),
        SDK_SOURCE,
    );
    client.cancel().await.expect("close the MCP session");
}

/// The shape half of the same server -> WASM -> SDK chain. A local
/// `Promise<X>` must name every root field of the live server schema in
/// `interface X`, accepting the SDK's camelCase spelling. Non-local returns
/// are allowed only through an exact, reasoned [`SdkShapeSkip`].
#[tokio::test]
async fn the_typescript_sdk_relays_every_checkable_output_shape() {
    let (_store, client) = connected().await;
    let tools = client.list_all_tools().await.expect("list tools");
    let wasm = BINDINGS
        .iter()
        .find(|binding| binding.name == "velesdb-wasm")
        .expect("velesdb-wasm is a declared binding");
    let findings = sdk_shape_findings(&tools, &published_methods(wasm));

    assert!(
        findings.is_empty(),
        "{} TypeScript SDK output-shape finding(s):\n{}\n\nA local Promise<X> return must name \
         every root key of the live MCP output schema in interface X (snake_case or camelCase). \
         Fix the SDK field, declare a deliberate field loss in SHAPE_DIVERGENCES WITH its \
         reason, or pin an uncheckable non-local return in SDK_SHAPE_SKIPS WITH its reason.",
        findings.len(),
        findings.join("\n"),
    );
    client.cancel().await.expect("close the MCP session");
}

#[test]
fn the_sdk_shape_parser_reads_real_returns_and_interfaces() {
    let returns = sdk_return_types();
    assert_eq!(
        returns.get("why").map(String::as_str),
        Some("MemoryExplanation")
    );
    assert_eq!(returns.get("remember").map(String::as_str), Some("string"));
    let interfaces = sdk_interfaces();
    let explanation = interfaces
        .get("MemoryExplanation")
        .expect("parse MemoryExplanation");
    assert!(explanation.contains("nodes") && explanation.contains("truncated"));
}

/// Mutation vector: stripping one field from a synthetic local return must
/// produce one finding and name the exact tool.field that disappeared.
#[test]
fn the_sdk_shape_check_names_the_field_a_stripped_interface_lost() {
    let output = ["edges", "nodes", "truncated"]
        .map(str::to_owned)
        .into_iter()
        .collect();
    let stripped = ["edges", "nodes"].map(str::to_owned).into_iter().collect();
    let gaps = sdk_output_gaps("why", &output, &stripped);

    assert_eq!(
        gaps.len(),
        1,
        "exactly the stripped field is reported: {gaps:?}"
    );
    assert!(gaps[0].contains("why.truncated"), "got: {}", gaps[0]);
}

#[test]
fn the_sdk_shape_check_is_silent_for_a_complete_interface() {
    let output = ["edges", "nodes", "truncated"]
        .map(str::to_owned)
        .into_iter()
        .collect();
    assert!(
        sdk_output_gaps("why", &output, &output).is_empty(),
        "a complete local return interface must produce no finding",
    );
}

#[test]
fn the_sdk_shape_check_accepts_the_camel_case_wire_spelling() {
    let output = ["relations_in"].map(str::to_owned).into_iter().collect();
    let sdk = ["relationsIn"].map(str::to_owned).into_iter().collect();
    assert!(sdk_output_gaps("entity", &output, &sdk).is_empty());
}

#[tokio::test]
async fn no_sdk_shape_skip_is_stale() {
    let (_store, client) = connected().await;
    let listed = client.list_all_tools().await.expect("list tools");
    let tools = listed
        .iter()
        .map(|tool| tool.name.to_string())
        .collect::<BTreeSet<_>>();
    let wasm = BINDINGS
        .iter()
        .find(|binding| binding.name == "velesdb-wasm")
        .expect("velesdb-wasm is a declared binding");
    let upstream = published_methods(wasm);
    let returns = sdk_return_types();
    let interfaces = sdk_interfaces();
    let stale: Vec<String> = SDK_SHAPE_SKIPS
        .iter()
        .filter_map(|skip| {
            stale_sdk_skip_reason(skip, &tools, &upstream, &returns, &interfaces)
                .map(|reason| format!("  {} -> {}: {reason}", skip.tool, skip.return_type))
        })
        .collect();
    assert!(
        stale.is_empty(),
        "{} stale SDK_SHAPE_SKIPS entry(ies):\n{}",
        stale.len(),
        stale.join("\n"),
    );
    client.cancel().await.expect("close the MCP session");
}

/// The refusal vector issue #1721 asks for, on a synthetic SDK so it can be
/// replayed without editing the real one: a method the wasm binding publishes
/// and the SDK dropped must be REPORTED BY NAME, never merely counted.
#[test]
fn the_sdk_check_names_the_method_a_stripped_sdk_lost() {
    let tools = ["remember", "unrelate", "recall_fused_dated"]
        .map(str::to_owned)
        .into_iter()
        .collect::<BTreeSet<_>>();
    let upstream = tools.clone();
    let stripped = ["remember", "recallFusedDated"]
        .map(str::to_owned)
        .into_iter()
        .collect::<BTreeSet<_>>();

    let gaps = sdk_gaps(&tools, &upstream, &stripped, &stripped);

    assert_eq!(
        gaps.len(),
        1,
        "exactly the dropped tool is reported: {gaps:?}"
    );
    assert!(
        gaps[0].contains("`unrelate`"),
        "the refusal names the missing tool, got: {}",
        gaps[0],
    );
}

/// The positive control the refusal above is worthless without: the SAME
/// inputs, complete, must produce nothing. Without it, a `sdk_gaps` that
/// returned a finding for every tool would pass the test above.
#[test]
fn the_sdk_check_is_silent_when_every_tool_is_relayed() {
    let tools = ["remember", "unrelate", "recall_fused_dated"]
        .map(str::to_owned)
        .into_iter()
        .collect::<BTreeSet<_>>();
    let complete = ["remember", "unrelate", "recallFusedDated"]
        .map(str::to_owned)
        .into_iter()
        .collect::<BTreeSet<_>>();

    assert!(
        sdk_gaps(&tools, &tools, &complete, &complete).is_empty(),
        "a complete SDK must produce no finding",
    );
}

/// A tool the WASM binding does not publish is NOT the SDK's problem: wasm's
/// own exemption governs, and duplicating it into a second list is how two
/// records of one decision start disagreeing.
#[test]
fn the_sdk_check_ignores_a_tool_that_never_reaches_wasm() {
    let tools = ["remember", "feedback"]
        .map(str::to_owned)
        .into_iter()
        .collect::<BTreeSet<_>>();
    let upstream = ["remember"].map(str::to_owned).into_iter().collect();
    let sdk = ["remember"].map(str::to_owned).into_iter().collect();

    assert!(
        sdk_gaps(&tools, &upstream, &sdk, &sdk).is_empty(),
        "`feedback` is exempted on wasm; the SDK must not be asked for it",
    );
}

/// Declaring a method in one of the two blocks and not the other is its own
/// failure, and the report must say WHICH — the two have different
/// consequences (`ensureCapability` keys off the interface; a caller reaches
/// the class).
#[test]
fn the_sdk_check_distinguishes_the_class_from_the_interface() {
    let tools = ["entity"].map(str::to_owned).into_iter().collect();
    let upstream = ["entity"].map(str::to_owned).into_iter().collect();
    let present: BTreeSet<String> = ["entity"].map(str::to_owned).into_iter().collect();
    let absent = BTreeSet::new();

    let gaps = sdk_gaps(&tools, &upstream, &present, &absent);
    assert!(gaps[0].contains("NOT the interface"), "got: {}", gaps[0]);
    let gaps = sdk_gaps(&tools, &upstream, &absent, &present);
    assert!(gaps[0].contains("NOT the class"), "got: {}", gaps[0]);
}

// --- Field parity: the context compiler's fragment ---------------------------
//
// Method parity, above, proves a tool is REACHABLE from the SDK. It says
// nothing about whether the SDK can express the tool's input — and that is a
// second way to publish a promise nobody can use.
//
// Measured on 2026-08-02: `CompileContextFragment` in the TypeScript SDK was
// missing `path` (file ingestion, V2b-1 — advertised by the server and by both
// SKILL.md copies) and `priority` ("higher packs first", the knob that decides
// what survives a token budget). A TypeScript caller could reach
// `compileContext` and still not use half of what it accepts.
//
// Both lists are read from source, never written down here: adding a field to
// the canonical fragment without adding it to the SDK turns this red on the
// spot, which a hard-coded expectation could not do.

/// Where the canonical wire shape of a context fragment is declared.
const FRAGMENT_SOURCE: &str = "crates/velesdb-memory/src/context/model.rs";
const FRAGMENT_STRUCT: &str = "pub struct ContextFragment {";

/// Where the TypeScript SDK declares the same fragment.
const SDK_FRAGMENT: &str = "export interface CompileContextFragment {";

/// Field names of the `struct` opened by `opening`, up to the next column-0 `}`.
fn rust_struct_fields(source_path: &str, opening: &str) -> BTreeSet<String> {
    let path = workspace_root().join(source_path);
    let source = std::fs::read_to_string(&path)
        .unwrap_or_else(|err| panic!("read {source_path} ({}): {err}", path.display()));
    let mut lines = source.lines().skip_while(|l| l.trim() != opening);
    assert!(
        lines.next().is_some(),
        "{source_path} no longer contains `{opening}` — point the constant at the renamed struct",
    );
    lines
        .take_while(|l| *l != "}")
        .filter_map(|line| {
            let declaration = line.trim().strip_prefix("pub ")?;
            let (name, _) = declaration.split_once(':')?;
            name.chars()
                .all(|c| c.is_alphanumeric() || c == '_')
                .then(|| name.to_owned())
        })
        .collect()
}

/// Field names of the TypeScript interface opened by `opening`.
fn typescript_interface_fields(opening: &str) -> BTreeSet<String> {
    let path = workspace_root().join(SDK_SOURCE);
    let source = std::fs::read_to_string(&path)
        .unwrap_or_else(|err| panic!("read {SDK_SOURCE} ({}): {err}", path.display()));
    let mut lines = source.lines().skip_while(|l| l.trim() != opening);
    assert!(
        lines.next().is_some(),
        "{SDK_SOURCE} no longer contains `{opening}` — point the constant at the renamed interface",
    );
    lines
        .take_while(|l| *l != "}")
        .filter_map(|line| {
            let declaration = line.trim();
            let (name, _) = declaration.split_once(':')?;
            let name = name.trim_end_matches('?');
            name.chars()
                .all(|c| c.is_alphanumeric() || c == '_')
                .then(|| name.to_owned())
        })
        .collect()
}

#[test]
fn the_fragment_scans_read_something_on_both_sides() {
    // Guard the guard: an empty scan on either side would make the parity
    // assertion below pass (or fail) for reasons that have nothing to do with
    // the SDK.
    let canonical = rust_struct_fields(FRAGMENT_SOURCE, FRAGMENT_STRUCT);
    assert!(
        canonical.contains("content") && canonical.contains("id"),
        "the canonical fragment scan parsed {canonical:?} — the scan is broken, not the struct",
    );
    let sdk = typescript_interface_fields(SDK_FRAGMENT);
    assert!(
        sdk.contains("content") && sdk.contains("id"),
        "the SDK fragment scan parsed {sdk:?} — the scan is broken, not the SDK",
    );
}

/// Fields the SDK deliberately does NOT declare, each with the reason.
///
/// Same shape as this file's tool-level exemptions, and for the same reason: a
/// field that is absent by decision and a field that is absent by oversight
/// look identical from the outside, and only a written reason tells them
/// apart.
const SDK_FRAGMENT_EXEMPTIONS: &[(&str, &str)] = &[(
    "path",
    "resolving a `path` fragment is a server-side I/O pre-pass gated on \
     VELESDB_MEMORY_INGEST_ROOTS, an operator-configured allowlist of \
     directories. This SDK runs on the WASM binding, which has neither a \
     filesystem nor that setting. NO binding declares it — velesdb-node and \
     velesdb-python do not resolve paths either; the MCP daemon is the only \
     surface that can honour one, so declaring it here would be a field that \
     always fails.",
)];

#[test]
fn every_fragment_exemption_names_a_field_that_actually_exists() {
    // An exemption for a field the canonical fragment no longer has would sit
    // here forever, silently excusing nothing — and would hide the day a real
    // field takes that name.
    let canonical = rust_struct_fields(FRAGMENT_SOURCE, FRAGMENT_STRUCT);
    for (field, _) in SDK_FRAGMENT_EXEMPTIONS {
        assert!(
            canonical.contains(*field),
            "`{field}` is exempted from the SDK fragment but is not a field of the canonical \
             fragment any more — delete the exemption",
        );
    }
}

#[test]
fn the_typescript_fragment_declares_every_field_the_wire_accepts() {
    let canonical = rust_struct_fields(FRAGMENT_SOURCE, FRAGMENT_STRUCT);
    let mut sdk = typescript_interface_fields(SDK_FRAGMENT);
    sdk.extend(
        SDK_FRAGMENT_EXEMPTIONS
            .iter()
            .map(|(field, _)| (*field).to_owned()),
    );
    let missing: Vec<&String> = canonical.difference(&sdk).collect();
    assert!(
        missing.is_empty(),
        "the TypeScript SDK's `CompileContextFragment` is missing {missing:?}, so a TypeScript \
         caller cannot express input the server accepts. The tool is reachable and half of its \
         contract is not — declare the field in {SDK_SOURCE}, or, if it is deliberately withheld, \
         say so where a reader will see it rather than leaving the absence to look like an \
         oversight.",
    );
}