neuralos-snn 0.1.0-alpha.7

Spiking Neural Network library, no_std, i16 fixed-point — NeuralOS sovereignty stack core
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
//! NIR — the Neuromorphic Intermediate Representation, slice 1:
//! import (JSON container) + honest export for the node subset the
//! substrate can honor exactly: **`Input`, `Linear`, `LIF`, `Output`**.
//!
//! # Provenance (the doctrine: layouts come from the reference VERBATIM)
//!
//! Reference implementation: `neuromorphs/NIR` @
//! `7883c3c85f1be27ed113ccc9e8d6ab47ab541df4` (BSD-3, read 2026-08-21;
//! clone pinned at `nir-ref/`, gitignored). The dict schema implemented
//! here is the reference's `NIRNode::to_dict()` / `dict2NIRNode` shape
//! (`nir/ir/{node,graph,neuron,linear}.py`):
//!
//! ```text
//! { "version": <string>,                     // exactly one version block
//!   "node": { "type": "NIRGraph",
//!             "edges": [[src, dst], ...],     // node-name pairs
//!             "nodes": { <name>: { "type": "LIF"|"Linear"|"Input"|"Output",
//!                                   ...kind fields... },
//!                        ... } } }
//! ```
//!
//! The reference's own file container at this sha is HDF5
//! (`nir/serialization.py`, h5py); the dict schema is
//! container-independent and the JSON encoding of it is what this
//! module reads and writes (the historical NIR container). HDF5 `.nir`
//! file IO is slice-2 work and lands std-side (needs the hdf5 crate).
//!
//! LIF (`nir/ir/neuron.py`): `tau` [s], `r` [Ω], `v_leak` [V],
//! `v_threshold` [V], `v_reset` [V] — per-neuron arrays; `v_reset`
//! may be absent (defaults to zeros, reference `from_dict`
//! semantics). Linear (`nir/ir/linear.py`): `weight` (2-D,
//! `y = W·x`, rows = outputs). Input/Output: `shape`.
//!
//! # The quantization contract (THE design axis — loud by design)
//!
//! NIR numbers are float; the substrate is i16 fixed-point. Every
//! float→i16 hop is recorded in the node's quant record and rendered
//! into the export's `metadata.neuralos` block (`provenance` = source
//! floats, `quant` = the derived integers' scales/errors):
//!
//! - **Linear**: `q = round(w / scale)`, `scale = absmax/32767`
//!   (dequant `w' = q · scale`), `max_abs_err = max|w − w'|`. An
//!   all-zero tensor keeps `scale = 1` and a note.
//! - **LIF**: potentials → voltage quanta (`×1000` on the mV grid,
//!   `×100_000` on centi-mV), `tau` → μs, `r` → MΩ; rounding errors
//!   recorded. **Hard failures, never silent:** `tau ≤ 0`, `tau < dt`,
//!   threshold quantizing to 0, any potential outside the membrane
//!   bounds, `r ≤ 0` or outside u16 MΩ, non-finite numbers anywhere.
//! - `dt` is an explicit import argument (NIR LIF carries no
//!   timestep); the derived integers + `dt_us` live in the record.
//!
//! Export renders the **derived** values (substrate-exact integers
//! converted back to source units exactly, dequantized weights) so
//! importing an export reproduces the identical quantized node — the
//! idempotence gate. Provenance rides `metadata`, which the reference
//! round-trips natively. The scale algebra is exact: the max-|w|
//! element maps to ±32767 by construction, so re-import recovers
//! `scale` bit-exactly and `round(q·scale/scale) = q` (the double
//! rounding error is ≤ 1 ulp ≈ 7e-12 of full scale, five orders below
//! the 0.5 rounding boundary).
//!
//! # `no_std`, zero-alloc, zero new deps
//!
//! Everything is buffer-based, like [`crate::bridge`]: the caller owns
//! all memory. Two-pass protocol — [`nir_scan`] counts,
//! [`NirBuffers`] + [`nir_import`] fills (the import itself walks the
//! document twice: nodes, then edges — edge endpoints may appear
//! before their nodes in key order). During import each Linear's
//! source f64 weights stage into [`NirBuffers::scratch`] (one slot
//! per cell) and quantize into the weight arena, so both must hold
//! the total weight count; the arena keeps exactly the quantized
//! result. Export writes into a caller byte slice. Strings are
//! borrowed from the input (schema strings must be escape-free
//! printable ASCII — loud error otherwise). `f64` appears in the
//! setup path only, never in any per-step hot path.
//!
//! The structured-entry seam: callers that already hold materialized
//! values (HDF5 import, builders) skip the JSON reader and call
//! [`quantize_linear`] / [`quantize_lif`] directly — the same
//! quantization contract, the same errors, arena placement included —
//! or assemble the whole graph in memory with [`NirBuilder`] (std).
//! The printable-ASCII string gate is a property of the JSON
//! container: it fires at read and at [`nir_export`], never on the
//! typed surface.

#![allow(clippy::module_name_repetitions)]
// `tau_s` vs `tau_us`, `r_ohm` vs `r_mohm`: the unit suffixes ARE the
// distinction — domain names, not near-duplicates.
#![allow(clippy::similar_names)]
#![allow(
    clippy::cast_possible_truncation,
    clippy::cast_possible_wrap,
    clippy::cast_sign_loss
)]

use crate::lif_neuron::{VoltageResolution, MEMBRANE_MV_MAX, MEMBRANE_MV_MIN};
use core::fmt::Write as _;

/// The pinned reference commit this schema derives from (provenance
/// string for exports and reports).
pub const NIR_REF_SHA: &str = "7883c3c85f1be27ed113ccc9e8d6ab47ab541df4";

/// The version string our exports carry (the ONE version block).
/// States the reference sha the schema is pinned to.
pub const EXPORT_VERSION: &str = "nir@7883c3c";

/// i16 weight full-scale used by the Linear quantizer.
const I16_FS: f64 = 32_767.0;

// ---------------------------------------------------------------------------
// Errors — loud, no clamping, no guessing
// ---------------------------------------------------------------------------

/// Everything that can go wrong importing/exporting NIR. Copy +
/// borrowed strings only (no alloc).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NirError<'a> {
    /// Malformed JSON at byte `pos`.
    Json(usize),
    /// A string we must read (type/name/version/edge endpoint) carries
    /// a JSON escape or non-ASCII byte — outside the documented subset.
    EscapedOrNonAsciiString(usize),
    /// A node name outside the escape-free printable-ASCII subset the
    /// JSON container can write (carries the name). The
    /// structured-entry builder accepts any `&str`; this gate fires
    /// only at the JSON boundary — [`nir_export`].
    NonAsciiNodeName(&'a str),
    /// A node kind outside the slice-1 subset (includes `Affine`,
    /// `CubaLIF`, `Conv1d`, …). Carries the kind name.
    UnsupportedNodeKind(&'a str),
    /// A required field is missing from a node/graph object.
    MissingField(&'static str),
    /// A field has the wrong JSON shape (e.g. scalar where the
    /// reference emits an array, weight not 2-D).
    BadShape(&'static str),
    /// `tau ≤ 0`, `r ≤ 0`/out of range, or a non-finite number.
    BadNumber(&'static str),
    /// `tau < dt` — the derived decay would be nonsense.
    TauBelowDt,
    /// `v_threshold` quantized to 0 quanta — a deaf neuron.
    ThresholdZero,
    /// A potential quantizes outside `[MEMBRANE_MV_MIN, MEMBRANE_MV_MAX]`.
    PotentialOutOfRange(&'static str),
    /// More nodes/edges/weights than the caller's buffers hold.
    BufferOverflow,
    /// An edge endpoint names no node.
    UnknownEdgeEndpoint(&'a str),
    /// The same edge appears twice (reference `validate_structure`).
    DuplicateEdge,
    /// A node name appears twice in `nodes`.
    DuplicateNodeName,
    /// Topology outside the slice-1 assembly (the format layer still
    /// imports it; only [`NirImport::build_chain_network`] rejects —
    /// e.g. a LIF population whose size ≠ the feeding Linear's
    /// rows).
    UnsupportedTopology(&'static str),
    /// A per-edge type-shape mismatch (reference `check_types`
    /// parity): the `src → dst` edge's tensor shapes disagree.
    EdgeShapeMismatch { src: &'a str, dst: &'a str },
    /// The export byte buffer is too small.
    ExportTooSmall,
}

impl core::fmt::Display for NirError<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Json(p) => write!(f, "malformed JSON at byte {p}"),
            Self::EscapedOrNonAsciiString(p) => write!(
                f,
                "string at byte {p} uses escapes/non-ASCII — outside the documented subset"
            ),
            Self::NonAsciiNodeName(n) => write!(
                f,
                "node name '{n}' outside the printable-ASCII subset the JSON container writes"
            ),
            Self::UnsupportedNodeKind(k) => write!(
                f,
                "node kind '{k}' outside the slice-1 subset (Input/Linear/LIF/Output)"
            ),
            Self::MissingField(n) => write!(f, "missing required field '{n}'"),
            Self::BadShape(n) => write!(f, "field '{n}' has the wrong shape"),
            Self::BadNumber(n) => {
                write!(f, "non-finite or out-of-range value in '{n}'")
            }
            Self::TauBelowDt => write!(f, "tau < dt — derived decay would be nonsense"),
            Self::ThresholdZero => write!(f, "v_threshold quantizes to 0 — a deaf neuron"),
            Self::PotentialOutOfRange(n) => {
                write!(f, "'{n}' quantizes outside the membrane bounds")
            }
            Self::BufferOverflow => write!(f, "caller buffers too small for this graph"),
            Self::UnknownEdgeEndpoint(n) => write!(f, "edge endpoint '{n}' names no node"),
            Self::DuplicateEdge => write!(f, "duplicate edge"),
            Self::DuplicateNodeName => write!(f, "duplicate node name"),
            Self::UnsupportedTopology(n) => {
                write!(f, "topology unsupported by slice 1: {n}")
            }
            Self::EdgeShapeMismatch { src, dst } => write!(
                f,
                "edge shape mismatch: '{src}' -> '{dst}' (reference type-check parity)"
            ),
            Self::ExportTooSmall => write!(f, "export byte buffer too small"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for NirError<'_> {}

// ---------------------------------------------------------------------------
// JSON reader — the documented subset, zero alloc, borrowed strings
// ---------------------------------------------------------------------------

/// Maximum container nesting [`Reader::skip_value`] will walk before
/// rejecting the document. The schema's own depth is ≤ 6; 64 leaves
/// generous headroom for foreign `metadata` while bounding the
/// recursion (no stack overflow on adversarial input).
const MAX_SKIP_DEPTH: usize = 64;

struct Reader<'a> {
    b: &'a [u8],
    pos: usize,
}

impl<'a> Reader<'a> {
    fn new(b: &'a [u8]) -> Self {
        Self { b, pos: 0 }
    }

    fn ws(&mut self) {
        while let Some(&c) = self.b.get(self.pos) {
            if matches!(c, b' ' | b'\t' | b'\n' | b'\r') {
                self.pos += 1;
            } else {
                break;
            }
        }
    }

    fn peek(&mut self) -> Option<u8> {
        self.ws();
        self.b.get(self.pos).copied()
    }

    fn eat(&mut self, c: u8) -> Result<(), NirError<'static>> {
        if self.peek() == Some(c) {
            self.pos += 1;
            Ok(())
        } else {
            Err(NirError::Json(self.pos))
        }
    }

    fn eat_lit(&mut self, lit: &str) -> Result<(), NirError<'static>> {
        if self.b[self.pos..].starts_with(lit.as_bytes()) {
            self.pos += lit.len();
            Ok(())
        } else {
            Err(NirError::Json(self.pos))
        }
    }

    /// A string we must READ: escape-free printable ASCII, borrowed.
    /// (`\` is 0x5c — printable — so it is checked explicitly first.)
    fn read_string(&mut self) -> Result<&'a str, NirError<'static>> {
        self.eat(b'"')?;
        let start = self.pos;
        loop {
            match self.b.get(self.pos) {
                None => return Err(NirError::Json(self.pos)),
                Some(b'"') => break,
                Some(&c) if c != b'\\' && (0x20..=0x7e).contains(&c) => self.pos += 1,
                Some(_) => return Err(NirError::EscapedOrNonAsciiString(self.pos)),
            }
        }
        let s = core::str::from_utf8(&self.b[start..self.pos])
            .map_err(|_| NirError::EscapedOrNonAsciiString(start))?;
        self.pos += 1; // closing quote
        Ok(s)
    }

    /// A number as f64 (core's correctly-rounded parser — the same
    /// values Python's `float()` produces for the same token).
    /// Grammar note: this accepts Rust's `f64::FromStr` set, a slight
    /// SUPERSET of JSON's (`.5`, `1.`, `+5`, `007` parse here, are
    /// errors in Python's `json`). The reference emitter never
    /// produces these forms and the numeric values agree wherever
    /// both accept — a documented laxity, not an accident.
    fn read_number(&mut self) -> Result<f64, NirError<'static>> {
        self.ws();
        let start = self.pos;
        while let Some(&c) = self.b.get(self.pos) {
            if matches!(c, b'-' | b'+' | b'.' | b'e' | b'E' | b'0'..=b'9') {
                self.pos += 1;
            } else {
                break;
            }
        }
        if start == self.pos {
            return Err(NirError::Json(self.pos));
        }
        let tok =
            core::str::from_utf8(&self.b[start..self.pos]).map_err(|_| NirError::Json(start))?;
        tok.parse::<f64>().map_err(|_| NirError::Json(start))
    }

    /// Array-element stepper: `first` gates the opening `[`; returns
    /// `true` when an element follows (caller parses it), `false` at
    /// `]`.
    fn array_step(&mut self, first: &mut bool) -> Result<bool, NirError<'static>> {
        if *first {
            self.eat(b'[')?;
            *first = false;
            if self.peek() == Some(b']') {
                self.pos += 1;
                return Ok(false);
            }
            Ok(true)
        } else if self.peek() == Some(b',') {
            self.pos += 1;
            Ok(true)
        } else if self.peek() == Some(b']') {
            self.pos += 1;
            Ok(false)
        } else {
            Err(NirError::Json(self.pos))
        }
    }

    /// Object-key stepper: yields the next key (value left for the
    /// caller to parse/skip), `None` at `}`. Duplicate keys:
    /// scalar fields are last-wins (Python `json.loads` semantics);
    /// repeated container fields (`edges`, `nodes`) are visited in
    /// order and their contents aggregate. Our exports never repeat
    /// a key; the reference emitter neither.
    fn object_step(&mut self, first: &mut bool) -> Result<Option<&'a str>, NirError<'static>> {
        if *first {
            self.eat(b'{')?;
            *first = false;
            if self.peek() == Some(b'}') {
                self.pos += 1;
                return Ok(None);
            }
        } else if self.peek() == Some(b',') {
            self.pos += 1;
        } else if self.peek() == Some(b'}') {
            self.pos += 1;
            return Ok(None);
        } else {
            return Err(NirError::Json(self.pos));
        }
        let key = self.read_string()?;
        self.eat(b':')?;
        Ok(Some(key))
    }

    /// Skip one value of any shape (unknown fields, `metadata`).
    /// Depth-capped: nesting beyond [`MAX_SKIP_DEPTH`] is rejected as
    /// malformed instead of overflowing the call stack (a 50k-deep
    /// junk array is adversarial input, not a document).
    fn skip_value(&mut self, depth: usize) -> Result<(), NirError<'static>> {
        if depth > MAX_SKIP_DEPTH {
            return Err(NirError::Json(self.pos));
        }
        match self.peek() {
            Some(b'"') => {
                self.pos += 1;
                while let Some(&c) = self.b.get(self.pos) {
                    self.pos += 1;
                    if c == b'\\' {
                        let _ = self.b.get(self.pos).ok_or(NirError::Json(self.pos))?;
                        self.pos += 1;
                    } else if c == b'"' {
                        return Ok(());
                    }
                }
                Err(NirError::Json(self.pos))
            }
            Some(b'{') => {
                let mut first = true;
                while self.object_step(&mut first)?.is_some() {
                    self.skip_value(depth + 1)?;
                }
                Ok(())
            }
            Some(b'[') => {
                let mut first = true;
                while self.array_step(&mut first)? {
                    self.skip_value(depth + 1)?;
                }
                Ok(())
            }
            Some(b't') => self.eat_lit("true"),
            Some(b'f') => self.eat_lit("false"),
            Some(b'n') => self.eat_lit("null"),
            Some(c) if c == b'-' || c.is_ascii_digit() => self.read_number().map(|_| ()),
            _ => Err(NirError::Json(self.pos)),
        }
    }
}

// ---------------------------------------------------------------------------
// Schema types
// ---------------------------------------------------------------------------

/// Node kinds of the slice-1 subset.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NirNodeKind {
    /// Virtual input plumbing (shape carrier).
    Input,
    /// Virtual output plumbing (shape carrier).
    Output,
    /// `y = W·x` — quantized into the weight arena.
    Linear,
    /// Leaky integrate-and-fire — quantized onto the voltage grid.
    Lif,
}

/// A quantized LIF node: provenance floats + derived substrate
/// integers. Export renders the derived fields; provenance rides
/// `metadata`.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct NirLif {
    // provenance (source units: s, Ω, V)
    pub tau_s: f64,
    pub r_ohm: f64,
    pub v_leak_v: f64,
    pub v_threshold_v: f64,
    pub v_reset_v: f64,
    pub v_reset_defaulted: bool,
    // derived (substrate units)
    pub tau_us: u32,
    pub resistance_mohm: u16,
    pub capacitance_pf: u16,
    pub capacitance_clamped: bool,
    pub leak_q: i16,
    pub threshold_q: i16,
    pub reset_q: i16,
    // quantization record
    pub tau_err_s: f64,
    pub max_v_err_v: f64,
}

/// A quantized LIF population view: `len` neurons with per-neuron
/// records in the caller's lifs buffer ([`NirBuffers::lifs`] or
/// [`NirImport::lifs`]), starting at `offset`. A length-1
/// population is the slice-1 single neuron.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NirLifPopulation {
    /// First record index in the lifs buffer.
    pub offset: usize,
    /// Population size (the per-neuron param array length).
    pub len: usize,
}

/// Per-neuron source-unit LIF parameters (population form) for the
/// structured-entry builder: every slice must have equal length
/// ≥ 1; `v_reset_v` `None` = the reference's absent-`v_reset`
/// zeros semantics.
#[derive(Debug, Clone, Copy)]
pub struct NirLifParams<'v> {
    pub tau_s: &'v [f64],
    pub r_ohm: &'v [f64],
    pub v_leak_v: &'v [f64],
    pub v_threshold_v: &'v [f64],
    pub v_reset_v: Option<&'v [f64]>,
}

/// A quantized Linear node: weights live in the shared i16 arena.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NirLinear {
    pub rows: usize,
    pub cols: usize,
    /// Arena view `[offset, offset + rows·cols)`, row-major
    /// (`weight[out][in]`, the reference layout).
    pub weight_offset: usize,
    /// Dequant factor: `w' = q · scale`.
    pub scale: f64,
    /// Source tensor max |w| (provenance).
    pub absmax: f64,
    /// `max |w − q·scale|` over the tensor.
    pub max_abs_err: f64,
    pub zero_tensor: bool,
}

/// One imported node. Shapes are `[u32; 4]` + length (≤ 4 dims,
/// reference shapes are 1–4-D). A LIF node carries a population
/// view into the lifs buffer.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NirNode<'a> {
    pub name: &'a str,
    pub kind: NirNodeKind,
    pub shape: [u32; 4],
    pub shape_len: usize,
    pub lif: Option<NirLifPopulation>,
    pub linear: Option<NirLinear>,
}

/// Import notes (loud lossiness — each counted, none silent).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(usize)]
pub enum NirNote {
    /// `v_reset` absent — defaulted to 0 V (reference semantics).
    VResetDefaulted = 0,
    /// Derived capacitance exceeded u16 — clamped (informational; the
    /// substrate's leak uses tau, not C).
    CapacitanceClamped,
    /// `tau` lost sub-μs precision in the →μs rounding.
    TauTruncated,
    /// Some potential lost sub-quantum precision.
    PotentialTruncated,
    /// All-zero weight tensor — scale pinned to 1.
    ZeroWeightTensor,
    /// The quantize→dequant round moved some weight (`max_abs_err` > 0).
    QuantizationLoss,
}

/// Number of note kinds ([`NirReport::notes`] length).
pub const NIR_NOTE_KINDS: usize = 6;

/// The loud import report.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct NirReport {
    pub inputs: usize,
    pub outputs: usize,
    pub linears: usize,
    pub lifs: usize,
    pub edges: usize,
    pub weight_cells: usize,
    pub notes: [usize; NIR_NOTE_KINDS],
}

impl NirReport {
    /// Total noted events (0 = a fully lossless import).
    #[must_use]
    pub fn note_count(&self) -> usize {
        self.notes.iter().sum()
    }

    fn note(&mut self, n: NirNote) {
        self.notes[n as usize] += 1;
    }
}

/// First pass: buffer sizes + the version string.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NirScan<'a> {
    pub version: &'a str,
    pub node_count: usize,
    pub edge_count: usize,
    pub weight_cells: usize,
    /// Total per-neuron LIF records (`sum` of the param array
    /// lengths). Foreign `tau`-like arrays on non-LIF nodes may
    /// inflate this bound on malformed documents (overprovision
    /// only, never under).
    pub lif_neurons: usize,
}

/// Caller-owned buffers for [`nir_import`]. `weights`, `lifs` and
/// `scratch` must each hold [`NirScan::weight_cells`],
/// [`NirScan::lif_neurons`], and `weight_cells + 5 × lif_neurons`
/// cells respectively: import stages each Linear's source f64
/// weights and each LIF's five param arrays into `scratch`
/// (transiently) while quantizing into the arena and the lifs
/// buffer, which keep exactly the results.
#[derive(Debug)]
pub struct NirBuffers<'buf, 'a> {
    /// `bufs` borrow lifetime (`'buf`) and json-data lifetime (`'a`).
    pub nodes: &'buf mut [NirNode<'a>],
    pub edges: &'buf mut [(u32, u32)],
    pub weights: &'buf mut [i16],
    /// Quantized LIF records (one per neuron; population views
    /// index into this).
    pub lifs: &'buf mut [NirLif],
    /// Transient f64 staging (import only; one slot per staged
    /// weight cell or LIF param).
    pub scratch: &'buf mut [f64],
}

/// Import options: the two things NIR does not carry that the
/// substrate needs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NirImportOptions {
    /// Simulation timestep (μs). Hard requirement: `tau ≥ dt`.
    pub dt_us: u32,
    /// Voltage grid the potentials quantize onto.
    pub resolution: VoltageResolution,
}

impl NirImportOptions {
    /// Options from `(dt, grid)`.
    #[must_use]
    pub const fn new(dt_us: u32, resolution: VoltageResolution) -> Self {
        Self { dt_us, resolution }
    }
}

impl Default for NirImportOptions {
    fn default() -> Self {
        Self::new(1_000, VoltageResolution::Millivolt)
    }
}

// ---------------------------------------------------------------------------
// Quantization (pure, unit-tested below)
// ---------------------------------------------------------------------------

/// `f64::round` (half away from zero) without std — `round`/`floor`/
/// `ceil` are std-only. Truncate-then-compare: `as i64` truncates
/// toward zero (saturating at the extremes), then the exact fraction
/// decides. (The classic add-±0.5-then-truncate idiom MISROUNDS
/// values up to 1 ulp below a half boundary — `0.49999999999999994`
/// would yield 1.0 — and that value is reachable via `r` in MΩ.)
/// Every caller pre-validates finiteness and bounds; saturation at
/// ±`i64::MAX` lands far outside every caller's accepted range and
/// fails their checks loudly.
#[allow(clippy::cast_precision_loss)]
fn round_half_away(x: f64) -> f64 {
    let t = x as i64 as f64;
    let frac = x - t;
    if frac >= 0.5 {
        t + 1.0
    } else if frac <= -0.5 {
        t - 1.0
    } else {
        t
    }
}

/// Quantize one potential (V) onto the grid. Loud on out-of-range.
fn quant_potential(
    v_v: f64,
    field: &'static str,
    scale: i32,
) -> Result<(i16, f64), NirError<'static>> {
    if !v_v.is_finite() {
        return Err(NirError::BadNumber(field));
    }
    let q_f = v_v * 1000.0 * f64::from(scale);
    let q = round_half_away(q_f);
    let lo = f64::from(MEMBRANE_MV_MIN) * f64::from(scale);
    let hi = f64::from(MEMBRANE_MV_MAX) * f64::from(scale);
    if q < lo || q > hi {
        return Err(NirError::PotentialOutOfRange(field));
    }
    let q = q as i16;
    let err = (q_f - f64::from(q)).abs() / (1000.0 * f64::from(scale));
    Ok((q, err))
}

/// Quantize a LIF parameter set (single neuron) onto the substrate
/// grids (μs / MΩ / voltage quanta) — the structured-entry seam's LIF
/// half. Every hard failure of the contract fires here.
///
/// # Errors
///
/// [`NirError::BadNumber`] on non-finite values, `tau ≤ 0`, `r ≤ 0`
/// or out-of-range magnitudes; [`NirError::TauBelowDt`];
/// [`NirError::ThresholdZero`]; [`NirError::PotentialOutOfRange`].
pub fn quantize_lif(
    tau_s: f64,
    r_ohm: f64,
    v_leak_v: f64,
    v_threshold_v: f64,
    v_reset_v: f64,
    v_reset_defaulted: bool,
    opts: NirImportOptions,
) -> Result<NirLif, NirError<'static>> {
    if [tau_s, r_ohm, v_leak_v, v_threshold_v, v_reset_v]
        .iter()
        .any(|v| !v.is_finite())
    {
        return Err(NirError::BadNumber("LIF param"));
    }
    if tau_s <= 0.0 {
        return Err(NirError::BadNumber("tau"));
    }
    if r_ohm <= 0.0 {
        return Err(NirError::BadNumber("r"));
    }
    let tau_us_f = tau_s * 1.0e6;
    let tau_us_round = round_half_away(tau_us_f);
    if !(1.0..=f64::from(u32::MAX)).contains(&tau_us_round) {
        return Err(NirError::BadNumber("tau"));
    }
    let tau_us = tau_us_round as u32;
    if f64::from(tau_us) < f64::from(opts.dt_us) {
        return Err(NirError::TauBelowDt);
    }
    let tau_err_s = (tau_us_f - f64::from(tau_us)).abs() * 1.0e-6;

    let r_millions = r_ohm / 1.0e6;
    let r_mohm = round_half_away(r_millions);
    if !(1.0..=f64::from(u16::MAX)).contains(&r_mohm) {
        return Err(NirError::BadNumber("r"));
    }

    let s = opts.resolution.scale();
    let (leak_q, e1) = quant_potential(v_leak_v, "v_leak", s)?;
    let (threshold_q, e2) = quant_potential(v_threshold_v, "v_threshold", s)?;
    if threshold_q == 0 {
        return Err(NirError::ThresholdZero);
    }
    let (reset_q, e3) = quant_potential(v_reset_v, "v_reset", s)?;

    // C[F] = tau/r → pF = tau_s/r · 1e12; clamp is informational
    let c_pf_f = tau_s / r_ohm * 1.0e12;
    let capacitance_clamped = c_pf_f > f64::from(u16::MAX);
    let capacitance_pf = round_half_away(c_pf_f.clamp(0.0, f64::from(u16::MAX))) as u16;

    Ok(NirLif {
        tau_s,
        r_ohm,
        v_leak_v,
        v_threshold_v,
        v_reset_v,
        v_reset_defaulted,
        tau_us,
        resistance_mohm: r_mohm as u16,
        capacitance_pf,
        capacitance_clamped,
        leak_q,
        threshold_q,
        reset_q,
        tau_err_s,
        max_v_err_v: e1.max(e2).max(e3),
    })
}

/// Quantize a materialized Linear weight tensor into the arena at
/// `offset` — the structured-entry seam's Linear half. `values` is
/// row-major `weight[out][in]` (`y = W·x`, rows = outputs), the same
/// contract the JSON importer applies; callers that hold f64 weights
/// (HDF5 import, builders) enter here without a JSON document.
///
/// # Errors
///
/// [`NirError::BadShape("weight")`] unless `values.len()` is exactly
/// `rows·cols` with both nonzero; [`NirError::BufferOverflow`] when
/// the arena cannot hold `offset + rows·cols`; [`NirError::BadNumber`]
/// on any non-finite value or a `scale` that underflows to 0 (denormal
/// `absmax` — the record would lie, per the R3 review finding).
pub fn quantize_linear(
    values: &[f64],
    rows: usize,
    cols: usize,
    arena: &mut [i16],
    offset: usize,
) -> Result<NirLinear, NirError<'static>> {
    if rows == 0 || cols == 0 {
        return Err(NirError::BadShape("weight"));
    }
    let n = rows.checked_mul(cols).ok_or(NirError::BadShape("weight"))?;
    if values.len() != n {
        return Err(NirError::BadShape("weight"));
    }
    let end = offset.checked_add(n).ok_or(NirError::BufferOverflow)?;
    if end > arena.len() {
        return Err(NirError::BufferOverflow);
    }
    let mut absmax = 0.0f64;
    for &v in values {
        if !v.is_finite() {
            return Err(NirError::BadNumber("weight"));
        }
        absmax = absmax.max(v.abs());
    }
    let (scale, zero_tensor) = if absmax == 0.0 {
        (1.0, true)
    } else {
        (absmax / I16_FS, false)
    };
    // Denormal `absmax`: `absmax/32767` underflows to exactly 0.0 —
    // finite, so every prior check passes, but then q·scale = 0 ≠
    // absmax and the quant record would lie (export would silently
    // zero the tensor and break idempotence). Loud, per the contract.
    // (NaN unreachable: absmax is finite and I16_FS is a nonzero
    // constant, so `== 0.0` is exact, not partial-order fuzz.)
    if scale == 0.0 {
        return Err(NirError::BadNumber("weight"));
    }
    let mut max_abs_err = 0.0f64;
    for (k, &v) in values.iter().enumerate() {
        let q = round_half_away(v / scale).clamp(-I16_FS, I16_FS) as i16;
        arena[offset + k] = q;
        max_abs_err = max_abs_err.max((v - f64::from(q) * scale).abs());
    }
    Ok(NirLinear {
        rows,
        cols,
        weight_offset: offset,
        scale,
        absmax,
        max_abs_err,
        zero_tensor,
    })
}

// ---------------------------------------------------------------------------
// Pass 1: scan (counts + version; full shape walk)
// ---------------------------------------------------------------------------

/// Scan a NIR JSON document: counts + version. Walks the full schema
/// shape (so malformed structure fails here); param values are NOT
/// validated — that is import's job.
pub fn nir_scan(json: &[u8]) -> Result<NirScan<'_>, NirError<'_>> {
    let mut r = Reader::new(json);
    let mut version: Option<&str> = None;
    let mut node_count = 0usize;
    let mut edge_count = 0usize;
    let mut weight_cells = 0usize;
    let mut lif_neurons = 0usize;
    let mut saw_node = false;

    let mut first = true;
    while let Some(key) = r.object_step(&mut first)? {
        match key {
            "version" => version = Some(r.read_string()?),
            "node" => {
                saw_node = true;
                scan_graph(
                    &mut r,
                    &mut node_count,
                    &mut edge_count,
                    &mut weight_cells,
                    &mut lif_neurons,
                )?;
            }
            _ => r.skip_value(0)?,
        }
    }
    // strict subset: nothing but whitespace may follow the root object
    // (Python's `json.loads` rejects trailing content; so do we)
    if r.peek().is_some() {
        return Err(NirError::Json(r.pos));
    }
    let version = version.ok_or(NirError::MissingField("version"))?;
    if !saw_node {
        return Err(NirError::MissingField("node"));
    }
    Ok(NirScan {
        version,
        node_count,
        edge_count,
        weight_cells,
        lif_neurons,
    })
}

fn scan_graph(
    r: &mut Reader<'_>,
    node_count: &mut usize,
    edge_count: &mut usize,
    weight_cells: &mut usize,
    lif_neurons: &mut usize,
) -> Result<(), NirError<'static>> {
    let mut first = true;
    while let Some(key) = r.object_step(&mut first)? {
        match key {
            "type" => {
                if r.read_string()? != "NIRGraph" {
                    return Err(NirError::BadShape("node.type"));
                }
            }
            "edges" => {
                let mut efirst = true;
                while r.array_step(&mut efirst)? {
                    let mut pfirst = true;
                    let mut n = 0;
                    while r.array_step(&mut pfirst)? {
                        r.read_string()?;
                        n += 1;
                    }
                    if n != 2 {
                        return Err(NirError::BadShape("edges"));
                    }
                    *edge_count += 1;
                }
            }
            "nodes" => {
                let mut nfirst = true;
                while r.object_step(&mut nfirst)?.is_some() {
                    scan_node(r, weight_cells, lif_neurons)?;
                    *node_count += 1;
                }
            }
            _ => r.skip_value(0)?,
        }
    }
    Ok(())
}

fn scan_node(
    r: &mut Reader<'_>,
    weight_cells: &mut usize,
    lif_neurons: &mut usize,
) -> Result<(), NirError<'static>> {
    // per-node LIF population bookkeeping: the five param arrays
    // must all be present-at-equal-length (v_reset may be absent)
    let mut pop: Option<usize> = None;
    let mut first = true;
    while let Some(key) = r.object_step(&mut first)? {
        match key {
            "type" => {
                r.read_string()?; // validated at import
            }
            "weight" => {
                let mut depth = 0usize;
                count_array(r, &mut depth, weight_cells)?;
            }
            "tau" | "r" | "v_leak" | "v_threshold" | "v_reset" => {
                let n = count_param_array(r)?;
                match pop {
                    None => pop = Some(n),
                    Some(p) if p != n => return Err(NirError::BadShape("LIF param")),
                    Some(_) => {}
                }
            }
            _ => r.skip_value(0)?,
        }
    }
    *lif_neurons += pop.unwrap_or(0);
    Ok(())
}

/// Walk one LIF param array, counting elements (each must be a
/// number — nesting is a wrong-shape rejection here, matching
/// [`count_array`]'s strictness).
fn count_param_array(r: &mut Reader<'_>) -> Result<usize, NirError<'static>> {
    let mut n = 0usize;
    let mut first = true;
    while r.array_step(&mut first)? {
        if r.peek() == Some(b'[') {
            return Err(NirError::BadShape("LIF param"));
        }
        r.read_number()?;
        n += 1;
    }
    Ok(n)
}

/// Recursively walk a weight array, counting leaves. A weight tensor
/// is EXACTLY 2-D of non-empty rows — 1-D, empty, empty-row, and 3-D
/// shapes all fail here (scan's "malformed structure fails here"
/// contract); ragged rows still fail at import (shape is checked,
/// not counted, there).
fn count_array(
    r: &mut Reader<'_>,
    depth: &mut usize,
    leaves: &mut usize,
) -> Result<(), NirError<'static>> {
    if *depth >= 2 {
        // 2-D max (a weight tensor); deeper = wrong shape
        return Err(NirError::BadShape("weight"));
    }
    let mut first = true;
    let mut elems = 0usize;
    let mut nested = false;
    while r.array_step(&mut first)? {
        if r.peek() == Some(b'[') {
            *depth += 1;
            nested = true;
            count_array(r, depth, leaves)?;
            *depth -= 1;
        } else {
            r.read_number()?;
            *leaves += 1;
        }
        elems += 1;
    }
    if elems == 0 {
        // `[]` outer or an empty row `[[]]`
        return Err(NirError::BadShape("weight"));
    }
    if *depth == 0 && !nested {
        // a flat array at the top = 1-D tensor
        return Err(NirError::BadShape("weight"));
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Pass 2: import (nodes walk, then edges walk)
// ---------------------------------------------------------------------------

/// Import a NIR JSON document into caller buffers. Fills `nodes` in
/// document order, `edges` as resolved node-index pairs, and the
/// quantized weights into the arena. On any error nothing is promised
/// about buffer contents.
// two documented walks (nodes, then edges) + their trailing checks;
// the 100-line cap is crossed by the EOF checks alone (network.rs
// precedent for the allow)
#[allow(clippy::too_many_lines)]
pub fn nir_import<'a>(
    json: &'a [u8],
    opts: NirImportOptions,
    bufs: &mut NirBuffers<'_, 'a>,
) -> Result<NirReport, NirError<'a>> {
    let mut report = NirReport::default();

    // walk 1: nodes (+ weights + LIF populations)
    let mut node_count = 0usize;
    let mut weight_fill = 0usize;
    let mut lif_fill = 0usize;
    {
        let mut r = Reader::new(json);
        let mut first = true;
        while let Some(key) = r.object_step(&mut first)? {
            if key == "node" {
                let mut gfirst = true;
                while let Some(gkey) = r.object_step(&mut gfirst)? {
                    if gkey == "nodes" {
                        let mut nfirst = true;
                        while let Some(name) = r.object_step(&mut nfirst)? {
                            if node_count >= bufs.nodes.len() {
                                return Err(NirError::BufferOverflow);
                            }
                            import_node(
                                &mut r,
                                name,
                                opts,
                                bufs,
                                &mut node_count,
                                &mut weight_fill,
                                &mut lif_fill,
                                &mut report,
                            )?;
                        }
                    } else {
                        r.skip_value(0)?;
                    }
                }
            } else {
                r.skip_value(0)?;
            }
        }
        // trailing content after the root is rejected here too
        if r.peek().is_some() {
            return Err(NirError::Json(r.pos));
        }
    }
    report.weight_cells = weight_fill;

    // duplicate node names (the reference keys nodes by name)
    for i in 0..node_count {
        for j in (i + 1)..node_count {
            if bufs.nodes[i].name == bufs.nodes[j].name {
                return Err(NirError::DuplicateNodeName);
            }
        }
    }

    // walk 2: edges (names now resolvable, key order irrelevant)
    let mut edge_count = 0usize;
    {
        let mut r = Reader::new(json);
        let mut first = true;
        while let Some(key) = r.object_step(&mut first)? {
            if key == "node" {
                let mut gfirst = true;
                while let Some(gkey) = r.object_step(&mut gfirst)? {
                    if gkey == "edges" {
                        let mut efirst = true;
                        while r.array_step(&mut efirst)? {
                            if edge_count >= bufs.edges.len() {
                                return Err(NirError::BufferOverflow);
                            }
                            let mut pair = [0u32; 2];
                            let mut pfirst = true;
                            let mut pi = 0;
                            while r.array_step(&mut pfirst)? {
                                if pi >= 2 {
                                    return Err(NirError::BadShape("edges"));
                                }
                                let name = r.read_string()?;
                                let idx = (0..node_count)
                                    .find(|&i| bufs.nodes[i].name == name)
                                    .ok_or(NirError::UnknownEdgeEndpoint(name))?;
                                pair[pi] = idx as u32;
                                pi += 1;
                            }
                            if pi != 2 {
                                return Err(NirError::BadShape("edges"));
                            }
                            bufs.edges[edge_count] = (pair[0], pair[1]);
                            edge_count += 1;
                            report.edges = edge_count;
                        }
                    } else {
                        r.skip_value(0)?;
                    }
                }
            } else {
                r.skip_value(0)?;
            }
        }
        // and here (walk 2 sees the same document)
        if r.peek().is_some() {
            return Err(NirError::Json(r.pos));
        }
    }

    // duplicate edges (reference validate_structure)
    for i in 0..edge_count {
        for j in (i + 1)..edge_count {
            if bufs.edges[i] == bufs.edges[j] {
                return Err(NirError::DuplicateEdge);
            }
        }
    }
    Ok(report)
}

/// Read one LIF param array, staging its values contiguously into
/// `scratch` at `*fill`; returns the `(start, len)` staged range.
fn read_param_array(
    r: &mut Reader<'_>,
    scratch: &mut [f64],
    fill: &mut usize,
    field: &'static str,
) -> Result<(usize, usize), NirError<'static>> {
    let start = *fill;
    let mut first = true;
    while r.array_step(&mut first)? {
        if r.peek() == Some(b'[') {
            return Err(NirError::BadShape(field));
        }
        let v = r.read_number()?;
        if *fill >= scratch.len() {
            return Err(NirError::BufferOverflow);
        }
        scratch[*fill] = v;
        *fill += 1;
    }
    Ok((start, *fill - start))
}

/// LIF tail of [`import_node`]: validate the five staged param
/// ranges (equal lengths, ≥ 1; `v_reset` absent = reference zeros),
/// quantize the population per neuron via [`quantize_lif`] into
/// `bufs.lifs`, note lossiness, and return the population view.
#[allow(clippy::too_many_arguments)]
fn finish_lif_population(
    tau: Option<(usize, usize)>,
    res: Option<(usize, usize)>,
    v_leak: Option<(usize, usize)>,
    v_threshold: Option<(usize, usize)>,
    v_reset: Option<(usize, usize)>,
    bufs: &mut NirBuffers<'_, '_>,
    opts: NirImportOptions,
    report: &mut NirReport,
    lif_fill: &mut usize,
) -> Result<NirLifPopulation, NirError<'static>> {
    let rng = |o: Option<(usize, usize)>, f: &'static str| o.ok_or(NirError::MissingField(f));
    let tau_r = rng(tau, "tau")?;
    let res_r = rng(res, "r")?;
    let leak_r = rng(v_leak, "v_leak")?;
    let thr_r = rng(v_threshold, "v_threshold")?;
    let n = tau_r.1;
    if n == 0 {
        return Err(NirError::BadShape("tau"));
    }
    for (r_, f) in [(res_r, "r"), (leak_r, "v_leak"), (thr_r, "v_threshold")] {
        if r_.1 != n {
            return Err(NirError::BadShape(f));
        }
    }
    if let Some(vr) = v_reset {
        if vr.1 != n {
            return Err(NirError::BadShape("v_reset"));
        }
    } else {
        report.note(NirNote::VResetDefaulted);
    }
    let start = *lif_fill;
    if start + n > bufs.lifs.len() {
        return Err(NirError::BufferOverflow);
    }
    for i in 0..n {
        let v_reset_val = match v_reset {
            Some((s, _)) => bufs.scratch[s + i],
            None => 0.0,
        };
        let lif = quantize_lif(
            bufs.scratch[tau_r.0 + i],
            bufs.scratch[res_r.0 + i],
            bufs.scratch[leak_r.0 + i],
            bufs.scratch[thr_r.0 + i],
            v_reset_val,
            v_reset.is_none(),
            opts,
        )?;
        if lif.tau_err_s > 0.0 {
            report.note(NirNote::TauTruncated);
        }
        if lif.max_v_err_v > 0.0 {
            report.note(NirNote::PotentialTruncated);
        }
        if lif.capacitance_clamped {
            report.note(NirNote::CapacitanceClamped);
        }
        bufs.lifs[start + i] = lif;
    }
    *lif_fill += n;
    Ok(NirLifPopulation {
        offset: start,
        len: n,
    })
}

// 106 lines once rustfmt reflows the call sites (2026-09-02); the body
// did not grow, the line count did
#[allow(clippy::too_many_arguments, clippy::too_many_lines)]
fn import_node<'a>(
    r: &mut Reader<'a>,
    name: &'a str,
    opts: NirImportOptions,
    bufs: &mut NirBuffers<'_, 'a>,
    node_count: &mut usize,
    weight_fill: &mut usize,
    lif_fill: &mut usize,
    report: &mut NirReport,
) -> Result<(), NirError<'a>> {
    let idx = *node_count;
    bufs.nodes[idx] = NirNode {
        name,
        kind: NirNodeKind::Input,
        shape: [0; 4],
        shape_len: 0,
        lif: None,
        linear: None,
    };

    let mut kind: Option<&str> = None;
    let mut shape = [0u32; 4];
    let mut shape_len = 0usize;
    // LIF params stage contiguously into scratch (per-node `fill`,
    // freed after quantization); ranges are (start, len) pairs.
    let mut staged = 0usize;
    let mut tau: Option<(usize, usize)> = None;
    let mut res: Option<(usize, usize)> = None;
    let mut v_leak: Option<(usize, usize)> = None;
    let mut v_threshold: Option<(usize, usize)> = None;
    let mut v_reset: Option<(usize, usize)> = None;

    let mut first = true;
    while let Some(key) = r.object_step(&mut first)? {
        match key {
            "type" => kind = Some(r.read_string()?),
            "shape" => {
                let mut sfirst = true;
                while r.array_step(&mut sfirst)? {
                    if shape_len >= 4 {
                        return Err(NirError::BadShape("shape"));
                    }
                    let d = r.read_number()?;
                    if !(0.0..=f64::from(u32::MAX)).contains(&d) {
                        return Err(NirError::BadShape("shape"));
                    }
                    shape[shape_len] = d as u32;
                    shape_len += 1;
                }
            }
            "tau" => tau = Some(read_param_array(r, bufs.scratch, &mut staged, "tau")?),
            "r" => res = Some(read_param_array(r, bufs.scratch, &mut staged, "r")?),
            "v_leak" => v_leak = Some(read_param_array(r, bufs.scratch, &mut staged, "v_leak")?),
            "v_threshold" => {
                v_threshold = Some(read_param_array(
                    r,
                    bufs.scratch,
                    &mut staged,
                    "v_threshold",
                )?);
            }
            "v_reset" => v_reset = Some(read_param_array(r, bufs.scratch, &mut staged, "v_reset")?),
            "weight" => {
                let lin = import_weight(r, bufs, *weight_fill)?;
                if lin.max_abs_err > 0.0 {
                    report.note(NirNote::QuantizationLoss);
                }
                if lin.zero_tensor {
                    report.note(NirNote::ZeroWeightTensor);
                }
                bufs.nodes[idx].linear = Some(lin);
                *weight_fill += lin.rows * lin.cols;
            }
            _ => r.skip_value(0)?, // metadata + unknown fields tolerated
        }
    }

    let kind = kind.ok_or(NirError::MissingField("type"))?;
    bufs.nodes[idx].shape = shape;
    bufs.nodes[idx].shape_len = shape_len;
    bufs.nodes[idx].kind = match kind {
        "Input" => {
            if shape_len == 0 {
                return Err(NirError::MissingField("shape"));
            }
            report.inputs += 1;
            NirNodeKind::Input
        }
        "Output" => {
            if shape_len == 0 {
                return Err(NirError::MissingField("shape"));
            }
            report.outputs += 1;
            NirNodeKind::Output
        }
        "Linear" => {
            if bufs.nodes[idx].linear.is_none() {
                return Err(NirError::MissingField("weight"));
            }
            report.linears += 1;
            NirNodeKind::Linear
        }
        "LIF" => {
            report.lifs += 1;
            bufs.nodes[idx].lif = Some(finish_lif_population(
                tau,
                res,
                v_leak,
                v_threshold,
                v_reset,
                bufs,
                opts,
                report,
                lif_fill,
            )?);
            NirNodeKind::Lif
        }
        other => return Err(NirError::UnsupportedNodeKind(other)),
    };
    *node_count += 1;
    Ok(())
}

/// Read a 2-D weight array and quantize it into the arena at
/// `offset`: parse + stage the source f64s into `bufs.scratch`
/// (row-major `weight[out][in]`: `y_o = Σ_i w[o][i] · x_i`), then
/// delegate to [`quantize_linear`] — the shared quantization
/// contract.
fn import_weight(
    r: &mut Reader<'_>,
    bufs: &mut NirBuffers<'_, '_>,
    offset: usize,
) -> Result<NirLinear, NirError<'static>> {
    let mut rows = 0usize;
    let mut cols: Option<usize> = None;
    let mut staged = 0usize; // f64 slots written to scratch

    let mut rfirst = true;
    while r.array_step(&mut rfirst)? {
        if r.peek() != Some(b'[') {
            return Err(NirError::BadShape("weight"));
        }
        // one pass per row: read, stage, count — raggedness compares
        // after the row (buffer contents are unspecified on error)
        let mut cfirst = true;
        let mut rc = 0usize;
        while r.array_step(&mut cfirst)? {
            if r.peek() == Some(b'[') {
                return Err(NirError::BadShape("weight")); // 3-D
            }
            let v = r.read_number()?;
            if staged >= bufs.scratch.len() {
                return Err(NirError::BufferOverflow);
            }
            bufs.scratch[staged] = v;
            staged += 1;
            rc += 1;
        }
        if rc == 0 {
            return Err(NirError::BadShape("weight")); // empty row
        }
        match cols {
            None => cols = Some(rc),
            Some(c) if c != rc => return Err(NirError::BadShape("weight")), // ragged
            Some(_) => {}
        }
        rows += 1;
    }
    let cols = cols.ok_or(NirError::BadShape("weight"))?; // `[]` outer
    quantize_linear(&bufs.scratch[..staged], rows, cols, bufs.weights, offset)
}

// ---------------------------------------------------------------------------
// Export — canonical bytes, derived values rendered, provenance in metadata
// ---------------------------------------------------------------------------

struct ByteWriter<'a> {
    out: &'a mut [u8],
    len: usize,
}

impl ByteWriter<'_> {
    fn push(&mut self, b: u8) -> Result<(), NirError<'static>> {
        if self.len >= self.out.len() {
            return Err(NirError::ExportTooSmall);
        }
        self.out[self.len] = b;
        self.len += 1;
        Ok(())
    }
    fn push_str(&mut self, s: &str) -> Result<(), NirError<'static>> {
        for &b in s.as_bytes() {
            self.push(b)?;
        }
        Ok(())
    }
}

impl core::fmt::Write for ByteWriter<'_> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        self.push_str(s).map_err(|_| core::fmt::Error)
    }
}

/// Render one f64 canonically: Rust's `Display` (shortest round-trip
/// — deterministic across platforms and releases) — parses back
/// bit-exactly with the same correctly-rounded reader.
fn write_f64(w: &mut ByteWriter<'_>, v: f64) -> Result<(), NirError<'static>> {
    if !v.is_finite() {
        return Err(NirError::BadNumber("export value"));
    }
    write!(w, "{v}").map_err(|_| NirError::ExportTooSmall)
}

fn write_u32(w: &mut ByteWriter<'_>, v: u32) -> Result<(), NirError<'static>> {
    write!(w, "{v}").map_err(|_| NirError::ExportTooSmall)
}

/// The JSON container's string subset: printable ASCII, no escape
/// char, no quote — exactly what [`Reader::read_string`] reads
/// back. The structured-entry builder accepts any `&str`; this
/// rule gates only the JSON boundary (export).
fn name_is_writable(s: &str) -> bool {
    s.bytes()
        .all(|c| c != b'"' && c != b'\\' && (0x20..=0x7e).contains(&c))
}

/// Export an imported graph: the SAME dict schema, canonical bytes —
/// fixed key order, derived (substrate-exact) values in the schema
/// fields, provenance + quant records in `metadata.neuralos`.
/// Returns the byte length written. Importing the export must
/// reproduce the import bit-for-bit (the idempotence gate).
///
/// # Errors
///
/// [`NirError::ExportTooSmall`] when `out` cannot hold the document;
/// [`NirError::NonAsciiNodeName`] when a node name cannot be written
/// escape-free (the builder accepts any `&str`; the container
/// cannot).
pub fn nir_export<'a>(
    nodes: &[NirNode<'a>],
    edges: &[(u32, u32)],
    weights: &[i16],
    lifs: &[NirLif],
    opts: NirImportOptions,
    out: &mut [u8],
) -> Result<usize, NirError<'a>> {
    for node in nodes {
        if !name_is_writable(node.name) {
            return Err(NirError::NonAsciiNodeName(node.name));
        }
    }
    let mut w = ByteWriter { out, len: 0 };
    w.push_str("{\"version\":\"")?;
    w.push_str(EXPORT_VERSION)?;
    w.push_str("\",\"node\":{\"type\":\"NIRGraph\",\"edges\":[")?;
    for (i, (a, b)) in edges.iter().enumerate() {
        if i > 0 {
            w.push(b',')?;
        }
        // edge indices must name nodes — "?" placeholders would be a
        // silent lie in an export document
        let an = nodes
            .get(*a as usize)
            .ok_or(NirError::BadShape("edges"))?
            .name;
        let bn = nodes
            .get(*b as usize)
            .ok_or(NirError::BadShape("edges"))?
            .name;
        w.push_str("[\"")?;
        w.push_str(an)?;
        w.push_str("\",\"")?;
        w.push_str(bn)?;
        w.push_str("\"]")?;
    }
    w.push_str("],\"nodes\":{")?;
    for (i, node) in nodes.iter().enumerate() {
        if i > 0 {
            w.push(b',')?;
        }
        w.push_str("\"")?;
        w.push_str(node.name)?;
        w.push_str("\":")?;
        export_node(&mut w, node, weights, lifs, opts)?;
    }
    w.push_str("}}}")?;
    Ok(w.len)
}

// the per-neuron array rendering (schema fields + metadata) crosses
// the 100-line cap with the population loops alone (nir_import has
// the precedent allow for its EOF checks)
#[allow(clippy::too_many_lines)]
fn export_node(
    w: &mut ByteWriter<'_>,
    node: &NirNode<'_>,
    weights: &[i16],
    lifs: &[NirLif],
    opts: NirImportOptions,
) -> Result<(), NirError<'static>> {
    let scale = opts.resolution.scale();
    let to_v = |q: i16| f64::from(q) / (1000.0 * f64::from(scale));
    match node.kind {
        NirNodeKind::Input | NirNodeKind::Output => {
            let t = if node.kind == NirNodeKind::Input {
                "Input"
            } else {
                "Output"
            };
            w.push_str("{\"type\":\"")?;
            w.push_str(t)?;
            w.push_str("\",\"shape\":[")?;
            for d in 0..node.shape_len {
                if d > 0 {
                    w.push(b',')?;
                }
                write_u32(w, node.shape[d])?;
            }
            w.push_str("]}")?;
        }
        NirNodeKind::Lif => {
            let pop = node.lif.ok_or(NirError::MissingField("lif"))?;
            let rec = |i: usize| {
                lifs.get(pop.offset + i)
                    .ok_or(NirError::MissingField("lif"))
            };
            // derived, rendered back in source units EXACTLY as the
            // substrate holds them (tau_us/1e6, r_mohm*1e6, q/1000):
            // re-import reproduces the identical quantized
            // population. Provenance + quant records ride metadata
            // as per-neuron arrays.
            w.push_str("{\"type\":\"LIF\",\"tau\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, f64::from(rec(i)?.tau_us) / 1.0e6)?;
            }
            w.push_str("],\"r\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, f64::from(rec(i)?.resistance_mohm) * 1.0e6)?;
            }
            w.push_str("],\"v_leak\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, to_v(rec(i)?.leak_q))?;
            }
            w.push_str("],\"v_threshold\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, to_v(rec(i)?.threshold_q))?;
            }
            w.push_str("],\"v_reset\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, to_v(rec(i)?.reset_q))?;
            }
            w.push_str(
                "],\"metadata\":{\"neuralos\":{\"provenance\":{\
\"tau_s\":[",
            )?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, rec(i)?.tau_s)?;
            }
            w.push_str("],\"r_ohm\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, rec(i)?.r_ohm)?;
            }
            w.push_str("],\"v_leak_v\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, rec(i)?.v_leak_v)?;
            }
            w.push_str("],\"v_threshold_v\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, rec(i)?.v_threshold_v)?;
            }
            w.push_str("],\"v_reset_v\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, rec(i)?.v_reset_v)?;
            }
            w.push_str("],\"v_reset_defaulted\":")?;
            w.push_str(if rec(0)?.v_reset_defaulted {
                "true"
            } else {
                "false"
            })?;
            w.push_str("},\"quant\":{\"grid\":\"")?;
            w.push_str(match opts.resolution {
                VoltageResolution::Millivolt => "mV",
                VoltageResolution::CentiMillivolt => "cV",
            })?;
            w.push_str("\",\"dt_us\":")?;
            write_u32(w, opts.dt_us)?;
            w.push_str(",\"tau_err_s\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, rec(i)?.tau_err_s)?;
            }
            w.push_str("],\"max_v_err_v\":[")?;
            for i in 0..pop.len {
                if i > 0 {
                    w.push(b',')?;
                }
                write_f64(w, rec(i)?.max_v_err_v)?;
            }
            w.push_str("]}}}}")?; // quant, neuralos, metadata, node
        }
        NirNodeKind::Linear => {
            let lin = node.linear.ok_or(NirError::MissingField("linear"))?;
            // dequantized weights: w' = q·scale — plain NIR a real
            // snnTorch/NIR stack can load; provenance rides metadata.
            w.push_str("{\"type\":\"Linear\",\"weight\":[")?;
            for row in 0..lin.rows {
                if row > 0 {
                    w.push(b',')?;
                }
                w.push(b'[')?;
                for col in 0..lin.cols {
                    if col > 0 {
                        w.push(b',')?;
                    }
                    let q = weights[lin.weight_offset + row * lin.cols + col];
                    write_f64(w, f64::from(q) * lin.scale)?;
                }
                w.push(b']')?;
            }
            w.push_str(
                "],\"metadata\":{\"neuralos\":{\"provenance\":{\
\"absmax\":",
            )?;
            write_f64(w, lin.absmax)?;
            w.push_str("},\"quant\":{\"scale\":")?;
            write_f64(w, lin.scale)?;
            w.push_str(",\"max_abs_err\":")?;
            write_f64(w, lin.max_abs_err)?;
            w.push_str(",\"zero_tensor\":")?;
            w.push_str(if lin.zero_tensor { "true" } else { "false" })?;
            w.push_str(",\"source\":\"")?;
            w.push_str(NIR_REF_SHA)?;
            w.push_str("\"}}}}")?; // quant, neuralos, metadata, node
        }
    }
    Ok(())
}

#[cfg(feature = "std")]
mod std_assembly {
    //! The network-assembly surface: the canonical chain
    //! `Input → Linear → LIF → Output` ([`NirImport::build_chain_network`],
    //! slice 1) and the GENERAL four-kind graph
    //! ([`NirImport::build_network`], slice 2): any reference-emitted
    //! Input/Linear/LIF/Output graph assembles and fires.
    //!
    //! # The general assembly contract
    //!
    //! - **Encoder stages (the Linear half):** the Linear sub-DAG is
    //!   evaluated symbolically at setup — for every drive Linear `L`
    //!   and root Input `r`, the composed matrix `W_L·G` (product of
    //!   the chain's matrices in f64, D2 fusion) is quantized ONCE via
    //!   [`quantize_linear`](super::quantize_linear). At step time the
    //!   encoder applies the substrate's `/100` gain per stage and
    //!   merges saturating-i16 into the global per-step current vector
    //!   (D5's mechanical merge; multiple Inputs read their own slices,
    //!   one stage matrix per (drive Linear, root) pair).
    //! - **Edges:** LIF→LIF wires `add_synapse(pre_i, post_i,
    //!   EDGE_PULSE_QUANTA)` per neuron pair (identity element
    //!   mapping — the reference defines no element semantics at this
    //!   sha); Input→Linear is the encoder entry; LIF→Output is
    //!   shape-checked decoration.
    //! - **Plasticity is FROZEN at assembly** (`NIR has no plasticity
    //!   term` — STDP would clamp ±32767 and corrupt the edge quanta);
    //!   recorded in the report.
    //! - **Grid:** any graph with a LIF→LIF edge assembles only on
    //!   [`VoltageResolution::CentiMillivolt`] options — a 20 μA edge
    //!   pulse is dead 10× over on mV. Explicit mV options on such a
    //!   graph reject BY NAME with the re-import remedy (never a
    //!   silent override).
    //! - Every rejection is NAMED (see the fixture table in the
    //!   tests): structure (empty / no Input / no Output / no LIF),
    //!   edge kinds (pass-through, direct drive, readout, self-loop,
    //!   Output-as-source), per-edge shapes
    //!   ([`NirError::EdgeShapeMismatch`], reference `check_types`
    //!   parity), Linear-only cycles, and the u16 neuron-id bound
    //!   ([`NirError::BufferOverflow`], D7).

    use std::collections::{BTreeMap, BTreeSet};

    use super::{
        NirError, NirImportOptions, NirLif, NirLifParams, NirLifPopulation, NirLinear, NirNode,
        NirNodeKind, NIR_REF_SHA,
    };
    use crate::lif_neuron::{LIFNeuron, NeuronType, VoltageResolution};
    use crate::network::SpikingNeuralNetwork;

    /// **THE D1 edge-semantics contract (principal-ratified 2026-08-22).**
    ///
    /// A spiking-stage edge (LIF→LIF) transmits ONE spike as ONE current
    /// pulse of this many μA, divided by the network's
    /// `synaptic_input_divisor` (default 10 → a 20 μA pulse), accumulated
    /// saturating-i16 across fan-in, integrated exactly one step later
    /// (the pinned session-F step order).
    ///
    /// Derivation at the reference-typical point (dt = 1 ms, τ = 20 ms →
    /// `dt_over_tau = 50`, R = 100 MΩ), on the integration wire
    /// `ΔV = trunc(dt_over_tau · trunc(I·R·s/1000) / 1000)`:
    ///
    /// | axis | mV grid (s=1) | centi-mV grid (s=100) |
    /// |---|---|---|
    /// | dead zone at rest | trunc(I/200) = 0 ⇒ **200 μA** | trunc(I/2) = 0 ⇒ **2 μA** |
    /// | one 20 μA pulse | trunc(50·2/1000) = **0 quanta — dead** | trunc(50·200/1000) = **+10 quanta** |
    /// | sustained `V_ss` lift/spiking neighbor | +2.0 mV (coupling, not ignition) | +200 quanta |
    /// | ≥ 1 quantum/pulse headroom | — | holds to τ = 200 ms |
    ///
    /// Saturation: 1638 coincident pulses saturate the i16 accumulator;
    /// plasticity is FROZEN at NIR assembly (NIR has no plasticity
    /// term — STDP's ±32767 clamp would corrupt the quanta on first
    /// touch), so the constant never rides a learning weight.
    ///
    /// Consequence (named, never silent): any graph with a LIF→LIF
    /// edge assembles only under `CentiMillivolt` import options — a
    /// single 20 μA pulse is dead 10× over on the mV grid. Explicit mV
    /// options on such a graph are rejected BY NAME with the remedy.
    pub const EDGE_PULSE_QUANTA: i16 = 200;

    #[cfg(test)]
    mod edge_contract_tests {
        use super::EDGE_PULSE_QUANTA;
        use crate::lif_neuron::{LIFNeuron, NeuronType, VoltageResolution};
        use crate::network::SpikingNeuralNetwork;

        /// An imported-style substrate neuron (the assembly's param
        /// mapping): τ=20 ms, R=100 MΩ, −70/−55/−80 V, deterministic.
        fn substrate_neuron(id: u16, res: VoltageResolution) -> LIFNeuron {
            let s = res.scale() as i16;
            let mut n = LIFNeuron::new_with_type_resolution(id, NeuronType::Excitatory, res);
            n.resting_potential = -70 * s;
            n.membrane_potential = -70 * s;
            n.threshold = -55 * s;
            n.reset_potential = -80 * s;
            n.tau_membrane_us = 20_000;
            n.tau_refractory_us = 1_000;
            n.resistance_mohm = 100;
            n.capacitance_pf = 200;
            n.noise_amplitude_ua = 0;
            n
        }

        /// **THE D1 exact-value pin, both grids in one test** (network
        /// `transmission_is_live_*` style). Pre driven over threshold on
        /// step 0 (3000 μA ⇒ +1500 centi / +15 mV — exactly threshold);
        /// the `EDGE_PULSE_QUANTA` synapse (200 / default divisor 10 =
        /// a 20 μA pulse) moves the post exactly one step later:
        /// centi +10 quanta (`dt_over_tau`=50: 50·200/1000), mV exactly
        /// ZERO (50·2/1000 truncates) — the dead-zone math that makes
        /// recurrent graphs a centi-only, named-rejection surface.
        #[test]
        fn edge_pulse_moves_post_exact_one_step_later_both_grids() {
            for (label, res, unmoved, moved) in [
                ("centi", VoltageResolution::CentiMillivolt, -7_000, -6_990),
                ("mV", VoltageResolution::Millivolt, -70, -70),
            ] {
                let mut net = SpikingNeuralNetwork::from_neurons(
                    vec![substrate_neuron(0, res), substrate_neuron(1, res)],
                    1_000,
                )
                .expect("two neurons");
                net.set_plasticity_enabled(false); // the assembly convention
                net.add_synapse(0, 1, EDGE_PULSE_QUANTA).expect("edge");
                net.finalize_synapses();

                let spikes = net.step(&[3000, 0]).expect("step 0");
                assert_eq!(spikes.len(), 1, "{label}: pre fires on step 0");
                assert_eq!(
                    net.neurons()[1].membrane_potential,
                    unmoved,
                    "{label}: post unmoved on the spike step — one-step delay"
                );

                net.step(&[0, 0]).expect("step 1");
                assert_eq!(
                    net.neurons()[1].membrane_potential,
                    moved,
                    "{label}: post integrates the 20 uA pulse one step later \
                     (centi: +10 quanta; mV: dead — the named-rejection math)"
                );
            }
        }

        /// The constant itself is part of the public contract — pin it.
        #[test]
        fn edge_pulse_quanta_is_two_hundred() {
            assert_eq!(EDGE_PULSE_QUANTA, 200);
        }
    }

    #[cfg(test)]
    mod assembly_tests {
        use super::super::quantize_linear;
        use super::{
            NirBuilder, NirError, NirImport, NirImportOptions, NirLifParams, EDGE_PULSE_QUANTA,
        };
        use crate::lif_neuron::VoltageResolution;

        const BRANCH: &[u8] = include_bytes!("../tests/nir_fixtures/branch.json");
        const MERGE: &[u8] = include_bytes!("../tests/nir_fixtures/merge.json");
        const RECURRENT: &[u8] = include_bytes!("../tests/nir_fixtures/recurrent.json");
        const CHAIN: &[u8] = include_bytes!("../tests/nir_fixtures/chain.json");

        fn centi() -> NirImportOptions {
            NirImportOptions::new(1_000, VoltageResolution::CentiMillivolt)
        }

        /// The frozen chain fixtures re-run UNCHANGED through both
        /// builders: `build_chain_network` (frozen pins) and
        /// `build_network` must produce the same neurons, the same
        /// encoder output, and the same 100-step spike raster — the
        /// general path subsumes the chain, proven bit-exact.
        #[test]
        fn chain_equivalence_both_builders_bit_exact() {
            let opts = NirImportOptions::default();
            let g = NirImport::from_json(CHAIN, opts).expect("chain imports");
            let (mut net1, enc1) = g.build_chain_network().expect("chain builder");
            let (mut net2, enc2, rep2) = g.build_network().expect("general builder");

            assert_eq!(rep2.neurons, 1);
            assert_eq!(rep2.synapses, 0, "the chain has no LIF->LIF edge");
            assert_eq!(rep2.stages, 1);
            assert!(rep2.fused.is_empty());
            assert!(!rep2.multi_linear_gain, "one drive Linear");
            assert!(rep2.undriven.is_empty());
            assert!(rep2.plasticity_frozen);

            // neurons bit-exact (Debug string = every field)
            let n1: Vec<String> = net1.neurons().iter().map(|n| format!("{n:?}")).collect();
            let n2: Vec<String> = net2.neurons().iter().map(|n| format!("{n:?}")).collect();
            assert_eq!(n1, n2);

            // encoder output identical on probe drives
            for x in [[4, 0, 0], [100, -50, 25], [0, 0, 0], [-32768, 32767, 1]] {
                assert_eq!(enc1.encode(&x), enc2.encode(&[&x]), "x={x:?}");
            }

            // same drive, same raster over 100 steps
            let mut r1 = Vec::new();
            let mut r2 = Vec::new();
            for _ in 0..100 {
                r1.extend(
                    net1.step(&enc1.encode(&[4, 0, 0]))
                        .unwrap()
                        .iter()
                        .map(|s| s.neuron_id),
                );
                r2.extend(
                    net2.step(&enc2.encode(&[&[4, 0, 0]]))
                        .unwrap()
                        .iter()
                        .map(|s| s.neuron_id),
                );
            }
            assert_eq!(r1, r2);
            assert!(!r1.is_empty(), "the frozen chain fires (9/100 pins)");
        }

        /// The branch fixture: report shape (fusion record for
        /// l1·l2, two stages, D6 note) + every population fires.
        #[test]
        fn branch_assembles_fuses_and_fires() {
            let g = NirImport::from_json(BRANCH, NirImportOptions::default()).expect("imports");
            let (mut net, enc, rep) = g.build_network().expect("assembles");
            assert_eq!(rep.neurons, 4);
            assert_eq!(rep.synapses, 0);
            assert_eq!(rep.inputs, 1);
            assert_eq!(rep.drive_linears, 2, "l2 (fused) and l3");
            assert_eq!(rep.stages, 2);
            assert_eq!(rep.fused.len(), 1, "the l1->l2 chain fused once");
            assert_eq!(rep.fused[0].chain, vec!["l1", "l2"]);
            assert!((rep.fused[0].scales[0] - 1.0 / 32_767.0).abs() < 1e-18);
            assert!(
                (rep.fused[0].scales[1] - 1.0 / 32_767.0).abs() < 1e-18,
                "l2 absmax is 1.0"
            );
            assert!(rep.multi_linear_gain, "D6: >1 drive Linear, loud");
            assert!(rep.undriven.is_empty());

            // fused-stage exactness: the quantized product must equal
            // quantize_linear(W2_dequant · W1_dequant) through the same
            // seam — the f64 reference IS the fixture's source tensors
            let w1 = [[0.5f64, -1.0, 0.25], [-0.25, 1.0, 0.5]];
            let w2 = [[1.0f64, 0.0], [0.0, -0.5]];
            let mut prod = [[0f64; 3]; 2];
            for r in 0..2 {
                for k in 0..3 {
                    prod[r][k] = w2[r][0] * w1[0][k] + w2[r][1] * w1[1][k];
                }
            }
            let flat: Vec<f64> = prod.iter().flat_map(|r| r.iter().copied()).collect();
            let mut expect = vec![0i16; 6];
            quantize_linear(&flat, 2, 3, &mut expect, 0).expect("reference quantizes");
            assert!(
                enc.mats.iter().any(|m| m.q == expect),
                "the fused stage is the ONCE-quantized product {:?} (mats: {:?})",
                expect,
                enc.mats.iter().map(|m| &m.q).collect::<Vec<_>>()
            );

            // every population fires under a seeded drive
            let mut fired = [false; 4];
            for _ in 0..100 {
                for s in net.step(&enc.encode(&[&[6, 0, 0]])).unwrap() {
                    fired[s.neuron_id as usize] = true;
                }
            }
            assert!(fired.iter().all(|&f| f), "all four neurons fire: {fired:?}");
        }

        /// Mirror of the assembly gate's leg-1 pin: the branch
        /// fixture's first-spike steps per neuron under x=[6,0,0] on
        /// mV — hand-verified integer dynamics (n0/n3: ct=98, `V_ss`
        /// +28, fires at g≤83, step 3; n1: 1-quanta/step crawl 49→34,
        /// step 14; n2: g/10 decay, −48≥−50, step 1).
        #[test]
        fn branch_first_spike_pins_match_the_gate() {
            let g = NirImport::from_json(BRANCH, NirImportOptions::default()).unwrap();
            let (mut net, enc, _) = g.build_network().unwrap();
            let mut firsts = vec![usize::MAX; 4];
            for t in 0..100 {
                for s in net.step(&enc.encode(&[&[6, 0, 0]])).unwrap() {
                    firsts[s.neuron_id as usize] = firsts[s.neuron_id as usize].min(t);
                }
            }
            assert_eq!(firsts, vec![3, 14, 1, 3]);
        }

        /// The merge fixture at graph scale: one branch stalls below
        /// the climb (81 μA → `V_ss−rest` 810 < the 1500 gap), the
        /// summed fan-in fires (162 μA) — the `transmission_pulses_sum`
        /// property, encoder edition. First-spike step pinned exactly
        /// (integer math, noise 0).
        #[test]
        fn merge_summed_fan_in_fires_where_single_stalls() {
            let g = NirImport::from_json(MERGE, centi()).expect("imports");
            let (mut net, enc, rep) = g.build_network().expect("assembles");
            assert_eq!(rep.neurons, 2);
            assert_eq!(rep.drive_linears, 2);
            assert!(rep.multi_linear_gain);

            // single branch: neuron 0 never fires over 200 steps (the
            // stall), while the always-hot control neuron 1 does (the
            // encoder is live — the stall is the current, not a dead wire)
            let mut counts = [0usize; 2];
            for _ in 0..200 {
                for s in net.step(&enc.encode(&[&[1, 1], &[]])).unwrap() {
                    counts[s.neuron_id as usize] += 1;
                }
            }
            assert_eq!(counts[0], 0, "81 uA stalls below the climb");
            assert!(counts[1] > 0, "the 327 uA control row fires");

            // fresh net, both branches: first spike on neuron 0 at
            // 0-based step 52 (g shrinks 1620 -> 116 by g/20 per step)
            let g2 = NirImport::from_json(MERGE, centi()).unwrap();
            let (mut net2, enc2, _) = g2.build_network().unwrap();
            let mut first = usize::MAX;
            let mut n0 = 0usize;
            for t in 0..100 {
                for s in net2.step(&enc2.encode(&[&[1, 0], &[1, 0]])).unwrap() {
                    if s.neuron_id == 0 {
                        first = first.min(t);
                        n0 += 1;
                    }
                }
            }
            assert_eq!(first, 52, "summed 162 uA crosses at step 52 exactly");
            assert_eq!(n0, 1, "one spike in the 100-step window (next ~112)");
        }

        /// The recurrent fixture at graph scale — D1's assert: a
        /// postsynaptic neuron moves EXACTLY +10 centi-quanta the
        /// step after its presynaptic spike, and the edge quanta ride
        /// no learning weight (plasticity frozen).
        #[test]
        fn recurrent_pulse_moves_postsynaptic_exactly() {
            let g = NirImport::from_json(RECURRENT, centi()).expect("imports");
            let (mut net, enc, rep) = g.build_network().expect("assembles");
            assert_eq!(rep.neurons, 4);
            assert_eq!(rep.synapses, 4, "a->b and b->a, 2 neurons each");
            assert_eq!(rep.stages, 1, "linear drives lif_a only");
            assert!(!rep.multi_linear_gain);
            assert!(rep.undriven.is_empty());

            // ids: lif_a = 0,1; lif_b = 2,3. Drive x=[1,0]: a0 at 327
            // uA fires; a1 undriven; b* has no encoder path.
            let mut a0_first = usize::MAX;
            for t in 0..80 {
                let fired_a0 = net
                    .step(&enc.encode(&[&[1, 0]]))
                    .unwrap()
                    .iter()
                    .any(|s| s.neuron_id == 0);
                if fired_a0 && a0_first == usize::MAX {
                    a0_first = t;
                    assert_eq!(
                        net.neurons()[2].membrane_potential,
                        -7_000,
                        "b0 unmoved on the spike step — one-step delay"
                    );
                }
                if a0_first != usize::MAX && t == a0_first + 1 {
                    assert_eq!(
                        net.neurons()[2].membrane_potential,
                        -6_990,
                        "b0 integrates the 20 uA pulse: exactly +10 quanta"
                    );
                }
            }
            assert_ne!(a0_first, usize::MAX, "a0 must fire in 80 steps");
            assert_eq!(
                net.neurons()[3].membrane_potential,
                -7_000,
                "b1 unmoved (a1 never fires — identity element mapping)"
            );
            // frozen plasticity: the edge quanta never moved
            assert!(net.synapses().iter().all(|s| s.weight == EDGE_PULSE_QUANTA));
        }

        /// The loud-rejections table: every named error, one
        /// reference-emitted negative fixture each (+ the mV-on-
        /// recurrent ruling, opts-driven, same fixture file).
        #[test]
        // 107 lines once rustfmt reflows the table rows (2026-09-02)
        #[allow(clippy::type_complexity, clippy::too_many_lines)]
        fn named_rejections_reference_fixtures() {
            let cases: Vec<(&str, &[u8], fn(&NirError<'_>) -> bool)> = vec![
                (
                    "pass-through",
                    include_bytes!("../tests/nir_fixtures/neg_asm_passthrough.json"),
                    |e| {
                        matches!(
                            e,
                            NirError::UnsupportedTopology("Input->Output pass-through")
                        )
                    },
                ),
                (
                    "direct drive",
                    include_bytes!("../tests/nir_fixtures/neg_asm_direct_drive.json"),
                    |e| {
                        matches!(
                        e,
                        NirError::UnsupportedTopology(
                            "direct drive (Input->LIF) deferred — drive convention not yet named"
                        )
                    )
                    },
                ),
                (
                    "no LIF (encoder-only)",
                    include_bytes!("../tests/nir_fixtures/neg_asm_no_lif.json"),
                    |e| {
                        matches!(
                        e,
                        NirError::UnsupportedTopology(
                            "graph without LIF: nothing to fire — encoder-only assembly deferred"
                        )
                    )
                    },
                ),
                (
                    "readout edge",
                    include_bytes!("../tests/nir_fixtures/neg_asm_lif_to_linear.json"),
                    |e| {
                        matches!(
                        e,
                        NirError::UnsupportedTopology(
                            "readout (LIF->Linear) deferred — spike-count readout convention not yet named"
                        )
                    )
                    },
                ),
                (
                    "self-loop",
                    include_bytes!("../tests/nir_fixtures/neg_asm_self_loop.json"),
                    |e| {
                        matches!(
                            e,
                            NirError::UnsupportedTopology(
                                "LIF self-loop — the substrate forbids self-synapse"
                            )
                        )
                    },
                ),
                (
                    "empty graph",
                    include_bytes!("../tests/nir_fixtures/neg_asm_empty.json"),
                    |e| matches!(e, NirError::UnsupportedTopology("empty graph")),
                ),
                (
                    "shape mismatch",
                    include_bytes!("../tests/nir_fixtures/neg_asm_shape_mismatch.json"),
                    |e| {
                        matches!(
                            e,
                            NirError::EdgeShapeMismatch {
                                src: "input",
                                dst: "l1"
                            }
                        )
                    },
                ),
                (
                    "cycle without Output",
                    include_bytes!("../tests/nir_fixtures/neg_asm_cycle_no_output.json"),
                    |e| matches!(e, NirError::UnsupportedTopology("no Output node")),
                ),
                (
                    "no Input",
                    include_bytes!("../tests/nir_fixtures/neg_asm_no_input.json"),
                    |e| matches!(e, NirError::UnsupportedTopology("no Input node")),
                ),
                ("recurrent on mV (opts-driven)", RECURRENT, |e| {
                    matches!(e, NirError::UnsupportedTopology(m) if m.contains(
                        "CentiMillivolt"
                    ) && m.contains("~200 uA dead zone"))
                }),
            ];
            for (label, doc, check) in cases {
                let opts = if label.starts_with("recurrent") {
                    NirImportOptions::default() // mV — the rejected request
                } else {
                    centi()
                };
                let g = NirImport::from_json(doc, opts)
                    .unwrap_or_else(|e| panic!("{label}: fixture must IMPORT: {e}"));
                let err = g
                    .build_network()
                    .err()
                    .unwrap_or_else(|| panic!("{label}: expected a named rejection"));
                assert!(check(&err), "{label}: got {err:?}");
            }
        }

        /// Builder-only rejections (unemittable shapes): Output as an
        /// edge source, a Linear-only cycle, a dead Input, and the D7
        /// population bound.
        #[test]
        #[allow(clippy::too_many_lines)] // four independent scenarios, one table
        fn named_rejections_builder_only() {
            // Output as edge source
            let mut b = NirBuilder::new(NirImportOptions::default());
            let inp = b.add_input("input", &[1]).unwrap();
            let lin = b.add_linear("l", &[0.5], 1, 1).unwrap();
            let lif = b
                .add_lif_population(
                    "lif",
                    &NirLifParams {
                        tau_s: &[0.02],
                        r_ohm: &[1e8],
                        v_leak_v: &[-0.07],
                        v_threshold_v: &[-0.055],
                        v_reset_v: Some(&[-0.08]),
                    },
                )
                .unwrap();
            let out = b.add_output("out", &[1]).unwrap();
            let out2 = b.add_output("out2", &[1]).unwrap();
            b.add_edge(inp, lin).unwrap();
            b.add_edge(lin, lif).unwrap();
            b.add_edge(lif, out).unwrap();
            b.add_edge(out2, lif).unwrap();
            let g = b.build().unwrap();
            let err = g.build_network().unwrap_err();
            assert!(matches!(
                err,
                NirError::UnsupportedTopology("Output node as edge source")
            ));

            // Linear-only cycle: no feedforward evaluation order
            let mut b = NirBuilder::new(NirImportOptions::default());
            let inp = b.add_input("input", &[1]).unwrap();
            let lin1 = b.add_linear("l1", &[1.0], 1, 1).unwrap();
            let lin2 = b.add_linear("l2", &[1.0], 1, 1).unwrap();
            let lif = b
                .add_lif_population(
                    "lif",
                    &NirLifParams {
                        tau_s: &[0.02],
                        r_ohm: &[1e8],
                        v_leak_v: &[-0.07],
                        v_threshold_v: &[-0.055],
                        v_reset_v: Some(&[-0.08]),
                    },
                )
                .unwrap();
            let out = b.add_output("out", &[1]).unwrap();
            b.add_edge(inp, lin1).unwrap();
            b.add_edge(lin1, lin2).unwrap();
            b.add_edge(lin2, lin1).unwrap();
            b.add_edge(lin2, lif).unwrap();
            b.add_edge(lif, out).unwrap();
            let g = b.build().unwrap();
            let err = g.build_network().unwrap_err();
            assert!(matches!(
                err,
                NirError::UnsupportedTopology(
                    "cycle through Linear nodes — no feedforward evaluation order exists"
                )
            ));

            // dead Input: no edge leaves it
            let mut b = NirBuilder::new(NirImportOptions::default());
            b.add_input("input", &[1]).unwrap();
            let lif = b
                .add_lif_population(
                    "lif",
                    &NirLifParams {
                        tau_s: &[0.02],
                        r_ohm: &[1e8],
                        v_leak_v: &[-0.07],
                        v_threshold_v: &[-0.055],
                        v_reset_v: Some(&[-0.08]),
                    },
                )
                .unwrap();
            let out = b.add_output("out", &[1]).unwrap();
            b.add_edge(lif, out).unwrap();
            let g = b.build().unwrap();
            let err = g.build_network().unwrap_err();
            assert!(matches!(
                err,
                NirError::UnsupportedTopology(
                    "no edge leaves an Input node — the reference cannot start type inference"
                )
            ));

            // D7: total neurons past u16::MAX is a loud BufferOverflow
            let pop_len = 33_000usize;
            let mut b = NirBuilder::new(NirImportOptions::default());
            let inp = b.add_input("input", &[1]).unwrap();
            let la = b.add_linear("la", &vec![0.5; pop_len], pop_len, 1).unwrap();
            let lb = b.add_linear("lb", &vec![0.5; pop_len], pop_len, 1).unwrap();
            let pop = |b: &mut NirBuilder<'_>, name: &'static str| {
                b.add_lif_population(
                    name,
                    &NirLifParams {
                        tau_s: &vec![0.02; pop_len],
                        r_ohm: &vec![1e8; pop_len],
                        v_leak_v: &vec![-0.07; pop_len],
                        v_threshold_v: &vec![-0.055; pop_len],
                        v_reset_v: Some(&vec![-0.08; pop_len]),
                    },
                )
                .unwrap()
            };
            let a = pop(&mut b, "a");
            let c = pop(&mut b, "c");
            let out = b.add_output("out", &[pop_len as u32]).unwrap();
            b.add_edge(inp, la).unwrap();
            b.add_edge(inp, lb).unwrap();
            b.add_edge(la, a).unwrap();
            b.add_edge(lb, c).unwrap();
            b.add_edge(a, out).unwrap();
            let g = b.build().unwrap();
            let err = g.build_network().unwrap_err();
            assert!(
                matches!(err, NirError::BufferOverflow),
                "66k neurons must reject loudly, got {err:?}"
            );
        }

        /// The plan-gate ruling on undriven populations: an
        /// Input-unreachable LIF population assembles with a
        /// STRUCTURAL note naming the node — and pins 0 spikes over
        /// the drive window (silence documented, never silent).
        #[test]
        fn undriven_population_notes_and_stays_silent() {
            let mut b = NirBuilder::new(NirImportOptions::default());
            let inp = b.add_input("input", &[1]).unwrap();
            let lin = b.add_linear("l", &[0.5], 1, 1).unwrap();
            let lif_a = b
                .add_lif_population(
                    "lif_a",
                    &NirLifParams {
                        tau_s: &[0.02],
                        r_ohm: &[1e8],
                        v_leak_v: &[-0.07],
                        v_threshold_v: &[-0.055],
                        v_reset_v: Some(&[-0.08]),
                    },
                )
                .unwrap();
            let lif_b = b
                .add_lif_population(
                    "lif_b",
                    &NirLifParams {
                        tau_s: &[0.02],
                        r_ohm: &[1e8],
                        v_leak_v: &[-0.07],
                        v_threshold_v: &[-0.055],
                        v_reset_v: Some(&[-0.08]),
                    },
                )
                .unwrap();
            let out1 = b.add_output("out1", &[1]).unwrap();
            let out2 = b.add_output("out2", &[1]).unwrap();
            b.add_edge(inp, lin).unwrap();
            b.add_edge(lin, lif_a).unwrap();
            b.add_edge(lif_a, out1).unwrap();
            b.add_edge(lif_b, out2).unwrap();
            let g = b.build().unwrap();
            let (mut net, enc, rep) = g.build_network().expect("assembles with the note");
            assert_eq!(rep.undriven, vec!["lif_b"], "the note names the node");
            assert_eq!(rep.neurons, 2);
            let mut counts = [0usize; 2];
            for _ in 0..100 {
                for s in net.step(&enc.encode(&[&[3]])).unwrap() {
                    counts[s.neuron_id as usize] += 1;
                }
            }
            assert!(counts[0] > 0, "the driven population fires");
            assert_eq!(counts[1], 0, "the undriven population stays silent");
        }
    }

    /// An imported graph in owned buffers (std convenience over the
    /// two-pass buffer API).
    #[derive(Debug)]
    pub struct NirImport<'a> {
        /// Nodes in document order.
        pub nodes: Vec<NirNode<'a>>,
        /// Resolved node-index pairs.
        pub edges: Vec<(u32, u32)>,
        /// Quantized weights (arena layout per [`NirLinear`]).
        pub weights: Vec<i16>,
        /// Quantized LIF records, one per neuron (population views
        /// index into this).
        pub lifs: Vec<NirLif>,
        /// The options the graph was imported under.
        pub opts: NirImportOptions,
        /// Reference provenance.
        pub ref_sha: &'static str,
    }

    impl NirImport<'_> {
        /// Scan + import a JSON document into owned buffers.
        ///
        /// # Errors
        ///
        /// Every [`NirError`] the buffer import can raise.
        pub fn from_json(
            json: &[u8],
            opts: NirImportOptions,
        ) -> Result<NirImport<'_>, NirError<'_>> {
            let scan = super::nir_scan(json)?;
            let mut nodes: Vec<NirNode<'_>> = vec![
                NirNode {
                    name: "",
                    kind: NirNodeKind::Input,
                    shape: [0; 4],
                    shape_len: 0,
                    lif: None,
                    linear: None,
                };
                scan.node_count
            ];
            let mut edges = vec![(0u32, 0u32); scan.edge_count];
            // scratch contract: arena + f64 scratch each hold the
            // exact weight-cell count (the arena keeps the result)
            let n = scan.weight_cells;
            let mut weights = vec![0i16; n];
            let mut lifs = vec![NirLif::default(); scan.lif_neurons];
            let mut scratch = vec![0f64; scan.weight_cells + 5 * scan.lif_neurons];
            {
                let mut bufs = super::NirBuffers {
                    nodes: &mut nodes,
                    edges: &mut edges,
                    weights: &mut weights,
                    lifs: &mut lifs,
                    scratch: &mut scratch,
                };
                super::nir_import(json, opts, &mut bufs)?;
            }
            Ok(NirImport {
                nodes,
                edges,
                weights,
                lifs,
                opts,
                ref_sha: NIR_REF_SHA,
            })
        }

        /// Assemble the canonical chain into a substrate network:
        /// returns the net (LIF params honored) + the Linear encoder
        /// (rows = neurons, cols = input features).
        ///
        /// # Errors
        ///
        /// [`NirError::UnsupportedTopology`] for anything but
        /// `Input → Linear → LIF → Output`; [`NirError::MissingField`]
        /// if the fixture starved a node.
        #[allow(clippy::missing_panics_doc)] // no panics — flagged for the unwrap on .first()
        pub fn build_chain_network(
            &self,
        ) -> Result<(SpikingNeuralNetwork, ChainEncoder<'_>), NirError<'_>> {
            let mut input_n = None;
            let mut linear_n = None;
            let mut lif_n = None;
            let mut output_n = None;
            for (i, n) in self.nodes.iter().enumerate() {
                match n.kind {
                    NirNodeKind::Input => input_n = input_n.or(Some(i)),
                    NirNodeKind::Linear => linear_n = linear_n.or(Some(i)),
                    NirNodeKind::Lif => lif_n = lif_n.or(Some(i)),
                    NirNodeKind::Output => output_n = output_n.or(Some(i)),
                }
            }
            let inp = input_n.ok_or(NirError::UnsupportedTopology("no Input node"))?;
            let lin = linear_n.ok_or(NirError::UnsupportedTopology("no Linear node"))?;
            let lif = lif_n.ok_or(NirError::UnsupportedTopology("no LIF node"))?;
            let out = output_n.ok_or(NirError::UnsupportedTopology("no Output node"))?;
            if self.nodes.len() != 4 {
                return Err(NirError::UnsupportedTopology(
                    "exactly 4 nodes (Input-Linear-LIF-Output) in slice 1",
                ));
            }
            let chain = [
                (inp as u32, lin as u32),
                (lin as u32, lif as u32),
                (lif as u32, out as u32),
            ];
            if self.edges.as_slice() != chain {
                return Err(NirError::UnsupportedTopology(
                    "edges must be Input->Linear->LIF->Output",
                ));
            }
            let pop = self.nodes[lif].lif.ok_or(NirError::MissingField("lif"))?;
            let linear_rec = self.nodes[lin]
                .linear
                .ok_or(NirError::MissingField("linear"))?;
            if linear_rec.rows > u16::MAX as usize {
                return Err(NirError::BufferOverflow);
            }
            if self.nodes[inp].shape.first().copied().unwrap_or(0) != linear_rec.cols as u32 {
                return Err(NirError::UnsupportedTopology("Input shape != Linear cols"));
            }
            // reference type-check parity: the LIF population must
            // match the feeding Linear's rows
            if pop.len != linear_rec.rows {
                return Err(NirError::UnsupportedTopology(
                    "LIF population != Linear rows",
                ));
            }

            // one neuron per Linear row, its own quantized params
            let mut neurons = Vec::with_capacity(linear_rec.rows);
            for id in 0..linear_rec.rows {
                let p = self
                    .lifs
                    .get(pop.offset + id)
                    .ok_or(NirError::MissingField("lif"))?;
                let mut n = LIFNeuron::new_with_type_resolution(
                    id as u16,
                    NeuronType::Excitatory,
                    self.opts.resolution,
                );
                n.resting_potential = p.leak_q;
                n.membrane_potential = p.leak_q;
                n.threshold = p.threshold_q;
                n.reset_potential = p.reset_q;
                n.tau_membrane_us = p.tau_us;
                n.tau_refractory_us = 1_000; // NIR LIF has no refractory → minimum
                n.resistance_mohm = p.resistance_mohm;
                n.capacitance_pf = p.capacitance_pf;
                n.noise_amplitude_ua = 0; // import is deterministic
                neurons.push(n);
            }
            let net = SpikingNeuralNetwork::from_neurons(neurons, self.opts.dt_us)
                .map_err(|_| NirError::BufferOverflow)?;
            let encoder = ChainEncoder {
                lin: linear_rec,
                weights: &self.weights,
            };
            Ok((net, encoder))
        }

        /// Assemble ANY reference-emitted four-kind graph onto a real
        /// substrate network: every LIF population becomes neurons
        /// (its own quantized params), every LIF→LIF edge becomes an
        /// [`EDGE_PULSE_QUANTA`] synapse pair, and the Linear DAG
        /// becomes quantized encoder stages (D2 fusion at the setup
        /// seam). Plasticity is frozen; the report records everything
        /// loud (fusion, undriven structure, multi-Linear gain).
        ///
        /// The import options ARE the grid request (one channel, no
        /// overrides): a graph with any LIF→LIF edge assembles only
        /// on centi-mV options — mV options reject by name with the
        /// re-import remedy (D1: a 20 μA pulse is dead 10× over on
        /// the mV grid). Feed-forward imports (no LIF→LIF edge) keep
        /// the historical mV default — the frozen chain pins.
        ///
        /// # Errors
        ///
        /// [`NirError::UnsupportedTopology`] for every named
        /// structural/kind/grid rejection; [`NirError::EdgeShapeMismatch`]
        /// per mismatched edge; [`NirError::BufferOverflow`] past the
        /// u16 neuron-id bound (D7); every
        /// [`quantize_linear`](super::quantize_linear) hard failure at
        /// the fusion seam (e.g. a composed-absmax overflow to
        /// non-finite).
        ///
        /// # Panics
        ///
        /// Never on a graph produced by [`NirImport::from_json`] or
        /// [`NirBuilder`] (edge indices are resolved/bounds-checked
        /// there); a hand-built `NirImport` with dangling edge indices
        /// panics on the node bounds.
        pub fn build_network(
            &self,
        ) -> Result<(SpikingNeuralNetwork, NirGraphEncoder, NirAssemblyReport<'_>), NirError<'_>>
        {
            self.validate_graph()?;

            let inputs: Vec<usize> = self
                .nodes
                .iter()
                .enumerate()
                .filter(|(_, n)| n.kind == NirNodeKind::Input)
                .map(|(i, _)| i)
                .collect();
            let lif_nodes: Vec<usize> = self
                .nodes
                .iter()
                .enumerate()
                .filter(|(_, n)| n.kind == NirNodeKind::Lif)
                .map(|(i, _)| i)
                .collect();

            // neuron sheet: population order = node order; global ids
            // sequential (D7: the u16 id bound is a loud bound)
            let mut total = 0usize;
            let mut pop_base: BTreeMap<usize, usize> = BTreeMap::new();
            for &l in &lif_nodes {
                pop_base.insert(l, total);
                total += self.nodes[l].lif.ok_or(NirError::MissingField("lif"))?.len;
            }
            if total > u16::MAX as usize {
                return Err(NirError::BufferOverflow);
            }
            let mut neurons: Vec<LIFNeuron> = Vec::with_capacity(total);
            for &l in &lif_nodes {
                let pop = self.nodes[l].lif.expect("counted above");
                for i in 0..pop.len {
                    let p = self
                        .lifs
                        .get(pop.offset + i)
                        .ok_or(NirError::MissingField("lif"))?;
                    neurons.push(substrate_neuron(
                        neurons.len() as u16,
                        p,
                        self.opts.resolution,
                    ));
                }
            }
            let mut net = SpikingNeuralNetwork::from_neurons(neurons, self.opts.dt_us)
                .map_err(|_| NirError::BufferOverflow)?;

            // LIF->LIF edges -> EDGE_PULSE_QUANTA synapse pairs
            // (identity element mapping; equal sizes shape-checked)
            let mut synapses = 0usize;
            for &(a, b) in &self.edges {
                let (na, nb) = (&self.nodes[a as usize], &self.nodes[b as usize]);
                if matches!((na.kind, nb.kind), (NirNodeKind::Lif, NirNodeKind::Lif)) {
                    let pa = na.lif.expect("checked");
                    let (ba, bb) = (pop_base[&(a as usize)], pop_base[&(b as usize)]);
                    for i in 0..pa.len {
                        net.add_synapse((ba + i) as u16, (bb + i) as u16, EDGE_PULSE_QUANTA)
                            .map_err(|_| NirError::BufferOverflow)?;
                        synapses += 1;
                    }
                }
            }
            net.finalize_synapses();
            net.set_plasticity_enabled(false); // NIR has no plasticity term

            // encoder stages + fusion records (the Linear DAG, fused)
            let order = self.linear_order()?;
            let EncoderPlan {
                mats,
                stages,
                fused,
                rooted,
                drive_linears,
            } = self.build_stages(&inputs, &order, &pop_base)?;
            let undriven = self.undriven_notes(&inputs, &rooted);
            let encoder = NirGraphEncoder {
                total,
                input_feats: inputs
                    .iter()
                    .map(|&i| self.nodes[i].shape.first().copied().unwrap_or(0) as usize)
                    .collect(),
                mats,
                stages,
            };
            let report = NirAssemblyReport {
                neurons: total,
                synapses,
                inputs: inputs.len(),
                drive_linears,
                stages: encoder.mats.len(),
                fused,
                undriven,
                multi_linear_gain: drive_linears > 1,
                plasticity_frozen: true,
            };
            Ok((net, encoder, report))
        }

        /// The named rejections, in deterministic order: empty /
        /// no-Input / no-Output / dead-Input / per-edge kinds /
        /// no-LIF / per-edge shapes / the mV-on-recurrent grid
        /// rejection. (Cycles through Linear nodes reject later, in
        /// [`Self::linear_order`]; D4's cycle boundary resolves here —
        /// a cycle legal at import is tolerated exactly when the
        /// graph still has its Input-rooted path and Output leaf,
        /// which the no-Input/no-Output checks and the reference's
        /// own constructor boundary mirror.)
        // the named-rejection ladder is one flat deterministic scan;
        // the 100-line cap is crossed by the edge tables alone
        // (nir_import's precedent allow)
        #[allow(clippy::too_many_lines)]
        fn validate_graph(&self) -> Result<(), NirError<'_>> {
            let nodes = &self.nodes;
            if nodes.is_empty() {
                return Err(NirError::UnsupportedTopology("empty graph"));
            }
            if !nodes.iter().any(|n| n.kind == NirNodeKind::Input) {
                return Err(NirError::UnsupportedTopology("no Input node"));
            }
            if !nodes.iter().any(|n| n.kind == NirNodeKind::Output) {
                return Err(NirError::UnsupportedTopology("no Output node"));
            }
            if !self
                .edges
                .iter()
                .any(|&(a, _)| nodes[a as usize].kind == NirNodeKind::Input)
            {
                return Err(NirError::UnsupportedTopology(
                    "no edge leaves an Input node — the reference cannot start type inference",
                ));
            }
            for &(a, b) in &self.edges {
                let (ka, kb) = (nodes[a as usize].kind, nodes[b as usize].kind);
                let err = match (ka, kb) {
                    (NirNodeKind::Input, NirNodeKind::Output) => Some(
                        NirError::UnsupportedTopology("Input->Output pass-through"),
                    ),
                    (NirNodeKind::Input, NirNodeKind::Lif) => Some(NirError::UnsupportedTopology(
                        "direct drive (Input->LIF) deferred — drive convention not yet named",
                    )),
                    (NirNodeKind::Lif, NirNodeKind::Linear) => Some(NirError::UnsupportedTopology(
                        "readout (LIF->Linear) deferred — spike-count readout convention not yet named",
                    )),
                    (NirNodeKind::Lif, NirNodeKind::Lif) if a == b => Some(
                        NirError::UnsupportedTopology("LIF self-loop — the substrate forbids self-synapse"),
                    ),
                    (NirNodeKind::Output, _) => Some(NirError::UnsupportedTopology(
                        "Output node as edge source",
                    )),
                    _ => None,
                };
                if let Some(e) = err {
                    return Err(e);
                }
            }
            if !nodes.iter().any(|n| n.kind == NirNodeKind::Lif) {
                return Err(NirError::UnsupportedTopology(
                    "graph without LIF: nothing to fire — encoder-only assembly deferred",
                ));
            }
            for &(a, b) in &self.edges {
                let (na, nb) = (&nodes[a as usize], &nodes[b as usize]);
                let ok = match (na.kind, nb.kind) {
                    (NirNodeKind::Input, NirNodeKind::Linear) => {
                        na.shape_len == 1
                            && na.shape[0] as usize == nb.linear.expect("checked").cols
                    }
                    (NirNodeKind::Linear, NirNodeKind::Linear) => {
                        na.linear.expect("checked").rows == nb.linear.expect("checked").cols
                    }
                    (NirNodeKind::Linear, NirNodeKind::Lif) => {
                        na.linear.expect("checked").rows == nb.lif.expect("checked").len
                    }
                    (NirNodeKind::Lif, NirNodeKind::Lif) => {
                        na.lif.expect("checked").len == nb.lif.expect("checked").len
                    }
                    (NirNodeKind::Lif, NirNodeKind::Output) => {
                        nb.shape_len == 1 && nb.shape[0] as usize == na.lif.expect("checked").len
                    }
                    (NirNodeKind::Linear, NirNodeKind::Output) => {
                        nb.shape_len == 1
                            && nb.shape[0] as usize == na.linear.expect("checked").rows
                    }
                    _ => true, // rejected above
                };
                if !ok {
                    return Err(NirError::EdgeShapeMismatch {
                        src: na.name,
                        dst: nb.name,
                    });
                }
            }
            let recurrent = self.edges.iter().any(|&(a, b)| {
                matches!(
                    (nodes[a as usize].kind, nodes[b as usize].kind),
                    (NirNodeKind::Lif, NirNodeKind::Lif)
                )
            });
            if recurrent && self.opts.resolution == VoltageResolution::Millivolt {
                return Err(NirError::UnsupportedTopology(RECURRENT_MV_REMEDY));
            }
            Ok(())
        }

        /// Topological order of the Linear nodes over Linear→Linear
        /// edges (iterative DFS — a hostile 10k-deep chain must not
        /// overflow the stack). A cycle is a named rejection: no
        /// feedforward evaluation order exists.
        fn linear_order(&self) -> Result<Vec<usize>, NirError<'_>> {
            let n = self.nodes.len();
            // Linear-child adjacency (index -> Linear children)
            let mut children: Vec<Vec<usize>> = vec![Vec::new(); n];
            for &(a, b) in &self.edges {
                if self.nodes[a as usize].kind == NirNodeKind::Linear
                    && self.nodes[b as usize].kind == NirNodeKind::Linear
                {
                    children[a as usize].push(b as usize);
                }
            }
            let mut state = vec![0u8; n]; // 0 new, 1 open, 2 done
            let mut order = Vec::new();
            for root in 0..n {
                if self.nodes[root].kind != NirNodeKind::Linear || state[root] != 0 {
                    continue;
                }
                state[root] = 1;
                let mut stack = vec![(root, 0usize)];
                while let Some(&mut (v, ref mut probe)) = stack.last_mut() {
                    let mut descended = false;
                    while *probe < children[v].len() {
                        let w = children[v][*probe];
                        *probe += 1;
                        match state[w] {
                            1 => {
                                return Err(NirError::UnsupportedTopology(
                                    "cycle through Linear nodes — no feedforward evaluation order exists",
                                ));
                            }
                            0 => {
                                state[w] = 1;
                                stack.push((w, 0));
                                descended = true;
                                break;
                            }
                            _ => {}
                        }
                    }
                    if !descended {
                        state[v] = 2;
                        order.push(v);
                        stack.pop();
                    }
                }
            }
            order.reverse(); // DFS post-order is children-first
            Ok(order)
        }

        /// Symbolically evaluate the Linear DAG (topological fold) and
        /// quantize one matrix per (drive Linear, root Input) stage.
        /// `G(L)[r]` is the transform arriving at `L`'s INPUT from
        /// root `r` — identity from an Input parent, `W_p·G(p)[r]`
        /// through a Linear parent (multi-parent sums are the linear
        /// DAG's native semantics) — so the stage matrix is `W_L·G`.
        /// D2 fusion records every stage composed from ≥2 matrices,
        /// with each component tensor's scale.
        // the symbolic DAG fold is one coherent unit; splitting the G
        // accumulation from stage quantization would spread the
        // borrowed-g map across two functions (nir_import precedent)
        #[allow(clippy::too_many_lines)]
        fn build_stages(
            &self,
            inputs: &[usize],
            order: &[usize],
            pop_base: &BTreeMap<usize, usize>,
        ) -> Result<EncoderPlan<'_>, NirError<'_>> {
            let n_nodes = self.nodes.len();
            let mut incoming: Vec<Vec<usize>> = vec![Vec::new(); n_nodes];
            let mut lif_children: Vec<Vec<usize>> = vec![Vec::new(); n_nodes];
            for &(src, dst) in &self.edges {
                incoming[dst as usize].push(src as usize);
                if self.nodes[src as usize].kind == NirNodeKind::Linear
                    && self.nodes[dst as usize].kind == NirNodeKind::Lif
                {
                    lif_children[src as usize].push(dst as usize);
                }
            }
            // G: Linear -> (root -> (cols x feat matrix, contributors))
            let mut g: BTreeMap<usize, GMap> = BTreeMap::new();
            for &l in order {
                let cols = self.nodes[l].linear.expect("checked").cols;
                let mut acc: GMap = BTreeMap::new();
                for &p in &incoming[l] {
                    match self.nodes[p].kind {
                        NirNodeKind::Input => {
                            let n_feat = self.nodes[p].shape[0] as usize; // == cols (checked)
                            let entry_val = acc
                                .entry(p)
                                .or_insert_with(|| (mat_zero(cols, n_feat), Vec::new()));
                            for i in 0..cols {
                                entry_val.0[i][i] += 1.0;
                            }
                        }
                        NirNodeKind::Linear => {
                            let lin_p = self.nodes[p].linear.expect("checked");
                            let w = &self.weights[lin_p.weight_offset
                                ..lin_p.weight_offset + lin_p.rows * lin_p.cols];
                            for (root, (m, contrib)) in &g[&p] {
                                let n_feat = m[0].len();
                                let entry_val = acc
                                    .entry(*root)
                                    .or_insert_with(|| (mat_zero(cols, n_feat), Vec::new()));
                                for row in 0..cols {
                                    for col in 0..lin_p.cols {
                                        // dequantized source value (q·scale) —
                                        // the product composes the tensors the
                                        // export renders, D2's f64 seam. NOTE:
                                        // stride and bound are the PARENT's
                                        // cols (row-major arena layout).
                                        let wv = f64::from(w[row * lin_p.cols + col]) * lin_p.scale;
                                        if wv != 0.0 {
                                            for (cell, &src) in
                                                entry_val.0[row].iter_mut().zip(&m[col])
                                            {
                                                *cell += wv * src;
                                            }
                                        }
                                    }
                                }
                                for &ci in contrib.iter().chain(core::iter::once(&p)) {
                                    if !entry_val.1.contains(&ci) {
                                        entry_val.1.push(ci);
                                    }
                                }
                            }
                        }
                        _ => {} // LIF parents are named-rejected already
                    }
                }
                g.insert(l, acc);
            }

            let mut mats: Vec<QuantMat> = Vec::new();
            let mut stages: Vec<DriveStage> = Vec::new();
            let mut fused: Vec<LinearFusedRecord<'_>> = Vec::new();
            let mut rooted: BTreeSet<usize> = BTreeSet::new();
            for &lin_idx in order {
                if g[&lin_idx].is_empty() {
                    continue; // no root path — never invoked (noted)
                }
                rooted.insert(lin_idx); // has an Input-rooted path, whatever it feeds
                if lif_children[lin_idx].is_empty() {
                    continue; // feeds no LIF — stages come from its consumers
                }
                let lin_l = self.nodes[lin_idx].linear.expect("checked");
                let rows = lin_l.rows;
                let w = &self.weights[lin_l.weight_offset..lin_l.weight_offset + rows * lin_l.cols];
                for (root, (m, contrib)) in &g[&lin_idx] {
                    let f = m[0].len();
                    // stage matrix: W_L · G  (rows x f, row-major)
                    let mut flat = vec![0f64; rows * f];
                    for r in 0..rows {
                        for k in 0..f {
                            let mut s = 0.0f64;
                            for c in 0..lin_l.cols {
                                s += f64::from(w[r * lin_l.cols + c]) * lin_l.scale * m[c][k];
                            }
                            flat[r * f + k] = s;
                        }
                    }
                    let mut q = vec![0i16; rows * f];
                    super::quantize_linear(&flat, rows, f, &mut q, 0)?;
                    let mat = mats.len();
                    mats.push(QuantMat { q, rows, cols: f });
                    let root_ord = inputs
                        .iter()
                        .position(|&i| i == *root)
                        .expect("root is an Input");
                    for &pop in &lif_children[lin_idx] {
                        stages.push(DriveStage {
                            pop_base: pop_base[&pop],
                            root: root_ord,
                            mat,
                        });
                    }
                    if !contrib.is_empty() {
                        let mut chain: Vec<&str> =
                            contrib.iter().map(|&i| self.nodes[i].name).collect();
                        chain.push(self.nodes[lin_idx].name);
                        let mut scales: Vec<f64> = contrib
                            .iter()
                            .map(|&i| self.nodes[i].linear.expect("checked").scale)
                            .collect();
                        scales.push(lin_l.scale);
                        fused.push(LinearFusedRecord { chain, scales });
                    }
                }
            }
            let drive_linears = (0..n_nodes)
                .filter(|&i| {
                    self.nodes[i].kind == NirNodeKind::Linear && !lif_children[i].is_empty()
                })
                .count();
            Ok(EncoderPlan {
                mats,
                stages,
                fused,
                rooted,
                drive_linears,
            })
        }

        /// `UndrivenPopulation` notes (structural, name-carrying): LIF
        /// populations with no Input-rooted path (permanently silent)
        /// and Linear nodes never invoked as encoders (no root path or
        /// no path to any LIF). The reference tolerates AND auto-wires
        /// such components at emission; we do not invent structure at
        /// import — silence is documented, never silent.
        fn undriven_notes(&self, inputs: &[usize], rooted: &BTreeSet<usize>) -> Vec<&'_ str> {
            let n = self.nodes.len();
            // forward closure from the Inputs, over all edges
            let mut reached = vec![false; n];
            let mut work: Vec<usize> = inputs.to_vec();
            for &i in &work {
                reached[i] = true;
            }
            while let Some(v) = work.pop() {
                for &(a, b) in &self.edges {
                    if a as usize == v && !reached[b as usize] {
                        reached[b as usize] = true;
                        work.push(b as usize);
                    }
                }
            }
            // backward closure from the LIF nodes (can reach a LIF)
            let mut can_spike_feed = vec![false; n];
            let mut work: Vec<usize> = (0..n)
                .filter(|&i| self.nodes[i].kind == NirNodeKind::Lif)
                .collect();
            for &i in &work {
                can_spike_feed[i] = true;
            }
            while let Some(v) = work.pop() {
                for &(a, b) in &self.edges {
                    if b as usize == v && !can_spike_feed[a as usize] {
                        can_spike_feed[a as usize] = true;
                        work.push(a as usize);
                    }
                }
            }
            (0..n)
                .filter(|&i| match self.nodes[i].kind {
                    NirNodeKind::Lif => !reached[i],
                    NirNodeKind::Linear => !(rooted.contains(&i) && can_spike_feed[i]),
                    _ => false,
                })
                .map(|i| self.nodes[i].name)
                .collect()
        }
    }

    /// One quantized encoder matrix (row-major `q`, the stage's
    /// `rows × cols` product quantized once via
    /// [`quantize_linear`](super::quantize_linear)).
    #[derive(Debug)]
    struct QuantMat {
        q: Vec<i16>,
        rows: usize,
        cols: usize,
    }

    /// What [`NirImport::build_network`] derives from the Linear DAG:
    /// quantized stage matrices, stage wiring, fusion records, the
    /// Input-rooted Linear set (for undriven notes), and the D6
    /// drive-Linear count.
    struct EncoderPlan<'a> {
        mats: Vec<QuantMat>,
        stages: Vec<DriveStage>,
        fused: Vec<LinearFusedRecord<'a>>,
        rooted: BTreeSet<usize>,
        drive_linears: usize,
    }

    /// A drive stage: matrix `mat` drives the population at neuron
    /// offset `pop_base` from root-Input ordinal `root`.
    #[derive(Debug)]
    struct DriveStage {
        pop_base: usize,
        root: usize,
        mat: usize,
    }

    /// The Linear half of a general graph: root-Input feature
    /// currents (μA) → the global per-step current vector (μA, one
    /// entry per neuron). One quantized matrix per (drive Linear,
    /// root) pair — fused chains arrive as a single composed matrix
    /// (D2: no hop-by-hop i16 encode-composition) — with the
    /// substrate's `/100` encoder gain and i64 row accumulation
    /// (`ChainEncoder` semantics per stage), merged saturating-i16
    /// across stages (D5's mechanical merge).
    #[derive(Debug)]
    pub struct NirGraphEncoder {
        total: usize,
        input_feats: Vec<usize>,
        mats: Vec<QuantMat>,
        stages: Vec<DriveStage>,
    }

    impl NirGraphEncoder {
        /// Number of Input nodes the encoder reads (the `encode`
        /// slice order — Input node order).
        #[must_use]
        pub fn input_count(&self) -> usize {
            self.input_feats.len()
        }

        /// Feature count of Input `i` (the slice length `encode` reads).
        #[must_use]
        pub fn input_features(&self, i: usize) -> usize {
            self.input_feats.get(i).copied().unwrap_or(0)
        }

        /// The quantized matrix of stage `i` (row-major), for exact
        /// external pins (fusion exactness gates read this).
        #[must_use]
        pub fn stage_quanta(&self, i: usize) -> Option<&[i16]> {
            self.mats.get(i).map(|m| m.q.as_slice())
        }

        /// Number of quantized stage matrices.
        #[must_use]
        pub fn stage_count(&self) -> usize {
            self.mats.len()
        }

        /// Encode per-Input feature currents into the global per-step
        /// current vector. Missing Inputs / missing feature entries
        /// read as 0 (the `ChainEncoder`'s graceful-zeros convention).
        #[must_use]
        pub fn encode(&self, per_input: &[&[i16]]) -> Vec<i16> {
            let mut out = vec![0i16; self.total];
            for st in &self.stages {
                let m = &self.mats[st.mat];
                let x = per_input.get(st.root).copied().unwrap_or(&[]);
                for r in 0..m.rows {
                    let mut acc: i64 = 0;
                    for c in 0..m.cols {
                        acc += i64::from(m.q[r * m.cols + c])
                            * i64::from(x.get(c).copied().unwrap_or(0));
                    }
                    acc /= 100;
                    let v = acc.clamp(i64::from(i16::MIN), i64::from(i16::MAX)) as i16;
                    out[st.pop_base + r] = out[st.pop_base + r].saturating_add(v);
                }
            }
            out
        }
    }

    /// A fused encoder stage (D2): the composed Linear chain
    /// (root-first, target last) with each component tensor's
    /// quantization scale.
    #[derive(Debug, Clone, PartialEq)]
    pub struct LinearFusedRecord<'a> {
        pub chain: Vec<&'a str>,
        pub scales: Vec<f64>,
    }

    /// The loud assembly report.
    #[derive(Debug, Clone, PartialEq)]
    pub struct NirAssemblyReport<'a> {
        /// Total neurons (all populations).
        pub neurons: usize,
        /// LIF→LIF synapse pairs at [`EDGE_PULSE_QUANTA`].
        pub synapses: usize,
        /// Input nodes (encoder entry points).
        pub inputs: usize,
        /// Linear nodes directly feeding a LIF stage (D6's note fires
        /// when > 1: each tensor's absmax scale absorbs its branch's
        /// true gain — the dequantizing global-scale encode is a
        /// named follow-up, NOT this surface).
        pub drive_linears: usize,
        /// Quantized (drive Linear × root) encoder matrices.
        pub stages: usize,
        /// D2 fusion records (stages composed from ≥2 tensors).
        pub fused: Vec<LinearFusedRecord<'a>>,
        /// `UndrivenPopulation` notes, node order (permanently-silent
        /// LIF populations; never-invoked Linear encoders).
        pub undriven: Vec<&'a str>,
        /// The D6 graph-level gain note: `drive_linears > 1`.
        pub multi_linear_gain: bool,
        /// Plasticity frozen at assembly (NIR has no plasticity term).
        pub plasticity_frozen: bool,
    }

    /// The named mV-on-recurrent rejection, remedy verbatim and
    /// copy-pasteable (D1 + the plan-gate ruling).
    const RECURRENT_MV_REMEDY: &str = "recurrent graph on mV: pulses fall in the ~200 uA dead \
     zone — re-import with NirImportOptions { resolution: \
     VoltageResolution::CentiMillivolt, ..NirImportOptions::default() }";

    /// The assembly's neuron mapping (`build_chain_network`'s, shared):
    /// per-neuron quantized params onto an Excitatory substrate
    /// neuron, deterministic (noise 0), minimum refractory.
    fn substrate_neuron(id: u16, p: &NirLif, res: VoltageResolution) -> LIFNeuron {
        let mut n = LIFNeuron::new_with_type_resolution(id, NeuronType::Excitatory, res);
        n.resting_potential = p.leak_q;
        n.membrane_potential = p.leak_q;
        n.threshold = p.threshold_q;
        n.reset_potential = p.reset_q;
        n.tau_membrane_us = p.tau_us;
        n.tau_refractory_us = 1_000; // NIR LIF has no refractory → minimum
        n.resistance_mohm = p.resistance_mohm;
        n.capacitance_pf = p.capacitance_pf;
        n.noise_amplitude_ua = 0; // import is deterministic
        n
    }

    fn mat_zero(r: usize, c: usize) -> Vec<Vec<f64>> {
        vec![vec![0.0; c]; r]
    }

    /// The value arriving at a Linear's input from one root Input:
    /// the composed matrix (cols × feat) + the contributing Linear
    /// node indices (topological order, deduplicated).
    type GVal = (Vec<Vec<f64>>, Vec<usize>);

    /// `G(L)`: root-Input index → arriving value.
    type GMap = BTreeMap<usize, GVal>;

    /// The Linear half of the chain: feature currents (μA) →
    /// per-neuron currents (μA), saturating i16 — `y = W·x` in
    /// substrate units. Weights are scaled by 1/100 (the substrate's
    /// synapse convention: a weight contributes `w/100` μA per unit
    /// input feature current), documented as the encoder's gain.
    #[derive(Debug)]
    pub struct ChainEncoder<'a> {
        lin: NirLinear,
        weights: &'a [i16],
    }

    impl ChainEncoder<'_> {
        /// Rows (neurons) of the encoder.
        #[must_use]
        pub fn rows(&self) -> usize {
            self.lin.rows
        }

        /// Columns (input features).
        #[must_use]
        pub fn cols(&self) -> usize {
            self.lin.cols
        }

        /// Encode feature currents into per-neuron currents.
        /// i64 accumulator: i32 would overflow (and in release,
        /// silently wrap) at cols ≥ 3 with |w| = |x| = 32767.
        #[must_use]
        pub fn encode(&self, x: &[i16]) -> Vec<i16> {
            let mut out = vec![0i16; self.lin.rows];
            for (r, o) in out.iter_mut().enumerate() {
                let mut acc: i64 = 0;
                for c in 0..self.lin.cols {
                    let w = i64::from(self.weights[self.lin.weight_offset + r * self.lin.cols + c]);
                    acc += w * i64::from(x.get(c).copied().unwrap_or(0));
                }
                acc /= 100;
                *o = acc.clamp(i64::from(i16::MIN), i64::from(i16::MAX)) as i16;
            }
            out
        }
    }

    /// Structured-entry graph builder: construct a NIR graph in
    /// memory — no JSON document. `quantize_lif` / `quantize_linear`
    /// are the quantization seam; this is the graph seam over them.
    /// Node names may be any `&str` (the printable-ASCII gate is a
    /// property of the JSON container and fires only at export);
    /// `add_*` return the node index `add_edge` wires together;
    /// [`NirBuilder::build`] runs the import-parity checks
    /// (duplicate names, duplicate edges) and yields a
    /// [`NirImport`] that exports and assembles like any other.
    #[derive(Debug)]
    pub struct NirBuilder<'a> {
        nodes: Vec<NirNode<'a>>,
        edges: Vec<(u32, u32)>,
        weights: Vec<i16>,
        lifs: Vec<NirLif>,
        opts: NirImportOptions,
    }

    impl<'a> NirBuilder<'a> {
        /// An empty graph under the given import options (dt + the
        /// voltage grid the LIF quantizer uses).
        #[must_use]
        pub fn new(opts: NirImportOptions) -> Self {
            Self {
                nodes: Vec::new(),
                edges: Vec::new(),
                weights: Vec::new(),
                lifs: Vec::new(),
                opts,
            }
        }

        /// Add an `Input` node (shape carrier; 1–4 dims).
        ///
        /// # Errors
        ///
        /// [`NirError::BadShape("shape")`] beyond 4 dims.
        pub fn add_input(&mut self, name: &'a str, shape: &[u32]) -> Result<usize, NirError<'a>> {
            self.push_node(name, NirNodeKind::Input, shape)
        }

        /// Add an `Output` node (shape carrier; 1–4 dims).
        ///
        /// # Errors
        ///
        /// [`NirError::BadShape("shape")`] beyond 4 dims.
        pub fn add_output(&mut self, name: &'a str, shape: &[u32]) -> Result<usize, NirError<'a>> {
            self.push_node(name, NirNodeKind::Output, shape)
        }

        /// Add a `LIF` population from per-neuron source-unit
        /// parameters — quantized via [`quantize_lif`](super::quantize_lif)
        /// under the builder's options. All slices must have equal
        /// length ≥ 1; `v_reset_v` `None` = the reference's
        /// absent-`v_reset` zeros semantics.
        ///
        /// Atomic: a failed add (including a mid-population
        /// quantize failure) leaves the builder exactly as it was.
        ///
        /// # Errors
        ///
        /// [`NirError::BadShape("LIF param")`] on length mismatch or
        /// empty arrays; every [`quantize_lif`](super::quantize_lif)
        /// hard failure otherwise.
        pub fn add_lif_population(
            &mut self,
            name: &'a str,
            params: &NirLifParams<'_>,
        ) -> Result<usize, NirError<'a>> {
            let n = params.tau_s.len();
            if n == 0
                || params.r_ohm.len() != n
                || params.v_leak_v.len() != n
                || params.v_threshold_v.len() != n
                || params.v_reset_v.is_some_and(|v| v.len() != n)
            {
                return Err(NirError::BadShape("LIF param"));
            }
            // Quantize the whole population into scratch FIRST: a
            // mid-population failure must leave the builder
            // untouched (no zombie node, no half-appended lif
            // records).
            let mut pop = Vec::with_capacity(n);
            for i in 0..n {
                let v_reset = match params.v_reset_v {
                    Some(v) => v[i],
                    None => 0.0,
                };
                pop.push(super::quantize_lif(
                    params.tau_s[i],
                    params.r_ohm[i],
                    params.v_leak_v[i],
                    params.v_threshold_v[i],
                    v_reset,
                    params.v_reset_v.is_none(),
                    self.opts,
                )?);
            }
            // Past this point nothing can fail (see add_linear).
            let idx = self.push_node(name, NirNodeKind::Lif, &[])?;
            let offset = self.lifs.len();
            self.lifs.extend_from_slice(&pop);
            self.nodes[idx].lif = Some(NirLifPopulation { offset, len: n });
            Ok(idx)
        }

        /// Add a `Linear` node from materialized row-major f64
        /// weights — quantized into the arena via
        /// [`quantize_linear`](super::quantize_linear).
        ///
        /// Atomic: a failed add leaves the builder exactly as it
        /// was — no node, no arena growth.
        ///
        /// # Errors
        ///
        /// Every [`quantize_linear`](super::quantize_linear) failure.
        pub fn add_linear(
            &mut self,
            name: &'a str,
            values: &[f64],
            rows: usize,
            cols: usize,
        ) -> Result<usize, NirError<'a>> {
            // Quantize into scratch FIRST: any failure must leave
            // the builder untouched (no zombie node, no zero tail
            // on the weights arena).
            let n = rows.checked_mul(cols).ok_or(NirError::BadShape("weight"))?;
            let mut scratch = vec![0i16; n];
            let mut lin = super::quantize_linear(values, rows, cols, &mut scratch, 0)?;
            // Past this point nothing can fail: push_node with an
            // empty shape is infallible.
            let idx = self.push_node(name, NirNodeKind::Linear, &[])?;
            lin.weight_offset = self.weights.len();
            self.weights.extend_from_slice(&scratch);
            self.nodes[idx].linear = Some(lin);
            Ok(idx)
        }

        /// Wire `from → to` (indices returned by the `add_*` calls).
        ///
        /// # Errors
        ///
        /// [`NirError::BadShape("edges")`] when an index names no
        /// node (the builder twin of import's endpoint resolution).
        pub fn add_edge(&mut self, from: usize, to: usize) -> Result<(), NirError<'a>> {
            let end = self.nodes.len();
            if from >= end || to >= end {
                return Err(NirError::BadShape("edges"));
            }
            self.edges.push((from as u32, to as u32));
            Ok(())
        }

        /// Finish: import-parity checks (duplicate node names,
        /// duplicate edges — the reference `validate_structure`
        /// semantics) and hand over the graph as a [`NirImport`].
        ///
        /// # Errors
        ///
        /// [`NirError::DuplicateNodeName`], [`NirError::DuplicateEdge`].
        pub fn build(self) -> Result<NirImport<'a>, NirError<'a>> {
            for i in 0..self.nodes.len() {
                for j in (i + 1)..self.nodes.len() {
                    if self.nodes[i].name == self.nodes[j].name {
                        return Err(NirError::DuplicateNodeName);
                    }
                }
            }
            for i in 0..self.edges.len() {
                for j in (i + 1)..self.edges.len() {
                    if self.edges[i] == self.edges[j] {
                        return Err(NirError::DuplicateEdge);
                    }
                }
            }
            Ok(NirImport {
                nodes: self.nodes,
                edges: self.edges,
                weights: self.weights,
                lifs: self.lifs,
                opts: self.opts,
                ref_sha: NIR_REF_SHA,
            })
        }

        fn push_node(
            &mut self,
            name: &'a str,
            kind: NirNodeKind,
            shape: &[u32],
        ) -> Result<usize, NirError<'a>> {
            if shape.len() > 4 {
                return Err(NirError::BadShape("shape"));
            }
            let mut s = [0u32; 4];
            s[..shape.len()].copy_from_slice(shape);
            self.nodes.push(NirNode {
                name,
                kind,
                shape: s,
                shape_len: shape.len(),
                lif: None,
                linear: None,
            });
            Ok(self.nodes.len() - 1)
        }
    }
}

#[cfg(feature = "std")]
pub use std_assembly::{
    ChainEncoder, LinearFusedRecord, NirAssemblyReport, NirBuilder, NirGraphEncoder, NirImport,
    EDGE_PULSE_QUANTA,
};

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

    const CHAIN: &str = "{\"version\":\"test\",\"node\":{\"type\":\"NIRGraph\",\
\"edges\":[[\"input\",\"linear\"],[\"linear\",\"lif\"],[\"lif\",\"output\"]],\
\"nodes\":{\"input\":{\"type\":\"Input\",\"shape\":[3]},\
\"linear\":{\"type\":\"Linear\",\"weight\":[[0.5,-1.0,0.25],[0.0,0.75,-0.5]]},\
\"lif\":{\"type\":\"LIF\",\"tau\":[0.02,0.02],\"r\":[100000000.0,100000000.0],\
\"v_leak\":[-0.07,-0.07],\"v_threshold\":[-0.055,-0.055],\"v_reset\":[-0.08,-0.08]},\
\"output\":{\"type\":\"Output\",\"shape\":[2]}}}}";

    #[test]
    fn scan_counts_the_chain() {
        let s = nir_scan(CHAIN.as_bytes()).expect("chain scans");
        assert_eq!(s.version, "test");
        assert_eq!(s.node_count, 4);
        assert_eq!(s.edge_count, 3);
        assert_eq!(s.weight_cells, 6);
        assert_eq!(s.lif_neurons, 2);
    }

    #[test]
    fn scan_rejects_malformed() {
        assert!(matches!(nir_scan(b"{not json"), Err(NirError::Json(_))));
        assert!(matches!(
            nir_scan(b"{}"),
            Err(NirError::MissingField("version"))
        ));
        assert!(matches!(
            nir_scan(b"{\"version\":\"x\"}"),
            Err(NirError::MissingField("node"))
        ));
    }

    #[allow(clippy::type_complexity)]
    fn import_chain(
        opts: NirImportOptions,
    ) -> (
        Vec<NirNode<'static>>,
        Vec<(u32, u32)>,
        Vec<i16>,
        Vec<NirLif>,
        NirReport,
    ) {
        let scan = nir_scan(CHAIN.as_bytes()).unwrap();
        let mut nodes = vec![
            NirNode {
                name: "",
                kind: NirNodeKind::Input,
                shape: [0; 4],
                shape_len: 0,
                lif: None,
                linear: None,
            };
            scan.node_count
        ];
        let mut edges = vec![(0u32, 0u32); scan.edge_count];
        let mut weights = vec![0i16; scan.weight_cells];
        let mut lifs = vec![NirLif::default(); scan.lif_neurons];
        let mut scratch = vec![0f64; scan.weight_cells + 5 * scan.lif_neurons];
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        let report = nir_import(CHAIN.as_bytes(), opts, &mut bufs).expect("chain imports");
        (nodes, edges, weights, lifs, report)
    }

    #[test]
    fn import_quantizes_the_chain() {
        let (nodes, edges, weights, lifs, report) = import_chain(NirImportOptions::default());
        assert_eq!(
            edges,
            vec![(0, 1), (1, 2), (2, 3)],
            "edges resolve by name to document order"
        );
        assert_eq!(
            (report.inputs, report.linears, report.lifs, report.outputs),
            (1, 1, 1, 1)
        );
        assert_eq!(report.weight_cells, 6);

        let pop = nodes[2].lif.expect("lif present");
        assert_eq!(pop.len, 2, "a 2x3 Linear feeds a 2-neuron population");
        assert_eq!(pop.offset, 0);
        let lif = lifs[pop.offset];
        assert_eq!(lif.tau_us, 20_000);
        assert_eq!(lif.resistance_mohm, 100);
        assert_eq!(lif.leak_q, -70);
        assert_eq!(lif.threshold_q, -55);
        assert_eq!(lif.reset_q, -80);
        assert_eq!(lif.capacitance_pf, 200, "C = tau/r: 0.02/1e8 F = 200 pF");

        let lin = nodes[1].linear.expect("linear present");
        assert_eq!((lin.rows, lin.cols), (2, 3));
        assert!((lin.scale - 1.0 / I16_FS).abs() < 1e-18, "absmax 1.0");
        // q = round(w/scale) = w*32767 exactly for these dyadics
        let q: Vec<i32> = weights.iter().map(|&q| i32::from(q)).collect();
        assert_eq!(q, vec![16384, -32767, 8192, 0, 24575, -16384]);
        // scale = 1/32767 is non-dyadic: dequant error ≤ scale/2 and
        // the loss note fires — loud lossiness doing its job
        assert!(lin.max_abs_err > 0.0 && lin.max_abs_err <= lin.scale / 2.0);
        assert!(report.notes[NirNote::QuantizationLoss as usize] >= 1);
    }

    #[test]
    // 114 lines once rustfmt reflows the assertions (2026-09-02)
    #[allow(clippy::too_many_lines)]
    fn lif_hard_failures() {
        // tau < dt: the whole import must fail loudly
        let opts = NirImportOptions {
            dt_us: 30_000,
            ..NirImportOptions::default()
        };
        let scan = nir_scan(CHAIN.as_bytes()).unwrap();
        let mut nodes = vec![
            NirNode {
                name: "",
                kind: NirNodeKind::Input,
                shape: [0; 4],
                shape_len: 0,
                lif: None,
                linear: None,
            };
            scan.node_count
        ];
        let mut edges = vec![(0u32, 0u32); scan.edge_count];
        let mut weights = vec![0i16; scan.weight_cells];
        let mut lifs = vec![NirLif::default(); scan.lif_neurons];
        let mut scratch = vec![0f64; scan.weight_cells + 5 * scan.lif_neurons];
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        assert_eq!(
            nir_import(CHAIN.as_bytes(), opts, &mut bufs),
            Err(NirError::TauBelowDt)
        );
        // direct quantize_lif contract:
        assert!(matches!(
            quantize_lif(
                0.0,
                1e8,
                -0.07,
                -0.055,
                -0.08,
                false,
                NirImportOptions::default()
            ),
            Err(NirError::BadNumber("tau"))
        ));
        assert!(matches!(
            quantize_lif(
                -0.02,
                1e8,
                -0.07,
                -0.055,
                -0.08,
                false,
                NirImportOptions::default()
            ),
            Err(NirError::BadNumber("tau"))
        ));
        assert!(matches!(
            quantize_lif(
                0.02,
                0.0,
                -0.07,
                -0.055,
                -0.08,
                false,
                NirImportOptions::default()
            ),
            Err(NirError::BadNumber("r"))
        ));
        assert!(matches!(
            quantize_lif(
                0.02,
                1e8,
                -0.07,
                -0.0004,
                -0.08,
                false,
                NirImportOptions::default()
            ),
            Err(NirError::ThresholdZero)
        ));
        assert!(matches!(
            quantize_lif(
                0.02,
                1e8,
                -0.07,
                -0.055,
                -0.08,
                false,
                NirImportOptions::new(30_000, VoltageResolution::Millivolt)
            ),
            Err(NirError::TauBelowDt)
        ));
        assert!(matches!(
            quantize_lif(
                0.02,
                1e8,
                -0.07,
                0.06,
                -0.08,
                false,
                NirImportOptions::default()
            ),
            Err(NirError::PotentialOutOfRange("v_threshold"))
        ));
        // threshold on the centi grid: -0.0555 V is representable
        let lif = quantize_lif(
            0.02,
            1e8,
            -0.07,
            -0.0555,
            -0.08,
            false,
            NirImportOptions::new(1_000, VoltageResolution::CentiMillivolt),
        )
        .expect("centi grid");
        assert_eq!(lif.threshold_q, -5550);
    }

    #[test]
    fn unknown_kinds_and_affine_reject_loudly() {
        let doc = "{\"version\":\"x\",\"node\":{\"type\":\"NIRGraph\",\"edges\":[],\
\"nodes\":{\"a\":{\"type\":\"Affine\",\"weight\":[[1.0]],\"bias\":[0.5]}}}}";
        let mut nodes = [NirNode {
            name: "",
            kind: NirNodeKind::Input,
            shape: [0; 4],
            shape_len: 0,
            lif: None,
            linear: None,
        }];
        let mut edges = [];
        let mut weights = [0i16; 8];
        let mut lifs = [];
        let mut scratch = [0f64; 8];
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        assert_eq!(
            nir_import(doc.as_bytes(), NirImportOptions::default(), &mut bufs),
            Err(NirError::UnsupportedNodeKind("Affine"))
        );
    }

    #[test]
    fn edges_before_nodes_resolve() {
        // key order edges-then-nodes (our own export order)
        let doc = "{\"version\":\"x\",\"node\":{\"edges\":[[\"a\",\"b\"]],\
\"type\":\"NIRGraph\",\"nodes\":{\"a\":{\"type\":\"Input\",\"shape\":[1]},\
\"b\":{\"type\":\"Output\",\"shape\":[1]}}}}";
        let mut nodes = [NirNode {
            name: "",
            kind: NirNodeKind::Input,
            shape: [0; 4],
            shape_len: 0,
            lif: None,
            linear: None,
        }; 2];
        let mut edges = [(0u32, 0u32); 1];
        let mut weights = [];
        let mut lifs = [];
        let mut scratch = [];
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        nir_import(doc.as_bytes(), NirImportOptions::default(), &mut bufs).expect("resolves");
        assert_eq!(edges[0], (0, 1));
    }

    #[test]
    fn structural_rejections() {
        let mk = |nodes: &str, edges: &str| {
            format!(
                "{{\"version\":\"x\",\"node\":{{\"type\":\"NIRGraph\",\"edges\":{edges},\"nodes\":{nodes}}}}}"
            )
        };
        #[allow(clippy::type_complexity)]
        let cases: Vec<(&str, String, fn(&NirError) -> bool)> = vec![
            (
                "unknown endpoint",
                mk("{\"a\":{\"type\":\"Input\",\"shape\":[1]}}", "[[\"a\",\"zz\"]]"),
                |e| matches!(e, NirError::UnknownEdgeEndpoint("zz")),
            ),
            (
                "duplicate edge",
                mk(
                    "{\"a\":{\"type\":\"Input\",\"shape\":[1]},\"b\":{\"type\":\"Output\",\"shape\":[1]}}",
                    "[[\"a\",\"b\"],[\"a\",\"b\"]]",
                ),
                |e| matches!(e, NirError::DuplicateEdge),
            ),
            (
                "duplicate node name",
                mk(
                    "{\"a\":{\"type\":\"Input\",\"shape\":[1]},\"a\":{\"type\":\"Output\",\"shape\":[1]}}",
                    "[]",
                ),
                |e| matches!(e, NirError::DuplicateNodeName),
            ),
            (
                "escaped name",
                "{\"version\":\"x\",\"node\":{\"type\":\"NIRGraph\",\"edges\":[],\"nodes\":{\"a\\u0041\":{\"type\":\"Input\",\"shape\":[1]}}}}".to_string(),
                |e| matches!(e, NirError::EscapedOrNonAsciiString(_)),
            ),
            (
                "3d weight",
                mk(
                    "{\"a\":{\"type\":\"Linear\",\"weight\":[[[1.0]]]}}",
                    "[]",
                ),
                |e| matches!(e, NirError::BadShape("weight")),
            ),
            (
                "ragged weight",
                mk(
                    "{\"a\":{\"type\":\"Linear\",\"weight\":[[1.0],[1.0,2.0]]}}",
                    "[]",
                ),
                |e| matches!(e, NirError::BadShape("weight")),
            ),
        ];
        for (label, doc, check) in cases {
            let mut nodes = [NirNode {
                name: "",
                kind: NirNodeKind::Input,
                shape: [0; 4],
                shape_len: 0,
                lif: None,
                linear: None,
            }; 4];
            let mut edges = [(0u32, 0u32); 4];
            let mut weights = [0i16; 64];
            let mut lifs = [];
            let mut scratch = [0f64; 64];
            let mut bufs = NirBuffers {
                nodes: &mut nodes,
                edges: &mut edges,
                weights: &mut weights,
                lifs: &mut lifs,
                scratch: &mut scratch,
            };
            let err = nir_import(doc.as_bytes(), NirImportOptions::default(), &mut bufs)
                .expect_err(label);
            assert!(check(&err), "{label}: got {err:?}");
        }
    }

    #[test]
    fn buffer_overflow_is_loud() {
        // node capacity: the nodes check fires before weights are read
        let mut nodes = [NirNode {
            name: "",
            kind: NirNodeKind::Input,
            shape: [0; 4],
            shape_len: 0,
            lif: None,
            linear: None,
        }];
        let mut edges = [];
        let mut weights = [];
        let mut lifs = [];
        let mut scratch = [];
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        assert_eq!(
            nir_import(CHAIN.as_bytes(), NirImportOptions::default(), &mut bufs),
            Err(NirError::BufferOverflow)
        );
        // arena too small: staging completes (scratch fits the 6
        // cells), the quantizer refuses — offset+6 > 2
        let mut nodes = [NirNode {
            name: "",
            kind: NirNodeKind::Input,
            shape: [0; 4],
            shape_len: 0,
            lif: None,
            linear: None,
        }; 4];
        let mut edges = [(0u32, 0u32); 3];
        let mut weights = [0i16; 2];
        let mut lifs = [NirLif::default(); 1];
        let mut scratch = [0f64; 11]; // 6 weight + 5 LIF staging
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        assert_eq!(
            nir_import(CHAIN.as_bytes(), NirImportOptions::default(), &mut bufs),
            Err(NirError::BufferOverflow)
        );
        // scratch too small: the staging loop refuses mid-tensor
        let mut nodes = [NirNode {
            name: "",
            kind: NirNodeKind::Input,
            shape: [0; 4],
            shape_len: 0,
            lif: None,
            linear: None,
        }; 4];
        let mut edges = [(0u32, 0u32); 3];
        let mut weights = [0i16; 64];
        let mut lifs = [NirLif::default(); 1];
        let mut scratch = [0f64; 2];
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        assert_eq!(
            nir_import(CHAIN.as_bytes(), NirImportOptions::default(), &mut bufs),
            Err(NirError::BufferOverflow)
        );
    }

    #[test]
    fn export_then_import_is_idempotent() {
        let opts = NirImportOptions::default();
        let (nodes, edges, weights, lifs, _report) = import_chain(opts);

        let mut out = [0u8; 2048];
        let n = nir_export(&nodes, &edges, &weights, &lifs, opts, &mut out).expect("exports");
        let exported = core::str::from_utf8(&out[..n]).expect("utf8");

        // byte-stable: second export identical
        let mut out2 = [0u8; 2048];
        let n2 = nir_export(&nodes, &edges, &weights, &lifs, opts, &mut out2).unwrap();
        assert_eq!(&out[..n], &out2[..n2], "export is byte-stable");

        // re-import reproduces the graph bit-for-bit
        let scan2 = nir_scan(exported.as_bytes()).unwrap();
        assert_eq!(scan2.version, EXPORT_VERSION);
        let mut nodes2 = vec![
            NirNode {
                name: "",
                kind: NirNodeKind::Input,
                shape: [0; 4],
                shape_len: 0,
                lif: None,
                linear: None,
            };
            scan2.node_count
        ];
        let mut edges2 = vec![(0u32, 0u32); scan2.edge_count];
        let mut weights2 = vec![0i16; scan2.weight_cells];
        let mut lifs2 = vec![NirLif::default(); scan2.lif_neurons];
        let mut scratch2 = vec![0f64; scan2.weight_cells + 5 * scan2.lif_neurons];
        let mut bufs2 = NirBuffers {
            nodes: &mut nodes2,
            edges: &mut edges2,
            weights: &mut weights2,
            lifs: &mut lifs2,
            scratch: &mut scratch2,
        };
        let report2 = nir_import(exported.as_bytes(), opts, &mut bufs2).expect("re-imports");
        assert_eq!(edges2, edges);
        assert_eq!(weights2, weights, "quantized weights identical");
        // The CONTRACT is quantized-state identity. The re-import's
        // notes MAY grow: the dequantized max (|q|·scale in f64) can
        // re-derive a scale 1 ulp off, which the lossy-tensor note
        // honestly records while the state stays identical.
        let _ = report2;
        let pop1 = nodes[2].lif.unwrap();
        let pop2 = nodes2[2].lif.unwrap();
        let lif1 = lifs[pop1.offset];
        let lif2 = lifs2[pop2.offset];
        assert_eq!(
            lif2, lif1,
            "the whole quantized record survives the round trip"
        );
        let linear_out = nodes2[1].linear.unwrap();
        assert_eq!(
            linear_out.scale,
            nodes[1].linear.unwrap().scale,
            "scale recovered exactly"
        );
        // provenance rode metadata: the ORIGINAL source floats survive
        assert_eq!(lif2.tau_s, lif1.tau_s);
    }

    #[test]
    fn exported_json_is_valid_shape() {
        let opts = NirImportOptions::default();
        let (nodes, edges, weights, lifs, _) = import_chain(opts);
        let mut out = [0u8; 2048];
        let n = nir_export(&nodes, &edges, &weights, &lifs, opts, &mut out).unwrap();
        let s = core::str::from_utf8(&out[..n]).unwrap();
        assert!(s.starts_with("{\"version\":\"nir@7883c3c\",\"node\":{\"type\":\"NIRGraph\""));
        assert!(s.contains("\"metadata\":{\"neuralos\":{\"provenance\":{\"absmax\":1}"));
        assert!(s.ends_with("}}}"));
        // one version block only
        assert_eq!(s.matches("\"version\"").count(), 1);
        // balanced braces
        let mut depth = 0i32;
        for c in s.chars() {
            if c == '{' {
                depth += 1;
            } else if c == '}' {
                depth -= 1;
            }
        }
        assert_eq!(depth, 0, "braces balance");
    }

    #[test]
    fn zero_tensor_and_lossy_notes() {
        let doc = "{\"version\":\"x\",\"node\":{\"type\":\"NIRGraph\",\"edges\":[],\
\"nodes\":{\"a\":{\"type\":\"Linear\",\"weight\":[[0.0,0.0],[0.0,0.0]]}}}}";
        let mut nodes = [NirNode {
            name: "",
            kind: NirNodeKind::Input,
            shape: [0; 4],
            shape_len: 0,
            lif: None,
            linear: None,
        }];
        let mut edges = [];
        let mut weights = [0i16; 64];
        let mut lifs = [];
        let mut scratch = [0f64; 64];
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        let rep = nir_import(doc.as_bytes(), NirImportOptions::default(), &mut bufs).unwrap();
        assert!(rep.notes[NirNote::ZeroWeightTensor as usize] >= 1);
        assert_eq!(nodes[0].linear.unwrap().scale, 1.0);

        // a lossy tensor (0.1 is not dyadic) is noted
        let doc2 = doc.replace("[[0.0,0.0],[0.0,0.0]]", "[[0.1,0.3]]");
        let mut bufs2 = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        let rep2 = nir_import(doc2.as_bytes(), NirImportOptions::default(), &mut bufs2).unwrap();
        assert!(rep2.notes[NirNote::QuantizationLoss as usize] >= 1);
    }

    #[test]
    fn v_reset_defaults_to_zero_with_note() {
        // reference from_dict semantics: absent v_reset = zeros
        let doc = "{\"version\":\"x\",\"node\":{\"type\":\"NIRGraph\",\"edges\":[],\
\"nodes\":{\"a\":{\"type\":\"LIF\",\"tau\":[0.02],\"r\":[100000000.0],\
\"v_leak\":[-0.07],\"v_threshold\":[-0.055]}}}}";
        let mut nodes = [NirNode {
            name: "",
            kind: NirNodeKind::Input,
            shape: [0; 4],
            shape_len: 0,
            lif: None,
            linear: None,
        }];
        let mut edges = [];
        let mut weights = [];
        let mut lifs = [NirLif::default(); 1];
        let mut scratch = [0f64; 5];
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        let rep = nir_import(doc.as_bytes(), NirImportOptions::default(), &mut bufs).unwrap();
        assert!(rep.notes[NirNote::VResetDefaulted as usize] >= 1);
        let pop = nodes[0].lif.unwrap();
        assert_eq!(pop.len, 1);
        let lif = lifs[pop.offset];
        assert_eq!(lif.reset_q, 0);
        assert!(lif.v_reset_defaulted);
    }

    // ---- fresh-eyes review (R9): adversarial pins for each fix ----

    #[test]
    fn skip_value_is_depth_capped() {
        let nest = |n: usize| format!("{}0{}", "[".repeat(n), "]".repeat(n));
        // deep junk at top level — a Json error, not a stack overflow
        let doc = format!("{{\"version\":\"x\",\"junk\":{}}}", nest(200));
        assert!(matches!(nir_scan(doc.as_bytes()), Err(NirError::Json(_))));
        // deep junk inside a node (the metadata path)
        let doc2 = format!(
            "{{\"version\":\"x\",\"node\":{{\"type\":\"NIRGraph\",\"edges\":[],\
             \"nodes\":{{\"a\":{{\"type\":\"Input\",\"shape\":[1],\"deep\":{}}}}}}}}}",
            nest(200)
        );
        assert!(matches!(nir_scan(doc2.as_bytes()), Err(NirError::Json(_))));
        // generous-but-legal nesting still passes (metadata headroom)
        let ok = format!(
            "{{\"version\":\"x\",\"node\":{{\"type\":\"NIRGraph\",\"edges\":[],\"nodes\":{{}}}},\"junk\":{}}}",
            nest(40)
        );
        assert!(nir_scan(ok.as_bytes()).is_ok());
    }

    #[test]
    fn trailing_content_after_root_is_rejected() {
        let mut b = CHAIN.as_bytes().to_vec();
        b.extend_from_slice(b"garbage");
        assert!(matches!(nir_scan(&b), Err(NirError::Json(_))));
        // even well-formed extra JSON after the root
        let mut b2 = CHAIN.as_bytes().to_vec();
        b2.extend_from_slice(b" {}");
        assert!(matches!(nir_scan(&b2), Err(NirError::Json(_))));
        // trailing whitespace is fine (the fixtures end with \n)
        let mut b3 = CHAIN.as_bytes().to_vec();
        b3.extend_from_slice(b" \n\t");
        assert!(nir_scan(&b3).is_ok());
        // the buffer-import walks enforce it too
        let doc = b"{\"version\":\"x\",\"node\":{\"type\":\"NIRGraph\",\"edges\":[],\
\"nodes\":{\"a\":{\"type\":\"Input\",\"shape\":[1]}}}} x";
        let mut nodes = [NirNode {
            name: "",
            kind: NirNodeKind::Input,
            shape: [0; 4],
            shape_len: 0,
            lif: None,
            linear: None,
        }];
        let mut edges = [];
        let mut weights = [];
        let mut lifs = [];
        let mut scratch = [];
        let mut bufs = NirBuffers {
            nodes: &mut nodes,
            edges: &mut edges,
            weights: &mut weights,
            lifs: &mut lifs,
            scratch: &mut scratch,
        };
        assert!(matches!(
            nir_import(doc, NirImportOptions::default(), &mut bufs),
            Err(NirError::Json(_))
        ));
    }

    #[test]
    fn denormal_absmax_scale_is_a_loud_error() {
        // absmax/32767 underflows to exactly 0.0: the record would
        // lie (q·scale = 0 ≠ absmax) and export would silently zero
        // the tensor, breaking idempotence
        for tok in ["5e-324", "1e-320", "2.47e-321"] {
            let doc = format!(
                "{{\"version\":\"x\",\"node\":{{\"type\":\"NIRGraph\",\"edges\":[],\
                 \"nodes\":{{\"a\":{{\"type\":\"Linear\",\"weight\":[[{tok}]]}}}}}}}}"
            );
            let mut nodes = [NirNode {
                name: "",
                kind: NirNodeKind::Input,
                shape: [0; 4],
                shape_len: 0,
                lif: None,
                linear: None,
            }];
            let mut edges = [];
            let mut weights = [0i16; 8];
            let mut lifs = [];
            let mut scratch = [0f64; 8];
            let mut bufs = NirBuffers {
                nodes: &mut nodes,
                edges: &mut edges,
                weights: &mut weights,
                lifs: &mut lifs,
                scratch: &mut scratch,
            };
            assert_eq!(
                nir_import(doc.as_bytes(), NirImportOptions::default(), &mut bufs),
                Err(NirError::BadNumber("weight")),
                "{tok}"
            );
        }
    }

    #[test]
    fn round_half_away_pins_half_boundaries() {
        // exact halves round away from zero
        assert_eq!(round_half_away(0.5), 1.0);
        assert_eq!(round_half_away(-0.5), -1.0);
        assert_eq!(round_half_away(2.5), 3.0);
        assert_eq!(round_half_away(-2.5), -3.0);
        // one ulp BELOW a half must round down — the classic
        // add-±0.5-then-truncate idiom rounded these UP (review
        // finding: 0.49999999999999994 + 0.5 == 1.0 in f64)
        let below = |x: f64| f64::from_bits(x.to_bits() - 1);
        assert_eq!(round_half_away(below(0.5)), 0.0);
        assert_eq!(round_half_away(below(1.5)), 1.0);
        assert_eq!(round_half_away(below(2.5)), 2.0);
        assert_eq!(round_half_away(below(10.5)), 10.0);
        // reachability: r one ulp below 0.5 MΩ is a loud BadNumber,
        // not a silent 1 MΩ
        assert!(matches!(
            quantize_lif(
                0.02,
                499_999.999_999_999_94,
                -0.07,
                -0.055,
                -0.08,
                false,
                NirImportOptions::default()
            ),
            Err(NirError::BadNumber("r"))
        ));
    }

    #[test]
    fn lif_population_semantics() {
        // mismatched param lengths are structural: scan rejects
        let mk_lif = |tau: &str, r: &str| {
            format!(
                "{{\"version\":\"x\",\"node\":{{\"type\":\"NIRGraph\",\"edges\":[],\
                 \"nodes\":{{\"a\":{{\"type\":\"LIF\",\"tau\":{tau},\"r\":{r},\
                 \"v_leak\":[-0.07,-0.07],\"v_threshold\":[-0.055,-0.055],\
                 \"v_reset\":[-0.08,-0.08]}}}}}}}}"
            )
        };
        assert!(matches!(
            nir_scan(mk_lif("[0.02,0.03]", "[1e8]").as_bytes()),
            Err(NirError::BadShape("LIF param"))
        ));
        // a coherent 2-neuron population scans and counts
        let ok = mk_lif("[0.02,0.03]", "[1e8,1e8]");
        let s = nir_scan(ok.as_bytes()).expect("population scans");
        assert_eq!(s.lif_neurons, 2);

        // assembly parity (reference type-check): population size
        // must equal the feeding Linear's rows
        let mut bld = NirBuilder::new(NirImportOptions::default());
        let inp = bld.add_input("i", &[3]).expect("input");
        let lin = bld.add_linear("l", &[1.0, 1.0, 1.0], 1, 3).expect("linear");
        let lif = bld
            .add_lif_population(
                "n",
                &NirLifParams {
                    tau_s: &[0.02, 0.03],
                    r_ohm: &[1e8, 1e8],
                    v_leak_v: &[-0.07, -0.07],
                    v_threshold_v: &[-0.055, -0.055],
                    v_reset_v: Some(&[-0.08, -0.08]),
                },
            )
            .expect("lif");
        let out = bld.add_output("o", &[1]).expect("output");
        for (a, c) in [(inp, lin), (lin, lif), (lif, out)] {
            bld.add_edge(a, c).expect("edge");
        }
        let g = bld.build().expect("builds — the format layer permits it");
        assert!(matches!(
            g.build_chain_network(),
            Err(NirError::UnsupportedTopology(
                "LIF population != Linear rows"
            ))
        ));
    }

    #[test]
    fn scan_rejects_non_2d_weight_shapes() {
        let mk = |weight: &str| {
            format!(
                "{{\"version\":\"x\",\"node\":{{\"type\":\"NIRGraph\",\"edges\":[],\
                 \"nodes\":{{\"a\":{{\"type\":\"Linear\",\"weight\":{weight}}}}}}}}}"
            )
        };
        // 1-D, empty outer, empty row, empty+full rows, 3-D — all at
        // SCAN now (the "malformed structure fails here" contract;
        // previously 1-D/empty slipped through to import)
        for w in ["[1.0, 2.0]", "[]", "[[]]", "[[],[1.0]]", "[[[1.0]]]"] {
            assert!(
                matches!(
                    nir_scan(mk(w).as_bytes()),
                    Err(NirError::BadShape("weight"))
                ),
                "scan must reject {w}"
            );
        }
        // 2-D non-empty scans; raggedness remains import's check
        assert!(nir_scan(mk("[[1.0],[1.0,2.0]]").as_bytes()).is_ok());
    }

    #[test]
    fn export_rejects_dangling_edge_indices() {
        let (nodes, _edges, weights, lifs, _report) = import_chain(NirImportOptions::default());
        let mut out = [0u8; 512];
        assert_eq!(
            nir_export(
                &nodes,
                &[(0, 99)],
                &weights,
                &lifs,
                NirImportOptions::default(),
                &mut out
            ),
            Err(NirError::BadShape("edges")),
            "edge indices must name nodes — no silent \"?\" placeholders"
        );
    }

    #[test]
    fn encoder_saturates_instead_of_wrapping() {
        // all-max weights × all-max inputs: 3·32767²/100 = 32212250
        // clamps to 32767 — the i32 accumulator produced a NEGATIVE
        // output here before the fix (release mode: a silent wrap)
        let doc = "{\"version\":\"x\",\"node\":{\"type\":\"NIRGraph\",\"edges\":[\
[\"i\",\"l\"],[\"l\",\"n\"],[\"n\",\"o\"]],\"nodes\":{\"i\":{\"type\":\"Input\",\"shape\":[3]},\
\"l\":{\"type\":\"Linear\",\"weight\":[[1.0,1.0,1.0]]},\
\"n\":{\"type\":\"LIF\",\"tau\":[0.02],\"r\":[100000000.0],\"v_leak\":[-0.07],\
\"v_threshold\":[-0.055],\"v_reset\":[-0.08]},\"o\":{\"type\":\"Output\",\"shape\":[1]}}}}";
        let g = NirImport::from_json(doc.as_bytes(), NirImportOptions::default()).expect("imports");
        let (_net, enc) = g.build_chain_network().expect("canonical chain");
        assert_eq!(enc.encode(&[32767, 32767, 32767]), vec![32767]);
        // negative saturation lands on i16::MIN (-32768)
        assert_eq!(enc.encode(&[-32767, -32767, -32767]), vec![-32768]);
        // arithmetic sanity: w·x/100 truncates
        assert_eq!(enc.encode(&[1, 0, 0]), vec![327]);
    }

    // ---- structured-entry seam: quantize_linear / quantize_lif ----

    #[test]
    fn quantize_linear_dyadic_vector_is_exact() {
        // the gate vector, direct: [0.5,-1,0.25] at absmax 1.0
        let vals = [0.5, -1.0, 0.25];
        let mut arena = [0i16; 8];
        let lin = quantize_linear(&vals, 1, 3, &mut arena, 0).expect("quantizes");
        assert_eq!((lin.rows, lin.cols, lin.weight_offset), (1, 3, 0));
        assert!((lin.scale - 1.0 / I16_FS).abs() < 1e-18);
        assert_eq!(&arena[..3], &[16384, -32767, 8192]);
        assert!(
            lin.max_abs_err > 0.0 && lin.max_abs_err <= lin.scale / 2.0,
            "scale 1/32767 is non-dyadic: bounded loss, recorded"
        );
        // offset placement: the record views exactly the slice it wrote
        let lin2 = quantize_linear(&vals, 1, 3, &mut arena, 3).expect("quantizes");
        assert_eq!(lin2.weight_offset, 3);
        assert_eq!(&arena[3..6], &[16384, -32767, 8192]);
        assert_eq!(lin2.scale, lin.scale, "same tensor, same scale");
        // absmax 32767 → scale exactly 1.0: integers are lossless
        let lin3 =
            quantize_linear(&[32767.0, -16384.0, 0.0], 1, 3, &mut arena, 0).expect("quantizes");
        assert_eq!(lin3.scale, 1.0);
        assert_eq!(lin3.max_abs_err, 0.0);
        assert_eq!(&arena[..3], &[32767, -16384, 0]);
    }

    #[test]
    fn quantize_linear_zero_tensor_and_full_scale() {
        let mut arena = [7i16; 4];
        let lin = quantize_linear(&[0.0; 4], 2, 2, &mut arena, 0).expect("quantizes");
        assert!(lin.zero_tensor);
        assert_eq!(lin.scale, 1.0);
        assert_eq!(lin.max_abs_err, 0.0);
        assert_eq!(&arena[..4], &[0, 0, 0, 0]);
        // the max-|w| element maps to ±32767 by construction
        // (1.0/(3/32767) = 10922.33 — far from the .5 boundary)
        let lin2 = quantize_linear(&[3.0, -3.0, 1.0], 1, 3, &mut arena, 0).expect("quantizes");
        assert!(!lin2.zero_tensor);
        assert_eq!(lin2.absmax, 3.0);
        assert_eq!(&arena[..3], &[32767, -32767, 10922]);
    }

    #[test]
    fn quantize_linear_denormal_absmax_is_loud() {
        // absmax/32767 underflows to 0.0: the record would lie
        // (q·scale = 0 ≠ absmax) and export would zero the tensor
        for v in [5e-324, 1e-320, 2.47e-321] {
            let mut arena = [0i16; 4];
            assert_eq!(
                quantize_linear(&[v], 1, 1, &mut arena, 0),
                Err(NirError::BadNumber("weight")),
                "{v}"
            );
        }
    }

    #[test]
    fn quantize_linear_rejects_loudly() {
        let mut arena = [0i16; 8];
        // non-finite (`1e400` reaches this same door via JSON parse)
        for v in [f64::NAN, f64::INFINITY, f64::NEG_INFINITY] {
            assert_eq!(
                quantize_linear(&[1.0, v], 1, 2, &mut arena, 0),
                Err(NirError::BadNumber("weight"))
            );
        }
        // len mismatch / zero dims
        assert_eq!(
            quantize_linear(&[1.0], 1, 2, &mut arena, 0),
            Err(NirError::BadShape("weight"))
        );
        assert_eq!(
            quantize_linear(&[], 0, 3, &mut arena, 0),
            Err(NirError::BadShape("weight"))
        );
        assert_eq!(
            quantize_linear(&[1.0], 1, 0, &mut arena, 0),
            Err(NirError::BadShape("weight"))
        );
        // arena bounds (including offset arithmetic overflow)
        assert_eq!(
            quantize_linear(&[1.0; 6], 2, 3, &mut arena, 4),
            Err(NirError::BufferOverflow)
        );
        assert_eq!(
            quantize_linear(&[1.0; 6], 2, 3, &mut arena, usize::MAX - 2),
            Err(NirError::BufferOverflow)
        );
    }

    use proptest::prelude::*;

    proptest! {
        /// The R9 200k-fuzz invariants, property-pinned: bounded
        /// dequant error, full-scale max element, and
        /// re-quantize(dequant) == q — the core of export
        /// idempotence.
        #[test]
        fn prop_quantize_linear_round_trip(
            rows in 1usize..=4,
            cols in 1usize..=5,
            values in proptest::collection::vec(-1024.0f64..1024.0, 20usize..=20),
        ) {
            let n = rows * cols;
            let values = &values[..n];
            let mut arena = [0i16; 32];
            let lin = quantize_linear(values, rows, cols, &mut arena, 0)
                .expect("finite values quantize");
            prop_assert!(lin.max_abs_err <= lin.scale * (0.5 + 1e-9));
            if lin.zero_tensor {
                prop_assert_eq!(lin.scale, 1.0);
            } else {
                prop_assert!(arena[..n].iter().any(|&q| q.abs() == 32767));
            }
            let deq: Vec<f64> = arena[..n].iter().map(|&q| f64::from(q) * lin.scale).collect();
            let mut arena2 = [0i16; 32];
            quantize_linear(&deq, rows, cols, &mut arena2, 0).expect("re-quantizes");
            prop_assert_eq!(&arena2[..n], &arena[..n]);
        }
    }

    // ---- structured-entry seam: the typed builder ----

    fn lif_params_single() -> NirLifParams<'static> {
        NirLifParams {
            tau_s: &[0.02],
            r_ohm: &[1e8],
            v_leak_v: &[-0.07],
            v_threshold_v: &[-0.055],
            v_reset_v: Some(&[-0.08]),
        }
    }

    fn builder_chain() -> NirImport<'static> {
        let mut bld = NirBuilder::new(NirImportOptions::default());
        let inp = bld.add_input("input", &[3]).expect("input");
        let lin = bld
            .add_linear("linear", &[0.5, -1.0, 0.25, 0.0, 0.75, -0.5], 2, 3)
            .expect("linear");
        let lif = bld
            .add_lif_population(
                "lif",
                &NirLifParams {
                    tau_s: &[0.02, 0.02],
                    r_ohm: &[1e8, 1e8],
                    v_leak_v: &[-0.07, -0.07],
                    v_threshold_v: &[-0.055, -0.055],
                    v_reset_v: Some(&[-0.08, -0.08]),
                },
            )
            .expect("lif");
        let out = bld.add_output("output", &[2]).expect("output");
        for (a, c) in [(inp, lin), (lin, lif), (lif, out)] {
            bld.add_edge(a, c).expect("edge");
        }
        bld.build().expect("builds")
    }

    #[test]
    fn builder_matches_the_json_path_exactly() {
        let built = builder_chain();
        let (jn, je, jw, jl, _rep) = import_chain(NirImportOptions::default());
        // same quantized state, node for node
        assert_eq!(built.edges, je);
        assert_eq!(built.weights, jw);
        assert_eq!(built.lifs, jl, "every LIF record quantized identically");
        for (b, j) in built.nodes.iter().zip(jn.iter()) {
            assert_eq!(b.name, j.name);
            assert_eq!(b.kind, j.kind);
            assert_eq!((b.shape, b.shape_len), (j.shape, j.shape_len));
            assert_eq!(b.lif, j.lif, "population views identical");
            assert_eq!(b.linear, j.linear, "Linear quantized identically");
        }
    }

    #[test]
    fn builder_chain_assembles_and_exports() {
        let g = builder_chain();
        let (mut net, enc) = g.build_chain_network().expect("canonical chain");
        assert_eq!((net.neuron_count(), enc.rows(), enc.cols()), (2, 2, 3));
        let spikes: usize = (0..100)
            .map(|_| net.step(&enc.encode(&[400, 0, 0])).unwrap().len())
            .sum();
        assert!(spikes > 0, "the built chain fires");

        // export → JSON boundary → re-import: state-identical
        let mut out = vec![0u8; 2048];
        let n =
            nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, &mut out).expect("exports");
        let g2 = NirImport::from_json(&out[..n], g.opts).expect("re-imports");
        assert_eq!(g2.weights, g.weights);
        assert_eq!(g2.edges, g.edges);
        assert_eq!(g2.lifs, g.lifs, "every quantized record survives");
    }

    #[test]
    fn ascii_gate_fires_at_export_only() {
        // a non-ASCII name is FINE on the typed surface…
        let mut bld = NirBuilder::new(NirImportOptions::default());
        let inp = bld.add_input("entrée", &[1]).expect("input");
        let lif = bld
            .add_lif_population("lif", &lif_params_single())
            .expect("lif");
        bld.add_edge(inp, lif).expect("edge");
        let g = bld.build().expect("builds — no gate on the typed surface");
        // …and the LIF quantized fine
        let pop = g.nodes[1].lif.unwrap();
        assert_eq!(g.lifs[pop.offset].tau_us, 20_000);
        // …but the JSON container cannot write it
        let mut out = vec![0u8; 2048];
        assert_eq!(
            nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, &mut out),
            Err(NirError::NonAsciiNodeName("entrée"))
        );
        // quotes and escapes are unwritable too (would corrupt the
        // container); the plain-ASCII builder chain exports fine
        let mut q = NirBuilder::new(NirImportOptions::default());
        let qi = q.add_input("a\"b", &[1]).expect("input");
        let qn = q
            .add_lif_population("lif", &lif_params_single())
            .expect("lif");
        q.add_edge(qi, qn).expect("edge");
        let gq = q.build().expect("builds");
        assert_eq!(
            nir_export(
                &gq.nodes,
                &gq.edges,
                &gq.weights,
                &gq.lifs,
                gq.opts,
                &mut out
            ),
            Err(NirError::NonAsciiNodeName("a\"b"))
        );
        let ok = builder_chain();
        assert!(nir_export(
            &ok.nodes,
            &ok.edges,
            &ok.weights,
            &ok.lifs,
            ok.opts,
            &mut out
        )
        .is_ok());
    }

    #[test]
    fn builder_parity_checks_and_rejections() {
        let mut b = NirBuilder::new(NirImportOptions::default());
        let i = b.add_input("a", &[1]).expect("input");
        let o = b.add_output("a", &[1]).expect("output");
        b.add_edge(i, o).expect("edge");
        assert!(matches!(b.build(), Err(NirError::DuplicateNodeName)));

        let mut b2 = NirBuilder::new(NirImportOptions::default());
        let i2 = b2.add_input("a", &[1]).expect("input");
        let o2 = b2.add_output("b", &[1]).expect("output");
        b2.add_edge(i2, o2).expect("edge");
        // shape > 4 dims and edge indices naming no node
        assert_eq!(
            b2.add_input("c", &[1, 2, 3, 4, 5]),
            Err(NirError::BadShape("shape"))
        );
        assert_eq!(b2.add_edge(0, 99), Err(NirError::BadShape("edges")));
        // LIF hard failures propagate from quantize_lif
        assert_eq!(
            b2.add_lif_population(
                "x",
                &NirLifParams {
                    tau_s: &[-0.02],
                    r_ohm: &[1e8],
                    v_leak_v: &[-0.07],
                    v_threshold_v: &[-0.055],
                    v_reset_v: Some(&[-0.08]),
                }
            ),
            Err(NirError::BadNumber("tau"))
        );
        // linear shape mismatch propagates from quantize_linear
        assert_eq!(
            b2.add_linear("y", &[1.0], 2, 3),
            Err(NirError::BadShape("weight"))
        );
        // duplicate edge fires at build (the reference's
        // validate_structure parity)
        b2.add_edge(i2, o2).expect("edge");
        assert!(matches!(b2.build(), Err(NirError::DuplicateEdge)));
    }

    #[test]
    fn failed_adds_leave_no_zombie_state() {
        let mut b = NirBuilder::new(NirImportOptions::default());
        let inp = b.add_input("input", &[3]).expect("input");
        // (a) failed add_linear: NaN weight — quantize_linear
        //     hard-fails AFTER the old push-first shape would have
        //     created the node and zero-extended the arena
        assert_eq!(
            b.add_linear("zombie", &[0.5, f64::NAN, 0.25], 1, 3),
            Err(NirError::BadNumber("weight"))
        );
        // (b) failed add_lif_population: neuron 2's tau ≤ 0 fails
        //     mid-population — neuron 1 quantized fine, the exact
        //     half-appended shape the old push-first code produced
        assert_eq!(
            b.add_lif_population(
                "zombie-lif",
                &NirLifParams {
                    tau_s: &[0.02, -0.02],
                    r_ohm: &[1e8, 1e8],
                    v_leak_v: &[-0.07, -0.07],
                    v_threshold_v: &[-0.055, -0.055],
                    v_reset_v: Some(&[-0.08, -0.08]),
                },
            ),
            Err(NirError::BadNumber("tau"))
        );
        // The builder is still usable and carries no zombie: the
        // identical clean chain (builder_chain's graph) builds to
        // exactly the same state as if the failures never happened.
        let lin = b
            .add_linear("linear", &[0.5, -1.0, 0.25, 0.0, 0.75, -0.5], 2, 3)
            .expect("linear");
        let lif = b
            .add_lif_population(
                "lif",
                &NirLifParams {
                    tau_s: &[0.02, 0.02],
                    r_ohm: &[1e8, 1e8],
                    v_leak_v: &[-0.07, -0.07],
                    v_threshold_v: &[-0.055, -0.055],
                    v_reset_v: Some(&[-0.08, -0.08]),
                },
            )
            .expect("lif");
        let out = b.add_output("output", &[2]).expect("output");
        for (a, c) in [(inp, lin), (lin, lif), (lif, out)] {
            b.add_edge(a, c).expect("edge");
        }
        let g = b.build().expect("builds");
        let clean = builder_chain();
        // no zombie nodes (count + no zombie names)
        assert_eq!(g.nodes.len(), clean.nodes.len());
        assert!(g
            .nodes
            .iter()
            .all(|n| n.name != "zombie" && n.name != "zombie-lif"));
        // no arena tail from the failed linear, no half-appended
        // lif records from the failed population
        assert_eq!(g.weights, clean.weights);
        assert_eq!(g.lifs, clean.lifs);
        assert_eq!(g.edges, clean.edges);
    }
}