File size: 64,436 Bytes
ff78003
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Tool capability graph β€” Node-side tests.
 *
 * Covers:
 *  - tagsForIntent against the shared fixture (parity with Python via
 *    artifacts/research-engine/tests/test_graph_client.py)
 *  - resolveSubgraph: 1-hop expansion, empty intent, shadow nodes
 *  - describeSubgraph string shape
 *  - recordPlannerGap accumulator (one row per tag, count grows)
 *  - autoExtendIfNeeded creates exactly one provisional node + edge per
 *    crossed-threshold gap, marks the gap as extended
 *  - approveNode / rejectNode round-trip
 *
 * Skipped when DATABASE_URL is unset.
 */
import { test } from "node:test";
import assert from "node:assert/strict";
import { randomBytes } from "node:crypto";
import { readFileSync } from "node:fs";
import { dirname, resolve } from "node:path";
import { fileURLToPath } from "node:url";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const ORIGINAL_DSN = process.env.DATABASE_URL || "";
const SKIP = !ORIGINAL_DSN;

function dsnWithSearchPath(dsn: string, schema: string): string {
  const opt = `options=-c%20search_path%3D${encodeURIComponent(schema)}`;
  return dsn.includes("?") ? `${dsn}&${opt}` : `${dsn}?${opt}`;
}

interface IntentCase {
  text: string;
  expected: string[];
}

function loadFixture(): IntentCase[] {
  const p = resolve(__dirname, "fixtures/intent-cases.json");
  const data = JSON.parse(readFileSync(p, "utf8")) as { cases: IntentCase[] };
  return data.cases;
}

test("tool-graph against live PG ephemeral schema", {
  skip: SKIP && "DATABASE_URL not set",
}, async (t) => {
  const schema = `tool_graph_test_${randomBytes(6).toString("hex")}`;
  process.env.DATABASE_URL = dsnWithSearchPath(ORIGINAL_DSN, schema);

  const { pool } = await import("@workspace/db");
  await pool.query(`CREATE SCHEMA "${schema}"`);

  // Mirror the four tables in lib/db/src/schema/toolGraph.ts.
  await pool.query(`
    CREATE TABLE "${schema}".tool_nodes (
      id text PRIMARY KEY,
      name text NOT NULL UNIQUE,
      description text NOT NULL DEFAULT '',
      capability_tags jsonb NOT NULL DEFAULT '[]'::jsonb,
      input_kind text NOT NULL DEFAULT 'json',
      output_kind text NOT NULL DEFAULT 'json',
      status text NOT NULL DEFAULT 'verified',
      owner_process text NOT NULL,
      spec_json jsonb NOT NULL,
      created_by text NOT NULL DEFAULT 'system',
      handler_ref text,
      handler_stub text,
      cost_hint double precision,
      latency_hint_ms integer,
      version integer NOT NULL DEFAULT 1,
      created_at timestamptz NOT NULL DEFAULT now(),
      updated_at timestamptz NOT NULL DEFAULT now()
    );
    CREATE TABLE "${schema}".tool_edges (
      id text PRIMARY KEY,
      from_node text NOT NULL REFERENCES "${schema}".tool_nodes(id) ON DELETE CASCADE,
      to_node text NOT NULL REFERENCES "${schema}".tool_nodes(id) ON DELETE CASCADE,
      relation text NOT NULL,
      weight double precision NOT NULL DEFAULT 1.0,
      contract jsonb,
      created_at timestamptz NOT NULL DEFAULT now(),
      CONSTRAINT tool_edges_uniq UNIQUE (from_node, to_node, relation)
    );
    CREATE TABLE "${schema}".tool_node_evidence (
      id text PRIMARY KEY,
      node_id text NOT NULL REFERENCES "${schema}".tool_nodes(id) ON DELETE CASCADE,
      kind text NOT NULL,
      payload jsonb NOT NULL,
      success integer NOT NULL DEFAULT 0,
      failure integer NOT NULL DEFAULT 0,
      shadow_user_id text,
      created_at timestamptz NOT NULL DEFAULT now(),
      seam_folded_at timestamptz
    );
    CREATE INDEX "${schema}_tne_seam_unfolded" ON "${schema}".tool_node_evidence(kind, seam_folded_at);
    CREATE TABLE "${schema}".tool_edge_health (
      id text PRIMARY KEY,
      edge_id text NOT NULL REFERENCES "${schema}".tool_edges(id) ON DELETE CASCADE,
      traversal_count integer NOT NULL DEFAULT 0,
      contract_issue_count integer NOT NULL DEFAULT 0,
      missing_field_count integer NOT NULL DEFAULT 0,
      ema_coverage double precision NOT NULL DEFAULT 1.0,
      ema_health_score double precision NOT NULL DEFAULT 1.0,
      top_missing_fields jsonb NOT NULL DEFAULT '{}'::jsonb,
      top_contract_issues jsonb NOT NULL DEFAULT '{}'::jsonb,
      formula_version integer NOT NULL DEFAULT 1,
      last_sample_at timestamptz,
      last_folded_evidence_id text,
      computed_at timestamptz NOT NULL DEFAULT now()
    );
    CREATE UNIQUE INDEX "${schema}_teh_edge" ON "${schema}".tool_edge_health(edge_id);
    CREATE TABLE "${schema}".tool_gap_signals (
      id text PRIMARY KEY,
      capability_tag text NOT NULL UNIQUE,
      invocation_count integer NOT NULL DEFAULT 0,
      status text NOT NULL DEFAULT 'open',
      last_context jsonb,
      extended_node_id text,
      first_seen_at timestamptz NOT NULL DEFAULT now(),
      last_seen_at timestamptz NOT NULL DEFAULT now()
    );
    CREATE TABLE "${schema}".tool_deprecation_candidates (
      id text PRIMARY KEY,
      node_id text NOT NULL REFERENCES "${schema}".tool_nodes(id) ON DELETE CASCADE,
      classification text NOT NULL,
      status text NOT NULL DEFAULT 'open',
      proposal_context jsonb NOT NULL DEFAULT '{}'::jsonb,
      defer_until timestamptz,
      re_arm_until timestamptz,
      decided_by text,
      decided_at timestamptz,
      created_at timestamptz NOT NULL DEFAULT now(),
      updated_at timestamptz NOT NULL DEFAULT now()
    );
    CREATE UNIQUE INDEX "${schema}_tdc_node" ON "${schema}".tool_deprecation_candidates(node_id);
    CREATE TABLE "${schema}".tool_nodes_archive (
      id text PRIMARY KEY,
      name text NOT NULL,
      description text NOT NULL DEFAULT '',
      capability_tags jsonb NOT NULL DEFAULT '[]'::jsonb,
      input_kind text NOT NULL DEFAULT 'json',
      output_kind text NOT NULL DEFAULT 'json',
      status text NOT NULL,
      owner_process text NOT NULL,
      spec_json jsonb NOT NULL,
      created_by text NOT NULL DEFAULT 'system',
      handler_ref text,
      handler_stub text,
      cost_hint double precision,
      latency_hint_ms integer,
      version integer NOT NULL DEFAULT 1,
      created_at timestamptz NOT NULL,
      updated_at timestamptz NOT NULL,
      archived_at timestamptz NOT NULL DEFAULT now()
    );
    CREATE TABLE "${schema}".tool_edges_archive (
      id text PRIMARY KEY,
      from_node text NOT NULL,
      to_node text NOT NULL,
      relation text NOT NULL,
      weight double precision NOT NULL DEFAULT 1.0,
      contract jsonb,
      created_at timestamptz NOT NULL,
      archived_at timestamptz NOT NULL DEFAULT now()
    );
    CREATE TABLE "${schema}".tool_node_evidence_archive (
      id text PRIMARY KEY,
      node_id text NOT NULL,
      kind text NOT NULL,
      payload jsonb NOT NULL,
      success integer NOT NULL DEFAULT 0,
      failure integer NOT NULL DEFAULT 0,
      shadow_user_id text,
      created_at timestamptz NOT NULL,
      seam_folded_at timestamptz,
      archived_at timestamptz NOT NULL DEFAULT now()
    );
    CREATE TABLE "${schema}".tool_summary_paths (
      id text PRIMARY KEY,
      name text NOT NULL UNIQUE,
      alias_node_id text,
      description_derived text NOT NULL DEFAULT '',
      description_override text,
      expansion_node_names jsonb NOT NULL DEFAULT '[]'::jsonb,
      head_atom_name text,
      tail_atom_name text,
      capability_tags jsonb NOT NULL DEFAULT '[]'::jsonb,
      est_cost_hint double precision,
      est_latency_ms_hint integer,
      status text NOT NULL DEFAULT 'active',
      version integer NOT NULL DEFAULT 1,
      traversal_count integer NOT NULL DEFAULT 0,
      created_at timestamptz NOT NULL DEFAULT now(),
      updated_at timestamptz NOT NULL DEFAULT now()
    );
    CREATE INDEX "${schema}_tsp_status" ON "${schema}".tool_summary_paths(status);
    CREATE TABLE "${schema}".tool_spawn_templates (
      id text PRIMARY KEY,
      fingerprint_hash text NOT NULL,
      fingerprint_algo_version integer NOT NULL DEFAULT 1,
      failure_mode text NOT NULL,
      missing_field_types jsonb NOT NULL DEFAULT '[]'::jsonb,
      unused_field_types jsonb NOT NULL DEFAULT '[]'::jsonb,
      downstream_input_schema_fingerprint text NOT NULL,
      promoted_input_schema_fingerprint text NOT NULL,
      promoted_output_schema_fingerprint text NOT NULL,
      handler_skeleton text NOT NULL DEFAULT '',
      spec_skeleton jsonb NOT NULL DEFAULT '{}'::jsonb,
      capability_tag text NOT NULL DEFAULT '',
      source_node_name text NOT NULL,
      offered_count integer NOT NULL DEFAULT 0,
      reuse_count integer NOT NULL DEFAULT 0,
      success_count integer NOT NULL DEFAULT 0,
      reject_count integer NOT NULL DEFAULT 0,
      status text NOT NULL DEFAULT 'active',
      demoted_at timestamptz,
      demoted_reason text,
      version integer NOT NULL DEFAULT 1,
      created_at timestamptz NOT NULL DEFAULT now(),
      updated_at timestamptz NOT NULL DEFAULT now()
    );
    CREATE INDEX "${schema}_tspt_fp" ON "${schema}".tool_spawn_templates(fingerprint_hash);
    CREATE INDEX "${schema}_tspt_status" ON "${schema}".tool_spawn_templates(status);
    CREATE INDEX "${schema}_tspt_failure_mode" ON "${schema}".tool_spawn_templates(failure_mode);
    CREATE UNIQUE INDEX "${schema}_tspt_uniq" ON "${schema}".tool_spawn_templates(fingerprint_hash, fingerprint_algo_version);
  `);

  const tg = await import("../tool-graph");

  t.after(async () => {
    try {
      await pool.query(`DROP SCHEMA IF EXISTS "${schema}" CASCADE`);
    } finally {
      process.env.DATABASE_URL = ORIGINAL_DSN;
    }
  });

  // -------------------------------------------------- intent fixture parity
  await t.test("tagsForIntent matches the shared fixture", () => {
    const cases = loadFixture();
    for (const c of cases) {
      const got = tg.tagsForIntent(c.text).slice().sort();
      const want = c.expected.slice().sort();
      assert.deepEqual(
        got,
        want,
        `case "${c.text}" β†’ got ${JSON.stringify(got)} want ${JSON.stringify(want)}`,
      );
    }
  });

  // -------------------------------------------------- subgraph behaviour
  // Seed a tiny graph: search_pubmed (literature_search) feeds
  // summarize_bucket (summarization). Plus an unrelated lookup_uniprot
  // (protein_lookup) that should be excluded from a literature query.
  let pubmedId = "";
  let summaryId = "";
  let uniprotId = "";

  await t.test("seed minimal verified graph", async () => {
    const a = await tg.upsertNode({
      id: "tnode_pubmed_1",
      name: "search_pubmed",
      description: "Search PubMed for papers.",
      capabilityTags: ["literature_search"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "search_pubmed" },
      createdBy: "system",
    });
    const b = await tg.upsertNode({
      id: "tnode_summary_1",
      name: "summarize_bucket",
      description: "Summarize a literature bucket.",
      capabilityTags: ["summarization"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "summarize_bucket" },
      createdBy: "system",
    });
    const c = await tg.upsertNode({
      id: "tnode_uniprot_1",
      name: "lookup_uniprot",
      description: "Lookup a UniProt protein.",
      capabilityTags: ["protein_lookup"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "lookup_uniprot" },
      createdBy: "system",
    });
    pubmedId = a.id;
    summaryId = b.id;
    uniprotId = c.id;
    await tg.upsertEdge({
      id: "tedge_1",
      fromNode: a.id,
      toNode: b.id,
      relation: "feeds",
      weight: 1.0,
    });
  });

  await t.test("resolveSubgraph: literature intent pulls in 1-hop summary", async () => {
    const sg = await tg.resolveSubgraph({ intentText: "find pubmed papers" });
    const names = sg.nodes.map((n) => n.name).sort();
    assert.deepEqual(names, ["search_pubmed", "summarize_bucket"]);
    assert.equal(sg.matchedTags[0], "literature_search");
    assert.ok(sg.edges.some((e) => e.relation === "feeds"));
    assert.ok(!names.includes("lookup_uniprot"));
  });

  await t.test("resolveSubgraph: empty intent returns empty subgraph", async () => {
    const sg = await tg.resolveSubgraph({ intentText: "δ½ ε₯½ε•Š" });
    assert.deepEqual(sg.nodes, []);
    assert.deepEqual(sg.edges, []);
    assert.deepEqual(sg.matchedTags, []);
  });

  await t.test("resolveSubgraph: includeShadowNodeIds appends provisional", async () => {
    // Add a provisional node not connected to anything literature-y.
    const prov = await tg.upsertNode({
      id: "tnode_shadow_1",
      name: "shadow_candidate",
      description: "Shadow candidate.",
      capabilityTags: ["literature_search"],
      inputKind: "json",
      outputKind: "json",
      status: "provisional",
      ownerProcess: "node",
      specJson: { name: "shadow_candidate" },
      createdBy: "auto",
    });
    const sg = await tg.resolveSubgraph({
      intentText: "find pubmed papers",
      includeShadowNodeIds: [prov.id],
    });
    const names = sg.nodes.map((n) => n.name).sort();
    assert.ok(names.includes("shadow_candidate"));
    // Without the shadow id, the provisional node must not appear.
    const sgPlain = await tg.resolveSubgraph({ intentText: "find pubmed papers" });
    assert.ok(!sgPlain.nodes.some((n) => n.name === "shadow_candidate"));
  });

  // -------------------------------------------------- describe
  await t.test("describeSubgraph renders names + connections", async () => {
    const sg = await tg.resolveSubgraph({ intentText: "find pubmed papers" });
    const text = tg.describeSubgraph(sg);
    assert.ok(text.includes("search_pubmed"));
    assert.ok(text.includes("summarize_bucket"));
    assert.ok(text.includes("Connections:"));
    assert.ok(text.includes("β€”[feeds]β†’"));
  });

  // -------------------------------------------------- planner gap accumulator
  await t.test("recordPlannerGap accumulates per tag", async () => {
    await tg.recordPlannerGap("orphan_capability", { ctx: 1 });
    await tg.recordPlannerGap("orphan_capability", { ctx: 2 });
    const rows = await tg.listGapSignals();
    const orphan = rows.find((r) => r.capabilityTag === "orphan_capability");
    assert.ok(orphan, "gap signal row must exist");
    assert.equal(orphan!.invocationCount, 2);
    assert.equal(orphan!.status, "open");
  });

  // -------------------------------------------------- auto-extend
  await t.test("autoExtendIfNeeded creates provisional + edge once threshold crossed", async () => {
    // Push the orphan_capability gap signal up to threshold (3 by default).
    await tg.recordPlannerGap("orphan_capability", { ctx: 3 });
    const before = await tg.listNodes({ status: "provisional" });
    const created = await tg.autoExtendIfNeeded();
    assert.equal(created, 1, "exactly one provisional node should be created");
    const after = await tg.listNodes({ status: "provisional" });
    assert.equal(
      after.length - before.length,
      1,
      "provisional node count must grow by 1",
    );
    const gaps = await tg.listGapSignals();
    const ext = gaps.find((g) => g.capabilityTag === "orphan_capability");
    assert.equal(ext?.status, "extended");
    assert.ok(ext?.extendedNodeId, "gap row must point at the new node");

    // Running auto-extend again should not duplicate.
    const created2 = await tg.autoExtendIfNeeded();
    assert.equal(created2, 0);
  });

  // -------------------------------------------------- approve / reject
  await t.test("approveNode promotes provisional to verified", async () => {
    const provs = await tg.listNodes({ status: "provisional" });
    const target = provs[0]!;
    const result = await tg.approveNode(target.id);
    assert.ok(result, "approveNode should return a node");
    assert.equal(result!.status, "verified");
    const verifiedNow = await tg.listNodes({ status: "verified" });
    assert.ok(verifiedNow.some((n) => n.id === target.id));
  });

  await t.test("rejectNode deletes node and dismisses linked gap", async () => {
    // Force a fresh provisional via another auto-extend cycle.
    await tg.recordPlannerGap("another_gap", { ctx: 1 });
    await tg.recordPlannerGap("another_gap", { ctx: 2 });
    await tg.recordPlannerGap("another_gap", { ctx: 3 });
    await tg.autoExtendIfNeeded();
    const provs = await tg.listNodes({ status: "provisional" });
    assert.ok(provs.length > 0);
    const target = provs[0]!;
    const ok = await tg.rejectNode(target.id);
    assert.equal(ok, true);
    const stillThere = await tg.getNode(target.id);
    assert.equal(stillThere, null);
    const gaps = await tg.listGapSignals();
    const linked = gaps.find((g) => g.extendedNodeId === target.id);
    assert.equal(linked?.status, "dismissed");
  });

  // -------------------------------------------------- contract validator (#156)
  await t.test("validateContract: matching shape passes", () => {
    const r = tg.validateContract({
      produces: {
        type: "object",
        properties: {
          ensembl_id: { type: "string" },
          name: { type: "string" },
        },
      },
      consumes: {
        type: "object",
        properties: { ensembl_id: { type: "string" } },
        required: ["ensembl_id"],
      },
    });
    assert.equal(r.ok, true);
    assert.deepEqual(r.issues, []);
  });

  await t.test("validateContract: missing required field flagged", () => {
    const r = tg.validateContract({
      produces: {
        type: "object",
        properties: { ensembl_id: { type: "string" } },
      },
      consumes: {
        type: "object",
        properties: {
          ensembl_id: { type: "string" },
          diseases: { type: "array" },
        },
        required: ["ensembl_id", "diseases"],
      },
    });
    assert.equal(r.ok, false);
    assert.equal(r.issues.length, 1);
    assert.equal(r.issues[0]!.field, "diseases");
    assert.equal(r.issues[0]!.reason, "missing_in_produces");
  });

  await t.test("validateContract: type mismatch flagged", () => {
    const r = tg.validateContract({
      produces: {
        type: "object",
        properties: { count: { type: "string" } },
      },
      consumes: {
        type: "object",
        properties: { count: { type: "integer" } },
        required: ["count"],
      },
    });
    assert.equal(r.ok, false);
    assert.equal(r.issues[0]!.reason, "type_mismatch");
    assert.equal(r.issues[0]!.expectedType, "integer");
    assert.equal(r.issues[0]!.actualType, "string");
  });

  await t.test("validateContract: optional missing field ignored", () => {
    const r = tg.validateContract({
      produces: {
        type: "object",
        properties: { a: { type: "string" } },
      },
      consumes: {
        type: "object",
        properties: {
          a: { type: "string" },
          b: { type: "string" },
        },
        required: ["a"],
      },
    });
    assert.equal(r.ok, true);
  });

  // -------------------------------------------------- composition_alias exclusion (#156)
  await t.test("composition_alias is excluded from resolveSubgraph", async () => {
    // Insert an alias node tagged "literature_search" β€” it would otherwise
    // be picked up by a literature intent. The resolver must exclude it
    // because its status is composition_alias, not verified.
    await tg.upsertNode({
      id: "tnode_alias_1",
      name: "summarize_literature_bucket_alias",
      description: "Decomposed alias.",
      capabilityTags: ["literature_search"],
      inputKind: "json",
      outputKind: "json",
      status: "composition_alias",
      ownerProcess: "node",
      specJson: { name: "summarize_literature_bucket_alias" },
      createdBy: "system",
    });
    const sg = await tg.resolveSubgraph({ intentText: "find pubmed papers" });
    const names = sg.nodes.map((n) => n.name);
    assert.ok(
      !names.includes("summarize_literature_bucket_alias"),
      `alias must not appear in subgraph; got ${JSON.stringify(names)}`,
    );
    // listNodes(verified) must also exclude it.
    const verified = await tg.listNodes({ status: "verified" });
    assert.ok(!verified.some((n) => n.name === "summarize_literature_bucket_alias"));
    // listNodes(any) must still see it (admin/debug surface).
    const any = await tg.listNodes({ status: "any" });
    assert.ok(any.some((n) => n.name === "summarize_literature_bucket_alias"));
  });

  // -------------------------------------------------- edge contract round-trip (#156)
  await t.test("upsertEdge persists contract jsonb and surfaces it on listEdges", async () => {
    // Two scratch nodes for the contract round-trip.
    const a = await tg.upsertNode({
      id: "tnode_ot_find_1",
      name: "opentargets_find_target_id_test",
      description: "Test atomic node A.",
      capabilityTags: ["target_disease"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "opentargets_find_target_id_test" },
      createdBy: "system",
    });
    const b = await tg.upsertNode({
      id: "tnode_ot_assoc_1",
      name: "opentargets_get_target_associations_test",
      description: "Test atomic node B.",
      capabilityTags: ["target_disease"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "opentargets_get_target_associations_test" },
      createdBy: "system",
    });
    const contract = {
      produces: {
        type: "object" as const,
        properties: { ensembl_id: { type: "string" as const } },
        required: ["ensembl_id"],
      },
      consumes: {
        type: "object" as const,
        properties: { ensembl_id: { type: "string" as const } },
        required: ["ensembl_id"],
      },
    };
    await tg.upsertEdge({
      id: "tedge_contract_1",
      fromNode: a.id,
      toNode: b.id,
      relation: "feeds",
      weight: 1.0,
      contract,
    });
    const edges = await tg.listEdges([a.id, b.id]);
    const ours = edges.find(
      (e) => e.fromNode === a.id && e.toNode === b.id && e.relation === "feeds",
    );
    assert.ok(ours, "edge must be persisted");
    assert.ok(ours!.contract, "contract must round-trip");
    assert.equal(
      ours!.contract!.produces.properties!["ensembl_id"]!.type,
      "string",
    );
  });

  // -------------------------------------------------- validateContract pure (#156)
  await t.test("validateContract flags missing fields and type mismatches", async () => {
    const okRes = tg.validateContract({
      produces: {
        type: "object",
        properties: { x: { type: "string" }, y: { type: "number" } },
      },
      consumes: {
        type: "object",
        properties: { x: { type: "string" } },
        required: ["x"],
      },
    });
    assert.equal(okRes.ok, true);

    const missing = tg.validateContract({
      produces: { type: "object", properties: { other: { type: "string" } } },
      consumes: {
        type: "object",
        properties: { x: { type: "string" } },
        required: ["x"],
      },
    });
    assert.equal(missing.ok, false);
    assert.ok(
      missing.issues.some((i) => i.field === "x" && i.reason === "missing_in_produces"),
    );

    const mismatch = tg.validateContract({
      produces: { type: "object", properties: { x: { type: "number" } } },
      consumes: {
        type: "object",
        properties: { x: { type: "string" } },
        required: ["x"],
      },
    });
    assert.equal(mismatch.ok, false);
    assert.ok(
      mismatch.issues.some((i) => i.field === "x" && i.reason === "type_mismatch"),
    );
  });

  // -------------------------------------------------- seed structural invariants (#156)
  await t.test("seed: every persisted edge carries a contract; aliases compose β‰₯2 atoms", async () => {
    const seedMod = await import("../tool-graph-seed");
    const { ATOMIC_NODES, ATOMIC_EDGES, TOOL_EDGES, ALIAS_TOOLS, DATA_SOURCES, deriveContract, aliasMembersOf } =
      seedMod.__SEED_INTERNALS__;

    // Universe of valid endpoint names: atoms βˆͺ aliases βˆͺ data sources.
    const atomNames = new Set(ATOMIC_NODES.map((n) => n.name));
    const aliasNames: Set<string> = ALIAS_TOOLS;
    const dataNames = new Set(DATA_SOURCES.map((d) => d.name));
    const known = new Set<string>([...atomNames, ...aliasNames, ...dataNames]);
    for (const edge of ATOMIC_EDGES) {
      assert.ok(
        known.has(edge.from),
        `ATOMIC_EDGES from "${edge.from}" must reference a known node`,
      );
      assert.ok(
        known.has(edge.to),
        `ATOMIC_EDGES to "${edge.to}" must reference a known node`,
      );
    }

    // Every alias must compose β‰₯2 atomic members (i.e. real decomposition,
    // not a degenerate one-step "alias"). ALIAS_TOOLS is a Set<string> of
    // alias names.
    for (const aliasName of ALIAS_TOOLS) {
      const members = aliasMembersOf(aliasName);
      assert.ok(
        members.length >= 2,
        `alias "${aliasName}" must compose β‰₯2 atoms, got ${members.length}`,
      );
    }

    // deriveContract on a known atomic feeds-edge must validate.
    // Pick the first feeds edge between two known atoms.
    const feeds = ATOMIC_EDGES.find(
      (e) =>
        e.relation === "feeds" &&
        atomNames.has(e.from) &&
        atomNames.has(e.to),
    );
    assert.ok(feeds, "expected at least one atom→atom feeds edge in ATOMIC_EDGES");
    const c = deriveContract(feeds!);
    assert.ok(c, "deriveContract must produce a contract for known feeds edge");
    const v = tg.validateContract(c);
    assert.equal(
      v.ok,
      true,
      `derived contract for ${feeds!.from}β†’${feeds!.to} must validate; issues=${JSON.stringify(v.issues)}`,
    );

    // Heuristic TOOL_EDGES are coarser (cross-domain adjacency) and per
    // #156 must NOT carry hand-authored contracts β€” they declare only
    // (from,to,relation) and the seed mechanically derives the contract
    // from each endpoint's declared schema. Assert the entries are
    // shape-only and that the persisted edges they produce all carry
    // a contract (see SEED_INTERNALS-backed structural test below).
    for (const e of TOOL_EDGES) {
      assert.ok(
        !("contract" in e),
        `TOOL_EDGES entry ${e.from}β†’${e.to} (${e.relation}) must NOT carry a hand-authored contract β€” derive from endpoint schemas instead`,
      );
    }

    // Substrate-drift regression (per #156 spec line 32). For every
    // backed_by edge whose upstream declares an explicit
    // `consumesSubstrateSchema`, the derived contract's `consumes`
    // side must match that declaration (not the substrate's
    // outputSchema). And the contract must validate as long as the
    // substrate produces every required substrate-row field.
    const backedBy = ATOMIC_EDGES.filter((e) => e.relation === "backed_by");
    let substrateChecks = 0;
    for (const e of backedBy) {
      const upNode = ATOMIC_NODES.find((n: { name: string }) => n.name === e.from);
      const dsNode = DATA_SOURCES.find((n: { name: string }) => n.name === e.to);
      if (!upNode || !dsNode || !upNode.consumesSubstrateSchema) continue;
      substrateChecks += 1;
      const derived = deriveContract(e);
      assert.deepEqual(
        derived.consumes,
        upNode.consumesSubstrateSchema,
        `backed_by ${e.from}β†’${e.to} consumes side must equal upstream's consumesSubstrateSchema`,
      );
      assert.deepEqual(
        derived.produces,
        dsNode.outputSchema,
        `backed_by ${e.from}β†’${e.to} produces side must equal substrate's outputSchema`,
      );
    }
    assert.ok(
      substrateChecks > 0,
      "expected at least one backed_by edge whose upstream declares consumesSubstrateSchema",
    );

    // Drift-injection: synthesize a fake atomic op whose
    // consumesSubstrateSchema requires a field the substrate does NOT
    // produce. The derived contract must FAIL structural validation β€”
    // proving substrate-shape drift is detectable at the seam.
    const fakeOp = {
      name: "__drift_test_op",
      capabilityTags: ["test"],
      sideEffectKind: "read" as const,
      ownerProcess: "node" as const,
      inputSchema: { type: "object" },
      outputSchema: { type: "object" },
      consumesSubstrateSchema: {
        type: "object",
        properties: {
          field_that_substrate_does_not_emit: { type: "string" },
        },
        required: ["field_that_substrate_does_not_emit"],
      },
    };
    const fakeContract: tg.ContractSpec = {
      produces: DATA_SOURCES[0].outputSchema as Record<string, unknown>,
      consumes: fakeOp.consumesSubstrateSchema as Record<string, unknown>,
    };
    const driftV = tg.validateContract(fakeContract);
    assert.equal(
      driftV.ok,
      false,
      "substrate-drift contract must fail structural validation",
    );
    assert.ok(
      driftV.issues.some(
        (i: { reason: string }) => i.reason === "missing_in_produces",
      ),
      "drift validation must report missing_in_produces",
    );
  });

  // -------------------------------------------------- planner provisional preservation (#156)
  await t.test("seed cleanup preserves provisional uncontracted edges", async () => {
    // Insert a provisional node + an uncontracted edge between two
    // provisional nodes. The seed's stale-cleanup must NOT delete it,
    // because both endpoints are outside the curated seed (TOOLS /
    // DATA_SOURCES / ATOMIC_NODES). This is the regression test for
    // the safety scope of the cleanup query.
    const provA = await tg.upsertNode({
      id: "tnode_prov_a",
      name: "provisional_atom_alpha",
      description: "Planner-extended provisional node A.",
      capabilityTags: ["custom_capability"],
      inputKind: "json",
      outputKind: "json",
      status: "provisional",
      ownerProcess: "node",
      specJson: { name: "provisional_atom_alpha" },
      createdBy: "planner",
    });
    const provB = await tg.upsertNode({
      id: "tnode_prov_b",
      name: "provisional_atom_beta",
      description: "Planner-extended provisional node B.",
      capabilityTags: ["custom_capability"],
      inputKind: "json",
      outputKind: "json",
      status: "provisional",
      ownerProcess: "node",
      specJson: { name: "provisional_atom_beta" },
      createdBy: "planner",
    });
    await tg.upsertEdge({
      id: "tedge_prov_uncontracted",
      fromNode: provA.id,
      toNode: provB.id,
      relation: "feeds",
      weight: 1.0,
      // contract intentionally omitted β€” provisional edges are free of
      // contracts until an admin verifies them.
    });

    // Run seed (full driver β€” touches the same DB).
    const seedMod = await import("../tool-graph-seed");
    await seedMod.seedToolGraph();

    // Per #156 spec line 82 ("Every edge ships with a contract or it
    // does not exist"), the seed cleanup must REMOVE this uncontracted
    // edge β€” even though both endpoints are provisional planner nodes.
    // The invariant is absolute; planner auto-extension code must
    // synthesize a contract before upserting any edge.
    const edges = await tg.listEdges([provA.id, provB.id]);
    const survived = edges.find(
      (e) => e.fromNode === provA.id && e.toNode === provB.id && e.relation === "feeds",
    );
    assert.equal(
      survived,
      undefined,
      "uncontracted edges must be removed by seed cleanup (spec invariant)",
    );
  });

  // -------------------------------------------------- seam health (#157)

  await t.test("seam-health detector: clean payload yields perfect score", async () => {
    const { evaluateSeam } = await import("../seam-health-detector");
    const r = evaluateSeam(
      {
        produces: { type: "object" },
        consumes: {
          type: "object",
          properties: { id: { type: "string" }, score: { type: "number" } },
          required: ["id"],
        },
      },
      { id: "abc", score: 0.5 },
    );
    assert.equal(r.missingFields.length, 0);
    assert.equal(r.contractIssues.length, 0);
    assert.equal(r.coverage, 1);
    assert.equal(r.healthScore, 1);
  });

  await t.test("seam-health detector: missing required field is flagged and scored down", async () => {
    const { evaluateSeam } = await import("../seam-health-detector");
    const r = evaluateSeam(
      {
        produces: { type: "object" },
        consumes: {
          type: "object",
          properties: { id: { type: "string" }, score: { type: "number" } },
          required: ["id", "score"],
        },
      },
      { id: "abc" },
    );
    assert.deepEqual(r.missingFields, ["score"]);
    assert.equal(r.coverage, 0.5);
    assert.ok(r.healthScore < 0.5, `expected healthScore < 0.5, got ${r.healthScore}`);
  });

  await t.test("seam-health detector: type mismatch raises contract issue", async () => {
    const { evaluateSeam } = await import("../seam-health-detector");
    const r = evaluateSeam(
      {
        produces: { type: "object" },
        consumes: {
          type: "object",
          properties: { items: { type: "array" }, name: { type: "string" } },
          required: ["items"],
        },
      },
      { items: "not-an-array", name: 42 },
    );
    assert.ok(r.contractIssues.includes("expected_array:items"));
    assert.ok(r.contractIssues.includes("type_mismatch:name"));
  });

  await t.test("seam-health detector: advisory contract suppresses contract issues", async () => {
    const { evaluateSeam } = await import("../seam-health-detector");
    const r = evaluateSeam(
      {
        produces: { type: "object" },
        consumes: {
          type: "object",
          properties: { items: { type: "array" } },
          required: ["items"],
        },
        mappingHints: { advisory: true },
      },
      { items: "broken" },
    );
    assert.deepEqual(r.contractIssues, []);
  });

  await t.test("seam-health detector: malformed contract degrades to empty sample (no throw)", async () => {
    const { evaluateSeam } = await import("../seam-health-detector");
    const r = evaluateSeam(null, { foo: 1 });
    assert.equal(r.healthScore, 1);
    assert.equal(r.coverage, 1);
  });

  await t.test("captureSeamHealth + recomputeEdgeHealth round-trip: missing field drops score, clean recovery raises EMA", async () => {
    // Set up a tiny seam: producer β†’ consumer with a contract requiring
    // `target.id`. Then write 3 broken samples followed by 3 clean
    // samples through the public capture API and watch the EMA move.
    const prod = await tg.upsertNode({
      id: "tnode_seam_prod",
      name: "seam_test_producer",
      description: "producer",
      capabilityTags: ["test_producer"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "seam_test_producer" },
      createdBy: "system",
    });
    const cons = await tg.upsertNode({
      id: "tnode_seam_cons",
      name: "seam_test_consumer",
      description: "consumer",
      capabilityTags: ["test_consumer"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "seam_test_consumer" },
      createdBy: "system",
    });
    await tg.upsertEdge({
      id: "tedge_seam_test",
      fromNode: prod.id,
      toNode: cons.id,
      relation: "feeds",
      weight: 1.0,
      contract: {
        produces: { type: "object" },
        consumes: {
          type: "object",
          properties: { id: { type: "string" }, score: { type: "number" } },
          required: ["id", "score"],
        },
      },
    });

    // 3 broken samples (missing score)
    for (let i = 0; i < 3; i++) {
      const r = await tg.captureSeamHealth({
        upstreamName: "seam_test_producer",
        downstreamName: "seam_test_consumer",
        payload: { id: `x${i}` },
      });
      assert.ok(r, "captureSeamHealth must return a result for declared edge");
      assert.deepEqual(r!.sample.missingFields, ["score"]);
    }
    let agg = await tg.recomputeEdgeHealth();
    assert.ok(agg.samplesFolded >= 3);
    let listed = await tg.listEdgeHealth();
    let mine = listed.find((h) => h.edgeId === "tedge_seam_test");
    assert.ok(mine, "edge health row must exist after recompute");
    assert.equal(mine!.traversalCount, 3);
    assert.ok(mine!.missingFieldCount >= 3);
    assert.ok(mine!.emaHealthScore < 0.7, `expected unhealthy EMA after misses, got ${mine!.emaHealthScore}`);
    assert.deepEqual(mine!.topMissingFields, { score: 3 });

    // listUnhealthyEdges surfaces it
    const bad = await tg.listUnhealthyEdges({ minTraversals: 1, maxHealthScore: 0.85 });
    assert.ok(bad.some((e) => e.edgeId === "tedge_seam_test"), "unhealthy listing must include the broken seam");

    // 3 clean samples β€” EMA should recover upward
    for (let i = 0; i < 3; i++) {
      await tg.captureSeamHealth({
        upstreamName: "seam_test_producer",
        downstreamName: "seam_test_consumer",
        payload: { id: `y${i}`, score: i * 0.1 },
      });
    }
    agg = await tg.recomputeEdgeHealth();
    listed = await tg.listEdgeHealth();
    mine = listed.find((h) => h.edgeId === "tedge_seam_test")!;
    assert.equal(mine.traversalCount, 6);
    assert.ok(
      mine.emaHealthScore > 0.4,
      `expected EMA to climb after clean samples, got ${mine.emaHealthScore}`,
    );

    // getEdgeHealth returns recent samples
    const detail = await tg.getEdgeHealth("tedge_seam_test", 10);
    assert.ok(detail.edge);
    assert.ok(detail.recent.length >= 1);
    assert.ok(
      typeof (detail.recent[0]!.payload as { health_score?: number }).health_score === "number",
    );
  });

  await t.test("captureSeamHealth is non-blocking when downstream is unknown", async () => {
    const r = await tg.captureSeamHealth({
      downstreamName: "definitely_does_not_exist",
      payload: { foo: 1 },
    });
    assert.equal(r, null);
  });

  // -------------------------------------------------- deprecation (#159)

  await t.test("classifier: cold node flagged after coldDays of inactivity", async () => {
    // Insert a verified node old enough to bypass the age cutoff and an
    // ancient invocation row. coldDays=30 β†’ 90 days old qualifies.
    const old = await tg.upsertNode({
      id: "tnode_cold_1",
      name: "cold_widget_search",
      description: "An unused capability.",
      capabilityTags: ["cold_widget"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "cold_widget_search" },
      createdBy: "system",
    });
    // Backdate created_at so the age filter inside runDeprecationDetector
    // accepts it.
    const ninetyDaysAgo = new Date(Date.now() - 90 * 86_400_000);
    await pool.query(
      `UPDATE "${schema}".tool_nodes SET created_at = $1, updated_at = $1 WHERE id = $2`,
      [ninetyDaysAgo, old.id],
    );
    await pool.query(
      `INSERT INTO "${schema}".tool_node_evidence (id, node_id, kind, payload, success, failure, created_at)
       VALUES ($1, $2, 'invocation', '{}'::jsonb, 1, 0, $3)`,
      ["tev_cold_old", old.id, ninetyDaysAgo],
    );
    const summary = await tg.runDeprecationDetector({ coldDays: 30 });
    assert.ok(summary.proposalsConsidered >= 1);
    const open = await tg.listDeprecationCandidates({ status: "open" });
    const mine = open.find((c) => c.nodeId === old.id);
    assert.ok(mine, "cold node must produce an open candidate");
    assert.equal(mine!.classification, "cold");
    const supportingMetrics = (mine!.proposalContext as { supportingMetrics: { invocationCount: number; daysSinceLastInvocation: number } }).supportingMetrics;
    assert.equal(supportingMetrics.invocationCount, 1);
    assert.ok(supportingMetrics.daysSinceLastInvocation! >= 30);
  });

  await t.test("classifier: redundant requires sibling AND health gap (false-positive guard)", async () => {
    // Two nodes with the same tag and similar invocation counts must NOT
    // be flagged as redundant β€” only when the sibling dominates by β‰₯10Γ—
    // AND has a healthy edge AND target has an unhealthy edge.
    const sibA = await tg.upsertNode({
      id: "tnode_redA",
      name: "popular_search",
      description: "Popular implementation.",
      capabilityTags: ["sibling_capability"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "popular_search" },
      createdBy: "system",
    });
    const sibB = await tg.upsertNode({
      id: "tnode_redB",
      name: "rival_search",
      description: "Equal-popularity rival.",
      capabilityTags: ["sibling_capability"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "rival_search" },
      createdBy: "system",
    });
    const ageOk = new Date(Date.now() - 90 * 86_400_000);
    await pool.query(
      `UPDATE "${schema}".tool_nodes SET created_at = $1 WHERE id IN ($2,$3)`,
      [ageOk, sibA.id, sibB.id],
    );
    // Equal traversals β†’ must not be flagged redundant for either.
    for (let i = 0; i < 5; i++) {
      await pool.query(
        `INSERT INTO "${schema}".tool_node_evidence (id, node_id, kind, payload, success, failure)
         VALUES ($1, $2, 'invocation', '{}'::jsonb, 1, 0)`,
        [`tev_eq_a_${i}`, sibA.id],
      );
      await pool.query(
        `INSERT INTO "${schema}".tool_node_evidence (id, node_id, kind, payload, success, failure)
         VALUES ($1, $2, 'invocation', '{}'::jsonb, 1, 0)`,
        [`tev_eq_b_${i}`, sibB.id],
      );
    }
    await tg.runDeprecationDetector({ coldDays: 30 });
    const cands = await tg.listDeprecationCandidates({ status: "any" });
    assert.ok(
      !cands.some((c) => c.classification === "redundant" && (c.nodeId === sibA.id || c.nodeId === sibB.id)),
      "evenly used siblings must not be flagged redundant",
    );
  });

  await t.test("classifier: superseded triggered by spec_json.replacedBy", async () => {
    const sup = await tg.upsertNode({
      id: "tnode_sup_1",
      name: "ancient_widget",
      description: "A node that has been spliced.",
      capabilityTags: ["splice_test"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "ancient_widget", replacedBy: "tnode_modern" },
      createdBy: "system",
    });
    const ageOk = new Date(Date.now() - 90 * 86_400_000);
    await pool.query(
      `UPDATE "${schema}".tool_nodes SET created_at = $1 WHERE id = $2`,
      [ageOk, sup.id],
    );
    await tg.runDeprecationDetector({ coldDays: 30 });
    const cands = await tg.listDeprecationCandidates({ status: "any" });
    const mine = cands.find((c) => c.nodeId === sup.id);
    assert.ok(mine, "spec_json.replacedBy must produce a candidate");
    assert.equal(mine!.classification, "superseded");
    const sm = (mine!.proposalContext as { supportingMetrics: { replacedBy?: string } }).supportingMetrics;
    assert.equal(sm.replacedBy, "tnode_modern");
  });

  await t.test("resolveSubgraph excludes deprecated nodes", async () => {
    // Promote one of our seeded literature nodes through the deprecation
    // flow and confirm it disappears from resolveSubgraph.
    const ageOk = new Date(Date.now() - 200 * 86_400_000);
    await pool.query(
      `UPDATE "${schema}".tool_nodes SET created_at = $1, updated_at = $1 WHERE id = $2`,
      [ageOk, pubmedId],
    );
    await tg.runDeprecationDetector({ coldDays: 30 });
    const cands = await tg.listDeprecationCandidates({ status: "open" });
    const mine = cands.find((c) => c.nodeId === pubmedId);
    assert.ok(mine, "pubmed seed must now be a candidate");
    await tg.deprecateNode(mine!.id, "tester");
    const sg = await tg.resolveSubgraph({ intentText: "find pubmed papers" });
    assert.ok(!sg.nodes.some((n) => n.id === pubmedId), "deprecated node must not appear in subgraph");

    // Direct reference helper exposes replacedBy when present.
    const direct = await tg.resolveNodeForDirectReference("search_pubmed");
    assert.ok(direct && direct.deprecated === true, "direct reference must flag deprecation");
  });

  await t.test("defer + re-arm windows are respected by the detector", async () => {
    const node = await tg.upsertNode({
      id: "tnode_defer_1",
      name: "defer_target",
      description: "Defer flow target.",
      capabilityTags: ["defer_capability"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "defer_target" },
      createdBy: "system",
    });
    const ageOk = new Date(Date.now() - 200 * 86_400_000);
    await pool.query(
      `UPDATE "${schema}".tool_nodes SET created_at = $1, updated_at = $1 WHERE id = $2`,
      [ageOk, node.id],
    );
    await tg.runDeprecationDetector({ coldDays: 30 });
    const opened = (await tg.listDeprecationCandidates({ status: "open" })).find(
      (c) => c.nodeId === node.id,
    );
    assert.ok(opened, "first detector pass must open the candidate");
    await tg.deferDeprecationCandidate(opened!.id, "tester", 14);
    const r1 = await tg.runDeprecationDetector({ coldDays: 30 });
    assert.ok(r1.skippedDeferred >= 1, "deferred candidate must be skipped");
    const stillDeferred = (
      await tg.listDeprecationCandidates({ status: "deferred" })
    ).find((c) => c.nodeId === node.id);
    assert.ok(stillDeferred, "row stays deferred when defer_until is in the future");

    // Reject path: re-arm window also blocks re-proposal.
    await tg.rejectDeprecationCandidate(opened!.id, "tester", 30);
    const r2 = await tg.runDeprecationDetector({ coldDays: 30 });
    assert.ok(r2.skippedReArm >= 1, "rejected candidate must be skipped");
  });

  await t.test("archive job moves rows transactionally and leaves nothing in hot tables", async () => {
    const node = await tg.upsertNode({
      id: "tnode_arch_1",
      name: "archive_target",
      description: "Archive flow target.",
      capabilityTags: ["arch_capability"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "archive_target" },
      createdBy: "system",
    });
    // Add an edge + an evidence row so all three archive tables receive
    // contributions.
    const peer = await tg.upsertNode({
      id: "tnode_arch_peer",
      name: "archive_peer",
      description: "peer",
      capabilityTags: ["arch_capability"],
      inputKind: "json",
      outputKind: "json",
      status: "verified",
      ownerProcess: "node",
      specJson: { name: "archive_peer" },
      createdBy: "system",
    });
    await tg.upsertEdge({
      id: "tedge_arch_1",
      fromNode: node.id,
      toNode: peer.id,
      relation: "feeds",
      weight: 1.0,
    });
    await pool.query(
      `INSERT INTO "${schema}".tool_node_evidence (id, node_id, kind, payload, success, failure)
       VALUES ($1, $2, 'invocation', '{}'::jsonb, 1, 0)`,
      ["tev_arch_1", node.id],
    );
    // Force-deprecate by creating a candidate row directly + approving.
    await pool.query(
      `INSERT INTO "${schema}".tool_deprecation_candidates (id, node_id, classification, status, proposal_context, created_at, updated_at)
       VALUES ($1, $2, 'cold', 'open', '{}'::jsonb, now(), now())`,
      ["tdep_arch_1", node.id],
    );
    await tg.deprecateNode("tdep_arch_1", "tester");
    // Backdate updated_at past the archive cutoff (default 180 days).
    await pool.query(
      `UPDATE "${schema}".tool_nodes SET updated_at = $1 WHERE id = $2`,
      [new Date(Date.now() - 200 * 86_400_000), node.id],
    );
    const r = await tg.runArchiveJob();
    assert.ok(r.nodesArchived >= 1, "archive job must move at least one node");
    assert.ok(r.edgesArchived >= 1);
    assert.ok(r.evidenceArchived >= 1);
    // Hot tables must be clean.
    const hotNode = await pool.query(
      `SELECT 1 FROM "${schema}".tool_nodes WHERE id = $1`,
      [node.id],
    );
    assert.equal(hotNode.rowCount, 0);
    const hotEdge = await pool.query(
      `SELECT 1 FROM "${schema}".tool_edges WHERE id = $1`,
      ["tedge_arch_1"],
    );
    assert.equal(hotEdge.rowCount, 0);
    const archNode = await pool.query(
      `SELECT 1 FROM "${schema}".tool_nodes_archive WHERE id = $1`,
      [node.id],
    );
    assert.equal(archNode.rowCount, 1);
    const archEdge = await pool.query(
      `SELECT 1 FROM "${schema}".tool_edges_archive WHERE id = $1`,
      ["tedge_arch_1"],
    );
    assert.equal(archEdge.rowCount, 1);
  });

  // -------------------------------------------------- spawn templates (#161)

  await t.test("schemaFingerprint strips field names but preserves structure", async () => {
    const st = await import("../spawn-templates");
    const a = st.schemaFingerprint({
      type: "object",
      properties: {
        target_id: { type: "string" },
        score: { type: "number" },
      },
      required: ["target_id"],
    });
    const b = st.schemaFingerprint({
      type: "object",
      properties: {
        // Different names, same structure β†’ same fingerprint.
        ensembl_id: { type: "string" },
        confidence: { type: "number" },
      },
      required: ["ensembl_id"],
    });
    assert.equal(a, b, "field names must not influence the fingerprint");

    // Type change must change the fingerprint.
    const c = st.schemaFingerprint({
      type: "object",
      properties: {
        target_id: { type: "integer" },
        score: { type: "number" },
      },
      required: ["target_id"],
    });
    assert.notEqual(a, c, "type changes must alter the fingerprint");
  });

  await t.test("computeSeamFingerprint is deterministic and salted", async () => {
    const st = await import("../spawn-templates");
    const input: import("../spawn-templates").SeamFingerprintInput = {
      failureMode: "missing_required_field",
      downstreamInputSchema: {
        type: "object",
        properties: { id: { type: "string" }, count: { type: "integer" } },
        required: ["id", "count"],
      },
      upstreamOutputSchema: {
        type: "object",
        properties: { id: { type: "string" }, extra: { type: "boolean" } },
      },
      missingRequiredFieldNames: ["count"],
      unusedFieldNames: ["extra"],
      capabilityTag: "demo_tag",
    };
    const fp1 = st.computeSeamFingerprint(input);
    const fp2 = st.computeSeamFingerprint(input);
    assert.equal(fp1.hash, fp2.hash, "same input β†’ same hash");
    assert.deepEqual(fp1.breakdown.missingFieldTypes, ["integer"]);
    assert.deepEqual(fp1.breakdown.unusedFieldTypes, ["boolean"]);
    // Salt change must alter the hash (we cannot mutate env safely;
    // exercise the canonical-string is identical instead).
    assert.equal(fp1.canonical, fp2.canonical);
    assert.match(fp1.hash, /^[0-9a-f]{64}$/);
  });

  await t.test("searchTemplates returns null when no rows exist", async () => {
    const st = await import("../spawn-templates");
    const r = await st.searchTemplates({
      failureMode: "capability_gap",
      downstreamInputSchema: { type: "object", properties: {} },
      capabilityTag: "no_match_tag_xyzzy",
    });
    assert.equal(r, null);
  });

  await t.test("persistTemplateOnPromote β†’ exact match found by searchTemplates", async () => {
    const st = await import("../spawn-templates");
    const fpInput: import("../spawn-templates").SeamFingerprintInput = {
      failureMode: "capability_gap",
      downstreamInputSchema: {
        type: "object",
        properties: { query: { type: "string" } },
        required: ["query"],
      },
      capabilityTag: "exact_match_test",
    };
    const persisted = await st.persistTemplateOnPromote({
      fingerprintInput: fpInput,
      promotedNodeName: "auto_exact_match_test_aaa111",
      promotedInputSchema: {
        type: "object",
        properties: { query: { type: "string" } },
      },
      promotedOutputSchema: { type: "object" },
      handlerSkeleton:
        "export async function invoke(args) { return { ok: true }; }",
      specSkeleton: {
        name: "auto_exact_match_test_aaa111",
        description: "templated handler",
        parameters: {
          type: "object",
          properties: { query: { type: "string" } },
          required: ["query"],
        },
      },
    });
    assert.ok(persisted, "template must persist");
    const match = await st.searchTemplates(fpInput);
    assert.ok(match, "exact match must be found");
    assert.equal(match!.strength, "exact");
    assert.equal(match!.score, 1);
    assert.equal(match!.template.id, persisted!.id);
  });

  await t.test("structurally identical seam across different tags is EXACT (tag is not in fingerprint)", async () => {
    const st = await import("../spawn-templates");
    const baseSchema = {
      type: "object" as const,
      properties: { id: { type: "string" as const } },
      required: ["id"],
    };
    await st.persistTemplateOnPromote({
      fingerprintInput: {
        failureMode: "missing_required_field",
        downstreamInputSchema: baseSchema,
        upstreamOutputSchema: { type: "object", properties: {} },
        missingRequiredFieldNames: ["id"],
        capabilityTag: "tag_alpha_for_exact_test",
      },
      promotedNodeName: "auto_tag_alpha_xxx",
      promotedInputSchema: baseSchema,
      promotedOutputSchema: { type: "object" },
      handlerSkeleton: "export async function invoke(a) { return { id: a.id }; }",
      specSkeleton: { name: "auto_tag_alpha_xxx", parameters: baseSchema },
    });
    const r = await st.searchTemplates({
      failureMode: "missing_required_field",
      downstreamInputSchema: baseSchema,
      upstreamOutputSchema: { type: "object", properties: {} },
      missingRequiredFieldNames: ["id"],
      capabilityTag: "tag_beta_for_exact_test",
    });
    assert.ok(r, "match must surface");
    assert.equal(r!.strength, "exact", "tag-only differences must collapse to exact");
    assert.equal(r!.score, 1);
  });

  await t.test("genuine NEAR match: same failure_mode + same downstream fp + Jaccard β‰₯ 0.8 over slot bag", async () => {
    const st = await import("../spawn-templates");
    const baseSchema = {
      type: "object" as const,
      properties: {
        a: { type: "string" as const },
        b: { type: "string" as const },
        c: { type: "string" as const },
        d: { type: "string" as const },
        e: { type: "string" as const },
      },
      required: ["a", "b", "c", "d", "e"],
    };
    // Seed a template with 4 missing fields.
    await st.persistTemplateOnPromote({
      fingerprintInput: {
        failureMode: "missing_required_field",
        downstreamInputSchema: baseSchema,
        upstreamOutputSchema: { type: "object", properties: {} },
        missingRequiredFieldNames: ["a", "b", "c", "d"],
        capabilityTag: "near_real_seed",
      },
      promotedNodeName: "auto_near_real_seed_xxx",
      promotedInputSchema: baseSchema,
      promotedOutputSchema: { type: "object" },
      handlerSkeleton: "export async function invoke(args) { return args; }",
      specSkeleton: { name: "auto_near_real_seed_xxx", parameters: baseSchema },
    });
    // Query with 5 missing fields (slot bag is βŠ‹ of seed) β†’ Jaccard
    // 4/5 = 0.8, exact key MISSES (different miss list β†’ different
    // canonical β†’ different hash) β†’ must hit near with score = 0.8.
    const r = await st.searchTemplates({
      failureMode: "missing_required_field",
      downstreamInputSchema: baseSchema,
      upstreamOutputSchema: { type: "object", properties: {} },
      missingRequiredFieldNames: ["a", "b", "c", "d", "e"],
      capabilityTag: "near_real_query",
    });
    assert.ok(r, "near match must surface");
    assert.equal(r!.strength, "near");
    assert.ok(r!.score >= 0.8 && r!.score < 1, `score must be in [0.8, 1) (got ${r!.score})`);
    assert.ok(
      r!.parameterizationGaps.includes("missing_field_types"),
      "missing_field_types must be flagged as a parameterization gap",
    );
  });

  await t.test("NO near match when downstream fingerprint differs (hard precondition)", async () => {
    const st = await import("../spawn-templates");
    const r = await st.searchTemplates({
      failureMode: "missing_required_field",
      downstreamInputSchema: {
        type: "object",
        properties: { totally_unique_field: { type: "boolean" } },
        required: ["totally_unique_field"],
      },
      upstreamOutputSchema: { type: "object", properties: {} },
      missingRequiredFieldNames: ["totally_unique_field"],
      capabilityTag: "downstream_fp_mismatch",
    });
    assert.equal(r, null, "downstream fingerprint mismatch must short-circuit near search");
  });

  await t.test("privacy: persisted template never contains user payload strings", async () => {
    const st = await import("../spawn-templates");
    // Inject "user data"-looking strings in the spec the reviewer
    // edited and into the handler skeleton. Ingestion must strip them.
    const userSecret = "user_secret_value_aaaaaaaaaaaaaaaaaaaa";
    const userExample = "patient-12345-PHI-record-DO-NOT-LEAK";
    const persisted = await st.persistTemplateOnPromote({
      fingerprintInput: {
        failureMode: "capability_gap",
        downstreamInputSchema: { type: "object", properties: {} },
        capabilityTag: "privacy_test_tag",
      },
      promotedNodeName: "auto_privacy_test_tag_zzz999",
      promotedInputSchema: { type: "object" },
      promotedOutputSchema: { type: "object" },
      handlerSkeleton: `export async function invoke(args) {
  const example = "${userExample}";
  return { example, secret: "${userSecret}" };
}`,
      specSkeleton: {
        name: "auto_privacy_test_tag_zzz999",
        description: "OK desc",
        parameters: { type: "object", properties: {} },
        // Reviewer left a comment field β€” must be dropped by allowlist.
        leaked_comment: userSecret,
        examples: [userExample],
      },
    });
    assert.ok(persisted);
    st.__assertNoUserDataLeaked(persisted!, [userSecret, userExample]);
  });

  await t.test("auto-demotion: low success rate after threshold offers triggers silent demote", async () => {
    const st = await import("../spawn-templates");
    const persisted = await st.persistTemplateOnPromote({
      fingerprintInput: {
        failureMode: "type_mismatch",
        downstreamInputSchema: {
          type: "object",
          properties: { x: { type: "string" } },
          required: ["x"],
        },
        capabilityTag: "demote_test_tag",
      },
      promotedNodeName: "auto_demote_test_xxx",
      promotedInputSchema: { type: "object" },
      promotedOutputSchema: { type: "object" },
      handlerSkeleton: "stub",
      specSkeleton: {},
    });
    assert.ok(persisted);
    // Set a low threshold via env then exercise.
    const prevMin = process.env["TEMPLATE_DEMOTE_MIN_OFFERS"];
    const prevRate = process.env["TEMPLATE_DEMOTE_SUCCESS_RATE"];
    process.env["TEMPLATE_DEMOTE_MIN_OFFERS"] = "5";
    process.env["TEMPLATE_DEMOTE_SUCCESS_RATE"] = "0.3";
    try {
      // Re-import to pick up env (module loads constants at import time
      // β€” for this test we simulate by issuing offers and choices then
      // calling maybeAutoDemote directly, which re-reads thresholds at
      // each call via constants β€” so we must reload the module).
      const stReload = await import("../spawn-templates");
      // Bump offered_count to 6 (above threshold) and success_count to 1
      // (rate ~ 0.17, below floor).
      for (let i = 0; i < 6; i += 1) {
        await stReload.recordTemplateOffered(persisted!.id);
      }
      await stReload.recordTemplatePromoteResult(persisted!.id);
      const r = await stReload.maybeAutoDemote(persisted!.id);
      assert.equal(r.demoted, true, "must demote when below floor");
      // Demoted templates excluded from search.
      const search = await stReload.searchTemplates({
        failureMode: "type_mismatch",
        downstreamInputSchema: {
          type: "object",
          properties: { x: { type: "string" } },
          required: ["x"],
        },
        capabilityTag: "demote_test_tag",
      });
      assert.equal(search, null, "demoted template must not be returned by searchTemplates");
    } finally {
      if (prevMin === undefined) delete process.env["TEMPLATE_DEMOTE_MIN_OFFERS"];
      else process.env["TEMPLATE_DEMOTE_MIN_OFFERS"] = prevMin;
      if (prevRate === undefined) delete process.env["TEMPLATE_DEMOTE_SUCCESS_RATE"];
      else process.env["TEMPLATE_DEMOTE_SUCCESS_RATE"] = prevRate;
    }
  });

  await t.test(
    "end-to-end: spawn β†’ promote β†’ next spawn for identical seam offers the template",
    async () => {
      // Use unique capability tags to avoid touching prior tests.
      const tag1 = "e2e_template_a";
      const tag2 = "e2e_template_b";
      // First spawn
      for (let i = 0; i < tg.GAP_AUTO_EXTEND_THRESHOLD; i += 1) {
        await tg.recordPlannerGap(tag1, { ctx: i });
      }
      const created1 = await tg.autoExtendIfNeeded();
      assert.ok(created1 >= 1);
      const provs1 = await tg.listNodes({ status: "provisional" });
      const target1 = provs1.find((n) => n.name.startsWith(`auto_${tag1}_`));
      assert.ok(target1, "first provisional must exist");
      // proposalContext must always be present (template_match may be
      // null or a match β€” earlier tests may have seeded templates).
      const ctx1 = (target1!.spec as { proposalContext?: { template_match?: unknown } })
        .proposalContext;
      assert.ok(ctx1, "proposalContext must be present on auto-spawned node");

      // Promote it. This persists a template into the library.
      const promoted = await tg.approveNode(target1!.id, {
        templateChoice: "fresh",
        reviewer: "tester",
      });
      assert.ok(promoted, "promotion must succeed");

      // Second spawn for an identical seam shape (different tag, same
      // structure) β†’ near match expected.
      for (let i = 0; i < tg.GAP_AUTO_EXTEND_THRESHOLD; i += 1) {
        await tg.recordPlannerGap(tag2, { ctx: i });
      }
      await tg.autoExtendIfNeeded();
      const provs2 = await tg.listNodes({ status: "provisional" });
      const target2 = provs2.find((n) => n.name.startsWith(`auto_${tag2}_`));
      assert.ok(target2, "second provisional must exist");
      const ctx2 = (
        target2!.spec as {
          proposalContext?: {
            template_match?: { template_id: string; strength: string } | null;
          };
        }
      ).proposalContext;
      assert.ok(
        ctx2 && ctx2.template_match,
        `second spawn must surface a template match (got ${JSON.stringify(ctx2)})`,
      );
      assert.ok(
        ctx2!.template_match!.strength === "exact" ||
          ctx2!.template_match!.strength === "near",
        "strength must be exact or near",
      );

      // Reviewer chooses Use+edit; template counters update.
      const tplId = ctx2!.template_match!.template_id;
      const before = await pool.query(
        `SELECT offered_count, reuse_count, success_count FROM "${schema}".tool_spawn_templates WHERE id = $1`,
        [tplId],
      );
      assert.ok(before.rows[0]);
      await tg.approveNode(target2!.id, {
        templateChoice: "use_edit",
        sourceTemplateId: tplId,
        reviewer: "tester",
      });
      const after = await pool.query(
        `SELECT offered_count, reuse_count, success_count FROM "${schema}".tool_spawn_templates WHERE id = $1`,
        [tplId],
      );
      assert.ok(
        after.rows[0].reuse_count > before.rows[0].reuse_count,
        "reuse_count must increment on use_edit",
      );
      assert.ok(
        after.rows[0].success_count > before.rows[0].success_count,
        "success_count must increment on promote-after-reuse",
      );
    },
  );

  // unused vars suppression
  void uniprotId;
  void summaryId;
  void pubmedId;
});