knixl 1.3.0

Compile opinionated KDL into maintainable, committed NixOS module source.
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
//! knixl CLI entry point: argument parsing and the per-command policy functions.

mod tui;

use std::collections::{BTreeMap, BTreeSet};
use std::io::IsTerminal;
use std::sync::Arc;

use clap::{Parser, Subcommand};
use knixl_lock::model::{HostBaseline, ModuleSourcePin, OracleModulePin};
use knixl_lock::{FileState, Plan};

#[derive(Parser)]
#[command(name = "knixl", version)]
struct Cli {
    #[command(subcommand)]
    cmd: Cmd,
    /// Machine-readable output.
    #[arg(long, global = true)]
    json: bool,
}

#[derive(Subcommand)]
enum Cmd {
    /// Recompute and report; write nothing.
    Plan {
        #[arg(long)]
        detailed_exitcode: bool,
    },
    /// CI gate: succeed only if every file is Clean. Never writes, never prompts.
    Check,
    /// Apply. Silent for Stale/Missing; refuses Drifted/skew without the matching flag.
    Generate {
        #[arg(long)]
        accept_drift: bool,
        #[arg(long)]
        prune: bool,
    },
    /// Version bump: show migration notes + diff, apply on --yes, then bump the lock.
    Upgrade {
        #[arg(long)]
        yes: bool,
    },
    /// Print the typed reference for a module node (from schema()).
    Doc { node: String },
    /// Add a package to a host: draft the KDL, verify under nix, preview, then regenerate.
    Install {
        /// The nixpkgs attribute name (e.g. ripgrep) or versioned form (e.g. ripgrep@13.0.0).
        pkg: String,
        #[arg(long)]
        host: Option<String>,
        #[arg(long)]
        yes: bool,
        /// Treat a skipped nix check (nix absent) as an error.
        #[arg(long)]
        strict: bool,
        /// Also build the package derivation (proves it builds, not just resolves).
        #[arg(long)]
        build: bool,
        /// Skip the build-feasibility check that picks the pin strategy: always commit-mix.
        #[arg(long)]
        no_abi_check: bool,
    },
    /// Open the interactive TUI (install, browse modules, scaffold a module).
    Tui,
}

#[repr(i32)]
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
enum Code {
    Clean = 0,
    Internal = 1,
    // clap owns exit 2 on arg-parse failure; the variant reserves the documented code.
    #[allow(dead_code)]
    Usage = 2,
    Drift = 3,
    NeedsAck = 4,
    Validation = 5,
    RegenPending = 6,
}

/// Precedence, most severe first: severity does not follow the numeric `Code` order. Drift
/// beats skew because a silent overwrite would lose a human edit.
fn verdict(plan: &Plan) -> Code {
    if plan.has_validation_errors() {
        return Code::Validation;
    }
    if plan.any(FileState::is_drifted) {
        return Code::Drift;
    }
    if plan.requires_ack() {
        return Code::NeedsAck;
    }
    if plan.any(FileState::is_dirty) {
        return Code::RegenPending;
    }
    Code::Clean
}

fn run(cli: Cli, ctx: &Ctx) -> Code {
    // `install`/`upgrade` resolve a declared-but-unresolved nixpkgs baseline before the
    // validation gate, since they are its only remedy. Resolved in memory only; `pending` is
    // written by `Cmd::Upgrade` on `--yes`, or by `install`/`commit_install` in the confirmed,
    // revertable commit path.
    let pending: BTreeMap<String, HostBaseline> =
        if matches!(cli.cmd, Cmd::Upgrade { .. } | Cmd::Install { .. }) {
            match resolve_pending_baselines(ctx) {
                Ok(p) => p,
                Err(code) => return code,
            }
        } else {
            BTreeMap::new()
        };

    // #35 phase 3: the project's declared `oracle-modules` (knixl.kdl) resolve the same way,
    // alongside the baseline pre-pass above (in memory only, never written here; see
    // `resolve_pending_project_modules`).
    let pending_modules: Option<Vec<OracleModulePin>> =
        if matches!(cli.cmd, Cmd::Upgrade { .. } | Cmd::Install { .. }) {
            match resolve_pending_project_modules(ctx) {
                Ok(p) => p,
                Err(code) => return code,
            }
        } else {
            None
        };

    // ADR 0008: a host's own `oracle-modules` override resolves the same way (in memory only;
    // see `resolve_pending_host_modules`), but can refuse the whole run since a host override
    // with no declared `nixpkgs release=` has nowhere in the lock to carry its pins.
    let pending_host_modules: BTreeMap<String, Vec<OracleModulePin>> =
        if matches!(cli.cmd, Cmd::Upgrade { .. } | Cmd::Install { .. }) {
            match resolve_pending_host_modules(ctx) {
                Ok(p) => p,
                Err(code) => return code,
            }
        } else {
            BTreeMap::new()
        };

    // Task 5b: declared `modules {}` sources (knixl.kdl) resolve the same way, alongside the
    // pre-passes above (in memory only, never written here; see
    // `resolve_pending_module_sources`).
    let pending_module_sources: Option<Vec<PendingModuleSource>> =
        if matches!(cli.cmd, Cmd::Upgrade { .. } | Cmd::Install { .. }) {
            match resolve_pending_module_sources(ctx) {
                Ok(p) => p,
                Err(code) => return code,
            }
        } else {
            None
        };

    // Plan off a lock patched with `pending` merged in, so the "not resolved: run knixl
    // upgrade" validation error does not block the very commands that would resolve it, and
    // `lock_next` (built from this patched lock) carries the pending baselines for
    // `Cmd::Upgrade` to write verbatim. `check`/`generate`/`plan` never populate `pending`, so
    // they always plan off the on-disk lock and inputs unchanged.
    let patched_lock = (!pending.is_empty()
        || !pending_host_modules.is_empty()
        || pending_module_sources.is_some())
    .then(|| {
        let mut lock = ctx.lock.clone();
        lock.baselines
            .extend(pending.iter().map(|(host, b)| (host.clone(), b.clone())));
        // Overlay each host's pending module-override resolution onto its baseline (which the
        // extend above may just have created for a release resolved in this same run): the
        // baseline carries both fields together, so `plan.lock_next` (built from this patched
        // lock) writes the release and the module pins as one coherent host block.
        for (host, pins) in &pending_host_modules {
            lock.baselines
                .entry(host.clone())
                .or_insert_with(|| HostBaseline {
                    release: String::new(),
                    nixpkgs_rev: String::new(),
                    options_hash: String::new(),
                    modules: Vec::new(),
                })
                .modules = pins.clone();
        }
        // Task 5b: a freshly resolved (or GC'd) module-source set replaces `module_sources`
        // wholesale, exactly as `pending_modules` replaces `oracle.modules` wholesale, so
        // `build_lock_next` (which copies `lock.module_sources` straight from this patched
        // lock) carries the resolved pins through to `Cmd::Upgrade`'s/`commit_install`'s write.
        if let Some(pending) = &pending_module_sources {
            lock.module_sources = pending.iter().map(|p| p.pin.clone()).collect();
        }
        lock
    });
    let patched_inputs =
        (!pending.is_empty()).then(|| patch_inputs_for_pending(&ctx.inputs, &pending));
    let lock_for_plan = patched_lock.as_ref().unwrap_or(&ctx.lock);
    let inputs_for_plan = patched_inputs.as_ref().unwrap_or(&ctx.inputs);

    // `build_lock_next` (knixl-lock) sources the GLOBAL `oracle` pin from `running.oracle`,
    // not from the `lock` passed to `Plan::compute` (unlike per-host `baselines`, which it
    // takes from `lock` -- see `build_lock_next`): so `pending_modules` needs to land in a
    // patched `Versions`, not the patched lock above, for `plan.lock_next` to carry it through
    // to `Cmd::Upgrade`'s/`commit_install`'s write.
    let patched_running = pending_modules
        .as_ref()
        .map(|pins| knixl_lock::reconcile::Versions {
            tool: ctx.running.tool.clone(),
            formatter: ctx.running.formatter.clone(),
            oracle: knixl_lock::model::OraclePin {
                nixpkgs_rev: ctx.running.oracle.nixpkgs_rev.clone(),
                options_hash: ctx.running.oracle.options_hash.clone(),
                modules: pins.clone(),
            },
            modules: ctx.running.modules.clone(),
        });
    let running_for_plan = patched_running.as_ref().unwrap_or(&ctx.running);

    // Plan::compute is pure; validation errors ride on the plan (verdict maps them to
    // the Validation exit code), so there is no fallible generation step here.
    let plan = Plan::compute(inputs_for_plan, &ctx.disk, lock_for_plan, running_for_plan);
    if plan.has_validation_errors() {
        report_validation(&plan.validation_errors, cli.json);
        return Code::Validation;
    }

    // Non-fatal generation lints. Reported for every command that acts on the plan; `doc`
    // does not reconcile a project, so it stays quiet.
    if !matches!(cli.cmd, Cmd::Doc { .. }) {
        report_warnings(&ctx.warnings);
    }

    match cli.cmd {
        Cmd::Plan { detailed_exitcode } => {
            print_plan(&plan, cli.json);
            if detailed_exitcode {
                verdict(&plan)
            } else {
                Code::Clean
            }
        }

        Cmd::Check => {
            print_plan(&plan, cli.json);
            verdict(&plan)
        }

        Cmd::Generate {
            accept_drift,
            prune,
        } => {
            // A version bump must go through `upgrade`, never a side effect of generate.
            // Drift is NOT this gate: it is handled per file below as exit 3.
            if plan.skew_needs_ack() {
                report_skew(&plan, cli.json);
                eprintln!("version skew present: run `knixl upgrade` to review and apply");
                return Code::NeedsAck;
            }
            let mut worst = Code::Clean;
            for f in &plan.files {
                match &f.state {
                    FileState::Clean => {}
                    FileState::Stale { .. } | FileState::Missing { .. } => write_file(ctx, f),
                    FileState::Drifted { .. } if accept_drift => write_file(ctx, f),
                    FileState::Drifted { .. } => {
                        report_taint(f, cli.json);
                        worst = worst.max(Code::Drift);
                    }
                    FileState::Orphaned if prune => delete_file(ctx, f),
                    FileState::Orphaned => {
                        note_orphan(f, cli.json);
                        worst = worst.max(Code::RegenPending);
                    }
                }
            }
            // Commit the lock ONLY on a clean apply, so it never lies about disk.
            if worst == Code::Clean {
                write_lock(ctx, &plan.lock_next);
            }
            worst
        }

        Cmd::Upgrade { yes } => {
            // A pending baseline/module resolution is work to do even when every file is
            // Clean and nothing needs ack: skip the "up to date" short-circuit so it gets
            // previewed (and, on --yes, written) below instead of silently vanishing.
            if !plan.requires_ack()
                && !plan.any(FileState::is_dirty)
                && pending.is_empty()
                && pending_modules.is_none()
                && pending_host_modules.is_empty()
                && pending_module_sources.is_none()
            {
                println!("already up to date");
                return Code::Clean;
            }
            print_migration_notes(&plan, &ctx.registry);
            print_plan(&plan, cli.json);
            if !yes {
                for (host, b) in &pending {
                    println!(
                        "would resolve nixpkgs release \"{}\" for host \"{host}\" -> {}",
                        b.release, b.nixpkgs_rev,
                    );
                }
                for m in pending_modules.iter().flatten() {
                    println!(
                        "would resolve oracle module \"{}\" ({}) -> {}",
                        m.name, m.url, m.rev,
                    );
                }
                for (host, pins) in &pending_host_modules {
                    for m in pins {
                        println!(
                            "would resolve oracle module \"{}\" ({}) for host \"{host}\" -> {}",
                            m.name, m.url, m.rev,
                        );
                    }
                }
                for p in pending_module_sources.iter().flatten() {
                    if p.fetched_text.is_some() {
                        println!(
                            "would resolve module source \"{}\" ({}) -> {}",
                            p.pin.name, p.pin.url, p.pin.rev,
                        );
                    }
                }
                eprintln!("re-run with --yes to apply");
                return Code::NeedsAck;
            }
            for f in &plan.files {
                if !matches!(f.state, FileState::Clean) {
                    write_file(ctx, f);
                }
            }
            for (host, b) in &pending {
                println!(
                    "resolved nixpkgs release \"{}\" for host \"{host}\" -> {}",
                    b.release, b.nixpkgs_rev,
                );
            }
            for m in pending_modules.iter().flatten() {
                println!(
                    "resolved oracle module \"{}\" ({}) -> {}",
                    m.name, m.url, m.rev,
                );
            }
            for (host, pins) in &pending_host_modules {
                for m in pins {
                    println!(
                        "resolved oracle module \"{}\" ({}) for host \"{host}\" -> {}",
                        m.name, m.url, m.rev,
                    );
                }
            }
            for p in pending_module_sources.iter().flatten() {
                if p.fetched_text.is_some() {
                    println!(
                        "resolved module source \"{}\" ({}) -> {}",
                        p.pin.name, p.pin.url, p.pin.rev,
                    );
                }
            }
            // `plan.lock_next` was built from the lock already patched with `pending`/
            // `pending_modules`/`pending_host_modules`/`pending_module_sources` (see `run`'s
            // planning step above), so this carries the baselines and module pins through; the
            // augmented-set build below (ADR 0008) may additionally set an `options-hash` on
            // top of that.
            let mut lock_next = plan.lock_next.clone();
            let mut changed_hosts: BTreeSet<String> = pending.keys().cloned().collect();
            changed_hosts.extend(pending_host_modules.keys().cloned());
            // `upgrade` has no `--strict` flag: a missing nix is always best-effort here.
            if let Err(code) = build_pending_oracle_sets(
                &ctx.root,
                &mut lock_next,
                pending_modules.is_some(),
                &changed_hosts,
                false,
            ) {
                return code;
            }
            // Cache file writes (a disk side effect) happen only here, strictly after the
            // `--yes` gate above, and before the lock write, so a lock pin is never recorded
            // for a manifest that failed to land in the cache (task 5b).
            if let Some(pending) = &pending_module_sources {
                if let Err(code) = write_module_source_caches(pending) {
                    return code;
                }
            }
            write_lock(ctx, &lock_next); // bump tool/module/formatter/oracle/baselines
            Code::Clean
        }

        Cmd::Doc { node } => {
            print_doc(ctx, &node, cli.json);
            Code::Clean
        }

        Cmd::Install {
            pkg,
            host,
            yes,
            strict,
            build,
            no_abi_check,
        } => install(
            ctx,
            &pkg,
            host.as_deref(),
            yes,
            strict,
            build,
            no_abi_check,
            &pending,
            pending_modules.as_deref(),
            &pending_host_modules,
            pending_module_sources.as_deref(),
        ),

        Cmd::Tui => unreachable!("tui is dispatched before Ctx::load"),
    }
}

// One argument per `Cmd::Install` field, plus `ctx` and the pending-baseline map/pending-module
// list threaded from `run`'s pre-pass: splitting these into a struct would obscure more than
// it saves here.
#[allow(clippy::too_many_arguments)]
fn install(
    ctx: &Ctx,
    pkg: &str,
    host: Option<&str>,
    yes: bool,
    strict: bool,
    build: bool,
    no_abi_check: bool,
    pending: &BTreeMap<String, HostBaseline>,
    pending_modules: Option<&[OracleModulePin]>,
    pending_host_modules: &BTreeMap<String, Vec<OracleModulePin>>,
    pending_module_sources: Option<&[PendingModuleSource]>,
) -> Code {
    use knixl_nix::pin::{PinError, PinResolver};
    use knixl_pipeline::install::{list_hosts, select_host};

    let (name, version) = match pkg.split_once('@') {
        Some((n, v)) => (n, Some(v)),
        None => (pkg, None),
    };

    let hosts = match list_hosts(&ctx.root) {
        Ok(h) => h,
        Err(e) => {
            eprintln!("knixl: {e}");
            return Code::Internal;
        }
    };
    let initial = match select_host(&hosts, host) {
        Ok(t) => t.clone(),
        Err(e) => {
            eprintln!("knixl: {e}");
            return Code::Usage;
        }
    };

    let interactive = std::io::stdin().is_terminal() && std::io::stdout().is_terminal();
    if interactive && !yes {
        let entry = tui::Entry::Install {
            pkg: name.to_string(),
            strict,
            host: Some(initial.name.clone()),
            version: version.map(str::to_string),
            no_abi_check,
        };
        let build_fn = build.then(|| make_build(ctx.root.clone()));
        let pin_fn = version.is_some().then(make_pin);
        // #28: decide the pin strategy inside the Install screen's Apply-gated verify
        // sequence, rather than build-testing a second time at commit (`commit_tui_install`'s
        // old `choose_strategy` call). Closes over the data needed to compute a baseline for
        // WHICHEVER host is selected at call time (the host list, the lock's baselines, the
        // global oracle rev, and any pending per-host resolutions), rather than a single
        // baseline fixed to `initial` (#28 review fix: a host switch inside the TUI used to
        // leave the strategy decided against the host that was selected when the TUI opened).
        let strategy_fn = version.is_some().then(|| {
            make_strategy(
                hosts.clone(),
                ctx.lock.baselines.clone(),
                ctx.lock.oracle.nixpkgs_rev.clone(),
                pending.clone(),
                no_abi_check,
            )
        });
        return match open_tui(entry, build_fn, pin_fn, strategy_fn) {
            Ok(tui::Outcome::Install {
                host,
                pkg,
                strict,
                version,
                pin,
                no_abi_check,
                strategy,
                strategy_reason,
            }) => {
                // The TUI may switch the target host from `initial`: look the pending
                // resolution up for whichever host was actually chosen.
                let baseline_pending = pending.get(&host.name).cloned();
                commit_tui_install(
                    host,
                    pkg,
                    strict,
                    version,
                    pin,
                    no_abi_check,
                    strategy,
                    strategy_reason,
                    baseline_pending,
                )
            }
            Ok(_) => {
                println!("cancelled");
                Code::Clean
            }
            Err(e) => {
                eprintln!("knixl: tui: {e}");
                Code::Internal
            }
        };
    }

    // The pending baseline resolution for the target host, if `run`'s pre-pass resolved one
    // (issue #22 review fix): written alongside the pin by `commit_install`, in the same
    // committed/revertable step, rather than up front by a pre-pass.
    let baseline_pending = pending.get(&initial.name).cloned();
    // As above, for the target host's own `oracle-modules` override (ADR 0008), if it has one.
    let host_modules_pending = pending_host_modules.get(&initial.name).cloned();

    // A version cannot be pinned without a resolved commit and a decided strategy: refuse
    // before anything else. A pin already on record for this exact (host, package, version)
    // is reused verbatim (its rev and strategy), skipping both resolution and selection: an
    // idempotent repeat install touches neither the network nor nix.
    let mut build_tested = false;
    let version_pin = match version {
        Some(v) => {
            let cached = ctx
                .lock
                .pins
                .get(&initial.name)
                .and_then(|pins| pins.iter().find(|p| p.package == name && p.version == v));
            let (resolved, strategy, reason) = if let Some(p) = cached {
                (
                    knixl_nix::pin::Resolved {
                        nixpkgs_rev: p.nixpkgs_rev.clone(),
                    },
                    p.strategy,
                    "cached".to_string(),
                )
            } else {
                let resolved = match PinResolver::resolve().lookup(name, v) {
                    Ok(r) => r,
                    Err(e @ (PinError::NotFound(_) | PinError::Failed(_))) => {
                        eprintln!("knixl: {e}");
                        return Code::Validation;
                    }
                    Err(e @ PinError::Unavailable(_)) => {
                        eprintln!("knixl: cannot resolve {name}@{v}: {e}");
                        return Code::Validation;
                    }
                };
                let baseline_rev = effective_baseline_rev(
                    &ctx.lock.oracle.nixpkgs_rev,
                    &ctx.lock.baselines,
                    &initial.path,
                    &initial.name,
                    baseline_pending.as_ref(),
                );
                match choose_strategy(name, &resolved.nixpkgs_rev, &baseline_rev, no_abi_check) {
                    Ok((strategy, reason, tested)) => {
                        build_tested = tested;
                        (resolved, strategy, reason)
                    }
                    Err((commit_mix, over)) => {
                        eprintln!(
                            "knixl: cannot pin {name}@{v}: commit-mix build failed: {commit_mix}"
                        );
                        eprintln!("knixl: cannot pin {name}@{v}: override build failed: {over}");
                        return Code::Validation;
                    }
                }
            };
            Some((v, resolved, strategy, reason))
        }
        None => None,
    };

    // Non-interactive / --yes: the plain path. Hard-check the package, then build it if
    // requested, then confirm unless --yes.
    match resolve_package(ctx, name) {
        tui::Resolve::No => {
            eprintln!("knixl: no nixpkgs package named `{name}`");
            return Code::Validation;
        }
        tui::Resolve::Skipped if strict => {
            eprintln!("knixl: --strict: nix unavailable, cannot verify `{name}`");
            return Code::Validation;
        }
        tui::Resolve::Skipped => {
            eprintln!("warning: nix unavailable, skipping package check for `{name}`");
        }
        tui::Resolve::Yes => {}
    }
    // A freshly build-tested version pin already proves `name` builds at the pinned rev:
    // reuse that instead of a second, separate build of the ambient package.
    if build && !build_tested {
        use knixl_nix::nixeval::{NixError, NixEval, Nixpkgs};
        let rev = &ctx.lock.oracle.nixpkgs_rev;
        let src = if rev.is_empty() {
            Nixpkgs::Ambient
        } else {
            Nixpkgs::PinnedRev(rev.clone())
        };
        match NixEval::resolve().builds(&src, name) {
            Ok(()) => {}
            Err(NixError::Unavailable(_)) if strict => {
                eprintln!("knixl: --strict: nix unavailable, cannot build `{name}`");
                return Code::Validation;
            }
            Err(NixError::Unavailable(_)) => {
                eprintln!("warning: nix unavailable, skipping build of `{name}`");
            }
            Err(NixError::Failed(m)) => {
                eprintln!("knixl: `{name}` failed to build: {m}");
                return Code::Validation;
            }
        }
    }
    if !yes && !confirm(&format!("install {pkg} on {}?", initial.name)) {
        println!("cancelled");
        return Code::Clean;
    }
    let pin_args = version_pin
        .as_ref()
        .map(|(v, resolved, strategy, _)| (*v, resolved, *strategy));
    let code = commit_install(
        &initial,
        name,
        pin_args,
        baseline_pending.as_ref(),
        pending_modules,
        host_modules_pending.as_deref(),
        pending_module_sources,
        strict,
    );
    if code == Code::Clean {
        if let Some(b) = &baseline_pending {
            println!(
                "resolved nixpkgs release \"{}\" for host \"{}\" -> {}",
                b.release, initial.name, b.nixpkgs_rev,
            );
        }
        for m in pending_modules.into_iter().flatten() {
            println!(
                "resolved oracle module \"{}\" ({}) -> {}",
                m.name, m.url, m.rev
            );
        }
        for m in host_modules_pending.iter().flatten() {
            println!(
                "resolved oracle module \"{}\" ({}) for host \"{}\" -> {}",
                m.name, m.url, initial.name, m.rev
            );
        }
        for p in pending_module_sources.into_iter().flatten() {
            if p.fetched_text.is_some() {
                println!(
                    "resolved module source \"{}\" ({}) -> {}",
                    p.pin.name, p.pin.url, p.pin.rev
                );
            }
        }
        if let Some((v, _, strategy, reason)) = &version_pin {
            println!(
                "pinned {name} {v} via {} ({reason})",
                strategy_label(*strategy)
            );
        }
    }
    code
}

#[allow(clippy::too_many_arguments)]
fn commit_install(
    chosen: &knixl_pipeline::install::HostInfo,
    pkg: &str,
    version_pin: Option<(
        &str,
        &knixl_nix::pin::Resolved,
        knixl_lock::model::PinStrategy,
    )>,
    baseline_pending: Option<&HostBaseline>,
    pending_modules: Option<&[OracleModulePin]>,
    host_modules_pending: Option<&[OracleModulePin]>,
    pending_module_sources: Option<&[PendingModuleSource]>,
    strict: bool,
) -> Code {
    use knixl_pipeline::install::add_package;

    let original = match std::fs::read_to_string(&chosen.path) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("knixl: {}: {e}", chosen.path.display());
            return Code::Internal;
        }
    };
    let draft = match add_package(&original, pkg, version_pin.map(|(v, _, _)| v)) {
        Ok(Some(d)) => d,
        Ok(None) => {
            println!("{pkg} is already installed on {}", chosen.name);
            return Code::Clean;
        }
        Err(e) => {
            eprintln!("knixl: cannot edit {}: {e}", chosen.path.display());
            return Code::Internal;
        }
    };

    // `oracle.modules` is a single project-wide list (unlike the pin/baseline, which are
    // fresh per-host inserts): capture what it held before this pending resolution, so a
    // revert below can restore it exactly rather than blank it out.
    let prior_modules = pending_modules.map(|_| Ctx::load().lock.oracle.modules.clone());
    // As above, for the target host's own baseline `.modules` (ADR 0008 host override): a
    // per-host list, but still something to restore rather than blank on revert, since a host
    // may already have had a different override in place before this pending resolution.
    let prior_host_modules = host_modules_pending.map(|_| {
        Ctx::load()
            .lock
            .baselines
            .get(&chosen.name)
            .map(|b| b.modules.clone())
            .unwrap_or_default()
    });
    // As above, for the project-wide `module_sources` list (task 5b): captured before the
    // write below, so a revert restores exactly what was pinned before, rather than blanking
    // sources this install did not touch.
    let prior_module_sources =
        pending_module_sources.map(|_| Ctx::load().lock.module_sources.clone());

    // The module-source cache file write (task 5b) is a disk side effect with nothing yet on
    // record to undo, so it runs first and, on failure, simply refuses before writing anything
    // else: a lock pin must never be recorded for a manifest that failed to land in the cache.
    if let Some(pending) = pending_module_sources {
        if let Err(code) = write_module_source_caches(pending) {
            return code;
        }
    }

    // Record the pin, any pending baseline resolution, and any pending oracle-module
    // resolution before the versioned KDL hits disk (issue #22 review fix: a baseline
    // resolved for this install used to be written by a pre-pass before confirmation; it is
    // now written here, in the same committed/revertable step as the pin): `generate` treats
    // a `package` node with a `version` prop and no matching lock pin as an error, so writing
    // the KDL first would (briefly, but for real, since the next line's gather sees it) make
    // the project fail to generate even on the success path.
    if let Some((v, resolved, strategy)) = version_pin {
        write_pin(&chosen.name, pkg, v, resolved, strategy);
    }
    if let Some(b) = baseline_pending {
        write_baseline(&chosen.name, &b.release, &b.nixpkgs_rev, &b.options_hash);
    }
    if let Some(pins) = pending_modules {
        write_oracle_modules(pins);
    }
    // After `write_baseline` above, so a host whose release resolved in this same run already
    // has a baseline entry for this to set `.modules` on.
    if let Some(pins) = host_modules_pending {
        write_host_oracle_modules(&chosen.name, pins);
    }
    if let Some(pending) = pending_module_sources {
        let pins: Vec<ModuleSourcePin> = pending.iter().map(|p| p.pin.clone()).collect();
        write_module_sources(&pins);
    }

    if let Err(e) = std::fs::write(&chosen.path, &draft) {
        eprintln!("knixl: {}: {e}", chosen.path.display());
        // The pin/baseline/modules above may already be on disk even though the KDL write
        // failed: undo them so a failed install never leaves any of them dangling.
        if version_pin.is_some() {
            remove_pin(&chosen.name, pkg);
        }
        if baseline_pending.is_some() {
            remove_baseline(&chosen.name);
        }
        if let Some(prior) = &prior_modules {
            restore_oracle_modules(prior);
        }
        if let Some(prior) = &prior_host_modules {
            restore_host_oracle_modules(&chosen.name, prior);
        }
        if let Some(prior) = &prior_module_sources {
            restore_module_sources(prior);
        }
        return Code::Internal;
    }
    // Undo the pin, baseline, and modules along with the KDL: all three may have been
    // written above, before the KDL hit disk, so an abort past this point must not leave any
    // of them dangling in the lock.
    let revert = || {
        let _ = std::fs::write(&chosen.path, &original);
        if version_pin.is_some() {
            remove_pin(&chosen.name, pkg);
        }
        if baseline_pending.is_some() {
            remove_baseline(&chosen.name);
        }
        if let Some(prior) = &prior_modules {
            restore_oracle_modules(prior);
        }
        if let Some(prior) = &prior_host_modules {
            restore_host_oracle_modules(&chosen.name, prior);
        }
        if let Some(prior) = &prior_module_sources {
            restore_module_sources(prior);
        }
    };

    let drafted = Ctx::load();
    let plan = Plan::compute(
        &drafted.inputs,
        &drafted.disk,
        &drafted.lock,
        &drafted.running,
    );
    if plan.has_validation_errors() {
        report_validation(&plan.validation_errors, false);
        revert();
        return Code::Validation;
    }
    if let Some(code) = verify_parse(&drafted, strict) {
        revert();
        return code;
    }

    let mut worst = Code::Clean;
    for f in &plan.files {
        match &f.state {
            FileState::Stale { .. } | FileState::Missing { .. } => write_file(&drafted, f),
            FileState::Drifted { .. } => {
                report_taint(f, false);
                worst = worst.max(Code::Drift);
            }
            FileState::Clean | FileState::Orphaned => {}
        }
    }
    if worst == Code::Clean {
        // ADR 0008: build + cache the augmented option set for whatever effective sets this
        // install just changed, folding each built content's hash into a local copy of
        // `plan.lock_next` before it is written; a missing nix is best-effort unless `strict`.
        let mut lock_next = plan.lock_next.clone();
        let mut changed_hosts = BTreeSet::new();
        if baseline_pending.is_some() || host_modules_pending.is_some() {
            changed_hosts.insert(chosen.name.clone());
        }
        if let Err(code) = build_pending_oracle_sets(
            &drafted.root,
            &mut lock_next,
            pending_modules.is_some(),
            &changed_hosts,
            strict,
        ) {
            revert();
            return code;
        }
        write_lock(&drafted, &lock_next);
        println!("installed {pkg} on {}", chosen.name);
    } else {
        revert();
    }
    worst
}

/// Insert or replace the resolved pin for `package` under `host` in the lock, then write it
/// back to disk. Dedups by package name, so re-pinning an already-pinned package replaces the
/// old entry rather than accumulating one per version. Loads the lock through `Ctx::load` so a
/// fresh project with no lock yet gets the same seeded default `gather` uses elsewhere.
fn write_pin(
    host: &str,
    package: &str,
    version: &str,
    resolved: &knixl_nix::pin::Resolved,
    strategy: knixl_lock::model::PinStrategy,
) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    let pins = lock.pins.entry(host.to_string()).or_default();
    pins.retain(|p| p.package != package);
    pins.push(knixl_lock::model::Pin {
        package: package.to_string(),
        version: version.to_string(),
        nixpkgs_rev: resolved.nixpkgs_rev.clone(),
        strategy,
    });
    pins.sort_by(|a, b| a.package.cmp(&b.package));
    write_lock(&ctx, &lock);
}

/// Undo `write_pin`: drop the pin for `package` under `host`, then write the lock back.
/// Mirrors `write_pin`. Called on `commit_install`'s revert path (a validation, parse, or
/// drift failure after the pin was already written), so an aborted install never leaves a
/// dangling pin behind. A no-op when no pin exists for that package on that host.
fn remove_pin(host: &str, package: &str) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    let pins = lock.pins.entry(host.to_string()).or_default();
    pins.retain(|p| p.package != package);
    write_lock(&ctx, &lock);
}

/// Mirrors `write_pin`, but for the host baseline.
fn write_baseline(host: &str, release: &str, rev: &str, options_hash: &str) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    lock.baselines.insert(
        host.to_string(),
        HostBaseline {
            release: release.to_string(),
            nixpkgs_rev: rev.to_string(),
            options_hash: options_hash.to_string(),
            modules: Vec::new(),
        },
    );
    write_lock(&ctx, &lock);
}

/// Undo `write_baseline` on `commit_install`'s revert path, so a cancelled/failed install
/// never leaves a freshly resolved baseline dangling. Mirrors `remove_pin`.
fn remove_baseline(host: &str) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    lock.baselines.remove(host);
    write_lock(&ctx, &lock);
}

/// Unlike `write_pin`/`write_baseline` (keyed per host), this is a single, project-wide list
/// resolved from `knixl.kdl`'s `oracle-modules` block.
fn write_oracle_modules(pins: &[OracleModulePin]) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    lock.oracle.modules = pins.to_vec();
    write_lock(&ctx, &lock);
}

/// Undo `write_oracle_modules`: restore the lock's `oracle.modules` to `prior` (captured by
/// `commit_install` before it wrote the pending resolution), then write the lock back. Called
/// on `commit_install`'s revert path, so a cancelled/failed install never leaves a module-pin
/// change dangling. Unlike `remove_baseline` (a plain removal: a resolved baseline for a host
/// is always a fresh insert), this restores the PRIOR value rather than clearing it, since
/// `oracle.modules` may already have held a different (still-valid) set before this pending
/// resolution replaced it.
fn restore_oracle_modules(prior: &[OracleModulePin]) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    lock.oracle.modules = prior.to_vec();
    write_lock(&ctx, &lock);
}

/// Set `host`'s baseline `.modules` to `pins` (ADR 0008 host override), leaving the rest of
/// the baseline unchanged. Mirrors `write_oracle_modules`, but per host. A no-op if `host`
/// has no baseline yet.
fn write_host_oracle_modules(host: &str, pins: &[OracleModulePin]) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    if let Some(b) = lock.baselines.get_mut(host) {
        b.modules = pins.to_vec();
    }
    write_lock(&ctx, &lock);
}

/// Undo `write_host_oracle_modules`: restore `host`'s baseline `.modules` to `prior` (captured
/// by `commit_install` before it wrote the pending resolution), then write the lock back.
/// Mirrors `restore_oracle_modules`, but per host.
fn restore_host_oracle_modules(host: &str, prior: &[OracleModulePin]) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    if let Some(b) = lock.baselines.get_mut(host) {
        b.modules = prior.to_vec();
    }
    write_lock(&ctx, &lock);
}

/// Replace the lock's `module_sources` pins with `pins`, then write it back to disk (task 5b).
/// Project-wide, like `write_oracle_modules`, not per host: a declared `modules {}` source is
/// not tied to any one host.
fn write_module_sources(pins: &[ModuleSourcePin]) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    lock.module_sources = pins.to_vec();
    write_lock(&ctx, &lock);
}

/// Undo `write_module_sources`: restore `module_sources` to `prior`, then write the lock
/// back. Mirrors `restore_oracle_modules`. Leaves any cache file a resolve may have written
/// untouched: the cache is content-addressed, so a stray entry is harmless.
fn restore_module_sources(prior: &[ModuleSourcePin]) {
    let ctx = Ctx::load();
    let mut lock = ctx.lock.clone();
    lock.module_sources = prior.to_vec();
    write_lock(&ctx, &lock);
}

/// The `nixpkgs release=".."` a host's own KDL declares, if any (issue #22). Scanned
/// straight from the file rather than the gathered project, since a target host is often
/// resolved before (or independently of) a full `gather`.
fn declared_release(host_path: &std::path::Path) -> Option<String> {
    let src = std::fs::read_to_string(host_path).ok()?;
    let doc = knixl_kdl::parse(&src).ok()?;
    let node = doc.nodes().iter().find(|n| n.name().value() == "host")?;
    knixl_kdl::child_prop_str(node, "nixpkgs", "release")
}

/// The blake3 hash of the options.json cached for `rev`, or an empty string when nothing is
/// cached (best-effort, same convention as an unresolved oracle rev).
fn options_hash_for_rev(rev: &str) -> String {
    knixl_oracle::cache_path(rev)
        .filter(|p| p.is_file())
        .and_then(|p| std::fs::read(&p).ok())
        .map(|bytes| knixl_nix::hash(&bytes))
        .unwrap_or_default()
}

/// Resolve `host`'s declared `nixpkgs release` baseline IN MEMORY ONLY: no write (issue #22
/// review fix; the pre-pass used to write straight to the lock here, before the validation
/// gate and before `--yes`/confirmation). `Ok(None)` when the host declares no release, or
/// its release already matches the lock's recorded baseline (the idempotent skip); otherwise
/// `Ok(Some(baseline))` with the freshly resolved value, for the caller to plan around and
/// decide whether to keep. `Err(Code::Validation)` on a resolver failure; the message is
/// already printed, so the caller only needs to return the code.
fn resolve_pending_baseline(
    ctx: &Ctx,
    host: &str,
    host_path: &std::path::Path,
) -> Result<Option<HostBaseline>, Code> {
    let Some(release) = declared_release(host_path) else {
        return Ok(None);
    };
    if let Some(b) = ctx.lock.baselines.get(host) {
        if b.release == release {
            return Ok(None);
        }
    }
    let rev = knixl_nix::baseline::BaselineResolver::resolve()
        .lookup(&release)
        .map_err(|e| {
            eprintln!(
                "knixl: cannot resolve nixpkgs release \"{release}\" for host \"{host}\": {e}"
            );
            Code::Validation
        })?;
    let options_hash = options_hash_for_rev(&rev);
    Ok(Some(HostBaseline {
        release,
        nixpkgs_rev: rev,
        options_hash,
        modules: Vec::new(),
    }))
}

/// For every host under `ctx.root` whose KDL declares a `nixpkgs release=".."` not already
/// resolved to that exact release in the lock, resolve it via `resolve_pending_baseline`
/// (in memory only). Called by `run`'s `Cmd::Upgrade`/`Cmd::Install` pre-pass, before the
/// shared validation gate: the "not resolved" validation error names `upgrade` as the fix,
/// and `install` needs a resolved baseline for `choose_strategy`, so neither could reach its
/// own remedy if the gate refused first. Returns the pending resolutions keyed by host name
/// (empty if every declared release already matched), or `Err(Code::Validation)` on the first
/// resolver failure (message already printed). Nothing here is written; see `Cmd::Upgrade`
/// and `commit_install` for where a caller commits (or discards) what this returns.
fn resolve_pending_baselines(ctx: &Ctx) -> Result<BTreeMap<String, HostBaseline>, Code> {
    use knixl_pipeline::install::list_hosts;
    let hosts = list_hosts(&ctx.root).unwrap_or_default();
    let mut pending = BTreeMap::new();
    for h in &hosts {
        if let Some(b) = resolve_pending_baseline(ctx, &h.name, &h.path)? {
            pending.insert(h.name.clone(), b);
        }
    }
    Ok(pending)
}

/// Resolve one declared `module "<name>" flake="<ref>" [attr="<attr>"]` (project `knixl.kdl`
/// or a host's own `oracle-modules` override) to a lock pin (#35 phase 3). `Err(Code::Validation)`
/// on a resolver failure (including an unsupported/empty flake ref -- `ModuleResolver::lookup`
/// refuses that before any resolver runs, so this never pins an empty rev); the message is
/// already printed, so the caller only needs to return the code.
fn resolve_oracle_module(
    m: &knixl_pipeline::project::OracleModule,
) -> Result<OracleModulePin, Code> {
    knixl_nix::module::ModuleResolver::resolve()
        .lookup(&m.flake)
        .map(|r| OracleModulePin {
            name: m.name.clone(),
            url: r.url,
            rev: r.rev,
            attr: m.attr.clone(),
        })
        .map_err(|e| {
            eprintln!(
                "knixl: cannot resolve oracle module \"{}\" ({}): {e}",
                m.name, m.flake
            );
            Code::Validation
        })
}

/// Resolve a whole declared module set (project default, or a host's override), in source
/// order. `Err` short-circuits on the first failing module (message already printed by
/// `resolve_oracle_module`).
fn resolve_oracle_modules(
    modules: &[knixl_pipeline::project::OracleModule],
) -> Result<Vec<OracleModulePin>, Code> {
    modules.iter().map(resolve_oracle_module).collect()
}

/// Whether `existing` (the lock's currently recorded pins) already matches `declared` (a
/// `knixl.kdl`/host `oracle-modules` block) by identity -- name, flake-derived url, and attr,
/// in the same order -- ignoring the pinned `rev`. Mirrors `resolve_pending_baseline`'s
/// release-string idempotency check: a declared module set that has not itself changed should
/// not be re-resolved (and re-hit the network) on every `install`/`upgrade`, even though the
/// upstream commit it is pinned to may have moved on since.
fn oracle_modules_up_to_date(
    existing: &[OracleModulePin],
    declared: &[knixl_pipeline::project::OracleModule],
) -> bool {
    existing.len() == declared.len()
        && existing.iter().zip(declared.iter()).all(|(pin, m)| {
            pin.name == m.name
                && pin.attr == m.attr
                && knixl_nix::module::url_from_flake_ref(&m.flake).as_deref()
                    == Some(pin.url.as_str())
        })
}

/// Resolve the project's default `oracle-modules` set (`knixl.kdl`) IN MEMORY ONLY: no write
/// (mirrors `resolve_pending_baseline`; #35 phase 3). `Ok(None)` when the project declares no
/// `oracle-modules` block AND the lock already has none pinned, or its declared set already
/// matches what is pinned (the idempotent skip, see `oracle_modules_up_to_date`); otherwise
/// `Ok(Some(pins))` for the caller to plan around and decide whether to keep. A project that
/// removes its `oracle-modules` block entirely resolves to `Ok(Some(Vec::new()))` rather than
/// `Ok(None)` when the lock still holds pins from a prior declaration: an empty declared set is
/// a real change (removal), and short-circuiting it to "nothing pending" would leave the stale
/// pins in the lock forever, un-GC'd (review finding on #35 phase 3). A malformed/unreadable
/// `knixl.kdl` is treated the same as an absent one (best-effort, matching `declared_release`'s
/// existing tolerance for a host's own KDL) rather than surfaced as an error here: the
/// project-config parser itself is not yet wired into `gather`'s validation (that is phase 5 of
/// #35), so there is no other path today that would report a genuinely malformed `knixl.kdl` to
/// the user.
fn resolve_pending_project_modules(ctx: &Ctx) -> Result<Option<Vec<OracleModulePin>>, Code> {
    let project = knixl_pipeline::project::parse_project(&ctx.root).unwrap_or_default();
    if project.oracle_modules.is_empty() {
        return Ok((!ctx.lock.oracle.modules.is_empty()).then(Vec::new));
    }
    if oracle_modules_up_to_date(&ctx.lock.oracle.modules, &project.oracle_modules) {
        return Ok(None);
    }
    Ok(Some(resolve_oracle_modules(&project.oracle_modules)?))
}

/// Resolve every host's own declared `oracle-modules` override IN MEMORY ONLY (mirrors
/// `resolve_pending_project_modules`, but per host and keyed on the host's own `nixpkgs
/// release=` declaration rather than the project file). ADR 0008's fixed decision: a host may
/// declare its own `oracle-modules` block only alongside a declared `nixpkgs release=` (that
/// baseline is the only place in the lock able to carry its pins). `Err(Code::Validation)`
/// (message already printed) the moment a host declares one with no declared release: this
/// refuses the whole run rather than silently ignoring the host's override, so `install`/
/// `upgrade` never resolve or write a pin they have nowhere to record. A host with no
/// `oracle-modules` block at all resolves to a present, empty entry when the lock still holds
/// a stale override for it (GC on removal, mirroring `resolve_pending_project_modules`'s
/// finding-1 fix), else is simply absent from the returned map.
fn resolve_pending_host_modules(ctx: &Ctx) -> Result<BTreeMap<String, Vec<OracleModulePin>>, Code> {
    use knixl_pipeline::install::list_hosts;
    let hosts = list_hosts(&ctx.root).unwrap_or_default();
    let mut pending = BTreeMap::new();
    for h in &hosts {
        let Ok(src) = std::fs::read_to_string(&h.path) else {
            continue;
        };
        let existing = ctx
            .lock
            .baselines
            .get(&h.name)
            .map(|b| b.modules.clone())
            .unwrap_or_default();
        match knixl_pipeline::project::parse_host_oracle_modules(&src) {
            None => {
                if !existing.is_empty() {
                    pending.insert(h.name.clone(), Vec::new());
                }
            }
            Some(declared) => {
                if declared_release(&h.path).is_none() {
                    eprintln!(
                        "knixl: host \"{}\": oracle-modules requires a declared nixpkgs release",
                        h.name
                    );
                    return Err(Code::Validation);
                }
                if oracle_modules_up_to_date(&existing, &declared) {
                    continue;
                }
                pending.insert(h.name.clone(), resolve_oracle_modules(&declared)?);
            }
        }
    }
    Ok(pending)
}

/// One resolved fetched-module-source pin from `run`'s pre-pass (task 5b), plus (only when
/// this run actually resolved and fetched it) the manifest text still waiting to be cached.
/// `fetched_text: None` means the existing lock pin was verified up to date and is simply
/// carried forward unchanged, needing no cache write; `Some(text)` means the pin is new or
/// changed this run and `text` must still be written to `module_cache_path(pin.url, pin.rev,
/// pin.path)` after the `--yes`/confirm gate (see `write_module_source_caches`).
#[derive(Clone)]
struct PendingModuleSource {
    pin: ModuleSourcePin,
    fetched_text: Option<String>,
}

/// Whether `pin` (the lock's currently recorded pin for a declared module source) is still
/// valid against `source` (that source's current `knixl.kdl` declaration): its `path` has not
/// moved AND its cache entry still hashes to `pin.hash`. Deliberately NOT a copy of
/// `oracle_modules_up_to_date`'s name+url-only identity check: `module_cache_path` keys the
/// cache location on `path` too, so a path edit that this check ignored would leave `gather`
/// looking in the wrong place forever, and `install`/`upgrade` re-running the naive check would
/// never notice or fix it (see task 5b brief and the task 5 report's "Half 2" analysis).
///
/// Limit: this checks `path` and the cached content hash only, never the declared `flake=`
/// ref itself. Changing `flake=` while `path=` and the cache entry it resolves to stay the
/// same is not detected here, since re-checking a flake ref means a network lookup, and this
/// check exists precisely so `install`/`upgrade` can skip that lookup when nothing changed.
/// Bump the source's rev, or drop its lock pin, to force re-resolution.
fn module_source_up_to_date(
    pin: &ModuleSourcePin,
    source: &knixl_pipeline::project::ModuleSource,
) -> bool {
    if pin.path != source.path {
        return false;
    }
    let Some(cache_path) =
        knixl_nix::module_fetch::module_cache_path(&pin.url, &pin.rev, &pin.path)
    else {
        return false;
    };
    let Ok(text) = std::fs::read_to_string(&cache_path) else {
        return false;
    };
    knixl_nix::module_fetch::hash_module(&text) == pin.hash
}

/// Resolve one declared module source that is not already up to date: look up its flake ref,
/// then fetch its manifest at the resolved rev. `Err(Code::Validation)` on a resolver or fetch
/// failure (message already printed, naming the source), never a silent skip.
fn resolve_module_source(
    source: &knixl_pipeline::project::ModuleSource,
) -> Result<PendingModuleSource, Code> {
    let resolved = knixl_nix::module::ModuleResolver::resolve()
        .lookup(&source.flake)
        .map_err(|e| {
            eprintln!(
                "knixl: cannot resolve module source \"{}\" ({}): {e}",
                source.name, source.flake
            );
            Code::Validation
        })?;
    let text = knixl_nix::module_fetch::fetch_module(&resolved.url, &resolved.rev, &source.path)
        .map_err(|e| {
            eprintln!(
                "knixl: cannot fetch module source \"{}\" ({}@{}): {e}",
                source.name, resolved.url, resolved.rev
            );
            Code::Validation
        })?;
    let pin = ModuleSourcePin {
        name: source.name.clone(),
        url: resolved.url,
        rev: resolved.rev,
        path: source.path.clone(),
        hash: knixl_nix::module_fetch::hash_module(&text),
    };
    Ok(PendingModuleSource {
        pin,
        fetched_text: Some(text),
    })
}

/// Resolve every declared `modules {}` source (`knixl.kdl`) IN MEMORY ONLY (task 5b; mirrors
/// `resolve_pending_project_modules`/`resolve_pending_baseline`): a source whose lock pin is
/// still up to date (`module_source_up_to_date`) is carried forward unchanged with no network
/// call at all; anything else is freshly resolved and fetched via `resolve_module_source`, read
/// but not yet written. `Ok(None)` when the resulting full set is identical to what the lock
/// already holds (nothing to plan or write, so `Cmd::Upgrade`'s "already up to date" short
/// circuit still fires); `Ok(Some(pins))` otherwise, always the FULL declared set (up-to-date
/// entries included), since `module_sources` is replaced wholesale on write, mirroring
/// `resolve_pending_project_modules`'s all-or-nothing GC-on-removal note: a project that drops
/// its `modules {}` block entirely resolves to `Ok(Some(Vec::new()))` when the lock still holds
/// stale pins, not `Ok(None)`, so those pins actually get GC'd rather than lingering forever.
/// A malformed `knixl.kdl` is `Err(Code::Validation)` (message already printed), never folded
/// into an empty config: unlike `resolve_pending_project_modules`, this pre-pass drives what
/// gets fetched and written, so silently treating a parse failure as "nothing declared" would
/// resolve and pin nothing while looking successful.
fn resolve_pending_module_sources(ctx: &Ctx) -> Result<Option<Vec<PendingModuleSource>>, Code> {
    let project = knixl_pipeline::project::parse_project(&ctx.root).map_err(|e| {
        eprintln!("knixl: {e}");
        Code::Validation
    })?;
    let mut next = Vec::new();
    for source in &project.module_sources {
        let existing = ctx
            .lock
            .module_sources
            .iter()
            .find(|p| p.name == source.name);
        if let Some(pin) = existing {
            if module_source_up_to_date(pin, source) {
                next.push(PendingModuleSource {
                    pin: pin.clone(),
                    fetched_text: None,
                });
                continue;
            }
        }
        next.push(resolve_module_source(source)?);
    }

    let mut next_sorted: Vec<&ModuleSourcePin> = next.iter().map(|p| &p.pin).collect();
    next_sorted.sort_by(|a, b| a.name.cmp(&b.name));
    let mut existing_sorted: Vec<&ModuleSourcePin> = ctx.lock.module_sources.iter().collect();
    existing_sorted.sort_by(|a, b| a.name.cmp(&b.name));
    if next_sorted == existing_sorted {
        return Ok(None);
    }
    Ok(Some(next))
}

/// Write every freshly resolved module source's manifest text to its cache location (task 5b),
/// creating the cache directory if needed. Skips any entry with `fetched_text: None` (already
/// verified in place, nothing to write). A write failure is a hard `Code::Internal`: a lock pin
/// must never be recorded for a manifest that failed to land in the cache. Called only after
/// the `--yes`/confirm gate (see `Cmd::Upgrade` and `commit_install`), never from the shared
/// pending pre-pass.
fn write_module_source_caches(pending: &[PendingModuleSource]) -> Result<(), Code> {
    for p in pending {
        let Some(text) = &p.fetched_text else {
            continue;
        };
        let Some(cache_path) =
            knixl_nix::module_fetch::module_cache_path(&p.pin.url, &p.pin.rev, &p.pin.path)
        else {
            eprintln!(
                "knixl: module source \"{}\": cannot determine a cache location (no XDG_CACHE_HOME or HOME)",
                p.pin.name
            );
            return Err(Code::Internal);
        };
        if let Some(parent) = cache_path.parent() {
            if let Err(e) = std::fs::create_dir_all(parent) {
                eprintln!("knixl: {}: {e}", parent.display());
                return Err(Code::Internal);
            }
        }
        if let Err(e) = std::fs::write(&cache_path, text) {
            eprintln!("knixl: {}: {e}", cache_path.display());
            return Err(Code::Internal);
        }
    }
    Ok(())
}

/// Build the augmented `options.json` for one effective set (a nixpkgs rev plus its module
/// pins) via `nixosOptionsDoc`, cache it at `cache_path_for`, and return the built content's
/// blake3 hash to record as an `options-hash` (ADR 0008). `Ok(None)`: nothing to build (`rev`
/// not yet resolved, or no cache directory determinable), or nix is unavailable and `strict` is
/// false (best-effort: a warning only, since a missing nix is "not verified", not "wrong").
/// `strict` (`install --strict` only; `upgrade` has no such flag and is always best-effort here)
/// turns a missing nix into a hard `Code::Validation`. A genuine build failure (nix present, the
/// declared module set itself does not evaluate) is always a hard error regardless of `strict`:
/// that is "the declared set is broken", not merely "unverified".
fn build_and_cache_options(
    rev: &str,
    modules: &[OracleModulePin],
    strict: bool,
) -> Result<Option<String>, Code> {
    // Base set fetch/build stays manual (docs/06, ADR 0007); only the augmented set (ADR 0008)
    // is built here. Guards a plain baseline resolution (no module pins) from triggering a
    // real nixpkgs fetch.
    if rev.is_empty() || modules.is_empty() {
        return Ok(None);
    }
    let tuples: Vec<(String, String, String)> = modules
        .iter()
        .map(|m| (m.url.clone(), m.rev.clone(), m.attr.clone()))
        .collect();
    let Some(path) = knixl_oracle::cache_path_for(rev, &tuples) else {
        return Ok(None);
    };
    let eval = knixl_nix::nixeval::NixEval::resolve();
    let json = match knixl_nix::optionsdoc::build_options_json(&eval, rev, &tuples) {
        Ok(json) => json,
        Err(knixl_nix::nixeval::NixError::Unavailable(_)) if strict => {
            eprintln!("knixl: --strict: nix unavailable, cannot build the oracle option set");
            return Err(Code::Validation);
        }
        Err(knixl_nix::nixeval::NixError::Unavailable(_)) => {
            eprintln!("warning: nix unavailable, skipping oracle option set build");
            return Ok(None);
        }
        Err(knixl_nix::nixeval::NixError::Failed(m)) => {
            eprintln!("knixl: building the oracle option set failed: {m}");
            return Err(Code::Validation);
        }
    };
    if let Some(parent) = path.parent() {
        if let Err(e) = std::fs::create_dir_all(parent) {
            eprintln!("knixl: {}: {e}", parent.display());
            return Ok(None);
        }
    }
    if let Err(e) = std::fs::write(&path, &json) {
        eprintln!("knixl: {}: {e}", path.display());
        return Ok(None);
    }
    Ok(Some(knixl_nix::hash(json.as_bytes())))
}

/// Build + cache the augmented option set for every effective set THIS RUN changed, recording
/// each built content's hash into the matching entry of `lock` (the project's `oracle` for the
/// project-wide default set, a host's `baseline` for its own effective set) (ADR 0008). Called
/// only on confirmed apply (`upgrade --yes`, a confirmed `install`), mutating a local, not-yet-
/// written copy of the lock the caller commits: building shells out to nix and writes the cache
/// directory, so it must never run from the shared pending pre-pass (which also drives a plain
/// preview) nor from `plan`/`generate`/`check` (those stay offline).
///
/// `project_modules_changed` marks the project-wide set as changed; `changed_hosts` names hosts
/// whose own release or module override just changed. A host with no override of its own falls
/// back to the project's default set (mirroring `gather`'s `effective_modules`-equivalent
/// lookup), so its effective set also changes when the project set does, even though its own
/// entry in `changed_hosts` did not.
fn build_pending_oracle_sets(
    root: &std::path::Path,
    lock: &mut knixl_lock::Lock,
    project_modules_changed: bool,
    changed_hosts: &BTreeSet<String>,
    strict: bool,
) -> Result<(), Code> {
    if project_modules_changed {
        let rev = lock.oracle.nixpkgs_rev.clone();
        let modules = lock.oracle.modules.clone();
        if let Some(hash) = build_and_cache_options(&rev, &modules, strict)? {
            lock.oracle.options_hash = hash;
        }
    }

    use knixl_pipeline::install::list_hosts;
    let hosts = list_hosts(root).unwrap_or_default();
    let project_modules = lock.oracle.modules.clone();
    for host in &hosts {
        let Some((rev, own_modules)) = lock
            .baselines
            .get(&host.name)
            .map(|b| (b.nixpkgs_rev.clone(), b.modules.clone()))
        else {
            continue;
        };
        let src = std::fs::read_to_string(&host.path).unwrap_or_default();
        let has_override = knixl_pipeline::project::parse_host_oracle_modules(&src).is_some();
        let effective_changed = if has_override {
            changed_hosts.contains(&host.name)
        } else {
            project_modules_changed || changed_hosts.contains(&host.name)
        };
        if !effective_changed {
            continue;
        }
        let modules = if has_override {
            own_modules
        } else {
            project_modules.clone()
        };
        if let Some(hash) = build_and_cache_options(&rev, &modules, strict)? {
            lock.baselines.get_mut(&host.name).unwrap().options_hash = hash;
        }
    }
    Ok(())
}

/// The nixpkgs rev `choose_strategy` should treat as `host`'s baseline: `pending`'s rev if
/// the caller already has a pending resolution for this host in hand (issue #22 review fix:
/// no network lookup happens here any more), else the lock's already-recorded baseline for a
/// host that declares a release, else the project's global oracle rev for a host that
/// declares none. Takes `oracle_rev`/`baselines` rather than `&Ctx` (#28 review fix): the
/// interactive install's strategy closure (`make_strategy`) recomputes this per host, from
/// data captured once up front rather than a borrowed `Ctx`, since it runs off the event loop
/// and outlives any single call into `install()`.
fn effective_baseline_rev(
    oracle_rev: &str,
    baselines: &BTreeMap<String, HostBaseline>,
    host_path: &std::path::Path,
    host: &str,
    pending: Option<&HostBaseline>,
) -> String {
    if let Some(b) = pending {
        return b.nixpkgs_rev.clone();
    }
    if declared_release(host_path).is_none() {
        return oracle_rev.to_string();
    }
    baselines
        .get(host)
        .map(|b| b.nixpkgs_rev.clone())
        .unwrap_or_else(|| oracle_rev.to_string())
}

/// The `Inputs` `run` should plan `Cmd::Upgrade`/`Cmd::Install` against once `pending` has
/// resolutions in hand (issue #22 review fix): the same inputs, minus the "is not resolved:
/// run knixl upgrade" validation error for each host now pending, so those two commands can
/// reach the remedy the error names instead of being refused by it. `check`/`generate`/`plan`
/// never call this (their `pending` is always empty), so their validation error still fires.
fn patch_inputs_for_pending(
    inputs: &knixl_lock::reconcile::Inputs,
    pending: &BTreeMap<String, HostBaseline>,
) -> knixl_lock::reconcile::Inputs {
    use knixl_lock::reconcile::{ExpectedFile, Inputs};
    let validation_errors = inputs
        .validation_errors
        .iter()
        .filter(|e| {
            !pending.keys().any(|host| {
                e.starts_with(&format!("host \"{host}\": nixpkgs release \""))
                    && e.ends_with("is not resolved: run knixl upgrade")
            })
        })
        .cloned()
        .collect();
    Inputs {
        expected: inputs
            .expected
            .iter()
            .map(|e| ExpectedFile {
                path: e.path.clone(),
                hash: e.hash.clone(),
                from: e.from.clone(),
                modules: e.modules.clone(),
            })
            .collect(),
        input_hashes: inputs.input_hashes.clone(),
        validation_errors,
        referenced_pins: inputs.referenced_pins.clone(),
        declared_baselines: inputs.declared_baselines.clone(),
    }
}

/// Turn the TUI's `Outcome::Install` pin payload into `commit_install`'s `version_pin`
/// argument, and commit `baseline_pending` alongside it. `strategy` was already chosen
/// inside the TUI's Apply-gated verify sequence, so this writes it straight to the lock
/// rather than build-testing again; it is `None` only for an unversioned install (Apply
/// gates on a chosen strategy whenever a version was requested, see
/// `InstallModel::apply_allowed`). `no_abi_check` is unused here since the strategy decision
/// is already made; kept for parameter symmetry with `Outcome::Install`. Shared by the
/// interactive `install` branch and the Hub flow.
#[allow(clippy::too_many_arguments)]
fn commit_tui_install(
    host: knixl_pipeline::install::HostInfo,
    pkg: String,
    strict: bool,
    version: Option<String>,
    pin: Option<String>,
    _no_abi_check: bool,
    strategy: Option<knixl_lock::model::PinStrategy>,
    strategy_reason: Option<String>,
    baseline_pending: Option<HostBaseline>,
) -> Code {
    let version_pin = match (version.as_deref(), pin, strategy) {
        (Some(v), Some(rev), Some(strategy)) => {
            let resolved = knixl_nix::pin::Resolved { nixpkgs_rev: rev };
            Some((v, resolved, strategy))
        }
        _ => None,
    };
    let pin_args = version_pin
        .as_ref()
        .map(|(v, resolved, strategy)| (*v, resolved, *strategy));
    // The TUI/Hub commit path does not resolve project, host, or module-source pins (#35
    // phase 3 / ADR 0008 / task 5b wire only the plain `install`/`upgrade` paths; see the note
    // on `commit_install`).
    let code = commit_install(
        &host,
        &pkg,
        pin_args,
        baseline_pending.as_ref(),
        None,
        None,
        None,
        strict,
    );
    if code == Code::Clean {
        if let Some(b) = &baseline_pending {
            println!(
                "resolved nixpkgs release \"{}\" for host \"{}\" -> {}",
                b.release, host.name, b.nixpkgs_rev,
            );
        }
        if let Some((v, _, strategy)) = &version_pin {
            match &strategy_reason {
                Some(reason) => println!(
                    "pinned {pkg} {v} via {} ({reason})",
                    strategy_label(*strategy)
                ),
                None => println!("pinned {pkg} {v} via {}", strategy_label(*strategy)),
            }
        }
    }
    code
}

/// Open the TUI for the given entry: discover the project, list hosts, and inject a verify
/// function that (off the event-loop thread) drafts the host and checks it under nix.
fn open_tui(
    entry: tui::Entry,
    build: Option<tui::BuildFn>,
    pin: Option<tui::PinFn>,
    strategy: Option<tui::StrategyFn>,
) -> Result<tui::Outcome, String> {
    use knixl_pipeline::install::list_hosts;
    let root = discover_root();
    let hosts = list_hosts(&root).map_err(|e| e.to_string())?;
    let modules = browse_modules(&root);
    tui::run(
        entry,
        root.clone(),
        hosts,
        make_verify(root.clone()),
        modules,
        build,
        pin,
        strategy,
    )
}

/// Enumerate registered modules for the Browse screen: node name, kind tag, rendered schema
/// doc, and a host-insertion skeleton. Built here (not in the TUI) since the registry is not
/// `Send`. An unreadable project yields an empty list rather than failing the whole TUI.
fn browse_modules(root: &std::path::Path) -> Vec<tui::BrowseModule> {
    use knixl_modules::ModuleKind;
    let Ok(registry) = knixl_pipeline::gather::registry(root) else {
        return Vec::new();
    };
    let manifests = declarative_manifests(root);
    registry
        .entries()
        .map(|(node, m)| {
            let schema = m.schema();
            tui::BrowseModule {
                node: node.to_string(),
                kind: match m.kind() {
                    ModuleKind::Builtin => "built-in".to_string(),
                    ModuleKind::Declarative => "declarative".to_string(),
                },
                doc: schema.render_doc(node),
                skeleton: skeleton_for(node, schema),
                manifest: manifests.get(node).cloned(),
            }
        })
        .collect()
}

/// Maps each declarative module's claimed node to its manifest path, for the Browse screen's
/// edit action. The registry itself does not carry the path (`DeclarativeModule` only keeps it
/// for error messages), so this walks `root/modules/*/knixl-module.kdl` directly and pairs each
/// manifest to the node it claims. An unreadable directory, or a manifest that fails to parse,
/// is skipped rather than failing the whole scan: Browse still shows a usable list, just without
/// an edit path for that one entry.
fn declarative_manifests(
    root: &std::path::Path,
) -> std::collections::BTreeMap<String, std::path::PathBuf> {
    let mut out = std::collections::BTreeMap::new();
    let dir = root.join("modules");
    let Ok(read_dir) = std::fs::read_dir(&dir) else {
        return out;
    };
    for entry in read_dir.filter_map(|e| e.ok()) {
        let manifest = entry.path().join("knixl-module.kdl");
        let Ok(text) = std::fs::read_to_string(&manifest) else {
            continue;
        };
        if let Ok(editable) = knixl_modules::template::load_editable(&text) {
            out.insert(editable.node, manifest);
        }
    }
    out
}

/// A starting skeleton for inserting a module node into a host: the node with placeholders
/// for its required positional args and required props, and an empty `{ }` block if it takes
/// children. A starting point the user then edits, not a guaranteed-valid node.
fn skeleton_for(node: &str, schema: &knixl_modules::NodeSchema) -> String {
    use knixl_modules::ValueTy;
    fn placeholder(ty: &ValueTy) -> String {
        match ty {
            ValueTy::Bool => "#true".to_string(),
            ValueTy::Int => "0".to_string(),
            ValueTy::Str | ValueTy::Node => "\"\"".to_string(),
            ValueTy::Enum(opts) => opts
                .first()
                .map(|o| format!("\"{o}\""))
                .unwrap_or_else(|| "\"\"".to_string()),
        }
    }

    let mut head = node.to_string();
    for arg in schema.args.iter().filter(|a| a.required) {
        head.push(' ');
        head.push_str(&placeholder(&arg.ty));
    }
    for prop in schema.props.iter().filter(|p| p.required) {
        head.push_str(&format!(" {}={}", prop.name, placeholder(&prop.ty)));
    }

    let has_block = schema.open_children || schema.children.iter().any(|c| c.required);
    if has_block {
        format!("{head} {{\n}}")
    } else {
        head
    }
}

/// The verify function handed to the Install screen. It closes over only `Send` data (the
/// project root) and rebuilds the registry per call, so it stays `Send + Sync` for the async
/// off-thread verify. Recomputes both `pkgs.<pkg>` existence and whether the drafted host
/// parses.
fn make_verify(root: std::path::PathBuf) -> tui::VerifyFn {
    Arc::new(move |pkg: &str, host: &knixl_pipeline::install::HostInfo| {
        let formatter = default_formatter();
        let tool: semver::Version = env!("CARGO_PKG_VERSION")
            .parse()
            .expect("tool version parses");
        match knixl_pipeline::gather::gather(&root, &formatter, tool.clone()) {
            Ok(project) => {
                let (preview, parses) = preview_host(
                    &project.registry,
                    &formatter,
                    &tool,
                    host,
                    pkg,
                    &project.oracles,
                );
                let resolves = resolve_package_rev(&project.lock.oracle.nixpkgs_rev, pkg);
                tui::Verified {
                    preview,
                    resolves,
                    parses,
                }
            }
            Err(e) => tui::Verified {
                preview: format!("(preview unavailable: {e})"),
                resolves: tui::Resolve::Skipped,
                parses: tui::Parse::Skipped,
            },
        }
    })
}

/// The build function for the Install screen: builds `pkgs.<pkg>` from the lock's pinned rev
/// (ambient fallback), mapping nix errors to a coarse outcome. Closes over only `root`.
fn make_build(root: std::path::PathBuf) -> tui::BuildFn {
    use knixl_nix::nixeval::{NixError, NixEval, Nixpkgs};
    Arc::new(move |pkg: &str| {
        let rev = read_pinned_rev(&root);
        let src = if rev.is_empty() {
            Nixpkgs::Ambient
        } else {
            Nixpkgs::PinnedRev(rev)
        };
        match NixEval::resolve().builds(&src, pkg) {
            Ok(()) => tui::BuildOutcome::Ok,
            Err(NixError::Unavailable(_)) => tui::BuildOutcome::Skipped,
            Err(NixError::Failed(_)) => tui::BuildOutcome::Failed,
        }
    })
}

/// The pin-resolve function for the Install screen: resolves `name@version` to a nixpkgs
/// commit via `PinResolver`, mapping resolver errors to a coarse outcome. Injected only when
/// a version was requested; takes no project state (the resolver is version-independent of
/// the current lock).
fn make_pin() -> tui::PinFn {
    use knixl_nix::pin::{PinError, PinResolver};
    Arc::new(
        move |name: &str, version: &str| match PinResolver::resolve().lookup(name, version) {
            Ok(r) => tui::PinOutcome::Resolved(r.nixpkgs_rev),
            Err(PinError::NotFound(_)) => tui::PinOutcome::NotFound,
            Err(PinError::Unavailable(_)) => tui::PinOutcome::Unavailable,
            Err(PinError::Failed(_)) => tui::PinOutcome::Failed,
        },
    )
}

/// Decide the strategy for pinning `name` at `rev`, then explain why in a short phrase for the
/// `pinned ... via ...` status line. The third element of the `Ok` tuple is whether a real
/// build attempt happened (as opposed to a skip on `no_abi_check`, absent nix, or `rev`
/// matching the baseline): callers reuse that to avoid a second, redundant build when `--build`
/// was also requested. `Err` carries both candidate build failures, verbatim from
/// `SelectError::NeitherBuilds` (which has no Display impl), for the caller to report.
fn choose_strategy(
    name: &str,
    rev: &str,
    baseline_rev: &str,
    no_abi_check: bool,
) -> Result<(knixl_lock::model::PinStrategy, String, bool), (String, String)> {
    use knixl_lock::model::PinStrategy;
    use knixl_nix::nixeval::NixEval;
    use knixl_pipeline::{select_strategy, SelectError};

    let nix_available = nix_build_available();
    let skipped = no_abi_check || !nix_available || rev == baseline_rev;
    let nix = NixEval::resolve();
    let build = |expr: &str| nix.builds_expr(expr).map_err(|e| e.to_string());
    match select_strategy(rev, baseline_rev, name, nix_available, no_abi_check, &build) {
        Ok(PinStrategy::Override) => Ok((PinStrategy::Override, "build ok".to_string(), true)),
        Ok(PinStrategy::CommitMix) if skipped => {
            let reason = if no_abi_check {
                "--no-abi-check"
            } else if !nix_available {
                "nix unavailable"
            } else {
                "matches baseline"
            };
            Ok((PinStrategy::CommitMix, reason.to_string(), false))
        }
        Ok(PinStrategy::CommitMix) => Ok((
            PinStrategy::CommitMix,
            "override build failed".to_string(),
            true,
        )),
        Err(SelectError::NeitherBuilds { commit_mix, over }) => Err((commit_mix, over)),
    }
}

/// Whether nix's build oracle (the same binary `NixEval::builds_expr` would use,
/// `KNIXL_NIX_BUILD` injectable) is present at all. Probed with `--version` rather than a real
/// evaluation, so detecting absence never touches the network. Feeds `select_strategy`'s
/// `nix_available` gate.
fn nix_build_available() -> bool {
    let nix = knixl_nix::nixeval::NixEval::resolve();
    std::process::Command::new(&nix.build_bin)
        .arg("--version")
        .output()
        .is_ok()
}

/// Maps `choose_strategy`'s decision to the TUI's `StrategyOutcome`, so `make_strategy`'s
/// closure shares `choose_strategy`'s decision logic (and the plain path's error text) instead
/// of a second copy that could drift.
fn strategy_outcome(
    name: &str,
    rev: &str,
    baseline_rev: &str,
    no_abi_check: bool,
) -> tui::StrategyOutcome {
    match choose_strategy(name, rev, baseline_rev, no_abi_check) {
        Ok((strategy, label, _tested)) => tui::StrategyOutcome::Chosen { strategy, label },
        Err((commit_mix, over)) => {
            tui::StrategyOutcome::Failed(format!("commit-mix: {commit_mix}; override: {over}"))
        }
    }
}

/// Builds the TUI's strategy-selection closure: given `(name, rev, host_name)`, computes
/// `host_name`'s own baseline (reproducing `effective_baseline_rev`'s logic against data
/// captured once, up front) and runs the same decision `choose_strategy` runs for the plain
/// path. Host-aware: a host switch inside the TUI re-fires this against the newly selected
/// host's baseline, not the one selected when the TUI opened.
fn make_strategy(
    hosts: Vec<knixl_pipeline::install::HostInfo>,
    baselines: BTreeMap<String, HostBaseline>,
    oracle_rev: String,
    pending: BTreeMap<String, HostBaseline>,
    no_abi_check: bool,
) -> tui::StrategyFn {
    Arc::new(move |name: &str, rev: &str, host_name: &str| {
        let baseline_rev = hosts
            .iter()
            .find(|h| h.name == host_name)
            .map(|h| {
                effective_baseline_rev(
                    &oracle_rev,
                    &baselines,
                    &h.path,
                    host_name,
                    pending.get(host_name),
                )
            })
            .unwrap_or_else(|| oracle_rev.clone());
        strategy_outcome(name, rev, &baseline_rev, no_abi_check)
    })
}

/// Human-readable name for a `PinStrategy`, for the `pinned ... via ...` status line.
fn strategy_label(strategy: knixl_lock::model::PinStrategy) -> &'static str {
    match strategy {
        knixl_lock::model::PinStrategy::CommitMix => "commit-mix",
        knixl_lock::model::PinStrategy::Override => "override",
    }
}

fn read_pinned_rev(root: &std::path::Path) -> String {
    let formatter = default_formatter();
    let tool: semver::Version = env!("CARGO_PKG_VERSION")
        .parse()
        .expect("tool version parses");
    knixl_pipeline::gather::gather(root, &formatter, tool)
        .map(|p| p.lock.oracle.nixpkgs_rev)
        .unwrap_or_default()
}

/// Generate the drafted host in memory (no disk writes) and parse it, for the TUI preview.
fn preview_host(
    registry: &knixl_modules::Registry,
    formatter: &knixl_nix::Formatter,
    tool: &semver::Version,
    host: &knixl_pipeline::install::HostInfo,
    pkg: &str,
    oracles: &std::collections::BTreeMap<String, knixl_oracle::Oracle>,
) -> (String, tui::Parse) {
    use knixl_pipeline::{generate, install::add_package, HostSource};
    let src = std::fs::read_to_string(&host.path).unwrap_or_default();
    let drafted = match add_package(&src, pkg, None) {
        Ok(Some(d)) => d,
        _ => src,
    };
    let no_pins = std::collections::BTreeMap::new();
    let nix = generate(
        &[HostSource {
            path: host.path.clone(),
            src: drafted,
        }],
        registry,
        formatter,
        tool,
        oracles,
        &no_pins,
        knixl_modules::SecretsBackend::default(),
    )
    .ok()
    .and_then(|files| {
        files
            .into_iter()
            .map(|f| f.text)
            .find(|t| t.contains("systemPackages"))
    })
    .unwrap_or_else(|| "(preview unavailable)".to_string());

    let snippet = systempackages_snippet(&nix);
    (snippet, parse_text(&nix))
}

/// The `environment.systemPackages = [ ... ];` block, for a compact preview.
fn systempackages_snippet(nix: &str) -> String {
    let mut out = Vec::new();
    let mut in_block = false;
    for line in nix.lines() {
        if line.contains("systemPackages") {
            in_block = true;
        }
        if in_block {
            out.push(line.trim_end());
            if line.contains("];") || (line.contains(';') && !line.contains('[')) {
                break;
            }
        }
    }
    if out.is_empty() {
        "(no packages)".to_string()
    } else {
        out.join("\n")
    }
}

fn parse_text(nix: &str) -> tui::Parse {
    use knixl_nix::nixeval::{NixError, NixEval};
    let tmp = std::env::temp_dir().join(format!("knixl-tui-parse-{}.nix", std::process::id()));
    if std::fs::write(&tmp, nix).is_err() {
        return tui::Parse::Skipped;
    }
    let verdict = NixEval::resolve().parses(&tmp);
    let _ = std::fs::remove_file(&tmp);
    match verdict {
        Ok(()) => tui::Parse::Ok,
        Err(NixError::Unavailable(_)) => tui::Parse::Skipped,
        Err(NixError::Failed(m)) => tui::Parse::Failed(m),
    }
}

/// `pkgs.<pkg>` existence against the lock's pinned rev (ambient fallback), as a `Resolve`.
fn resolve_package(ctx: &Ctx, pkg: &str) -> tui::Resolve {
    resolve_package_rev(&ctx.lock.oracle.nixpkgs_rev, pkg)
}

/// As `resolve_package`, but taking the pinned rev directly (for the TUI verify closure,
/// which rebuilds its own project state).
fn resolve_package_rev(rev: &str, pkg: &str) -> tui::Resolve {
    use knixl_nix::nixeval::{NixError, NixEval, Nixpkgs};
    let src = if rev.is_empty() {
        Nixpkgs::Ambient
    } else {
        Nixpkgs::PinnedRev(rev.to_string())
    };
    match NixEval::resolve().package_exists(&src, pkg) {
        Ok(true) => tui::Resolve::Yes,
        Ok(false) => tui::Resolve::No,
        Err(NixError::Unavailable(_)) => tui::Resolve::Skipped,
        Err(NixError::Failed(_)) => tui::Resolve::No,
    }
}

/// Parse the drafted generated files. `Some(code)` to stop, `None` to proceed. A missing
/// nix skips silently (the package step already reported it).
fn verify_parse(ctx: &Ctx, _strict: bool) -> Option<Code> {
    use knixl_nix::nixeval::{NixError, NixEval};
    let nix = NixEval::resolve();
    for (path, text) in &ctx.generated {
        let name = path
            .file_name()
            .and_then(|s| s.to_str())
            .unwrap_or("out.nix");
        let tmp = std::env::temp_dir().join(format!("knixl-parse-{}-{name}", std::process::id()));
        if std::fs::write(&tmp, text).is_err() {
            continue;
        }
        let verdict = nix.parses(&tmp);
        let _ = std::fs::remove_file(&tmp);
        match verdict {
            Ok(()) => {}
            Err(NixError::Unavailable(_)) => return None,
            Err(NixError::Failed(m)) => {
                eprintln!("knixl: generated {} does not parse: {m}", path.display());
                return Some(Code::Validation);
            }
        }
    }
    None
}

fn confirm(prompt: &str) -> bool {
    use std::io::Write;
    print!("{prompt} [y/N] ");
    let _ = std::io::stdout().flush();
    let mut line = String::new();
    std::io::stdin().read_line(&mut line).is_ok()
        && matches!(line.trim().chars().next(), Some('y') | Some('Y'))
}

fn main() {
    // A panic anywhere in planning or applying maps to the Internal exit code (docs/05),
    // so callers get a stable code instead of a raw abort.
    let code = std::panic::catch_unwind(dispatch).unwrap_or(Code::Internal);
    std::process::exit(code as i32);
}

fn dispatch() -> Code {
    let cli = Cli::parse();
    // The TUI does not reconcile a project up front (Home works anywhere), so it skips
    // Ctx::load and its formatter requirement.
    if matches!(cli.cmd, Cmd::Tui) {
        if !std::io::stdin().is_terminal() || !std::io::stdout().is_terminal() {
            eprintln!("knixl: tui needs an interactive terminal");
            return Code::Usage;
        }
        return match open_tui(tui::Entry::Hub, None, None, None) {
            Ok(outcome) => finish_tui_outcome(outcome),
            Err(e) => {
                eprintln!("knixl: {e}");
                Code::Internal
            }
        };
    }
    run(cli, &Ctx::load())
}

/// Act on what the hub session decided: an Install outcome commits the package; a plain quit
/// or cancel does nothing.
fn finish_tui_outcome(outcome: tui::Outcome) -> Code {
    match outcome {
        // `knixl tui` (the Hub flow) has no `--no-abi-check` flag: `Entry::Hub` seeds it
        // `false` in `InstallModel::enter`, matching prior behaviour. The Hub flow also has
        // no `run` pre-pass (`Cmd::Tui` is dispatched before `Ctx::load`), so it resolves
        // this one host's pending baseline itself before committing (issue #22 review fix:
        // this used to be skipped entirely for an ambient install here). It also never
        // requests a version (`Entry::Hub` seeds `version: None`), so `strategy` and
        // `strategy_reason` are always `None` here too.
        tui::Outcome::Install {
            host,
            pkg,
            strict,
            version,
            pin,
            no_abi_check,
            strategy,
            strategy_reason,
        } => {
            let ctx = Ctx::load();
            let baseline_pending = match resolve_pending_baseline(&ctx, &host.name, &host.path) {
                Ok(b) => b,
                Err(code) => return code,
            };
            commit_tui_install(
                host,
                pkg,
                strict,
                version,
                pin,
                no_abi_check,
                strategy,
                strategy_reason,
                baseline_pending,
            )
        }
        tui::Outcome::Insert {
            host,
            node,
            skeleton,
        } => commit_insert(&host, &node, &skeleton),
        tui::Outcome::Scaffold { name, manifest } => commit_scaffold(&name, &manifest),
        tui::Outcome::SaveModule { path, text } => commit_save_module(&path, &text),
        tui::Outcome::Cancelled | tui::Outcome::Quit => Code::Clean,
    }
}

/// Write a scaffolded module manifest to `modules/<name>/knixl-module.kdl`, refusing to
/// overwrite an existing module.
fn commit_scaffold(name: &str, manifest: &str) -> Code {
    let dir = discover_root().join("modules").join(name);
    let path = dir.join("knixl-module.kdl");
    if path.exists() {
        eprintln!(
            "knixl: module `{name}` already exists at {}",
            path.display()
        );
        return Code::Validation;
    }
    if let Err(e) = std::fs::create_dir_all(&dir) {
        eprintln!("knixl: {}: {e}", dir.display());
        return Code::Internal;
    }
    if let Err(e) = std::fs::write(&path, manifest) {
        eprintln!("knixl: {}: {e}", path.display());
        return Code::Internal;
    }
    println!(
        "created module `{name}`: edit {} then declare it on a host",
        path.display()
    );
    Code::Clean
}

/// Overwrite an existing module manifest with edited text (Edit mode). Unlike commit_scaffold
/// this expects the file to exist and replaces it.
fn commit_save_module(path: &std::path::Path, text: &str) -> Code {
    if let Err(e) = std::fs::write(path, text) {
        eprintln!("knixl: {}: {e}", path.display());
        return Code::Internal;
    }
    println!("updated {}", path.display());
    Code::Clean
}

/// Scaffold a module node into a host's KDL: splice the skeleton and write the file. Unlike
/// install this does not regenerate, since the skeleton is a starting point the user edits
/// before running `knixl generate`.
fn commit_insert(host: &knixl_pipeline::install::HostInfo, node: &str, skeleton: &str) -> Code {
    use knixl_pipeline::install::add_node;
    let original = match std::fs::read_to_string(&host.path) {
        Ok(s) => s,
        Err(e) => {
            eprintln!("knixl: {}: {e}", host.path.display());
            return Code::Internal;
        }
    };
    match add_node(&original, node, skeleton) {
        Ok(Some(draft)) => {
            if let Err(e) = std::fs::write(&host.path, &draft) {
                eprintln!("knixl: {}: {e}", host.path.display());
                return Code::Internal;
            }
            println!(
                "added {node} to {}: edit {} then run `knixl generate`",
                host.name,
                host.path.display()
            );
            Code::Clean
        }
        Ok(None) => {
            println!("{node} is already declared on {}", host.name);
            Code::Clean
        }
        Err(e) => {
            eprintln!("knixl: cannot edit {}: {e}", host.path.display());
            Code::Internal
        }
    }
}

struct Ctx {
    inputs: knixl_lock::reconcile::Inputs,
    disk: knixl_lock::reconcile::DiskState,
    lock: knixl_lock::Lock,
    running: knixl_lock::reconcile::Versions,
    registry: knixl_modules::Registry,
    root: std::path::PathBuf,
    generated: std::collections::BTreeMap<std::path::PathBuf, String>,
    warnings: Vec<String>,
}
impl Ctx {
    fn load() -> Ctx {
        let root = discover_root();
        let tool = env!("CARGO_PKG_VERSION")
            .parse()
            .expect("tool version parses");
        let project = knixl_pipeline::gather::gather(&root, &default_formatter(), tool)
            .unwrap_or_else(|e| {
                eprintln!("knixl: {e}");
                std::process::exit(Code::Internal as i32);
            });
        Ctx {
            inputs: project.inputs,
            disk: project.disk,
            lock: project.lock,
            running: project.versions,
            registry: project.registry,
            root: project.root,
            generated: project.generated,
            warnings: project.warnings,
        }
    }
}

/// Walk up from the working directory to the first dir holding a lock or a `hosts/`.
fn discover_root() -> std::path::PathBuf {
    let start = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));
    let mut dir = start.as_path();
    loop {
        if dir.join("knixl.lock.kdl").exists() || dir.join("hosts").is_dir() {
            return dir.to_path_buf();
        }
        match dir.parent() {
            Some(parent) => dir = parent,
            None => return start,
        }
    }
}

/// The pinned formatter. `KNIXL_FORMATTER` overrides the binary (e.g. `cat` in tests);
/// otherwise the binary is autodetected among the known names for the RFC-style nixfmt.
/// The recorded formatter *name* stays `nixfmt-rfc-style` regardless of the binary file
/// name, so the lock is stable across machines.
fn default_formatter() -> knixl_nix::Formatter {
    let bin = choose_formatter_bin(std::env::var("KNIXL_FORMATTER").ok(), formatter_runs);
    knixl_nix::Formatter::detect("nixfmt-rfc-style", bin.into(), "0.6.0")
}

/// Pick the formatter binary. `KNIXL_FORMATTER` wins; otherwise the first candidate that
/// runs (the packaged binary is `nixfmt`, but some setups expose `nixfmt-rfc-style`); if
/// none run, `nixfmt` so the not-found error names the usual binary.
fn choose_formatter_bin(env_override: Option<String>, runs: impl Fn(&str) -> bool) -> String {
    if let Some(bin) = env_override {
        return bin;
    }
    for cand in ["nixfmt", "nixfmt-rfc-style"] {
        if runs(cand) {
            return cand.to_string();
        }
    }
    "nixfmt".to_string()
}

/// Whether `bin --version` runs successfully (used to probe candidate formatter names).
fn formatter_runs(bin: &str) -> bool {
    std::process::Command::new(bin)
        .arg("--version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false)
}

fn state_label(state: &FileState) -> &'static str {
    match state {
        FileState::Clean => "clean",
        FileState::Stale { .. } => "stale",
        FileState::Drifted { .. } => "drifted",
        FileState::Missing { .. } => "missing",
        FileState::Orphaned => "orphaned",
    }
}

fn print_plan(p: &Plan, json: bool) {
    if json {
        let files: Vec<String> = p
            .files
            .iter()
            .map(|f| {
                format!(
                    "{{\"path\":{:?},\"state\":{:?}}}",
                    f.path.display().to_string(),
                    state_label(&f.state)
                )
            })
            .collect();
        println!("{{\"files\":[{}]}}", files.join(","));
        return;
    }
    if p.files.is_empty() {
        println!("no generated files tracked");
        return;
    }
    for f in &p.files {
        println!("{:>8}  {}", state_label(&f.state), f.path.display());
    }
}
fn print_migration_notes(plan: &Plan, registry: &knixl_modules::Registry) {
    // Module deltas are identical across files; read them from the first skew present.
    let Some(skew) = plan.files.iter().find_map(|f| f.skew.as_ref()) else {
        println!("(no migration notes)");
        return;
    };
    let mut printed = false;
    for (name, delta) in &skew.modules {
        let Some(module) = registry.get(name) else {
            continue;
        };
        let notes = module.migration_notes(&delta.locked, &delta.running);
        if notes.is_empty() {
            continue;
        }
        println!("{name} {} -> {}:", delta.locked, delta.running);
        for n in &notes {
            println!("  - {n}");
        }
        printed = true;
    }
    if !printed {
        println!("(no migration notes)");
    }
}

fn print_doc(ctx: &Ctx, node: &str, _json: bool) {
    match ctx.registry.get(node) {
        Some(m) => print!("{}", m.schema().render_doc(node)),
        None => eprintln!("no module claims node `{node}`"),
    }
}

fn report_validation(errors: &[String], _json: bool) {
    for e in errors {
        eprintln!("validation: {e}");
    }
}

fn report_warnings(warnings: &[String]) {
    for w in warnings {
        eprintln!("warning: {w}");
    }
}

fn report_skew(_p: &Plan, _json: bool) {
    eprintln!("version skew: recorded versions differ from the running tool; run `knixl upgrade`");
}

fn report_taint(f: &knixl_lock::FilePlan, _json: bool) {
    eprintln!(
        "drift: {} was hand-edited; refusing to overwrite (use --accept-drift)",
        f.path.display()
    );
}

fn note_orphan(f: &knixl_lock::FilePlan, _json: bool) {
    eprintln!(
        "orphan: {} is no longer generated (use --prune to delete)",
        f.path.display()
    );
}

/// Write the freshly generated content for `f` to disk, creating parent directories.
fn write_file(ctx: &Ctx, f: &knixl_lock::FilePlan) {
    let target = ctx.root.join(&f.path);
    let Some(text) = ctx.generated.get(&f.path) else {
        eprintln!("knixl: no generated content for {}", f.path.display());
        return;
    };
    if let Some(parent) = target.parent() {
        if let Err(e) = std::fs::create_dir_all(parent) {
            eprintln!("knixl: {}: {e}", parent.display());
            return;
        }
    }
    if let Err(e) = std::fs::write(&target, text) {
        eprintln!("knixl: {}: {e}", target.display());
    }
}

fn delete_file(ctx: &Ctx, f: &knixl_lock::FilePlan) {
    let target = ctx.root.join(&f.path);
    if let Err(e) = std::fs::remove_file(&target) {
        eprintln!("knixl: {}: {e}", target.display());
    }
}

fn write_lock(ctx: &Ctx, lock: &knixl_lock::Lock) {
    let target = ctx.root.join("knixl.lock.kdl");
    if let Err(e) = std::fs::write(&target, lock.render()) {
        eprintln!("knixl: {}: {e}", target.display());
    }
}

#[cfg(test)]
mod tests {
    use super::{
        choose_formatter_bin, commit_save_module, commit_tui_install, make_strategy, Code,
    };
    use crate::tui;

    /// Serializes tests that mutate `KNIXL_NIX_BUILD` (a process-global env var): cargo runs
    /// `#[test]` fns in this binary concurrently by default, so two tests each doing their own
    /// sequential set/assert/restore of the same var would otherwise race each other.
    static ENV_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());

    #[test]
    fn env_override_wins() {
        assert_eq!(choose_formatter_bin(Some("cat".into()), |_| true), "cat");
    }

    #[test]
    fn prefers_nixfmt_when_present() {
        assert_eq!(choose_formatter_bin(None, |_| true), "nixfmt");
    }

    #[test]
    fn falls_back_to_alternative_name() {
        // nixfmt not found, but the nixfmt-rfc-style binary is.
        assert_eq!(
            choose_formatter_bin(None, |b| b == "nixfmt-rfc-style"),
            "nixfmt-rfc-style"
        );
    }

    #[test]
    fn defaults_to_nixfmt_when_none_run() {
        assert_eq!(choose_formatter_bin(None, |_| false), "nixfmt");
    }

    #[test]
    fn commit_save_module_overwrites_an_existing_file() {
        let path = std::env::temp_dir().join(format!(
            "knixl-cli-commit-save-module-{}.kdl",
            std::process::id()
        ));
        std::fs::write(&path, "original").unwrap();
        let code = commit_save_module(&path, "updated text");
        assert!(code == Code::Clean, "commit_save_module writes cleanly");
        assert_eq!(std::fs::read_to_string(&path).unwrap(), "updated text");
        let _ = std::fs::remove_file(&path);
    }

    /// A shim mimicking `nix-build`: exits 0 when `build_ok`, else 1. Mirrors
    /// `tests/cli.rs`'s `build_shim`; this test stubs `KNIXL_NIX_BUILD` in-process (no
    /// subprocess) since `make_strategy`'s closure runs directly, not via the `knixl` binary.
    fn build_shim(tag: &str, build_ok: bool) -> std::path::PathBuf {
        use std::io::Write;
        use std::os::unix::fs::PermissionsExt;
        let path =
            std::env::temp_dir().join(format!("knixl-cli-mainshim-{}-{tag}", std::process::id()));
        let exit = if build_ok { 0 } else { 1 };
        let script = format!("#!/bin/sh\nexit {exit}\n");
        let mut f = std::fs::File::create(&path).unwrap();
        f.write_all(script.as_bytes()).unwrap();
        f.flush().unwrap();
        drop(f); // close before exec, or spawning races with ETXTBSY
        std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
        path
    }

    /// A single-host `make_strategy` closure: `host` is not on disk, so `declared_release`
    /// finds nothing and `effective_baseline_rev` falls straight through to `oracle_rev`,
    /// mirroring what the old, single-baseline `make_strategy` test exercised.
    fn single_host_strategy_fn(oracle_rev: &str, no_abi_check: bool) -> tui::StrategyFn {
        let host = knixl_pipeline::install::HostInfo {
            name: "h".into(),
            default: false,
            path: std::path::PathBuf::from("/nonexistent/knixl-cli-make-strategy-test/h.kdl"),
        };
        make_strategy(
            vec![host],
            std::collections::BTreeMap::new(),
            oracle_rev.to_string(),
            std::collections::BTreeMap::new(),
            no_abi_check,
        )
    }

    /// `make_strategy`'s closure maps `choose_strategy`'s two outcomes: override builds ->
    /// `Chosen { Override, .. }`, neither builds -> `Failed(..)`. Both cases live in one test
    /// (rather than two `#[test]` fns) because `KNIXL_NIX_BUILD` is process-global and cargo
    /// runs tests in this binary concurrently; sharing one thread's sequential env-var swap
    /// avoids a race with itself.
    #[test]
    fn make_strategy_maps_build_outcomes() {
        let _guard = ENV_LOCK.lock().unwrap();
        let prior = std::env::var_os("KNIXL_NIX_BUILD");

        let ok_build = build_shim("override-ok", true);
        std::env::set_var("KNIXL_NIX_BUILD", &ok_build);
        let strategy_fn = single_host_strategy_fn("baseline-rev", false);
        match strategy_fn("pkg", "new-rev", "h") {
            tui::StrategyOutcome::Chosen { strategy, label } => {
                assert_eq!(strategy, knixl_lock::model::PinStrategy::Override);
                assert_eq!(label, "build ok");
            }
            tui::StrategyOutcome::Failed(msg) => panic!("expected Chosen, got Failed({msg})"),
        }
        let _ = std::fs::remove_file(&ok_build);

        let never_build = build_shim("neither-builds", false);
        std::env::set_var("KNIXL_NIX_BUILD", &never_build);
        let strategy_fn = single_host_strategy_fn("baseline-rev", false);
        match strategy_fn("pkg", "new-rev", "h") {
            tui::StrategyOutcome::Failed(msg) => {
                assert!(msg.contains("commit-mix"), "got: {msg}");
                assert!(msg.contains("override"), "got: {msg}");
            }
            tui::StrategyOutcome::Chosen { .. } => panic!("expected Failed, got Chosen"),
        }
        let _ = std::fs::remove_file(&never_build);

        match prior {
            Some(v) => std::env::set_var("KNIXL_NIX_BUILD", v),
            None => std::env::remove_var("KNIXL_NIX_BUILD"),
        }
    }

    /// `make_strategy` must decide against whichever host is named at call time, not a
    /// baseline fixed when the closure was built. Two hosts share a nixpkgs release but have
    /// different recorded baselines, so the same `(name, rev)` matches one host's baseline
    /// (skipped) but not the other's (build-tested).
    #[test]
    fn make_strategy_recomputes_the_baseline_for_the_selected_host() {
        let _guard = ENV_LOCK.lock().unwrap();
        let prior = std::env::var_os("KNIXL_NIX_BUILD");

        let root = std::env::temp_dir().join(format!(
            "knixl-cli-makestrategy-hosts-{}",
            std::process::id()
        ));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(&root).unwrap();
        let host_a_path = root.join("a.kdl");
        let host_b_path = root.join("b.kdl");
        std::fs::write(
            &host_a_path,
            "host \"a\" {\n    nixpkgs release=\"24.05\"\n}\n",
        )
        .unwrap();
        std::fs::write(
            &host_b_path,
            "host \"b\" {\n    nixpkgs release=\"24.05\"\n}\n",
        )
        .unwrap();

        let mut baselines = std::collections::BTreeMap::new();
        baselines.insert(
            "a".to_string(),
            knixl_lock::model::HostBaseline {
                release: "24.05".into(),
                nixpkgs_rev: "rev-a".into(),
                options_hash: String::new(),
                modules: Vec::new(),
            },
        );
        baselines.insert(
            "b".to_string(),
            knixl_lock::model::HostBaseline {
                release: "24.05".into(),
                nixpkgs_rev: "rev-b".into(),
                options_hash: String::new(),
                modules: Vec::new(),
            },
        );
        let hosts = vec![
            knixl_pipeline::install::HostInfo {
                name: "a".into(),
                default: false,
                path: host_a_path,
            },
            knixl_pipeline::install::HostInfo {
                name: "b".into(),
                default: false,
                path: host_b_path,
            },
        ];

        // Always succeeds: a rev decided against a baseline it does NOT match runs a real
        // override build test, which this shim always passes.
        let build_ok = build_shim("host-baseline", true);
        std::env::set_var("KNIXL_NIX_BUILD", &build_ok);
        let strategy_fn = make_strategy(
            hosts,
            baselines,
            "oracle-rev".to_string(),
            std::collections::BTreeMap::new(),
            false,
        );

        // "rev-b" IS host b's own baseline: matched without a build.
        match strategy_fn("pkg", "rev-b", "b") {
            tui::StrategyOutcome::Chosen { strategy, label } => {
                assert_eq!(strategy, knixl_lock::model::PinStrategy::CommitMix);
                assert_eq!(label, "matches baseline");
            }
            tui::StrategyOutcome::Failed(msg) => panic!("expected Chosen, got Failed({msg})"),
        }

        // The SAME rev, decided against host a instead: "rev-b" != host a's baseline
        // ("rev-a"), so this one build-tests and picks Override.
        match strategy_fn("pkg", "rev-b", "a") {
            tui::StrategyOutcome::Chosen { strategy, label } => {
                assert_eq!(strategy, knixl_lock::model::PinStrategy::Override);
                assert_eq!(label, "build ok");
            }
            tui::StrategyOutcome::Failed(msg) => panic!("expected Chosen, got Failed({msg})"),
        }

        let _ = std::fs::remove_file(&build_ok);
        let _ = std::fs::remove_dir_all(&root);
        match prior {
            Some(v) => std::env::set_var("KNIXL_NIX_BUILD", v),
            None => std::env::remove_var("KNIXL_NIX_BUILD"),
        }
    }

    /// Restores process-global state (`Ctx::load`'s cwd-based root discovery, `KNIXL_FORMATTER`,
    /// `KNIXL_NIX_BUILD`) on drop, so a panicked assertion in the test below still leaves the
    /// process sane for whichever other test runs next in this binary.
    struct RestoreEnv {
        cwd: std::path::PathBuf,
        formatter: Option<std::ffi::OsString>,
        build: Option<std::ffi::OsString>,
    }
    impl Drop for RestoreEnv {
        fn drop(&mut self) {
            let _ = std::env::set_current_dir(&self.cwd);
            match &self.formatter {
                Some(v) => std::env::set_var("KNIXL_FORMATTER", v),
                None => std::env::remove_var("KNIXL_FORMATTER"),
            }
            match &self.build {
                Some(v) => std::env::set_var("KNIXL_NIX_BUILD", v),
                None => std::env::remove_var("KNIXL_NIX_BUILD"),
            }
        }
    }

    /// `commit_tui_install` must use the strategy the TUI already chose, not re-derive it via
    /// a second build test. Proven with an always-failing build shim: if it still
    /// build-tested, both candidates would fail and the install would exit `Validation`
    /// instead of committing the passed-in `Override`.
    #[test]
    fn commit_tui_install_reuses_the_chosen_strategy_without_a_second_build() {
        use knixl_lock::model::PinStrategy;
        use knixl_pipeline::install::HostInfo;

        let _guard = ENV_LOCK.lock().unwrap();
        let _restore = RestoreEnv {
            cwd: std::env::current_dir().unwrap(),
            formatter: std::env::var_os("KNIXL_FORMATTER"),
            build: std::env::var_os("KNIXL_NIX_BUILD"),
        };

        let root = std::env::temp_dir().join(format!("knixl-cli-committui-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&root);
        std::fs::create_dir_all(root.join("hosts")).unwrap();
        std::fs::create_dir_all(root.join("modules/web-service")).unwrap();
        let examples = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../examples");
        std::fs::copy(examples.join("hosts/web.kdl"), root.join("hosts/web.kdl")).unwrap();
        std::fs::copy(
            examples.join("../crates/knixl-modules/stdlib/web-service/knixl-module.kdl"),
            root.join("modules/web-service/knixl-module.kdl"),
        )
        .unwrap();

        std::env::set_var("KNIXL_FORMATTER", "cat");
        // Always fails: the canary that proves `commit_tui_install` never calls the build
        // oracle itself any more.
        let never_build = build_shim("commit-tui-install", false);
        std::env::set_var("KNIXL_NIX_BUILD", &never_build);
        std::env::set_current_dir(&root).unwrap(); // `Ctx::load` (inside `commit_install`) discovers the root from cwd

        let host = HostInfo {
            name: "web".into(),
            default: false,
            path: root.join("hosts/web.kdl"),
        };
        let code = commit_tui_install(
            host,
            "htop".into(),
            false,
            Some("3.2.1".into()),
            Some("abc123".into()),
            false,
            Some(PinStrategy::Override),
            Some("build ok".into()),
            None,
        );
        assert!(
            code == Code::Clean,
            "a pre-chosen strategy commits without build-testing again"
        );

        let lock = std::fs::read_to_string(root.join("knixl.lock.kdl")).unwrap();
        assert!(
            lock.contains("strategy=\"override\""),
            "the passed-in strategy is recorded verbatim: {lock}"
        );

        let _ = std::fs::remove_file(&never_build);
        let _ = std::fs::remove_dir_all(&root);
    }
}