gresil 0.1.0-alpha.9

Spiking neural networks in no_std Rust, i16 fixed point, for microcontrollers
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
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
// SPDX-FileCopyrightText: 2026 philo-the-dendron
// SPDX-License-Identifier: AGPL-3.0-or-later
//! 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`
//! files are read outside this crate: by `neuralos-rt`'s `hdf5` feature (the
//! C library) and by `gresil-nir2json` (`hdf5-pure`, no C), the converter
//! that turns a stranger's `.nir` into this JSON.
//!
//! 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`.
//!
//! Each node carries its kind's fields, `type` and `metadata`, and
//! nothing else; the graph object carries `type`, `edges`, `nodes` and
//! `metadata`, the document `version` and `node`. Any other key, or a
//! key given twice, is refused by name ([`NirError::UnexpectedField`],
//! [`NirError::DuplicateField`]), as the reference's `from_dict`,
//! `cls(**kwargs)`, refuses a key its class lacks (at the document, of
//! which the reference's reader takes only `node`, the rule is this
//! module's own): a key skipped in silence is a field this module does
//! not honor, and a misspelled `v_reset` would reset to 0 V.
//! `metadata`'s contents stay free.
//!
//! # 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, and in
//! simulation units the units mark):
//!
//! - **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`,
//!   `tau > 1000·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 `bridge` (behind `unstable-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 node stages
//! its source f64s into [`NirBuffers::scratch`], one node at a time (a
//! Linear its weights, a LIF its parameter arrays), and quantizes them
//! into the weight arena or the lifs buffer, which keep exactly the
//! results; the sizes [`NirBuffers`] names are enough for any graph.
//! Export writes into a caller byte slice. Strings are
//! borrowed from the input (schema strings must be escape-free
//! printable ASCII — loud error otherwise); an error copies the names
//! it reports, so it borrows nothing. `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::{dt_over_tau, 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
// ---------------------------------------------------------------------------

/// A name a [`NirError`] reports (a node's key, a node kind, a field),
/// copied, so that the error borrows nothing from the document or the
/// caller. It keeps 32 bytes at most: a longer name is cut at the last
/// char boundary that fits and marked, so [`is_cut`](Self::is_cut) says
/// so, its `Display` ends with "…", and it equals no `&str`, since it is
/// not the name. Two names compare by the bytes they kept and the mark.
/// `Copy`, no allocation.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct NirName {
    len: u8,
    cut: bool,
    bytes: [u8; NirName::CAP],
}

impl NirName {
    /// The most bytes a name keeps, 32 as the type's doc says; private,
    /// so it can grow in a minor release.
    const CAP: usize = 32;

    /// `name`, whole if it fits 32 bytes; else its bytes up to the last
    /// char boundary that fits, marked cut.
    #[must_use]
    pub fn new(name: &str) -> Self {
        let mut end = name.len().min(Self::CAP);
        while !name.is_char_boundary(end) {
            end -= 1;
        }
        let mut bytes = [0u8; Self::CAP];
        bytes[..end].copy_from_slice(&name.as_bytes()[..end]);
        Self {
            len: end as u8,
            cut: end < name.len(),
            bytes,
        }
    }

    /// The bytes kept: the whole name, or the cut one's first part.
    #[must_use]
    pub fn as_str(&self) -> &str {
        core::str::from_utf8(&self.bytes[..usize::from(self.len)]).unwrap_or("")
    }

    /// Whether the name was longer than 32 bytes, and cut.
    #[must_use]
    pub fn is_cut(&self) -> bool {
        self.cut
    }
}

/// The kept bytes, then "…" when the name was cut.
impl core::fmt::Display for NirName {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.write_str(self.as_str())?;
        if self.cut {
            f.write_str("…")?;
        }
        Ok(())
    }
}

/// The kept bytes as `&str`'s `Debug` writes them, quoted, then "…" when
/// the name was cut.
impl core::fmt::Debug for NirName {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        core::fmt::Debug::fmt(self.as_str(), f)?;
        if self.cut {
            f.write_str("…")?;
        }
        Ok(())
    }
}

/// The name is `other`, whole: a cut name equals no `str`.
impl PartialEq<str> for NirName {
    fn eq(&self, other: &str) -> bool {
        !self.cut && self.as_str() == other
    }
}

/// As `PartialEq<str>`.
impl PartialEq<&str> for NirName {
    fn eq(&self, other: &&str) -> bool {
        *self == **other
    }
}

/// Everything that can go wrong importing/exporting NIR. `Copy`, no
/// allocation, no lifetime: a name it reports is copied into a
/// [`NirName`], so `?` carries it past the document it reports on. Not
/// exhaustive, nor are its four struct variants: a variant, or a field
/// of one, may come in a minor release.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum NirError {
    /// JSON the reader cannot read, at byte `pos`: malformed, nested
    /// deeper than the reader walks, or well-formed with a value of
    /// another JSON type than the schema's (a string where `tau`'s array
    /// goes, say).
    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(NirName),
    /// A node kind outside the slice-1 subset (includes `Affine`,
    /// `CubaLIF`, `Conv1d`, …). Carries the kind name.
    UnsupportedNodeKind(NirName),
    /// 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, a non-finite number, or a
    /// Linear weight [`quantize_linear`] cannot record: an `absmax` so
    /// small that the scale underflows to 0, or f64's largest, whose
    /// scale is finite but whose cell at full scale dequantizes to
    /// infinity, and its `max_abs_err` with it.
    BadNumber(&'static str),
    /// `tau < dt` — the derived decay would be nonsense.
    TauBelowDt,
    /// `tau > 1000·dt`: the leak rate [`dt_over_tau`] truncates to 0,
    /// and a neuron at 0 never moves, neither leaking nor integrating.
    TauTooLongForDt,
    /// Under [`NirUnits::Simulation`], a Linear weight whose true-scale
    /// quanta pass ±32,767: `round(w · SIM_CURRENT_QUANTA)` into a drive
    /// stage, `round(w · 10 · SIM_CURRENT_QUANTA)` on a spiking Linear
    /// edge, so `|w|` up to about 32.767 and 3.2767. Refused, never
    /// clamped.
    WeightOutOfRange,
    /// `v_threshold` quantized to 0 quanta — a deaf neuron.
    ThresholdZero,
    /// A potential quantizes outside `[MEMBRANE_MV_MIN, MEMBRANE_MV_MAX]`,
    /// or, under [`NirUnits::Simulation`], a threshold at
    /// `MEMBRANE_MV_MAX`, named `v_threshold + 1`: stored one quantum
    /// up, it sits past the ceiling, and the membrane never reaches it.
    PotentialOutOfRange(&'static str),
    /// More nodes/edges/weights than the caller's buffers hold.
    BufferOverflow,
    /// An edge endpoint names no node.
    UnknownEdgeEndpoint(NirName),
    /// The same edge appears twice (reference `validate_structure`).
    DuplicateEdge,
    /// A node name appears twice in `nodes`.
    DuplicateNodeName,
    /// A key its object does not carry: a node carries its kind's
    /// fields, `type` and `metadata`; the graph object `type`, `edges`,
    /// `nodes` and `metadata`; the document `version` and `node`. The
    /// reference's `from_dict` is `cls(**kwargs)`, which refuses any
    /// other key, a field of another kind included; at the document, of
    /// which its reader takes only `node`, the rule is this module's.
    #[non_exhaustive]
    UnexpectedField {
        /// The node's key in `nodes`; `None` for the graph object and
        /// the document.
        node: Option<NirName>,
        /// The key.
        field: NirName,
    },
    /// A key given twice in one object. JSON leaves its meaning open
    /// and the reference reads no JSON, so the reader refuses it, as it
    /// refuses a node name or an edge given twice.
    #[non_exhaustive]
    DuplicateField {
        /// The node's key in `nodes`; `None` for the graph object and
        /// the document.
        node: Option<NirName>,
        /// The key.
        field: NirName,
    },
    /// A graph the assembly does not build, though the format layer
    /// imports it. Both builders refuse by name:
    /// `NirImport::build_chain_network` anything but its chain (e.g. a
    /// LIF population whose size ≠ the feeding Linear's rows), and
    /// `NirImport::build_network` each graph it cannot assemble (e.g. no
    /// Input node, or a spike edge on the mV grid, whose message is the
    /// remedy).
    UnsupportedTopology(&'static str),
    /// [`NirUnits::Simulation`] given to the slice-1 chain builder,
    /// `NirImport::build_chain_network`, which reads native units only;
    /// `build_network` assembles such a graph.
    UnsupportedUnits,
    /// A node's units disagree with the import options'. The export
    /// marks each LIF node of a graph in [`NirUnits::Simulation`]
    /// (`"units":"simulation"` in its `metadata.neuralos.quant`); the
    /// import reads a mark on any node, and a LIF node with no mark
    /// reads as [`NirUnits::Native`]: under the other units every
    /// potential, `r` and weight would read on another scale.
    #[non_exhaustive]
    UnitsMismatch {
        /// The units a node of the file is in: its mark, or native
        /// units for a LIF node with none.
        file: NirUnits,
        /// The units the import options read.
        options: NirUnits,
    },
    /// A per-edge type-shape mismatch (reference `check_types`
    /// parity): the `src → dst` edge's tensor shapes disagree.
    #[non_exhaustive]
    EdgeShapeMismatch {
        /// The source node's key in the document.
        src: NirName,
        /// The destination node's key in the document.
        dst: NirName,
    },
    /// 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,
                "JSON the reader cannot read at byte {p}: malformed, nested too deep, \
                 or a value of another JSON type than the schema's"
            ),
            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::TauTooLongForDt => write!(
                f,
                "tau > 1000·dt — the leak rate truncates to 0, a neuron that never moves"
            ),
            Self::WeightOutOfRange => write!(
                f,
                "a Linear weight's quanta pass ±32,767 at true scale (simulation units)"
            ),
            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::UnexpectedField {
                node: Some(n),
                field,
            } => write!(
                f,
                "node '{n}' carries '{field}', a key its kind does not have"
            ),
            Self::UnexpectedField { node: None, field } => {
                write!(
                    f,
                    "'{field}' is not a key of the graph object or the document"
                )
            }
            Self::DuplicateField {
                node: Some(n),
                field,
            } => write!(f, "node '{n}' gives '{field}' twice"),
            Self::DuplicateField { node: None, field } => {
                write!(
                    f,
                    "'{field}' is given twice in the graph object or the document"
                )
            }
            Self::UnsupportedTopology(n) => {
                write!(f, "topology unsupported by slice 1: {n}")
            }
            Self::UnsupportedUnits => write!(
                f,
                "the chain builder reads native units only — build_network takes simulation units"
            ),
            Self::UnitsMismatch { file, options } => write!(
                f,
                "the file is in {} units and the options read {} units — {}",
                units_name(*file),
                units_name(*options),
                units_remedy(*file)
            ),
            Self::EdgeShapeMismatch { src, dst } => write!(
                f,
                "edge shape mismatch: '{src}' -> '{dst}' (reference type-check parity)"
            ),
            Self::ExportTooSmall => write!(f, "export byte buffer too small"),
        }
    }
}

/// A unit system's name in [`NirError::UnitsMismatch`]'s message.
const fn units_name(u: NirUnits) -> &'static str {
    match u {
        NirUnits::Native => "native",
        NirUnits::Simulation => "simulation",
    }
}

/// The options a file in `file` units imports with, copy-pasteable.
const fn units_remedy(file: NirUnits) -> &'static str {
    match file {
        NirUnits::Native => {
            "import it with NirImportOptions::new(dt_us, VoltageResolution::Millivolt), or a \
             graph with a spike edge with NirImportOptions::new(dt_us, \
             VoltageResolution::CentiMillivolt); a gresil-nir2json --sim-units file written \
             before the units mark has none: convert its .nir again"
        }
        NirUnits::Simulation => {
            "import a gresil-nir2json --sim-units file with NirImportOptions::sim_units(dt_us)"
        }
    }
}

impl core::error::Error for NirError {}

// The same pin as the crate `Error`'s, in every configuration (lib.rs
// carries the why).
const _: fn() = || {
    fn a<T: core::error::Error>() {}
    a::<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). The scan and
/// both walks of the import skip a node's field from the field itself,
/// so all three read a field 64 containers deep.
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> {
        if self.peek() == Some(c) {
            self.pos += 1;
            Ok(())
        } else {
            Err(NirError::Json(self.pos))
        }
    }

    fn eat_lit(&mut self, lit: &str) -> Result<(), NirError> {
        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> {
        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> {
        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> {
        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 `}`. A key given twice comes
    /// back each time it occurs: the document, the graph object and a
    /// node refuse the repeat (`see_field`, `NodeKeys`), and a repeat
    /// inside `metadata`, whose contents stay free, is not refused.
    fn object_step(&mut self, first: &mut bool) -> Result<Option<&'a str>, NirError> {
        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: `metadata`, and a node's key no
    /// kind carries, read whole before its node is judged.
    /// 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> {
        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)]
#[non_exhaustive]
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`. Not exhaustive: a field may come in a minor release, so
/// outside this crate a record comes from [`quantize_lif`], or a caller
/// writes one from [`BLANK`](Self::BLANK).
#[derive(Debug, Clone, Copy, PartialEq, Default)]
#[non_exhaustive]
pub struct NirLif {
    // provenance (source units: s, Ω, V)
    /// Source `tau`, seconds.
    pub tau_s: f64,
    /// Source `r`, ohms.
    pub r_ohm: f64,
    /// Source `v_leak`, volts.
    pub v_leak_v: f64,
    /// Source `v_threshold`, volts.
    pub v_threshold_v: f64,
    /// Source `v_reset`, volts.
    pub v_reset_v: f64,
    /// [`NirNote::VResetDefaulted`].
    pub v_reset_defaulted: bool,
    // derived (substrate units)
    /// `tau` on the substrate's clock, μs.
    pub tau_us: u32,
    /// `r` on the substrate's grid, MΩ.
    pub resistance_mohm: u16,
    /// The substrate's own `tau` over its own `r`, pF:
    /// `tau_us / resistance_mohm`, since µs / MΩ is pF. The export
    /// writes those two, so a re-import computes the same C; the
    /// source's is `tau_s / r_ohm`.
    pub capacitance_pf: u16,
    /// [`NirNote::CapacitanceClamped`].
    pub capacitance_clamped: bool,
    /// `v_leak` on the voltage grid, quanta.
    pub leak_q: i16,
    /// `v_threshold` on the voltage grid, quanta.
    pub threshold_q: i16,
    /// `v_reset` on the voltage grid, quanta.
    pub reset_q: i16,
    // quantization record
    /// [`NirNote::TauTruncated`], in seconds.
    pub tau_err_s: f64,
    /// [`NirNote::PotentialTruncated`], in volts.
    pub max_v_err_v: f64,
}

impl NirLif {
    /// A blank record, every number 0 and every flag `false`, as
    /// `NirLif::default()` gives: what a lifs buffer holds before the
    /// import writes it. A `const`, so it fills a `static` buffer, which
    /// `default()`, a function call, cannot.
    ///
    /// ```
    /// use gresil::nir::NirLif;
    ///
    /// static LIFS: [NirLif; 4] = [NirLif::BLANK; 4];
    /// assert_eq!(LIFS[3], NirLif::default());
    /// ```
    pub const BLANK: Self = Self {
        tau_s: 0.0,
        r_ohm: 0.0,
        v_leak_v: 0.0,
        v_threshold_v: 0.0,
        v_reset_v: 0.0,
        v_reset_defaulted: false,
        tau_us: 0,
        resistance_mohm: 0,
        capacitance_pf: 0,
        capacitance_clamped: false,
        leak_q: 0,
        threshold_q: 0,
        reset_q: 0,
        tau_err_s: 0.0,
        max_v_err_v: 0.0,
    };
}

/// 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. Not exhaustive: a field may
/// come in a minor release, so outside this crate a caller writes one
/// from [`BLANK`](Self::BLANK).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct NirLifPopulation {
    /// First record index in the lifs buffer.
    pub offset: usize,
    /// Population size (the per-neuron param array length).
    pub len: usize,
}

impl NirLifPopulation {
    /// A blank view, offset 0 and length 0: its length is written after
    /// it, and its offset when the records do not start at 0. A LIF node
    /// written by hand, as the structured-entry seam builds one where
    /// `NirBuilder` (std) is not there, takes one.
    ///
    /// ```
    /// use gresil::nir::{nir_export, quantize_lif, NirLifPopulation, NirNode, NirNodeKind};
    /// use gresil::{NirImportOptions, VoltageResolution};
    ///
    /// let opts = NirImportOptions::new(1_000, VoltageResolution::Millivolt);
    /// let lif = quantize_lif(0.02, 1e8, -0.07, -0.055, -0.08, false, opts).unwrap();
    /// let mut pop = NirLifPopulation::BLANK;
    /// pop.len = 1; // one record, at offset 0 of the lifs buffer
    /// let mut input = NirNode::BLANK;
    /// input.name = "input";
    /// input.shape[0] = 1;
    /// input.shape_len = 1;
    /// let mut neuron = NirNode::BLANK;
    /// neuron.name = "lif";
    /// neuron.kind = NirNodeKind::Lif;
    /// neuron.lif = Some(pop);
    /// let mut out = [0u8; 1024];
    /// let n = nir_export(&[input, neuron], &[(0, 1)], &[], &[lif], opts, &mut out).unwrap();
    /// assert!(out[..n].starts_with(b"{\"version\""));
    /// ```
    pub const BLANK: Self = Self { offset: 0, len: 0 };
}

/// 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.
///
/// Exhaustive on purpose: a caller builds one by a literal, which puts
/// each of the five slices beside its name, and a sixth field would be a
/// breaking release.
#[derive(Debug, Clone, Copy)]
pub struct NirLifParams<'v> {
    /// Source `tau`, one per neuron, seconds.
    pub tau_s: &'v [f64],
    /// Source `r`, one per neuron, ohms.
    pub r_ohm: &'v [f64],
    /// Source `v_leak`, one per neuron, volts.
    pub v_leak_v: &'v [f64],
    /// Source `v_threshold`, one per neuron, volts.
    pub v_threshold_v: &'v [f64],
    /// Source `v_reset`, one per neuron, volts; `None` as the struct
    /// doc says.
    pub v_reset_v: Option<&'v [f64]>,
}

/// A quantized Linear node: weights live in the shared i16 arena. Not
/// exhaustive: a field may come in a minor release, so outside this crate
/// a view comes from [`quantize_linear`], or a caller writes one from
/// [`BLANK`](Self::BLANK).
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct NirLinear {
    /// The `weight` tensor's out dimension.
    pub rows: usize,
    /// The `weight` tensor's in dimension.
    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,
    /// [`NirNote::ZeroWeightTensor`].
    pub zero_tensor: bool,
}

impl NirLinear {
    /// A blank view: no rows, no cols, offset 0, every number 0, not a
    /// zero tensor. A slot to be overwritten, or a value with a field
    /// written after it.
    ///
    /// ```
    /// use gresil::nir::NirLinear;
    ///
    /// let mut mark = NirLinear::BLANK;
    /// mark.max_abs_err = f64::NAN;
    /// assert_eq!((mark.rows, mark.cols, mark.scale), (0, 0, 0.0));
    /// ```
    pub const BLANK: Self = Self {
        rows: 0,
        cols: 0,
        weight_offset: 0,
        scale: 0.0,
        absmax: 0.0,
        max_abs_err: 0.0,
        zero_tensor: false,
    };
}

/// One node, imported or built. Shapes are `[u32; 4]` + length (≤ 4 dims,
/// reference shapes are 1–4-D). A LIF node carries a population
/// view into the lifs buffer. Not exhaustive: a field may come in a
/// minor release, so outside this crate a caller writes a node from
/// [`BLANK`](Self::BLANK).
#[derive(Debug, Clone, Copy, PartialEq)]
#[non_exhaustive]
pub struct NirNode<'a> {
    /// This node's key in the document's `nodes` object, or the name a
    /// `NirBuilder` caller gave it.
    pub name: &'a str,
    /// Which of the four slice-1 kinds: the one the document's `type`
    /// named, or the one the `NirBuilder` method makes.
    pub kind: NirNodeKind,
    /// The document's `shape`, or the builder caller's, its first
    /// `shape_len` entries used.
    pub shape: [u32; 4],
    /// Dimensions carried in `shape` (0 to 4).
    pub shape_len: usize,
    /// The population view of a `LIF` node, `None` on any other kind.
    pub lif: Option<NirLifPopulation>,
    /// The quantized tensor of a `Linear` node, `None` on any other kind.
    pub linear: Option<NirLinear>,
}

impl NirNode<'_> {
    /// A blank node: an `Input` with an empty name, no shape and no view,
    /// what a node buffer holds before the import writes it. A `const`,
    /// so it fills a `static` arena.
    ///
    /// ```
    /// use gresil::nir::{NirNode, NirNodeKind};
    ///
    /// static NODES: [NirNode<'static>; 4] = [NirNode::BLANK; 4];
    /// assert_eq!(NODES[3].kind, NirNodeKind::Input);
    /// assert_eq!((NODES[3].name, NODES[3].shape_len), ("", 0));
    /// assert!(NODES[3].lif.is_none() && NODES[3].linear.is_none());
    /// ```
    pub const BLANK: Self = Self {
        name: "",
        kind: NirNodeKind::Input,
        shape: [0; 4],
        shape_len: 0,
        lif: None,
        linear: None,
    };
}

/// Import notes (loud lossiness — each counted, none silent).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(usize)]
#[non_exhaustive]
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,
}

/// The loud import report. The import builds it and a caller reads it.
/// Not exhaustive: a field may come in a minor release.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct NirReport {
    /// `Input` nodes.
    pub inputs: usize,
    /// `Output` nodes.
    pub outputs: usize,
    /// `Linear` nodes.
    pub linears: usize,
    /// `LIF` nodes; [`NirScan::lif_neurons`] counts the neurons.
    pub lifs: usize,
    /// Entries of the document's `edges` list.
    pub edges: usize,
    /// Cells written into the weight arena.
    pub weight_cells: usize,
    /// One counter per [`NirNote`]; the accessor is `notes()`.
    notes: NoteCounts,
}

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

    /// How many times the import noted `n`, 0 when it never did.
    #[must_use]
    pub fn notes(&self, n: NirNote) -> usize {
        self.notes.get(n)
    }

    fn note(&mut self, n: NirNote) {
        *self.notes.slot(n) += 1;
    }
}

/// The report's note counters, one named field per [`NirNote`], and the
/// guard that a new note does not compile without its counter. `slot`
/// and `get` are each one exhaustive `match` on `NirNote`, which
/// `#[non_exhaustive]` does not loosen inside this crate, so a new
/// variant fails both until each has its arm; `total` destructures every
/// field with no `..`, so a new field it leaves out fails too. No array,
/// so no length to keep in step with the variants.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
struct NoteCounts {
    v_reset_defaulted: usize,
    capacitance_clamped: usize,
    tau_truncated: usize,
    potential_truncated: usize,
    zero_weight_tensor: usize,
    quantization_loss: usize,
}

impl NoteCounts {
    fn slot(&mut self, n: NirNote) -> &mut usize {
        match n {
            NirNote::VResetDefaulted => &mut self.v_reset_defaulted,
            NirNote::CapacitanceClamped => &mut self.capacitance_clamped,
            NirNote::TauTruncated => &mut self.tau_truncated,
            NirNote::PotentialTruncated => &mut self.potential_truncated,
            NirNote::ZeroWeightTensor => &mut self.zero_weight_tensor,
            NirNote::QuantizationLoss => &mut self.quantization_loss,
        }
    }

    fn get(&self, n: NirNote) -> usize {
        match n {
            NirNote::VResetDefaulted => self.v_reset_defaulted,
            NirNote::CapacitanceClamped => self.capacitance_clamped,
            NirNote::TauTruncated => self.tau_truncated,
            NirNote::PotentialTruncated => self.potential_truncated,
            NirNote::ZeroWeightTensor => self.zero_weight_tensor,
            NirNote::QuantizationLoss => self.quantization_loss,
        }
    }

    fn total(&self) -> usize {
        let Self {
            v_reset_defaulted,
            capacitance_clamped,
            tau_truncated,
            potential_truncated,
            zero_weight_tensor,
            quantization_loss,
        } = *self;
        v_reset_defaulted
            + capacitance_clamped
            + tau_truncated
            + potential_truncated
            + zero_weight_tensor
            + quantization_loss
    }
}

/// First pass: buffer sizes + the version string. [`nir_scan`] builds
/// it and a caller reads it. Not exhaustive: a field may come in a minor
/// release.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct NirScan<'a> {
    /// The document's `version` string, borrowed from the JSON.
    pub version: &'a str,
    /// Entries of the document's `nodes` object.
    pub node_count: usize,
    /// Entries of the document's `edges` list.
    pub edge_count: usize,
    /// Cells the weight arena needs: the elements of every node's
    /// `weight`, exact for a document the import takes. A node of a kind
    /// outside the subset, or with no `type`, still counts its `weight`,
    /// and the import refuses the document: overprovision only, never
    /// under.
    pub weight_cells: usize,
    /// Total per-neuron LIF records (`sum` of the param array lengths),
    /// exact for a document the import takes. A node of a kind outside
    /// the subset, or with no `type`, still counts its `tau`-like arrays,
    /// and the import refuses the document: 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.
///
/// Exhaustive on purpose: a caller lends its five buffers by a literal,
/// each beside its name, and a sixth field would be a breaking release.
#[derive(Debug)]
pub struct NirBuffers<'buf, 'a> {
    /// Nodes in document order: the slice is the caller's (`'buf`), and
    /// the names borrow the JSON (`'a`).
    pub nodes: &'buf mut [NirNode<'a>],
    /// Edges as `(from, to)` indices into `nodes`.
    pub edges: &'buf mut [(u32, u32)],
    /// The shared i16 weight arena, `NirLinear` views into it.
    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 three things NIR does not carry that the
/// substrate needs. Not exhaustive: a field may come in a minor release,
/// so outside this crate no struct literal builds options:
/// [`new`](Self::new) and [`sim_units`](Self::sim_units) do.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct NirImportOptions {
    /// Simulation timestep (μs). Hard requirement: `dt ≤ tau ≤ 1000·dt`
    /// (`TauBelowDt` below it, `TauTooLongForDt` above it).
    pub dt_us: u32,
    /// Voltage grid the potentials quantize onto.
    pub resolution: VoltageResolution,
    /// How the graph's numbers read: [`NirUnits`].
    pub units: NirUnits,
}

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

    /// Options for a graph in simulation units, as
    /// `gresil-nir2json --sim-units` writes one: the centi-mV grid and
    /// [`NirUnits::Simulation`].
    #[must_use]
    pub const fn sim_units(dt_us: u32) -> Self {
        Self {
            dt_us,
            resolution: VoltageResolution::CentiMillivolt,
            units: NirUnits::Simulation,
        }
    }
}

/// How an imported graph's numbers read. NIR carries no units, so the
/// caller says: [`NirImportOptions::new`] gives
/// [`Native`](Self::Native), [`NirImportOptions::sim_units`] gives
/// [`Simulation`](Self::Simulation). The export marks a graph in
/// simulation units, and the JSON import refuses a file whose units
/// are not the options' ([`NirError::UnitsMismatch`]).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum NirUnits {
    /// The document's own units, seconds, ohms and volts, under the
    /// substrate's named conventions: each Linear scaled by its own
    /// absmax, the substrate's firing rule `v ≥ threshold`, a refractory
    /// period of 1 ms. No numeric-parity claim.
    Native,
    /// The simulation-unit convention of snnTorch, after
    /// `gresil-nir2json --sim-units` has read the potentials and `r`. A
    /// Linear weight `w` keeps its true scale,
    /// [`SIM_CURRENT_QUANTA`] current quanta per unit, and a weight
    /// whose quanta pass ±32,767 is refused
    /// ([`NirError::WeightOutOfRange`]). A current summed in one step,
    /// the drive a stage sums or the pulses a neuron receives, is not
    /// refused at import: past `i16`, 32.767 units, it saturates at run
    /// time, as a native one does. The threshold is stored one quantum
    /// up, so the substrate's `v ≥ threshold` fires on NIR's
    /// `v > v_threshold` (membranes are integers), and a threshold at
    /// the membrane's ceiling, which could then never fire, is refused
    /// ([`NirError::PotentialOutOfRange`]). The refractory period is 0,
    /// and adaptation is off, since NIR's LIF has neither.
    Simulation,
}

/// Current quanta per simulation unit under [`NirUnits::Simulation`]. A
/// Linear weight `w` into a drive stage is `round(w · SIM_CURRENT_QUANTA)`
/// quanta per unit of input; a spike through a spiking Linear edge
/// delivers the same, and a spike through a direct LIF→LIF edge one unit
/// (the std weight is ten times that, under the assembly's synaptic
/// divisor 10). `gresil-nir2json --sim-units` divides `r` by it, so
/// the product `r·I` keeps the source's scale.
pub const SIM_CURRENT_QUANTA: i16 = 1_000;

// ---------------------------------------------------------------------------
// 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> {
    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::TauTooLongForDt`]; [`NirError::ThresholdZero`];
/// [`NirError::PotentialOutOfRange`], under [`NirUnits::Simulation`]
/// also for a threshold at the membrane's ceiling.
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> {
    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);
    }
    if dt_over_tau(opts.dt_us, tau_us) == 0 {
        return Err(NirError::TauTooLongForDt);
    }
    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);
    }
    // simulation units store it one quantum up (`substrate_neuron`):
    // at the ceiling, that is a threshold the membrane never reaches
    let stored_q = match opts.units {
        NirUnits::Native => i32::from(threshold_q),
        NirUnits::Simulation => i32::from(threshold_q) + 1,
    };
    if stored_q > i32::from(MEMBRANE_MV_MAX) * s {
        return Err(NirError::PotentialOutOfRange("v_threshold + 1"));
    }
    let (reset_q, e3) = quant_potential(v_reset_v, "v_reset", s)?;

    // C = tau/r on the substrate's own integers (µs / MΩ is pF): the
    // export writes tau_us and r_mohm, so a re-import computes the same
    // C; the clamp is informational
    let c_pf_f = f64::from(tau_us) / r_mohm;
    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")`](NirError::BadShape) 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, a `scale` that underflows to 0 (denormal
/// `absmax` — the record would lie, per the R3 review finding), or an
/// `absmax` of f64's largest, whose cell at full scale, `32767·scale`,
/// rounds to infinity.
pub fn quantize_linear(
    values: &[f64],
    rows: usize,
    cols: usize,
    arena: &mut [i16],
    offset: usize,
) -> Result<NirLinear, NirError> {
    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"));
    }
    // f64's largest as `absmax`: the scale is finite, but the cell at
    // full scale dequantizes to 32767·scale, which rounds to infinity,
    // so `max_abs_err` would be infinite and the export, which writes
    // q·scale, would refuse its own import. Loud too. No smaller
    // `absmax` rounds that far: one double below, it holds.
    if !(I16_FS * scale).is_finite() {
        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,
    })
}

// ---------------------------------------------------------------------------
// The keys each object carries: the reference's fields, nothing else
// ---------------------------------------------------------------------------

/// The document's keys: the reference's file holds `version` and `node`.
const DOCUMENT_FIELDS: [&str; 2] = ["version", "node"];

/// The graph object's keys, as `NIRGraph.to_dict` writes them.
const GRAPH_FIELDS: [&str; 4] = ["type", "edges", "nodes", "metadata"];

/// Every key a slice-1 node may carry: bit `i` of [`NodeKeys`]'s
/// `seen` stands for `NODE_FIELDS[i]`.
const NODE_FIELDS: [&str; 9] = [
    "type",
    "shape",
    "weight",
    "tau",
    "r",
    "v_leak",
    "v_threshold",
    "v_reset",
    "metadata",
];

/// A slice-1 kind and its keys: its fields as the reference's `to_dict`
/// writes them (`shape` for Input and Output), `type` and `metadata`.
/// `None` for any other kind, which the import refuses by name.
fn kind_fields(kind: &str) -> Option<(NirNodeKind, &'static [&'static str])> {
    match kind {
        "Input" => Some((NirNodeKind::Input, &["type", "shape", "metadata"])),
        "Output" => Some((NirNodeKind::Output, &["type", "shape", "metadata"])),
        "Linear" => Some((NirNodeKind::Linear, &["type", "weight", "metadata"])),
        "LIF" => Some((
            NirNodeKind::Lif,
            &[
                "type",
                "tau",
                "r",
                "v_leak",
                "v_threshold",
                "v_reset",
                "metadata",
            ],
        )),
        _ => None,
    }
}

/// One key of the document or the graph object, judged as it is read:
/// refused when `fields` lacks it, or when `seen` holds it already.
fn see_field(seen: &mut u8, fields: &[&str], key: &str) -> Result<(), NirError> {
    let i = fields
        .iter()
        .position(|&f| f == key)
        .ok_or_else(|| NirError::UnexpectedField {
            node: None,
            field: NirName::new(key),
        })?;
    if *seen & (1 << i) != 0 {
        return Err(NirError::DuplicateField {
            node: None,
            field: NirName::new(key),
        });
    }
    *seen |= 1 << i;
    Ok(())
}

/// The keys one node object carries, in any order: `type` can come after
/// the fields (the reference's LIF writes it last), so they are judged
/// once the object is read and its kind is known.
#[derive(Default)]
struct NodeKeys<'a> {
    /// Bit `i`: `NODE_FIELDS[i]` seen.
    seen: u16,
    /// A key given twice, if any.
    twice: Option<&'a str>,
    /// A key no slice-1 kind carries, if any.
    unknown: Option<&'a str>,
}

impl<'a> NodeKeys<'a> {
    fn see(&mut self, key: &'a str) {
        match NODE_FIELDS.iter().position(|&f| f == key) {
            Some(i) => {
                if self.seen & (1 << i) != 0 {
                    self.twice.get_or_insert(key);
                }
                self.seen |= 1 << i;
            }
            None => {
                self.unknown.get_or_insert(key);
            }
        }
    }

    /// The node's keys against its kind's, `own`: refuses a key given
    /// twice, a key no kind carries or a key of another kind. Which one
    /// is named when a node has several is not part of the contract.
    fn check(&self, node: &'a str, own: &[&str]) -> Result<(), NirError> {
        if let Some(field) = self.twice {
            return Err(NirError::DuplicateField {
                node: Some(NirName::new(node)),
                field: NirName::new(field),
            });
        }
        if let Some(field) = self.unknown {
            return Err(NirError::UnexpectedField {
                node: Some(NirName::new(node)),
                field: NirName::new(field),
            });
        }
        match NODE_FIELDS
            .iter()
            .enumerate()
            .find(|&(i, f)| self.seen & (1 << i) != 0 && !own.contains(f))
        {
            Some((_, &field)) => Err(NirError::UnexpectedField {
                node: Some(NirName::new(node)),
                field: NirName::new(field),
            }),
            None => Ok(()),
        }
    }
}

// ---------------------------------------------------------------------------
// 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, a key its object does not
/// carry and a key given twice included); param values are NOT
/// validated — that is import's job, as is a node kind outside the
/// subset.
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;
    let mut seen = 0u8;
    while let Some(key) = r.object_step(&mut first)? {
        see_field(&mut seen, &DOCUMENT_FIELDS, key)?;
        if key == "version" {
            version = Some(r.read_string()?);
        } else {
            saw_node = true;
            scan_graph(
                &mut r,
                &mut node_count,
                &mut edge_count,
                &mut weight_cells,
                &mut lif_neurons,
            )?;
        }
    }
    // 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> {
    let mut first = true;
    let mut seen = 0u8;
    while let Some(key) = r.object_step(&mut first)? {
        see_field(&mut seen, &GRAPH_FIELDS, key)?;
        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 let Some(name) = r.object_step(&mut nfirst)? {
                    scan_node(r, name, weight_cells, lif_neurons)?;
                    *node_count += 1;
                }
            }
            _ => r.skip_value(0)?, // `metadata`, its contents free
        }
    }
    Ok(())
}

fn scan_node<'a>(
    r: &mut Reader<'a>,
    name: &'a str,
    weight_cells: &mut usize,
    lif_neurons: &mut usize,
) -> Result<(), NirError> {
    // 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 unequal = false;
    let mut kind: Option<&'a str> = None;
    let mut keys = NodeKeys::default();
    let mut first = true;
    while let Some(key) = r.object_step(&mut first)? {
        keys.see(key);
        match key {
            "type" => kind = Some(r.read_string()?),
            "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 => unequal = true,
                    Some(_) => {}
                }
            }
            _ => r.skip_value(0)?,
        }
    }
    // the keys before the lengths, so a field on the wrong kind is
    // refused by name; a kind outside the subset, or none, is the
    // import's to refuse
    if let Some((_, own)) = kind.and_then(kind_fields) {
        keys.check(name, own)?;
    }
    if unequal {
        return Err(NirError::BadShape("LIF param"));
    }
    *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> {
    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> {
    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. A units mark, on any node, and
/// the native units of a LIF node with none must be `opts.units`
/// ([`NirError::UnitsMismatch`]). A key its object does not carry, or
/// one given twice, is refused by name ([`NirError::UnexpectedField`],
/// [`NirError::DuplicateField`]), a kind outside the subset first
/// ([`NirError::UnsupportedNodeKind`]). An Input's or an Output's
/// `shape` holds 1 to 4 dims: empty or past 4 it is
/// [`NirError::BadShape("shape")`](NirError::BadShape), as the builder
/// refuses one, and absent
/// [`NirError::MissingField("shape")`](NirError::MissingField). 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> {
    let mut report = NirReport::default();

    // walk 1: nodes (+ weights + LIF populations), and each key of the
    // document and the graph object judged as it is read
    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;
        let mut seen = 0u8;
        while let Some(key) = r.object_step(&mut first)? {
            see_field(&mut seen, &DOCUMENT_FIELDS, key)?;
            if key == "node" {
                let mut gfirst = true;
                let mut gseen = 0u8;
                while let Some(gkey) = r.object_step(&mut gfirst)? {
                    see_field(&mut gseen, &GRAPH_FIELDS, gkey)?;
                    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_else(|| {
                                        NirError::UnknownEdgeEndpoint(NirName::new(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 if gkey == "nodes" {
                        // field by field, as walk 1 reads them: skipped
                        // whole, a node's field would sit two levels down
                        // and nest two containers less than walk 1 reads
                        let mut nfirst = true;
                        while r.object_step(&mut nfirst)?.is_some() {
                            let mut ffirst = true;
                            while r.object_step(&mut ffirst)?.is_some() {
                                r.skip_value(0)?;
                            }
                        }
                    } 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> {
    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> {
    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,
    })
}

/// A node's units mark, `neuralos.quant.units` in its `metadata`: the
/// export writes `"simulation"` on each LIF node of a graph in
/// [`NirUnits::Simulation`], and nothing in native units. `None` when
/// the node carries no mark; any other value is refused. The rest of
/// `metadata` is skipped, whatever its shape.
fn read_units_mark(r: &mut Reader<'_>) -> Result<Option<NirUnits>, NirError> {
    let mut mark = None;
    if r.peek() != Some(b'{') {
        r.skip_value(0)?;
        return Ok(mark);
    }
    let mut first = true;
    while let Some(key) = r.object_step(&mut first)? {
        if key != "neuralos" || r.peek() != Some(b'{') {
            r.skip_value(1)?;
            continue;
        }
        let mut nfirst = true;
        while let Some(nkey) = r.object_step(&mut nfirst)? {
            if nkey != "quant" || r.peek() != Some(b'{') {
                r.skip_value(2)?;
                continue;
            }
            let mut qfirst = true;
            while let Some(qkey) = r.object_step(&mut qfirst)? {
                if qkey == "units" {
                    if r.read_string()? != "simulation" {
                        return Err(NirError::BadShape("units"));
                    }
                    mark = Some(NirUnits::Simulation);
                } else {
                    r.skip_value(3)?;
                }
            }
        }
    }
    Ok(mark)
}

/// An Input's or an Output's shape, once the walk has refused one past 4
/// dims: absent, a missing field; given and empty, a bad shape, as
/// `NirBuilder` refuses one.
fn carrier_shape(given: bool, len: usize) -> Result<(), NirError> {
    if !given {
        return Err(NirError::MissingField("shape"));
    }
    if len == 0 {
        return Err(NirError::BadShape("shape"));
    }
    Ok(())
}

// 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> {
    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;
    // given and empty is a bad shape, absent a missing field
    let mut shape_given = false;
    // 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 mark: Option<NirUnits> = None;
    let mut keys = NodeKeys::default();

    let mut first = true;
    while let Some(key) = r.object_step(&mut first)? {
        keys.see(key);
        match key {
            "type" => kind = Some(r.read_string()?),
            "shape" => {
                shape_given = true;
                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;
            }
            "metadata" => mark = read_units_mark(r)?,
            _ => r.skip_value(0)?, // judged below, once the kind is known
        }
    }

    let kind = kind.ok_or(NirError::MissingField("type"))?;
    // a LIF node with no units mark reads as native units
    let file = match (kind, mark) {
        (_, Some(units)) => Some(units),
        ("LIF", None) => Some(NirUnits::Native),
        (_, None) => None,
    };
    if let Some(file) = file.filter(|&units| units != opts.units) {
        return Err(NirError::UnitsMismatch {
            file,
            options: opts.units,
        });
    }
    // the kind before the keys, so a kind outside the subset is refused
    // by name whatever keys it carries; the keys before any staged value
    // is used, since a stray `weight` stages over a LIF's fields
    let (node_kind, own) =
        kind_fields(kind).ok_or_else(|| NirError::UnsupportedNodeKind(NirName::new(kind)))?;
    keys.check(name, own)?;
    bufs.nodes[idx].shape = shape;
    bufs.nodes[idx].shape_len = shape_len;
    bufs.nodes[idx].kind = match node_kind {
        NirNodeKind::Input => {
            carrier_shape(shape_given, shape_len)?;
            report.inputs += 1;
            NirNodeKind::Input
        }
        NirNodeKind::Output => {
            carrier_shape(shape_given, shape_len)?;
            report.outputs += 1;
            NirNodeKind::Output
        }
        NirNodeKind::Linear => {
            if bufs.nodes[idx].linear.is_none() {
                return Err(NirError::MissingField("weight"));
            }
            report.linears += 1;
            NirNodeKind::Linear
        }
        NirNodeKind::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
        }
    };
    *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> {
    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> {
        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> {
        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> {
    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> {
    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`. In
/// [`NirUnits::Simulation`] each LIF node's quant record carries the
/// units mark, `"units":"simulation"`, which [`nir_import`] checks.
/// 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(
    nodes: &[NirNode<'_>],
    edges: &[(u32, u32)],
    weights: &[i16],
    lifs: &[NirLif],
    opts: NirImportOptions,
    out: &mut [u8],
) -> Result<usize, NirError> {
    for node in nodes {
        if !name_is_writable(node.name) {
            return Err(NirError::NonAsciiNodeName(NirName::new(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> {
    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)?;
            // the units mark (`read_units_mark`): a native export keeps
            // its bytes, since no mark reads as native units
            match opts.units {
                NirUnits::Native => {}
                NirUnits::Simulation => w.push_str(",\"units\":\"simulation\"")?,
            }
            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) natively and at
    //!   true scale in simulation units. At step time the encoder
    //!   reads each feature in [`Thousandths`] of a unit and divides
    //!   each stage's sum by 1,000 and the substrate's gain,
    //!   [`LINEAR_GAIN_DIVISOR`], truncating, or in simulation units
    //!   ([`NirUnits::Simulation`], whose quanta are already true
    //!   scale) by 1,000 alone, rounding half away from zero; the
    //!   stages merge 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)`, or `10 · SIM_CURRENT_QUANTA` in simulation
    //!   units, per neuron pair (identity element mapping — the
    //!   reference defines no element semantics at this sha); a
    //!   LIF→Linear→…→LIF path is the spiking Linear edge
    //!   (D8, [`LINEAR_GAIN_DIVISOR`]): the same fused walk with the
    //!   LIF as its root, one synapse per nonzero composed weight, or
    //!   per nonzero true-scale std weight in simulation units;
    //!   Input→Linear is the encoder entry; LIF→Output is
    //!   shape-checked decoration.
    //! - **Plasticity is FROZEN at assembly** (`NIR has no plasticity
    //!   term` — STDP would move an edge's pulse weight on its first
    //!   update, inside its `[0, 2000]` bounds); recorded in the report.
    //! - **Grid:** any graph with a LIF→LIF edge assembles only on
    //!   [`VoltageResolution::CentiMillivolt`] options — a 20 pA edge
    //!   pulse is dead 10× over on mV — and D8 extends the rule to any
    //!   graph with a LIF→Linear edge. 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, self-loop,
    //!   Output-as-source), the readout (a Linear a spike reaches,
    //!   with an Output successor), a spiking Linear edge onto its own
    //!   neuron, 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, NirName,
        NirNode, NirNodeKind, NirUnits, NIR_REF_SHA, SIM_CURRENT_QUANTA,
    };
    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 pA, divided by the network's
    /// `synaptic_input_divisor` (default 10 → a 20 pA 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 pA** | trunc(I/2) = 0 ⇒ **2 pA** |
    /// | one 20 pA 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: at the default divisor the 1,639th coincident pulse
    /// saturates the i16 accumulator (1,638 × 20 pA is 32,760);
    /// plasticity is FROZEN at NIR assembly (NIR has no plasticity
    /// term — STDP would move the pulse's weight on its first update,
    /// inside its `[0, 2000]` bounds), 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 pA pulse is dead 10× over on the mV grid. Explicit mV
    /// options on such a graph are rejected BY NAME with the remedy.
    ///
    /// Native units only. Under [`NirUnits::Simulation`] a spike through
    /// a LIF→LIF edge delivers one unit, a std weight of
    /// `10 · SIM_CURRENT_QUANTA`, NIR's "1 held for one step".
    pub const EDGE_PULSE_QUANTA: i16 = 200;

    /// **D8, the spiking Linear edge (decided 2026-09-14, PR G).**
    ///
    /// A spike on a `LIF→Linear→…→LIF` path is the value 1 into the
    /// Linear chain. The pulse it delivers to post neuron `i` from pre
    /// neuron `j` is `trunc(q_ij / LINEAR_GAIN_DIVISOR)` pA, where `q`
    /// is the composed chain's quantized i16 weight: the chain's
    /// matrices composed in f64 from the LIF and quantized ONCE by
    /// [`quantize_linear`](super::quantize_linear), the D2 fusion code
    /// and record of the drive side (one Linear is a chain of one). The
    /// pulse is delivered one step later and accumulates saturating-i16
    /// across fan-in, plasticity frozen at assembly, as a D1 pulse does.
    /// A zero `q` makes no synapse; a nonzero `q_ii` from a population
    /// back onto itself would be a self-synapse and rejects by name.
    /// `q` is scaled by the composed matrix's own absmax, as each drive
    /// stage is (D6's note), so a chain's largest weight always maps to
    /// ±32767: a 1×1 chain delivers ±327 pA whatever its nonzero weight.
    ///
    /// The divisor is the substrate's encoder gain: both drive encoders
    /// read this constant, so an input feature of 1 and a spike of 1
    /// through the same Linear give the same pA. In the std network the
    /// edge is one synapse per nonzero `(j, i)` of weight
    /// `trunc(q_ij / 10)` under the assembly's divisor 10, and the
    /// step's `weight / 10` is `trunc(trunc(q/10)/10) = trunc(q/100)`
    /// for every i16 `q` (truncation toward zero composes; pinned over
    /// all 65,536 values), which `FixedSynapse::from_network` carries.
    /// The band: a `q` with `0 < |q| < LINEAR_GAIN_DIVISOR` still makes
    /// its synapse, which delivers 0 pA (its std weight is already 0
    /// under 10), kept on purpose, since an input feature of 1 through
    /// the same `q` gives 0 pA too and the nonzero composed weights are
    /// the graph's topology, which the visualizer draws and STDP moves
    /// but never adds to.
    ///
    /// The grid: D1's rule extends to it. A graph with a LIF→Linear
    /// edge assembles only under `CentiMillivolt` import options;
    /// explicit mV options reject BY NAME with the remedy.
    ///
    /// Deferred, by name: `LIF→Linear→Output`, the readout to Output,
    /// met by any Linear a spike reaches that has an Output successor,
    /// whatever its other successors (an Input-rooted Linear with an
    /// Output beside its LIF stays legal, decoration as before D8);
    /// `Input→LIF` (direct drive); encoder-only graphs. The freeze keeps
    /// the band's zero-pulse synapses too (check 8 of `docs/ROADMAP.md`
    /// § 0.1.0, PR I): 92 of 9,409 on a 97×97 Gaussian layer
    /// (`torch.randn`, seed 0), 0 of 1 on both assembling fixtures, each
    /// 6 bytes of the capacity bar and about 122 ns a step on the C3
    /// (`evidence/esp32c3-bringup/README.md` § Ninth entry). Kept, since
    /// `FixedNetwork::try_from` holds `S` equal to the network's synapse
    /// count, one freezer writes the library's cases and a stranger's,
    /// and `S` is the graph's topology, the count the visualizer shows;
    /// a drop would need a fixture that assembles with a band of a tenth
    /// of its `S`.
    ///
    /// Native units only. Under [`NirUnits::Simulation`] a weight keeps
    /// its true scale: the std weight is `round(w · 10 · SIM_CURRENT_QUANTA)`,
    /// so a spike delivers `w` units truncated to one quantum; a weight
    /// that rounds to a std weight of 0 makes no synapse, and the band is
    /// the std weights ±1 to ±9, whose pulse truncates to 0. A weight
    /// whose std weight passes ±32,767 is refused,
    /// [`NirError::WeightOutOfRange`].
    pub const LINEAR_GAIN_DIVISOR: i16 = 100;

    /// D8's std weight divisor: [`LINEAR_GAIN_DIVISOR`] over the
    /// assembly's `synaptic_input_divisor`, the network's default 10,
    /// which `build_network` never changes. Native units only:
    /// `a_two_linear_chain_after_a_lif_is_fused_once` and
    /// `an_input_feature_and_a_spike_through_one_linear_give_the_same_pa`
    /// pin the pulse end to end; the witnesses import in simulation
    /// units, which never read it.
    const D8_WEIGHT_DIVISOR: i16 = LINEAR_GAIN_DIVISOR / 10;

    #[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(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.refractory_period_us = 1_000;
            n.resistance_mohm = 100;
            n.noise_amplitude_pa = 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 pA ⇒ +1500 centi / +15 mV — exactly threshold);
        /// the `EDGE_PULSE_QUANTA` synapse (200 / default divisor 10 =
        /// a 20 pA 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.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 pA 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, Thousandths,
            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. On both
        /// grids, so each builder's is read: the mV one and the centi-mV
        /// one.
        #[test]
        fn chain_equivalence_both_builders_bit_exact() {
            chain_equivalence(NirImportOptions::new(1_000, VoltageResolution::Millivolt));
            chain_equivalence(centi());
        }

        fn chain_equivalence(opts: NirImportOptions) {
            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, a whole input in
            // thousandths
            for x in [[4, 0, 0], [100, -50, 25], [0, 0, 0], [-32768, 32767, 1]] {
                let units = x.map(|v| Thousandths(i32::from(v) * 1_000));
                assert_eq!(enc1.encode(&x), enc2.encode(&[&units]), "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_000, 0, 0].map(Thousandths)]))
                        .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::new(1_000, VoltageResolution::Millivolt),
            )
            .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_000, 0, 0].map(Thousandths)]))
                    .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::new(1_000, VoltageResolution::Millivolt),
            )
            .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_000, 0, 0].map(Thousandths)]))
                    .unwrap()
                {
                    firsts[s.neuron_id as usize] = firsts[s.neuron_id as usize].min(t);
                }
            }
            assert_eq!(firsts, vec![3, 14, 1, 3]);
        }

        /// Two LIF neurons for the stage tests below, at the
        /// parameters `--sim-units` writes, which import in either units.
        fn lif2(b: &mut NirBuilder<'_>) -> usize {
            b.add_lif_population(
                "lif",
                &NirLifParams {
                    tau_s: &[0.005; 2],
                    r_ohm: &[5e8; 2],
                    v_leak_v: &[0.0; 2],
                    v_threshold_v: &[0.01; 2],
                    v_reset_v: Some(&[0.0; 2]),
                },
            )
            .expect("lif")
        }

        /// A root that reaches one Linear twice, directly and through
        /// another Linear (a skip), arrives as the sum of both paths: the
        /// stage is `W_l·(I + W_p)` of the dequantized weights, whichever
        /// of the two edges into `l` the graph lists first. Natively a
        /// stage is quantized against its own absmax; in simulation units
        /// it keeps its true scale. Both are pinned as the stored identity
        /// gave them (round 56): the source tensors' product would end in
        /// 10,922 natively.
        #[test]
        fn a_skip_sums_the_direct_path_and_the_composed_one() {
            let units = [
                (centi(), [32_767, 13_107, -9_830, 10_923]),
                (NirImportOptions::sim_units(100), [1_875, 750, -563, 625]),
            ];
            for (opts, want) in units {
                for direct_first in [true, false] {
                    let mut b = NirBuilder::new(opts);
                    let inp = b.add_input("input", &[2]).expect("input");
                    let p = b
                        .add_linear("p", &[0.5, -0.25, 0.75, 1.0], 2, 2)
                        .expect("p");
                    let l = b.add_linear("l", &[1.0, 0.5, -0.5, 0.25], 2, 2).expect("l");
                    let lif = lif2(&mut b);
                    let out = b.add_output("out", &[2]).expect("out");
                    let mut edges = vec![(inp, p), (p, l), (l, lif), (lif, out)];
                    edges.insert(if direct_first { 0 } else { 2 }, (inp, l));
                    for (from, to) in edges {
                        b.add_edge(from, to).expect("edge");
                    }
                    let g = b.build().expect("builds");
                    let (_, enc, rep) = g.build_network().expect("assembles");
                    assert_eq!(rep.stages, 1, "input → l, one stage");
                    assert_eq!(rep.fused.len(), 1);
                    assert_eq!(rep.fused[0].chain, vec!["p", "l"]);
                    let units = opts.units;
                    assert_eq!(
                        enc.mats[0].q, want,
                        "{units:?}, direct first: {direct_first}"
                    );
                }
            }
        }

        /// Three Linears in a chain compose as their product,
        /// `W_3·W_2·W_1` of the dequantized weights, natively and in
        /// simulation units, pinned as the stored identity gave them
        /// (round 56). Two chains: 2 → 2 → 2 → 2 features, where each
        /// composed matrix has two rows and two columns, and 2 → 1 → 1 → 2,
        /// where each is one row high.
        #[test]
        fn three_linears_in_a_chain_compose_as_their_product() {
            let square: [(&[f64], usize, usize); 3] = [
                (&[0.5, -0.25, 0.75, 1.0], 2, 2),
                (&[1.0, 0.5, -0.5, 0.25], 2, 2),
                (&[0.25, -1.0, 1.0, 0.5], 2, 2),
            ];
            let narrow: [(&[f64], usize, usize); 3] =
                [(&[0.5, -0.25], 1, 2), (&[0.75], 1, 1), (&[1.0, -0.5], 2, 1)];
            let chains = [
                (square, centi(), [10_923, -12_136, 32_767, 16_991]),
                (
                    square,
                    NirImportOptions::sim_units(100),
                    [281, -313, 844, 438],
                ),
                (narrow, centi(), [32_767, -16_384, -16_384, 8_192]),
                (
                    narrow,
                    NirImportOptions::sim_units(100),
                    [375, -188, -188, 94],
                ),
            ];
            for (linears, opts, want) in chains {
                let mut b = NirBuilder::new(opts);
                let mut prev = b.add_input("input", &[2]).expect("input");
                for (name, (w, rows, cols)) in ["l1", "l2", "l3"].into_iter().zip(linears) {
                    let l = b.add_linear(name, w, rows, cols).expect("linear");
                    b.add_edge(prev, l).expect("edge");
                    prev = l;
                }
                let lif = lif2(&mut b);
                let out = b.add_output("out", &[2]).expect("out");
                b.add_edge(prev, lif).expect("edge");
                b.add_edge(lif, out).expect("edge");
                let g = b.build().expect("builds");
                let (_, enc, rep) = g.build_network().expect("assembles");
                assert_eq!(rep.stages, 1, "input → l3, one stage");
                assert_eq!(rep.fused.len(), 1);
                assert_eq!(rep.fused[0].chain, vec!["l1", "l2", "l3"]);
                let units = opts.units;
                assert_eq!(enc.mats[0].q, want, "{units:?}");
            }
        }

        /// A root edge listed twice counts twice: the copy sums with the
        /// first, `2·W_l` alone and `W_l·(2I + W_p)` in a skip whose direct
        /// edge is listed twice before its composed path. `from_json` and
        /// `NirBuilder::build` refuse a duplicate edge, but `edges` is
        /// `pub`, and a caller can push one onto a built graph. Natively
        /// a stage is quantized against its own absmax, so `2·W_l` shows
        /// in simulation units alone and the skip in both. Pinned as the
        /// stored identity gave them (round 56).
        #[test]
        fn a_root_edge_listed_twice_counts_twice() {
            let cases = [
                (false, centi(), [32_767, 16_384, -16_384, 8_192]),
                (
                    false,
                    NirImportOptions::sim_units(100),
                    [2_000, 1_000, -1_000, 500],
                ),
                (true, centi(), [32_767, 14_247, -12_110, 9_973]),
                (
                    true,
                    NirImportOptions::sim_units(100),
                    [2_875, 1_250, -1_063, 875],
                ),
            ];
            for (skip, opts, want) in cases {
                let mut b = NirBuilder::new(opts);
                let inp = b.add_input("input", &[2]).expect("input");
                let l = b.add_linear("l", &[1.0, 0.5, -0.5, 0.25], 2, 2).expect("l");
                let lif = lif2(&mut b);
                let out = b.add_output("out", &[2]).expect("out");
                let mut edges = vec![(inp, l), (l, lif), (lif, out)];
                if skip {
                    let p = b
                        .add_linear("p", &[0.5, -0.25, 0.75, 1.0], 2, 2)
                        .expect("p");
                    edges.extend([(inp, p), (p, l)]);
                }
                for (from, to) in edges {
                    b.add_edge(from, to).expect("edge");
                }
                let mut g = b.build().expect("builds");
                // the direct edge again, right after itself: before the
                // skip's composed path
                let direct = g.edges[0];
                g.edges.insert(1, direct);
                let (_, enc, rep) = g.build_network().expect("assembles");
                assert_eq!(rep.stages, 1, "input → l, one stage");
                assert_eq!(rep.fused.len(), usize::from(skip));
                let units = opts.units;
                assert_eq!(enc.mats[0].q, want, "{units:?}, skip: {skip}");
            }
        }

        /// The merge fixture at graph scale: one branch stalls below
        /// the climb (81 pA → `V_ss−rest` 810 < the 1500 gap), the
        /// summed fan-in fires (162 pA) — 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(&[&[Thousandths(1_000); 2], &[]]))
                    .unwrap()
                {
                    counts[s.neuron_id as usize] += 1;
                }
            }
            assert_eq!(counts[0], 0, "81 pA stalls below the climb");
            assert!(counts[1] > 0, "the 327 pA 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 {
                let x = [1_000, 0].map(Thousandths);
                for s in net2.step(&enc2.encode(&[&x, &x])).unwrap() {
                    if s.neuron_id == 0 {
                        first = first.min(t);
                        n0 += 1;
                    }
                }
            }
            assert_eq!(first, 52, "summed 162 pA 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
            // pA 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_000, 0].map(Thousandths)]))
                    .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 pA 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->Output) 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, dst }
                                if 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 pA dead zone"))
                }),
            ];
            for (label, doc, check) in cases {
                let opts = if label.starts_with("recurrent") {
                    // mV, the rejected request
                    NirImportOptions::new(1_000, VoltageResolution::Millivolt)
                } 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::new(1_000, VoltageResolution::Millivolt));
            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::new(1_000, VoltageResolution::Millivolt));
            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::new(1_000, VoltageResolution::Millivolt));
            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::new(1_000, VoltageResolution::Millivolt));
            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::new(1_000, VoltageResolution::Millivolt));
            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(&[&[Thousandths(3_000)]])).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");
        }
    }

    /// D8, the spiking Linear edge: the two witnesses (snnTorch's own
    /// export and the NIR paper's `two_lif_neurons`, each converted by
    /// `gresil-nir2json --sim-units`; `tests/nir_fixtures/README.md`),
    /// the fused tripwire, the std weight's identity, the gain the drive
    /// side shares, and the named rejections.
    #[cfg(test)]
    mod spiking_linear_tests {
        use super::{
            NirBuilder, NirError, NirGraphEncoder, NirImport, NirImportOptions, NirLifParams,
            NirUnits, Thousandths, D8_WEIGHT_DIVISOR, LINEAR_GAIN_DIVISOR, RECURRENT_MV_REMEDY,
            SPIKING_LINEAR_MV_REMEDY,
        };
        use crate::fixed::FixedSynapse;
        use crate::lif_neuron::VoltageResolution;
        use crate::network::SpikingNeuralNetwork;

        const SNNTORCH: &[u8] = include_bytes!("../tests/nir_fixtures/snntorch_two_layer_sim.json");
        const TWO_LIF: &[u8] = include_bytes!("../tests/nir_fixtures/two_lif_neurons_sim.json");
        const READOUT: &str =
            "readout (LIF->Linear->Output) deferred — spike-count readout convention not yet named";
        const SELF_SYNAPSE: &str = "spiking Linear edge onto its own neuron (LIF->Linear->LIF, \
             a nonzero diagonal) — the substrate forbids self-synapse";

        /// The one native synapse of a 1×1 chain: 327 pA. A 1×1 weight
        /// quantizes to full scale, 32767, and 32767 / 100 truncates to
        /// 327.
        const ONE_EDGE: [FixedSynapse; 1] = [FixedSynapse {
            pre: 0,
            post: 1,
            pulse_pa: 327,
        }];

        /// The one synapse of both simulation-unit witnesses: lif1 →
        /// lif2, 1,000 quanta a spike, a weight of 1.0 at true scale.
        const ONE_SIM_EDGE: [FixedSynapse; 1] = [FixedSynapse {
            pre: 0,
            post: 1,
            pulse_pa: 1_000,
        }];

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

        /// A biological-scale LIF population of `n`: τ 20 ms, 100 MΩ,
        /// −70 / −55 / −80 mV.
        fn lif(bld: &mut NirBuilder<'_>, name: &'static str, n: usize) -> usize {
            bld.add_lif_population(
                name,
                &NirLifParams {
                    tau_s: &vec![0.02; n],
                    r_ohm: &vec![1e8; n],
                    v_leak_v: &vec![-0.07; n],
                    v_threshold_v: &vec![-0.055; n],
                    v_reset_v: Some(&vec![-0.08; n]),
                },
            )
            .expect("a biological LIF population")
        }

        /// The builder's nodes wired by these edges, by index.
        fn wire(bld: &mut NirBuilder<'_>, edges: &[(usize, usize)]) {
            for &(from, to) in edges {
                bld.add_edge(from, to).expect("indices from the builder");
            }
        }

        /// The spike steps of each of two neurons over `steps` steps of
        /// a one-feature drive, in whole units.
        fn raster(
            net: &mut SpikingNeuralNetwork,
            enc: &NirGraphEncoder,
            steps: u32,
            drive: impl Fn(u32) -> i16,
        ) -> [Vec<u32>; 2] {
            let mut spike_steps = [Vec::new(), Vec::new()];
            for step in 0..steps {
                let spikes = net
                    .step(&enc.encode(&[&[Thousandths(i32::from(drive(step)) * 1_000)]]))
                    .expect("an assembled network steps");
                for spike in spikes {
                    spike_steps[usize::from(spike.neuron_id)].push(step);
                }
            }
            spike_steps
        }

        /// The constant is part of the public contract: pin it, with the
        /// std weight's divisor it implies.
        #[test]
        fn linear_gain_divisor_is_one_hundred() {
            assert_eq!(LINEAR_GAIN_DIVISOR, 100);
            assert_eq!(D8_WEIGHT_DIVISOR * 10, LINEAR_GAIN_DIVISOR);
        }

        /// The std weight composes to the pulse for every i16 `q`: the
        /// step divides `trunc(q / 10)` by the assembly's divisor 10, and
        /// `trunc(trunc(q / 10) / 10) = trunc(q / 100)`, truncation toward
        /// zero composing. All 65,536 values.
        #[test]
        fn the_std_weight_composes_to_the_pulse_for_every_i16() {
            for q in i16::MIN..=i16::MAX {
                assert_eq!(
                    q / D8_WEIGHT_DIVISOR / 10,
                    q / LINEAR_GAIN_DIVISOR,
                    "q = {q}"
                );
            }
        }

        /// The framework witness of ROADMAP § 0.1.0 check 6: snnTorch
        /// 1.0's own export of `Linear → Leaky → Linear → Leaky`, read
        /// under `--sim-units` (τ 5 ms, 500 MΩ, threshold 10 mV at V 10)
        /// and imported in simulation units at 0.1 ms, the stranger's
        /// path. Two neurons, one synapse of 1,000 quanta a spike, and the
        /// drive side's 1,000 for a feature of 1: a weight of 1.0 at true
        /// scale. Driven 1 on steps 0–9 and 50–59, a step adds exactly one
        /// threshold and NIR fires on `v > v_threshold`, so lif1 fires on
        /// every other driven step from step 1; each spike reaches lif2 one
        /// step later, and lif2 fires when a pulse finds it above rest.
        #[test]
        fn the_snntorch_two_layer_graph_builds_and_fires() {
            let graph = NirImport::from_json(SNNTORCH, NirImportOptions::sim_units(100))
                .expect("the emission imports");
            let (mut net, enc, rep) = graph.build_network().expect("the two-layer graph builds");
            assert_eq!((rep.neurons, rep.synapses), (2, 1));
            assert_eq!(net.synaptic_input_divisor(), 10, "the assembly's divisor");
            assert_eq!(net.synapses()[0].weight, 10_000, "1.0 · 1,000 · 10");
            assert_eq!(FixedSynapse::from_network(&net), ONE_SIM_EDGE);
            assert_eq!(
                enc.encode(&[&[Thousandths(1_000)]]),
                [1_000, 0],
                "the drive side, the same 1,000 quanta"
            );
            let [lif1, lif2] = raster(&mut net, &enc, 150, |step| {
                i16::from(matches!(step, 0..=9 | 50..=59))
            });
            assert_eq!(lif1, [1, 3, 5, 7, 9, 51, 53, 55, 57, 59]);
            assert_eq!(lif2, [4, 8, 52, 56, 60]);
        }

        /// The reference witness: the NIR paper's own `two_lif_neurons`
        /// (written through the `nir` library, not a framework), the same
        /// shape under `--sim-units` and imported in simulation units at
        /// 0.1 ms. lif1's leak, 1.2, sits above its threshold, 1.0, so it
        /// fires with no input: at step 0 from rest, then every 201 steps
        /// from reset, where the same equation in floating point takes
        /// 179, since the substrate truncates each step's move to whole
        /// quanta, two or three of them near this threshold
        /// (`LIFNeuron::integrate_and_fire` § Semantics, Rounding). lif2
        /// (threshold 20, V 0.5, its r of 0.5 MΩ rounded up to 1) stays
        /// below threshold, as the paper's model means it to.
        #[test]
        fn the_two_lif_neurons_graph_builds_and_fires_with_no_input() {
            let graph = NirImport::from_json(TWO_LIF, NirImportOptions::sim_units(100))
                .expect("the file imports");
            let (mut net, enc, rep) = graph.build_network().expect("the two-layer graph builds");
            assert_eq!((rep.neurons, rep.synapses), (2, 1));
            assert_eq!(FixedSynapse::from_network(&net), ONE_SIM_EDGE);
            let [lif1, lif2] = raster(&mut net, &enc, 500, |_| 0);
            assert_eq!(lif1, [0, 201, 402], "from rest, then every 201 steps");
            assert_eq!(lif2, Vec::<u32>::new());
        }

        /// Both witnesses carry the units mark, so native options refuse
        /// them at import on either grid, before the mV grid rule could,
        /// and the message names the options they need.
        #[test]
        fn native_options_refuse_both_witnesses_by_their_units() {
            for (label, doc) in [("snntorch", SNNTORCH), ("two_lif", TWO_LIF)] {
                for opts in [
                    NirImportOptions::new(1_000, VoltageResolution::Millivolt),
                    centi(),
                ] {
                    let err = NirImport::from_json(doc, opts)
                        .map(|_| ())
                        .expect_err("a file in simulation units");
                    assert_eq!(
                        err,
                        NirError::UnitsMismatch {
                            file: NirUnits::Simulation,
                            options: NirUnits::Native,
                        },
                        "{label}"
                    );
                    assert_eq!(
                        err.to_string(),
                        "the file is in simulation units and the options read native units — \
                         import a gresil-nir2json --sim-units file with \
                         NirImportOptions::sim_units(dt_us)",
                        "{label}"
                    );
                }
            }
        }

        /// The other way: a file with no mark reads as native units, so
        /// simulation options refuse it. The message names the native
        /// options, and says a `--sim-units` file written before the mark
        /// is converted again. `chain.json` is a reference emission, with
        /// no mark.
        #[test]
        fn simulation_options_refuse_a_file_with_no_mark() {
            let chain = include_bytes!("../tests/nir_fixtures/chain.json");
            let err = NirImport::from_json(chain, NirImportOptions::sim_units(1_000))
                .map(|_| ())
                .expect_err("no mark: native units");
            assert_eq!(
                err,
                NirError::UnitsMismatch {
                    file: NirUnits::Native,
                    options: NirUnits::Simulation,
                }
            );
            assert_eq!(
                err.to_string(),
                "the file is in native units and the options read simulation units — import it \
                 with NirImportOptions::new(dt_us, VoltageResolution::Millivolt), or a graph \
                 with a spike edge with NirImportOptions::new(dt_us, \
                 VoltageResolution::CentiMillivolt); a gresil-nir2json --sim-units file \
                 written before the units mark has none: convert its .nir again"
            );
        }

        /// A mark the export never writes is refused, whatever the
        /// options.
        #[test]
        fn a_units_mark_other_than_simulation_is_refused() {
            let doc = String::from_utf8(SNNTORCH.to_vec())
                .expect("ASCII")
                .replace("\"units\":\"simulation\"", "\"units\":\"native\"");
            for opts in [
                NirImportOptions::sim_units(100),
                NirImportOptions::new(1_000, VoltageResolution::Millivolt),
            ] {
                assert_eq!(
                    NirImport::from_json(doc.as_bytes(), opts).map(|_| ()),
                    Err(NirError::BadShape("units"))
                );
            }
        }

        /// D6's note is a native one: in simulation units no absmax scale
        /// absorbs a branch's gain, so the witness's two drive Linears
        /// note none.
        #[test]
        fn simulation_units_note_no_multi_linear_gain() {
            let graph = NirImport::from_json(SNNTORCH, NirImportOptions::sim_units(100))
                .expect("the emission imports");
            let (_, _, rep) = graph.build_network().expect("the two-layer graph builds");
            assert_eq!(rep.drive_linears, 2);
            assert!(!rep.multi_linear_gain);
        }

        /// D1's grid rule extends to D8: a spiking Linear edge on the mV
        /// grid is refused by name, with the remedy. A builder's graph
        /// carries no units mark, so the grid rule is what it meets.
        #[test]
        fn the_mv_grid_rejects_a_spiking_linear_edge_by_name() {
            let mut bld =
                NirBuilder::new(NirImportOptions::new(1_000, VoltageResolution::Millivolt));
            let inp = bld.add_input("input", &[1]).expect("input");
            let l0 = bld.add_linear("l0", &[1.0], 1, 1).expect("linear");
            let pre = lif(&mut bld, "a", 1);
            let l1 = bld.add_linear("l1", &[1.0], 1, 1).expect("linear");
            let post = lif(&mut bld, "b", 1);
            let out = bld.add_output("out", &[1]).expect("output");
            wire(
                &mut bld,
                &[(inp, l0), (l0, pre), (pre, l1), (l1, post), (post, out)],
            );
            let graph = bld.build().expect("builds");
            let err = graph
                .build_network()
                .map(|_| ())
                .expect_err("mV must reject a spiking Linear edge");
            assert!(
                matches!(err, NirError::UnsupportedTopology(m) if m == SPIKING_LINEAR_MV_REMEDY),
                "{err:?}"
            );
        }

        /// A graph with a spiking Linear edge or a LIF→LIF edge on mV
        /// options is refused with one of these remedies, and
        /// each names the options a graph in simulation units needs: a
        /// builder's graph carries no units mark, and neither does a file
        /// the converter wrote before the mark. Each names the native
        /// options on the centi-mV grid by `new`, a call: a struct literal
        /// of the options does not compile outside this crate.
        #[test]
        fn the_mv_remedies_name_the_options_each_graph_needs() {
            for remedy in [SPIKING_LINEAR_MV_REMEDY, RECURRENT_MV_REMEDY] {
                assert!(
                    remedy.contains("NirImportOptions::sim_units(dt_us)"),
                    "{remedy}"
                );
                assert!(
                    remedy.contains(
                        "NirImportOptions::new(dt_us, VoltageResolution::CentiMillivolt)"
                    ),
                    "{remedy}"
                );
            }
        }

        /// The tripwire that separates fused from per-segment:
        /// `a → Linear([[2.0]]) → Linear([[0.3]]) → b` composes to 0.6,
        /// absmax 0.6, `q = 32767`, a 327 pA pulse. Quantizing each
        /// tensor to full scale and composing in i16 would deliver about
        /// 32,767. The chain is one fusion record, as on the drive side.
        #[test]
        fn a_two_linear_chain_after_a_lif_is_fused_once() {
            let mut bld = NirBuilder::new(centi());
            let inp = bld.add_input("input", &[1]).expect("input");
            let l0 = bld.add_linear("l0", &[1.0], 1, 1).expect("linear");
            let pre = lif(&mut bld, "a", 1);
            let l1 = bld.add_linear("l1", &[2.0], 1, 1).expect("linear");
            let l2 = bld.add_linear("l2", &[0.3], 1, 1).expect("linear");
            let post = lif(&mut bld, "b", 1);
            let out = bld.add_output("out", &[1]).expect("output");
            wire(
                &mut bld,
                &[
                    (inp, l0),
                    (l0, pre),
                    (pre, l1),
                    (l1, l2),
                    (l2, post),
                    (post, out),
                ],
            );
            let graph = bld.build().expect("builds");
            let (net, _enc, rep) = graph.build_network().expect("the chain assembles");
            assert_eq!(
                FixedSynapse::from_network(&net),
                ONE_EDGE,
                "327, not 32,767"
            );
            assert_eq!(rep.fused.len(), 1);
            assert_eq!(rep.fused[0].chain, ["l1", "l2"]);
        }

        /// One constant, two sides: an input feature of 1 and a spike of
        /// 1 through the same 1×1 Linear give the same pA. `l` has two
        /// roots, the Input (an encoder stage onto `b`) and the LIF `a`
        /// (a D8 synapse onto `b`); both quantize the same matrix and
        /// read `LINEAR_GAIN_DIVISOR`.
        #[test]
        fn an_input_feature_and_a_spike_through_one_linear_give_the_same_pa() {
            let mut bld = NirBuilder::new(centi());
            let inp = bld.add_input("input", &[1]).expect("input");
            let to_pre = bld.add_linear("la", &[1.0], 1, 1).expect("linear");
            let pre = lif(&mut bld, "a", 1);
            let shared = bld.add_linear("l", &[0.25], 1, 1).expect("linear");
            let post = lif(&mut bld, "b", 1);
            let out = bld.add_output("out", &[1]).expect("output");
            wire(
                &mut bld,
                &[
                    (inp, to_pre),
                    (to_pre, pre),
                    (inp, shared),
                    (pre, shared),
                    (shared, post),
                    (post, out),
                ],
            );
            let graph = bld.build().expect("builds");
            let (net, enc, _) = graph.build_network().expect("assembles");
            let drive = enc.encode(&[&[Thousandths(1_000)]])[1];
            let pulse = FixedSynapse::from_network(&net)[0].pulse_pa;
            assert_eq!((drive, pulse), (327, 327));
            assert_eq!(pulse, 32_767 / LINEAR_GAIN_DIVISOR);
        }

        /// The band, kept on purpose (`LINEAR_GAIN_DIVISOR`'s doc): a
        /// nonzero `q` under `LINEAR_GAIN_DIVISOR` still makes its
        /// synapse, which delivers 0 pA. `a` (three neurons) →
        /// `Linear([[1.0, 0.001, 0.0002]])` → `b`: absmax 1.0, so `q` is
        /// 32767, 33 and 7, the std weights 3276, 3 and 0, the pulses
        /// 327, 0 and 0.
        #[test]
        fn a_q_in_the_band_makes_a_synapse_that_delivers_nothing() {
            let mut bld = NirBuilder::new(centi());
            let inp = bld.add_input("input", &[1]).expect("input");
            let l0 = bld.add_linear("l0", &[1.0; 3], 3, 1).expect("linear");
            let pre = lif(&mut bld, "a", 3);
            let l1 = bld
                .add_linear("l1", &[1.0, 0.001, 0.000_2], 1, 3)
                .expect("linear");
            let post = lif(&mut bld, "b", 1);
            let out = bld.add_output("out", &[1]).expect("output");
            wire(
                &mut bld,
                &[(inp, l0), (l0, pre), (pre, l1), (l1, post), (post, out)],
            );
            let graph = bld.build().expect("builds");
            let (net, _enc, rep) = graph.build_network().expect("assembles");
            assert_eq!(rep.synapses, 3);
            let weights: Vec<i16> = net.synapses().iter().map(|s| s.weight).collect();
            assert_eq!(weights, [3_276, 3, 0]);
            let pulses: Vec<i16> = FixedSynapse::from_network(&net)
                .iter()
                .map(|s| s.pulse_pa)
                .collect();
            assert_eq!(pulses, [327, 0, 0]);
        }

        /// The readout stays deferred, by name, wherever a spike path
        /// would feed an Output: a Linear after a LIF with an Output
        /// successor, whatever its others (here a LIF too). An
        /// Input-rooted Linear with an Output beside its LIF assembles,
        /// as before D8 (the principal's call, 2026-09-14). A Linear
        /// after a LIF feeding the Output alone is the fixture case in
        /// the named-rejection table.
        #[test]
        fn a_spike_path_into_an_output_is_the_deferred_readout() {
            let mut bld = NirBuilder::new(centi());
            let inp = bld.add_input("input", &[1]).expect("input");
            let l0 = bld.add_linear("l0", &[1.0], 1, 1).expect("linear");
            let pre = lif(&mut bld, "a", 1);
            let mixed = bld.add_linear("l", &[1.0], 1, 1).expect("linear");
            let post = lif(&mut bld, "b", 1);
            let out = bld.add_output("out", &[1]).expect("output");
            wire(
                &mut bld,
                &[
                    (inp, l0),
                    (l0, pre),
                    (pre, mixed),
                    (mixed, post),
                    (mixed, out),
                ],
            );
            let graph = bld.build().expect("builds");
            let err = graph
                .build_network()
                .expect_err("a spike path into the Output");
            assert!(
                matches!(err, NirError::UnsupportedTopology(m) if m == READOUT),
                "{err:?}"
            );

            let mut bld = NirBuilder::new(centi());
            let inp = bld.add_input("input", &[1]).expect("input");
            let drive = bld.add_linear("l", &[1.0], 1, 1).expect("linear");
            let pop = lif(&mut bld, "a", 1);
            let out = bld.add_output("out", &[1]).expect("output");
            wire(&mut bld, &[(inp, drive), (drive, pop), (drive, out)]);
            let graph = bld.build().expect("builds");
            assert!(
                graph.build_network().is_ok(),
                "an Input-rooted Linear may feed an Output beside its LIF"
            );
        }

        /// A population's spikes back onto itself through a Linear: the
        /// off-diagonal weights are synapses and a zero weight makes
        /// none, but a nonzero diagonal would be a self-synapse, which
        /// the substrate forbids, so it rejects by name.
        #[test]
        fn a_spiking_linear_edge_onto_its_own_neuron_rejects_by_name() {
            fn recurrent(weights: &[f64]) -> Result<Vec<FixedSynapse>, String> {
                let mut bld = NirBuilder::new(centi());
                let inp = bld.add_input("input", &[1]).expect("input");
                let l0 = bld.add_linear("l0", &[1.0, 1.0], 2, 1).expect("linear");
                let pop = lif(&mut bld, "a", 2);
                let back = bld.add_linear("l", weights, 2, 2).expect("linear");
                let out = bld.add_output("out", &[2]).expect("output");
                wire(
                    &mut bld,
                    &[(inp, l0), (l0, pop), (pop, back), (back, pop), (pop, out)],
                );
                let graph = bld.build().expect("builds");
                graph
                    .build_network()
                    .map(|(net, _, _)| FixedSynapse::from_network(&net))
                    .map_err(|e| e.to_string())
            }
            let both = vec![
                FixedSynapse {
                    pre: 0,
                    post: 1,
                    pulse_pa: 327,
                },
                FixedSynapse {
                    pre: 1,
                    post: 0,
                    pulse_pa: 327,
                },
            ];
            assert_eq!(recurrent(&[0.0, 1.0, 1.0, 0.0]), Ok(both));
            let err = recurrent(&[0.0, 1.0, 1.0, 0.5]).expect_err("a nonzero diagonal");
            assert!(err.contains(SELF_SYNAPSE), "{err}");
        }
    }

    /// An imported graph in owned buffers (std convenience over the
    /// two-pass buffer API). Not exhaustive: a field may come in a minor
    /// release, so outside this crate a graph comes from
    /// [`from_json`](Self::from_json) or [`NirBuilder::build`].
    #[derive(Debug)]
    #[non_exhaustive]
    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];
            // the sizes `NirBuffers` names: the arena the weight cells,
            // which it keeps, and the f64 scratch enough for any node's
            // staging
            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`, its three edges in any order;
        /// [`NirError::UnsupportedUnits`]
        /// under [`NirUnits::Simulation`]; [`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> {
            match self.opts.units {
                NirUnits::Native => {}
                NirUnits::Simulation => return Err(NirError::UnsupportedUnits),
            }
            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",
                ));
            }
            // the three edges in any order: an exporter lists them in its
            // own (snnTorch's follows Python's hash seed)
            let mut chain = [
                (inp as u32, lin as u32),
                (lin as u32, lif as u32),
                (lif as u32, out as u32),
            ];
            chain.sort_unstable();
            let mut edges = self.edges.clone();
            edges.sort_unstable();
            if edges != 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(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.refractory_period_us = 1_000; // NIR LIF has no refractory → minimum
                n.resistance_mohm = p.resistance_mohm;
                n.noise_amplitude_pa = 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, the Linear DAG becomes
        /// quantized encoder stages from its Inputs (D2 fusion at the
        /// setup seam), and from its LIFs one synapse per nonzero
        /// composed weight (D8, [`LINEAR_GAIN_DIVISOR`]). Plasticity is
        /// frozen; the report records everything loud (fusion,
        /// undriven structure, multi-Linear gain). That is the native
        /// mapping. Under [`NirUnits::Simulation`] every weight keeps
        /// its true scale, [`SIM_CURRENT_QUANTA`] quanta per unit: a
        /// drive stage's quanta are `round(w · SIM_CURRENT_QUANTA)`, a
        /// LIF→LIF pair is `10 · SIM_CURRENT_QUANTA`, one unit a spike,
        /// and a spiking Linear edge makes one synapse per nonzero std
        /// weight, `round(w · 10 · SIM_CURRENT_QUANTA)`; no gain is
        /// absorbed, so the report notes no multi-Linear gain.
        ///
        /// The import options ARE the grid request (one channel, no
        /// overrides): a graph with any LIF→LIF or LIF→Linear edge
        /// assembles only on centi-mV options — mV options reject by
        /// name with the re-import remedy (D1: a 20 pA pulse is dead
        /// 10× over on the mV grid; D8 extends the rule). Feed-forward
        /// imports without a spike edge assemble on mV options too, as
        /// the frozen chain pins do.
        ///
        /// # 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); under [`NirUnits::Simulation`],
        /// [`NirError::WeightOutOfRange`] for a weight whose true-scale
        /// quanta pass ±32,767.
        ///
        /// # Panics
        ///
        /// Never on a graph [`NirImport::from_json`] or [`NirBuilder`]
        /// produced and the caller left as it was (edge indices are
        /// resolved and bounds-checked there). The fields are public, so
        /// an edited graph can panic where the assembly reads what the
        /// import checked: for instance an edge index past the nodes, a
        /// LIF or Linear node whose view was set to `None`, or a Linear
        /// view past the weights.
        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));
                }
            }
            let mut net = SpikingNeuralNetwork::from_neurons(neurons, self.opts.dt_us)
                .map_err(|_| NirError::BufferOverflow)?;

            // encoder stages, D8 synapses and fusion records: the Linear
            // DAG, fused, from every root (an Input, or a LIF)
            let order = self.linear_order()?;
            let EncoderPlan {
                mats,
                stages,
                spiking,
                fused,
                rooted,
                drive_linears,
            } = self.build_stages(&inputs, &order, &pop_base)?;

            // LIF->LIF edges -> EDGE_PULSE_QUANTA synapse pairs, or one
            // unit a spike in simulation units (identity element mapping;
            // equal sizes shape-checked)
            let d1 = match self.opts.units {
                NirUnits::Native => EDGE_PULSE_QUANTA,
                NirUnits::Simulation => 10 * SIM_CURRENT_QUANTA,
            };
            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, d1)
                            .map_err(|_| NirError::BufferOverflow)?;
                        synapses += 1;
                    }
                }
            }
            // LIF->Linear->…->LIF paths -> the D8 synapses, in stage order
            for &(pre, post, weight) in &spiking {
                net.add_synapse(pre, post, weight)
                    .map_err(|_| NirError::BufferOverflow)?;
                synapses += 1;
            }
            net.finalize_synapses();
            // NIR has no plasticity term, and none to switch off: a network
            // starts with plasticity off (both constructors).

            let undriven = self.undriven_notes(&inputs, &rooted);
            let encoder = NirGraphEncoder {
                total,
                units: self.opts.units,
                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,
                // D6's premise, an absmax scale that absorbs a branch's
                // gain, holds in native units only
                multi_linear_gain: match self.opts.units {
                    NirUnits::Native => drive_linears > 1,
                    NirUnits::Simulation => false,
                },
                plasticity_frozen: !net.plasticity_enabled(),
            };
            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 readout (D8: a Linear a
        /// spike reaches, with an Output successor) / the two mV grid
        /// rejections, LIF→LIF then LIF→Linear. (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::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::Linear) => {
                        na.lif.expect("checked").len == nb.linear.expect("checked").cols
                    }
                    (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: NirName::new(na.name),
                        dst: NirName::new(nb.name),
                    });
                }
            }
            // D8: the Linears a spike reaches, over a LIF→Linear edge
            // and then Linear→Linear ones. One with an Output successor
            // is the readout, deferred by name whatever its other
            // successors; an Input-rooted Linear with an Output beside
            // its LIF stays legal (decoration, as before D8).
            let mut linear_children: Vec<Vec<usize>> = vec![Vec::new(); nodes.len()];
            let mut work: Vec<usize> = Vec::new();
            for &(a, b) in &self.edges {
                match (nodes[a as usize].kind, nodes[b as usize].kind) {
                    (NirNodeKind::Linear, NirNodeKind::Linear) => {
                        linear_children[a as usize].push(b as usize);
                    }
                    (NirNodeKind::Lif, NirNodeKind::Linear) => work.push(b as usize),
                    _ => {}
                }
            }
            let mut spiked = vec![false; nodes.len()];
            while let Some(l) = work.pop() {
                if !spiked[l] {
                    spiked[l] = true;
                    work.extend(&linear_children[l]);
                }
            }
            if self
                .edges
                .iter()
                .any(|&(a, b)| spiked[a as usize] && nodes[b as usize].kind == NirNodeKind::Output)
            {
                return Err(NirError::UnsupportedTopology(
                    "readout (LIF->Linear->Output) deferred — spike-count readout convention not yet named",
                ));
            }
            let recurrent = self.edges.iter().any(|&(a, b)| {
                matches!(
                    (nodes[a as usize].kind, nodes[b as usize].kind),
                    (NirNodeKind::Lif, NirNodeKind::Lif)
                )
            });
            let mv = self.opts.resolution == VoltageResolution::Millivolt;
            if recurrent && mv {
                return Err(NirError::UnsupportedTopology(RECURRENT_MV_REMEDY));
            }
            if spiked.contains(&true) && mv {
                return Err(NirError::UnsupportedTopology(SPIKING_LINEAR_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) stage, the root
        /// an Input or (D8) a LIF. `G(L)[r]` is the transform arriving
        /// at `L`'s INPUT from root `r` — identity from an Input or LIF
        /// 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`. An Input root's matrix is an encoder
        /// stage; a LIF root's is one synapse per nonzero weight (D8).
        /// D2 fusion records every stage composed from ≥2 matrices,
        /// with each component tensor's scale, from either root. A root
        /// enters as the identity without storing it, so the memory
        /// grows with the weights: a stage a root feeds once is its
        /// Linear's own weights, and a chain composes from its first
        /// Linear. The identity is stored where a root reaches one
        /// Linear twice: a skip, or an edge listed twice, which only a
        /// hand-built graph has.
        // 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, `None` the
            // identity, 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 {
                        // a root enters as identity: an Input's features,
                        // or (D8) a LIF's spikes, each the value 1 into
                        // the chain; its width is `cols` (shape-checked).
                        // Stored only when it meets `l` twice: a composed
                        // path first (a skip), or its own edge listed
                        // twice, which only a hand-built graph has
                        NirNodeKind::Input | NirNodeKind::Lif => {
                            if let Some((m, _)) = acc.get_mut(&p) {
                                let m = m.get_or_insert_with(|| mat_eye(cols));
                                for (i, row) in m.iter_mut().enumerate() {
                                    row[i] += 1.0;
                                }
                            } else {
                                acc.insert(p, (None, Vec::new()));
                            }
                        }
                        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] {
                                // the identity's width: the root feeds p
                                let n_feat = m.as_ref().map_or(lin_p.cols, |gm| gm[0].len());
                                let entry_val = acc
                                    .entry(*root)
                                    .or_insert_with(|| (Some(mat_zero(cols, n_feat)), Vec::new()));
                                // the root fed l directly too: its identity
                                let dst = entry_val.0.get_or_insert_with(|| mat_eye(cols));
                                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 {
                                            match m {
                                                // W_p·I: the weight, at its column
                                                None => dst[row][col] += wv,
                                                Some(gm) => {
                                                    for (cell, &src) in
                                                        dst[row].iter_mut().zip(&gm[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);
                                    }
                                }
                            }
                        }
                        NirNodeKind::Output => {} // named-rejected as a source already
                    }
                }
                g.insert(l, acc);
            }

            let mut mats: Vec<QuantMat> = Vec::new();
            let mut stages: Vec<DriveStage> = Vec::new();
            let mut spiking: Vec<(u16, u16, i16)> = 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 a root path (an Input's or a LIF's), 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.as_ref().map_or(lin_l.cols, |gm| gm[0].len());
                    // stage matrix: W_L · G  (rows x f, row-major); W_L
                    // itself where G is the identity
                    let mut flat = vec![0f64; rows * f];
                    for r in 0..rows {
                        for k in 0..f {
                            flat[r * f + k] = match m {
                                None => f64::from(w[r * lin_l.cols + k]) * lin_l.scale,
                                Some(gm) => {
                                    let mut s = 0.0f64;
                                    for c in 0..lin_l.cols {
                                        s += f64::from(w[r * lin_l.cols + c])
                                            * lin_l.scale
                                            * gm[c][k];
                                    }
                                    s
                                }
                            };
                        }
                    }
                    let mut q = vec![0i16; rows * f];
                    super::quantize_linear(&flat, rows, f, &mut q, 0)?;
                    if self.nodes[*root].kind == NirNodeKind::Lif {
                        // D8: pre j of the root population to post i of
                        // each LIF this Linear feeds, one synapse per
                        // nonzero composed weight
                        let pre_base = pop_base[root];
                        for &pop in &lif_children[lin_idx] {
                            let post_base = pop_base[&pop];
                            for i in 0..rows {
                                for j in 0..f {
                                    let qij = q[i * f + j];
                                    // a zero makes no synapse: the quanta
                                    // natively, in simulation units the
                                    // true-scale std weight
                                    let (wij, zero) = match self.opts.units {
                                        NirUnits::Native => (qij / D8_WEIGHT_DIVISOR, qij == 0),
                                        NirUnits::Simulation => {
                                            let w = sim_quanta(flat[i * f + j], 10)?;
                                            (w, w == 0)
                                        }
                                    };
                                    if zero {
                                        continue;
                                    }
                                    let (pre, post) = (pre_base + j, post_base + i);
                                    if pre == post {
                                        return Err(NirError::UnsupportedTopology(
                                            "spiking Linear edge onto its own neuron (LIF->Linear->LIF, a nonzero diagonal) — the substrate forbids self-synapse",
                                        ));
                                    }
                                    spiking.push((pre as u16, post as u16, wij));
                                }
                            }
                        }
                    } else {
                        let mat = mats.len();
                        let q = match self.opts.units {
                            NirUnits::Native => q,
                            NirUnits::Simulation => flat
                                .iter()
                                .map(|&w| sim_quanta(w, 1))
                                .collect::<Result<Vec<_>, _>>()?,
                        };
                        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,
                spiking,
                fused,
                rooted,
                drive_linears,
            })
        }

        /// `UndrivenPopulation` notes (structural, name-carrying): LIF
        /// populations with no Input-rooted path (permanently silent)
        /// and Linear nodes never invoked (no root path, Input or LIF,
        /// 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) natively and at true
    /// scale in simulation units).
    #[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, the D8 synapses, fusion
    /// records, the rooted Linear set (for undriven notes), and the D6
    /// drive-Linear count.
    struct EncoderPlan<'a> {
        mats: Vec<QuantMat>,
        stages: Vec<DriveStage>,
        /// D8: `(pre, post, std weight)`, in stage order: natively one
        /// per nonzero quantized composed weight `q`, carrying
        /// `q / D8_WEIGHT_DIVISOR`; in simulation units one per nonzero
        /// true-scale std weight.
        spiking: Vec<(u16, u16, i16)>,
        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,
    }

    /// One input feature's value at one step, as
    /// [`NirGraphEncoder::encode`] reads it, in thousandths of a unit:
    /// `Thousandths(1_000)` is 1.0, a spike or a unit held for the step,
    /// and `Thousandths(500)` is 0.5. Its own type, so a call written for
    /// whole units does not compile as thousandths, and an `i32`, so an
    /// input can pass 32.767 units (raw pixels, 0 to 255, say); the
    /// current a neuron takes from a stage in one step still saturates
    /// at ±32,767 quanta.
    ///
    /// Exhaustive on purpose: `Thousandths(n)` is how a caller writes one,
    /// and a second field would be a breaking release.
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
    pub struct Thousandths(pub i32);

    /// [`Thousandths`] in a unit.
    const PER_UNIT: i128 = 1_000;

    /// `a / d` rounded half away from zero, as a weight's quanta are
    /// (`round_half_away`); `d` positive.
    fn div_round_half_away(a: i128, d: i128) -> i128 {
        let half = d / 2;
        if a < 0 {
            (a - half) / d
        } else {
            (a + half) / d
        }
    }

    /// The Linear half of a general graph: root-Input feature values,
    /// [`Thousandths`] of a unit, → the global per-step current vector
    /// (pA, 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) — each row
    /// summed exactly and divided as [`encode`](Self::encode) says,
    /// natively by the substrate's encoder gain, [`LINEAR_GAIN_DIVISOR`],
    /// too (`ChainEncoder`'s current for a whole input), merged
    /// saturating-i16 across stages (D5's mechanical merge).
    #[derive(Debug)]
    pub struct NirGraphEncoder {
        total: usize,
        units: NirUnits,
        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 values, each [`Thousandths`] of a
        /// unit, into the global per-step current vector. A stage sums
        /// `q · x` over its features, then divides: under
        /// [`NirUnits::Simulation`], whose quanta are true scale, by
        /// 1,000, rounding half away from zero as a weight's quanta
        /// round; natively by 1,000 and [`LINEAR_GAIN_DIVISOR`],
        /// truncating, `ChainEncoder`'s current for a whole input.
        /// Missing Inputs / missing feature entries read as 0 (the
        /// `ChainEncoder`'s graceful-zeros convention).
        #[must_use]
        pub fn encode(&self, per_input: &[&[Thousandths]]) -> 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 {
                    // a quantum times a feature is within 2^46, so no
                    // count of features takes the sum past an i128
                    let mut acc: i128 = 0;
                    for c in 0..m.cols {
                        acc += i128::from(m.q[r * m.cols + c])
                            * i128::from(x.get(c).map_or(0, |v| v.0));
                    }
                    let acc = match self.units {
                        NirUnits::Native => acc / (PER_UNIT * i128::from(LINEAR_GAIN_DIVISOR)),
                        NirUnits::Simulation => div_round_half_away(acc, PER_UNIT),
                    };
                    let v = acc.clamp(i128::from(i16::MIN), i128::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. The assembly builds it and a caller reads it.
    /// Not exhaustive: a field may come in a minor release, so a pattern
    /// needs `..`.
    #[derive(Debug, Clone, PartialEq)]
    #[non_exhaustive]
    pub struct LinearFusedRecord<'a> {
        /// The composed nodes' names, root first, target last.
        pub chain: Vec<&'a str>,
        /// Each component tensor's quantization scale, in `chain` order.
        pub scales: Vec<f64>,
    }

    /// The loud assembly report. The assembly builds it and a caller
    /// reads it. Not exhaustive: a field may come in a minor release.
    #[derive(Debug, Clone, PartialEq)]
    #[non_exhaustive]
    pub struct NirAssemblyReport<'a> {
        /// Total neurons (all populations).
        pub neurons: usize,
        /// Synapses: the LIF→LIF pairs at [`EDGE_PULSE_QUANTA`] (D1) and
        /// the spiking Linear edges' nonzero composed weights (D8);
        /// under [`NirUnits::Simulation`], the pairs at
        /// `10 · SIM_CURRENT_QUANTA` and the edges' nonzero true-scale
        /// std weights.
        pub synapses: usize,
        /// Input nodes (encoder entry points).
        pub inputs: usize,
        /// Linear nodes directly feeding a LIF stage, from an Input or
        /// (D8) a LIF (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).
        /// Under [`NirUnits::Simulation`] the weights keep their true
        /// scale and no gain is absorbed, so the note does not fire.
        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` in
        /// [`NirUnits::Native`], never in [`NirUnits::Simulation`].
        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 pA dead \
     zone — re-import a gresil-nir2json --sim-units file with \
     NirImportOptions::sim_units(dt_us), a graph in native units with \
     NirImportOptions::new(dt_us, VoltageResolution::CentiMillivolt)";

    /// The named mV-on-spiking-Linear rejection (D8 extends D1's grid
    /// rule), remedy verbatim and copy-pasteable.
    const SPIKING_LINEAR_MV_REMEDY: &str = "spiking Linear edge (LIF->Linear->LIF) on mV: D8 \
     assembles on the centi-mV grid only, as LIF->LIF does — re-import a \
     gresil-nir2json --sim-units file with NirImportOptions::sim_units(dt_us), a graph in \
     native units with NirImportOptions::new(dt_us, VoltageResolution::CentiMillivolt)";

    #[cfg(test)]
    mod sim_units_tests {
        //! `NirUnits::Simulation`: true-scale weights, NIR's firing rule,
        //! no refractory, no adaptation, a direct edge at one unit a
        //! spike, and the refusals. The graphs are what `--sim-units`
        //! writes at V 10:
        //! τ 5 ms, r 50 → 500 MΩ, threshold 1.0 → 10 mV, leak and reset 0.
        use super::super::{
            nir_export, NirBuilder, NirError, NirImport, NirImportOptions, NirLifParams, NirUnits,
            Thousandths, SIM_CURRENT_QUANTA,
        };
        use super::{DriveStage, NirGraphEncoder, QuantMat};
        use crate::fixed::FixedSynapse;
        use crate::lif_neuron::VoltageResolution;

        fn lif(bld: &mut NirBuilder<'_>, name: &'static str) -> usize {
            bld.add_lif_population(
                name,
                &NirLifParams {
                    tau_s: &[0.005],
                    r_ohm: &[5e8],
                    v_leak_v: &[0.0],
                    v_threshold_v: &[0.01],
                    v_reset_v: Some(&[0.0]),
                },
            )
            .expect("a simulation-units LIF")
        }

        /// `Input → Linear [[w1]] → LIF`, then `→ Linear [[w2]] → LIF`
        /// when `w2` is given, `→ LIF` directly when `direct`.
        fn graph(
            opts: NirImportOptions,
            w1: f64,
            w2: Option<f64>,
            direct: bool,
        ) -> NirImport<'static> {
            let mut b = NirBuilder::new(opts);
            let inp = b.add_input("input", &[1]).expect("input");
            let l1 = b.add_linear("l1", &[w1], 1, 1).expect("l1");
            let a = lif(&mut b, "a");
            let out = b.add_output("output", &[1]).expect("output");
            b.add_edge(inp, l1).expect("edge");
            b.add_edge(l1, a).expect("edge");
            if let Some(w2) = w2 {
                let l2 = b.add_linear("l2", &[w2], 1, 1).expect("l2");
                let c = lif(&mut b, "c");
                b.add_edge(a, l2).expect("edge");
                b.add_edge(l2, c).expect("edge");
                b.add_edge(c, out).expect("edge");
            } else if direct {
                let c = lif(&mut b, "c");
                b.add_edge(a, c).expect("edge");
                b.add_edge(c, out).expect("edge");
            } else {
                b.add_edge(a, out).expect("edge");
            }
            b.build().expect("the graph builds")
        }

        fn sim() -> NirImportOptions {
            NirImportOptions::sim_units(100)
        }

        /// NIR's LIF has no adaptation: a network imported in simulation
        /// units builds every neuron with it off, a native one with it on,
        /// as `LIFNeuron::new` builds it.
        #[test]
        fn simulation_units_build_neurons_without_adaptation() {
            let native = NirImportOptions::new(100, VoltageResolution::CentiMillivolt);
            for (opts, on) in [(sim(), false), (native, true)] {
                let (net, _, _) = graph(opts, 1.0, Some(0.3), false)
                    .build_network()
                    .expect("builds");
                assert_eq!(net.neurons().len(), 2);
                assert!(
                    net.neurons().iter().all(|n| n.adaptation_enabled() == on),
                    "{:?}",
                    opts.units
                );
            }
        }

        /// The units mark rides the export in simulation units only, one
        /// on each LIF node's quant record; a native export has none, so
        /// it keeps its bytes. The marked export imports back.
        #[test]
        fn the_export_marks_simulation_units_only() {
            let native = NirImportOptions::new(100, VoltageResolution::CentiMillivolt);
            for (opts, marks) in [(sim(), 2), (native, 0)] {
                let g = graph(opts, 1.0, Some(0.3), false);
                let mut out = vec![0u8; 1 << 16];
                let n = nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, &mut out)
                    .expect("exports");
                let json = core::str::from_utf8(&out[..n]).expect("ASCII");
                assert_eq!(json.matches("\"units\":\"simulation\"").count(), marks);
                assert!(NirImport::from_json(&out[..n], opts).is_ok());
            }
        }

        #[test]
        fn a_drive_weight_keeps_its_true_scale() {
            let (_, enc, _) = graph(sim(), 0.3, None, false)
                .build_network()
                .expect("builds");
            assert_eq!(enc.encode(&[&[Thousandths(1_000)]]), [300], "0.3 · 1,000");
            let native = NirImportOptions::new(100, VoltageResolution::CentiMillivolt);
            let (_, enc, _) = graph(native, 0.3, None, false)
                .build_network()
                .expect("builds");
            assert_eq!(
                enc.encode(&[&[Thousandths(1_000)]]),
                [327],
                "native: absmax, / 100"
            );
        }

        /// An input is thousandths of a unit, and a stage's sum divides by
        /// 1,000, rounding half away from zero: through 0.5, 500 quanta a
        /// unit, 0.001 gives half a quantum, so 1, 0.003 gives 1.5, so 2,
        /// and 0.005 gives 2.5, so 3; each negative the same way down. An
        /// input past 32.767 units reads whole, and the stage saturates at
        /// the `i16` bound.
        #[test]
        fn an_input_reads_in_thousandths_and_the_division_rounds() {
            let current = |w: f64, x: i32| {
                let (_, enc, _) = graph(sim(), w, None, false)
                    .build_network()
                    .expect("builds");
                enc.encode(&[&[Thousandths(x)]])[0]
            };
            assert_eq!(
                [1, 3, 5, -1, -3, -5].map(|x| current(0.5, x)),
                [1, 2, 3, -1, -2, -3]
            );
            assert_eq!(
                [499, 500, 1_000, 2_000].map(|x| current(0.5, x)),
                [250, 250, 500, 1_000]
            );
            assert_eq!(current(0.1, 255_000), 25_500, "255 units, raw pixels");
            assert_eq!(
                [400_000, -400_000].map(|x| current(0.1, x)),
                [i16::MAX, i16::MIN]
            );
        }

        /// Natively an input is thousandths too, and the division by 1,000
        /// and `LINEAR_GAIN_DIVISOR` truncates: through 32,767 quanta (the
        /// absmax), 0.5 gives 163.835, so 163, as `ChainEncoder` would give
        /// it if it took a half.
        #[test]
        fn a_native_input_in_thousandths_truncates() {
            let native = NirImportOptions::new(100, VoltageResolution::CentiMillivolt);
            let (_, enc, _) = graph(native, 0.3, None, false)
                .build_network()
                .expect("builds");
            let at = |x: i32| enc.encode(&[&[Thousandths(x)]])[0];
            assert_eq!([500, -500, 1_000, 999].map(at), [163, -163, 327, 327]);
        }

        /// A missing Input, or a missing feature of one, reads as 0 in
        /// simulation units too, where one thousandth through 1,000 quanta
        /// is a whole quantum.
        #[test]
        fn a_missing_input_or_feature_reads_as_zero() {
            let (_, enc, _) = graph(sim(), 1.0, None, false)
                .build_network()
                .expect("builds");
            assert_eq!(enc.encode(&[&[Thousandths(1)]]), [1], "one thousandth");
            assert_eq!(enc.encode(&[&[]]), [0], "no feature");
            assert_eq!(enc.encode(&[]), [0], "no Input");
        }

        /// No count of features takes a stage's sum past its `i128`:
        /// 140,000 features at the largest input, each through 32,767
        /// quanta, pass `i64::MAX`, and the stage saturates at +32,767 in
        /// either units. The encoder is built by hand: its one matrix
        /// holds `i16::MAX` in either units.
        #[test]
        fn a_stage_sums_past_i64_and_saturates() {
            const N: usize = 140_000;
            let n = i128::try_from(N).expect("fits");
            assert!(n * i128::from(i16::MAX) * i128::from(i32::MAX) > i128::from(i64::MAX));
            for units in [NirUnits::Native, NirUnits::Simulation] {
                let enc = NirGraphEncoder {
                    total: 1,
                    units,
                    input_feats: vec![N],
                    mats: vec![QuantMat {
                        q: vec![i16::MAX; N],
                        rows: 1,
                        cols: N,
                    }],
                    stages: vec![DriveStage {
                        pop_base: 0,
                        root: 0,
                        mat: 0,
                    }],
                };
                let x = vec![Thousandths(i32::MAX); N];
                assert_eq!(enc.encode(&[&x]), [i16::MAX], "{units:?}");
            }
        }

        #[test]
        fn a_spiking_linear_edge_keeps_its_true_scale() {
            let (net, _, _) = graph(sim(), 1.0, Some(0.3), false)
                .build_network()
                .expect("builds");
            assert_eq!(net.synapses()[0].weight, 3_000, "0.3 · 1,000 · 10");
            let pulse = [FixedSynapse {
                pre: 0,
                post: 1,
                pulse_pa: 300,
            }];
            assert_eq!(FixedSynapse::from_network(&net), pulse);
        }

        #[test]
        fn a_direct_edge_delivers_one_unit_a_spike() {
            let (net, _, _) = graph(sim(), 1.0, None, true)
                .build_network()
                .expect("builds");
            assert_eq!(net.synapses()[0].weight, 10 * SIM_CURRENT_QUANTA);
        }

        #[test]
        fn nir_s_firing_rule_and_no_refractory() {
            let (net, _, _) = graph(sim(), 1.0, None, false)
                .build_network()
                .expect("builds");
            let n = &net.neurons()[0];
            assert_eq!(n.threshold, 1_001, "10 mV is 1,000 quanta, one up");
            assert_eq!(n.refractory_period_us, 0);
            let native = NirImportOptions::new(100, VoltageResolution::CentiMillivolt);
            let (net, _, _) = graph(native, 1.0, None, false)
                .build_network()
                .expect("builds");
            let n = &net.neurons()[0];
            assert_eq!((n.threshold, n.refractory_period_us), (1_000, 1_000));
        }

        #[test]
        fn a_weight_that_does_not_fit_is_refused() {
            // Ok when it builds, Err(true) when refused as out of range
            let fits = |w1, w2| match graph(sim(), w1, w2, false).build_network() {
                Ok(_) => Ok(()),
                Err(e) => Err(matches!(e, NirError::WeightOutOfRange)),
            };
            assert_eq!(fits(32.767, None), Ok(()), "32,767 quanta fit");
            assert_eq!(fits(32.768, None), Err(true));
            assert_eq!(fits(1.0, Some(3.2767)), Ok(()), "32,767 fits");
            assert_eq!(fits(1.0, Some(-3.2768)), Err(true));
        }

        #[test]
        fn a_weight_that_rounds_to_zero_makes_no_synapse() {
            // 0.000_04 at true scale is a std weight of 0.4, rounded to 0
            let g = graph(sim(), 1.0, Some(0.000_04), false);
            let (net, _, rep) = g.build_network().expect("builds");
            assert!(net.synapses().is_empty());
            assert_eq!(rep.synapses, 0);
        }

        #[test]
        fn a_weight_in_the_band_makes_a_synapse_that_delivers_nothing() {
            // 0.000_5 at true scale is a std weight of 5, in the band:
            // the synapse is made, and its pulse, 5 / 10, truncates to 0
            let g = graph(sim(), 1.0, Some(0.000_5), false);
            let (net, _, rep) = g.build_network().expect("builds");
            assert_eq!(rep.synapses, 1);
            assert_eq!(net.synapses()[0].weight, 5);
            let silent = [FixedSynapse {
                pre: 0,
                post: 1,
                pulse_pa: 0,
            }];
            assert_eq!(FixedSynapse::from_network(&net), silent);
        }

        #[test]
        fn the_chain_builder_refuses_simulation_units() {
            let g = graph(sim(), 0.3, None, false);
            assert!(matches!(
                g.build_chain_network(),
                Err(NirError::UnsupportedUnits)
            ));
        }
    }

    /// The assembly's neuron mapping: per-neuron quantized params onto
    /// an Excitatory substrate neuron, deterministic (noise 0), minimum
    /// refractory.
    ///
    /// Written as the constructor chain, which is also what the freezer
    /// emits (`fixed::freeze`), so an imported neuron and its frozen
    /// copy are built by the same calls. `build_chain_network`'s inline
    /// block is a SECOND implementation of this mapping, by assignment,
    /// and is deliberately not shared with this one:
    /// `chain_equivalence_both_builders_bit_exact` holds the two against
    /// each other on `chain.json`, which is only a comparison while they
    /// are written twice. What that test sees is narrow — of the eight
    /// assignments, one differs from the constructor's defaults on that
    /// fixture, the refractory period: the membrane coupling does not
    /// (its `v_leak` is the default −70 on the mV grid). The coupling is
    /// pinned by the traces of `snntorch-two-layer` and
    /// `two-lif-neurons`, by the freezer's at-rest assert, and by
    /// `with_resting_potential`'s doctest.
    ///
    /// In simulation units the threshold is one quantum up, so the
    /// substrate's `v ≥ threshold` fires on NIR's `v > v_threshold`, the
    /// refractory period is 0 and adaptation is off. The chain builder
    /// refuses simulation units, so its inline block stays the native
    /// mapping this one is compared with.
    fn substrate_neuron(id: u16, p: &NirLif, opts: NirImportOptions) -> LIFNeuron {
        // NIR's LIF has no refractory: 0 in simulation units, the
        // native minimum otherwise
        let (threshold, refractory_us) = match opts.units {
            NirUnits::Native => (p.threshold_q, 1_000),
            NirUnits::Simulation => (p.threshold_q.saturating_add(1), 0), // NIR's strict `>`
        };
        LIFNeuron::new(id, NeuronType::Excitatory, opts.resolution)
            .with_resting_potential(p.leak_q) // the membrane with it: built at rest
            .with_threshold(threshold)
            .with_reset_potential(p.reset_q)
            .with_tau_membrane_us(p.tau_us)
            .with_refractory_period_us(refractory_us)
            .with_resistance_mohm(p.resistance_mohm)
            .with_noise_amplitude_pa(0) // import is deterministic
            // NIR's LIF has no adaptation: off in simulation units
            .with_adaptation_enabled(matches!(opts.units, NirUnits::Native))
    }

    /// A weight at true scale in simulation units:
    /// `round(w · SIM_CURRENT_QUANTA · per)`, where `per` is 1 into a
    /// drive stage and 10 for a spiking Linear edge's std weight (the
    /// assembly's divisor 10). Refused past ±32,767.
    fn sim_quanta(w: f64, per: i16) -> Result<i16, NirError> {
        let q = super::round_half_away(w * f64::from(SIM_CURRENT_QUANTA) * f64::from(per));
        if (-f64::from(i16::MAX)..=f64::from(i16::MAX)).contains(&q) {
            Ok(q as i16)
        } else {
            Err(NirError::WeightOutOfRange)
        }
    }

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

    /// The identity a root enters with (`n × n`), stored only where the
    /// root reaches one Linear twice: a skip, or an edge listed twice.
    fn mat_eye(n: usize) -> Vec<Vec<f64>> {
        let mut m = mat_zero(n, n);
        for (i, row) in m.iter_mut().enumerate() {
            row[i] = 1.0;
        }
        m
    }

    /// The value arriving at a Linear's input from one root, an Input
    /// or (D8) a LIF: the composed matrix (cols × feat), `None` for the
    /// identity, which is not stored (as a matrix it costs 8 bytes a
    /// pair of the root's features) + the contributing Linear node
    /// indices (topological order, deduplicated).
    type GVal = (Option<Vec<Vec<f64>>>, Vec<usize>);

    /// `G(L)`: root node index (an Input or, D8, a LIF) → arriving value.
    type GMap = BTreeMap<usize, GVal>;

    /// The Linear half of the chain: feature currents (pA) →
    /// per-neuron currents (pA), saturating i16 — `y = W·x` in
    /// substrate units. Weights are scaled by 1/[`LINEAR_GAIN_DIVISOR`]
    /// (the substrate's convention: a weight contributes `w/100` pA per
    /// unit input feature current; D8's spike side reads the same
    /// constant), 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 /= i64::from(LINEAR_GAIN_DIVISOR);
                *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 and the units, which the LIF quantizer and the
        /// assembly read.
        #[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")`](NirError::BadShape) outside 1–4
        /// dims, as the reader refuses a shape given empty or past 4.
        pub fn add_input(&mut self, name: &'a str, shape: &[u32]) -> Result<usize, NirError> {
            self.push_carrier(name, NirNodeKind::Input, shape)
        }

        /// Add an `Output` node (shape carrier; 1–4 dims).
        ///
        /// # Errors
        ///
        /// [`NirError::BadShape("shape")`](NirError::BadShape) outside 1–4
        /// dims, as the reader refuses a shape given empty or past 4.
        pub fn add_output(&mut self, name: &'a str, shape: &[u32]) -> Result<usize, NirError> {
            self.push_carrier(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")`](NirError::BadShape) 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> {
            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> {
            // 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")`](NirError::BadShape) 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> {
            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> {
            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,
            })
        }

        /// An `Input` or an `Output`: a shape of 1 to 4 dims, as the
        /// reader takes one. A Linear and a LIF carry none, so
        /// `push_node` itself takes an empty shape.
        fn push_carrier(
            &mut self,
            name: &'a str,
            kind: NirNodeKind,
            shape: &[u32],
        ) -> Result<usize, NirError> {
            if shape.is_empty() {
                return Err(NirError::BadShape("shape"));
            }
            self.push_node(name, kind, shape)
        }

        fn push_node(
            &mut self,
            name: &'a str,
            kind: NirNodeKind,
            shape: &[u32],
        ) -> Result<usize, NirError> {
            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,
    Thousandths, EDGE_PULSE_QUANTA, LINEAR_GAIN_DIVISOR,
};

#[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"))
        ));
    }

    /// Each blank equals the literal its sites wrote: a node arena's slot
    /// (the import's, `nir_format_gate`'s, the fixtures' three), leg A's
    /// Linear sentinel but its NaN, and a lifs buffer's
    /// `NirLif::default()`; a population view, which no site wrote, is
    /// zeros.
    #[test]
    fn each_blank_is_what_its_sites_wrote() {
        assert_eq!(
            NirNode::BLANK,
            NirNode {
                name: "",
                kind: NirNodeKind::Input,
                shape: [0; 4],
                shape_len: 0,
                lif: None,
                linear: None,
            }
        );
        assert_eq!(
            NirLinear::BLANK,
            NirLinear {
                rows: 0,
                cols: 0,
                weight_offset: 0,
                scale: 0.0,
                absmax: 0.0,
                max_abs_err: 0.0,
                zero_tensor: false,
            }
        );
        assert_eq!(NirLif::BLANK, NirLif::default());
        assert_eq!(
            NirLifPopulation::BLANK,
            NirLifPopulation { offset: 0, len: 0 }
        );
    }

    #[allow(clippy::type_complexity)]
    fn import_chain(
        opts: NirImportOptions,
    ) -> (
        Vec<NirNode<'static>>,
        Vec<(u32, u32)>,
        Vec<i16>,
        Vec<NirLif>,
        NirReport,
    ) {
        import_chain_variant(CHAIN, opts).expect("chain imports")
    }

    /// CHAIN with `field` added to its LIF node.
    fn chain_with(field: &str) -> String {
        let doc = CHAIN.replacen("\"lif\":{", &format!("\"lif\":{{{field},"), 1);
        assert_ne!(doc, CHAIN, "the LIF node takes the field");
        doc
    }

    /// `doc`, CHAIN or a variant of it, through `nir_import` alone, into
    /// buffers sized by CHAIN's scan. The scan skips a node's fields
    /// under the same cap, so going around it makes a refusal the
    /// import's own.
    #[allow(clippy::type_complexity)]
    fn import_chain_variant(
        doc: &str,
        opts: NirImportOptions,
    ) -> Result<
        (
            Vec<NirNode<'_>>,
            Vec<(u32, u32)>,
            Vec<i16>,
            Vec<NirLif>,
            NirReport,
        ),
        NirError,
    > {
        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(doc.as_bytes(), opts, &mut bufs)?;
        Ok((nodes, edges, weights, lifs, report))
    }

    #[test]
    fn import_quantizes_the_chain() {
        let (nodes, edges, weights, lifs, report) =
            import_chain(NirImportOptions::new(1_000, VoltageResolution::Millivolt));
        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) >= 1);
    }

    #[test]
    fn a_leak_rate_that_truncates_to_zero_is_refused() {
        // dt_over_tau = trunc(dt·1000/tau): 0 past tau = 1000·dt, where
        // a neuron never moves; 1 at the edge, which still integrates.
        let at = |tau_s: f64, dt_us: u32| {
            quantize_lif(
                tau_s,
                1e8,
                -0.07,
                -0.055,
                -0.08,
                false,
                NirImportOptions::new(dt_us, VoltageResolution::Millivolt),
            )
        };
        assert!(at(0.1, 100).is_ok(), "tau 1000·dt: the leak rate is 1");
        assert!(matches!(at(0.100_001, 100), Err(NirError::TauTooLongForDt)));
        assert!(at(1.0, 1_000).is_ok(), "tau 1000·dt at 1 ms");
        assert!(matches!(
            at(1.000_001, 1_000),
            Err(NirError::TauTooLongForDt)
        ));
    }

    #[test]
    fn a_threshold_at_the_ceiling_is_refused_in_simulation_units() {
        // 5,000 centi quanta, stored one quantum up in simulation units,
        // is 5,001, past the membrane's clamp at 5,000: never a spike
        let at =
            |v_threshold: f64, opts| quantize_lif(0.005, 5e8, 0.0, v_threshold, 0.0, false, opts);
        let sim = NirImportOptions::sim_units(100);
        assert!(matches!(
            at(0.05, sim),
            Err(NirError::PotentialOutOfRange("v_threshold + 1"))
        ));
        assert!(at(0.049_99, sim).is_ok(), "4,999 quanta, stored 5,000");
        let native = NirImportOptions::new(100, VoltageResolution::CentiMillivolt);
        assert!(
            at(0.05, native).is_ok(),
            "natively `v ≥ threshold` fires there"
        );
    }

    #[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::new(30_000, VoltageResolution::Millivolt);
        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::new(1_000, VoltageResolution::Millivolt)
            ),
            Err(NirError::BadNumber("tau"))
        ));
        assert!(matches!(
            quantize_lif(
                -0.02,
                1e8,
                -0.07,
                -0.055,
                -0.08,
                false,
                NirImportOptions::new(1_000, VoltageResolution::Millivolt)
            ),
            Err(NirError::BadNumber("tau"))
        ));
        assert!(matches!(
            quantize_lif(
                0.02,
                0.0,
                -0.07,
                -0.055,
                -0.08,
                false,
                NirImportOptions::new(1_000, VoltageResolution::Millivolt)
            ),
            Err(NirError::BadNumber("r"))
        ));
        assert!(matches!(
            quantize_lif(
                0.02,
                1e8,
                -0.07,
                -0.0004,
                -0.08,
                false,
                NirImportOptions::new(1_000, VoltageResolution::Millivolt)
            ),
            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::new(1_000, VoltageResolution::Millivolt)
            ),
            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::new(1_000, VoltageResolution::Millivolt),
                &mut bufs
            ),
            Err(NirError::UnsupportedNodeKind(NirName::new("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::new(1_000, VoltageResolution::Millivolt),
            &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(n) if n == "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::new(1_000, VoltageResolution::Millivolt),
                &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::new(1_000, VoltageResolution::Millivolt),
                &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::new(1_000, VoltageResolution::Millivolt),
                &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::new(1_000, VoltageResolution::Millivolt),
                &mut bufs
            ),
            Err(NirError::BufferOverflow)
        );
    }

    #[test]
    fn export_then_import_is_idempotent() {
        let opts = NirImportOptions::new(1_000, VoltageResolution::Millivolt);
        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);
    }

    /// C is the substrate's own `tau` over its own `r`, the two integers
    /// the export writes, so the round trip keeps it: off the µs grid
    /// with `r` on the MΩ grid (10.0006 ms is 10,001 µs, over 2 MΩ
    /// 5,000.5 pF, rounded away to 5,001), and with `r` off it (0.5 MΩ
    /// is 1 MΩ on the substrate: 10,000 pF, not the source's 20,000).
    #[test]
    fn capacitance_is_the_substrates_own_tau_over_r() {
        let opts = NirImportOptions::new(1_000, VoltageResolution::Millivolt);
        let doc = CHAIN.replacen(
            "\"tau\":[0.02,0.02],\"r\":[100000000.0,100000000.0]",
            "\"tau\":[0.0100006,0.01],\"r\":[2000000.0,500000.0]",
            1,
        );
        assert_ne!(doc, CHAIN, "the LIF takes the new tau and r");
        let (nodes, edges, weights, lifs, _) = import_chain_variant(&doc, opts).expect("imports");
        let c = |l: &[NirLif]| [l[0].capacitance_pf, l[1].capacitance_pf];
        assert_eq!(c(&lifs), [5_001, 10_000]);
        let mut out = [0u8; 4096];
        let n = nir_export(&nodes, &edges, &weights, &lifs, opts, &mut out).expect("exports");
        let exported = core::str::from_utf8(&out[..n]).expect("utf8");
        let (_, _, _, again, _) = import_chain_variant(exported, opts).expect("re-imports");
        assert_eq!(c(&again), c(&lifs), "the round trip keeps C");
    }

    /// `doc` through `nir_import` alone, into buffers wide enough for
    /// every variant of CHAIN below, so the refusal shown is the
    /// import's own.
    fn import_alone(doc: &str) -> Result<(), NirError> {
        let mut nodes = [NirNode {
            name: "",
            kind: NirNodeKind::Input,
            shape: [0; 4],
            shape_len: 0,
            lif: None,
            linear: None,
        }; 8];
        let mut edges = [(0u32, 0u32); 8];
        let mut weights = [0i16; 64];
        let mut lifs = [NirLif::default(); 16];
        let mut scratch = [0f64; 64];
        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::new(1_000, VoltageResolution::Millivolt),
            &mut bufs,
        )
        .map(|_| ())
    }

    /// A key its object does not carry, or one given twice, is refused
    /// by name, by the scan and by the import alone. A field of another
    /// kind is one: a LIF's `tau` on an Input, or a `weight` after a
    /// LIF's fields, which staged over them and imported a tau of 30 ms;
    /// one that voids tau (`[[-1.0]]`) is refused by name too, not as
    /// `BadNumber("tau")`, since the keys are judged before any staged
    /// value is used. So is an unknown or misspelled key, at any level.
    /// The import alone reads values as they come, so a stray field
    /// whose value is malformed meets the value's refusal there first.
    #[test]
    #[allow(clippy::too_many_lines)] // twenty-three cases, one table
    fn keys_outside_their_object_are_refused_by_name() {
        let on = |node: &str, field: &str| {
            let at = format!("\"{node}\":{{");
            let doc = CHAIN.replacen(&at, &format!("{at}{field},"), 1);
            assert_ne!(doc, CHAIN, "{node} takes {field}");
            doc
        };
        let swap = |old: &str, new: &str| {
            let doc = CHAIN.replacen(old, new, 1);
            assert_ne!(doc, CHAIN, "CHAIN holds {old}");
            doc
        };
        let out = |node: Option<&str>, field: &str| NirError::UnexpectedField {
            node: node.map(NirName::new),
            field: NirName::new(field),
        };
        let twice = |node: Option<&str>, field: &str| NirError::DuplicateField {
            node: node.map(NirName::new),
            field: NirName::new(field),
        };
        let lif_end = "\"v_reset\":[-0.08,-0.08]}";
        let cases: Vec<(&str, String, NirError, Option<NirError>)> = vec![
            (
                "a LIF's tau on an Input",
                on("input", "\"tau\":[0.02]"),
                out(Some("input"), "tau"),
                None,
            ),
            (
                "a weight on an Input",
                on("input", "\"weight\":[[1.0]]"),
                out(Some("input"), "weight"),
                None,
            ),
            (
                "a weight after a LIF's fields",
                swap(lif_end, "\"v_reset\":[-0.08,-0.08],\"weight\":[[0.03]]}"),
                out(Some("lif"), "weight"),
                None,
            ),
            (
                "a weight that voids tau after a LIF's fields",
                swap(lif_end, "\"v_reset\":[-0.08,-0.08],\"weight\":[[-1.0]]}"),
                out(Some("lif"), "weight"),
                None,
            ),
            (
                "a weight on an Output",
                on("output", "\"weight\":[[2.0,3.0]]"),
                out(Some("output"), "weight"),
                None,
            ),
            (
                "a shape on a LIF",
                on("lif", "\"shape\":[2]"),
                out(Some("lif"), "shape"),
                None,
            ),
            (
                "a shape on a Linear",
                on("linear", "\"shape\":[2]"),
                out(Some("linear"), "shape"),
                None,
            ),
            (
                "a LIF's fields on a Linear",
                on(
                    "linear",
                    "\"tau\":[0.02,0.02],\"r\":[1e8,1e8],\"v_leak\":[-0.07,-0.07],\
                     \"v_threshold\":[-0.055,-0.055]",
                ),
                out(Some("linear"), "tau"),
                None,
            ),
            (
                "a v_reset alone on an Input",
                on("input", "\"v_reset\":[0.0]"),
                out(Some("input"), "v_reset"),
                None,
            ),
            (
                "a tau and an r of two lengths on an Input",
                on("input", "\"tau\":[0.02,0.03],\"r\":[1e8]"),
                out(Some("input"), "tau"),
                None,
            ),
            (
                "a ragged weight on an Output",
                on("output", "\"weight\":[[1.0,2.0],[3.0]]"),
                out(Some("output"), "weight"),
                Some(NirError::BadShape("weight")),
            ),
            (
                "a tau of -1 on an Input",
                on("input", "\"tau\":[-1.0]"),
                out(Some("input"), "tau"),
                None,
            ),
            (
                "an unknown key on an Input",
                on("input", "\"foo\":1"),
                out(Some("input"), "foo"),
                None,
            ),
            (
                "v_rest for v_reset",
                swap("\"v_reset\"", "\"v_rest\""),
                out(Some("lif"), "v_rest"),
                None,
            ),
            (
                "an unknown key on the graph object",
                swap("\"node\":{", "\"node\":{\"foo\":1,"),
                out(None, "foo"),
                None,
            ),
            (
                "an unknown key on the document",
                swap("{\"version\"", "{\"foo\":1,\"version\""),
                out(None, "foo"),
                None,
            ),
            (
                "a tau given twice",
                swap(lif_end, "\"v_reset\":[-0.08,-0.08],\"tau\":[0.03,0.03]}"),
                twice(Some("lif"), "tau"),
                None,
            ),
            (
                "a weight given twice",
                on("linear", "\"weight\":[[1.0,1.0,1.0],[1.0,1.0,1.0]]"),
                twice(Some("linear"), "weight"),
                None,
            ),
            (
                "a shape given twice",
                on("input", "\"shape\":[3]"),
                twice(Some("input"), "shape"),
                None,
            ),
            (
                "a type given twice",
                on("output", "\"type\":\"Output\""),
                twice(Some("output"), "type"),
                None,
            ),
            (
                "a metadata given twice",
                on("lif", "\"metadata\":{},\"metadata\":{}"),
                twice(Some("lif"), "metadata"),
                None,
            ),
            (
                "edges given twice",
                swap("\"node\":{", "\"node\":{\"edges\":[],"),
                twice(None, "edges"),
                None,
            ),
            (
                "a version given twice",
                swap("{\"version\"", "{\"version\":\"x\",\"version\""),
                twice(None, "version"),
                None,
            ),
        ];
        for (label, doc, scan, alone) in cases {
            assert_eq!(
                nir_scan(doc.as_bytes()).map(|_| ()),
                Err(scan),
                "{label}: the scan"
            );
            assert_eq!(
                import_alone(&doc),
                Err(alone.unwrap_or(scan)),
                "{label}: the import alone"
            );
        }
    }

    /// The two key errors name what was refused: the node and the key,
    /// or, when no node holds it, the graph object and the document.
    #[test]
    fn the_key_errors_name_the_node_and_the_key() {
        let out = |node: Option<&str>, field: &str| {
            NirError::UnexpectedField {
                node: node.map(NirName::new),
                field: NirName::new(field),
            }
            .to_string()
        };
        let twice = |node: Option<&str>, field: &str| {
            NirError::DuplicateField {
                node: node.map(NirName::new),
                field: NirName::new(field),
            }
            .to_string()
        };
        assert_eq!(
            out(Some("lif"), "v_rest"),
            "node 'lif' carries 'v_rest', a key its kind does not have"
        );
        assert_eq!(
            out(None, "foo"),
            "'foo' is not a key of the graph object or the document"
        );
        assert_eq!(twice(Some("lif"), "tau"), "node 'lif' gives 'tau' twice");
        assert_eq!(
            twice(None, "edges"),
            "'edges' is given twice in the graph object or the document"
        );
    }

    /// 32 bytes are kept whole and a 33rd cuts the name: the cut is
    /// marked, `Display` and `Debug` end it with "…", and the cut name
    /// equals no `&str`, neither the name nor the bytes it kept.
    #[test]
    fn a_name_keeps_32_bytes_and_cuts_the_33rd() {
        let n32 = "abcdefghijklmnopqrstuvwxyz012345";
        let n33 = "abcdefghijklmnopqrstuvwxyz0123456";
        assert_eq!((n32.len(), n33.len()), (32, 33));
        let whole = NirName::new(n32);
        assert!(!whole.is_cut());
        assert_eq!(whole.as_str(), n32);
        assert_eq!(whole, n32);
        assert_eq!(whole, *n32);
        assert_eq!(whole.to_string(), n32);
        assert_eq!(format!("{whole:?}"), format!("{n32:?}"));
        let cut = NirName::new(n33);
        assert!(cut.is_cut());
        assert_eq!(cut.as_str(), n32);
        assert_ne!(cut, n33);
        assert_ne!(cut, n32);
        assert_ne!(cut, *n32);
        assert_eq!(cut.to_string(), format!("{n32}…"));
        assert_eq!(format!("{cut:?}"), format!("{n32:?}…"));
        assert_ne!(cut, whole, "the same bytes, one of them cut");
        assert_eq!(NirName::new(""), "");
    }

    /// A name is cut at the last char boundary that fits: thirty bytes
    /// of ASCII and a three-byte '€' pass 32, so the '€' goes whole and
    /// thirty bytes stay; a two-byte 'é' in its place fits, and one byte
    /// later it goes, thirty-one staying.
    #[test]
    fn a_name_is_cut_at_a_char_boundary() {
        let a30 = "a".repeat(30);
        let cut = NirName::new(&format!("{a30}€"));
        assert!(cut.is_cut());
        assert_eq!(cut.as_str(), a30);
        assert_eq!(cut.to_string(), format!("{a30}…"));
        let fits = format!("{a30}é");
        assert_eq!(fits.len(), 32);
        assert!(!NirName::new(&fits).is_cut());
        assert_eq!(NirName::new(&fits), fits.as_str());
        let a31 = "a".repeat(31);
        let over = NirName::new(&format!("{a31}é"));
        assert!(over.is_cut());
        assert_eq!(over.as_str(), a31);
    }

    /// Each of the six names an error reports comes through its door as
    /// the document or the builder wrote it, and `Display` prints it: a
    /// node kind, a node and the key it carries or gives twice, the graph
    /// object's key, an edge's endpoint, an edge's two ends at the
    /// assembly, and a name the JSON container cannot write. Past 32
    /// bytes a name comes through cut, at a char boundary. The error
    /// outlives the document it reports on.
    #[test]
    fn each_name_an_error_reports_comes_through_its_door() {
        let opts = NirImportOptions::new(1_000, VoltageResolution::Millivolt);
        let refused = |doc: String| {
            import_chain_variant(&doc, opts)
                .map(|_| ())
                .expect_err("refused")
        };
        let e = refused(CHAIN.replacen("\"type\":\"LIF\"", "\"type\":\"CubaLIF\"", 1));
        assert!(
            matches!(e, NirError::UnsupportedNodeKind(k) if k == "CubaLIF"),
            "{e:?}"
        );
        assert!(e.to_string().contains("'CubaLIF'"), "{e}");
        let e = refused(chain_with("\"v_rest\":[0.0,0.0]"));
        assert!(
            matches!(e, NirError::UnexpectedField { node: Some(n), field }
                if n == "lif" && field == "v_rest"),
            "{e:?}"
        );
        assert!(e.to_string().contains("node 'lif' carries 'v_rest'"), "{e}");
        let e = refused(chain_with("\"tau\":[0.02,0.02]"));
        assert!(
            matches!(e, NirError::DuplicateField { node: Some(n), field }
                if n == "lif" && field == "tau"),
            "{e:?}"
        );
        assert!(
            e.to_string().contains("node 'lif' gives 'tau' twice"),
            "{e}"
        );
        let e = refused(CHAIN.replacen(
            "\"type\":\"NIRGraph\",",
            "\"type\":\"NIRGraph\",\"junk\":1,",
            1,
        ));
        assert!(
            matches!(e, NirError::UnexpectedField { node: None, field } if field == "junk"),
            "{e:?}"
        );
        assert!(e.to_string().contains("'junk'"), "{e}");
        let long = "model.encoder.block_3.spiking_layer.lif";
        let e =
            refused(CHAIN.replacen("[\"lif\",\"output\"]", &format!("[\"lif\",\"{long}\"]"), 1));
        let NirError::UnknownEdgeEndpoint(n) = e else {
            panic!("{e:?}")
        };
        assert!(n.is_cut());
        assert_eq!(n.as_str(), &long[..32]);
        assert!(
            e.to_string().contains(&format!("'{}…'", &long[..32])),
            "{e}"
        );
        let centi = NirImportOptions::new(1_000, VoltageResolution::CentiMillivolt);
        let g = NirImport::from_json(
            include_bytes!("../tests/nir_fixtures/neg_asm_shape_mismatch.json"),
            centi,
        )
        .expect("imports");
        let e = g.build_network().map(|_| ()).expect_err("refused");
        assert!(
            matches!(e, NirError::EdgeShapeMismatch { src, dst } if src == "input" && dst == "l1"),
            "{e:?}"
        );
        assert!(e.to_string().contains("'input' -> 'l1'"), "{e}");
        let name = "é".repeat(20);
        let mut b = NirBuilder::new(opts);
        // a writable node first: the error names the node it cannot write
        b.add_input("in", &[1]).expect("input");
        b.add_input(&name, &[1])
            .expect("the builder takes any name");
        let g = b.build().expect("builds");
        let mut out = [0u8; 1024];
        let e = nir_export(&g.nodes, &g.edges, &g.weights, &g.lifs, g.opts, &mut out)
            .expect_err("the container cannot write it");
        let NirError::NonAsciiNodeName(n) = e else {
            panic!("{e:?}")
        };
        assert!(n.is_cut());
        assert_eq!(n.as_str(), "é".repeat(16));
        assert!(
            e.to_string().contains(&format!("'{}…'", "é".repeat(16))),
            "{e}"
        );
    }

    #[test]
    fn exported_json_is_valid_shape() {
        let opts = NirImportOptions::new(1_000, VoltageResolution::Millivolt);
        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::new(1_000, VoltageResolution::Millivolt),
            &mut bufs,
        )
        .unwrap();
        assert!(rep.notes(NirNote::ZeroWeightTensor) >= 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::new(1_000, VoltageResolution::Millivolt),
            &mut bufs2,
        )
        .unwrap();
        assert!(rep2.notes(NirNote::QuantizationLoss) >= 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::new(1_000, VoltageResolution::Millivolt),
            &mut bufs,
        )
        .unwrap();
        assert!(rep.notes(NirNote::VResetDefaulted) >= 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 in the graph's metadata — a Json error, not a stack
        // overflow
        let doc = format!(
            "{{\"version\":\"x\",\"node\":{{\"type\":\"NIRGraph\",\"metadata\":{},\
             \"edges\":[],\"nodes\":{{}}}}}}",
            nest(200)
        );
        assert!(matches!(nir_scan(doc.as_bytes()), Err(NirError::Json(_))));
        // deep junk inside a node, skipped whole before the node is judged
        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\":{{}},\
             \"metadata\":{}}}}}",
            nest(40)
        );
        assert!(nir_scan(ok.as_bytes()).is_ok());
        // the graph's metadata at the cap, as a node's field: 64 deep pass
        // the scan and the import, one more is malformed
        for (n, ok) in [(64, true), (65, false)] {
            let doc = format!(
                "{{\"version\":\"x\",\"node\":{{\"type\":\"NIRGraph\",\"metadata\":{},\
                 \"edges\":[],\"nodes\":{{}}}}}}",
                nest(n)
            );
            assert_eq!(nir_scan(doc.as_bytes()).is_ok(), ok, "the scan, {n}");
            assert_eq!(import_alone(&doc).is_ok(), ok, "the import alone, {n}");
        }
        // a key the document does not carry is refused before its value
        // is read, however deep that value nests
        let junk = format!("{{\"version\":\"x\",\"junk\":{}}}", nest(200));
        assert_eq!(
            nir_scan(junk.as_bytes()).map(|_| ()),
            Err(NirError::UnexpectedField {
                node: None,
                field: NirName::new("junk")
            })
        );
    }

    /// A node's field nests 64 containers deep, `MAX_SKIP_DEPTH`,
    /// wherever a walk skips it, counted from the field's value: in the
    /// scan, in the import's node walk, at each level of `metadata` the
    /// units reader skips, and in the import's edge walk. One more is
    /// refused as malformed, by the scan and the import alike. A key no
    /// kind carries is skipped whole before its node is judged, so at
    /// 64 it is refused by name and at 65 as malformed.
    #[test]
    fn a_node_field_nests_64_containers_wherever_it_is_skipped() {
        // each field opens `levels` objects, then nests arrays around a
        // number: `n` containers in all
        let deep = NirError::UnexpectedField {
            node: Some(NirName::new("lif")),
            field: NirName::new("deep"),
        };
        for (label, open, levels, at_64) in [
            ("a key no kind carries", "\"deep\":", 0, Err(deep)),
            ("metadata that is not an object", "\"metadata\":", 0, Ok(())),
            (
                "a metadata key other than neuralos",
                "\"metadata\":{\"other\":",
                1,
                Ok(()),
            ),
            (
                "a neuralos key other than quant",
                "\"metadata\":{\"neuralos\":{\"other\":",
                2,
                Ok(()),
            ),
            (
                "a quant key other than units",
                "\"metadata\":{\"neuralos\":{\"quant\":{\"other\":",
                3,
                Ok(()),
            ),
        ] {
            let field = |n: usize| {
                let arrays = n - levels;
                format!(
                    "{open}{}0{}{}",
                    "[".repeat(arrays),
                    "]".repeat(arrays),
                    "}".repeat(levels)
                )
            };
            let doc = chain_with(&field(64));
            assert_eq!(
                nir_scan(doc.as_bytes()).map(|_| ()),
                at_64,
                "{label}: the scan, 64"
            );
            assert_eq!(
                import_chain_variant(
                    &doc,
                    NirImportOptions::new(1_000, VoltageResolution::Millivolt)
                )
                .map(|_| ()),
                at_64,
                "{label}: the import, 64"
            );
            let doc = chain_with(&field(65));
            assert!(
                matches!(nir_scan(doc.as_bytes()), Err(NirError::Json(_))),
                "{label}: the scan, 65"
            );
            assert!(
                matches!(
                    import_chain_variant(
                        &doc,
                        NirImportOptions::new(1_000, VoltageResolution::Millivolt)
                    ),
                    Err(NirError::Json(_))
                ),
                "{label}: the import, 65"
            );
        }
    }

    /// The units mark is `metadata.neuralos.quant.units` and nothing
    /// else: `metadata` of any other shape is skipped unread, a `units`
    /// key off that path included, and the LIF node reads as native
    /// units, both ways.
    #[test]
    fn metadata_of_any_other_shape_carries_no_mark() {
        for metadata in [
            "5",
            "{\"neuralos\":5}",
            "{\"neuralos\":{\"quant\":5}}",
            "{\"other\":{\"quant\":{\"units\":\"simulation\"}}}",
            "{\"neuralos\":{\"other\":{\"units\":\"simulation\"}}}",
        ] {
            let doc = chain_with(&format!("\"metadata\":{metadata}"));
            assert_eq!(
                import_chain_variant(
                    &doc,
                    NirImportOptions::new(1_000, VoltageResolution::Millivolt)
                )
                .map(|_| ()),
                Ok(()),
                "{metadata}"
            );
            assert_eq!(
                import_chain_variant(&doc, NirImportOptions::sim_units(1_000)).map(|_| ()),
                Err(NirError::UnitsMismatch {
                    file: NirUnits::Native,
                    options: NirUnits::Simulation,
                }),
                "{metadata}"
            );
        }
    }

    /// The import reads a units mark on any node, not only on the LIF
    /// nodes the export marks: CHAIN's Linear marked `simulation` is
    /// refused under native options, and, its LIF node unmarked, under
    /// simulation options too.
    #[test]
    fn a_units_mark_on_a_linear_node_is_read() {
        let mark = "\"metadata\":{\"neuralos\":{\"quant\":{\"units\":\"simulation\"}}}";
        let doc = CHAIN.replacen("\"linear\":{", &format!("\"linear\":{{{mark},"), 1);
        assert_ne!(doc, CHAIN, "the Linear node takes the mark");
        assert_eq!(
            import_chain_variant(
                &doc,
                NirImportOptions::new(1_000, VoltageResolution::Millivolt)
            )
            .map(|_| ()),
            Err(NirError::UnitsMismatch {
                file: NirUnits::Simulation,
                options: NirUnits::Native,
            })
        );
        assert_eq!(
            import_chain_variant(&doc, NirImportOptions::sim_units(1_000)).map(|_| ()),
            Err(NirError::UnitsMismatch {
                file: NirUnits::Native,
                options: NirUnits::Simulation,
            })
        );
    }

    #[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::new(1_000, VoltageResolution::Millivolt),
                &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::new(1_000, VoltageResolution::Millivolt),
                    &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::new(1_000, VoltageResolution::Millivolt)
            ),
            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::new(1_000, VoltageResolution::Millivolt));
        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::new(1_000, VoltageResolution::Millivolt));
        let mut out = [0u8; 512];
        assert_eq!(
            nir_export(
                &nodes,
                &[(0, 99)],
                &weights,
                &lifs,
                NirImportOptions::new(1_000, VoltageResolution::Millivolt),
                &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::new(1_000, VoltageResolution::Millivolt),
        )
        .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_f64_max_absmax_is_loud() {
        // absmax/32767 is finite, but the cell at full scale, 32767·scale,
        // rounds to infinity: the record's max_abs_err would be infinite
        // and the export would refuse the weight it writes
        for v in [f64::MAX, -f64::MAX] {
            let mut arena = [0i16; 4];
            assert_eq!(
                quantize_linear(&[1.0, v], 1, 2, &mut arena, 0),
                Err(NirError::BadNumber("weight")),
                "{v:e}"
            );
        }
        // one double below, the cell at full scale is the weight itself
        let below = 1.797_693_134_862_315_5e308;
        assert_eq!(below, f64::MAX.next_down());
        let mut arena = [0i16; 4];
        let lin = quantize_linear(&[below], 1, 1, &mut arena, 0).expect("quantizes");
        assert_eq!(arena[0], 32767);
        assert_eq!(f64::from(arena[0]) * lin.scale, below);
        assert_eq!(lin.max_abs_err, 0.0);
    }

    #[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::new(1_000, VoltageResolution::Millivolt));
        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::new(1_000, VoltageResolution::Millivolt));
        // 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::new(1_000, VoltageResolution::Millivolt));
        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(NirName::new("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::new(1_000, VoltageResolution::Millivolt));
        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(NirName::new("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::new(1_000, VoltageResolution::Millivolt));
        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::new(1_000, VoltageResolution::Millivolt));
        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)));
    }

    /// An Input `i` feeding an Output `o`, each with the shape given as
    /// JSON.
    fn carriers(input: &str, output: &str) -> String {
        format!(
            "{{\"version\":\"test\",\"node\":{{\"type\":\"NIRGraph\",\
             \"edges\":[[\"i\",\"o\"]],\"nodes\":{{\
             \"i\":{{\"type\":\"Input\",\"shape\":{input}}},\
             \"o\":{{\"type\":\"Output\",\"shape\":{output}}}}}}}}}"
        )
    }

    #[test]
    fn an_empty_shape_is_refused_by_the_builder_as_by_the_reader() {
        let opts = NirImportOptions::new(1_000, VoltageResolution::Millivolt);
        let whole = carriers("[1]", "[1]");
        assert!(NirImport::from_json(whole.as_bytes(), opts).is_ok());
        for (input, output) in [("[]", "[1]"), ("[1]", "[]")] {
            let d = carriers(input, output);
            assert_eq!(
                NirImport::from_json(d.as_bytes(), opts).err(),
                Some(NirError::BadShape("shape")),
                "input {input}, output {output}"
            );
        }
        // the builder refuses each by the error it gives past 4 dims,
        // and keeps no node of either
        let mut b = NirBuilder::new(opts);
        assert_eq!(b.add_input("i", &[]), Err(NirError::BadShape("shape")));
        assert_eq!(b.add_output("o", &[]), Err(NirError::BadShape("shape")));
        let i = b.add_input("i", &[1]).expect("input");
        let o = b.add_output("o", &[1]).expect("output");
        assert_eq!((i, o), (0, 1));
    }

    /// A shape the document does not give is missing, the reader's alone
    /// to see: the builder takes a shape with every carrier.
    #[test]
    fn an_absent_shape_is_a_missing_field() {
        let opts = NirImportOptions::new(1_000, VoltageResolution::Millivolt);
        let whole = carriers("[1]", "[1]");
        for (node, kind) in [("i", "Input"), ("o", "Output")] {
            let d = whole.replacen(
                &format!("\"{node}\":{{\"type\":\"{kind}\",\"shape\":[1]}}"),
                &format!("\"{node}\":{{\"type\":\"{kind}\"}}"),
                1,
            );
            assert_ne!(d, whole, "{kind} carries its shape");
            assert_eq!(
                NirImport::from_json(d.as_bytes(), opts).err(),
                Some(NirError::MissingField("shape")),
                "{kind} without its shape"
            );
        }
    }

    #[test]
    fn four_dims_are_taken_and_a_fifth_refused_by_the_builder_as_by_the_reader() {
        // the other end of 1–4: the reader's own shape array holds four
        let opts = NirImportOptions::new(1_000, VoltageResolution::Millivolt);
        let four = carriers("[1,1,1,1]", "[1,1,1,1]");
        assert!(NirImport::from_json(four.as_bytes(), opts).is_ok());
        for (input, output) in [("[1,1,1,1,1]", "[1]"), ("[1]", "[1,1,1,1,1]")] {
            let d = carriers(input, output);
            assert_eq!(
                NirImport::from_json(d.as_bytes(), opts).err(),
                Some(NirError::BadShape("shape")),
                "input {input}, output {output}"
            );
        }
        let mut b = NirBuilder::new(opts);
        let five = [1; 5];
        assert_eq!(b.add_input("i", &five), Err(NirError::BadShape("shape")));
        assert_eq!(b.add_output("o", &five), Err(NirError::BadShape("shape")));
        let i = b.add_input("i", &[1; 4]).expect("input");
        let o = b.add_output("o", &[1; 4]).expect("output");
        assert_eq!((i, o), (0, 1));
    }

    #[test]
    fn a_stage_composed_to_f64_s_largest_is_refused_at_the_build() {
        // build_network quantizes each stage as the import quantizes a
        // weight: 10 then 1.7976931348623158e307, each a Linear that
        // imports, compose to exactly f64's largest (-10 to its
        // negative), 10 then 1e308 past it; 10 then
        // 1.7976931348623155e307 compose under it and build
        let chain = |w1: f64, w2: f64| {
            let mut bld =
                NirBuilder::new(NirImportOptions::new(1_000, VoltageResolution::Millivolt));
            let inp = bld.add_input("input", &[1]).expect("input");
            let l1 = bld.add_linear("l1", &[w1], 1, 1).expect("l1 imports");
            let l2 = bld.add_linear("l2", &[w2], 1, 1).expect("l2 imports");
            let lif = bld
                .add_lif_population("lif", &lif_params_single())
                .expect("lif");
            let out = bld.add_output("output", &[1]).expect("output");
            for (a, c) in [(inp, l1), (l1, l2), (l2, lif), (lif, out)] {
                bld.add_edge(a, c).expect("edge");
            }
            bld.build().expect("builds")
        };
        for (w1, w2) in [
            (10.0, 1.797_693_134_862_315_8e307),
            (-10.0, 1.797_693_134_862_315_8e307),
            (10.0, 1e308),
        ] {
            assert_eq!(
                chain(w1, w2).build_network().err(),
                Some(NirError::BadNumber("weight")),
                "{w1:e} then {w2:e}"
            );
        }
        assert!(chain(10.0, 1.797_693_134_862_315_5e307)
            .build_network()
            .is_ok());
    }

    #[test]
    fn failed_adds_leave_no_zombie_state() {
        let mut b = NirBuilder::new(NirImportOptions::new(1_000, VoltageResolution::Millivolt));
        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);
    }

    // ----- ISA round 70: a test for the nir sentence no test held at
    // 645afa8; run green there and red under its mutant. -----

    /// `NirGraphEncoder::encode` merges its stages saturating in `i16`
    /// (D5): two Inputs, each through its own Linear at full scale, drive
    /// one neuron, each stage's current at its own ceiling, 32,767; the
    /// sum stays there, where a wrapping add would read −2.
    #[test]
    fn two_stages_into_one_neuron_merge_saturating() {
        let mut bld = NirBuilder::new(NirImportOptions::new(1_000, VoltageResolution::Millivolt));
        let a = bld.add_input("a", &[1]).expect("input");
        let b = bld.add_input("b", &[1]).expect("input");
        let la = bld.add_linear("la", &[1.0], 1, 1).expect("linear");
        let lb = bld.add_linear("lb", &[1.0], 1, 1).expect("linear");
        let lif = bld
            .add_lif_population("lif", &lif_params_single())
            .expect("lif");
        let out = bld.add_output("out", &[1]).expect("output");
        for (from, to) in [(a, la), (b, lb), (la, lif), (lb, lif), (lif, out)] {
            bld.add_edge(from, to).expect("edge");
        }
        let (_, enc, _) = bld
            .build()
            .expect("builds")
            .build_network()
            .expect("assembles");
        assert_eq!(enc.stage_count(), 2, "one stage per Input");
        let x = [Thousandths(100_000)];
        assert_eq!(
            enc.encode(&[&x, &[Thousandths(0)]]),
            [32_767],
            "one stage at its ceiling"
        );
        assert_eq!(enc.encode(&[&x, &x]), [32_767], "two, merged saturating");
    }

    // ----- ISA round 70: tests for the nir.rs survivors of a mutation pass
    // at 645afa8, each value measured there and each test run red under the
    // survivors it answers. -----

    const READOUT_DEFERRED: &str =
        "readout (LIF->Linear->Output) deferred — spike-count readout convention not yet named";
    const LINEAR_CYCLE: &str =
        "cycle through Linear nodes — no feedforward evaluation order exists";

    fn mv_options() -> NirImportOptions {
        NirImportOptions::new(1_000, VoltageResolution::Millivolt)
    }

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

    /// `doc` scanned, then imported into buffers its own scan sized,
    /// the report kept (`NirImport::from_json` drops it).
    #[allow(clippy::type_complexity)]
    fn import_with_report(
        doc: &str,
        opts: NirImportOptions,
    ) -> Result<(Vec<NirNode<'_>>, Vec<i16>, Vec<NirLif>, NirReport), NirError> {
        let scan = nir_scan(doc.as_bytes())?;
        let mut nodes = vec![NirNode::BLANK; 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::BLANK; 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(doc.as_bytes(), opts, &mut bufs)?;
        Ok((nodes, weights, lifs, report))
    }

    /// A LIF population of `n` at the chain's biology: τ 20 ms, 100 MΩ,
    /// −70 / −55 / −80 mV.
    fn population(b: &mut NirBuilder<'_>, name: &'static str, n: usize) -> usize {
        b.add_lif_population(
            name,
            &NirLifParams {
                tau_s: &vec![0.02; n],
                r_ohm: &vec![1e8; n],
                v_leak_v: &vec![-0.07; n],
                v_threshold_v: &vec![-0.055; n],
                v_reset_v: Some(&vec![-0.08; n]),
            },
        )
        .expect("a biological population")
    }

    /// A `rows × cols` Linear, its weights 0.5, −0.25, 1.0, 0.75 over
    /// and over.
    fn dense(b: &mut NirBuilder<'_>, name: &'static str, rows: usize, cols: usize) -> usize {
        let w: Vec<f64> = (0..rows * cols)
            .map(|i| [0.5, -0.25, 1.0, 0.75][i % 4])
            .collect();
        b.add_linear(name, &w, rows, cols).expect("a Linear")
    }

    /// The builder's nodes wired by these edges.
    fn wired(b: &mut NirBuilder<'_>, edges: &[(usize, usize)]) {
        for &(from, to) in edges {
            b.add_edge(from, to).expect("indices from the builder");
        }
    }

    /// Each note counts what its doc names, once per neuron or tensor.
    /// Neuron 0's tau rounds up to 100,000 µs (the note reads the
    /// error's size, not its sign), its threshold loses half a
    /// millivolt and its capacitance, 100,000 pF, is clamped. Neuron 1's
    /// reset loses half a millivolt and its capacitance, 0.3 pF, rounds
    /// to 0. Neuron 2 loses nothing, its capacitance exactly 65,535 pF
    /// and not clamped. A Linear at scale 1 loses nothing; the same
    /// Linear all zeros is noted once. Every count is exact, and
    /// `note_count` is their sum: one tau note and two potential notes,
    /// since 2 · 2 is 2 + 2, and a `+` made `*` in the sum would not show.
    #[test]
    fn each_note_counts_what_its_doc_names() {
        let doc = "{\"version\":\"t\",\"node\":{\"type\":\"NIRGraph\",\
\"edges\":[[\"in\",\"lin\"],[\"lin\",\"lif\"],[\"lif\",\"out\"]],\
\"nodes\":{\"in\":{\"type\":\"Input\",\"shape\":[2]},\
\"lin\":{\"type\":\"Linear\",\"weight\":[[32767.0,-1.0],[0.0,2.0],[-3.0,4.0]]},\
\"lif\":{\"type\":\"LIF\",\"tau\":[0.0999996,0.02,0.196605],\
\"r\":[1000000.0,65535000000.0,3000000.0],\"v_leak\":[-0.07,-0.07,-0.07],\
\"v_threshold\":[-0.0555,-0.05,-0.05],\"v_reset\":[-0.08,-0.0805,-0.08]},\
\"out\":{\"type\":\"Output\",\"shape\":[3]}}}}";
        let counts = |r: &NirReport| {
            [
                NirNote::VResetDefaulted,
                NirNote::CapacitanceClamped,
                NirNote::TauTruncated,
                NirNote::PotentialTruncated,
                NirNote::ZeroWeightTensor,
                NirNote::QuantizationLoss,
            ]
            .map(|n| r.notes(n))
        };
        let (nodes, _, lifs, report) = import_with_report(doc, mv_options()).expect("imports");
        assert_eq!(counts(&report), [0, 1, 1, 2, 0, 0]);
        assert_eq!(report.note_count(), 4);
        let taus: Vec<u32> = lifs.iter().map(|l| l.tau_us).collect();
        assert_eq!(taus, [100_000, 20_000, 196_605]);
        assert!(lifs[0].tau_err_s > 0.0, "rounded up");
        assert_eq!((lifs[1].tau_err_s, lifs[2].tau_err_s), (0.0, 0.0));
        let c: Vec<(u16, bool)> = lifs
            .iter()
            .map(|l| (l.capacitance_pf, l.capacitance_clamped))
            .collect();
        assert_eq!(c, [(65_535, true), (0, false), (65_535, false)]);
        let v: Vec<(i16, i16, f64)> = lifs
            .iter()
            .map(|l| (l.threshold_q, l.reset_q, l.max_v_err_v))
            .collect();
        assert_eq!(v, [(-56, -80, 5e-4), (-50, -81, 5e-4), (-50, -80, 0.0)]);
        let lin = nodes[1].linear.expect("a Linear");
        assert_eq!(
            (lin.scale, lin.max_abs_err, lin.zero_tensor),
            (1.0, 0.0, false)
        );
        let zeros = doc.replace(
            "[[32767.0,-1.0],[0.0,2.0],[-3.0,4.0]]",
            "[[0.0,0.0],[0.0,0.0],[0.0,0.0]]",
        );
        let (_, _, _, report) = import_with_report(&zeros, mv_options()).expect("imports");
        assert_eq!(counts(&report), [0, 1, 1, 2, 1, 0]);
        assert_eq!(report.note_count(), 5);
    }

    /// A shape's dimension is a `u32`: −1 and 2³² are refused by name,
    /// 2³² − 1 is taken whole.
    #[test]
    fn a_shape_dimension_outside_u32_is_refused() {
        let with_dim = |d: &str| {
            let doc = CHAIN.replace("\"shape\":[3]", &format!("\"shape\":[{d}]"));
            assert_ne!(doc, CHAIN, "the Input takes {d}");
            doc
        };
        for d in ["-1", "4294967296"] {
            let refused = import_alone(&with_dim(d));
            assert_eq!(refused, Err(NirError::BadShape("shape")), "{d}");
        }
        let widest = with_dim("4294967295");
        let (nodes, ..) = import_chain_variant(&widest, mv_options()).expect("imports");
        assert_eq!((nodes[0].shape[0], nodes[0].shape_len), (u32::MAX, 1));
    }

    /// The graph encoder's accessors read its own fields: two Inputs of
    /// two features and one, each through its own Linear into one
    /// neuron, give two inputs, their widths (0 past the last), two
    /// stages and each one's quanta (none past the last). Stage 0 is
    /// `lb`'s: the stages follow the Linears' order, not the Inputs'.
    #[test]
    fn the_graph_encoder_reads_its_inputs_and_stages() {
        let mut b = NirBuilder::new(mv_options());
        let ia = b.add_input("a", &[2]).expect("input");
        let ib = b.add_input("b", &[1]).expect("input");
        let la = b.add_linear("la", &[1.0, -0.5], 1, 2).expect("linear");
        let lb = b.add_linear("lb", &[2.0], 1, 1).expect("linear");
        let lif = b
            .add_lif_population("lif", &lif_params_single())
            .expect("lif");
        let out = b.add_output("out", &[1]).expect("output");
        wired(
            &mut b,
            &[(ia, la), (ib, lb), (la, lif), (lb, lif), (lif, out)],
        );
        let (_, enc, _) = b
            .build()
            .expect("builds")
            .build_network()
            .expect("assembles");
        assert_eq!(enc.input_count(), 2);
        let widths = [0, 1, 2].map(|i| enc.input_features(i));
        assert_eq!(widths, [2, 1, 0]);
        assert_eq!(enc.stage_count(), 2);
        assert_eq!(enc.stage_quanta(0), Some(&[32_767][..]));
        assert_eq!(enc.stage_quanta(1), Some(&[32_767, -16_384][..]));
        assert_eq!(enc.stage_quanta(2), None);
    }

    /// `build_chain_network` refuses what is not the chain, by name: a
    /// fifth node, the Output fed by the Linear, and an Input narrower
    /// than the Linear's columns.
    #[test]
    fn the_chain_builder_refuses_what_is_not_the_chain() {
        let refusal = |doc: &str| {
            assert_ne!(doc, CHAIN, "a variant of the chain");
            NirImport::from_json(doc.as_bytes(), mv_options())
                .expect("imports")
                .build_chain_network()
                .err()
        };
        let fifth = CHAIN.replacen(
            "\"output\":{",
            "\"extra\":{\"type\":\"Output\",\"shape\":[2]},\"output\":{",
            1,
        );
        assert_eq!(
            refusal(&fifth),
            Some(NirError::UnsupportedTopology(
                "exactly 4 nodes (Input-Linear-LIF-Output) in slice 1"
            ))
        );
        let rewired = CHAIN.replace("[\"lif\",\"output\"]", "[\"linear\",\"output\"]");
        assert_eq!(
            refusal(&rewired),
            Some(NirError::UnsupportedTopology(
                "edges must be Input->Linear->LIF->Output"
            ))
        );
        let narrow = CHAIN.replace("\"shape\":[3]", "\"shape\":[2]");
        assert_eq!(
            refusal(&narrow),
            Some(NirError::UnsupportedTopology("Input shape != Linear cols"))
        );
    }

    /// `build_chain_network` gives each neuron its own record: two
    /// neurons whose five parameters all differ keep their own. And the
    /// chain's encoder sums each row of its own weights, a missing
    /// feature reading 0, and divides by the gain, truncating: row 0 is
    /// (16,384 · 25 − 32,767 · 3) / 100 = 3,112, row 1 is
    /// 24,575 · 3 / 100 = 737.
    #[test]
    fn the_chain_builder_gives_each_neuron_its_record_and_each_row_its_weights() {
        let doc = CHAIN
            .replace("\"tau\":[0.02,0.02]", "\"tau\":[0.02,0.03]")
            .replace(
                "\"r\":[100000000.0,100000000.0]",
                "\"r\":[100000000.0,200000000.0]",
            )
            .replace("\"v_leak\":[-0.07,-0.07]", "\"v_leak\":[-0.07,-0.065]")
            .replace(
                "\"v_threshold\":[-0.055,-0.055]",
                "\"v_threshold\":[-0.055,-0.05]",
            )
            .replace("\"v_reset\":[-0.08,-0.08]", "\"v_reset\":[-0.08,-0.075]");
        let g = NirImport::from_json(doc.as_bytes(), mv_options()).expect("imports");
        let (net, enc) = g.build_chain_network().expect("the chain");
        let records: Vec<_> = net
            .neurons()
            .iter()
            .map(|n| {
                (
                    n.resting_potential,
                    n.membrane_potential,
                    n.threshold,
                    n.reset_potential,
                    n.tau_membrane_us,
                    n.resistance_mohm,
                )
            })
            .collect();
        assert_eq!(
            records,
            [
                (-70, -70, -55, -80, 20_000, 100),
                (-65, -65, -50, -75, 30_000, 200)
            ]
        );
        assert_eq!((enc.rows(), enc.cols()), (2, 3));
        assert_eq!(enc.encode(&[25, 3]), [3_112, 737]);
        assert_eq!(
            enc.encode(&[25, 3, 0]),
            [3_112, 737],
            "the missing feature is 0"
        );
    }

    /// D7, the u16 neuron-id bound, at its edge: a chain of 65,535
    /// neurons assembles by both builders, and 65,536 is refused by
    /// both.
    #[test]
    fn sixty_five_thousand_five_hundred_thirty_five_neurons_assemble_and_no_more() {
        for n in [65_535usize, 65_536] {
            let mut b = NirBuilder::new(mv_options());
            let input = b.add_input("in", &[1]).expect("input");
            let lin = b.add_linear("lin", &vec![0.5; n], n, 1).expect("linear");
            let lif = population(&mut b, "lif", n);
            let width = u32::try_from(n).expect("fits");
            let out = b.add_output("out", &[width]).expect("output");
            wired(&mut b, &[(input, lin), (lin, lif), (lif, out)]);
            let g = b.build().expect("builds");
            let chain = g.build_chain_network().map(|(net, _)| net.neuron_count());
            let full = g
                .build_network()
                .map(|(net, _, report)| (net.neuron_count(), report.neurons));
            if n == 65_535 {
                assert_eq!(chain, Ok(65_535));
                assert_eq!(full, Ok((65_535, 65_535)));
            } else {
                assert_eq!(chain, Err(NirError::BufferOverflow));
                assert_eq!(full, Err(NirError::BufferOverflow));
            }
        }
    }

    /// Every edge kind's shapes are checked, and a mismatch is refused
    /// with both node names: Linear→Linear (rows into columns),
    /// LIF→LIF (sizes), LIF→Linear (size into columns), LIF→Output and
    /// Linear→Output (the Output's one dimension against the size, or
    /// the rows of a Linear beside its LIF).
    #[test]
    fn every_edge_kind_refuses_a_shape_mismatch_by_name() {
        let mismatch = |src: &str, dst: &str| {
            Some(NirError::EdgeShapeMismatch {
                src: NirName::new(src),
                dst: NirName::new(dst),
            })
        };
        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[2]).expect("input");
        let l1 = dense(&mut bld, "l1", 3, 2);
        let l2 = dense(&mut bld, "l2", 2, 4);
        let lif_a = population(&mut bld, "a", 2);
        let out = bld.add_output("out", &[2]).expect("output");
        wired(
            &mut bld,
            &[(input, l1), (l1, l2), (l2, lif_a), (lif_a, out)],
        );
        let err = bld.build().expect("builds").build_network().err();
        assert_eq!(err, mismatch("l1", "l2"), "Linear -> Linear");

        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[1]).expect("input");
        let lin = dense(&mut bld, "l", 2, 1);
        let lif_a = population(&mut bld, "a", 2);
        let lif_c = population(&mut bld, "c", 3);
        let out = bld.add_output("out", &[3]).expect("output");
        wired(
            &mut bld,
            &[(input, lin), (lin, lif_a), (lif_a, lif_c), (lif_c, out)],
        );
        let err = bld.build().expect("builds").build_network().err();
        assert_eq!(err, mismatch("a", "c"), "LIF -> LIF");

        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[1]).expect("input");
        let l0 = dense(&mut bld, "l0", 2, 1);
        let lif_a = population(&mut bld, "a", 2);
        let l1 = dense(&mut bld, "l1", 2, 3);
        let lif_c = population(&mut bld, "c", 2);
        let out = bld.add_output("out", &[2]).expect("output");
        wired(
            &mut bld,
            &[
                (input, l0),
                (l0, lif_a),
                (lif_a, l1),
                (l1, lif_c),
                (lif_c, out),
            ],
        );
        let err = bld.build().expect("builds").build_network().err();
        assert_eq!(err, mismatch("a", "l1"), "LIF -> Linear");

        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[1]).expect("input");
        let lin = dense(&mut bld, "l", 2, 1);
        let lif_a = population(&mut bld, "a", 2);
        let out = bld.add_output("out", &[3]).expect("output");
        wired(&mut bld, &[(input, lin), (lin, lif_a), (lif_a, out)]);
        let err = bld.build().expect("builds").build_network().err();
        assert_eq!(err, mismatch("a", "out"), "LIF -> Output");

        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[1]).expect("input");
        let lin = dense(&mut bld, "l", 2, 1);
        let lif_a = population(&mut bld, "a", 2);
        let out = bld.add_output("out", &[2]).expect("output");
        let out2 = bld.add_output("out2", &[3]).expect("output");
        wired(
            &mut bld,
            &[(input, lin), (lin, lif_a), (lif_a, out), (lin, out2)],
        );
        let err = bld.build().expect("builds").build_network().err();
        assert_eq!(err, mismatch("l", "out2"), "Linear -> Output");
    }

    /// D8's reach crosses Linear→Linear edges: a spike two Linears deep
    /// into an Output is the deferred readout, as one Linear deep is.
    #[test]
    fn a_spike_two_linears_deep_into_an_output_is_the_readout() {
        let mut bld = NirBuilder::new(centi_options());
        let input = bld.add_input("in", &[2]).expect("input");
        let l0 = dense(&mut bld, "l0", 2, 2);
        let lif_a = population(&mut bld, "a", 2);
        let out = bld.add_output("out", &[2]).expect("output");
        let l1 = dense(&mut bld, "l1", 2, 2);
        let l2 = dense(&mut bld, "l2", 2, 2);
        let out2 = bld.add_output("out2", &[2]).expect("output");
        wired(
            &mut bld,
            &[
                (input, l0),
                (l0, lif_a),
                (lif_a, out),
                (lif_a, l1),
                (l1, l2),
                (l2, out2),
            ],
        );
        let err = bld.build().expect("builds").build_network().err();
        assert_eq!(err, Some(NirError::UnsupportedTopology(READOUT_DEFERRED)));
    }

    /// A Linear cycle a spike reaches is refused by name at the
    /// ordering: D8's reach walks it first and visits each Linear once.
    #[test]
    fn a_linear_cycle_a_spike_reaches_is_refused_by_name() {
        let mut bld = NirBuilder::new(centi_options());
        let input = bld.add_input("in", &[2]).expect("input");
        let l0 = dense(&mut bld, "l0", 2, 2);
        let lif_a = population(&mut bld, "a", 2);
        let out = bld.add_output("out", &[2]).expect("output");
        let l1 = dense(&mut bld, "l1", 2, 2);
        let l2 = dense(&mut bld, "l2", 2, 2);
        wired(
            &mut bld,
            &[
                (input, l0),
                (l0, lif_a),
                (lif_a, out),
                (lif_a, l1),
                (l1, l2),
                (l2, l1),
            ],
        );
        let err = bld.build().expect("builds").build_network().err();
        assert_eq!(err, Some(NirError::UnsupportedTopology(LINEAR_CYCLE)));
    }

    /// A Linear cycle is found past the first Linear the ordering walks
    /// from: in l0 → l1 → l2 → l1 the cycle is l1's and l2's.
    #[test]
    fn a_linear_cycle_past_the_first_linear_is_refused_by_name() {
        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[2]).expect("input");
        let l0 = dense(&mut bld, "l0", 2, 2);
        let l1 = dense(&mut bld, "l1", 2, 2);
        let l2 = dense(&mut bld, "l2", 2, 2);
        let lif_a = population(&mut bld, "a", 2);
        let out = bld.add_output("out", &[2]).expect("output");
        wired(
            &mut bld,
            &[
                (input, l0),
                (l0, l1),
                (l1, l2),
                (l2, l1),
                (l2, lif_a),
                (lif_a, out),
            ],
        );
        let err = bld.build().expect("builds").build_network().err();
        assert_eq!(err, Some(NirError::UnsupportedTopology(LINEAR_CYCLE)));
    }

    /// A Linear feeding two Linears feeds both: l0 fans out to l1 and
    /// l2, each into its own population, and each stage is l0 composed
    /// with its own Linear (l1 is the identity, l2 swaps the rows).
    #[test]
    fn a_linear_fanning_out_to_two_linears_feeds_both() {
        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[2]).expect("input");
        let l0 = bld
            .add_linear("l0", &[1.0, 0.5, -0.5, 1.0], 2, 2)
            .expect("linear");
        let l1 = bld
            .add_linear("l1", &[1.0, 0.0, 0.0, 1.0], 2, 2)
            .expect("linear");
        let lif_a = population(&mut bld, "a", 2);
        let out = bld.add_output("out", &[2]).expect("output");
        let l2 = bld
            .add_linear("l2", &[0.0, 1.0, 1.0, 0.0], 2, 2)
            .expect("linear");
        let lif_c = population(&mut bld, "c", 2);
        let out2 = bld.add_output("out2", &[2]).expect("output");
        wired(
            &mut bld,
            &[
                (input, l0),
                (l0, l1),
                (l1, lif_a),
                (lif_a, out),
                (l0, l2),
                (l2, lif_c),
                (lif_c, out2),
            ],
        );
        let g = bld.build().expect("builds");
        let (_, enc, report) = g.build_network().expect("assembles");
        assert_eq!(enc.stage_count(), 2);
        assert_eq!(
            enc.stage_quanta(0),
            Some(&[-16_384, 32_767, 32_767, 16_384][..])
        );
        assert_eq!(
            enc.stage_quanta(1),
            Some(&[32_767, 16_384, -16_384, 32_767][..])
        );
        let chains: Vec<&[&str]> = report.fused.iter().map(|f| f.chain.as_slice()).collect();
        assert_eq!(chains, [&["l0", "l2"][..], &["l0", "l1"][..]]);
        let x = [Thousandths(1_000), Thousandths(2_000)];
        assert_eq!(enc.encode(&[&x]), [655, 491, 491, 655]);
    }

    /// A diamond names each contributor once: l0 reaches l3 through l1
    /// and through l2, and l3's fusion record lists l0 once.
    #[test]
    fn a_diamond_names_each_contributor_once() {
        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[2]).expect("input");
        let l0 = dense(&mut bld, "l0", 2, 2);
        let l1 = dense(&mut bld, "l1", 2, 2);
        let l2 = dense(&mut bld, "l2", 2, 2);
        let l3 = dense(&mut bld, "l3", 2, 2);
        let lif_a = population(&mut bld, "a", 2);
        let out = bld.add_output("out", &[2]).expect("output");
        wired(
            &mut bld,
            &[
                (input, l0),
                (l0, l1),
                (l0, l2),
                (l1, l3),
                (l2, l3),
                (l3, lif_a),
                (lif_a, out),
            ],
        );
        let g = bld.build().expect("builds");
        let (_, _, report) = g.build_network().expect("assembles");
        let chains: Vec<&[&str]> = report.fused.iter().map(|f| f.chain.as_slice()).collect();
        assert_eq!(chains, [&["l0", "l1", "l2", "l3"][..]]);
    }

    /// A path whose weight is an exact zero carries nothing, an
    /// overflowed one too: l2 · l1 overflows to infinity in one row,
    /// l3's weight on that row is 0, and the stage it reaches is 0, not
    /// 0 · ∞, a NaN refused as a non-finite weight.
    #[test]
    fn a_zero_weight_carries_no_overflowed_path() {
        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[1]).expect("input");
        let l1 = bld.add_linear("l1", &[1e308, 1.0], 2, 1).expect("linear");
        let l2 = bld
            .add_linear("l2", &[1e308, 0.0, 0.0, 1e308], 2, 2)
            .expect("linear");
        let l3 = bld.add_linear("l3", &[0.0, 1.0], 1, 2).expect("linear");
        let l4 = bld.add_linear("l4", &[1.0], 1, 1).expect("linear");
        let lif_a = population(&mut bld, "a", 1);
        let out = bld.add_output("out", &[1]).expect("output");
        wired(
            &mut bld,
            &[
                (input, l1),
                (l1, l2),
                (l2, l3),
                (l3, l4),
                (l4, lif_a),
                (lif_a, out),
            ],
        );
        let g = bld.build().expect("builds");
        let (_, enc, report) = g.build_network().expect("the zero stops the overflow");
        assert_eq!(enc.stage_quanta(0), Some(&[0][..]));
        let chains: Vec<&[&str]> = report.fused.iter().map(|f| f.chain.as_slice()).collect();
        assert_eq!(chains, [&["l1", "l2", "l3", "l4"][..]]);
    }

    /// The undriven notes name a Linear never invoked, as their doc
    /// says, beside a LIF no Input reaches: `ldec` has an Input's path
    /// and none to a LIF, `lorph` has no root path, and `c`, which only
    /// `lorph` feeds, has no Input-rooted path. In node order.
    #[test]
    fn undriven_linears_are_noted_by_name_beside_the_populations() {
        let mut bld = NirBuilder::new(mv_options());
        let input = bld.add_input("in", &[2]).expect("input");
        let l0 = dense(&mut bld, "l0", 2, 2);
        let lif_a = population(&mut bld, "a", 2);
        let out = bld.add_output("out", &[2]).expect("output");
        let ldec = dense(&mut bld, "ldec", 2, 2);
        let out2 = bld.add_output("out2", &[2]).expect("output");
        let lorph = dense(&mut bld, "lorph", 2, 2);
        let lif_c = population(&mut bld, "c", 2);
        let out3 = bld.add_output("out3", &[2]).expect("output");
        wired(
            &mut bld,
            &[
                (input, l0),
                (l0, lif_a),
                (lif_a, out),
                (input, ldec),
                (ldec, out2),
                (lorph, lif_c),
                (lif_c, out3),
            ],
        );
        let g = bld.build().expect("builds");
        let (_, _, report) = g.build_network().expect("assembles with the notes");
        assert_eq!(report.undriven, ["ldec", "lorph", "c"]);
    }

    /// `add_lif_population` refuses an empty population, and each field
    /// of another length than `tau`'s, `v_reset` included, as
    /// `BadShape("LIF param")`.
    #[test]
    fn the_builder_refuses_each_lif_field_of_another_length() {
        fn params<'v>(
            tau: &'v [f64],
            r: &'v [f64],
            l: &'v [f64],
            t: &'v [f64],
            v: &'v [f64],
        ) -> NirLifParams<'v> {
            NirLifParams {
                tau_s: tau,
                r_ohm: r,
                v_leak_v: l,
                v_threshold_v: t,
                v_reset_v: Some(v),
            }
        }
        let (one, two) = ([0.02], [0.02, 0.02]);
        let (r1, r2) = ([1e8], [1e8, 1e8]);
        let (l1, l2) = ([-0.07], [-0.07, -0.07]);
        let (t1, t2) = ([-0.055], [-0.055, -0.055]);
        let (v1, v2) = ([-0.08], [-0.08, -0.08]);
        let empty = NirLifParams {
            tau_s: &[],
            r_ohm: &[],
            v_leak_v: &[],
            v_threshold_v: &[],
            v_reset_v: None,
        };
        let cases = [
            ("empty", empty),
            ("r", params(&two, &r1, &l2, &t2, &v2)),
            ("v_leak", params(&two, &r2, &l1, &t2, &v2)),
            ("v_threshold", params(&two, &r2, &l2, &t1, &v2)),
            ("v_reset", params(&two, &r2, &l2, &t2, &v1)),
            ("tau", params(&one, &r2, &l2, &t2, &v2)),
        ];
        for (field, p) in &cases {
            let mut b = NirBuilder::new(mv_options());
            let refused = b.add_lif_population("lif", p);
            assert_eq!(refused, Err(NirError::BadShape("LIF param")), "{field}");
        }
    }

    /// Whitespace between tokens is JSON's: the chain written with
    /// Python's default separators, `", "` and `": "`, imports as the
    /// compact one, a space before each number past an array's first,
    /// in a shape of two dimensions too, the one array whose numbers are
    /// read with no look ahead first.
    #[test]
    fn python_s_default_separators_import_as_the_compact_chain() {
        let compact = CHAIN.replace("\"shape\":[3]", "\"shape\":[3,1]");
        assert_ne!(compact, CHAIN, "the Input takes two dimensions");
        let spaced = compact.replace(',', ", ").replace(':', ": ");
        let (nodes, edges, weights, lifs, _) =
            import_chain_variant(&compact, mv_options()).expect("imports");
        assert_eq!(nodes[0].shape[..2], [3, 1]);
        let again = import_chain_variant(&spaced, mv_options()).expect("imports");
        assert_eq!(again.0, nodes);
        assert_eq!(again.1, edges);
        assert_eq!(again.2, weights);
        assert_eq!(again.3, lifs);
    }

    /// The graph's metadata is skipped whole, any JSON value in it: an
    /// empty string, escaped quotes, an escaped backslash, `null`,
    /// `true`, `false`, a number and nested containers.
    #[test]
    fn metadata_takes_every_json_value() {
        for meta in [
            "{\"a\":\"\"}",
            "{\"a\":\"say \\\"hi\\\"\",\"b\":1}",
            "{\"a\":\"\\\\\",\"b\":1}",
            "{\"a\":null,\"b\":true,\"c\":false,\"d\":-1.5e3,\"e\":[1,{\"f\":null}]}",
        ] {
            let doc = CHAIN.replacen(
                "\"type\":\"NIRGraph\",",
                &format!("\"type\":\"NIRGraph\",\"metadata\":{meta},"),
                1,
            );
            assert!(nir_scan(doc.as_bytes()).is_ok(), "the scan, {meta}");
            assert_eq!(import_alone(&doc), Ok(()), "the import alone, {meta}");
        }
    }

    /// A value JSON refuses is refused at its first byte in metadata
    /// too: a misspelled literal, and a number that starts with `+` or
    /// `.`, which the skip does not take for a number.
    #[test]
    fn a_value_json_refuses_is_refused_at_its_byte_in_metadata() {
        for meta in ["{\"a\":nulx}", "{\"a\":tru}", "{\"a\":+1}", "{\"a\":.5}"] {
            let doc = CHAIN.replacen(
                "\"type\":\"NIRGraph\",",
                &format!("\"type\":\"NIRGraph\",\"metadata\":{meta},"),
                1,
            );
            let at = doc.find("\"metadata\"").expect("metadata") + "\"metadata\":{\"a\":".len();
            assert_eq!(
                nir_scan(doc.as_bytes()).err(),
                Some(NirError::Json(at)),
                "{meta}"
            );
        }
    }

    /// A missing comma is refused at the byte where it was due, by the
    /// scan and by the import alone.
    #[test]
    fn a_missing_comma_is_refused_where_it_was_due() {
        let doc = CHAIN.replace("\"tau\":[0.02,0.02]", "\"tau\":[0.02 0.02]");
        let at = doc.find("0.02 0.02").expect("the pair") + "0.02 ".len();
        assert_eq!(nir_scan(doc.as_bytes()).err(), Some(NirError::Json(at)));
        assert_eq!(import_alone(&doc), Err(NirError::Json(at)));
    }

    /// The graph object's `type` is `NIRGraph`; another is refused by
    /// the scan, by name.
    #[test]
    fn a_graph_of_another_type_is_refused_by_the_scan() {
        let doc = CHAIN.replace("\"type\":\"NIRGraph\"", "\"type\":\"Graph\"");
        assert_ne!(doc, CHAIN);
        let refused = nir_scan(doc.as_bytes()).err();
        assert_eq!(refused, Some(NirError::BadShape("node.type")));
    }

    /// `tau < dt` is the refusal, so a tau equal to dt imports; 1 µs
    /// less is refused.
    #[test]
    fn a_tau_equal_to_dt_imports() {
        let doc = CHAIN.replace("\"tau\":[0.02,0.02]", "\"tau\":[0.001,0.001]");
        let (.., lifs, _) = import_chain_variant(&doc, mv_options()).expect("tau = dt imports");
        assert_eq!(lifs[0].tau_us, 1_000);
        let below = CHAIN.replace("\"tau\":[0.02,0.02]", "\"tau\":[0.000999,0.000999]");
        assert_eq!(import_alone(&below), Err(NirError::TauBelowDt));
    }

    /// The membrane's floor is inside its bounds: a reset at −100 mV
    /// imports, and half a millivolt lower is refused by name.
    #[test]
    fn a_potential_at_the_membrane_floor_imports() {
        let at = CHAIN.replace("\"v_reset\":[-0.08,-0.08]", "\"v_reset\":[-0.1,-0.1]");
        let (.., lifs, _) = import_chain_variant(&at, mv_options()).expect("the floor imports");
        assert_eq!(lifs[0].reset_q, MEMBRANE_MV_MIN);
        let under = CHAIN.replace("\"v_reset\":[-0.08,-0.08]", "\"v_reset\":[-0.1005,-0.1005]");
        assert_eq!(
            import_alone(&under),
            Err(NirError::PotentialOutOfRange("v_reset"))
        );
    }

    /// A LIF population of no neurons is refused by name, though the
    /// scan counts it, as 0 neurons.
    #[test]
    fn an_empty_lif_population_is_refused() {
        let doc = CHAIN
            .replace("\"tau\":[0.02,0.02]", "\"tau\":[]")
            .replace("\"r\":[100000000.0,100000000.0]", "\"r\":[]")
            .replace("\"v_leak\":[-0.07,-0.07]", "\"v_leak\":[]")
            .replace("\"v_threshold\":[-0.055,-0.055]", "\"v_threshold\":[]")
            .replace("\"v_reset\":[-0.08,-0.08]", "\"v_reset\":[]");
        let scan = nir_scan(doc.as_bytes()).expect("the scan counts it");
        assert_eq!(scan.lif_neurons, 0);
        let refused = NirImport::from_json(doc.as_bytes(), mv_options()).err();
        assert_eq!(refused, Some(NirError::BadShape("tau")));
    }

    /// Each malformed shape is refused by the scan and by the import
    /// alone, by name: an edge of three names or of one, a flat weight,
    /// an empty row, ragged rows (the import's alone: the scan counts
    /// cells), a nested `tau`, and an `r` or a `v_reset` longer than
    /// `tau`, which the scan names a LIF param and the import by field.
    #[test]
    fn each_malformed_shape_is_refused_by_the_scan_and_by_the_import_alone() {
        use NirError::BadShape;
        let weight = "[[0.5,-1.0,0.25],[0.0,0.75,-0.5]]";
        let edge = "[\"lif\",\"output\"]";
        let cases = [
            (
                edge,
                "[\"lif\",\"output\",\"input\"]",
                Some(BadShape("edges")),
                BadShape("edges"),
            ),
            (
                edge,
                "[\"lif\"]",
                Some(BadShape("edges")),
                BadShape("edges"),
            ),
            (
                weight,
                "[0.5,-1.0,0.25]",
                Some(BadShape("weight")),
                BadShape("weight"),
            ),
            (weight, "[[]]", Some(BadShape("weight")), BadShape("weight")),
            (
                weight,
                "[[0.5,-1.0,0.25],[0.0,0.75]]",
                None,
                BadShape("weight"),
            ),
            (
                "\"tau\":[0.02,0.02]",
                "\"tau\":[[0.02],[0.02]]",
                Some(BadShape("LIF param")),
                BadShape("tau"),
            ),
            (
                "\"r\":[100000000.0,100000000.0]",
                "\"r\":[100000000.0,100000000.0,100000000.0]",
                Some(BadShape("LIF param")),
                BadShape("r"),
            ),
            (
                "\"v_reset\":[-0.08,-0.08]",
                "\"v_reset\":[-0.08,-0.08,-0.08]",
                Some(BadShape("LIF param")),
                BadShape("v_reset"),
            ),
        ];
        for (from, to, scan, alone) in cases {
            let doc = CHAIN.replacen(from, to, 1);
            assert_ne!(doc, CHAIN, "{to}");
            assert_eq!(nir_scan(doc.as_bytes()).err(), scan, "the scan, {to}");
            assert_eq!(import_alone(&doc), Err(alone), "the import alone, {to}");
        }
    }

    /// The export marks an all-zero tensor in its `quant` block, as the
    /// import recorded it.
    #[test]
    fn the_export_marks_a_zero_tensor() {
        let doc = "{\"version\":\"x\",\"node\":{\"type\":\"NIRGraph\",\"edges\":[],\
\"nodes\":{\"a\":{\"type\":\"Linear\",\"weight\":[[0.0,0.0],[0.0,0.0]]}}}}";
        let (nodes, weights, lifs, _) = import_with_report(doc, mv_options()).expect("imports");
        let mut out = [0u8; 1024];
        let n = nir_export(&nodes, &[], &weights, &lifs, mv_options(), &mut out).expect("exports");
        let text = core::str::from_utf8(&out[..n]).expect("utf8");
        assert!(text.contains("\"zero_tensor\":true"), "{text}");
    }

    // ----- ISA round 70: the tests that the chain builder takes its three
    // edges, and its nodes, in any order; the first red at 645afa8 and
    // green with the change. -----

    /// `build_chain_network` takes the chain's three edges in any order:
    /// an exporter lists them in its own (snnTorch's follows Python's
    /// hash seed), and the reordered chain builds the same network and
    /// encoder as the ordered one.
    #[test]
    fn the_chain_builds_from_its_edges_in_any_order() {
        let reordered = CHAIN.replace(
            "[[\"input\",\"linear\"],[\"linear\",\"lif\"],[\"lif\",\"output\"]]",
            "[[\"linear\",\"lif\"],[\"lif\",\"output\"],[\"input\",\"linear\"]]",
        );
        assert_ne!(reordered, CHAIN, "the edges reordered");
        let ordered = NirImport::from_json(CHAIN.as_bytes(), mv_options()).expect("imports");
        let other = NirImport::from_json(reordered.as_bytes(), mv_options()).expect("imports");
        let (net, enc) = ordered.build_chain_network().expect("the chain");
        let (net2, enc2) = other
            .build_chain_network()
            .expect("the chain, its edges reordered");
        assert_eq!(net2.neuron_count(), net.neuron_count());
        assert_eq!(enc2.encode(&[25, 3, 7]), enc.encode(&[25, 3, 7]));
    }

    /// The chain's nodes in another order build too: a graph lists its
    /// nodes in its exporter's order, and listed by name (`input`, `lif`,
    /// `linear`, `output`) the chain's three edges, in chain order, are out
    /// of index order. The same network and encoder as the chain listed in
    /// chain order.
    #[test]
    fn the_chain_builds_from_its_nodes_in_any_order() {
        let by_name = "{\"version\":\"test\",\"node\":{\"type\":\"NIRGraph\",\
\"edges\":[[\"input\",\"linear\"],[\"linear\",\"lif\"],[\"lif\",\"output\"]],\
\"nodes\":{\"input\":{\"type\":\"Input\",\"shape\":[3]},\
\"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]},\
\"linear\":{\"type\":\"Linear\",\"weight\":[[0.5,-1.0,0.25],[0.0,0.75,-0.5]]},\
\"output\":{\"type\":\"Output\",\"shape\":[2]}}}}";
        let ordered = NirImport::from_json(CHAIN.as_bytes(), mv_options()).expect("imports");
        let other = NirImport::from_json(by_name.as_bytes(), mv_options()).expect("imports");
        let names: Vec<&str> = other.nodes.iter().map(|n| n.name).collect();
        assert_eq!(names, ["input", "lif", "linear", "output"]);
        let (net, enc) = ordered.build_chain_network().expect("the chain");
        let (net2, enc2) = other
            .build_chain_network()
            .expect("the chain, its nodes by name");
        assert_eq!(net2.neuron_count(), net.neuron_count());
        assert_eq!(enc2.encode(&[25, 3, 7]), enc.encode(&[25, 3, 7]));
    }
}