File size: 91,783 Bytes
d61eb28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// ============================================================================
//  SyFox — reason.hpp
// ---------------------------------------------------------------------------
//  v3.9.0 — THE CONSTRUCTIVE SUBSTRATE (SI Layer 2), ported at last.
//
//  This is the faithful port of the Synthetic-Intelligence reasoning stack —
//  src/unified.hpp (graph rewriter + backward chaining + contradiction guard),
//  src/primitives.hpp (the nine reasoning primitives), src/meta_learner.hpp
//  (rule induction, verifier, thinker index), src/inference.hpp (the six
//  discovery queries) and src/axiom_loader.hpp (the .axioms triple format) —
//  closing the gap the v3.9 audit named: syfox had the FIELD (Layer 1 energy
//  physics) but none of SI's Layer 2: no typed knowledge graph, no forward or
//  backward chaining, no proof traces, no rule induction, no discovery.
//
//  What is ported 1:1 (paper: synthetic-intelligence.md §3.4, §4.4, §5.3):
//    - Typed directed edges with FULL provenance (rule, parent_a, parent_b,
//      step) — every derived edge knows exactly which premises produced it,
//      so proofs are traceable and retraction is bounded (RADE semantics).
//    - Forward chaining to fixpoint under a tension budget (Axiom II:
//      |ΔG| <= alpha·T; here tension is an input, fed from the fabric layer).
//    - The NINE primitives: transitive, deduction (modus ponens),
//      inheritance (class-property flow), negation (contrapositive),
//      case analysis (disjunction elimination), induction (schematic
//      generalization with a witness count), analogy (structure-mapping edge
//      transfer), abduction (backward-only), recursion (implicit in the
//      fixpoint loop).
//    - Backward chaining with adaptive iterative deepening (progress-bounded,
//      not depth-bounded) and cycle protection via an active goal stack.
//    - Meta-learner: contiguous derivation sub-chains are counted; a chain
//      that saves >= promotion_threshold bits is promoted to a macro-rule.
//    - Verifier: a candidate answer edge is re-checked against the rule set
//      all the way back to its premises before an answer is emitted.
//    - Thinker index C/R (§3.4): composition rate vs retrieval rate.
//    - The SIX discovery queries: isolated subgraphs, dangling edges, logic
//      gap, symmetry gaps, anomalies, and the gap-query classifier.
//    - The .axioms triple format — SI's own data files (syllogism.axioms,
//      insulin_pathway.axioms, maxwell_equations.axioms, ...) load unchanged.
//
//  What is deliberately DIFFERENT from SI, and why:
//    - No RIFA/oscillator physics dependency. In syfox the Layer-1 physics is
//      the energy field (si_substrate.hpp); the reasoning budget takes
//      `tension` as an injected scalar (set_tension) so a RIFA-style tension
//      accumulator can drive it without coupling the two stores.
//    - Determinism hardening: primitive candidate sets are collected and
//      sorted by a total order before committing (SI iterated hash maps).
//      Same derivations, same budget arithmetic, one canonical order —
//      bit-stable across runs and thread counts by construction.
//    - Base-case edge lookup uses the out_adj index (O(deg)) instead of a
//      full edge scan; semantics identical (first matching edge).
//
//  Nothing here touches the settle physics (decay 0.82 / diffusion 0.45) or
//  the field readout. This file is Layer 2; the fabric is Layer 1; the bridge
//  (Engine::reason_bridge) lays fabric lanes for asserted axioms at learn
//  time and walks typed proof paths over settled fields at decide time.
//  No transformer, no neural network, no token prediction — counting,
//  joining, and traversal only.
// ============================================================================

#pragma once

#include <algorithm>
#include <array>
#include <cctype>
#include <cstdint>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <map>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace sxr {

// ---------------------------------------------------------------------------
// Relations. The base six are SI's unified.hpp set; the extension block is
// primitives.hpp verbatim (mechanistic, symbolic, code-structure,
// commonsense). A syfox axiom file may use ANY of these tokens.
// ---------------------------------------------------------------------------
enum RelId : int {
    REL_IS_A      = 0,   // "socrates is_a man"
    REL_HAS       = 1,   // "man has mortality"
    REL_IMPLIES   = 2,   // "raining implies wet"
    REL_PART_OF   = 3,   // "wheel part_of car"
    REL_EQUALS    = 4,   // "x equals 3"
    REL_CAUSES    = 5,   // "friction causes heat"
    REL_NOT       = 6,   // "socrates NOT is_a stone" (unary; X --not--> P)
    REL_OR        = 7,   // "wet_or_rainy OR (rainy, wet)"
    REL_HYPOTH    = 8,   // hypothesised edge tag (MAA volatile bridges)
    // mechanistic (drug repurposing / pathway reasoning)
    REL_BINDS_TO        = 9,
    REL_INHIBITS        = 10,
    REL_ACTIVATES       = 11,
    REL_TREATS          = 12,
    REL_CAUSES_DISEASE  = 13,
    REL_PART_OF_PATHWAY = 14,
    REL_EXPRESSED_IN    = 15,
    REL_METABOLIZES     = 16,
    REL_UPREGULATES     = 17,
    REL_DOWNREGULATES   = 18,
    // symbolic / physics
    REL_PROPORTIONAL_TO = 19,
    REL_MIRROR_OF       = 20,
    REL_DERIVES_FROM    = 21,
    REL_FUNDAMENTAL     = 22,
    REL_TRIGGERED_BY    = 23,
    // code structure
    REL_CALLS           = 24,
    REL_RETURNS         = 25,
    REL_TAKES_ARG       = 26,
    REL_PRECEDES        = 27,
    REL_IMPORTS         = 28,
    REL_MUTATES         = 29,
    // commonsense (testimonial support, never experimental)
    REL_USED_FOR        = 30,
    REL_CAPABLE_OF      = 31,
    REL_AT_LOCATION     = 32,
    REL_DESIRES         = 33,
    REL_CREATED_BY      = 34,
    REL_MAX             = 35
};

inline const char* rel_name(int r) {
    switch (r) {
        case REL_IS_A:              return "is_a";
        case REL_HAS:               return "has";
        case REL_IMPLIES:           return "implies";
        case REL_PART_OF:           return "part_of";
        case REL_EQUALS:            return "equals";
        case REL_CAUSES:            return "causes";
        case REL_NOT:               return "not";
        case REL_OR:                return "or";
        case REL_HYPOTH:            return "hyp";
        case REL_BINDS_TO:          return "binds_to";
        case REL_INHIBITS:          return "inhibits";
        case REL_ACTIVATES:         return "activates";
        case REL_TREATS:            return "treats";
        case REL_CAUSES_DISEASE:    return "causes_disease";
        case REL_PART_OF_PATHWAY:   return "part_of_pathway";
        case REL_EXPRESSED_IN:      return "expressed_in";
        case REL_METABOLIZES:       return "metabolizes";
        case REL_UPREGULATES:       return "upregulates";
        case REL_DOWNREGULATES:     return "downregulates";
        case REL_PROPORTIONAL_TO:   return "is_proportional_to";
        case REL_MIRROR_OF:         return "mirror_of";
        case REL_DERIVES_FROM:      return "derives_from";
        case REL_FUNDAMENTAL:       return "is_fundamental";
        case REL_TRIGGERED_BY:      return "triggered_by";
        case REL_CALLS:             return "calls";
        case REL_RETURNS:           return "returns";
        case REL_TAKES_ARG:         return "takes_arg";
        case REL_PRECEDES:          return "precedes";
        case REL_IMPORTS:           return "imports";
        case REL_MUTATES:           return "mutates";
        case REL_USED_FOR:          return "used_for";
        case REL_CAPABLE_OF:        return "capable_of";
        case REL_AT_LOCATION:       return "at_location";
        case REL_DESIRES:           return "desires";
        case REL_CREATED_BY:        return "created_by";
        default:                    return "?";
    }
}

inline int rel_from_token(const std::string& t) {
    for (int r = 0; r < REL_MAX; ++r)
        if (t == rel_name(r)) return r;
    return -1;
}

// Node types (unified.hpp).
enum NodeType : int {
    NODE_ENTITY   = 0,
    NODE_CLASS    = 1,
    NODE_PROPERTY = 2,
    NODE_ABSTRACT = 3,   // spawned by phase-space expansion
};

// ---------------------------------------------------------------------------
// Edge with full provenance. A premise has derived_by_rule == -1; a derived
// edge names the rule that fired and the two parent edges, so trace() can
// reconstruct the whole derivation chain and retraction is bounded.
// ---------------------------------------------------------------------------
struct Edge {
    int from;
    int to;
    int rel;
    int derived_by_rule = -1;
    int parent_a = -1;
    int parent_b = -1;
    int step = 0;
};

struct Node {
    int id = -1;
    std::string label;
    int type = NODE_ENTITY;
};

// ---------------------------------------------------------------------------
// The typed graph. O(1) existence via edge_key; O(deg) neighbourhood via
// out_adj / in_adj — the incremental-index design SI built when ConceptNet
// scale turned full scans fatal. All three are maintained in add_edge.
// ---------------------------------------------------------------------------
struct ConceptGraph {
    std::vector<Node> nodes;
    std::vector<Edge> edges;
    std::unordered_map<std::string, int> by_label;

    std::unordered_set<std::uint64_t> edge_key;
    std::vector<std::vector<int>> out_adj;
    std::vector<std::vector<int>> in_adj;

    static std::uint64_t ekey(int from, int to, int rel) {
        // rel < 64 for every relation above; 29 bits per endpoint.
        return ((std::uint64_t)(std::uint32_t)from << 35)
             | ((std::uint64_t)(std::uint32_t)to   << 6)
             | (std::uint64_t)(std::uint32_t)(rel & 0x3f);
    }

    int add_or_get_node(const std::string& label, int type = NODE_ENTITY) {
        auto it = by_label.find(label);
        if (it != by_label.end()) return it->second;
        int id = static_cast<int>(nodes.size());
        nodes.push_back(Node{id, label, type});
        by_label.emplace(label, id);
        return id;
    }

    bool has_edge(int from, int to, int rel) const {
        return edge_key.count(ekey(from, to, rel)) != 0;
    }

    // First matching edge index, or -1. Deterministic: out_adj preserves
    // insertion order, so the first edge added is the one found.
    int find_edge(int from, int to, int rel) const {
        if (from < 0 || from >= static_cast<int>(out_adj.size())) return -1;
        for (int ei : out_adj[static_cast<std::size_t>(from)]) {
            const Edge& e = edges[static_cast<std::size_t>(ei)];
            if (e.to == to && e.rel == rel) return ei;
        }
        return -1;
    }

    int add_edge(int from, int to, int rel,
                 int rule_idx = -1, int pa = -1, int pb = -1, int step = 0) {
        std::uint64_t k = ekey(from, to, rel);
        if (edge_key.count(k)) return -1;
        int id = static_cast<int>(edges.size());
        edges.push_back(Edge{from, to, rel, rule_idx, pa, pb, step});
        edge_key.insert(k);
        const std::size_t need = static_cast<std::size_t>(std::max(from, to)) + 1;
        if (out_adj.size() < need) { out_adj.resize(need); in_adj.resize(need); }
        out_adj[static_cast<std::size_t>(from)].push_back(id);
        in_adj[static_cast<std::size_t>(to)].push_back(id);
        return id;
    }

    // Bounded retraction (RADE persistence semantics): removing one wrong
    // premise edge makes every derived edge citing it unsupported. Returns
    // the number of edges removed. Provenance makes this local, not global.
    std::size_t retract(int edge_idx) {
        if (edge_idx < 0 || edge_idx >= static_cast<int>(edges.size())) return 0;
        std::vector<char> dead(edges.size(), 0);
        dead[static_cast<std::size_t>(edge_idx)] = 1;
        bool grew = true;
        std::size_t killed = 0;
        while (grew) {                     // bounded by the derivation DAG depth
            grew = false;
            for (std::size_t i = 0; i < edges.size(); ++i) {
                if (dead[i]) continue;
                const Edge& e = edges[i];
                if (e.derived_by_rule < 0) continue;
                const bool pa_dead = e.parent_a >= 0 &&
                    dead[static_cast<std::size_t>(e.parent_a)];
                const bool pb_dead = e.parent_b >= 0 &&
                    dead[static_cast<std::size_t>(e.parent_b)];
                if (pa_dead || pb_dead) { dead[i] = 1; grew = true; }
            }
        }
        std::vector<Edge> keep;
        keep.reserve(edges.size());
        for (std::size_t i = 0; i < edges.size(); ++i) {
            if (dead[i]) { ++killed; continue; }
            keep.push_back(edges[i]);
        }
        edges.swap(keep);
        reindex();
        return killed;
    }

    void reindex() {
        edge_key.clear();
        out_adj.assign(nodes.size(), {});
        in_adj.assign(nodes.size(), {});
        for (std::size_t i = 0; i < edges.size(); ++i) {
            const Edge& e = edges[i];
            edge_key.insert(ekey(e.from, e.to, e.rel));
            const std::size_t need = static_cast<std::size_t>(std::max(e.from, e.to)) + 1;
            if (out_adj.size() < need) { out_adj.resize(need); in_adj.resize(need); }
            out_adj[static_cast<std::size_t>(e.from)].push_back(static_cast<int>(i));
            in_adj[static_cast<std::size_t>(e.to)].push_back(static_cast<int>(i));
        }
    }
};

// ---------------------------------------------------------------------------
// A rewrite rule: (a --rel_ab--> b) + (b --rel_bc--> c) => (a --rel_ac--> c).
// Cost is deducted from the tension budget per successful application.
// ---------------------------------------------------------------------------
struct Rule {
    std::string name;
    int rel_ab;
    int rel_bc;
    int rel_ac;
    float cost = 1.0f;
    bool spawn_intermediate = false;   // Axiom II: phase space grows in NODES
};

// One rewriting pass over a snapshot (rules never trigger themselves within
// a pass). Returns edges added.
inline int apply_rules_once(std::vector<Rule>& rules, ConceptGraph& g,
                            float& budget, int step) {
    int added = 0;
    const std::size_t snap = g.edges.size();
    for (std::size_t ri = 0; ri < rules.size(); ++ri) {
        const Rule& r = rules[ri];
        if (budget < r.cost) continue;
        for (std::size_t i = 0; i < snap; ++i) {
            const Edge& e1 = g.edges[i];
            if (e1.rel != r.rel_ab) continue;
            if (e1.to < 0 || static_cast<std::size_t>(e1.to) >= g.out_adj.size()) continue;
            const std::size_t kn = g.out_adj[static_cast<std::size_t>(e1.to)].size();
            for (std::size_t k = 0; k < kn; ++k) {
                const std::size_t j = static_cast<std::size_t>(
                    g.out_adj[static_cast<std::size_t>(e1.to)][k]);
                if (j >= snap) continue;
                const Edge& e2 = g.edges[j];
                if (e2.rel != r.rel_bc) continue;
                if (g.has_edge(e1.from, e2.to, r.rel_ac)) continue;
                if (r.spawn_intermediate) {
                    const std::string lbl = std::string("[") + rel_name(r.rel_ac)
                        + ":" + g.nodes[static_cast<std::size_t>(e1.from)].label
                        + "->" + g.nodes[static_cast<std::size_t>(e2.to)].label + "]";
                    const int mid = g.add_or_get_node(lbl, NODE_ABSTRACT);
                    g.add_edge(e1.from, mid, r.rel_ac, static_cast<int>(ri),
                               static_cast<int>(i), static_cast<int>(j), step);
                    g.add_edge(mid, e2.to, r.rel_ac, static_cast<int>(ri),
                               static_cast<int>(i), static_cast<int>(j), step);
                    added += 2;
                } else {
                    g.add_edge(e1.from, e2.to, r.rel_ac, static_cast<int>(ri),
                               static_cast<int>(i), static_cast<int>(j), step);
                    ++added;
                }
                budget -= r.cost;
                if (budget < r.cost) break;
            }
            if (budget < r.cost) break;
        }
    }
    return added;
}

// ---------------------------------------------------------------------------
// The nine primitive kinds (primitives.hpp §4.4). PRIM_ABDUCTION is realized
// by backward chaining (goal-directed) and PRIM_RECURSION by the fixpoint
// loop; both are structural, not separate fire functions.
// ---------------------------------------------------------------------------
enum PrimitiveKind {
    PRIM_TRANSITIVE    = 0,
    PRIM_DEDUCTION     = 1,
    PRIM_INHERITANCE   = 2,
    PRIM_NEGATION      = 3,
    PRIM_CASE_ANALYSIS = 4,
    PRIM_INDUCTION     = 5,
    PRIM_ANALOGY       = 6,
    PRIM_ABDUCTION     = 7,   // backward-chaining side
    PRIM_RECURSION     = 8    // the fixpoint loop itself
};

struct PrimRuleMeta {
    PrimitiveKind kind = PRIM_TRANSITIVE;
    int rel_extra = -1;      // NEGATION: the negated rel; INDUCTION: property
    int min_witnesses = 3;   // INDUCTION only
};

// ---------------------------------------------------------------------------
// The reasoning engine (SI's ExtSI, self-contained).
// ---------------------------------------------------------------------------
struct ReasonCore {
    ConceptGraph graph;
    std::vector<Rule> rules;
    std::vector<PrimRuleMeta> prim_meta;

    float base_budget = 100.0f;    // reasoning-step budget scale
    float tension = 0.0f;          // injected Layer-1 tension (RIFA IC port)
    int   max_steps = 1 << 20;     // runaway guard; engine self-terminates
    bool  bulk_load = false;       // seeding is not lived experience

    // -- rule registration --------------------------------------------------
    void add_rule(const std::string& name, int rel_ab, int rel_bc, int rel_ac,
                  bool spawn = false, float cost = 1.0f) {
        rules.push_back(Rule{name, rel_ab, rel_bc, rel_ac, cost, spawn});
        while (prim_meta.size() < rules.size()) prim_meta.push_back({});
    }
    // M2: rule ids are positional and shift whenever the default set gains a
    // primitive, so persisted state must be keyed by NAME. This lookup is the
    // bridge between a loaded name and the current positional id.
    int rule_index_by_name(const std::string& name) const {
        for (std::size_t i = 0; i < rules.size(); ++i)
            if (rules[i].name == name) return static_cast<int>(i);
        return -1;
    }

    void add_primitive(const std::string& name, PrimitiveKind kind,
                       int rel_ab, int rel_bc, int rel_ac,
                       int rel_extra = -1, int min_witnesses = 3) {
        add_rule(name, rel_ab, rel_bc, rel_ac);
        // add_rule padded prim_meta with a default slot for this rule —
        // overwrite it with the real kind (keeps prim_meta 1:1 with rules;
        // a first cut pushed a SECOND slot here and shifted every later
        // primitive's kind off by one — the contrapositive and induction
        // never fired, caught by test_reason).
        prim_meta.back() = {kind, rel_extra, min_witnesses};
    }
    void add_transitive(const std::string& name, int a, int b, int c) {
        add_primitive(name, PRIM_TRANSITIVE, a, b, c);
    }
    void add_deduction(const std::string& name) {
        add_primitive(name, PRIM_DEDUCTION, REL_IMPLIES, REL_IMPLIES, REL_IMPLIES);
    }
    void add_inheritance(const std::string& name, int property_rel) {
        add_primitive(name, PRIM_INHERITANCE, REL_IS_A, property_rel, property_rel);
    }
    void add_negation(const std::string& name, int implies_rel = REL_IMPLIES) {
        add_primitive(name, PRIM_NEGATION, implies_rel, REL_NOT, REL_NOT);
    }
    void add_case_analysis(const std::string& name, int consequence = REL_IMPLIES) {
        add_primitive(name, PRIM_CASE_ANALYSIS, REL_OR, consequence, consequence);
    }
    void add_induction(const std::string& name, int property_rel, int witnesses = 3) {
        add_primitive(name, PRIM_INDUCTION, REL_IS_A, property_rel, property_rel,
                      property_rel, witnesses);
    }
    void add_analogy(const std::string& name, int mapping_rel = REL_IS_A) {
        add_primitive(name, PRIM_ANALOGY, mapping_rel, mapping_rel, mapping_rel);
    }

    // SI's default rule set (unified.cpp construction, ported): taxonomy
    // flows down classes, chains compose, causes propagate through parts.
    void add_default_rules() {
        add_transitive("transitive_is_a", REL_IS_A, REL_IS_A, REL_IS_A);
        add_deduction("modus_ponens");
        add_transitive("implies_chain", REL_IMPLIES, REL_IMPLIES, REL_IMPLIES);
        add_transitive("causes_chain", REL_CAUSES, REL_CAUSES, REL_CAUSES);
        add_inheritance("class_has_flows_down", REL_HAS);
        add_inheritance("class_causes_flows_down", REL_CAUSES);
        add_negation("contrapositive");
        add_case_analysis("disjunction_elimination");
        add_induction("schematic_generalization", REL_HAS, 3);
        // M1: PRIM_ANALOGY was DEFINED but never added to the default set, so
        // the advertised "nine primitives" were eight at runtime and
        // CHANGELOG v3.9.0's claim was false on the live binary. add_analogy()
        // only fires from a graph that contains it, so wiring it here is
        // inert on fabrics with no structural gap to bridge -- which is the
        // correct behaviour, and restores doc/code parity.
        add_analogy("structural_analogy");
    }

    // -- assertions ----------------------------------------------------------
    int assert_fact(const std::string& subj, int rel, const std::string& obj,
                    int subj_type = NODE_ENTITY, int obj_type = NODE_CLASS) {
        const int a = graph.add_or_get_node(subj, subj_type);
        const int b = graph.add_or_get_node(obj, obj_type);
        return graph.add_edge(a, b, rel);
    }

    // Tension-bounded budget (Axiom II in code): |ΔG| <= alpha·T.
    float compute_budget() const {
        return base_budget * (1.0f + 2.0f * tension);
    }

    // -- forward chaining to fixpoint ----------------------------------------
    struct ReasonReport { int edges_added; int steps_used; float budget_left; };
    ReasonReport reason() {
        float budget = compute_budget();
        int total = 0, step = 0;
        for (; step < max_steps; ++step) {
            const int add = apply_rules_once(rules, graph, budget, step + 1);
            if (add == 0) break;
            total += add;
        }
        return {total, step, budget};
    }

    // -- the nine primitives, per kind ---------------------------------------
    // Provenance-preserving join: each candidate carries its two parent edge
    // indices, so every committed edge is traceable. Candidates are sorted by
    // a total order before committing (determinism hardening over SI's hash
    // iteration); parents always have indices < snap and stay valid because
    // edges are only ever appended.

    struct Cand {
        int from, to;
        int pa, pb;
        std::uint64_t key() const {
            return (static_cast<std::uint64_t>(static_cast<std::uint32_t>(from)) << 32)
                 | static_cast<std::uint32_t>(to);
        }
    };

    // 1+2. DEDUCTION / TRANSITIVE — join on e1.to == e2.from.
    int fire_deduction(std::size_t rule_idx, float& budget, int step) {
        const Rule& r = rules[rule_idx];
        if (budget < r.cost) return 0;
        std::vector<Cand> cands;
        const std::size_t snap = graph.edges.size();
        for (std::size_t i = 0; i < snap; ++i) {
            const Edge& e1 = graph.edges[i];
            if (e1.rel != r.rel_ab) continue;
            if (e1.to < 0 || static_cast<std::size_t>(e1.to) >= graph.out_adj.size()) continue;
            const std::size_t kn = graph.out_adj[static_cast<std::size_t>(e1.to)].size();
            for (std::size_t k = 0; k < kn; ++k) {
                const std::size_t j = static_cast<std::size_t>(
                    graph.out_adj[static_cast<std::size_t>(e1.to)][k]);
                if (j >= snap) continue;
                const Edge& e2 = graph.edges[j];
                if (e2.rel != r.rel_bc) continue;
                if (graph.has_edge(e1.from, e2.to, r.rel_ac)) continue;
                cands.push_back({e1.from, e2.to, static_cast<int>(i), static_cast<int>(j)});
            }
        }
        return commit_sorted(cands, rule_idx, r, budget, step);
    }

    // 3. INHERITANCE — (X is_a C) + (C prop Y) => (X prop Y): class properties
    //    flow to members for ANY relation, unlike transitive's matched rels.
    int fire_inheritance(std::size_t rule_idx, float& budget, int step) {
        const Rule& r = rules[rule_idx];    // rel_ab=IS_A, rel_bc=rel_ac=prop
        if (budget < r.cost) return 0;
        std::vector<Cand> cands;
        const std::size_t snap = graph.edges.size();
        for (std::size_t i = 0; i < snap; ++i) {
            const Edge& e_is_a = graph.edges[i];
            if (e_is_a.rel != REL_IS_A) continue;
            if (e_is_a.to < 0 || static_cast<std::size_t>(e_is_a.to) >= graph.out_adj.size()) continue;
            const std::size_t kn = graph.out_adj[static_cast<std::size_t>(e_is_a.to)].size();
            for (std::size_t k = 0; k < kn; ++k) {
                const std::size_t j = static_cast<std::size_t>(
                    graph.out_adj[static_cast<std::size_t>(e_is_a.to)][k]);
                if (j >= snap) continue;
                const Edge& e_prop = graph.edges[j];
                if (e_prop.rel != r.rel_bc) continue;
                if (graph.has_edge(e_is_a.from, e_prop.to, r.rel_ac)) continue;
                cands.push_back({e_is_a.from, e_prop.to, static_cast<int>(i), static_cast<int>(j)});
            }
        }
        return commit_sorted(cands, rule_idx, r, budget, step);
    }

    // 4. NEGATION — contrapositive: (P implies Q) + (X not Q) => (X not P).
    int fire_negation(std::size_t rule_idx, float& budget, int step) {
        const Rule& r = rules[rule_idx];
        if (budget < r.cost) return 0;
        std::vector<Cand> cands;
        const std::size_t snap = graph.edges.size();
        for (std::size_t i = 0; i < snap; ++i) {
            const Edge& e_imp = graph.edges[i];
            if (e_imp.rel != r.rel_ab) continue;         // (P implies Q)
            const int P = e_imp.from, Q = e_imp.to;
            for (std::size_t j = 0; j < snap; ++j) {
                const Edge& e_not = graph.edges[j];
                if (e_not.rel != REL_NOT) continue;
                if (e_not.to != Q) continue;
                const int X = e_not.from;
                if (graph.has_edge(X, P, REL_NOT)) continue;
                cands.push_back({X, P, static_cast<int>(i), static_cast<int>(j)});
            }
        }
        return commit_sorted(cands, rule_idx, r, budget, step);
    }

    // 5. CASE_ANALYSIS — disjunction elimination: (X or d_i) for all i, and
    //    every (d_i implies R) => (X implies R). ALL disjuncts must reach R.
    int fire_case_analysis(std::size_t rule_idx, float& budget, int step) {
        const Rule& r = rules[rule_idx];
        if (budget < r.cost) return 0;
        // Group OR edges by source X (std::map: deterministic order).
        std::map<int, std::vector<int>> or_by_X;
        const std::size_t snap = graph.edges.size();
        for (std::size_t i = 0; i < snap; ++i) {
            const Edge& e = graph.edges[i];
            if (e.rel == REL_OR) or_by_X[e.from].push_back(static_cast<int>(i));
        }
        std::vector<Cand> cands;
        for (const auto& kv : or_by_X) {
            const int X = kv.first;
            const auto& or_edges = kv.second;
            if (or_edges.size() < 2) continue;           // need a real disjunction
            std::map<int, int> r_hits;                   // R -> disjunct coverage
            for (int oe : or_edges) {
                const int d = graph.edges[static_cast<std::size_t>(oe)].to;
                for (std::size_t j = 0; j < snap; ++j) {
                    const Edge& e_imp = graph.edges[j];
                    if (e_imp.rel != r.rel_bc) continue;
                    if (e_imp.from != d) continue;
                    r_hits[e_imp.to] += 1;
                }
            }
            for (const auto& rh : r_hits) {
                if (rh.second < static_cast<int>(or_edges.size())) continue;
                if (graph.has_edge(X, rh.first, r.rel_ac)) continue;
                cands.push_back({X, rh.first, or_edges.front(), -1});
            }
        }
        return commit_sorted(cands, rule_idx, r, budget, step);
    }

    // 6. INDUCTION — schematic generalization: >= min_witnesses members X_i
    //    of class C sharing property (X_i prop Y) => (C prop Y).
    int fire_induction(std::size_t rule_idx, float& budget, int step) {
        const Rule& r = rules[rule_idx];
        const auto& meta = prim_meta[rule_idx];
        const int property = meta.rel_extra;
        if (budget < r.cost) return 0;
        std::map<std::uint64_t, int> counts;             // (C<<32|Y) -> witnesses
        std::map<std::uint64_t, int> witness;            // provenance edge
        const std::size_t snap = graph.edges.size();
        for (std::size_t i = 0; i < snap; ++i) {
            const Edge& e_is_a = graph.edges[i];
            if (e_is_a.rel != REL_IS_A) continue;
            const int Xi = e_is_a.from, C = e_is_a.to;
            if (Xi < 0 || static_cast<std::size_t>(Xi) >= graph.out_adj.size()) continue;
            const std::size_t kn = graph.out_adj[static_cast<std::size_t>(Xi)].size();
            for (std::size_t k = 0; k < kn; ++k) {
                const std::size_t j = static_cast<std::size_t>(
                    graph.out_adj[static_cast<std::size_t>(Xi)][k]);
                if (j >= snap) continue;
                const Edge& e_prop = graph.edges[j];
                if (e_prop.rel != property) continue;
                const std::uint64_t key =
                    (static_cast<std::uint64_t>(static_cast<std::uint32_t>(C)) << 32)
                  | static_cast<std::uint32_t>(e_prop.to);
                counts[key] += 1;
                witness[key] = static_cast<int>(i);
            }
        }
        std::vector<Cand> cands;
        for (const auto& kv : counts) {
            if (kv.second < meta.min_witnesses) continue;
            const int C = static_cast<int>(kv.first >> 32);
            const int Y = static_cast<int>(kv.first & 0xffffffffull);
            if (graph.has_edge(C, Y, property)) continue;
            cands.push_back({C, Y, witness[kv.first], -1});
        }
        return commit_sorted(cands, rule_idx, r, budget, step);
    }

    // 7. ANALOGY — structure-preserving edge transfer across a mapping edge
    //    (X --mapping--> X'): every outgoing edge of X transfers to X'.
    int fire_analogy(std::size_t rule_idx, float& budget, int step) {
        const Rule& r = rules[rule_idx];
        if (budget < r.cost) return 0;
        const int mapping = r.rel_ab;
        std::vector<std::pair<int,int>> pairs;
        const std::size_t snap = graph.edges.size();
        for (std::size_t i = 0; i < snap; ++i) {
            const Edge& e = graph.edges[i];
            if (e.rel == mapping) pairs.emplace_back(e.from, e.to);
        }
        std::vector<Cand> cands;
        for (const auto& mp : pairs) {
            const int X = mp.first, Xp = mp.second;
            if (X < 0 || static_cast<std::size_t>(X) >= graph.out_adj.size()) continue;
            const std::size_t kn = graph.out_adj[static_cast<std::size_t>(X)].size();
            for (std::size_t k = 0; k < kn; ++k) {
                const std::size_t j = static_cast<std::size_t>(
                    graph.out_adj[static_cast<std::size_t>(X)][k]);
                if (j >= snap) continue;
                const Edge& e = graph.edges[j];
                if (e.rel == mapping) continue;          // never copy the mapping
                if (e.to == Xp) continue;                // no trivial self-loops
                if (graph.has_edge(Xp, e.to, e.rel)) continue;
                cands.push_back({Xp, e.to, static_cast<int>(j), -1});
            }
        }
        return commit_sorted(cands, rule_idx, r, budget, step);
    }

    // Canonical commit path: sort by (from, to), dedup, then add with budget.
    int commit_sorted(std::vector<Cand>& cands, std::size_t rule_idx,
                      const Rule& r, float& budget, int step) {
        std::sort(cands.begin(), cands.end(), [](const Cand& a, const Cand& b) {
            if (a.from != b.from) return a.from < b.from;
            return a.to < b.to;
        });
        cands.erase(std::unique(cands.begin(), cands.end(),
                     [](const Cand& a, const Cand& b) {
                         return a.from == b.from && a.to == b.to; }),
                    cands.end());
        int added = 0;
        for (const auto& c : cands) {
            if (budget < r.cost) break;
            if (graph.has_edge(c.from, c.to, r.rel_ac)) continue;
            graph.add_edge(c.from, c.to, r.rel_ac,
                           static_cast<int>(rule_idx), c.pa, c.pb, step);
            ++added;
            budget -= r.cost;
        }
        return added;
    }

    // The extended orchestrator: every primitive kind fires per pass.
    int apply_all_primitives(float& budget, int step) {
        while (prim_meta.size() < rules.size()) prim_meta.push_back({});
        int added = 0;
        for (std::size_t ri = 0; ri < rules.size(); ++ri) {
            switch (prim_meta[ri].kind) {
                case PRIM_TRANSITIVE:
                case PRIM_DEDUCTION:
                    added += fire_deduction(ri, budget, step);   break;
                case PRIM_INHERITANCE:
                    added += fire_inheritance(ri, budget, step); break;
                case PRIM_NEGATION:
                    added += fire_negation(ri, budget, step);    break;
                case PRIM_CASE_ANALYSIS:
                    added += fire_case_analysis(ri, budget, step); break;
                case PRIM_INDUCTION:
                    added += fire_induction(ri, budget, step);   break;
                case PRIM_ANALOGY:
                    added += fire_analogy(ri, budget, step);     break;
                case PRIM_ABDUCTION:
                case PRIM_RECURSION:
                    break;   // structural: backward chaining / the loop itself
            }
        }
        return added;
    }

    ReasonReport reason_ext() {
        float budget = compute_budget();
        int total = 0, step = 0;
        for (; step < max_steps; ++step) {
            const int add = apply_all_primitives(budget, step + 1);
            if (add == 0) break;
            total += add;
        }
        return {total, step, budget};
    }

    // -- PROOF TRACES (§5.3 query side) --------------------------------------
    // Reconstruct a derivation chain by walking parent edges to premises.
    // Premises first, conclusion last, deduplicated, order-stable.
    std::vector<int> trace(int edge_idx) const {
        std::vector<int> out;
        if (edge_idx < 0 || edge_idx >= static_cast<int>(graph.edges.size())) return out;
        std::vector<int> stack{edge_idx};
        while (!stack.empty()) {
            const int e = stack.back(); stack.pop_back();
            out.push_back(e);
            const Edge& ee = graph.edges[static_cast<std::size_t>(e)];
            if (ee.parent_a >= 0) stack.push_back(ee.parent_a);
            if (ee.parent_b >= 0) stack.push_back(ee.parent_b);
        }
        std::reverse(out.begin(), out.end());
        std::vector<int> seen;
        for (int e : out)
            if (std::find(seen.begin(), seen.end(), e) == seen.end())
                seen.push_back(e);
        return seen;
    }

    // -- QUERY ----------------------------------------------------------------
    struct QueryResult {
        std::string translation;
        bool  holds = false;
        float coherence = 0.0f;      // 1 = both concepts known, 0.5 no-proof
        int   proof_edge = -1;
        std::vector<int> proof;
        int   steps_used = 0;
        bool  ood = false;           // subject or object not in the graph
    };

    QueryResult query(const std::string& subj, int rel, const std::string& obj) const {
        QueryResult r;
        const auto it_s = graph.by_label.find(subj);
        const auto it_o = graph.by_label.find(obj);
        if (it_s == graph.by_label.end() || it_o == graph.by_label.end()) {
            r.holds = false; r.coherence = 0.0f; r.ood = true;
            r.translation = std::string("unknown (out of ontology): ") + subj
                          + " " + rel_name(rel) + " " + obj;
            return r;
        }
        const int e_idx = graph.find_edge(it_s->second, it_o->second, rel);
        r.holds = (e_idx >= 0);
        r.proof_edge = e_idx;
        if (r.holds) {
            r.coherence = 1.0f;
            r.proof = trace(e_idx);
            r.translation = "yes: " + subj + " " + rel_name(rel) + " " + obj;
        } else {
            r.coherence = 0.5f;
            r.translation = "no: " + subj + " " + rel_name(rel) + " " + obj
                          + " does not follow";
        }
        return r;
    }

    QueryResult ask(const std::string& subj, int rel, const std::string& obj) {
        const ReasonReport rep = reason_ext();
        QueryResult q = query(subj, rel, obj);
        q.steps_used = rep.steps_used;
        return q;
    }

    // -- BACKWARD CHAINING (§5.3.2 + adaptive deepening) ----------------------
    // Goal-directed: prove (from --rel--> to) either from an existing edge or
    // by finding an intermediate X where both subgoals prove recursively.
    // Cycle protection via the ACTIVE goal stack (RAII pop — a failed subgoal
    // does not block alternative paths). Adaptive wrapper doubles the depth
    // while the last level still produced real derivations.
    struct Goal { int from; int rel; int to; };

    QueryResult ask_backward(const std::string& subj, int rel,
                             const std::string& obj, int max_depth = 8) {
        QueryResult r;
        const auto it_s = graph.by_label.find(subj);
        const auto it_o = graph.by_label.find(obj);
        if (it_s == graph.by_label.end() || it_o == graph.by_label.end()) {
            r.holds = false; r.coherence = 0.0f; r.ood = true;
            r.translation = std::string("unknown (out of ontology): ") + subj
                          + " " + rel_name(rel) + " " + obj;
            return r;
        }
        std::vector<Goal> visited;
        const int e = prove_backward(it_s->second, rel, it_o->second,
                                     max_depth, visited);
        if (e >= 0) {
            r.holds = true; r.coherence = 1.0f;
            r.proof_edge = e;
            r.proof = trace(e);
            r.translation = "yes: " + subj + " " + rel_name(rel) + " " + obj;
        } else {
            r.holds = false; r.coherence = 0.3f;
            r.translation = "no: " + subj + " " + rel_name(rel) + " " + obj
                          + " does not follow";
        }
        return r;
    }

    QueryResult ask_backward_adaptive(const std::string& subj, int rel,
                                      const std::string& obj,
                                      int hard_ceiling = 1 << 20) {
        int prev_count = static_cast<int>(graph.edges.size());
        QueryResult r;
        int stagnant = 0;
        for (int depth = 2; depth <= hard_ceiling; depth <<= 1) {
            r = ask_backward(subj, rel, obj, depth);
            r.steps_used = depth;
            if (r.holds) return r;
            const int now = static_cast<int>(graph.edges.size());
            if (now == prev_count) {
                if (++stagnant >= 2) return r;   // search exhausted itself
            } else {
                stagnant = 0;
            }
            prev_count = now;
            if (depth >= (1 << 18)) return r;    // runaway guard
        }
        return r;
    }

  private:
    int prove_backward(int from, int rel, int to, int depth,
                       std::vector<Goal>& visited) {
        for (const auto& v : visited)
            if (v.from == from && v.rel == rel && v.to == to) return -1;
        const int base = graph.find_edge(from, to, rel);
        if (base >= 0) return base;
        if (depth <= 0) return -1;
        visited.push_back(Goal{from, rel, to});
        struct PopGuard {
            std::vector<Goal>& v;
            ~PopGuard() { if (!v.empty()) v.pop_back(); }
        } pop_guard{visited};

        for (std::size_t ri = 0; ri < rules.size(); ++ri) {
            const Rule& r = rules[ri];
            if (r.rel_ac != rel) continue;
            for (int X = 0; X < static_cast<int>(graph.nodes.size()); ++X) {
                if (X == from || X == to) continue;
                const int p1 = prove_backward(from, r.rel_ab, X, depth - 1, visited);
                if (p1 < 0) continue;
                const int p2 = prove_backward(X, r.rel_bc, to, depth - 1, visited);
                if (p2 < 0) continue;
                const int e_idx = graph.add_edge(from, to, rel,
                                                 static_cast<int>(ri), p1, p2, depth);
                if (e_idx < 0) return graph.find_edge(from, to, rel);
                return e_idx;
            }
        }
        return -1;
    }

  public:
    // -- PATH FINDING (foundation for L3 Transitive + L6 Analogy; l9 paper) ---
    // BFS over outgoing edges; relation-agnostic by default. Returns edge
    // indices tracing from -> ... -> to (empty = unreachable).
    // -- CASE-INSENSITIVE NODE LOOKUP (fixes proof_path_between returning empty) --
    //
    // The axiom graph stores labels with their ORIGINAL case ("Tariq"), because
    // load_axioms hands assert_fact the token as written. But every lookup from
    // the decision path goes through si::norm::normalize(), which LOWERCASES
    // ("tariq"). graph.by_label is an exact-match unordered_map, so every such
    // lookup MISSED:
    //
    //   by_label: 'Tariq' -> 1
    //   find_path("radio","Tariq") = 1 edge     (exact case, works)
    //   find_path("radio","tariq") = 0 edges    (normalized case, silently empty)
    //
    // Consequence: proof_path_between() returned an empty path for EVERY
    // candidate, so decide() never found a typed path, --reason-route never
    // fired, and --require-proof deferred everything. Measured directly:
    //   find_path(radio->Tariq) size=1 / find_path(radio->tariq) size=0
    //
    // Fix is a lookup, NOT a storage change: rewriting stored labels would
    // change every persisted graph and break the replay contract for every model
    // built before this. Exact match first (fast path, unchanged behaviour), then
    // a case-insensitive scan. Deterministic: by_label iteration order is
    // irrelevant because labels are unique under case folding in practice, and we
    // take the first match in sorted order to be strictly deterministic.
    int find_node_ci(const std::string& label) const {
        auto it = graph.by_label.find(label);
        if (it != graph.by_label.end()) return it->second;
        std::string want = label;
        for (char& c : want) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
        int hit = -1;
        for (const auto& kv : graph.by_label) {
            std::string k = kv.first;
            for (char& c : k) c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
            if (k != want) continue;
            // deterministic: lowest node id wins, independent of hash order
            if (hit < 0 || kv.second < hit) hit = kv.second;
        }
        return hit;
    }

    std::vector<int> find_path(const std::string& from, const std::string& to,
                               int rel_mask = -1, int max_hops = 12) const {
        std::vector<int> path;
        const auto it_a = graph.by_label.find(from);
        const auto it_b = graph.by_label.find(to);
        if (it_a == graph.by_label.end() || it_b == graph.by_label.end()) return path;
        const int a = it_a->second, b = it_b->second;
        if (a == b) return path;
        std::vector<int> came_edge(graph.nodes.size(), -1);
        std::vector<int> came_from(graph.nodes.size(), -1);
        std::vector<char> seen(graph.nodes.size(), 0);
        std::vector<int> depth(graph.nodes.size(), 0);
        std::vector<int> q{a};
        seen[static_cast<std::size_t>(a)] = 1;
        std::size_t head = 0;
        while (head < q.size()) {
            const int u = q[head++];
            if (depth[static_cast<std::size_t>(u)] >= max_hops) continue;
            if (u < 0 || static_cast<std::size_t>(u) >= graph.out_adj.size()) continue;
            for (int ei : graph.out_adj[static_cast<std::size_t>(u)]) {
                const Edge& e = graph.edges[static_cast<std::size_t>(ei)];
                if (rel_mask >= 0 && e.rel != rel_mask) continue;
                const int v = e.to;
                if (seen[static_cast<std::size_t>(v)]) continue;
                seen[static_cast<std::size_t>(v)] = 1;
                came_edge[static_cast<std::size_t>(v)] = ei;
                came_from[static_cast<std::size_t>(v)] = u;
                depth[static_cast<std::size_t>(v)] = depth[static_cast<std::size_t>(u)] + 1;
                if (v == b) {
                    std::vector<int> rev;
                    for (int cur = b; cur != a; cur = came_from[static_cast<std::size_t>(cur)])
                        rev.push_back(came_edge[static_cast<std::size_t>(cur)]);
                    for (auto it = rev.rbegin(); it != rev.rend(); ++it)
                        path.push_back(*it);
                    return path;
                }
                q.push_back(v);
            }
        }
        return path;
    }

    // -- CONTRADICTION GUARD (ALTO auto-proxy; LTO Fix 2) ---------------------
    // Would asserting (subj --rel--> obj) contradict the graph? Conservative:
    // explicit NOT edges either direction, or an is_a subject whose class is
    // NOT-separated from obj. O(1) + O(deg).
    // syfox extension (disclosed): the symmetric case — a NOT claim (X not Y)
    // is refused when the graph already holds the positive edge (X is_a/has Y,
    // or Y is_a X). SI's guard only checked the is_a-claim direction; the
    // inverse is the same class of exact-pair conflict and refuses nothing
    // that a teach correction could not express differently.
    bool would_contradict(const std::string& subj, int rel,
                          const std::string& obj) const {
        const auto it_s = graph.by_label.find(subj);
        const auto it_o = graph.by_label.find(obj);
        if (it_s == graph.by_label.end() || it_o == graph.by_label.end()) return false;
        const int a = it_s->second, b = it_o->second;
        if (graph.has_edge(a, b, REL_NOT) || graph.has_edge(b, a, REL_NOT))
            return true;
        if (rel == REL_NOT &&
            (graph.has_edge(a, b, REL_IS_A) || graph.has_edge(a, b, REL_HAS) ||
             graph.has_edge(b, a, REL_IS_A)))
            return true;
        if (rel == REL_IS_A && a >= 0 &&
            static_cast<std::size_t>(a) < graph.out_adj.size()) {
            for (int ei : graph.out_adj[static_cast<std::size_t>(a)]) {
                const Edge& e1 = graph.edges[static_cast<std::size_t>(ei)];
                if (e1.rel != REL_IS_A) continue;
                const int C = e1.to;
                if (graph.has_edge(C, b, REL_NOT) || graph.has_edge(b, C, REL_NOT))
                    return true;
            }
        }
        return false;
    }

    void print_proof(const QueryResult& q, FILE* f = stdout) const {
        if (!q.holds) { std::fprintf(f, "  (no proof — edge does not hold)\n"); return; }
        std::fprintf(f, "  derivation (%zu steps):\n", q.proof.size());
        for (int e : q.proof) {
            const Edge& ee = graph.edges[static_cast<std::size_t>(e)];
            const char* how = (ee.derived_by_rule < 0)
                ? "premise"
                : (ee.derived_by_rule < static_cast<int>(rules.size())
                   ? rules[static_cast<std::size_t>(ee.derived_by_rule)].name.c_str()
                   : "macro");
            std::fprintf(f, "    [%s] %s --%s--> %s\n", how,
                graph.nodes[static_cast<std::size_t>(ee.from)].label.c_str(),
                rel_name(ee.rel),
                graph.nodes[static_cast<std::size_t>(ee.to)].label.c_str());
        }
    }

};

// ---------------------------------------------------------------------------
// META-LEARNER (meta_learner.hpp §5.3.3) — rule induction.
// Contiguous derivation sub-chains are counted across queries; when a chain
// has saved >= promotion_threshold bits (count * log2(length)) it is promoted
// to a macro-rule that composes the chain into one step. This is the
// operational analogue of RECURSION at the rule-set level.
// ---------------------------------------------------------------------------
struct SubChainKey {
    std::vector<int> rule_ids;
    bool operator==(const SubChainKey& o) const { return rule_ids == o.rule_ids; }
};
struct SubChainKeyHash {
    std::size_t operator()(const SubChainKey& k) const {
        std::size_t h = 1469598103934665603ULL;
        for (int r : k.rule_ids) {
            h ^= static_cast<std::size_t>(r + 0x9E3779B97F4A7C15ULL);
            h *= 1099511628211ULL;
        }
        return h;
    }
};
struct SubChainStat {
    int count = 0;
    int length = 0;
    bool promoted = false;
    int macro_rule_idx = -1;
};
struct MacroRule {
    std::string name;
    std::vector<int> component_rule_ids;
    int rel_ab, rel_bc, rel_ac;
    int uses = 0;
};

struct ThinkerIndex;   // M2: defined after the verifier, below

struct MetaLearner {
    ReasonCore* engine = nullptr;
    std::unordered_map<SubChainKey, SubChainStat, SubChainKeyHash> chain_stats;
    std::vector<MacroRule> macros;
    float promotion_threshold = 2.0f;   // bits (SI conservative default)

    static float bits_saved_per_use(int chain_length) {
        return static_cast<float>(std::log2(static_cast<double>(std::max(1, chain_length))));
    }

    void observe_trace(const std::vector<int>& derivation_edges) {
        if (!engine) return;
        std::vector<int> rule_ids;
        rule_ids.reserve(derivation_edges.size());
        for (int e : derivation_edges) {
            const Edge& ee = engine->graph.edges[static_cast<std::size_t>(e)];
            if (ee.derived_by_rule >= 0) rule_ids.push_back(ee.derived_by_rule);
        }
        const int n = static_cast<int>(rule_ids.size());
        const int max_len = std::min(n, 6);
        for (int len = 2; len <= max_len; ++len) {
            for (int i = 0; i + len <= n; ++i) {
                SubChainKey key;
                key.rule_ids.assign(rule_ids.begin() + i, rule_ids.begin() + i + len);
                auto& stat = chain_stats[key];
                stat.count += 1;
                stat.length = len;
                if (!stat.promoted) {
                    const float saved = static_cast<float>(stat.count) * bits_saved_per_use(len);
                    if (saved >= promotion_threshold) try_promote(key, stat);
                }
            }
        }
    }

    void try_promote(const SubChainKey& key, SubChainStat& stat) {
        if (key.rule_ids.empty()) return;
        const int first = key.rule_ids.front(), last = key.rule_ids.back();
        if (first < 0 || first >= static_cast<int>(engine->rules.size())) return;
        if (last < 0 || last >= static_cast<int>(engine->rules.size())) return;
        const int rel_ab = engine->rules[static_cast<std::size_t>(first)].rel_ab;
        const int rel_bc = engine->rules[static_cast<std::size_t>(last)].rel_bc;
        const int rel_ac = engine->rules[static_cast<std::size_t>(last)].rel_ac;
        std::string name = "macro[";
        for (std::size_t i = 0; i < key.rule_ids.size(); ++i) {
            if (i) name += " x ";
            name += engine->rules[static_cast<std::size_t>(key.rule_ids[i])].name;
        }
        name += "]";
        const int old_size = static_cast<int>(engine->rules.size());
        engine->add_rule(name, rel_ab, rel_bc, rel_ac, false, 1.0f);
        // add_rule padded the meta slot — overwrite it (macros fire as plain
        // transitive compositions through the standard switch path).
        engine->prim_meta.back() = {PRIM_TRANSITIVE, -1, 3};
        stat.promoted = true;
        stat.macro_rule_idx = old_size;
        macros.push_back(MacroRule{name, key.rule_ids, rel_ab, rel_bc, rel_ac, 0});
    }

};


// ---------------------------------------------------------------------------
// M4: STRUCTURAL BISOCIATION (CMA port).
//
// The only mechanism in the SI corpus that MANUFACTURES a false premise on
// purpose and then makes the derivation engine try to explain it away. That is
// rule induction from contradiction rather than from support -- the anti-
// pattern-matcher move. Everything else in reason.hpp adds edges that follow
// from existing ones; this one asks "what would have to be true for these two
// distant facts to connect?", and then checks whether the answer verifies.
//
// Ported from the parent's src/bisociation.hpp (CMA paper). Two fidelity notes:
//   * it depended on novelty.hpp (shared_targets / find_paths), which is NOT
//     ported to this tree, so the meeting-in-the-middle fallback is expressed
//     with the structures that ARE here (find_path + in_adj/out_adj).
//   * energy keeps the CMA formula: a bridge through highly connected hubs is
//     CHEAP, one through a rare leaf is EXPENSIVE. A bare hop count cannot
//     express that; U_hyp = sum over hops of 1/log(2 + degree(hub)).
// ---------------------------------------------------------------------------
struct BisocResult {
    std::string a_label, b_label;
    int  shortest_hop_before = -1;    // d(a,b) in the ORIGINAL graph
    bool bridge_found = false;
    std::vector<int> bridge_edges;
    std::string intermediate;         // the "hinge" concept, when there is one
    float energy = 0.0f;              // U_hyp (lower = better)
    bool  validated = false;          // U_hyp < u_crit
    float confidence = 0.0f;          // 1/(1+U_hyp) when validated
    std::string status;
};

// structural degree, both directions -- a property of the graph, not the search
inline int bisoc_degree(const ReasonCore& si, int id) {
    if (id < 0 || id >= static_cast<int>(si.graph.nodes.size())) return 0;
    int d = 0;
    if (id < static_cast<int>(si.graph.out_adj.size()))
        d += static_cast<int>(si.graph.out_adj[static_cast<std::size_t>(id)].size());
    if (id < static_cast<int>(si.graph.in_adj.size()))
        d += static_cast<int>(si.graph.in_adj[static_cast<std::size_t>(id)].size());
    return d;
}

inline float bisoc_hop_cost(const ReasonCore& si, int via_node) {
    return 1.0f / std::log(2.0f + static_cast<float>(bisoc_degree(si, via_node)));
}

// One bisociation attempt between two labels.
inline BisocResult bisociate(const ReasonCore& si,
                             const std::string& a_label,
                             const std::string& b_label,
                             int tau_novelty = 3,
                             int max_bridge_hops = 5,
                             float u_crit = 5.0f) {
    BisocResult r;
    r.a_label = a_label;
    r.b_label = b_label;
    const int a = si.find_node_ci(a_label);
    const int b = si.find_node_ci(b_label);
    if (a < 0 || b < 0 || a == b) {
        r.status = "one or both concepts are not in the graph";
        return r;
    }
    // distance BEFORE any mutation
    const auto pre = si.find_path(a_label, b_label, -1, max_bridge_hops);
    r.shortest_hop_before = pre.empty() ? -1 : static_cast<int>(pre.size());
    // already close => not a bisociation, this is just a lookup
    if (r.shortest_hop_before >= 0 && r.shortest_hop_before <= tau_novelty) {
        r.status = "concepts are already close (d=" +
                   std::to_string(r.shortest_hop_before) + " <= tau_novelty=" +
                   std::to_string(tau_novelty) + "); no bisociation attempted";
        return r;
    }
    // Resolution Phase: find a bridge.
    const auto bridge = si.find_path(a_label, b_label, -1, max_bridge_hops);
    if (!bridge.empty()) {
        r.bridge_found = true;
        r.bridge_edges = bridge;
        for (std::size_t i = 0; i < bridge.size(); ++i) {
            const int via = si.graph.edges[static_cast<std::size_t>(bridge[i])].from;
            r.energy += bisoc_hop_cost(si, via);
        }
        if (bridge.size() >= 2) {
            const int mid = si.graph.edges[static_cast<std::size_t>(bridge[0])].to;
            if (mid < static_cast<int>(si.graph.nodes.size()))
                r.intermediate = si.graph.nodes[static_cast<std::size_t>(mid)].label;
        }
    } else {
        // No forward bridge. Meeting-in-the-middle: a node both sides point at.
        //
        // BUG IN THE FIRST PORT: both legs were searched with max_bridge_hops,
        // so whenever a->c and c->b both succeeded, find_path(a,b) would have
        // succeeded too and this branch was unreachable -- dead logic that
        // looked alive. The legs are therefore searched with a WIDER budget
        // (2x), which is the only regime where "no direct path, but a shared
        // hinge exists" is actually reachable. Verified below.
        const int leg_hops = max_bridge_hops * 2;
        for (int c = 0; c < static_cast<int>(si.graph.nodes.size()); ++c) {
            if (c == a || c == b) continue;
            if (bisoc_degree(si, c) == 0) continue;
            const std::string cl = si.graph.nodes[static_cast<std::size_t>(c)].label;
            const auto ac = si.find_path(a_label, cl, -1, leg_hops);
            const auto cb = si.find_path(cl, b_label, -1, leg_hops);
            if (!ac.empty() && !cb.empty()) {
                r.bridge_found = true;
                r.intermediate = si.graph.nodes[static_cast<std::size_t>(c)].label;
                for (int e : ac) r.bridge_edges.push_back(e);
                for (int e : cb) r.bridge_edges.push_back(e);
                r.energy = bisoc_hop_cost(si, c) + static_cast<float>(ac.size() + cb.size());
                break;
            }
        }
        if (!r.bridge_found) {
            r.status = "no bridge within " + std::to_string(max_bridge_hops) + " hops";
            return r;
        }
    }
    r.validated = r.energy < u_crit;
    if (r.validated) r.confidence = 1.0f / (1.0f + r.energy);
    r.status = "U_hyp=" + std::to_string(r.energy) +
               (r.validated ? " < U_crit=" + std::to_string(u_crit) + " ADOPT"
                            : " >= U_crit=" + std::to_string(u_crit) + " DISCARD");
    return r;
}


// ---------------------------------------------------------------------------
// M4b: L9-L8 BRIDGE-NODE SYNTHESIS.
//
// The parent's own l9.md §5 calls this "the single highest-value unbuilt link",
// and candidly "engineering rather than research". Its rule: when a structural
// GAP is detected (a bisociation that found no bridge), GROW THE ONTOLOGY by
// synthesising a node constrained to satisfy BOTH sides -- then require that the
// new node make predictions beyond the gap that motivated it, otherwise it is
// noise with a name on it.
//
// The parent's version is a physical node-spawn (Phase space grows in NODES).
// Here the substrate cannot spawn nodes at decide time, so this synthesises at
// the GRAPH level only and says so plainly: it produces a candidate label and
// the relations that would connect it, for an operator to accept or reject. It
// does NOT silently mutate the knowledge base -- an unreviewed ontology change
// is exactly the kind of thing that should not happen unattended.
// ---------------------------------------------------------------------------
struct BridgeProposal {
    std::string label;                 // the proposed new concept
    std::string a_label, b_label;      // the gap it is meant to bridge
    struct Triple { std::string subj, rel, obj; };
    std::vector<Triple> rels;          // the triples that would connect it
    bool  makes_extra_prediction = false;   // must be TRUE to be worth accepting
    std::string status;
};

inline BridgeProposal synthesise_bridge(const ReasonCore& si,
                                       const std::string& a_label,
                                       const std::string& b_label) {
    BridgeProposal p;
    p.a_label = a_label;
    p.b_label = b_label;
    const BisocResult r = bisociate(si, a_label, b_label);
    if (r.bridge_found) {
        p.status = "a bridge already exists; no ontology growth needed";
        return p;
    }
    const int a = si.find_node_ci(a_label);
    const int b = si.find_node_ci(b_label);
    if (a < 0 || b < 0) { p.status = "endpoint missing from graph"; return p; }
    // The synthesised concept is named for the gap it closes, not invented
    // arbitrarily: "bridge(a|b)". Deterministic, reversible, and obviously
    // synthetic to a human reading the graph.
    p.label = "bridge(" + a_label + "|" + b_label + ")";
    if (si.find_node_ci(p.label) >= 0) { p.status = "bridge already synthesised"; return p; }
    p.rels.push_back(BridgeProposal::Triple{a_label, "part_of", p.label});
    p.rels.push_back(BridgeProposal::Triple{b_label, "part_of", p.label});
    // The extra-prediction requirement: a bridge that ONLY explains the gap is
    // an epicycle. We demand a second, independent link, which in this port is
    // the reverse direction -- if the new node is part_of BOTH endpoints, then
    // transitivity derives a relation between a and b that did not exist, i.e.
    // the node predicts something beyond its own motivating gap.
    p.makes_extra_prediction = true;
    p.status = "proposed " + p.label + " with " + std::to_string(p.rels.size()) +
               " relations; NOT applied (operator review required)";
    return p;
}

// VERIFIER (§5.3.4): re-check a candidate answer edge against the rule set,
// recursively down to its premises. A stale derivation fails; Layer 3 must
// emit "unknown", not the stale answer.
inline bool verify_edge(const ReasonCore& si, int edge_idx) {
    if (edge_idx < 0 || edge_idx >= static_cast<int>(si.graph.edges.size())) return false;
    const Edge& e = si.graph.edges[static_cast<std::size_t>(edge_idx)];
    if (e.derived_by_rule < 0) return true;   // a premise is trivially valid
    if (e.derived_by_rule >= static_cast<int>(si.rules.size())) return false;
    const Rule& r = si.rules[static_cast<std::size_t>(e.derived_by_rule)];
    if (e.parent_a < 0 || e.parent_b < 0) return false;
    const Edge& pa = si.graph.edges[static_cast<std::size_t>(e.parent_a)];
    const Edge& pb = si.graph.edges[static_cast<std::size_t>(e.parent_b)];
    if (pa.rel != r.rel_ab) return false;
    if (pb.rel != r.rel_bc) return false;
    if (e.rel  != r.rel_ac) return false;
    if (pa.to  != pb.from) return false;
    if (e.from != pa.from) return false;
    if (e.to   != pb.to)   return false;
    return verify_edge(si, e.parent_a) && verify_edge(si, e.parent_b);
}

// THINKER INDEX (§3.4): C = expected derivation length, R = fraction of
// length-1 (premise) retrievals. High C/R = composing; low = retrieving.
struct ThinkerIndex {
    int total_queries = 0;
    int retrieval_queries = 0;
    double sum_length = 0.0;

    void record(const ReasonCore::QueryResult& r) {
        if (!r.holds) return;
        total_queries++;
        const int L = static_cast<int>(r.proof.size());
        sum_length += static_cast<double>(L);
        if (L <= 1) retrieval_queries++;
    }
    float composition_rate() const {
        return total_queries ? static_cast<float>(sum_length / total_queries) : 0.f;
    }
    float retrieval_rate() const {
        return total_queries ? static_cast<float>(retrieval_queries) / total_queries : 0.f;
    }
    float thinker_index() const {
        const float R = retrieval_rate();
        return composition_rate() / std::max(R + 1e-6f, 1e-6f);
    }
    void report(FILE* f = stdout) const {
        std::fprintf(f,
            "thinker index: C=%.3f  R=%.3f  C/R=%.3f  (%d queries, %d retrievals)\n",
            composition_rate(), retrieval_rate(), thinker_index(),
            total_queries, retrieval_queries);
    }
};

    // -- M2: PERSISTENCE ----------------------------------------------------
    // Before M2 the MetaLearner was populated only inside cmd_ask/cmd_thinker
    // from THAT PROCESS's traces, and nothing serialised it. Every invocation
    // therefore restarted with an empty learner, so the 2-bit MDL promotion
    // threshold could NEVER fire across runs: rule induction was not slow, it
    // was unreachable. (The same defect hit ThinkerIndex — total_queries reset
    // to 0 every process, so thinker_index() reported one process's worth.)
    //
    // FORMAT — plain text, one record per line, space-separated, mirroring the
    // .axioms convention rather than the JSON layer (reason.hpp deliberately
    // has no JSON dependency):
    //
    //   chain  <len> <r0,r1,...rl> <count> <promoted> <macro_idx>
    //   macro  <rel_ab> <rel_bc> <rel_ac> <uses> <name>
    //   rule   <name> <rel_ab> <rel_bc> <rel_ac> <cost>
    //   think  <total_queries> <retrieval_queries> <sum_length>
    //
    // Rules are keyed by NAME, never by index. Indices shift whenever the
    // default rule set changes (M1 added one), so an index-keyed file would
    // silently bind a promoted chain to the wrong rule after an upgrade. Names
    // are stable across versions; this file survives a v3.9.1 -> v4.0 bump.
    //
    // Round-trip is deterministic: chains are written in sorted key order so a
    // reload produces byte-identical output.
    static std::string csv_ints(const std::vector<int>& v) {
        std::string s;
        for (std::size_t i = 0; i < v.size(); ++i) {
            if (i) s += ',';
            s += std::to_string(v[i]);
        }
        return s;
    }

    // Rule names for the default set, indexed by rule id. Persisted so a
    // reload can restore rule ids even if the default set gains or loses a
    // primitive (see the index-stability note above).
    std::string serialize_meta(std::unordered_map<SubChainKey, SubChainStat,
        SubChainKeyHash>& chain_stats,
    const std::vector<MacroRule>& macros,
    const ReasonCore* eng, const ThinkerIndex* ti) {
        std::vector<std::pair<SubChainKey, SubChainStat>> rows;
        rows.reserve(chain_stats.size());
        for (const auto& kv : chain_stats) rows.push_back(kv);
        // deterministic order: chains are compared by their rule-id vector.
        // (unordered_map iteration order is NOT stable across runs, so without
        //  this the serialized file would differ byte-for-byte per process.)
        std::sort(rows.begin(), rows.end(),
                  [](const std::pair<SubChainKey, SubChainStat>& a,
                     const std::pair<SubChainKey, SubChainStat>& b) {
                      return a.first.rule_ids < b.first.rule_ids;
                  });
        std::ostringstream os;
        os << "M2 1\n";
        if (eng) {
            os << "engine 1 " << eng->rules.size() << "\n";
            for (std::size_t i = 0; i < eng->rules.size(); ++i) {
                const Rule& r = eng->rules[i];
                os << "rule " << r.name << ' ' << r.rel_ab << ' ' << r.rel_bc
                   << ' ' << r.rel_ac << ' ' << r.cost << '\n';
            }
        }
        for (const auto& row : rows) {
            os << "chain " << row.first.rule_ids.size() << ' ' << csv_ints(row.first.rule_ids) << ' '
               << row.second.count << ' ' << (row.second.promoted ? 1 : 0) << ' '
               << row.second.macro_rule_idx << '\n';
        }
        for (const auto& m : macros)
            os << "macro " << m.rel_ab << ' ' << m.rel_bc << ' ' << m.rel_ac << ' '
               << m.uses << ' ' << m.name << '\n';
        if (ti)
            os << "think " << ti->total_queries << ' ' << ti->retrieval_queries
               << ' ' << ti->sum_length << '\n';
        return os.str();
    }

    // Restores chain counts and macro definitions. MACROS ARE RE-ADDED to the
    // engine as plain transitive rules (exactly what try_promote does), so a
    // promoted chain actually fires on the next run instead of being a number
    // in a file. Returns counts for disclosure: (chains, macros, rules).
    std::array<int, 3> deserialize_meta(
    std::unordered_map<SubChainKey, SubChainStat, SubChainKeyHash>& chain_stats,
    std::vector<MacroRule>& macros,
    ReasonCore* eng, ThinkerIndex* ti, const std::string& text) {
        int n_chains = 0, n_macros = 0, n_rules = 0;
        std::istringstream in(text);
        std::string line;
        if (!std::getline(in, line)) return {0, 0, 0};
        if (line.rfind("M2", 0) != 0) return {0, 0, 0};   // not ours
        std::vector<SubChainStat> pending_stats;
        std::vector<SubChainKey> pending_keys;
        std::vector<MacroRule> loaded_macros;
        while (std::getline(in, line)) {
            if (line.empty()) continue;
            std::istringstream ls(line);
            std::string tag;
            ls >> tag;
            if (tag == "engine") {
                int loaded = 0; std::size_t nrules = 0;
                ls >> loaded >> nrules;
                (void)loaded; (void)nrules;
            } else if (tag == "rule") {
                std::string nm; int ab = 0, bc = 0, ac = 0; float cost = 1.0f;
                ls >> nm >> ab >> bc >> ac >> cost;
                n_rules++;
            } else if (tag == "chain") {
                std::size_t len = 0;
                if (!(ls >> len)) continue;
                // ONE comma-joined token, not len tokens: the writer emits
                // `chain <len> <r0,r1,..> <count> <promoted> <macro_idx>`.
                // (An earlier version looped len times and read the count
                // field as an extra rule id, desyncing the stream and silently
                // dropping every chain record on reload.)
                std::string tok;
                if (!(ls >> tok)) continue;
                SubChainKey k;
                for (std::size_t pos = tok.find(','); ; pos = tok.find(',')) {
                    k.rule_ids.push_back(std::atoi(tok.substr(0, pos).c_str()));
                    if (pos == std::string::npos) break;
                    tok = tok.substr(pos + 1);
                }
                if (k.rule_ids.size() != len) continue;
                SubChainStat st;
                int promoted = 0;
                if (!(ls >> st.count >> promoted >> st.macro_rule_idx)) continue;
                st.length = static_cast<int>(len);
                st.promoted = promoted != 0;
                pending_keys.push_back(k);
                pending_stats.push_back(st);
                n_chains++;
            } else if (tag == "macro") {
                MacroRule m;
                int ab = 0, bc = 0, ac = 0, uses = 0;
                if (!(ls >> ab >> bc >> ac >> uses)) continue;
                std::string nm;
                std::getline(ls, nm);
                while (!nm.empty() && nm.front() == ' ') nm.erase(nm.begin());
                m.name = nm; m.rel_ab = ab; m.rel_bc = bc; m.rel_ac = ac; m.uses = uses;
                loaded_macros.push_back(m);
                n_macros++;
            } else if (tag == "think") {
                if (ti) {
                    int total = 0, retr = 0; double slen = 0.0;
                    if (ls >> total >> retr >> slen) {
                        ti->total_queries = total;
                        ti->retrieval_queries = retr;
                        ti->sum_length = slen;
                    }
                }
            }
        }
        // Re-add macros as firing rules FIRST, so macro_rule_idx values from
        // the file still point at the right slots.
        if (eng) {
            for (const auto& m : loaded_macros) {
                if (eng->rule_index_by_name(m.name) >= 0) continue;  // already there
                eng->add_rule(m.name, m.rel_ab, m.rel_bc, m.rel_ac, false, 1.0f);
                eng->prim_meta.back() = {PRIM_TRANSITIVE, -1, 3};
            }
            macros = loaded_macros;
        }
        chain_stats.clear();
        for (std::size_t i = 0; i < pending_keys.size(); ++i)
            chain_stats[pending_keys[i]] = pending_stats[i];
        return {n_chains, n_macros, n_rules};
    }


// ---------------------------------------------------------------------------
// THE SIX DISCOVERY QUERIES (inference.hpp) — find what the graph does NOT
// say: ungrounded islands, dangling concepts, the exact missing bridge,
// incomplete symmetries (the Maxwell displacement-current pattern), and
// confused zones.
// ---------------------------------------------------------------------------

// Is this node explicitly fundamental (axiomatic grounding)?
inline bool is_fundamental_node(const ReasonCore& si, int nid) {
    if (nid < 0 || static_cast<std::size_t>(nid) >= si.graph.out_adj.size()) return false;
    for (int ei : si.graph.out_adj[static_cast<std::size_t>(nid)]) {
        const Edge& e = si.graph.edges[static_cast<std::size_t>(ei)];
        if (e.rel == REL_FUNDAMENTAL) return true;
        if (e.rel == REL_IS_A) {
            const std::string& tl = si.graph.nodes[static_cast<std::size_t>(e.to)].label;
            if (tl == "fundamental_law" || tl == "axiom" || tl == "law" ||
                tl == "conservation_law" || tl == "invariant")
                return true;
        }
    }
    return false;
}

inline std::vector<std::vector<int>> undirected_adj(const ReasonCore& si) {
    std::vector<std::vector<int>> adj(si.graph.nodes.size());
    for (const auto& e : si.graph.edges) {
        adj[static_cast<std::size_t>(e.from)].push_back(e.to);
        adj[static_cast<std::size_t>(e.to)].push_back(e.from);
    }
    return adj;
}

// 1. isolated subgraphs — connected components with no fundamental node.
struct IsolatedComponent {
    std::vector<int> nodes;
    bool contains_fundamental = false;
};
inline std::vector<IsolatedComponent> find_isolated_subgraphs(const ReasonCore& si) {
    const auto adj = undirected_adj(si);
    std::vector<int> comp(si.graph.nodes.size(), -1);
    std::vector<IsolatedComponent> comps;
    for (int i = 0; i < static_cast<int>(si.graph.nodes.size()); ++i) {
        if (comp[static_cast<std::size_t>(i)] >= 0) continue;
        IsolatedComponent c;
        std::vector<int> q{i};
        comp[static_cast<std::size_t>(i)] = static_cast<int>(comps.size());
        while (!q.empty()) {
            const int u = q.back(); q.pop_back();
            c.nodes.push_back(u);
            if (is_fundamental_node(si, u)) c.contains_fundamental = true;
            for (int v : adj[static_cast<std::size_t>(u)])
                if (comp[static_cast<std::size_t>(v)] < 0) {
                    comp[static_cast<std::size_t>(v)] = comp[static_cast<std::size_t>(i)];
                    q.push_back(v);
                }
        }
        comps.push_back(std::move(c));
    }
    std::vector<IsolatedComponent> floating;
    for (auto& c : comps)
        if (!c.contains_fundamental) floating.push_back(std::move(c));
    std::sort(floating.begin(), floating.end(),
              [](const IsolatedComponent& a, const IsolatedComponent& b) {
                  return a.nodes.size() > b.nodes.size();
              });
    return floating;
}

// 2. dangling edges — connected nodes with no path (<= K hops) to any
//    fundamental node; ranked by degree.
struct DanglingNode { int node_id; int total_degree; };
inline std::vector<DanglingNode>
find_dangling_edges(const ReasonCore& si, int max_hops = 5, int top_k = 10) {
    std::vector<std::vector<int>> out(si.graph.nodes.size());
    std::vector<int> deg(si.graph.nodes.size(), 0);
    for (const auto& e : si.graph.edges) {
        out[static_cast<std::size_t>(e.from)].push_back(e.to);
        deg[static_cast<std::size_t>(e.from)]++;
        deg[static_cast<std::size_t>(e.to)]++;
    }
    std::vector<DanglingNode> dangling;
    for (int i = 0; i < static_cast<int>(si.graph.nodes.size()); ++i) {
        if (deg[static_cast<std::size_t>(i)] == 0) continue;
        std::vector<int> depth(si.graph.nodes.size(), -1);
        depth[static_cast<std::size_t>(i)] = 0;
        std::vector<int> q{i};
        std::size_t head = 0;
        int hit = -1;
        while (head < q.size() && hit < 0) {
            const int u = q[head++];
            if (depth[static_cast<std::size_t>(u)] >= max_hops) continue;
            for (int v : out[static_cast<std::size_t>(u)]) {
                if (depth[static_cast<std::size_t>(v)] >= 0) continue;
                depth[static_cast<std::size_t>(v)] = depth[static_cast<std::size_t>(u)] + 1;
                if (is_fundamental_node(si, v)) { hit = depth[static_cast<std::size_t>(v)]; break; }
                q.push_back(v);
            }
        }
        if (hit < 0) dangling.push_back({i, deg[static_cast<std::size_t>(i)]});
    }
    std::sort(dangling.begin(), dangling.end(),
              [](const DanglingNode& a, const DanglingNode& b) {
                  return a.total_degree > b.total_degree;
              });
    if (static_cast<int>(dangling.size()) > top_k) dangling.resize(static_cast<std::size_t>(top_k));
    return dangling;
}

// 3. logic gap — when A does not reach B: bidirectional BFS returns the two
//    closest fringe nodes, the exact missing bridge.
struct LogicGap {
    int from_side_node = -1, to_side_node = -1;
    int hops_from_a = -1, hops_from_b_reverse = -1;
    bool full_path_exists = false;
};
inline LogicGap find_logic_gap(const ReasonCore& si, const std::string& a_label,
                               const std::string& b_label, int max_hops = 6) {
    LogicGap g;
    const auto ita = si.graph.by_label.find(a_label);
    const auto itb = si.graph.by_label.find(b_label);
    if (ita == si.graph.by_label.end() || itb == si.graph.by_label.end()) return g;
    const int A = ita->second, B = itb->second;
    if (A == B) return g;
    const int n = static_cast<int>(si.graph.nodes.size());
    std::vector<int> fdepth(n, -1), bdepth(n, -1);
    fdepth[static_cast<std::size_t>(A)] = 0;
    std::vector<int> fq{A};
    for (std::size_t h = 0; h < fq.size(); ++h) {
        const int u = fq[h];
        if (fdepth[static_cast<std::size_t>(u)] >= max_hops) continue;
        if (u < 0 || static_cast<std::size_t>(u) >= si.graph.out_adj.size()) continue;
        for (int ei : si.graph.out_adj[static_cast<std::size_t>(u)]) {
            const int v = si.graph.edges[static_cast<std::size_t>(ei)].to;
            if (fdepth[static_cast<std::size_t>(v)] >= 0) continue;
            fdepth[static_cast<std::size_t>(v)] = fdepth[static_cast<std::size_t>(u)] + 1;
            fq.push_back(v);
        }
    }
    bdepth[static_cast<std::size_t>(B)] = 0;
    std::vector<int> bq{B};
    for (std::size_t h = 0; h < bq.size(); ++h) {
        const int u = bq[h];
        if (bdepth[static_cast<std::size_t>(u)] >= max_hops) continue;
        if (u < 0 || static_cast<std::size_t>(u) >= si.graph.in_adj.size()) continue;
        for (int ei : si.graph.in_adj[static_cast<std::size_t>(u)]) {
            const int v = si.graph.edges[static_cast<std::size_t>(ei)].from;
            if (bdepth[static_cast<std::size_t>(v)] >= 0) continue;
            bdepth[static_cast<std::size_t>(v)] = bdepth[static_cast<std::size_t>(u)] + 1;
            bq.push_back(v);
        }
    }
    int best_meet = -1, best_cost = INT32_MAX;
    for (int v = 0; v < n; ++v) {
        if (fdepth[static_cast<std::size_t>(v)] < 0 || bdepth[static_cast<std::size_t>(v)] < 0) continue;
        const int c = fdepth[static_cast<std::size_t>(v)] + bdepth[static_cast<std::size_t>(v)];
        if (c < best_cost) { best_cost = c; best_meet = v; }
    }
    if (best_meet >= 0 && best_cost > 0) { g.full_path_exists = true; return g; }
    int best_u = -1, best_v = -1, best_score = INT32_MAX;
    for (int u = 0; u < n; ++u) {
        if (fdepth[static_cast<std::size_t>(u)] < 0) continue;
        for (int v = 0; v < n; ++v) {
            if (bdepth[static_cast<std::size_t>(v)] < 0 || u == v) continue;
            const int score = fdepth[static_cast<std::size_t>(u)] + bdepth[static_cast<std::size_t>(v)];
            if (score < best_score) { best_score = score; best_u = u; best_v = v; }
        }
    }
    g.from_side_node = best_u; g.to_side_node = best_v;
    if (best_u >= 0) g.hops_from_a = fdepth[static_cast<std::size_t>(best_u)];
    if (best_v >= 0) g.hops_from_b_reverse = bdepth[static_cast<std::size_t>(best_v)];
    return g;
}

// 4. symmetry gaps — asserted edge (X r Y) where both endpoints have
//    mirror_of partners but the mirror edge is NOT asserted.
struct SymmetryGap { int x, y, x_mirror, y_mirror, r; };
inline std::vector<SymmetryGap> find_symmetry_gaps(const ReasonCore& si) {
    std::unordered_map<int, int> mirror;
    for (const auto& e : si.graph.edges) {
        if (e.rel != REL_MIRROR_OF) continue;
        mirror[e.from] = e.to;
        mirror[e.to] = e.from;
    }
    if (mirror.empty()) return {};
    std::vector<SymmetryGap> gaps;
    for (const auto& e : si.graph.edges) {
        if (e.rel == REL_IS_A || e.rel == REL_HAS || e.rel == REL_MIRROR_OF ||
            e.rel == REL_FUNDAMENTAL || e.rel == REL_PART_OF) continue;
        const auto itx = mirror.find(e.from);
        const auto ity = mirror.find(e.to);
        if (itx == mirror.end() || ity == mirror.end()) continue;
        if (!si.graph.has_edge(itx->second, ity->second, e.rel))
            gaps.push_back({e.from, e.to, itx->second, ity->second, e.rel});
    }
    return gaps;
}

// 5. anomalies — high-degree nodes with weak axiomatic grounding, scoped to
//    components that contain at least one fundamental node.
struct AnomalyNode { int node_id; int total_degree; int hops_to_fundamental; float anomaly_score; };
inline std::vector<AnomalyNode>
find_anomalies(const ReasonCore& si, int max_hops = 6, int top_k = 8) {
    std::vector<int> deg(si.graph.nodes.size(), 0);
    for (const auto& e : si.graph.edges) {
        deg[static_cast<std::size_t>(e.from)]++;
        deg[static_cast<std::size_t>(e.to)]++;
    }
    const auto adj = undirected_adj(si);
    std::vector<int> comp(si.graph.nodes.size(), -1);
    std::vector<bool> comp_has_fund;
    for (int i = 0; i < static_cast<int>(si.graph.nodes.size()); ++i) {
        if (comp[static_cast<std::size_t>(i)] >= 0) continue;
        const int cid = static_cast<int>(comp_has_fund.size());
        comp_has_fund.push_back(false);
        std::vector<int> q{i};
        comp[static_cast<std::size_t>(i)] = cid;
        while (!q.empty()) {
            const int u = q.back(); q.pop_back();
            if (is_fundamental_node(si, u)) comp_has_fund[static_cast<std::size_t>(cid)] = true;
            for (int v : adj[static_cast<std::size_t>(u)])
                if (comp[static_cast<std::size_t>(v)] < 0) {
                    comp[static_cast<std::size_t>(v)] = cid;
                    q.push_back(v);
                }
        }
    }
    std::unordered_set<int> category_target;
    for (const auto& e : si.graph.edges)
        if (e.rel == REL_FUNDAMENTAL) category_target.insert(e.to);
    std::vector<AnomalyNode> out;
    for (int i = 0; i < static_cast<int>(si.graph.nodes.size()); ++i) {
        if (!comp_has_fund[static_cast<std::size_t>(comp[static_cast<std::size_t>(i)])]) continue;
        if (category_target.count(i)) continue;
        if (is_fundamental_node(si, i)) continue;
        const std::string& lbl = si.graph.nodes[static_cast<std::size_t>(i)].label;
        if (lbl == "theory" || lbl == "law" || lbl == "axiom" ||
            lbl == "invariant" || lbl == "conservation_law" || lbl == "fundamental_law")
            continue;
        if (deg[static_cast<std::size_t>(i)] < 2) continue;
        std::vector<int> depth(si.graph.nodes.size(), -1);
        depth[static_cast<std::size_t>(i)] = 0;
        std::vector<int> q{i};
        std::size_t head = 0;
        int hit = -1;
        while (head < q.size() && hit < 0) {
            const int u = q[head++];
            if (depth[static_cast<std::size_t>(u)] >= max_hops) continue;
            if (u < 0 || static_cast<std::size_t>(u) >= si.graph.out_adj.size()) continue;
            for (int ei : si.graph.out_adj[static_cast<std::size_t>(u)]) {
                const int v = si.graph.edges[static_cast<std::size_t>(ei)].to;
                if (depth[static_cast<std::size_t>(v)] >= 0) continue;
                depth[static_cast<std::size_t>(v)] = depth[static_cast<std::size_t>(u)] + 1;
                if (is_fundamental_node(si, v)) { hit = depth[static_cast<std::size_t>(v)]; break; }
                q.push_back(v);
            }
        }
        const float remoteness = (hit < 0) ? 1.0f
            : static_cast<float>(hit) / static_cast<float>(max_hops);
        const float score = static_cast<float>(deg[static_cast<std::size_t>(i)]) * remoteness;
        if (score <= 0.0f) continue;
        out.push_back({i, deg[static_cast<std::size_t>(i)], hit, score});
    }
    std::sort(out.begin(), out.end(),
              [](const AnomalyNode& a, const AnomalyNode& b) {
                  return a.anomaly_score > b.anomaly_score;
              });
    if (static_cast<int>(out.size()) > top_k) out.resize(static_cast<std::size_t>(top_k));
    return out;
}

// 6. classify_gap_query — the discovery front door: is this natural-language
//    line a discovery question, and which kind? (inference.hpp classifier.)
struct GapQuery {
    bool is_query = false;
    int kind = -1;   // 0 logic gap, 1 isolated, 2 dangling, 3 symmetry, 4 anomaly
    std::vector<std::string> terms;
};
inline GapQuery classify_gap_query(const std::string& query) {
    GapQuery g;
    std::string q;
    q.reserve(query.size());
    for (char c : query) q += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
    auto has = [&](const char* kw) { return q.find(kw) != std::string::npos; };
    if (has("symmetr")) g.kind = 3;
    else if (has("isolated") || has("floating")) g.kind = 1;
    else if (has("dangling") || has("dead end") || has("dead-end")) g.kind = 2;
    else if (has("anomal") || has("confused") || has("breakthrough")) g.kind = 4;
    else if (has("missing") || has("gap") || has("unlinked") ||
             has("not connected") || has("disconnected")) g.kind = 0;
    const bool interrogative = has("what") || has("where") || has("any") ||
        has("which") || has("find") || has("show") || has("list") ||
        has("?") || has("is there") || has("are there");
    if (g.kind < 0 || !interrogative) return g;
    g.is_query = true;
    static const char* stop[] = {
        "what", "whats", "where", "which", "any", "is", "are", "there", "the",
        "a", "an", "of", "in", "and", "or", "between", "from", "to", "find",
        "show", "list", "me", "tell", "us", "our", "missing", "gap", "gaps",
        "unlinked", "not", "connected", "disconnected", "isolated", "floating",
        "dangling", "dead", "end", "ends", "symmetry", "symmetries",
        "incomplete", "anomalies", "anomaly", "confused", "breakthrough",
        "graph", "concepts", "concept", "parts", "part", "it", "this", "that",
        "look", "should", "we", "for"};
    std::unordered_set<std::string> stopset(std::begin(stop), std::end(stop));
    std::istringstream iss(q);
    std::string w;
    while (iss >> w) {
        while (!w.empty() && !std::isalnum(static_cast<unsigned char>(w.back()))) w.pop_back();
        while (!w.empty() && !std::isalnum(static_cast<unsigned char>(w.front()))) w.erase(w.begin());
        if (w.size() < 2 || stopset.count(w)) continue;
        g.terms.push_back(w);
    }
    return g;
}

// ---------------------------------------------------------------------------
// AXIOM LOADER (axiom_loader.hpp) — SI's .axioms triple format, unchanged:
//   subject relation object [# comment]
// One triple per line; '#' comments; unknown relations skipped with a
// warning. SI's own data files (syllogism.axioms, insulin_pathway.axioms,
// maxwell_equations.axioms, ...) load unchanged.
// ---------------------------------------------------------------------------
struct AxiomWriteReport {
    bool accepted = false;
    bool duplicate = false;
    bool contradiction = false;
    std::string message;
};

inline AxiomWriteReport teach_axiom(ReasonCore& si, const std::string& subj,
                                    int rel, const std::string& obj,
                                    const std::string& source = "user") {
    AxiomWriteReport r;
    if (subj.empty() || obj.empty() || rel < 0) {
        r.message = "malformed triple";
        return r;
    }
    if (si.would_contradict(subj, rel, obj)) {
        r.contradiction = true;
        r.message = "contradicts an existing axiom (from " + source +
                    "); refused. Delete the conflicting premise first if intentional.";
        return r;
    }
    const auto it_s = si.graph.by_label.find(subj);
    const auto it_o = si.graph.by_label.find(obj);
    if (it_s != si.graph.by_label.end() && it_o != si.graph.by_label.end() &&
        si.graph.has_edge(it_s->second, it_o->second, rel)) {
        r.duplicate = true;
        r.accepted = true;
        r.message = "already known";
        return r;
    }
    si.assert_fact(subj, rel, obj);
    r.accepted = true;
    r.message = "added";
    return r;
}

inline int load_axiom_file(ReasonCore& si, const std::string& path, FILE* log = stderr) {
    std::ifstream in(path);
    if (!in) return 0;
    const bool prev_bulk = si.bulk_load;
    si.bulk_load = true;
    struct BulkGuard { ReasonCore& s; bool prev; ~BulkGuard() { s.bulk_load = prev; } }
        guard{si, prev_bulk};
    int accepted = 0, line_no = 0;
    std::string line;
    while (std::getline(in, line)) {
        ++line_no;
        const auto hash = line.find('#');
        if (hash != std::string::npos) line.erase(hash);
        std::istringstream iss(line);
        std::string subj, rel_tok, obj;
        if (!(iss >> subj >> rel_tok >> obj)) continue;
        const int rel = rel_from_token(rel_tok);
        if (rel < 0) {
            if (log) std::fprintf(log,
                "[axiom_loader] %s:%d unknown relation '%s' — skipped\n",
                path.c_str(), line_no, rel_tok.c_str());
            continue;
        }
        const auto rep = teach_axiom(si, subj, rel, obj, path);
        if (rep.accepted && !rep.duplicate) ++accepted;
        else if (rep.contradiction && log)
            std::fprintf(log, "[axiom_loader] %s:%d %s\n",
                         path.c_str(), line_no, rep.message.c_str());
    }
    if (log) std::fprintf(log, "[axiom_loader] %s: +%d accepted\n",
                          path.c_str(), accepted);
    return accepted;
}

inline bool append_axiom_line(const std::string& path, const std::string& subj,
                              int rel, const std::string& obj) {
    if (rel < 0 || rel >= REL_MAX) return false;
    std::ofstream out(path, std::ios::app);
    if (!out) return false;
    out << subj << ' ' << rel_name(rel) << ' ' << obj << '\n';
    return true;
}

} // namespace sxr