buildwithnexus 0.12.3

A hilariously fast agentic AI coding CLI — remote or local models, full TUI with live autocomplete, clean diffs, multimodal input, hooks, and checkpoints
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
// Library root. The binary is a thin shim; everything lives here so integration
// suites can reach the internals directly.

pub mod agent;
pub mod checkpoint;
pub mod config;
pub mod hooks;
pub mod knowledge;
pub mod local;
pub mod media;
pub mod onboarding;
pub mod provider;
pub mod report;
pub mod rules;
pub mod session;
pub mod tools;
pub mod trace;
pub mod tui;
pub mod update;
pub mod verifier;
pub mod workflow;

use std::io::IsTerminal;
use std::path::PathBuf;

use agent::Permission;
use config::Settings;
use provider::Msg;
use provider::Provider;

const VERSION: &str = env!("CARGO_PKG_VERSION");
const MAX_ATTACHED_FILE_BYTES: u64 = 48 * 1024;

#[derive(Default, Clone)]
struct CliOptions {
    provider: Option<String>,
    model: Option<String>,
    permission_mode: Option<String>,
    prompt: Option<String>,
}

fn parse_cli_options(args: Vec<String>) -> (CliOptions, Vec<String>) {
    let mut opts = CliOptions::default();
    let mut rest = Vec::new();
    let mut it = args.into_iter();
    while let Some(arg) = it.next() {
        if let Some(v) = arg.strip_prefix("--provider=") {
            opts.provider = Some(v.to_string());
        } else if arg == "--provider" {
            opts.provider = it.next();
        } else if let Some(v) = arg.strip_prefix("--model=") {
            opts.model = Some(v.to_string());
        } else if arg == "--model" {
            opts.model = it.next();
        } else if let Some(v) = arg.strip_prefix("--permission-mode=") {
            opts.permission_mode = Some(v.to_string());
        } else if let Some(v) = arg.strip_prefix("--permission=") {
            opts.permission_mode = Some(v.to_string());
        } else if arg == "--permission-mode" || arg == "--permission" {
            opts.permission_mode = it.next();
        } else if let Some(v) = arg.strip_prefix("--prompt=") {
            opts.prompt = Some(v.to_string());
        } else if arg == "--prompt" {
            opts.prompt = it.next();
        } else {
            rest.push(arg);
        }
    }
    (opts, rest)
}

pub fn run() {
    let mut args: Vec<String> = std::env::args().skip(1).collect();
    if args.iter().any(|a| a == "--json") {
        args.retain(|a| a != "--json");
        report::set(report::Mode::Json);
    }
    let (opts, args) = parse_cli_options(args);
    let cmd = args.first().map(String::as_str).unwrap_or("");
    let rest = || args[1..].join(" ");

    match cmd {
        "" => interactive(opts.prompt.clone(), opts),
        "init" | "da-init" | "setup" => {
            onboarding::run();
        }
        "providers" => {
            for p in config::PRESETS {
                let tag = if p.local { "local" } else { "remote" };
                println!("  {:<12} {:<26} {}", p.id, p.label, tag);
            }
        }
        "run" | "build" | "headless" | "--headless" | "-p" | "--print" => {
            headless(&opts, |p, perm, cwd| {
                agent::run_build(p, perm, "engineer", &rest(), &cwd)
            })
        }
        "plan" => headless(&opts, |p, perm, cwd| {
            agent::run_plan(p, perm, &rest(), &cwd)
        }),
        "brainstorm" => headless(&opts, |p, perm, cwd| {
            agent::run_brainstorm(p, perm, &cwd, &rest()).map(|_| ())
        }),
        "sessions" => {
            for s in session::list() {
                let title: String = s.title.chars().take(48).collect();
                println!("  {}  {:<48}  {}", s.id, title, s.cwd);
            }
        }
        "continue" | "-c" | "--continue" => {
            headless(&opts, |p, perm, cwd| match session::latest() {
                Some(s) => {
                    agent::run_build_resumed(p, perm, "engineer", &rest(), &cwd, s.msgs, &s.id)
                }
                None => Err("no sessions to continue".into()),
            })
        }
        "resume" | "-r" | "--resume" => {
            let id = args.get(1).cloned().unwrap_or_default();
            let task = if args.len() > 2 {
                args[2..].join(" ")
            } else {
                String::new()
            };
            headless(&opts, |p, perm, cwd| match session::load(&id) {
                Some(s) => {
                    agent::run_build_resumed(p, perm, "engineer", &task, &cwd, s.msgs, &s.id)
                }
                None => Err(format!("no session '{id}'")),
            })
        }
        "-v" | "-V" | "--version" | "version" => println!("buildwithnexus {VERSION}"),
        "-h" | "--help" | "help" => usage(),
        "doctor" => run_doctor(),
        _ if !args.is_empty() => {
            interactive(opts.prompt.clone().or_else(|| Some(args.join(" "))), opts)
        }
        other => {
            eprintln!("unknown command: {other}\n");
            usage();
            std::process::exit(2);
        }
    }
}

fn provider_or_onboard(opts: &CliOptions) -> Result<(Provider, Permission), String> {
    let mut settings = match config::load_settings() {
        Some(s) => s,
        None => onboarding::run().ok_or("setup cancelled")?,
    };
    if let Some(p) = &opts.provider {
        settings.provider = p.clone();
    }
    let mut provider = build_provider(&settings)?;
    if let Some(model) = &opts.model {
        provider.model = model.clone();
    }
    let perm_name = opts
        .permission_mode
        .as_deref()
        .unwrap_or(&settings.permission);
    Ok((provider, agent::permission(perm_name)))
}

pub fn build_provider(s: &Settings) -> Result<Provider, String> {
    let preset = config::preset(&s.provider).ok_or_else(|| {
        format!(
            "unknown provider '{}'; run `buildwithnexus init`",
            s.provider
        )
    })?;
    let base_url = match &s.base_url {
        Some(u) if !preset.env_key.is_empty() && !u.starts_with("https://") => {
            return Err(format!(
                "refusing to send the {} API key to a non-HTTPS endpoint ({u}); use https:// or a local provider",
                preset.env_key
            ));
        }
        Some(u) => u.clone(),
        None => preset.base_url.to_string(),
    };
    let model = if s.model.is_empty() {
        preset.default_model.to_string()
    } else {
        s.model.clone()
    };
    let api_key = if preset.env_key.is_empty() {
        None
    } else {
        config::load_key(preset.env_key)
    };
    if !preset.env_key.is_empty() && api_key.is_none() {
        return Err(format!(
            "{} not set; run `buildwithnexus init`",
            preset.env_key
        ));
    }
    let mut context_tokens = match preset.id {
        "anthropic" => 200_000,
        _ if preset.local => 8_192,
        _ => 128_000,
    };
    // An explicit settings override wins over presets and detection alike.
    if let Some(n) = s.context_tokens {
        context_tokens = n as usize;
    }
    // Backward compatibility: an explicit base_url pointing at the OpenAI-compat
    // surface (`…/v1`) keeps the OpenAI protocol — configs saved before the
    // native Ollama path existed (and users deliberately targeting a /v1
    // proxy) must not switch wire formats. Native is for root URLs only.
    let mut protocol = preset.protocol;
    if protocol == config::Protocol::OllamaNative
        && s.base_url
            .as_deref()
            .is_some_and(|u| u.trim_end_matches('/').ends_with("/v1"))
    {
        protocol = config::Protocol::OpenAi;
    }
    let mut provider = Provider {
        protocol,
        base_url,
        model,
        api_key,
        context_tokens,
        temperature: s.temperature,
        max_tokens: s.max_tokens,
        ollama_ctx: std::sync::OnceLock::new(),
    };
    if provider.protocol == config::Protocol::OllamaNative {
        if let Some(n) = s.context_tokens {
            // Pre-seed the probe cache: the native path uses the override as
            // num_ctx without ever querying /api/show.
            let _ = provider.ollama_ctx.set(Some(n));
        } else if let Some(n) = provider::ollama_ctx(&provider) {
            // Detected window replaces the hardcoded 8k local default so
            // compaction thresholds match what the model can actually hold.
            provider.context_tokens = n as usize;
        }
    }
    Ok(provider)
}

fn headless(
    opts: &CliOptions,
    f: impl FnOnce(&Provider, Permission, PathBuf) -> Result<(), String>,
) {
    let (provider, perm) = match provider_or_onboard(opts) {
        Ok(v) => v,
        Err(e) => {
            eprintln!("{}", tui::red(&e));
            std::process::exit(1);
        }
    };
    provider::prewarm(&provider);
    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));
    hooks::init(&cwd, false);
    hooks::notify("SessionStart", &cwd);

    if !report::is_json() {
        // No hand-drawn box: long provider/model/cwd values would shatter
        // fixed-width borders. Plain aligned rows can't overflow.
        println!(
            "{} {}",
            tui::bold("buildwithnexus headless"),
            tui::dim(&format!("v{}", crate::VERSION))
        );
        println!(
            "{}",
            tui::dim(&format!(
                "  model  {} · {}",
                provider.protocol, provider.model
            ))
        );
        println!("{}", tui::dim(&format!("  cwd    {}", cwd.display())));
        println!();
        // Off the critical path: five `which` probes cost real startup latency,
        // and with interactive=false this only prints when something is missing.
        std::thread::spawn(|| check_and_offer_install_dependencies(false));
    }

    let start_time = std::time::Instant::now();
    let r = f(&provider, perm, cwd.clone());
    let elapsed = start_time.elapsed();
    hooks::notify("SessionEnd", &cwd);

    if !report::is_json() {
        println!();
        if r.is_ok() {
            println!("{}", tui::green(&format!("✓ done in {elapsed:.2?}")));
        } else {
            println!("{}", tui::red(&format!("✗ failed after {elapsed:.2?}")));
        }
    }

    if let Err(e) = r {
        eprintln!("{}", tui::red(&e));
        std::process::exit(1);
    }
}

fn interactive(initial_prompt: Option<String>, opts: CliOptions) {
    // Always scaffold on interactive launch so existing users also get the
    // directory skeleton and starter Agents.md if they're missing.
    config::scaffold_home();
    let onboarded = config::load_settings().is_some();
    if !onboarded && onboarding::run().is_none() {
        return;
    }
    let (provider, perm) = match provider_or_onboard(&opts) {
        Ok(v) => v,
        Err(e) => {
            eprintln!("{}", tui::red(&e));
            std::process::exit(1);
        }
    };
    provider::prewarm(&provider);
    let cwd = std::env::current_dir().unwrap_or_else(|_| PathBuf::from("."));

    let raw = std::io::stdin().is_terminal() && std::io::stdout().is_terminal();
    hooks::init(&cwd, raw);
    hooks::notify("SessionStart", &cwd);
    tui::enter_alt(raw);
    let result = repl(provider, perm, &cwd, raw, initial_prompt);
    tui::leave_alt();
    hooks::notify("SessionEnd", &cwd);
    if let Err(e) = result {
        eprintln!("{}", tui::red(&e));
    }
}

// ── REPL ──────────────────────────────────────────────────────────────────────
fn repl(
    mut provider: Provider,
    mut perm: Permission,
    cwd: &std::path::Path,
    raw: bool,
    initial_prompt: Option<String>,
) -> Result<(), String> {
    let settings = config::load_settings().unwrap_or_default();
    tui::set_permission_mode(permission_label(&perm));

    // Show the full-screen header banner.
    let mode_name = "BUILD"; // default starting mode
    tui::show_banner(
        &settings.provider,
        &provider.model,
        mode_name,
        &cwd.display().to_string(),
    );
    tui::line(&tui::dim(
        "  describe a task · /help for all commands · !<cmd> for shell · Shift+Tab to change mode",
    ));
    if let Some(notice) = update::startup_notice(&settings.auto_update) {
        tui::line(&tui::dim(&notice));
    }
    // Off the critical path: five `which` probes cost real startup latency,
    // and with interactive=false this only prints when something is missing.
    std::thread::spawn(|| check_and_offer_install_dependencies(false));
    update::spawn_check(&settings.auto_update);

    let mut transcript: Vec<provider::Msg> = Vec::new();
    let mut sid = session::new_id();
    trace::set_session(&sid);
    let mut mode = Mode::Build;
    let mut last_suggested_mode: Option<&'static str> = None;
    // /btw: extra context injected into the next task without interrupting.
    let mut btw_ctx: Option<String> = None;
    let mut pending_prompt = initial_prompt;

    loop {
        // Tick background workflows and surface any completion notifications.
        // Color by outcome — a "✗ workflow failed" line must not render green.
        if let Some(note) = workflow::tick() {
            if note.contains('') {
                tui::line(&tui::red(&note));
            } else {
                tui::line(&tui::green(&note));
            }
        }
        // Prune old done/cancelled workflows, keep last 20.
        workflow::prune(20);

        // Show workflow activity badge if any are pending/running.
        let active = workflow::active_count();
        if active > 0 {
            tui::line(&tui::dim(&format!(
                "{} workflow{} in queue — /workflows to manage",
                active,
                if active == 1 { "" } else { "s" }
            )));
        }

        let mut task = if let Some(prompted) = pending_prompt.take() {
            tui::line("");
            tui::line(&format!(
                "{} {} {}",
                tui::mode_badge(mode_label(&mode)),
                tui::accent(""),
                prompted
            ));
            prompted
        } else {
            tui::line("");
            let prompt = format!(
                "{} {} ",
                tui::mode_badge(mode_label(&mode)),
                tui::accent("")
            );
            match tui::ask_task(&prompt) {
                None => return Ok(()),
                Some(tui::InputEvent::CycleMode) => {
                    mode = mode.next();
                    last_suggested_mode = None;
                    tui::show_mode_change(mode_label(&mode));
                    continue;
                }
                Some(tui::InputEvent::Text(t)) => t,
            }
        };
        let mut t = task.trim();
        if t.is_empty() {
            continue;
        }

        // Shell passthrough: `!cmd` runs in the shell directly.
        if let Some(cmd) = t.strip_prefix('!') {
            let cmd = cmd.trim();
            if !cmd.is_empty() {
                let out = tools::run("run_command", &serde_json::json!({ "command": cmd }), cwd);
                for l in out.content.lines() {
                    tui::line(&tui::dim(&format!("  {l}")));
                }
            }
            continue;
        }

        // /mode with an inline argument, e.g. `/mode build`, `/mode 1`.
        if let Some(mode_arg) = t.strip_prefix("/mode ") {
            match mode_arg.trim() {
                "1" | "plan" => {
                    mode = Mode::Plan;
                    last_suggested_mode = None;
                    tui::show_mode_change("PLAN");
                }
                "2" | "build" => {
                    mode = Mode::Build;
                    last_suggested_mode = None;
                    tui::show_mode_change("BUILD");
                }
                "3" | "brainstorm" => {
                    mode = Mode::Brainstorm;
                    last_suggested_mode = None;
                    tui::show_mode_change("BRAINSTORM");
                }
                other => tui::line(&tui::red(&format!(
                    "  unknown mode '{other}' — try: plan, build, brainstorm"
                ))),
            }
            continue;
        }

        // /permissions with an inline argument, e.g. `/permissions auto`.
        if let Some(perm_arg) = t.strip_prefix("/permissions ") {
            match perm_arg.trim() {
                "ask" | "1" => apply_permission(&mut perm, "ask"),
                "auto" | "2" => apply_permission(&mut perm, "auto"),
                "readonly" | "3" => apply_permission(&mut perm, "readonly"),
                other => tui::line(&tui::red(&format!(
                    "  unknown permission '{other}' — try: ask, auto, readonly"
                ))),
            }
            continue;
        }

        // /mouse or /scroll on|off — wheel transcript scrolling is on by
        // default; off restores terminal-native text selection.
        if let Some(mouse_arg) = t.strip_prefix("/mouse ") {
            handle_mouse(Some(mouse_arg.trim()));
            continue;
        }
        if let Some(mouse_arg) = t.strip_prefix("/scroll ") {
            handle_mouse(Some(mouse_arg.trim()));
            continue;
        }

        // /model with an inline argument — hot-swap the model mid-session.
        if let Some(model_arg) = t.strip_prefix("/model ") {
            let new_model = model_arg.trim();
            if !new_model.is_empty() {
                provider.model = new_model.to_string();
                if let Some(mut s) = config::load_settings() {
                    s.model = new_model.to_string();
                    config::save_settings(&s);
                }
                tui::line(&tui::green(&format!("  ✓ model → {new_model}")));
            }
            continue;
        }

        // /schedule <delay> <task>  e.g. `/schedule 5m git pull && cargo test`
        if let Some(rest) = t.strip_prefix("/schedule ") {
            let rest = rest.trim();
            let mut parts = rest.splitn(2, char::is_whitespace);
            let delay_str = parts.next().unwrap_or("").trim();
            let task = parts.next().unwrap_or("").trim();
            if task.is_empty() {
                tui::line(&tui::red(
                    "  usage: /schedule <delay> <task>  e.g. /schedule 5m cargo test",
                ));
            } else if let Some(fire_at) = workflow::parse_delay(delay_str) {
                let id = workflow::enqueue(
                    task,
                    workflow::WorkflowKind::Scheduled {
                        fire_at_ms: fire_at,
                    },
                );
                tui::line(&tui::green(&format!(
                    "  ✓ scheduled workflow #{id}: {task}"
                )));
            } else {
                tui::line(&tui::red(&format!(
                    "  invalid delay '{delay_str}' — try: 30s, 5m, 1h"
                )));
            }
            continue;
        }

        // /loop <interval> <task>  e.g. `/loop 10m cargo test`
        if let Some(rest) = t.strip_prefix("/loop ") {
            let rest = rest.trim();
            let mut parts = rest.splitn(2, char::is_whitespace);
            let interval_str = parts.next().unwrap_or("").trim();
            let task = parts.next().unwrap_or("").trim();
            if task.is_empty() {
                tui::line(&tui::red(
                    "  usage: /loop <interval> <task>  e.g. /loop 30m cargo test",
                ));
            } else if let Some(secs) = workflow::parse_interval_secs(interval_str) {
                let id = workflow::enqueue(
                    task,
                    workflow::WorkflowKind::Loop {
                        interval_secs: secs,
                    },
                );
                tui::line(&tui::green(&format!(
                    "  ✓ loop workflow #{id} every {secs}s: {task}"
                )));
            } else {
                tui::line(&tui::red(&format!(
                    "  invalid interval '{interval_str}' — try: 30s, 5m, 1h"
                )));
            }
            continue;
        }

        // /btw <context> — inject context into the next agent turn without stopping current work.
        if let Some(ctx) = t.strip_prefix("/btw ") {
            let ctx = ctx.trim();
            if ctx.is_empty() {
                tui::line(&tui::red(
                    "  usage: /btw <context>  e.g. /btw also update the tests",
                ));
            } else {
                btw_ctx = Some(ctx.to_string());
                tui::line(&tui::dim(&format!(
                    "  ⚑ context queued for next turn: {ctx}"
                )));
            }
            continue;
        }

        if let Some(task) = t.strip_prefix("/plan ") {
            tui::line("");
            if let Err(e) = agent::run_plan(&provider, perm, task.trim(), cwd) {
                tui::line(&tui::red(&format!("  {e}")));
            }
            tui::bell();
            continue;
        }
        if let Some(task) = t.strip_prefix("/build ") {
            tui::line("");
            if let Err(e) = agent::run_build_session(
                &provider,
                perm,
                "engineer",
                task.trim(),
                cwd,
                &mut transcript,
                &sid,
            ) {
                tui::line(&tui::red(&format!("  {e}")));
            }
            tui::bell();
            continue;
        }
        if let Some(task) = t.strip_prefix("/brainstorm ") {
            tui::line("");
            if let Err(e) = agent::run_brainstorm(&provider, perm, cwd, task.trim()).map(|_| ()) {
                tui::line(&tui::red(&format!("  {e}")));
            }
            tui::bell();
            continue;
        }

        match t {
            "/exit" | "/quit" | "exit" => return Ok(()),
            "/clear" => {
                transcript.clear();
                sid = session::new_id();
                trace::set_session(&sid);
                tui::clear();
                tui::line(&tui::dim("  ✓ context cleared — fresh session"));
                continue;
            }
            "/new" => {
                transcript.clear();
                sid = session::new_id();
                trace::set_session(&sid);
                tui::line(&tui::dim("  started a fresh session"));
                continue;
            }
            "/resume" => {
                handle_resume(&mut transcript, &mut sid);
                trace::set_session(&sid);
                continue;
            }
            "/trace" => {
                trace::render_list(30);
                continue;
            }
            "/help" => {
                print_help();
                continue;
            }
            "/init" => {
                tui::leave_alt();
                onboarding::run();
                tui::enter_alt(raw);
                continue;
            }
            "/model" => {
                handle_model(&mut provider);
                continue;
            }
            "/compact" => {
                handle_compact(&provider, &mut transcript);
                continue;
            }
            "/review" => {
                tui::line(&tui::accent("  /review — AI code review"));
                tui::line(&tui::dim("  Reviews staged changes (or the last diff). Press Enter to review, or type a focus area."));
                let focus = tui::ask("  focus (optional): ").unwrap_or_default();
                let task = if focus.trim().is_empty() {
                    "Review the current git diff (git diff HEAD and git diff --staged). Summarize what changed, identify bugs, style issues, and potential improvements. Be concise.".to_string()
                } else {
                    format!("Review the current git diff focusing on: {}. Run `git diff HEAD` and `git diff --staged` to see the changes.", focus.trim())
                };
                tui::line("");
                if let Err(e) = agent::run_build_session(
                    &provider,
                    perm,
                    "researcher",
                    &task,
                    cwd,
                    &mut transcript,
                    &sid,
                ) {
                    tui::line(&tui::red(&format!("  {e}")));
                }
                tui::bell();
                continue;
            }
            "/commit" => {
                let task = "Generate a conventional git commit message for the staged changes. Run `git diff --staged` to see what's staged. Then run `git commit -m \"<message>\"` with the generated message. If nothing is staged, remind the user to `git add` files first.";
                tui::line("");
                if let Err(e) = agent::run_build_session(
                    &provider,
                    perm,
                    "engineer",
                    task,
                    cwd,
                    &mut transcript,
                    &sid,
                ) {
                    tui::line(&tui::red(&format!("  {e}")));
                }
                tui::bell();
                continue;
            }
            "/pr" => {
                tui::line(&tui::accent("  /pr — AI pull request"));
                tui::line(&tui::dim(
                    "  Generates a PR title and description from your branch diff.",
                ));
                let task = "Generate a pull request title and description for the current branch. Run `git log main..HEAD --oneline` and `git diff main...HEAD` (or use origin/main if main isn't local) to understand the changes. Then use `gh pr create` (if gh is available) or just print the title and description so the user can paste it.";
                tui::line("");
                if let Err(e) = agent::run_build_session(
                    &provider,
                    perm,
                    "engineer",
                    task,
                    cwd,
                    &mut transcript,
                    &sid,
                ) {
                    tui::line(&tui::red(&format!("  {e}")));
                }
                tui::bell();
                continue;
            }
            "/workflows" | "/tasks" => {
                handle_workflows();
                continue;
            }
            "/doctor" | "/debug" => {
                handle_doctor_tui();
                continue;
            }
            "/diff" => {
                handle_diff(cwd);
                continue;
            }
            "/context" => {
                handle_context(&transcript, provider.context_tokens);
                continue;
            }
            "/agents" => {
                handle_agents();
                continue;
            }
            "/checkpoints" => {
                handle_checkpoints(cwd);
                continue;
            }
            "/undo" | "/rewind" => {
                handle_undo(cwd, "");
                continue;
            }
            "/grill-me" | "/align" | "/interview" => {
                handle_align(cwd);
                continue;
            }
            "/teamwork" | "/teamwork-preview" | "/swarm" => {
                handle_teamwork();
                continue;
            }
            "/mode" => {
                tui::line(&format!(
                    "  Current mode: {}",
                    tui::mode_badge(mode_label(&mode))
                ));
                tui::line(&tui::dim(
                    "  Tab-complete: /mode plan|build|brainstorm  ·  Shift+Tab to cycle",
                ));
                tui::line("");
                tui::line(&format!("    {}  {}", tui::bold("1"), "plan"));
                tui::line(&format!("    {}  {}", tui::bold("2"), "build"));
                tui::line(&format!("    {}  {}", tui::bold("3"), "brainstorm"));
                tui::line("");
                let pick =
                    tui::ask("  switch to [1/2/3 or name, Enter to keep]: ").unwrap_or_default();
                match pick.trim() {
                    "1" | "plan" => {
                        mode = Mode::Plan;
                        last_suggested_mode = None;
                        tui::show_mode_change("PLAN");
                    }
                    "2" | "build" => {
                        mode = Mode::Build;
                        last_suggested_mode = None;
                        tui::show_mode_change("BUILD");
                    }
                    "3" | "brainstorm" => {
                        mode = Mode::Brainstorm;
                        last_suggested_mode = None;
                        tui::show_mode_change("BRAINSTORM");
                    }
                    _ => {}
                }
                continue;
            }
            "/permissions" => {
                handle_permissions(&mut perm);
                continue;
            }
            "/mouse" => {
                handle_mouse(None);
                continue;
            }
            "/scroll" => {
                handle_mouse(None);
                continue;
            }
            "/config" => {
                handle_config(&provider, perm, cwd);
                continue;
            }
            "/memory" => {
                handle_memory(&provider, perm, cwd, &mut transcript, &sid);
                continue;
            }
            "/skills" => {
                handle_skills();
                continue;
            }
            "/tools" => {
                handle_tools();
                continue;
            }
            "/mcp" => {
                handle_mcp();
                continue;
            }
            "/vim" => {
                let current = tui::toggle_vim_mode();
                tui::line(&format!(
                    "  Vim modal editing mode is now {}",
                    if current {
                        tui::green("ENABLED [Normal/Insert]")
                    } else {
                        tui::yellow("DISABLED [Standard Emacs/Readline]")
                    }
                ));
                continue;
            }
            "/local" => {
                handle_local(&mut provider);
                continue;
            }
            "/rules" => {
                handle_rules(cwd);
                continue;
            }
            "/kb" | "/index" => {
                handle_kb_index(cwd);
                continue;
            }
            "/verify" | "/audit" => {
                handle_verify_audit(cwd);
                continue;
            }
            _ => {}
        }

        if let Some(arg) = t.strip_prefix("/voice") {
            if let Some(voice_text) = handle_voice(arg) {
                if !voice_text.trim().is_empty() {
                    tui::line(&format!(
                        "  {} {}",
                        tui::green("✓ Voice input transcribed:"),
                        tui::bold(&voice_text)
                    ));
                    task = voice_text;
                    t = task.trim();
                } else {
                    continue;
                }
            } else {
                continue;
            }
        }

        if let Some(arg) = t
            .strip_prefix("/undo ")
            .or_else(|| t.strip_prefix("/rewind "))
        {
            handle_undo(cwd, arg);
            continue;
        }

        if let Some(id) = t.strip_prefix("/trace ") {
            match id.trim().parse::<u64>() {
                Ok(id) => trace::render_detail(id),
                Err(_) => tui::line(&tui::red("  usage: /trace <id>")),
            }
            continue;
        }

        // Check for custom user-defined slash commands.
        if t.starts_with('/') {
            let mut words = t.trim_start_matches('/').splitn(2, char::is_whitespace);
            let cmd_name = words.next().unwrap_or("");
            let cmd_args = words.next().unwrap_or("").trim();
            if let Some(custom) = find_custom_command(cmd_name) {
                if let Some(script) = custom.script {
                    // Shell-quote the script path to guard against spaces (UX-007).
                    let escaped = script.to_string_lossy().replace('\'', "'\"'\"'");
                    let shell_cmd = if cmd_args.is_empty() {
                        format!("'{escaped}'")
                    } else {
                        format!("'{escaped}' {cmd_args}")
                    };
                    let tool_input = serde_json::json!({"command": shell_cmd});
                    // UX-002: script-based custom commands must pass through the
                    // permission gate and PreToolUse hooks just like any run_command.
                    if let hooks::PreDecision::Deny(r) =
                        hooks::pre_tool_use("run_command", &tool_input, cwd)
                    {
                        tui::line(&tui::red(&format!("  blocked by hook: {r}")));
                        tui::bell();
                        continue;
                    }
                    if let Some(reason) = agent::gate(perm, "run_command", &tool_input, cwd) {
                        tui::line(&tui::red(&format!("  {reason}")));
                        tui::bell();
                        continue;
                    }
                    let out = tools::run("run_command", &tool_input, cwd);
                    for l in out.content.lines() {
                        tui::line(&format!("  {l}"));
                    }
                } else {
                    // Inject the skill content as context and run in BUILD mode.
                    let user_input = if cmd_args.is_empty() {
                        t.to_string()
                    } else {
                        format!("{t} {cmd_args}")
                    };
                    let task_with_context =
                        format!("{user_input}\n\n[Skill: {cmd_name}]\n{}", custom.content);
                    tui::line("");
                    if let Err(e) = agent::run_build_session(
                        &provider,
                        perm,
                        "engineer",
                        &task_with_context,
                        cwd,
                        &mut transcript,
                        &sid,
                    ) {
                        tui::line(&tui::red(&format!("  {e}")));
                    }
                }
                tui::bell();
                continue;
            }
            // UX-001: unknown slash command — show error instead of falling through to AI.
            if !cmd_name.is_empty() {
                tui::line(&tui::red(&format!(
                    "  unknown command /{cmd_name} — /help for all commands"
                )));
                continue;
            }
        }

        // Natural-language mode/permission switch: "switch to build mode", "use readonly", etc.
        if let Some(new_mode) = detect_mode_switch(t) {
            mode = new_mode;
            last_suggested_mode = None;
            tui::show_mode_change(mode_label(&mode));
            continue;
        }
        if let Some(new_perm) = detect_permission_switch(t) {
            apply_permission(&mut perm, new_perm);
            continue;
        }

        // Mode routing: auto-switch out of BRAINSTORM when the task clearly
        // demands real work (chat mode can't fulfill "build X"); elsewhere only
        // hint, and stay quiet for greetings and ordinary questions.
        if should_answer_conversationally(t, &mode) {
            last_suggested_mode = None;
        } else if let Some(new_mode) = auto_switch_mode(t, &mode) {
            mode = new_mode;
            last_suggested_mode = None;
            tui::line(&tui::dim(&format!(
                "  auto-switched to {} for this task — /mode to switch back",
                mode_label(&mode)
            )));
            tui::show_mode_change(mode_label(&mode));
        } else {
            suggest_mode_if_mismatch(t, &mode, &mut last_suggested_mode);
        }

        // Extract @path tokens. Images become multimodal attachments; text files
        // are appended into the prompt with optional @file:start-end ranges.
        // its own Msg::User push and uses this multimodal turn instead.
        let vision = media::model_supports_vision(&provider);
        let (clean_task, image_data) = extract_attachments(t, cwd, vision);
        let n_images = image_data.len();
        if n_images > 0 {
            transcript.push(Msg::UserImages {
                text: clean_task.clone(),
                images: image_data,
            });
            tui::line(&tui::dim(&format!(
                "  ⎘ attached {n_images} image{}",
                if n_images == 1 { "" } else { "s" }
            )));
        }

        // Merge any /btw context queued since the last turn.
        let effective_task = if let Some(ctx) = btw_ctx.take() {
            format!("{}\n\n[btw: {}]", clean_task, ctx)
        } else {
            clean_task.clone()
        };
        let t = effective_task.as_str();

        tui::line("");
        let r = if should_answer_conversationally(t, &mode) {
            agent::run_chat_turn(&provider, perm, cwd, t)
        } else {
            match &mode {
                Mode::Plan => agent::run_plan(&provider, perm, t, cwd),
                Mode::Build => agent::run_build_session(
                    &provider,
                    perm,
                    "engineer",
                    t,
                    cwd,
                    &mut transcript,
                    &sid,
                ),
                Mode::Brainstorm => match agent::run_brainstorm(&provider, perm, cwd, t) {
                    Err(e) => Err(e),
                    Ok(None) => Ok(()),
                    Ok(Some(agent::ModeHint::Build)) => {
                        mode = Mode::Build;
                        tui::show_mode_change("BUILD");
                        Ok(())
                    }
                    Ok(Some(agent::ModeHint::Plan)) => {
                        mode = Mode::Plan;
                        tui::show_mode_change("PLAN");
                        Ok(())
                    }
                    Ok(Some(agent::ModeHint::CycleMode)) => {
                        mode = mode.next();
                        tui::show_mode_change(mode_label(&mode));
                        Ok(())
                    }
                },
            }
        };
        if let Err(e) = r {
            tui::line(&tui::red(&format!("  {e}")));
        }
        tui::bell();
    }
}

fn mode_label(mode: &Mode) -> &'static str {
    match mode {
        Mode::Plan => "PLAN",
        Mode::Build => "BUILD",
        Mode::Brainstorm => "BRAINSTORM",
    }
}

// Auto-switch when the task phrasing clearly demands a different mode.
// Conservative matrix: only ever escalates out of BRAINSTORM — a chat mode
// can't fulfill a build/plan request. A deliberate PLAN gate is never bypassed
// silently; build-shaped tasks there still get the tip below.
fn auto_switch_mode(task: &str, current: &Mode) -> Option<Mode> {
    let target = classify(task);
    match (&target, current) {
        (Mode::Build, Mode::Brainstorm) | (Mode::Plan, Mode::Brainstorm) => Some(target),
        _ => None,
    }
}

// Suggest switching modes when the task phrasing strongly implies a different mode.
// Suppresses the tip if it was already shown for this mode combo in the current session.
fn suggest_mode_if_mismatch(task: &str, current: &Mode, last_suggested: &mut Option<&'static str>) {
    let suggested = classify(task);
    let mismatch = matches!((&suggested, current), (Mode::Build, Mode::Plan));
    if mismatch {
        let sug_label = mode_label(&suggested);
        if *last_suggested != Some(sug_label) {
            tui::line(&tui::dim(&format!(
                "  tip: this looks like a {} task — Shift+Tab or /mode to switch",
                sug_label
            )));
            *last_suggested = Some(sug_label);
        }
    } else {
        *last_suggested = None;
    }
}

fn should_answer_conversationally(task: &str, current: &Mode) -> bool {
    if matches!(current, Mode::Brainstorm) {
        return false;
    }

    if is_simple_conversation(task) {
        return true;
    }

    matches!(classify(task), Mode::Brainstorm) && !looks_like_action_request(task)
}

fn is_simple_conversation(task: &str) -> bool {
    let normalized = task
        .trim()
        .trim_matches(|c: char| c.is_ascii_punctuation() || c.is_whitespace())
        .to_ascii_lowercase();
    matches!(
        normalized.as_str(),
        "hi" | "hello"
            | "hey"
            | "yo"
            | "sup"
            | "thanks"
            | "thank you"
            | "ok"
            | "okay"
            | "cool"
            | "nice"
            | "what can you do"
            | "what can you do?"
            | "who are you"
            | "who are you?"
            | "help"
    )
}

fn looks_like_action_request(task: &str) -> bool {
    let l = task.to_ascii_lowercase();
    let action_words = [
        "build",
        "create",
        "add",
        "fix",
        "implement",
        "write",
        "refactor",
        "run",
        "make",
        "edit",
        "change",
        "update",
        "delete",
        "remove",
        "start",
        "launch",
        "open",
        "find",
        "search",
        "read",
        "inspect",
        "list",
    ];
    action_words.iter().any(|word| {
        l == *word
            || l.starts_with(&format!("{word} "))
            || l.contains(&format!(" {word} "))
            || l.contains(&format!(" {word} me "))
    })
}

fn handle_resume(transcript: &mut Vec<provider::Msg>, sid: &mut String) {
    let mut sessions = session::list();
    if sessions.is_empty() {
        tui::line(&tui::dim("  no saved sessions yet"));
        return;
    }
    tui::line(&tui::dim("  recent sessions:"));
    for (i, s) in sessions.iter().take(15).enumerate() {
        tui::line(&format!(
            "  {}  {}",
            tui::bold(&(i + 1).to_string()),
            s.title
        ));
    }
    let pick = tui::ask(&tui::dim("  resume # (Enter to cancel): "))
        .as_deref()
        .map(str::trim)
        .and_then(|x| x.parse::<usize>().ok());
    if let Some(n) = pick {
        if n >= 1 && n <= sessions.len().min(15) {
            let s = sessions.swap_remove(n - 1);
            tui::line(&tui::green(&format!("  ✓ resumed: {}", s.title)));
            *transcript = s.msgs;
            *sid = s.id;
        }
    }
}

fn handle_config(provider: &Provider, perm: Permission, cwd: &std::path::Path) {
    tui::line(&tui::accent("  /config — AI-assisted configuration"));
    tui::line(&tui::dim(
        "  Tell me what to configure (hooks, memory, custom commands, settings…)",
    ));
    tui::line(&tui::dim(
        "  Examples: 'add a hook to log every command run'",
    ));
    tui::line(&tui::dim(
        "            'remember I prefer TypeScript over JavaScript'",
    ));
    tui::line(&tui::dim("            'create a /deploy slash command'"));
    tui::line("");

    let input = match tui::ask(&format!("  {} ", tui::accent(""))) {
        None => return,
        Some(s) => s,
    };
    let t = input.trim();
    if t.is_empty() {
        return;
    }

    // Show current config context to the model.
    let home_dir = config::home();
    let settings_json = std::fs::read_to_string(home_dir.join("settings.json")).unwrap_or_default();
    let memory_md = config::load_memory().unwrap_or_default();

    let context = format!(
        "The user wants to configure buildwithnexus. Their current settings.json:\n```json\n{settings_json}\n```\n\
        Their current memory.md:\n```markdown\n{memory_md}\n```\n\
        Home directory: {home}\n\
        User request: {t}",
        home = home_dir.display()
    );

    let full_task = format!(
        "Help configure buildwithnexus based on this request. You can:\n\
        - Write to ~/.buildwithnexus/settings.json to add/edit hooks\n\
        - Write to ~/.buildwithnexus/memory.md to add memory\n\
        - Create files in ~/.buildwithnexus/commands/ for custom slash commands\n\
        - Create files in ~/.buildwithnexus/skills/ for skills\n\
        - Create files in ~/.buildwithnexus/hooks/<Event>/ for auto-discovered hook scripts\n\n\
        {context}"
    );

    tui::line("");
    if let Err(e) = agent::run_build(provider, perm, "engineer", &full_task, cwd) {
        tui::line(&tui::red(&format!("  {e}")));
    }
}

fn handle_memory(
    provider: &Provider,
    perm: Permission,
    cwd: &std::path::Path,
    transcript: &mut Vec<provider::Msg>,
    sid: &str,
) {
    tui::line(&tui::accent("  session memory"));
    match config::load_memory() {
        None => tui::line(&tui::dim("  no memory saved yet")),
        Some(mem) => {
            // memory.md is Markdown — render headings/bullets/emphasis.
            tui::line(&tui::render_md(&mem));
        }
    }
    tui::line("");
    tui::line(&tui::dim(
        "  [a] add entry  [c] clear  [e] edit via AI  [Enter] dismiss",
    ));
    let pick = tui::ask(&tui::dim("  action › ")).unwrap_or_default();
    match pick.trim() {
        "a" => {
            if let Some(entry) = tui::ask("  note to save: ") {
                if !entry.trim().is_empty() {
                    config::append_memory(entry.trim());
                    tui::line(&tui::green("  ✓ saved"));
                }
            }
        }
        "c" => {
            config::save_memory("");
            tui::line(&tui::yellow("  memory cleared"));
        }
        "e" => {
            let task = "Review and clean up the memory.md file at ~/.buildwithnexus/memory.md. \
                Remove duplicates, organize by topic, and keep it concise.";
            if let Err(e) =
                agent::run_build_session(provider, perm, "engineer", task, cwd, transcript, sid)
            {
                tui::line(&tui::red(&format!("  {e}")));
            }
        }
        _ => {}
    }
}

fn handle_skills() {
    let skills = config::load_skills();
    if skills.is_empty() {
        tui::line(&tui::dim("  No skills found."));
        tui::line(&tui::dim(&format!(
            "  Add .md files to {}/skills/",
            config::home().display()
        )));
        return;
    }
    let mut items: Vec<(String, String)> = skills
        .into_iter()
        .map(|(name, content)| (format!("/{name}"), content))
        .collect();
    for cmd in config::load_custom_commands()
        .into_iter()
        .filter(|c| c.script.is_some())
    {
        items.push((
            format!("/{}", cmd.name),
            "[script command] runs through the run_command permission gate and hooks.".to_string(),
        ));
    }
    items.sort_by(|a, b| a.0.cmp(&b.0));
    tui::browse_items("skills", &items);
}

fn handle_tools() {
    let mut items: Vec<(String, String)> = tools::defs(true)
        .into_iter()
        .map(|d| {
            let schema =
                serde_json::to_string_pretty(&d.schema).unwrap_or_else(|_| d.schema.to_string());
            (
                d.name.to_string(),
                format!("{}\n\nSchema:\n{schema}", d.description),
            )
        })
        .collect();
    items.sort_by(|a, b| a.0.cmp(&b.0));
    tui::browse_items("tools", &items);
}

fn handle_mcp() {
    let mut items = Vec::new();
    if let Some(s) = config::load_settings() {
        for (name, val) in &s.mcp_servers {
            let desc = serde_json::to_string_pretty(val).unwrap_or_else(|_| val.to_string());
            items.push((name.clone(), format!("MCP Server Configuration:\n{desc}")));
        }
    }
    if items.is_empty() {
        tui::line(&tui::dim(
            "  No MCP servers configured in settings.json (mcp_servers).",
        ));
        tui::line(&tui::dim(
            "  Add servers to settings.json to enable enterprise tool dispatch via `mcp_call`.",
        ));
        return;
    }
    items.sort_by(|a, b| a.0.cmp(&b.0));
    tui::browse_items("mcp servers", &items);
}

fn find_custom_command(name: &str) -> Option<config::CustomCommand> {
    config::load_custom_commands()
        .into_iter()
        .find(|c| c.name == name)
}

fn handle_model(provider: &mut Provider) {
    tui::line(&tui::accent(
        "  /model — interactive model selection & hot-swap",
    ));
    tui::line(&format!(
        "  Current active model: {}",
        tui::bold(&provider.model)
    ));
    tui::line("");

    let mut options: Vec<(String, String)> = Vec::new();

    // 1. Scan for local GGUF models
    let mut gguf_found = false;
    for name in crate::local::scan_gguf() {
        if !gguf_found {
            tui::line(&tui::bold("  [Local GGUF Models Found]"));
            gguf_found = true;
        }
        let model_id = format!("local/{}", name);
        options.push((model_id.clone(), "Local GGUF Model".to_string()));
    }
    if !gguf_found {
        tui::line(&tui::dim(
            "  [No local GGUF models found in ~/.buildwithnexus/models or scanned dirs]",
        ));
    }

    tui::line("");
    tui::line(&tui::bold("  [Standard Cloud & Server Presets]"));
    let presets = [
        (
            "claude-3-7-sonnet",
            "Anthropic Claude 3.7 Sonnet (Reasoning)",
        ),
        (
            "claude-3-5-sonnet",
            "Anthropic Claude 3.5 Sonnet (Balanced)",
        ),
        ("claude-3-haiku", "Anthropic Claude 3 Haiku (Fast/Light)"),
        ("gpt-4o", "OpenAI GPT-4o (Multimodal Flagship)"),
        ("gpt-4o-mini", "OpenAI GPT-4o Mini (Fast/Economic)"),
        ("gemini-2.5-pro", "Google Gemini 2.5 Pro (Long Context)"),
        (
            "gemini-2.5-flash",
            "Google Gemini 2.5 Flash (Fast/High Volume)",
        ),
        ("ollama/llama3", "Local Ollama Llama 3"),
        ("ollama/qwen2.5-coder", "Local Ollama Qwen 2.5 Coder"),
    ];
    for (id, desc) in presets {
        options.push((id.to_string(), desc.to_string()));
    }

    for (idx, (id, desc)) in options.iter().enumerate() {
        let num = format!("{:>2}", idx + 1);
        tui::line(&format!(
            "  {} {}{}",
            tui::accent(&num),
            tui::bold(id),
            tui::dim(desc)
        ));
    }
    tui::line("");
    tui::line(&tui::dim(
        "  Tip: Type a number (e.g. `1`), a model name, or press Enter to keep current.",
    ));

    let pick = tui::ask("  Select model: ").unwrap_or_default();
    let pick = pick.trim();
    if !pick.is_empty() {
        let chosen = if let Ok(idx) = pick.parse::<usize>() {
            if idx > 0 && idx <= options.len() {
                options[idx - 1].0.clone()
            } else {
                tui::line(&tui::red(&format!(
                    "  ✗ number {idx} out of range (1-{}), keeping current model",
                    options.len()
                )));
                return;
            }
        } else {
            pick.to_string()
        };
        provider.model = chosen.clone();
        if let Some(mut s) = config::load_settings() {
            s.model = chosen.clone();
            config::save_settings(&s);
        }
        tui::line(&tui::green(&format!(
            "  ✓ active model hot-swapped → {chosen}"
        )));
    }
}

fn handle_voice(arg: &str) -> Option<String> {
    tui::line(&tui::accent("  voice input"));
    tui::line(&tui::dim(
        "  supported backends: whisper-cpp, whisper-cli, openai-whisper, local models",
    ));
    let audio_path = if arg.trim().is_empty() {
        tui::line(&tui::dim(
            "  Tip: You can drop an audio file (.wav/.mp3/.m4a) directly or pass `/voice <path>`",
        ));
        tui::ask("  path to audio file (or press Enter to check local microphone/whisper): ")
            .unwrap_or_default()
    } else {
        arg.trim().to_string()
    };
    if audio_path.trim().is_empty() {
        let has_whisper = std::process::Command::new("whisper-cpp")
            .arg("--help")
            .output()
            .is_ok()
            || std::process::Command::new("whisper-cli")
                .arg("--help")
                .output()
                .is_ok()
            || std::process::Command::new("whisper")
                .arg("--help")
                .output()
                .is_ok();
        if has_whisper {
            tui::line(&tui::green(
                "  Local whisper binary detected! Ready for voice-to-text transcription.",
            ));
            tui::line(&tui::dim(
                "  To transcribe and run a prompt, use `/voice <path_to_audio_file>`",
            ));
        } else {
            tui::line(&tui::yellow("  No local whisper binary found in PATH."));
            tui::line(&tui::dim("  To enable offline zero-latency voice input, install `whisper-cpp` or `openai-whisper`."));
        }
        None
    } else {
        let path = audio_path.trim();
        if std::path::Path::new(path).exists() {
            tui::line(&format!("  Transcribing audio from {}...", tui::bold(path)));
            let bins = ["whisper-cpp", "whisper-cli", "whisper"];
            for bin in bins {
                if let Ok(_o) = std::process::Command::new(bin)
                    .args(["-f", path, "-otxt"])
                    .output()
                {
                    tui::line(&tui::green(&format!("  ✓ transcribed via {bin}")));
                    let txt_path = format!("{path}.txt");
                    if let Ok(txt) = std::fs::read_to_string(&txt_path) {
                        let _ = std::fs::remove_file(&txt_path);
                        return Some(txt.trim().to_string());
                    }
                    if let Ok(txt) = std::fs::read_to_string(
                        path.replace(".wav", ".txt").replace(".mp3", ".txt"),
                    ) {
                        return Some(txt.trim().to_string());
                    }
                }
            }
            tui::line(&tui::yellow("  Could not transcribe: please ensure `whisper-cpp`, `whisper-cli`, or `whisper` is installed and the audio format is supported."));
            None
        } else {
            tui::line(&tui::red(&format!("  File not found: {path}")));
            None
        }
    }
}

fn handle_local(_provider: &mut Provider) {
    tui::line(&tui::accent("  local models"));
    tui::line(&tui::dim("  scanning local servers and model directories…"));
    let mut servers = Vec::new();
    if let Ok(o) = std::process::Command::new("curl")
        .args(["-s", "http://localhost:11434/api/tags"])
        .output()
    {
        if o.status.success() {
            servers.push("Ollama (port 11434 - ACTIVE)");
        }
    }
    if let Ok(o) = std::process::Command::new("curl")
        .args(["-s", "http://localhost:8080/v1/models"])
        .output()
    {
        if o.status.success() {
            servers.push("llama.cpp / vLLM (port 8080 - ACTIVE)");
        }
    }
    if servers.is_empty() {
        tui::line(&tui::dim("  No running local model servers detected on port 11434 (Ollama) or 8080 (llama.cpp/vLLM)."));
    } else {
        for s in servers {
            tui::line(&format!("{}", tui::green(s)));
        }
    }
    let ggufs = crate::local::scan_gguf();
    if !ggufs.is_empty() {
        tui::line("  Local GGUF models found:");
        for m in ggufs {
            tui::line(&format!("    - {}", tui::bold(&m)));
        }
    }
    tui::line(&tui::dim("  Tip: Use `/model ollama/llama3` or `/model local/qwen2.5-coder` to switch inference to local models."));
}

fn handle_rules(cwd: &std::path::Path) {
    tui::line(&tui::accent(
        "  /rules — active engineering constraints & business logic rules",
    ));
    let mut engine = crate::rules::RuleEngine::load_defaults();
    let rules_dir = cwd.join(".buildwithnexus").join("rules");
    if let Ok(rd) = std::fs::read_dir(&rules_dir) {
        for e in rd.flatten() {
            if let Ok(loaded) =
                crate::rules::RuleEngine::load_from_file(&e.path().to_string_lossy())
            {
                for r in loaded.rules {
                    engine.add_rule(r);
                }
            }
        }
    }
    tui::line(&format!(
        "  {} active rules loaded for workspace:",
        tui::bold(&engine.rules.len().to_string())
    ));
    for r in &engine.rules {
        let sev_badge = match r.severity {
            crate::rules::Severity::Critical => tui::red("CRITICAL"),
            crate::rules::Severity::High => tui::red("HIGH"),
            crate::rules::Severity::Medium => tui::yellow("MEDIUM"),
            crate::rules::Severity::Low | crate::rules::Severity::Info => tui::dim("INFO/LOW"),
        };
        tui::line(&format!(
            "  [{sev_badge}] {}{}",
            tui::bold(&r.id),
            r.description
        ));
    }
    tui::line(&tui::dim("  Tip: Add custom JSON/YAML rules to `.buildwithnexus/rules/` or use `@rules:<id>` in prompt"));
}

fn handle_kb_index(cwd: &std::path::Path) {
    tui::line(&tui::accent(
        "  /kb (/index) — project structured knowledge base & symbol indexing",
    ));
    let mut kb = crate::knowledge::KnowledgeBase::new(&cwd.to_string_lossy());
    tui::line(&format!(
        "  Current knowledge base contains {} entities.",
        tui::bold(&kb.entities.len().to_string())
    ));

    tui::line(&tui::dim(
        "  scanning workspace for source files and symbols…",
    ));
    let mut count = 0;
    let mut dirs_to_visit = vec![cwd.to_path_buf()];
    while let Some(dir) = dirs_to_visit.pop() {
        if let Ok(rd) = std::fs::read_dir(&dir) {
            for entry in rd.flatten() {
                let path = entry.path();
                let name = entry.file_name().to_string_lossy().into_owned();
                if name.starts_with('.')
                    || name == "target"
                    || name == "node_modules"
                    || name == "vendor"
                    || name == "dist"
                {
                    continue;
                }
                if path.is_dir() {
                    dirs_to_visit.push(path);
                } else if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
                    if matches!(ext, "rs" | "js" | "ts" | "py" | "go" | "java" | "c" | "cpp") {
                        let rel_path = path
                            .strip_prefix(cwd)
                            .unwrap_or(&path)
                            .to_string_lossy()
                            .to_string();
                        if let Ok(content) = std::fs::read_to_string(&path) {
                            for line in content.lines() {
                                let trimmed = line.trim();
                                let mut entity_type = None;
                                let mut sym_name = None;
                                if ext == "rs" {
                                    if trimmed.starts_with("fn ")
                                        || trimmed.starts_with("pub fn ")
                                        || trimmed.starts_with("async fn ")
                                        || trimmed.starts_with("pub async fn ")
                                    {
                                        entity_type = Some(crate::knowledge::EntityType::Function);
                                        if let Some(idx) = trimmed.find("fn ") {
                                            let rest = &trimmed[idx + 3..];
                                            if let Some(paren) = rest.find('(') {
                                                sym_name = Some(rest[..paren].trim().to_string());
                                            }
                                        }
                                    } else if trimmed.starts_with("struct ")
                                        || trimmed.starts_with("pub struct ")
                                    {
                                        entity_type = Some(crate::knowledge::EntityType::Class);
                                        if let Some(idx) = trimmed.find("struct ") {
                                            let rest = &trimmed[idx + 7..];
                                            let name_part =
                                                rest.split_whitespace().next().unwrap_or("");
                                            sym_name = Some(
                                                name_part
                                                    .trim_matches(|c| {
                                                        c == '{' || c == '(' || c == ';'
                                                    })
                                                    .to_string(),
                                            );
                                        }
                                    } else if trimmed.starts_with("enum ")
                                        || trimmed.starts_with("pub enum ")
                                    {
                                        entity_type = Some(crate::knowledge::EntityType::Class);
                                        if let Some(idx) = trimmed.find("enum ") {
                                            let rest = &trimmed[idx + 5..];
                                            let name_part =
                                                rest.split_whitespace().next().unwrap_or("");
                                            sym_name = Some(
                                                name_part
                                                    .trim_matches(|c| {
                                                        c == '{' || c == '(' || c == ';'
                                                    })
                                                    .to_string(),
                                            );
                                        }
                                    }
                                } else if ext == "py" {
                                    if let Some(rest) = trimmed.strip_prefix("def ") {
                                        entity_type = Some(crate::knowledge::EntityType::Function);
                                        if let Some(paren) = rest.find('(') {
                                            sym_name = Some(rest[..paren].trim().to_string());
                                        }
                                    } else if let Some(rest) = trimmed.strip_prefix("class ") {
                                        entity_type = Some(crate::knowledge::EntityType::Class);
                                        if let Some(paren) = rest.find(['(', ':']) {
                                            sym_name = Some(rest[..paren].trim().to_string());
                                        }
                                    }
                                } else if matches!(ext, "js" | "ts") {
                                    if trimmed.starts_with("function ")
                                        || trimmed.starts_with("export function ")
                                    {
                                        entity_type = Some(crate::knowledge::EntityType::Function);
                                        if let Some(idx) = trimmed.find("function ") {
                                            let rest = &trimmed[idx + 9..];
                                            if let Some(paren) = rest.find('(') {
                                                sym_name = Some(rest[..paren].trim().to_string());
                                            }
                                        }
                                    } else if trimmed.starts_with("class ")
                                        || trimmed.starts_with("export class ")
                                    {
                                        entity_type = Some(crate::knowledge::EntityType::Class);
                                        if let Some(idx) = trimmed.find("class ") {
                                            let rest = &trimmed[idx + 6..];
                                            let name_part =
                                                rest.split_whitespace().next().unwrap_or("");
                                            sym_name = Some(
                                                name_part.trim_matches(|c| c == '{').to_string(),
                                            );
                                        }
                                    }
                                }
                                if let (Some(et), Some(sn)) = (entity_type, sym_name) {
                                    if !sn.is_empty()
                                        && sn
                                            .chars()
                                            .all(|c| c.is_alphanumeric() || c == '_' || c == '$')
                                    {
                                        let id = format!("{sn}@{rel_path}");
                                        kb.add_entity(crate::knowledge::Entity {
                                            id,
                                            entity_type: et,
                                            name: sn,
                                            path: Some(rel_path.clone()),
                                            description: Some(format!(
                                                "Extracted symbol from {rel_path}"
                                            )),
                                            metadata: serde_json::json!({"auto_indexed": true}),
                                            relationships: vec![],
                                            last_updated: crate::knowledge::chrono_now_iso(),
                                        });
                                        count += 1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if let Err(e) = kb.save() {
        tui::line(&tui::red(&format!("  Failed to save knowledge base: {e}")));
    } else {
        tui::line(&tui::green(&format!("  ✓ indexed {count} symbols")));
        tui::line(&tui::dim(
            "  tip: @kb:<name> or @symbol:<name> injects symbol definitions into prompts",
        ));
    }
}

fn handle_verify_audit(cwd: &std::path::Path) {
    tui::line(&tui::accent(
        "  verifying workspace against rules and tests",
    ));

    let mut changed_files = Vec::new();
    if let Ok(o) = std::process::Command::new("git")
        .args(["status", "-s"])
        .current_dir(cwd)
        .output()
    {
        if o.status.success() {
            let out = String::from_utf8_lossy(&o.stdout);
            for line in out.lines() {
                if line.len() > 3 {
                    let path = line[3..].trim().to_string();
                    changed_files.push(path);
                }
            }
        }
    }
    if changed_files.is_empty() {
        tui::line(&tui::dim(
            "  no modified git files — checking recent files in the workspace…",
        ));
        if let Ok(rd) = std::fs::read_dir(cwd) {
            for e in rd.flatten() {
                if let Some(s) = e.path().to_str() {
                    if !s.contains(".git") && !s.contains("target") && !s.contains("node_modules") {
                        changed_files.push(e.path().to_string_lossy().to_string());
                    }
                }
            }
        }
    }

    let verifier = crate::verifier::Verifier::new(&cwd.to_string_lossy());
    let ctx = crate::verifier::VerificationContext {
        task_description: "Interactive workspace verification and operational audit".to_string(),
        task_type: Some(crate::rules::TaskType::CodeReview),
        changed_files: changed_files.clone(),
        tool_calls: vec![],
        evidence_gathered: vec![],
        tests_added: vec![],
        dependencies_changed: vec![],
        git_diff: None,
    };

    let report = verifier.verify(&ctx);
    // The report is Markdown — render it instead of echoing raw ##/**/`.
    let report_str = crate::verifier::Verifier::format_report(&report);
    tui::line(&tui::render_md(&report_str));
    tui::line(&tui::dim(
        "  tip: @rules:<id> or /rules inspects specific constraints",
    ));
}

fn handle_compact(provider: &Provider, transcript: &mut Vec<provider::Msg>) {
    if transcript.is_empty() {
        tui::line(&tui::dim("  nothing to compact (empty transcript)"));
        return;
    }
    let before = transcript.len();
    let taken = std::mem::take(transcript);
    *transcript = agent::compact_msgs(provider, taken);
    let after = transcript.len();
    tui::line(&tui::green(&format!(
        "  ✓ compacted: {before}{after} messages"
    )));
}

fn handle_workflows() {
    let snaps = workflow::snapshots();
    if snaps.is_empty() {
        tui::line(&tui::dim(
            "  no workflows yet — /schedule or /loop to create one",
        ));
        return;
    }
    tui::line(&tui::accent("  background workflows"));
    tui::line(&tui::rule());
    for s in &snaps {
        let status_color = match s.status_str.as_str() {
            "running" => tui::blue(&s.status_str),
            "done" => tui::green(&s.status_str),
            "failed" => tui::red(&s.status_str),
            _ => tui::dim(&s.status_str),
        };
        let elapsed = s
            .elapsed_secs
            .map(|e| format!(" [{e}s]"))
            .unwrap_or_default();
        let iter_label = if s.iteration > 1 {
            format!(" ×{}", s.iteration)
        } else {
            String::new()
        };
        tui::line(&format!(
            "  #{:<3}  {}{}  [{}]  {}{}",
            s.id,
            status_color,
            elapsed,
            s.kind_str,
            tui::dim(&s.task),
            iter_label
        ));
    }
    tui::line(&tui::rule());
    tui::line(&tui::dim(
        "  c<id> cancel  ·  i<id> inspect output  ·  Enter dismiss",
    ));
    let action = tui::ask("  action: ").unwrap_or_default();
    let action = action.trim();
    if let Some(rest) = action.strip_prefix('c') {
        if let Ok(id) = rest.trim().parse::<usize>() {
            if workflow::cancel(id) {
                tui::line(&tui::yellow(&format!("  cancelled workflow #{id}")));
            } else {
                tui::line(&tui::dim(&format!(
                    "  workflow #{id} not found or already finished"
                )));
            }
        }
    } else if let Some(rest) = action.strip_prefix('i') {
        if let Ok(id) = rest.trim().parse::<usize>() {
            let lines = workflow::output(id);
            if lines.is_empty() {
                tui::line(&tui::dim(&format!(
                    "  no output captured for workflow #{id}"
                )));
            } else {
                tui::line(&tui::accent(&format!("  workflow #{id} output:")));
                for l in lines.iter().take(100) {
                    tui::line(&format!("    {}", tui::dim(l)));
                }
                if lines.len() > 100 {
                    tui::line(&tui::dim(&format!(
                        "  … ({} more lines)",
                        lines.len() - 100
                    )));
                }
            }
        }
    }
}

// Detect intent to switch agent mode from natural language input.
// Only catches unambiguous switch phrases — not ordinary task verbs like "plan this".
fn detect_mode_switch(t: &str) -> Option<Mode> {
    let l = t.trim().to_lowercase();
    let l = l.trim_end_matches(['!', '.', '?']).trim();

    let verb_prefixes: &[&str] = &[
        "switch to ",
        "switch mode to ",
        "change to ",
        "change mode to ",
        "go to ",
        "set mode to ",
        "set mode ",
    ];
    for prefix in verb_prefixes {
        if let Some(rest) = l.strip_prefix(prefix) {
            let rest = rest.trim().trim_end_matches("mode").trim();
            match rest {
                "plan" | "planning" => return Some(Mode::Plan),
                "build" | "building" | "code" => return Some(Mode::Build),
                "brainstorm" | "brainstorming" => return Some(Mode::Brainstorm),
                _ => {}
            }
        }
    }
    // "use X mode" — the word "mode" makes the intent unambiguous.
    if let Some(rest) = l.strip_prefix("use ") {
        if let Some(name) = rest.trim().strip_suffix(" mode") {
            match name.trim() {
                "plan" | "planning" => return Some(Mode::Plan),
                "build" | "building" | "code" => return Some(Mode::Build),
                "brainstorm" | "brainstorming" => return Some(Mode::Brainstorm),
                _ => {}
            }
        }
    }
    // Bare "X mode" when that's the entire input (2 words or fewer).
    if t.split_whitespace().count() <= 2 {
        let bare = l.trim_end_matches("mode").trim();
        match bare {
            "plan" | "planning" => return Some(Mode::Plan),
            "build" | "building" => return Some(Mode::Build),
            "brainstorm" | "brainstorming" => return Some(Mode::Brainstorm),
            _ => {}
        }
    }
    None
}

// Detect intent to switch permission mode from natural language input.
fn detect_permission_switch(t: &str) -> Option<&'static str> {
    let l = t.trim().to_lowercase();
    let l = l.trim_end_matches(['!', '.', '?']).trim();

    let verb_prefixes: &[&str] = &[
        "switch to ",
        "change to ",
        "change permission to ",
        "set permission to ",
        "set permission ",
        "use ",
    ];
    for prefix in verb_prefixes {
        if let Some(rest) = l.strip_prefix(prefix) {
            let rest = rest
                .trim()
                .trim_end_matches("mode")
                .trim()
                .trim_end_matches("permission")
                .trim();
            match rest {
                "ask" | "confirm" => return Some("ask"),
                "auto" | "yolo" | "approve all" => return Some("auto"),
                "readonly" | "read only" | "read-only" | "safe" => return Some("readonly"),
                _ => {}
            }
        }
    }
    // Bare "use readonly", "use ask" — short and unambiguous.
    if t.split_whitespace().count() <= 3 {
        match l.trim() {
            "readonly" | "read-only" | "read only" => return Some("readonly"),
            "auto permission" | "auto mode" => return Some("auto"),
            "ask permission" | "ask mode" => return Some("ask"),
            _ => {}
        }
    }
    None
}

// Apply a permission string, update the in-session value, and persist to settings.json.
fn apply_permission(perm: &mut Permission, ps: &str) {
    *perm = agent::permission(ps);
    if let Some(mut settings) = config::load_settings() {
        settings.permission = ps.to_string();
        config::save_settings(&settings);
    }
    tui::set_permission_mode(permission_label(perm));
    tui::line(&tui::green(&format!("  ✓ permission: {ps}")));
}

fn permission_label(perm: &Permission) -> &'static str {
    match perm {
        Permission::Ask => "ask",
        Permission::Auto => "auto",
        Permission::ReadOnly => "readonly",
    }
}

fn handle_permissions(perm: &mut Permission) {
    let current = permission_label(perm);
    tui::line(&tui::accent("  /permissions — tool permission mode"));
    tui::line(&format!("  Current: {}", tui::bold(current)));
    tui::line(&tui::dim("  Tab-complete: /permissions ask|auto|readonly"));
    tui::line("");
    tui::line(&format!(
        "    {}  {} — confirm before each file write or command  {}",
        tui::bold("1"),
        tui::bold("ask"),
        tui::dim("(recommended)")
    ));
    tui::line(&format!(
        "    {}  {} — auto-approve all actions                   {}",
        tui::bold("2"),
        tui::bold("auto"),
        tui::dim("(yolo)")
    ));
    tui::line(&format!(
        "    {}  {} — never write files or run commands",
        tui::bold("3"),
        tui::bold("readonly")
    ));
    tui::line("");
    let pick = tui::ask("  choice [1/2/3 or name, Enter to keep]: ").unwrap_or_default();
    match pick.trim() {
        "1" | "ask" => apply_permission(perm, "ask"),
        "2" | "auto" => apply_permission(perm, "auto"),
        "3" | "readonly" => apply_permission(perm, "readonly"),
        _ => {}
    }
}

fn handle_mouse(arg: Option<&str>) {
    let cmd = arg.unwrap_or("").trim();
    match cmd {
        "on" | "enable" => {
            tui::set_mouse_capture(true);
            tui::line(&tui::green(
                "  ✓ mouse: on — wheel scroll and drag-to-copy are enabled",
            ));
        }
        "off" | "disable" => {
            tui::set_mouse_capture(false);
            tui::line(&tui::green(
                "  ✓ mouse: off — terminal-native selection restored; use PgUp/PgDn to scroll",
            ));
        }
        "" | "toggle" => {
            let new_state = !tui::mouse_capture_enabled();
            tui::set_mouse_capture(new_state);
            if new_state {
                tui::line(&tui::green(
                    "  ✓ mouse: on — wheel scroll and drag-to-copy are enabled",
                ));
            } else {
                tui::line(&tui::green(
                    "  ✓ mouse: off — terminal-native selection restored; use PgUp/PgDn to scroll",
                ));
            }
        }
        "status" => {
            let state = if tui::mouse_capture_enabled() {
                "on"
            } else {
                "off"
            };
            tui::line(&tui::accent("  /mouse — mouse wheel scrolling"));
            tui::line(&format!("  Current: {}", tui::bold(state)));
            tui::line(&tui::dim(
                "  Default is on: wheel scrolls transcript and drag copies selected transcript text. /mouse off restores terminal-native selection.",
            ));
        }
        other => tui::line(&tui::red(&format!(
            "  unknown mouse setting '{other}' — try: on, off, toggle, status"
        ))),
    }
}

fn handle_diff(cwd: &std::path::Path) {
    let out = tools::run(
        "run_command",
        &serde_json::json!({"command": "git diff --stat && git diff --shortstat"}),
        cwd,
    );
    for line in out.content.lines() {
        tui::line(&tui::dim(&format!("  {line}")));
    }
}

fn msg_token_estimate(msgs: &[provider::Msg]) -> usize {
    let chars: usize = msgs
        .iter()
        .map(|m| match m {
            provider::Msg::System(s) | provider::Msg::User(s) => s.len(),
            provider::Msg::UserImages { text, images } => text.len() + images.len() * 1024,
            provider::Msg::Assistant { text, calls } => {
                text.len()
                    + calls
                        .iter()
                        .map(|c| c.input.to_string().len())
                        .sum::<usize>()
            }
            provider::Msg::Tool(results) => results.iter().map(|r| r.content.len()).sum(),
        })
        .sum();
    chars / 4
}

fn handle_context(transcript: &[provider::Msg], total: usize) {
    let used = msg_token_estimate(transcript);
    tui::context_meter(used, total);
    tui::line(&tui::dim(&format!(
        "  {} messages in session",
        transcript.len()
    )));
}

fn handle_checkpoints(cwd: &std::path::Path) {
    let items = checkpoint::list(cwd);
    if items.is_empty() {
        tui::line(&tui::dim("  no checkpoints for this directory"));
        return;
    }
    for cp in items.iter().take(10) {
        tui::line(&format!(
            "  {}  {}  {}",
            tui::bold(&cp.id),
            cp.action,
            cp.path.display()
        ));
    }
}

fn handle_undo(cwd: &std::path::Path, arg: &str) {
    let arg = arg.trim();
    if arg == "git" {
        match checkpoint::git_rollback(cwd) {
            Ok(msg) => tui::line(&tui::green(&format!("  ✓ git reset: {msg}"))),
            Err(e) => tui::line(&tui::red(&format!("  git reset error: {e}"))),
        }
    } else if arg == "all" || arg == "session" {
        let since = checkpoint::now_ms().saturating_sub(24 * 3600 * 1000);
        match checkpoint::undo_all_since(cwd, since) {
            Ok(cps) => {
                tui::line(&tui::green(&format!(
                    "  ✓ restored {} files across session:",
                    cps.len()
                )));
                for c in cps {
                    tui::line(&format!("    - {} ({})", c.path.display(), c.action));
                }
            }
            Err(e) => tui::line(&tui::red(&format!("  {e}"))),
        }
    } else if !arg.is_empty() {
        match checkpoint::undo_by_id(cwd, arg) {
            Ok(cp) => tui::line(&tui::green(&format!(
                "  ✓ restored checkpoint {} ({})",
                cp.id,
                cp.path.display()
            ))),
            Err(e) => tui::line(&tui::red(&format!("  {e}"))),
        }
    } else {
        match checkpoint::undo_latest(cwd) {
            Ok(cp) => tui::line(&tui::green(&format!(
                "  ✓ restored latest {}",
                cp.path.display()
            ))),
            Err(e) => tui::line(&tui::red(&format!("  {e}"))),
        }
    }
}

fn handle_align(cwd: &std::path::Path) {
    tui::line(&tui::accent("  alignment interview"));
    tui::line(&tui::dim(
        "  a short alignment review before proceeding with complex changes",
    ));

    let q1 = tui::ask("  1. What is the primary operational risk? [1: Regression | 2: Data Loss | 3: Performance | 4: Security]: ").unwrap_or_default();
    let risk_label = match q1.trim() {
        "2" => "Data Loss",
        "3" => "Performance Degradation",
        "4" => "Security Vulnerability",
        _ => "System Regression",
    };

    let q2 = tui::ask("  2. What is the reversibility of this change? [1: Easy (flag/config) | 2: Moderate (revert) | 3: Hard (db/contract) | 4: Irreversible]: ").unwrap_or_default();
    let rev_label = match q2.trim() {
        "1" => "Easy (Feature Flag / Config)",
        "3" => "Hard (Database Migration / API Contract)",
        "4" => "Irreversible",
        _ => "Moderate (Code Revert)",
    };

    let q3 = tui::ask("  3. What is the target confidence threshold? [1: High (>90%) | 2: Medium (>75%) | 3: Exploratory]: ").unwrap_or_default();
    let conf_label = match q3.trim() {
        "1" => "High (>90%)",
        "3" => "Exploratory / Prototype",
        _ => "Medium (>75%)",
    };

    tui::line(&tui::green("  ✓ alignment recorded"));
    tui::line(&format!("    • Primary Risk: {}", tui::bold(risk_label)));
    tui::line(&format!("    • Reversibility: {}", tui::bold(rev_label)));
    tui::line(&format!(
        "    • Confidence Threshold: {}",
        tui::bold(conf_label)
    ));

    let mut kb = crate::knowledge::KnowledgeBase::new(&cwd.to_string_lossy());
    let id = format!("dec-{}", crate::checkpoint::now_ms());
    let entity = crate::knowledge::Entity {
        id: id.clone(),
        entity_type: crate::knowledge::EntityType::ArchitectureDecision,
        name: format!("Operational Alignment ({})", risk_label),
        path: None,
        description: Some(format!(
            "Risk: {}, Reversibility: {}, Confidence: {}",
            risk_label, rev_label, conf_label
        )),
        metadata: serde_json::json!({
            "risk": risk_label,
            "reversibility": rev_label,
            "confidence_target": conf_label,
            "timestamp": crate::checkpoint::now_ms()
        }),
        relationships: vec![],
        last_updated: "now".to_string(),
    };
    kb.add_entity(entity);
    let _ = kb.save();
    tui::line(&tui::dim(
        "  Decision recorded into structured knowledge base (.buildwithnexus/knowledge/).",
    ));
}

fn handle_teamwork() {
    tui::line(&tui::accent("  teamwork — multi-agent swarm preview"));
    tui::line(&tui::dim(
        "  for complex projects, buildwithnexus orchestrates specialized subagent teams:",
    ));
    tui::line(&format!(
        "{} — Explores documentation, code graphs, and symbol trees",
        tui::bold("Researcher Subagent")
    ));
    tui::line(&format!(
        "{} — Analyzes logs, stack traces, and test regressions",
        tui::bold("Debugger Subagent")
    ));
    tui::line(&format!(
        "{} — Edits code files, runs migrations, and applies patches",
        tui::bold("Code Writer Subagent")
    ));
    tui::line(&format!(
        "{} — Checks engineering rules, static analysis, and confidence",
        tui::bold("Verifier Subagent")
    ));
    tui::line(&tui::dim("  Tip: Use `invoke_subagent` in your custom rules/workflows to dispatch tasks to this team."));
}

fn handle_agents() {
    match config::load_agents() {
        Some(agents) => {
            // Agents.md is Markdown — render it instead of dumping #/**/- raw.
            let shown: Vec<&str> = agents.lines().take(80).collect();
            tui::line(&tui::render_md(&shown.join("\n")));
            let total = agents.lines().count();
            if total > 80 {
                tui::line(&tui::dim(&format!("  …(+{} more lines)", total - 80)));
            }
        }
        None => tui::line(&tui::dim("  no Agents.md found")),
    }
}

fn handle_doctor_tui() {
    tui::line(&tui::accent(&format!("  buildwithnexus {VERSION} doctor")));
    match config::load_settings() {
        Some(s) => {
            tui::line(&format!("  provider: {}", s.provider));
            tui::line(&format!("  model: {}", s.model));
            tui::line(&format!("  permission: {}", s.permission));
        }
        None => tui::line(&tui::yellow("  settings: not configured")),
    }
    tui::line(&format!("  home: {}", config::home().display()));
    tui::line(&format!(
        "  rust: {}",
        std::process::Command::new("rustc")
            .arg("--version")
            .output()
            .ok()
            .and_then(|o| String::from_utf8(o.stdout).ok())
            .map(|s| s.trim().to_string())
            .unwrap_or_else(|| "not found".to_string())
    ));
}

fn print_help() {
    // (command, args/aliases hint, description) grouped by section. Rendered
    // as an auto-aligned table so alignment can't drift as commands change.
    type Row = (&'static str, &'static str, &'static str);
    let sections: &[(&str, &[Row])] = &[
        (
            "modes",
            &[
                ("Shift+Tab", "", "cycle PLAN → BUILD → BRAINSTORM"),
                ("/mode", "[plan|build|brainstorm]", "show or switch mode"),
                (
                    "/permissions",
                    "[ask|auto|readonly]",
                    "tool permission level",
                ),
                ("/model", "[name]", "hot-swap the AI model mid-session"),
                ("/local", "", "local model server & GGUF/Ollama management"),
            ],
        ),
        (
            "context & git",
            &[
                ("/compact", "", "compress context to free token budget"),
                ("/context", "", "show context window usage"),
                ("/diff", "", "show current git diff summary"),
                ("/review", "", "AI code review of staged git diff"),
                ("/commit", "", "AI-drafted conventional commit message"),
                ("/pr", "", "AI-drafted PR title + description"),
                ("/checkpoints", "", "list edit checkpoints"),
                ("/undo", "(/rewind) <git|all|id>", "restore a checkpoint"),
            ],
        ),
        (
            "automation",
            &[
                ("/schedule", "<delay> <task>", "one-shot scheduled workflow"),
                ("/loop", "<interval> <task>", "repeating scheduled workflow"),
                ("/workflows", "(/tasks)", "list background workflows"),
                ("/btw", "<context>", "inject context into next agent turn"),
                ("/teamwork", "(/swarm)", "multi-agent swarm preview"),
                ("/grill-me", "(/align)", "operational alignment interview"),
            ],
        ),
        (
            "project",
            &[
                ("/memory", "", "view and edit session memory"),
                ("/skills", "", "list skills and custom commands"),
                ("/tools", "", "browse callable tools"),
                ("/rules", "", "inspect engineering rules and violations"),
                ("/kb", "(/index)", "query or index project knowledge base"),
                (
                    "/verify",
                    "(/audit)",
                    "verify codebase against rules and tests",
                ),
                ("/agents", "", "show loaded Agents.md context"),
                ("/mcp", "", "inspect configured MCP servers"),
                ("/trace", "", "inspect hooks, tools, skills, subagents"),
            ],
        ),
        (
            "session",
            &[
                ("/new", "", "start a fresh session"),
                ("/resume", "", "pick a saved session to resume"),
                ("/init", "", "run setup (keys, providers, local models)"),
                ("/config", "", "configure hooks, memory, commands via AI"),
                ("/voice", "[<file>]", "audio transcription & voice input"),
                ("/vim", "", "toggle Vim modal editing"),
                ("/mouse", "[on|off]", "wheel scroll + drag-copy (/scroll)"),
                ("/doctor", "(/debug)", "diagnose setup"),
                ("/clear", "", "clear the screen"),
                ("/exit", "", "exit"),
            ],
        ),
    ];

    let cmd_w = sections
        .iter()
        .flat_map(|(_, rows)| rows.iter())
        .map(|(cmd, _, _)| cmd.chars().count())
        .max()
        .unwrap_or(0);

    tui::line("");
    tui::line(&tui::bold(&tui::accent("  commands")));
    for (title, rows) in sections {
        tui::line("");
        tui::line(&tui::dim(&format!("  {title}")));
        for (cmd, args, desc) in rows.iter() {
            let pad = " ".repeat(cmd_w.saturating_sub(cmd.chars().count()));
            let args_part = if args.is_empty() {
                String::new()
            } else {
                format!("  {}", tui::dim(args))
            };
            tui::line(&format!("    {}{pad}  {desc}{args_part}", tui::bold(cmd)));
        }
    }
    tui::line("");
    tui::line(&tui::dim("  input"));
    tui::line(&tui::dim(
        "    !<cmd> shell command · @<path> attach file/image/video · @diff @kb: @symbol:",
    ));
    tui::line(&tui::dim(
        "    ^V paste image/text · Tab complete · ↑↓ history · ^R search · ^G $EDITOR",
    ));
    tui::line(&tui::dim(
        "    ←→ ^A ^E move · ^W ^U ^K kill · ^Y yank · PgUp/PgDn scroll",
    ));
    tui::line("");
}

// ── Mode ──────────────────────────────────────────────────────────────────────
pub enum Mode {
    Plan,
    Build,
    Brainstorm,
}

impl Mode {
    // Shift+Tab cycles PLAN → BUILD → BRAINSTORM → PLAN.
    pub fn next(&self) -> Mode {
        match self {
            Mode::Plan => Mode::Build,
            Mode::Build => Mode::Brainstorm,
            Mode::Brainstorm => Mode::Plan,
        }
    }
}

// Parse `@path` attachment tokens. Images become multimodal entries; videos
// are parsed with ffmpeg into sampled frames + a metadata block; readable
// text files are appended to the prompt. Unreadable tokens are left
// unchanged. `vision` gates image/video attachment to multimodal models.
fn extract_attachments(
    task: &str,
    cwd: &std::path::Path,
    vision: bool,
) -> (String, Vec<(String, String)>) {
    use std::io::Read;
    let image_exts = ["png", "jpg", "jpeg", "gif", "webp"];
    let mut images: Vec<(String, String)> = Vec::new();
    let mut clean = String::new();
    let mut text_attachments = Vec::new();
    let words: Vec<String> = shlex::split(task)
        .unwrap_or_else(|| task.split_whitespace().map(|s| s.to_string()).collect());
    for word_str in &words {
        let word = word_str.as_str();
        let is_at = word.starts_with('@');
        let clean_word = word.trim_matches(|c| {
            c == '\'' || c == '"' || c == ',' || c == ';' || c == '(' || c == ')' || c == '`'
        });
        let ext = clean_word.rsplit('.').next().unwrap_or("").to_lowercase();
        let is_img = image_exts.contains(&ext.as_str());
        let is_video = media::VIDEO_EXTS.contains(&ext.as_str());
        if !is_at && !is_img && !is_video {
            if !clean.is_empty() {
                clean.push(' ');
            }
            clean.push_str(word);
            continue;
        }
        if let Some(raw_path) = if is_at {
            word.strip_prefix('@')
        } else {
            Some(clean_word)
        } {
            if raw_path == "diff" || raw_path == "git:diff" {
                if let Ok(o) = std::process::Command::new("git")
                    .args(["diff", "HEAD"])
                    .current_dir(cwd)
                    .output()
                {
                    let diff_text = String::from_utf8_lossy(&o.stdout);
                    if !diff_text.trim().is_empty() {
                        text_attachments.push(format!("[git diff HEAD]\n{}", diff_text));
                        if !clean.is_empty() {
                            clean.push(' ');
                        }
                        clean.push_str("[git diff HEAD]");
                        continue;
                    }
                }
            } else if raw_path == "status" || raw_path == "git:status" {
                if let Ok(o) = std::process::Command::new("git")
                    .args(["status", "-s"])
                    .current_dir(cwd)
                    .output()
                {
                    let stat_text = String::from_utf8_lossy(&o.stdout);
                    if !stat_text.trim().is_empty() {
                        text_attachments.push(format!("[git status]\n{}", stat_text));
                        if !clean.is_empty() {
                            clean.push(' ');
                        }
                        clean.push_str("[git status]");
                        continue;
                    }
                }
            } else if let Some(kb_query) = raw_path.strip_prefix("kb:") {
                let kb = crate::knowledge::KnowledgeBase::new(&cwd.to_string_lossy());
                let res = kb.search(kb_query);
                if !res.is_empty() {
                    let summary = res
                        .iter()
                        .map(|e| {
                            format!(
                                "Entity: {} ({:?})\nDescription: {}\nPath: {:?}",
                                e.name,
                                e.entity_type,
                                e.description.as_deref().unwrap_or(""),
                                e.path
                            )
                        })
                        .collect::<Vec<_>>()
                        .join("\n---\n");
                    text_attachments.push(format!("[knowledge base: {}]\n{}", kb_query, summary));
                    if !clean.is_empty() {
                        clean.push(' ');
                    }
                    clean.push_str(&format!("[kb: {}]", kb_query));
                    continue;
                }
            } else if raw_path == "rules" || raw_path.starts_with("rule:") {
                let engine = crate::rules::RuleEngine::load_defaults();
                let rules_summary = engine
                    .rules
                    .iter()
                    .map(|r| {
                        format!(
                            "Rule [{}]: {} (Severity: {})",
                            r.id, r.description, r.severity
                        )
                    })
                    .collect::<Vec<_>>()
                    .join("\n");
                text_attachments.push(format!("[active engineering rules]\n{}", rules_summary));
                if !clean.is_empty() {
                    clean.push(' ');
                }
                clean.push_str("[active rules]");
                continue;
            } else if let Some(url) = raw_path
                .strip_prefix("url:")
                .or_else(|| raw_path.strip_prefix("web:"))
            {
                if let Ok(o) = std::process::Command::new("curl")
                    .args(["-sL", "--max-time", "5", url])
                    .output()
                {
                    let web_text = String::from_utf8_lossy(&o.stdout);
                    if !web_text.trim().is_empty() {
                        let snippet: String = web_text.chars().take(8000).collect();
                        text_attachments.push(format!("[web: {}]\n{}", url, snippet));
                        if !clean.is_empty() {
                            clean.push(' ');
                        }
                        clean.push_str(&format!("[web: {}]", url));
                        continue;
                    }
                }
            } else if let Some(sym_query) = raw_path.strip_prefix("symbol:") {
                if let Ok(o) = std::process::Command::new("grep")
                    .args(["-rnI", sym_query, "."])
                    .current_dir(cwd)
                    .output()
                {
                    let sym_text = String::from_utf8_lossy(&o.stdout);
                    if !sym_text.trim().is_empty() {
                        let snippet: String =
                            sym_text.lines().take(30).collect::<Vec<_>>().join("\n");
                        text_attachments
                            .push(format!("[symbol search: {}]\n{}", sym_query, snippet));
                        if !clean.is_empty() {
                            clean.push(' ');
                        }
                        clean.push_str(&format!("[symbol: {}]", sym_query));
                        continue;
                    }
                }
            }
            let (raw_path, range) = split_attachment_range(raw_path);
            let ext = raw_path.rsplit('.').next().unwrap_or("").to_lowercase();
            let p = if let Some(rest) = raw_path.strip_prefix("~/") {
                std::env::var_os("HOME")
                    .map(PathBuf::from)
                    .unwrap_or_else(|| cwd.to_path_buf())
                    .join(rest)
            } else if raw_path.starts_with('/') {
                PathBuf::from(raw_path)
            } else {
                cwd.join(raw_path)
            };
            if image_exts.contains(&ext.as_str()) && p.exists() {
                if !vision {
                    tui::line(&tui::yellow(
                        "  ⚠ current model is not multimodal — image not attached",
                    ));
                } else if let Ok(mut f) = std::fs::File::open(&p) {
                    let mut buf = Vec::new();
                    if f.read_to_end(&mut buf).is_ok() {
                        let media_type = match ext.as_str() {
                            "jpg" | "jpeg" => "image/jpeg",
                            "gif" => "image/gif",
                            "webp" => "image/webp",
                            _ => "image/png",
                        };
                        images.push((media_type.to_string(), media::b64_encode(&buf)));
                        // Show the attachment in the transcript: a half-block
                        // thumbnail rendered inline (any truecolor terminal).
                        if let Some((tw, th, rgb)) = media::decode_thumbnail(&p, 64, 36) {
                            let rows = tui::image_preview(&rgb, tw, th);
                            if !rows.is_empty() {
                                tui::line(&rows.join("\n"));
                            }
                        }
                        if !clean.is_empty() {
                            clean.push(' ');
                        }
                        clean.push_str(&format!(
                            "[image: {}]",
                            p.file_name().unwrap_or_default().to_string_lossy()
                        ));
                        continue;
                    }
                }
            } else if media::VIDEO_EXTS.contains(&ext.as_str()) && p.exists() {
                if !vision {
                    tui::line(&tui::yellow(
                        "  ⚠ current model is not multimodal — video not attached",
                    ));
                } else if !media::ffmpeg_available() {
                    tui::line(&tui::yellow(
                        "  ⚠ ffmpeg/ffprobe not found — install ffmpeg to attach videos",
                    ));
                } else {
                    tui::line(&tui::dim(&format!(
                        "  ⎘ parsing video {} with ffmpeg…",
                        p.file_name().unwrap_or_default().to_string_lossy()
                    )));
                    if let Some(v) = media::attach_video(&p) {
                        let n = v.frames.len();
                        images.extend(v.frames);
                        text_attachments.push(v.summary);
                        // First frame as an inline thumbnail.
                        if let Some((tw, th, rgb)) = media::decode_thumbnail(&p, 64, 36) {
                            let rows = tui::image_preview(&rgb, tw, th);
                            if !rows.is_empty() {
                                tui::line(&rows.join("\n"));
                            }
                        }
                        if !clean.is_empty() {
                            clean.push(' ');
                        }
                        clean.push_str(&format!(
                            "[video: {}{n} sampled frames attached in order]",
                            p.file_name().unwrap_or_default().to_string_lossy()
                        ));
                        continue;
                    }
                    tui::line(&tui::red(&format!(
                        "  ✗ could not decode video {}",
                        p.display()
                    )));
                }
            } else if let Some(text) = read_text_attachment(&p, range) {
                text_attachments.push(format!("[file: {}]\n{}", p.display(), text));
                if !clean.is_empty() {
                    clean.push(' ');
                }
                clean.push_str(&format!("[file: {}]", p.display()));
                continue;
            }
        }
        if !clean.is_empty() {
            clean.push(' ');
        }
        clean.push_str(word);
    }
    if !text_attachments.is_empty() {
        clean.push_str("\n\n[attached files]\n");
        clean.push_str(&text_attachments.join("\n\n"));
    }
    (clean, images)
}

fn split_attachment_range(raw: &str) -> (&str, Option<(usize, usize)>) {
    let Some((path, suffix)) = raw.rsplit_once(':') else {
        return (raw, None);
    };
    let parse_line = |s: &str| s.parse::<usize>().ok().filter(|n| *n > 0);
    if let Some((a, b)) = suffix.split_once('-') {
        if let (Some(start), Some(end)) = (parse_line(a), parse_line(b)) {
            return (path, Some((start, end.max(start))));
        }
    } else if let Some(line) = parse_line(suffix) {
        return (path, Some((line, line)));
    }
    (raw, None)
}

fn read_text_attachment(path: &std::path::Path, range: Option<(usize, usize)>) -> Option<String> {
    let meta = std::fs::metadata(path).ok()?;
    if meta.len() > MAX_ATTACHED_FILE_BYTES {
        return None;
    }
    let text = std::fs::read_to_string(path).ok()?;
    let Some((start, end)) = range else {
        return Some(text);
    };
    Some(
        text.lines()
            .enumerate()
            .filter_map(|(i, line)| {
                let line_no = i + 1;
                if line_no >= start && line_no <= end {
                    Some(line)
                } else {
                    None
                }
            })
            .collect::<Vec<_>>()
            .join("\n"),
    )
}

// Suggest a mode from the task phrasing (used for the "tip" hint, not a gate).
pub fn classify(task: &str) -> Mode {
    let l = task.to_lowercase();
    let has = |words: &[&str]| words.iter().any(|w| l.contains(*w));
    if has(&[
        "what should",
        "what if",
        "ideas for",
        "what do you think",
        "why would",
        "why is",
        "why does",
        "how about",
        "what are the options",
        "advice on",
        "suggest",
        "tradeoffs",
    ]) {
        return Mode::Brainstorm;
    }
    if has(&[
        "plan",
        "design",
        "architect",
        "break down",
        "roadmap",
        "scope",
    ]) {
        return Mode::Plan;
    }
    if has(&[
        "build",
        "create",
        "add",
        "fix",
        "implement",
        "write",
        "refactor",
        "run",
        "make",
    ]) {
        return Mode::Build;
    }
    if task.split_whitespace().count() > 8 {
        Mode::Plan
    } else {
        Mode::Build
    }
}

fn usage() {
    println!(
        "buildwithnexus {VERSION} — agentic AI CLI harness\n\n\
         USAGE:\n\
         \x20 buildwithnexus                 interactive session (all modes, full TUI)\n\
         \x20 buildwithnexus run <task>      execute a task (agentic BUILD loop)\n\
         \x20 buildwithnexus plan <task>     decompose, approve, then execute\n\
         \x20 buildwithnexus brainstorm <q>  chat with tools (grep, fetch, read, etc.)\n\
         \x20 buildwithnexus continue <task> continue the most recent session\n\
         \x20 buildwithnexus resume <id> <t> resume a specific session\n\
         \x20 buildwithnexus sessions        list saved sessions\n\
         \x20 buildwithnexus init            (re)configure provider / model / key\n\
         \x20 buildwithnexus providers       list built-in providers\n\
         \x20 buildwithnexus doctor          diagnose setup (keys, tools, connectivity)\n\
         \x20 buildwithnexus version | help\n\n\
         INTERACTIVE:\n\
         \x20 Shift+Tab              cycle mode (PLAN → BUILD → BRAINSTORM → PLAN)\n\
         \x20 /mode [plan|build|brainstorm]    show or switch mode\n\
         \x20 /model [name]                    hot-swap the AI model\n\
         \x20 /permissions [ask|auto|readonly] show or switch tool permission level\n\
         \x20 /mouse|/scroll [on|off|status]   wheel scroll + drag-to-copy (on by default)\n\
         \x20   or say: \"switch to build mode\" / \"use readonly\"\n\
         \x20 /compact               compress context to free up token budget\n\
         \x20 /context               show current context usage\n\
         \x20 /diff                  show current git diff summary\n\
         \x20 /review                AI code review of staged git diff\n\
         \x20 /commit                AI-drafted conventional commit message\n\
         \x20 /pr                    AI-drafted pull request title + description\n\
         \x20 /schedule <delay> <t>  one-shot workflow  (e.g. /schedule 5m cargo test)\n\
         \x20 /loop <interval> <t>   repeating workflow (e.g. /loop 30m cargo test)\n\
         \x20 /workflows /tasks      list and manage background workflows\n\
         \x20 /btw <context>         inject context into next agent turn\n\
         \x20 /config                configure hooks, memory, commands via AI\n\
         \x20 /memory                view and edit session memory\n\
         \x20 /skills                browse available skills and custom commands\n\
         \x20 /tools                 browse callable tools\n\
         \x20 /trace                 inspect hooks, tools, skills, and subagents\n\
         \x20 /agents /checkpoints /undo /doctor\n\
         \x20 /help /clear /new /resume /init /exit\n\
         \x20 !<cmd>                 run shell command directly\n\
         \x20 @<path>                Tab-complete a file path\n\
         \x20 Tab                    autocomplete /commands and sub-args\n"
    );
}

fn run_doctor() {
    println!("buildwithnexus {VERSION} — doctor");
    println!();

    // Settings
    match config::load_settings() {
        None => println!("  ✗ settings       not found — run `buildwithnexus init`"),
        Some(s) => {
            println!(
                "  ✓ settings       provider={} model={} permission={}",
                s.provider,
                if s.model.is_empty() {
                    "(default)"
                } else {
                    &s.model
                },
                s.permission
            );
        }
    }

    // API key
    for preset in config::PRESETS
        .iter()
        .filter(|p| !p.env_key.is_empty() && !p.local)
    {
        match config::load_key(preset.env_key) {
            Some(_) => println!("{}  set", preset.env_key),
            None => println!(
                "{}  not set (needed for {})",
                preset.env_key, preset.label
            ),
        }
    }

    // Memory
    match config::load_memory() {
        None => println!("  ·  memory.md     (empty)"),
        Some(m) => println!("  ✓ memory.md      {} chars", m.len()),
    }

    // External tools
    let tools_to_check = [
        ("git", "version control"),
        ("cargo", "Rust build tool"),
        ("node", "Node.js runtime"),
        ("npm", "Node package manager"),
        ("python3", "Python runtime"),
        ("gh", "GitHub CLI (optional)"),
        ("docker", "Docker (optional)"),
        ("rg", "ripgrep (fast search, optional)"),
    ];
    for (bin, label) in &tools_to_check {
        let found = std::process::Command::new("which")
            .arg(bin)
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false);
        let glyph = if found { "" } else { "·" };
        println!("  {glyph} {bin:<12} {label}");
    }

    if crate::tools::is_wsl() {
        println!();
        println!("  ✓ WSL2 runtime     detected");
        let home = config::home();
        if crate::tools::is_wsl_windows_mount(&home) {
            println!(
                "  ⚠ WSL2 filesystem  NEXUS_HOME is on a Windows mount ({}).",
                home.display()
            );
            println!("                     Set NEXUS_HOME to a Linux path (~/.buildwithnexus) for 10x faster I/O.");
        } else {
            println!("  ✓ WSL2 filesystem  native Linux filesystem detected (optimal I/O speed)");
        }
    }

    // Connectivity (quick HEAD to detect outbound network)
    println!();
    println!("  checking connectivity...");
    let reachable = std::process::Command::new("curl")
        .args([
            "-sS",
            "--max-time",
            "5",
            "-o",
            "/dev/null",
            "-w",
            "%{http_code}",
            "https://api.anthropic.com",
        ])
        .output()
        .ok()
        .and_then(|o| String::from_utf8(o.stdout).ok())
        .map(|code| code.trim() != "000")
        .unwrap_or(false);
    if reachable {
        println!("  ✓ api.anthropic.com reachable");
    } else {
        println!("  ✗ api.anthropic.com unreachable — check firewall / proxy");
    }

    println!();
    check_and_offer_install_dependencies(true);
    println!();
    println!("  Run `buildwithnexus init` to fix any missing configuration.");
}

pub fn check_and_offer_install_dependencies(interactive: bool) {
    let tools_to_check = [
        ("git", "git", "git", "Version control & workspace tracking"),
        (
            "rg",
            "ripgrep",
            "ripgrep",
            "High-speed regex/pattern file searching",
        ),
        ("node", "node", "nodejs", "Node.js runtime & MCP servers"),
        ("npm", "node", "npm", "Node package manager"),
        (
            "python3",
            "python",
            "python3",
            "Python runtime & data scripting",
        ),
    ];

    let mut missing = Vec::new();
    for (bin, brew_pkg, apt_pkg, desc) in &tools_to_check {
        let found = std::process::Command::new("which")
            .arg(bin)
            .output()
            .map(|o| o.status.success())
            .unwrap_or(false);
        if !found {
            missing.push((*bin, *brew_pkg, *apt_pkg, *desc));
        }
    }

    if missing.is_empty() {
        if interactive {
            tui::line(&tui::green(
                "  ✓ dependencies installed (git, rg, node, npm, python3)",
            ));
        }
        return;
    }

    tui::line(&tui::yellow(&format!(
        "  ⚠ Missing {} OOTB development tool(s):",
        missing.len()
    )));
    for (bin, _, _, desc) in &missing {
        tui::line(&format!("{}{}", tui::bold(bin), desc));
    }

    if !interactive {
        tui::line(&tui::dim("  Tip: Run `buildwithnexus init` or `buildwithnexus doctor` to auto-install missing dependencies."));
        return;
    }

    let brew_available = std::process::Command::new("which")
        .arg("brew")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);
    let apt_available = std::process::Command::new("which")
        .arg("apt-get")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);

    for (bin, brew_pkg, apt_pkg, desc) in missing {
        let ask_msg = format!(
            "  Would you like to install '{}' ({}) now? [Y/n]: ",
            bin, desc
        );
        let ans = match tui::ask(&ask_msg) {
            Some(a) => a.trim().to_lowercase(),
            None => break,
        };
        if ans == "n" || ans == "no" {
            tui::line(&tui::dim(&format!("    Skipped installing {bin}.")));
            continue;
        }

        if brew_available {
            tui::line(&tui::accent(&format!(
                "    Running `brew install {brew_pkg}`..."
            )));
            let res = std::process::Command::new("brew")
                .args(["install", brew_pkg])
                .status();
            match res {
                Ok(s) if s.success() => {
                    tui::line(&tui::green(&format!("    ✓ Successfully installed {bin}!")))
                }
                _ => tui::line(&tui::red(&format!(
                    "    ✗ Failed to install {brew_pkg} via Homebrew."
                ))),
            }
        } else if apt_available {
            tui::line(&tui::accent(&format!(
                "    Running `sudo apt-get install -y {apt_pkg}`..."
            )));
            let res = std::process::Command::new("sudo")
                .args(["apt-get", "install", "-y", apt_pkg])
                .status();
            match res {
                Ok(s) if s.success() => {
                    tui::line(&tui::green(&format!("    ✓ Successfully installed {bin}!")))
                }
                _ => tui::line(&tui::red(&format!(
                    "    ✗ Failed to install {apt_pkg} via apt-get."
                ))),
            }
        } else {
            tui::line(&tui::yellow(&format!(
                "    Neither Homebrew nor apt-get found. Please install '{bin}' manually."
            )));
        }
    }
}

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

    #[test]
    fn classify_brainstorm_phrases() {
        assert!(matches!(
            classify("what should I name this?"),
            Mode::Brainstorm
        ));
        assert!(matches!(
            classify("any ideas for the API?"),
            Mode::Brainstorm
        ));
        assert!(matches!(classify("why is this slow"), Mode::Brainstorm));
    }

    #[test]
    fn classify_plan_phrases() {
        assert!(matches!(classify("design the auth system"), Mode::Plan));
        assert!(matches!(classify("architect a new module"), Mode::Plan));
        assert!(matches!(classify("break down the migration"), Mode::Plan));
    }

    #[test]
    fn classify_build_phrases() {
        assert!(matches!(classify("add a login button"), Mode::Build));
        assert!(matches!(classify("fix the off-by-one bug"), Mode::Build));
        assert!(matches!(classify("refactor the parser"), Mode::Build));
    }

    #[test]
    fn classify_short_defaults_build() {
        assert!(matches!(classify("foo bar"), Mode::Build));
    }

    #[test]
    fn classify_long_unmatched_defaults_plan() {
        assert!(matches!(
            classify("the quick brown fox jumps over the lazy sleeping dog today"),
            Mode::Plan
        ));
    }

    #[test]
    fn classify_is_case_insensitive() {
        assert!(matches!(classify("DESIGN the system"), Mode::Plan));
    }

    #[test]
    fn auto_switch_escalates_out_of_brainstorm_only() {
        // A build request in chat mode must actually build, not chat.
        assert!(matches!(
            auto_switch_mode("build me a snake game", &Mode::Brainstorm),
            Some(Mode::Build)
        ));
        assert!(matches!(
            auto_switch_mode("plan the migration to sqlite", &Mode::Brainstorm),
            Some(Mode::Plan)
        ));
        // A deliberate PLAN gate is never silently bypassed.
        assert!(auto_switch_mode("fix the parser bug", &Mode::Plan).is_none());
        // Matching mode: nothing to do.
        assert!(auto_switch_mode("fix the parser bug", &Mode::Build).is_none());
    }

    #[test]
    fn conversational_turns_bypass_plan_and_build_dispatch() {
        assert!(should_answer_conversationally("hello", &Mode::Build));
        assert!(should_answer_conversationally(
            "what can you do?",
            &Mode::Plan
        ));
        assert!(should_answer_conversationally(
            "why is this slow?",
            &Mode::Build
        ));
    }

    #[test]
    fn action_turns_stay_in_active_dispatch() {
        assert!(!should_answer_conversationally(
            "build me a canvas game",
            &Mode::Build
        ));
        assert!(!should_answer_conversationally(
            "find a folder named nexus",
            &Mode::Build
        ));
        assert!(!should_answer_conversationally(
            "read my projects file and tell me what repos I have",
            &Mode::Plan
        ));
    }

    #[test]
    fn mode_cycles_correctly() {
        assert!(matches!(Mode::Plan.next(), Mode::Build));
        assert!(matches!(Mode::Build.next(), Mode::Brainstorm));
        assert!(matches!(Mode::Brainstorm.next(), Mode::Plan));
    }

    #[test]
    fn detect_mode_switch_verb_prefixes() {
        assert!(matches!(
            detect_mode_switch("switch to plan mode"),
            Some(Mode::Plan)
        ));
        assert!(matches!(
            detect_mode_switch("change to build"),
            Some(Mode::Build)
        ));
        assert!(matches!(
            detect_mode_switch("go to brainstorm"),
            Some(Mode::Brainstorm)
        ));
        assert!(matches!(
            detect_mode_switch("set mode to planning"),
            Some(Mode::Plan)
        ));
        assert!(matches!(
            detect_mode_switch("use build mode"),
            Some(Mode::Build)
        ));
        assert!(matches!(
            detect_mode_switch("use brainstorm mode"),
            Some(Mode::Brainstorm)
        ));
    }

    #[test]
    fn detect_mode_switch_bare_short_form() {
        assert!(matches!(detect_mode_switch("plan mode"), Some(Mode::Plan)));
        assert!(matches!(
            detect_mode_switch("build mode"),
            Some(Mode::Build)
        ));
        assert!(matches!(
            detect_mode_switch("brainstorm mode"),
            Some(Mode::Brainstorm)
        ));
        assert!(matches!(detect_mode_switch("planning"), Some(Mode::Plan)));
    }

    #[test]
    fn detect_mode_switch_no_false_positives() {
        assert!(detect_mode_switch("build me a todo app").is_none());
        assert!(detect_mode_switch("plan the migration carefully").is_none());
        assert!(detect_mode_switch("let's brainstorm some ideas").is_none());
        assert!(detect_mode_switch("use this library instead").is_none());
    }

    #[test]
    fn detect_permission_switch_verb_prefixes() {
        assert_eq!(
            detect_permission_switch("switch to readonly"),
            Some("readonly")
        );
        assert_eq!(detect_permission_switch("change to auto"), Some("auto"));
        assert_eq!(
            detect_permission_switch("set permission to ask"),
            Some("ask")
        );
        assert_eq!(
            detect_permission_switch("use readonly mode"),
            Some("readonly")
        );
    }

    #[test]
    fn parse_cli_options_extracts_model_and_permission() {
        let (opts, rest) = parse_cli_options(vec![
            "--model".into(),
            "qwen3".into(),
            "--permission-mode=acceptEdits".into(),
            "fix".into(),
            "tests".into(),
        ]);
        assert_eq!(opts.model.as_deref(), Some("qwen3"));
        assert_eq!(opts.permission_mode.as_deref(), Some("acceptEdits"));
        assert_eq!(rest, vec!["fix", "tests"]);
    }

    #[test]
    fn attachment_range_parsing() {
        assert_eq!(
            split_attachment_range("src/lib.rs:10-12"),
            ("src/lib.rs", Some((10, 12)))
        );
        assert_eq!(
            split_attachment_range("src/lib.rs:5"),
            ("src/lib.rs", Some((5, 5)))
        );
        assert_eq!(
            split_attachment_range("src/lib.rs:nope"),
            ("src/lib.rs:nope", None)
        );
    }

    #[test]
    fn text_attachment_context_is_extracted() {
        let dir = std::env::temp_dir().join(format!("bwn-attach-test-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(dir.join("src")).unwrap();
        std::fs::write(dir.join("src/lib.rs"), "one\ntwo\nthree\n").unwrap();

        let (text, images) = extract_attachments("please read @src/lib.rs:2-3", &dir, true);
        assert!(images.is_empty());
        assert!(text.contains("[file:"));
        assert!(text.contains("two\nthree"));
        assert!(!text.contains("\none\n"));
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn build_provider_rejects_http_for_keyed_preset() {
        let s = Settings {
            provider: "openai".into(),
            model: String::new(),
            permission: "ask".into(),
            base_url: Some("http://insecure.local/v1".into()),
            allowed_commands: Vec::new(),
            ..Default::default()
        };
        match build_provider(&s) {
            Err(e) => assert!(e.contains("non-HTTPS")),
            Ok(_) => panic!("expected http base_url to be rejected"),
        }
    }

    #[test]
    fn build_provider_unknown_provider() {
        let s = Settings {
            provider: "does-not-exist".into(),
            model: String::new(),
            permission: "ask".into(),
            base_url: None,
            allowed_commands: Vec::new(),
            ..Default::default()
        };
        match build_provider(&s) {
            Err(e) => assert!(e.contains("unknown provider")),
            Ok(_) => panic!("expected unknown provider error"),
        }
    }

    #[test]
    fn build_provider_local_preset_allows_http() {
        let s = Settings {
            provider: "ollama".into(),
            model: String::new(),
            permission: "ask".into(),
            base_url: Some("http://localhost:11434/v1".into()),
            allowed_commands: Vec::new(),
            ..Default::default()
        };
        match build_provider(&s) {
            Ok(p) => {
                assert!(p.api_key.is_none());
                assert_eq!(p.model, "llama3.2");
            }
            Err(e) => panic!("local http should build: {e}"),
        }
    }

    #[test]
    fn test_handle_voice_missing_file_returns_none() {
        let res = super::handle_voice("/nonexistent/audio/path.wav");
        assert!(res.is_none());
    }

    #[test]
    fn test_handle_kb_index_and_rules() {
        let dir = std::env::temp_dir().join(format!("bwn-kb-test-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let file_path = dir.join("test_code.rs");
        std::fs::write(
            &file_path,
            "pub fn authenticate_user() {}\npub struct SessionData {}",
        )
        .unwrap();
        super::handle_kb_index(&dir);

        let kb = crate::knowledge::KnowledgeBase::new(&dir.to_string_lossy());
        assert!(!kb.entities.is_empty());
        assert!(kb.entities.values().any(|e| e.name == "authenticate_user"));
        assert!(kb.entities.values().any(|e| e.name == "SessionData"));

        super::handle_rules(&dir);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_handle_verify_audit() {
        let dir = std::env::temp_dir().join(format!("bwn-verify-test-{}", std::process::id()));
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).unwrap();
        let file_path = dir.join("test_file.rs");
        std::fs::write(&file_path, "fn main() {}\n").unwrap();
        super::handle_verify_audit(&dir);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn test_check_and_offer_install_dependencies() {
        super::check_and_offer_install_dependencies(false);
    }
}