mimir-harness 0.1.0

Mimir transparent agent launch harness.
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
//! Entry point for the `mimir` transparent agent harness.

use std::collections::BTreeMap;
use std::fs;
use std::io::{self, IsTerminal, Read};
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use mimir_harness::{
    capture_session_drafts, generate_session_id, list_checkpoint_notes, prepare_launch_plan,
    prepare_remote_sync_plan, render_draft_next, render_draft_show, render_draft_triage,
    render_drafts_list, render_drafts_status, render_launch_banner, render_memory_context,
    render_memory_explain, render_memory_health, render_memory_list, render_memory_show,
    render_operator_status, render_project_doctor, render_remote_dry_run,
    render_remote_restore_drill_dry_run, render_remote_restore_drill_report, render_remote_status,
    render_remote_sync_report, run_child, run_remote_restore_drill, run_remote_sync,
    submit_memory_revoke_request, write_checkpoint_note, CheckpointNoteMetadata, HarnessError,
    RemoteSyncDirection,
};
use mimir_librarian::DraftState;

const USAGE: &str = "\
mimir — transparent Mimir agent launch harness.

Usage:
    mimir [mimir flags] <agent> [agent args...]
    mimir checkpoint [--title <title>] [--list] [text...]
    mimir status [options]
    mimir doctor [options]
    mimir health [options]
    mimir context [options]
    mimir memory <list|show|explain|revoke> [options]
    mimir drafts <status|list|show|next|skip|quarantine> [options]
    mimir hook-context
    mimir config init [options]
    mimir remote <status|push|pull|drill> [options]
    mimir setup-agent <status|doctor|install|remove> --agent <claude|codex> --scope <project|user> [options]

Mimir flags:
    --project <name>    Override detected project/scope label.
    -h, --help          Show this help.
    --version           Show version.

Arguments after <agent> are passed to the child unchanged.
On first run, launch still enters the requested agent; Mimir exposes
bootstrap/config state through MIMIR_* environment variables and a
structured session capsule.

Examples:
    mimir claude --r
    mimir codex --model gpt-5.4
    mimir --project Mimir claude --r
    mimir checkpoint --title \"Handoff\" \"The librarian now processes captured drafts.\"
    mimir status
    mimir doctor
    mimir health
    mimir context --limit 12
    mimir memory list --limit 20
    mimir memory explain --id @__mem_0
    mimir drafts list --state pending
    mimir hook-context
    mimir config init --operator hasnobeef --organization buildepicshit --remote-url git@github.com:org/mimir-memory.git
    mimir remote status
    mimir remote push --dry-run
    mimir remote drill --dry-run
    mimir setup-agent doctor --agent codex --scope project
    mimir setup-agent install --agent codex --scope project --from \"$MIMIR_AGENT_SETUP_DIR\"
";

fn main() -> ExitCode {
    let args: Vec<String> = std::env::args().skip(1).collect();
    if matches!(args.as_slice(), [flag] if flag == "-h" || flag == "--help") {
        println!("{USAGE}");
        return ExitCode::SUCCESS;
    }
    if args.len() == 1 && args[0] == "--version" {
        println!("mimir {}", env!("CARGO_PKG_VERSION"));
        return ExitCode::SUCCESS;
    }
    if args.first().is_some_and(|arg| arg == "checkpoint") {
        return run_checkpoint_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "status") {
        return run_status_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "doctor") {
        return run_doctor_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "health") {
        return run_health_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "context") {
        return run_context_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "memory") {
        return run_memory_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "drafts") {
        return run_drafts_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "hook-context") {
        return run_hook_context_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "config") {
        return run_config_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "remote") {
        return run_remote_command(&args[1..]);
    }
    if args.first().is_some_and(|arg| arg == "setup-agent") {
        return run_setup_agent_command(&args[1..]);
    }

    let session_id = generate_session_id();
    let current_dir = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    let env: BTreeMap<String, String> = std::env::vars().collect();
    let plan = match prepare_launch_plan(args, session_id, &current_dir, &env) {
        Ok(plan) => plan,
        Err(err) => return report_argument_error(&err),
    };

    eprint!("{}", render_launch_banner(&plan));

    match run_child(&plan) {
        Ok(status) => {
            if let Err(err) =
                capture_session_drafts(&plan, status.code(), std::time::SystemTime::now())
            {
                eprintln!("mimir: {err}");
            }
            exit_code_from_status(status.code())
        }
        Err(err) => report_spawn_error(&err),
    }
}

fn run_status_command(args: &[String]) -> ExitCode {
    if matches!(args, [flag] if flag == "-h" || flag == "--help") {
        println!("{}", status_usage());
        return ExitCode::SUCCESS;
    }
    let cwd = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    match status_command(args, &cwd, std::env::vars().collect()) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn status_command(
    args: &[String],
    cwd: &Path,
    mut env: BTreeMap<String, String>,
) -> Result<String, HarnessError> {
    let options = ProjectCommandOptions::parse(args, cwd, status_usage)?;
    if options.project_root_source.is_explicit() || options.config_path.is_some() {
        remove_inherited_project_env(&mut env);
    }
    if let Some(config_path) = &options.config_path {
        env.insert(
            "MIMIR_CONFIG_PATH".to_string(),
            config_path.display().to_string(),
        );
    }
    render_operator_status(&options.project_root, &env)
}

fn status_usage() -> String {
    "Usage: mimir status [--project-root <dir>] [--config <file>]".to_string()
}

fn run_doctor_command(args: &[String]) -> ExitCode {
    if matches!(args, [flag] if flag == "-h" || flag == "--help") {
        println!("{}", doctor_usage());
        return ExitCode::SUCCESS;
    }
    let cwd = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    match doctor_command(args, &cwd, std::env::vars().collect()) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn doctor_command(
    args: &[String],
    cwd: &Path,
    mut env: BTreeMap<String, String>,
) -> Result<String, HarnessError> {
    let options = ProjectCommandOptions::parse(args, cwd, doctor_usage)?;
    if options.project_root_source.is_explicit() || options.config_path.is_some() {
        remove_inherited_project_env(&mut env);
    }
    if let Some(config_path) = &options.config_path {
        env.insert(
            "MIMIR_CONFIG_PATH".to_string(),
            config_path.display().to_string(),
        );
    }
    render_project_doctor(&options.project_root, &env)
}

fn doctor_usage() -> String {
    "Usage: mimir doctor [--project-root <dir>] [--config <file>]".to_string()
}

fn run_health_command(args: &[String]) -> ExitCode {
    if matches!(args, [flag] if flag == "-h" || flag == "--help") {
        println!("{}", health_usage());
        return ExitCode::SUCCESS;
    }
    let cwd = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    match health_command(args, &cwd, std::env::vars().collect()) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn health_command(
    args: &[String],
    cwd: &Path,
    mut env: BTreeMap<String, String>,
) -> Result<String, HarnessError> {
    let options = ProjectCommandOptions::parse(args, cwd, health_usage)?;
    if options.project_root_source.is_explicit() || options.config_path.is_some() {
        remove_inherited_project_env(&mut env);
    }
    if let Some(config_path) = &options.config_path {
        env.insert(
            "MIMIR_CONFIG_PATH".to_string(),
            config_path.display().to_string(),
        );
    }
    render_memory_health(&options.project_root, &env)
}

fn health_usage() -> String {
    "Usage: mimir health [--project-root <dir>] [--config <file>]".to_string()
}

fn run_context_command(args: &[String]) -> ExitCode {
    if matches!(args, [flag] if flag == "-h" || flag == "--help") {
        println!("{}", context_usage());
        return ExitCode::SUCCESS;
    }
    let cwd = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    match context_command(args, &cwd, std::env::vars().collect()) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn context_command(
    args: &[String],
    cwd: &Path,
    mut env: BTreeMap<String, String>,
) -> Result<String, HarnessError> {
    let options = ContextCommandOptions::parse(args, cwd)?;
    if options.project_root_source.is_explicit() || options.config_path.is_some() {
        remove_inherited_project_env(&mut env);
    }
    if let Some(config_path) = &options.config_path {
        env.insert(
            "MIMIR_CONFIG_PATH".to_string(),
            config_path.display().to_string(),
        );
    }
    render_memory_context(&options.project_root, &env, options.limit)
}

fn context_usage() -> String {
    "Usage: mimir context [--project-root <dir>] [--config <file>] [--limit <records>]".to_string()
}

fn run_memory_command(args: &[String]) -> ExitCode {
    if matches!(args, [flag] if flag == "-h" || flag == "--help") {
        println!("{}", memory_usage());
        return ExitCode::SUCCESS;
    }
    let cwd = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    match memory_command(args, &cwd, std::env::vars().collect()) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn memory_command(
    args: &[String],
    cwd: &Path,
    mut env: BTreeMap<String, String>,
) -> Result<String, HarnessError> {
    let options = MemoryCommandOptions::parse(args, cwd)?;
    if options.project_root_source.is_explicit() || options.config_path.is_some() {
        remove_inherited_project_env(&mut env);
    }
    if let Some(config_path) = &options.config_path {
        env.insert(
            "MIMIR_CONFIG_PATH".to_string(),
            config_path.display().to_string(),
        );
    }
    match options.action {
        MemoryCommandAction::List => render_memory_list(
            &options.project_root,
            &env,
            options.limit,
            options.kind.as_deref(),
        ),
        MemoryCommandAction::Show => render_memory_show(
            &options.project_root,
            &env,
            options
                .id
                .as_deref()
                .ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory show requires --id <memory-id>".to_string(),
                })?,
        ),
        MemoryCommandAction::Explain => render_memory_explain(
            &options.project_root,
            &env,
            options
                .id
                .as_deref()
                .ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory explain requires --id <memory-id>".to_string(),
                })?,
        ),
        MemoryCommandAction::Revoke => submit_memory_revoke_request(
            &options.project_root,
            &env,
            options
                .id
                .as_deref()
                .ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory revoke requires --id <memory-id>".to_string(),
                })?,
            options
                .reason
                .as_deref()
                .ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory revoke requires --reason <reason>".to_string(),
                })?,
            options.dry_run,
        ),
    }
}

fn memory_usage() -> String {
    "Usage: mimir memory <list|show|explain|revoke> [--project-root <dir>] [--config <file>] [--limit <records>] [--kind all|sem|epi|pro|inf] [--id <memory-id>] [--reason <text>] [--dry-run]"
        .to_string()
}

fn run_drafts_command(args: &[String]) -> ExitCode {
    if matches!(args, [flag] if flag == "-h" || flag == "--help") {
        println!("{}", drafts_usage());
        return ExitCode::SUCCESS;
    }
    let cwd = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    match drafts_command(args, &cwd, std::env::vars().collect()) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn drafts_command(
    args: &[String],
    cwd: &Path,
    mut env: BTreeMap<String, String>,
) -> Result<String, HarnessError> {
    let options = DraftsCommandOptions::parse(args, cwd)?;
    if options.project_root_source.is_explicit() || options.config_path.is_some() {
        remove_inherited_project_env(&mut env);
    }
    if let Some(config_path) = &options.config_path {
        env.insert(
            "MIMIR_CONFIG_PATH".to_string(),
            config_path.display().to_string(),
        );
    }
    match options.action {
        DraftsCommandAction::Status => {
            render_drafts_status(&options.project_root, &env, options.drafts_dir.as_deref())
        }
        DraftsCommandAction::List => render_drafts_list(
            &options.project_root,
            &env,
            options.drafts_dir.as_deref(),
            options.state.unwrap_or(DraftState::Pending),
        ),
        DraftsCommandAction::Next => render_draft_next(
            &options.project_root,
            &env,
            options.drafts_dir.as_deref(),
            options.state.unwrap_or(DraftState::Pending),
        ),
        DraftsCommandAction::Skip => render_draft_triage(
            &options.project_root,
            &env,
            options.drafts_dir.as_deref(),
            options
                .id
                .as_deref()
                .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                    message: "drafts skip requires a draft id".to_string(),
                })?,
            options.state.unwrap_or(DraftState::Pending),
            DraftState::Skipped,
            options
                .reason
                .as_deref()
                .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                    message: "drafts skip requires --reason".to_string(),
                })?,
        ),
        DraftsCommandAction::Quarantine => render_draft_triage(
            &options.project_root,
            &env,
            options.drafts_dir.as_deref(),
            options
                .id
                .as_deref()
                .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                    message: "drafts quarantine requires a draft id".to_string(),
                })?,
            options.state.unwrap_or(DraftState::Pending),
            DraftState::Quarantined,
            options
                .reason
                .as_deref()
                .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                    message: "drafts quarantine requires --reason".to_string(),
                })?,
        ),
        DraftsCommandAction::Show => render_draft_show(
            &options.project_root,
            &env,
            options.drafts_dir.as_deref(),
            options
                .id
                .as_deref()
                .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                    message: "drafts show requires a draft id".to_string(),
                })?,
            options.state,
        ),
    }
}

fn drafts_usage() -> String {
    "Usage: mimir drafts <status|list|show|next|skip|quarantine> [ID] [--reason <text>] [--state pending|processing|accepted|skipped|failed|quarantined] [--project-root <dir>] [--config <file>] [--drafts-dir <dir>]"
        .to_string()
}

fn run_hook_context_command(args: &[String]) -> ExitCode {
    if !args.is_empty() {
        eprintln!("mimir: hook-context does not accept arguments");
        return ExitCode::from(2);
    }
    let env: BTreeMap<String, String> = std::env::vars().collect();
    print!("{}", hook_context_output(&env));
    ExitCode::SUCCESS
}

fn run_config_command(args: &[String]) -> ExitCode {
    if matches!(args, [flag] if flag == "-h" || flag == "--help") {
        println!("{}", config_usage());
        return ExitCode::SUCCESS;
    }
    let cwd = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    match config_command(args, &cwd) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn config_command(args: &[String], cwd: &Path) -> Result<String, String> {
    let Some(action) = args.first() else {
        return Err(config_usage());
    };
    match action.as_str() {
        "init" => config_init_command(&args[1..], cwd),
        "-h" | "--help" => Err(config_usage()),
        unknown => Err(format!("unknown config action `{unknown}`; expected init")),
    }
}

fn config_usage() -> String {
    "Usage: mimir config init [--path <file>|--project-root <dir>] [--data-root <dir>] [--drafts-dir <dir>] [--operator <id>] [--organization <id>] [--remote-url <url>] [--remote-kind git|service] [--remote-branch <branch>] [--librarian-after-capture off|defer|archive_raw|process] [--dry-run]"
        .to_string()
}

fn run_remote_command(args: &[String]) -> ExitCode {
    if matches!(args, [flag] if flag == "-h" || flag == "--help") {
        println!("{}", remote_usage());
        return ExitCode::SUCCESS;
    }
    let cwd = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    match remote_command(args, &cwd, std::env::vars().collect()) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn remote_command(
    args: &[String],
    cwd: &Path,
    mut env: BTreeMap<String, String>,
) -> Result<String, HarnessError> {
    let options = RemoteCommandOptions::parse(args, cwd)?;
    if options.project_root_source.is_explicit() || options.config_path.is_some() {
        remove_inherited_project_env(&mut env);
    }
    if let Some(config_path) = &options.config_path {
        env.insert(
            "MIMIR_CONFIG_PATH".to_string(),
            config_path.display().to_string(),
        );
    }
    match options.action {
        RemoteCommandAction::Status => {
            render_remote_status(&options.project_root, &env, options.refresh)
        }
        RemoteCommandAction::Push => {
            if options.dry_run {
                render_remote_dry_run(&options.project_root, &env, RemoteSyncDirection::Push)
            } else {
                let plan = prepare_remote_sync_plan(&options.project_root, &env)?;
                let report = run_remote_sync(&plan, RemoteSyncDirection::Push)?;
                Ok(render_remote_sync_report(&report))
            }
        }
        RemoteCommandAction::Pull => {
            if options.dry_run {
                render_remote_dry_run(&options.project_root, &env, RemoteSyncDirection::Pull)
            } else {
                let plan = prepare_remote_sync_plan(&options.project_root, &env)?;
                let report = run_remote_sync(&plan, RemoteSyncDirection::Pull)?;
                Ok(render_remote_sync_report(&report))
            }
        }
        RemoteCommandAction::Drill => {
            let plan = prepare_remote_sync_plan(&options.project_root, &env)?;
            if options.dry_run {
                Ok(render_remote_restore_drill_dry_run(&plan))
            } else {
                let report = run_remote_restore_drill(&plan, options.destructive)?;
                Ok(render_remote_restore_drill_report(&report))
            }
        }
    }
}

fn remote_usage() -> String {
    "Usage: mimir remote <status|push|pull|drill> [--project-root <dir>] [--config <file>] [--dry-run] [--refresh] [--destructive]"
        .to_string()
}

fn remove_inherited_project_env(env: &mut BTreeMap<String, String>) {
    env.remove("MIMIR_CONFIG_PATH");
    env.remove("MIMIR_DRAFTS_DIR");
    env.remove("MIMIR_CAPTURE_SUMMARY_PATH");
}

#[derive(Debug, Clone)]
struct ProjectCommandOptions {
    project_root: PathBuf,
    config_path: Option<PathBuf>,
    project_root_source: ProjectRootSource,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ProjectRootSource {
    Cwd,
    Flag,
}

impl ProjectRootSource {
    const fn is_explicit(self) -> bool {
        matches!(self, Self::Flag)
    }
}

impl ProjectCommandOptions {
    fn parse(args: &[String], cwd: &Path, usage: fn() -> String) -> Result<Self, HarnessError> {
        let mut project_root = cwd.to_path_buf();
        let mut config_path = None;
        let mut project_root_source = ProjectRootSource::Cwd;

        let mut iter = args.iter();
        while let Some(arg) = iter.next() {
            match arg.as_str() {
                "--project-root" => {
                    project_root_source = ProjectRootSource::Flag;
                    let value = iter
                        .next()
                        .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                            message: "status --project-root requires a value".to_string(),
                        })?;
                    project_root = PathBuf::from(non_empty_command_flag_value(
                        "status",
                        "--project-root",
                        value,
                    )?);
                }
                "--config" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                            message: "status --config requires a value".to_string(),
                        })?;
                    config_path = Some(PathBuf::from(non_empty_command_flag_value(
                        "status", "--config", value,
                    )?));
                }
                "-h" | "--help" => {
                    return Err(HarnessError::RemoteSyncUnavailable { message: usage() });
                }
                unknown if unknown.starts_with('-') => {
                    return Err(HarnessError::RemoteSyncUnavailable {
                        message: format!("unknown status flag `{unknown}`"),
                    });
                }
                unknown => {
                    return Err(HarnessError::RemoteSyncUnavailable {
                        message: format!("unexpected status argument `{unknown}`"),
                    });
                }
            }
        }

        Ok(Self {
            project_root,
            config_path,
            project_root_source,
        })
    }
}

#[derive(Debug, Clone)]
struct ContextCommandOptions {
    project_root: PathBuf,
    config_path: Option<PathBuf>,
    project_root_source: ProjectRootSource,
    limit: usize,
}

impl ContextCommandOptions {
    fn parse(args: &[String], cwd: &Path) -> Result<Self, HarnessError> {
        let mut project_root = cwd.to_path_buf();
        let mut config_path = None;
        let mut project_root_source = ProjectRootSource::Cwd;
        let mut limit = 12_usize;

        let mut iter = args.iter();
        while let Some(arg) = iter.next() {
            match arg.as_str() {
                "--project-root" => {
                    project_root_source = ProjectRootSource::Flag;
                    let value = iter
                        .next()
                        .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                            message: "context --project-root requires a value".to_string(),
                        })?;
                    project_root = PathBuf::from(non_empty_command_flag_value(
                        "context",
                        "--project-root",
                        value,
                    )?);
                }
                "--config" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                            message: "context --config requires a value".to_string(),
                        })?;
                    config_path = Some(PathBuf::from(non_empty_command_flag_value(
                        "context", "--config", value,
                    )?));
                }
                "--limit" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                            message: "context --limit requires a value".to_string(),
                        })?;
                    let value = non_empty_command_flag_value("context", "--limit", value)?;
                    limit = value.parse::<usize>().map_err(|_| {
                        HarnessError::RemoteSyncUnavailable {
                            message: "context --limit must be a positive integer between 1 and 64"
                                .to_string(),
                        }
                    })?;
                    if !(1..=64).contains(&limit) {
                        return Err(HarnessError::RemoteSyncUnavailable {
                            message: "context --limit must be a positive integer between 1 and 64"
                                .to_string(),
                        });
                    }
                }
                "-h" | "--help" => {
                    return Err(HarnessError::RemoteSyncUnavailable {
                        message: context_usage(),
                    });
                }
                unknown if unknown.starts_with('-') => {
                    return Err(HarnessError::RemoteSyncUnavailable {
                        message: format!("unknown context flag `{unknown}`"),
                    });
                }
                unknown => {
                    return Err(HarnessError::RemoteSyncUnavailable {
                        message: format!("unexpected context argument `{unknown}`"),
                    });
                }
            }
        }

        Ok(Self {
            project_root,
            config_path,
            project_root_source,
            limit,
        })
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum MemoryCommandAction {
    List,
    Show,
    Explain,
    Revoke,
}

impl MemoryCommandAction {
    fn parse(value: &str) -> Result<Self, HarnessError> {
        match value {
            "list" => Ok(Self::List),
            "show" => Ok(Self::Show),
            "explain" => Ok(Self::Explain),
            "revoke" => Ok(Self::Revoke),
            _ => Err(HarnessError::MemoryUnavailable {
                message: format!(
                    "unknown memory action `{value}`; expected list, show, explain, or revoke"
                ),
            }),
        }
    }
}

#[derive(Debug, Clone)]
struct MemoryCommandOptions {
    action: MemoryCommandAction,
    project_root: PathBuf,
    project_root_source: ProjectRootSource,
    config_path: Option<PathBuf>,
    limit: usize,
    kind: Option<String>,
    id: Option<String>,
    reason: Option<String>,
    dry_run: bool,
}

impl MemoryCommandOptions {
    fn parse(args: &[String], cwd: &Path) -> Result<Self, HarnessError> {
        let Some(action_arg) = args.first() else {
            return Err(HarnessError::MemoryUnavailable {
                message: memory_usage(),
            });
        };
        if action_arg == "-h" || action_arg == "--help" {
            return Err(HarnessError::MemoryUnavailable {
                message: memory_usage(),
            });
        }
        let action = MemoryCommandAction::parse(action_arg)?;
        let mut options = Self {
            action,
            project_root: cwd.to_path_buf(),
            project_root_source: ProjectRootSource::Cwd,
            config_path: None,
            limit: 50,
            kind: None,
            id: None,
            reason: None,
            dry_run: false,
        };
        let mut iter = args[1..].iter();
        while let Some(arg) = iter.next() {
            options.parse_arg(arg, &mut iter)?;
        }
        options.validate()?;
        Ok(options)
    }

    fn parse_arg(
        &mut self,
        arg: &str,
        iter: &mut std::slice::Iter<'_, String>,
    ) -> Result<(), HarnessError> {
        match arg {
            "--project-root" => {
                self.project_root_source = ProjectRootSource::Flag;
                let value = iter.next().ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory --project-root requires a value".to_string(),
                })?;
                self.project_root = PathBuf::from(non_empty_command_flag_value(
                    "memory",
                    "--project-root",
                    value,
                )?);
            }
            "--config" => {
                let value = iter.next().ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory --config requires a value".to_string(),
                })?;
                self.config_path = Some(PathBuf::from(non_empty_command_flag_value(
                    "memory", "--config", value,
                )?));
            }
            "--limit" => {
                let value = iter.next().ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory --limit requires a value".to_string(),
                })?;
                let value = non_empty_command_flag_value("memory", "--limit", value)?;
                self.limit =
                    value
                        .parse::<usize>()
                        .map_err(|_| HarnessError::MemoryUnavailable {
                            message: "memory --limit must be a positive integer between 1 and 1000"
                                .to_string(),
                        })?;
                if !(1..=1000).contains(&self.limit) {
                    return Err(HarnessError::MemoryUnavailable {
                        message: "memory --limit must be a positive integer between 1 and 1000"
                            .to_string(),
                    });
                }
            }
            "--kind" => {
                let value = iter.next().ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory --kind requires a value".to_string(),
                })?;
                self.kind =
                    Some(non_empty_command_flag_value("memory", "--kind", value)?.to_string());
            }
            "--id" => {
                let value = iter.next().ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory --id requires a value".to_string(),
                })?;
                self.id = Some(non_empty_command_flag_value("memory", "--id", value)?.to_string());
            }
            "--reason" => {
                let value = iter.next().ok_or_else(|| HarnessError::MemoryUnavailable {
                    message: "memory --reason requires a value".to_string(),
                })?;
                self.reason =
                    Some(non_empty_command_flag_value("memory", "--reason", value)?.to_string());
            }
            "--dry-run" => self.dry_run = true,
            "-h" | "--help" => {
                return Err(HarnessError::MemoryUnavailable {
                    message: memory_usage(),
                });
            }
            unknown if unknown.starts_with('-') => {
                return Err(HarnessError::MemoryUnavailable {
                    message: format!("unknown memory flag `{unknown}`"),
                });
            }
            unknown => {
                return Err(HarnessError::MemoryUnavailable {
                    message: format!("unexpected memory argument `{unknown}`"),
                });
            }
        }
        Ok(())
    }

    fn validate(&self) -> Result<(), HarnessError> {
        match self.action {
            MemoryCommandAction::List => Ok(()),
            MemoryCommandAction::Show | MemoryCommandAction::Explain => {
                if self.id.is_none() {
                    return Err(HarnessError::MemoryUnavailable {
                        message: "memory show/explain requires --id <memory-id>".to_string(),
                    });
                }
                Ok(())
            }
            MemoryCommandAction::Revoke => {
                if self.id.is_none() {
                    return Err(HarnessError::MemoryUnavailable {
                        message: "memory revoke requires --id <memory-id>".to_string(),
                    });
                }
                if self.reason.is_none() {
                    return Err(HarnessError::MemoryUnavailable {
                        message: "memory revoke requires --reason <reason>".to_string(),
                    });
                }
                Ok(())
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum DraftsCommandAction {
    Status,
    List,
    Show,
    Next,
    Skip,
    Quarantine,
}

impl DraftsCommandAction {
    fn parse(value: &str) -> Result<Self, HarnessError> {
        match value {
            "status" => Ok(Self::Status),
            "list" => Ok(Self::List),
            "show" => Ok(Self::Show),
            "next" => Ok(Self::Next),
            "skip" => Ok(Self::Skip),
            "quarantine" => Ok(Self::Quarantine),
            _ => Err(HarnessError::RemoteSyncUnavailable {
                message: format!(
                    "unknown drafts action `{value}`; expected status, list, show, next, skip, or quarantine"
                ),
            }),
        }
    }
}

#[derive(Debug, Clone)]
struct DraftsCommandOptions {
    action: DraftsCommandAction,
    project_root: PathBuf,
    project_root_source: ProjectRootSource,
    config_path: Option<PathBuf>,
    drafts_dir: Option<PathBuf>,
    state: Option<DraftState>,
    id: Option<String>,
    reason: Option<String>,
}

impl DraftsCommandOptions {
    fn parse(args: &[String], cwd: &Path) -> Result<Self, HarnessError> {
        let Some(action_arg) = args.first() else {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: drafts_usage(),
            });
        };
        if action_arg == "-h" || action_arg == "--help" {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: drafts_usage(),
            });
        }
        let action = DraftsCommandAction::parse(action_arg)?;
        let mut options = Self::new(action, cwd);

        let mut iter = args[1..].iter();
        while let Some(arg) = iter.next() {
            options.parse_arg(arg, &mut iter)?;
        }
        options.validate()?;
        Ok(options)
    }

    fn new(action: DraftsCommandAction, cwd: &Path) -> Self {
        Self {
            action,
            project_root: cwd.to_path_buf(),
            project_root_source: ProjectRootSource::Cwd,
            config_path: None,
            drafts_dir: None,
            state: None,
            id: None,
            reason: None,
        }
    }

    fn parse_arg(
        &mut self,
        arg: &str,
        iter: &mut std::slice::Iter<'_, String>,
    ) -> Result<(), HarnessError> {
        match arg {
            "--project-root" => {
                self.project_root_source = ProjectRootSource::Flag;
                self.project_root =
                    PathBuf::from(take_drafts_option_value(iter, "--project-root")?);
            }
            "--config" => {
                self.config_path = Some(PathBuf::from(take_drafts_option_value(iter, "--config")?));
            }
            "--drafts-dir" => {
                self.drafts_dir = Some(PathBuf::from(take_drafts_option_value(
                    iter,
                    "--drafts-dir",
                )?));
            }
            "--state" => {
                self.state = Some(parse_draft_state(&take_drafts_option_value(
                    iter, "--state",
                )?)?);
            }
            "--reason" => {
                self.reason = Some(take_drafts_option_value(iter, "--reason")?);
            }
            "-h" | "--help" => {
                return Err(HarnessError::RemoteSyncUnavailable {
                    message: drafts_usage(),
                });
            }
            unknown if unknown.starts_with('-') => {
                return Err(HarnessError::RemoteSyncUnavailable {
                    message: format!("unknown drafts flag `{unknown}`"),
                });
            }
            value => self.parse_positional(value)?,
        }
        Ok(())
    }

    fn parse_positional(&mut self, value: &str) -> Result<(), HarnessError> {
        if !matches!(
            self.action,
            DraftsCommandAction::Show | DraftsCommandAction::Skip | DraftsCommandAction::Quarantine
        ) {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: format!("unexpected drafts argument `{value}`"),
            });
        }
        if self.id.is_some() {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: format!("unexpected drafts argument `{value}`"),
            });
        }
        self.id = Some(value.to_string());
        Ok(())
    }

    fn validate(&self) -> Result<(), HarnessError> {
        if self.action == DraftsCommandAction::Show && self.id.is_none() {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: "drafts show requires a draft id".to_string(),
            });
        }
        if self.action == DraftsCommandAction::Skip && self.id.is_none() {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: "drafts skip requires a draft id".to_string(),
            });
        }
        if self.action == DraftsCommandAction::Quarantine && self.id.is_none() {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: "drafts quarantine requires a draft id".to_string(),
            });
        }
        if self.action == DraftsCommandAction::Skip && self.reason.is_none() {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: "drafts skip requires --reason".to_string(),
            });
        }
        if self.action == DraftsCommandAction::Quarantine && self.reason.is_none() {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: "drafts quarantine requires --reason".to_string(),
            });
        }
        if !matches!(
            self.action,
            DraftsCommandAction::Skip | DraftsCommandAction::Quarantine
        ) && self.reason.is_some()
        {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: "drafts --reason is only supported for skip or quarantine".to_string(),
            });
        }
        Ok(())
    }
}

fn take_drafts_option_value(
    iter: &mut std::slice::Iter<'_, String>,
    flag: &str,
) -> Result<String, HarnessError> {
    let value = iter
        .next()
        .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
            message: format!("drafts {flag} requires a value"),
        })?;
    Ok(non_empty_command_flag_value("drafts", flag, value)?.to_string())
}

fn parse_draft_state(value: &str) -> Result<DraftState, HarnessError> {
    match value {
        "pending" => Ok(DraftState::Pending),
        "processing" => Ok(DraftState::Processing),
        "accepted" => Ok(DraftState::Accepted),
        "skipped" => Ok(DraftState::Skipped),
        "failed" => Ok(DraftState::Failed),
        "quarantined" => Ok(DraftState::Quarantined),
        _ => Err(HarnessError::RemoteSyncUnavailable {
            message: format!(
                "unknown draft state `{value}`; expected pending, processing, accepted, skipped, failed, or quarantined"
            ),
        }),
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum RemoteCommandAction {
    Status,
    Push,
    Pull,
    Drill,
}

impl RemoteCommandAction {
    fn parse(value: &str) -> Result<Self, HarnessError> {
        match value {
            "status" => Ok(Self::Status),
            "push" => Ok(Self::Push),
            "pull" => Ok(Self::Pull),
            "drill" => Ok(Self::Drill),
            _ => Err(HarnessError::RemoteSyncUnavailable {
                message: format!(
                    "unknown remote action `{value}`; expected status, push, pull, or drill"
                ),
            }),
        }
    }
}

#[derive(Debug, Clone)]
struct RemoteCommandOptions {
    action: RemoteCommandAction,
    project_root: PathBuf,
    project_root_source: ProjectRootSource,
    config_path: Option<PathBuf>,
    dry_run: bool,
    refresh: bool,
    destructive: bool,
}

impl RemoteCommandOptions {
    fn parse(args: &[String], cwd: &Path) -> Result<Self, HarnessError> {
        let Some(action_arg) = args.first() else {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: remote_usage(),
            });
        };
        if action_arg == "-h" || action_arg == "--help" {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: remote_usage(),
            });
        }
        let action = RemoteCommandAction::parse(action_arg)?;
        let mut project_root = cwd.to_path_buf();
        let mut project_root_source = ProjectRootSource::Cwd;
        let mut config_path = None;
        let mut dry_run = false;
        let mut refresh = false;
        let mut destructive = false;

        let mut iter = args[1..].iter();
        while let Some(arg) = iter.next() {
            match arg.as_str() {
                "--project-root" => {
                    project_root_source = ProjectRootSource::Flag;
                    let value = iter
                        .next()
                        .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                            message: "remote --project-root requires a value".to_string(),
                        })?;
                    project_root =
                        PathBuf::from(non_empty_remote_flag_value("--project-root", value)?);
                }
                "--config" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| HarnessError::RemoteSyncUnavailable {
                            message: "remote --config requires a value".to_string(),
                        })?;
                    config_path = Some(PathBuf::from(non_empty_remote_flag_value(
                        "--config", value,
                    )?));
                }
                "--dry-run" => dry_run = true,
                "--refresh" => refresh = true,
                "--destructive" => destructive = true,
                "-h" | "--help" => {
                    return Err(HarnessError::RemoteSyncUnavailable {
                        message: remote_usage(),
                    });
                }
                unknown if unknown.starts_with('-') => {
                    return Err(HarnessError::RemoteSyncUnavailable {
                        message: format!("unknown remote flag `{unknown}`"),
                    });
                }
                unknown => {
                    return Err(HarnessError::RemoteSyncUnavailable {
                        message: format!("unexpected remote argument `{unknown}`"),
                    });
                }
            }
        }

        if refresh && action != RemoteCommandAction::Status {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: "remote --refresh is only valid with status".to_string(),
            });
        }
        if destructive && action != RemoteCommandAction::Drill {
            return Err(HarnessError::RemoteSyncUnavailable {
                message: "remote --destructive is only valid with drill".to_string(),
            });
        }

        Ok(Self {
            action,
            project_root,
            project_root_source,
            config_path,
            dry_run,
            refresh,
            destructive,
        })
    }
}

fn non_empty_remote_flag_value<'a>(flag: &str, value: &'a str) -> Result<&'a str, HarnessError> {
    non_empty_command_flag_value("remote", flag, value)
}

fn non_empty_command_flag_value<'a>(
    command: &str,
    flag: &str,
    value: &'a str,
) -> Result<&'a str, HarnessError> {
    let value = value.trim();
    if value.is_empty() {
        return Err(HarnessError::RemoteSyncUnavailable {
            message: format!("{command} {flag} cannot be empty"),
        });
    }
    Ok(value)
}

#[derive(Debug, Clone)]
struct ConfigInitOptions {
    path: Option<PathBuf>,
    project_root: PathBuf,
    data_root: String,
    drafts_dir: Option<String>,
    operator: String,
    organization: String,
    remote_kind: String,
    remote_url: String,
    remote_branch: String,
    librarian_after_capture: String,
    dry_run: bool,
}

impl ConfigInitOptions {
    fn parse(args: &[String], cwd: &Path) -> Result<Self, String> {
        let mut options = Self {
            path: None,
            project_root: cwd.to_path_buf(),
            data_root: "state".to_string(),
            drafts_dir: None,
            operator: String::new(),
            organization: String::new(),
            remote_kind: "git".to_string(),
            remote_url: String::new(),
            remote_branch: "main".to_string(),
            librarian_after_capture: "process".to_string(),
            dry_run: false,
        };

        let mut iter = args.iter();
        while let Some(arg) = iter.next() {
            match arg.as_str() {
                "--path" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "config init --path requires a value".to_string())?;
                    options.path = Some(PathBuf::from(non_empty_flag_value("--path", value)?));
                }
                "--project-root" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "config init --project-root requires a value".to_string())?;
                    options.project_root =
                        PathBuf::from(non_empty_flag_value("--project-root", value)?);
                }
                "--data-root" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "config init --data-root requires a value".to_string())?;
                    options.data_root = non_empty_flag_value("--data-root", value)?.to_string();
                }
                "--drafts-dir" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "config init --drafts-dir requires a value".to_string())?;
                    options.drafts_dir =
                        Some(non_empty_flag_value("--drafts-dir", value)?.to_string());
                }
                "--operator" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "config init --operator requires a value".to_string())?;
                    options.operator = non_empty_flag_value("--operator", value)?.to_string();
                }
                "--organization" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "config init --organization requires a value".to_string())?;
                    options.organization =
                        non_empty_flag_value("--organization", value)?.to_string();
                }
                "--remote-kind" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "config init --remote-kind requires a value".to_string())?;
                    let value = non_empty_flag_value("--remote-kind", value)?;
                    validate_remote_kind(value)?;
                    options.remote_kind = value.to_string();
                }
                "--remote-url" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "config init --remote-url requires a value".to_string())?;
                    options.remote_url = non_empty_flag_value("--remote-url", value)?.to_string();
                }
                "--remote-branch" => {
                    let value = iter.next().ok_or_else(|| {
                        "config init --remote-branch requires a value".to_string()
                    })?;
                    options.remote_branch =
                        non_empty_flag_value("--remote-branch", value)?.to_string();
                }
                "--librarian-after-capture" => {
                    let value = iter.next().ok_or_else(|| {
                        "config init --librarian-after-capture requires a value".to_string()
                    })?;
                    let value = non_empty_flag_value("--librarian-after-capture", value)?;
                    options.librarian_after_capture =
                        normalize_librarian_after_capture(value)?.to_string();
                }
                "--dry-run" => options.dry_run = true,
                "-h" | "--help" => return Err(config_usage()),
                unknown if unknown.starts_with('-') => {
                    return Err(format!("unknown config init flag `{unknown}`"));
                }
                unknown => return Err(format!("unexpected config init argument `{unknown}`")),
            }
        }

        validate_remote_kind(&options.remote_kind)?;
        options.librarian_after_capture =
            normalize_librarian_after_capture(&options.librarian_after_capture)?.to_string();
        Ok(options)
    }

    fn config_path(&self) -> PathBuf {
        self.path
            .clone()
            .unwrap_or_else(|| self.project_root.join(".mimir").join("config.toml"))
    }
}

fn config_init_command(args: &[String], cwd: &Path) -> Result<String, String> {
    let options = ConfigInitOptions::parse(args, cwd)?;
    let path = options.config_path();
    let config = render_config_init_toml(&options);
    if !options.dry_run {
        if path.exists() {
            return Err(format!(
                "refusing to overwrite existing config: {}",
                path.display()
            ));
        }
        if let Some(parent) = path
            .parent()
            .filter(|parent| !parent.as_os_str().is_empty())
        {
            fs::create_dir_all(parent)
                .map_err(|err| format!("failed to create {}: {err}", parent.display()))?;
        }
        fs::write(&path, &config)
            .map_err(|err| format!("failed to write {}: {err}", path.display()))?;
    }

    let mut output = String::new();
    output.push_str("mode=");
    output.push_str(if options.dry_run {
        "dry-run"
    } else {
        "written"
    });
    output.push('\n');
    output.push_str("path=");
    output.push_str(&path.display().to_string());
    output.push('\n');
    if options.dry_run {
        output.push_str(&config);
    }
    Ok(output)
}

fn render_config_init_toml(options: &ConfigInitOptions) -> String {
    let mut text = String::new();
    text.push_str("[storage]\n");
    text.push_str("data_root = ");
    text.push_str(&toml_string_literal(&options.data_root));
    text.push_str("\n\n");
    if let Some(drafts_dir) = &options.drafts_dir {
        text.push_str("[drafts]\n");
        text.push_str("dir = ");
        text.push_str(&toml_string_literal(drafts_dir));
        text.push_str("\n\n");
    }
    text.push_str("[native_memory]\n");
    text.push_str("claude = []\n");
    text.push_str("codex = []\n\n");
    text.push_str("[remote]\n");
    text.push_str("kind = ");
    text.push_str(&toml_string_literal(&options.remote_kind));
    text.push('\n');
    text.push_str("url = ");
    text.push_str(&toml_string_literal(&options.remote_url));
    text.push('\n');
    text.push_str("branch = ");
    text.push_str(&toml_string_literal(&options.remote_branch));
    text.push_str("\nauto_push_after_capture = false\n\n");
    text.push_str("[librarian]\n");
    text.push_str("after_capture = ");
    text.push_str(&toml_string_literal(&options.librarian_after_capture));
    text.push_str("\n\n");
    text.push_str("[identity]\n");
    text.push_str("operator = ");
    text.push_str(&toml_string_literal(&options.operator));
    text.push('\n');
    text.push_str("organization = ");
    text.push_str(&toml_string_literal(&options.organization));
    text.push('\n');
    text
}

fn non_empty_flag_value<'a>(flag: &str, value: &'a str) -> Result<&'a str, String> {
    let value = value.trim();
    if value.is_empty() {
        return Err(format!("config init {flag} cannot be empty"));
    }
    Ok(value)
}

fn validate_remote_kind(value: &str) -> Result<(), String> {
    match value {
        "git" | "service" => Ok(()),
        _ => Err(format!(
            "config init --remote-kind `{value}` is invalid; expected git or service"
        )),
    }
}

fn normalize_librarian_after_capture(value: &str) -> Result<&'static str, String> {
    match value {
        "off" => Ok("off"),
        "defer" => Ok("defer"),
        "archive_raw" | "archive-raw" => Ok("archive_raw"),
        "process" => Ok("process"),
        _ => Err(format!(
            "config init --librarian-after-capture `{value}` is invalid; expected off, defer, archive_raw, or process"
        )),
    }
}

fn toml_string_literal(value: &str) -> String {
    let mut literal = String::from("\"");
    for ch in value.chars() {
        match ch {
            '\\' => literal.push_str("\\\\"),
            '"' => literal.push_str("\\\""),
            '\n' => literal.push_str("\\n"),
            '\r' => literal.push_str("\\r"),
            '\t' => literal.push_str("\\t"),
            other => literal.push(other),
        }
    }
    literal.push('"');
    literal
}

fn hook_context_output(env: &BTreeMap<String, String>) -> String {
    if env.get("MIMIR_HARNESS").map(String::as_str) != Some("1") {
        return String::new();
    }

    let agent = env.get("MIMIR_AGENT").map_or("agent", String::as_str);
    let checkpoint = env
        .get("MIMIR_CHECKPOINT_COMMAND")
        .map_or("mimir checkpoint", String::as_str);
    let mut output = String::new();
    output.push_str("Mimir wrapper active for ");
    output.push_str(agent);
    output.push_str(". Use `");
    output.push_str(checkpoint);
    output.push_str(" --title \"Short title\" \"Memory note\"` for durable session memories. ");
    output.push_str("Do not write canonical Mimir memory directly; checkpoint notes are untrusted librarian drafts.");
    match env
        .get("MIMIR_SESSION_DRAFTS_DIR")
        .filter(|value| !value.trim().is_empty())
    {
        Some(path) => {
            output.push_str(" Checkpoint route: ready at ");
            output.push_str(path);
            output.push('.');
        }
        None => output.push_str(
            " Checkpoint route: missing MIMIR_SESSION_DRAFTS_DIR; checkpoint capture will not work until the session is launched through Mimir.",
        ),
    }
    output.push_str(" Before compaction or a long handoff, capture durable decisions, unresolved blockers, and setup changes with the checkpoint command.");

    if let Some(path) = env
        .get("MIMIR_AGENT_GUIDE_PATH")
        .filter(|value| !value.trim().is_empty())
    {
        output.push_str(" Guide: ");
        output.push_str(path);
        output.push('.');
    }
    if let Some(path) = env
        .get("MIMIR_AGENT_SETUP_DIR")
        .filter(|value| !value.trim().is_empty())
    {
        output.push_str(" Native setup artifacts: ");
        output.push_str(path);
        output.push('.');
    }
    if env.get("MIMIR_BOOTSTRAP").map(String::as_str) == Some("required") {
        output.push_str(" First-run setup is pending; read MIMIR_BOOTSTRAP_GUIDE_PATH before assuming governed memory is active.");
    }
    output.push('\n');
    output
}

fn run_checkpoint_command(args: &[String]) -> ExitCode {
    let env: BTreeMap<String, String> = std::env::vars().collect();
    match checkpoint_command(args, &env) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn run_setup_agent_command(args: &[String]) -> ExitCode {
    if matches!(args, [flag] if flag == "-h" || flag == "--help") {
        println!("{}", setup_agent_usage());
        return ExitCode::SUCCESS;
    }
    let env: BTreeMap<String, String> = std::env::vars().collect();
    let cwd = match std::env::current_dir() {
        Ok(path) => path,
        Err(err) => {
            eprintln!("mimir: failed to detect current directory: {err}");
            return ExitCode::from(2);
        }
    };
    match setup_agent_command(args, &env, &cwd) {
        Ok(output) => {
            print!("{output}");
            ExitCode::SUCCESS
        }
        Err(err) => {
            eprintln!("mimir: {err}");
            ExitCode::from(2)
        }
    }
}

fn setup_agent_command(
    args: &[String],
    env: &BTreeMap<String, String>,
    cwd: &Path,
) -> Result<String, String> {
    let options = SetupAgentOptions::parse(args, env, cwd)?;
    if !options.dry_run {
        match options.action {
            SetupAgentAction::Status | SetupAgentAction::Doctor => {}
            SetupAgentAction::Install => install_agent_setup(&options)?,
            SetupAgentAction::Remove => remove_agent_setup(&options)?,
        }
    }
    Ok(if options.action == SetupAgentAction::Doctor {
        render_setup_agent_doctor(&options)
    } else {
        render_setup_agent_status(&options)
    })
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SetupAgentAction {
    Status,
    Doctor,
    Install,
    Remove,
}

impl SetupAgentAction {
    fn parse(value: &str) -> Result<Self, String> {
        match value {
            "status" => Ok(Self::Status),
            "doctor" => Ok(Self::Doctor),
            "install" => Ok(Self::Install),
            "remove" => Ok(Self::Remove),
            _ => Err(format!(
                "unknown setup-agent action `{value}`; expected status, doctor, install, or remove"
            )),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SetupAgentKind {
    Claude,
    Codex,
}

impl SetupAgentKind {
    fn parse(value: &str) -> Result<Self, String> {
        match value {
            "claude" => Ok(Self::Claude),
            "codex" => Ok(Self::Codex),
            _ => Err(format!(
                "unknown setup-agent --agent `{value}`; expected claude or codex"
            )),
        }
    }

    fn as_str(self) -> &'static str {
        match self {
            Self::Claude => "claude",
            Self::Codex => "codex",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SetupAgentScope {
    Project,
    User,
}

impl SetupAgentScope {
    fn parse(value: &str) -> Result<Self, String> {
        match value {
            "project" => Ok(Self::Project),
            "user" => Ok(Self::User),
            _ => Err(format!(
                "unknown setup-agent --scope `{value}`; expected project or user"
            )),
        }
    }

    fn as_str(self) -> &'static str {
        match self {
            Self::Project => "project",
            Self::User => "user",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct SetupFeatures {
    skill: bool,
    hook: bool,
}

impl SetupFeatures {
    const ALL: Self = Self {
        skill: true,
        hook: true,
    };

    fn parse(value: &str) -> Result<Self, String> {
        let mut features = Self {
            skill: false,
            hook: false,
        };
        for part in value.split(',') {
            match part.trim() {
                "all" => features = Self::ALL,
                "skill" | "skills" => features.skill = true,
                "hook" | "hooks" => features.hook = true,
                "" => {}
                unknown => {
                    return Err(format!(
                        "unknown setup-agent --features value `{unknown}`; expected all, skill, or hook"
                    ));
                }
            }
        }
        if !features.skill && !features.hook {
            return Err("setup-agent --features must select at least one feature".to_string());
        }
        Ok(features)
    }
}

#[derive(Debug, Clone)]
struct SetupAgentOptions {
    action: SetupAgentAction,
    agent: SetupAgentKind,
    scope: SetupAgentScope,
    features: SetupFeatures,
    dry_run: bool,
    setup_dir: Option<PathBuf>,
    project_root: PathBuf,
    home_dir: PathBuf,
}

impl SetupAgentOptions {
    fn parse(args: &[String], env: &BTreeMap<String, String>, cwd: &Path) -> Result<Self, String> {
        let Some(action_arg) = args.first() else {
            return Err(setup_agent_usage());
        };
        if action_arg == "-h" || action_arg == "--help" {
            return Err(setup_agent_usage());
        }
        let action = SetupAgentAction::parse(action_arg)?;
        let mut agent = None;
        let mut scope = None;
        let mut features = SetupFeatures::ALL;
        let mut dry_run = false;
        let mut setup_dir = env
            .get("MIMIR_AGENT_SETUP_DIR")
            .filter(|value| !value.trim().is_empty())
            .map(PathBuf::from);
        let mut project_root = cwd.to_path_buf();
        let mut home_dir = env
            .get("HOME")
            .filter(|value| !value.trim().is_empty())
            .map_or_else(|| cwd.to_path_buf(), PathBuf::from);

        let mut iter = args[1..].iter();
        while let Some(arg) = iter.next() {
            match arg.as_str() {
                "--agent" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "setup-agent --agent requires a value".to_string())?;
                    agent = Some(SetupAgentKind::parse(value)?);
                }
                "--scope" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "setup-agent --scope requires a value".to_string())?;
                    scope = Some(SetupAgentScope::parse(value)?);
                }
                "--features" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "setup-agent --features requires a value".to_string())?;
                    features = SetupFeatures::parse(value)?;
                }
                "--from" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "setup-agent --from requires a value".to_string())?;
                    setup_dir = Some(PathBuf::from(value));
                }
                "--project-root" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "setup-agent --project-root requires a value".to_string())?;
                    project_root = PathBuf::from(value);
                }
                "--home" => {
                    let value = iter
                        .next()
                        .ok_or_else(|| "setup-agent --home requires a value".to_string())?;
                    home_dir = PathBuf::from(value);
                }
                "--dry-run" => dry_run = true,
                "-h" | "--help" => return Err(setup_agent_usage()),
                unknown if unknown.starts_with('-') => {
                    return Err(format!("unknown setup-agent flag `{unknown}`"));
                }
                unknown => return Err(format!("unexpected setup-agent argument `{unknown}`")),
            }
        }

        let agent = agent.ok_or_else(|| "setup-agent requires --agent claude|codex".to_string())?;
        let scope = scope.ok_or_else(|| "setup-agent requires --scope project|user".to_string())?;
        if action == SetupAgentAction::Install && setup_dir.is_none() {
            return Err(
                "setup-agent install requires --from <MIMIR_AGENT_SETUP_DIR> or MIMIR_AGENT_SETUP_DIR"
                    .to_string(),
            );
        }

        Ok(Self {
            action,
            agent,
            scope,
            features,
            dry_run,
            setup_dir,
            project_root,
            home_dir,
        })
    }

    fn root(&self) -> &Path {
        match self.scope {
            SetupAgentScope::Project => &self.project_root,
            SetupAgentScope::User => &self.home_dir,
        }
    }

    fn skill_source_dir(&self) -> Result<PathBuf, String> {
        let setup_dir = self
            .setup_dir
            .as_ref()
            .ok_or_else(|| "MIMIR_AGENT_SETUP_DIR is not set".to_string())?;
        Ok(setup_dir
            .join(self.agent.as_str())
            .join("skills")
            .join("mimir-checkpoint"))
    }

    fn skill_target_dir(&self) -> PathBuf {
        match self.agent {
            SetupAgentKind::Claude => self
                .root()
                .join(".claude")
                .join("skills")
                .join("mimir-checkpoint"),
            SetupAgentKind::Codex => self
                .root()
                .join(".agents")
                .join("skills")
                .join("mimir-checkpoint"),
        }
    }

    fn hook_json_path(&self) -> PathBuf {
        match self.agent {
            SetupAgentKind::Claude => self.root().join(".claude").join("settings.json"),
            SetupAgentKind::Codex => self.root().join(".codex").join("hooks.json"),
        }
    }

    fn codex_config_path(&self) -> Option<PathBuf> {
        (self.agent == SetupAgentKind::Codex)
            .then(|| self.root().join(".codex").join("config.toml"))
    }
}

fn setup_agent_usage() -> String {
    "Usage: mimir setup-agent <status|doctor|install|remove> --agent <claude|codex> --scope <project|user> [--features all|skill|hook] [--from <dir>] [--project-root <dir>] [--home <dir>] [--dry-run]"
        .to_string()
}

fn install_agent_setup(options: &SetupAgentOptions) -> Result<(), String> {
    if options.features.hook {
        validate_agent_hook_install(options)?;
    }
    if options.features.skill {
        install_agent_skill(options)?;
    }
    if options.features.hook {
        install_agent_hook(options)?;
    }
    Ok(())
}

fn remove_agent_setup(options: &SetupAgentOptions) -> Result<(), String> {
    if options.features.skill {
        remove_agent_skill(options)?;
    }
    if options.features.hook {
        remove_agent_hook(options)?;
    }
    Ok(())
}

fn install_agent_skill(options: &SetupAgentOptions) -> Result<(), String> {
    let source = options.skill_source_dir()?;
    let target = options.skill_target_dir();
    if !source.join("SKILL.md").is_file() {
        return Err(format!(
            "setup skill source is missing: {}",
            source.join("SKILL.md").display()
        ));
    }
    if target.exists() {
        if skill_files_match(&source, &target)? {
            return Ok(());
        }
        return Err(format!(
            "target skill already exists and differs: {}; remove it first",
            target.display()
        ));
    }
    copy_dir_recursive(&source, &target)
}

fn remove_agent_skill(options: &SetupAgentOptions) -> Result<(), String> {
    let target = options.skill_target_dir();
    if target.exists() {
        if !skill_target_owned_by_mimir(&target)? {
            return Err(format!(
                "refusing to remove non-Mimir skill target: {}",
                target.display()
            ));
        }
        fs::remove_dir_all(&target)
            .map_err(|source| format!("failed to remove {}: {source}", target.display()))?;
    }
    Ok(())
}

fn skill_target_owned_by_mimir(target: &Path) -> Result<bool, String> {
    let path = target.join("SKILL.md");
    if !path.exists() {
        return Ok(false);
    }
    let text = fs::read_to_string(&path)
        .map_err(|err| format!("failed to read {}: {err}", path.display()))?;
    Ok(text
        .lines()
        .map(str::trim)
        .any(|line| line == "name: mimir-checkpoint"))
}

fn skill_files_match(source: &Path, target: &Path) -> Result<bool, String> {
    let source_text = fs::read(source.join("SKILL.md"))
        .map_err(|err| format!("failed to read source skill: {err}"))?;
    let target_text = fs::read(target.join("SKILL.md"))
        .map_err(|err| format!("failed to read target skill: {err}"))?;
    Ok(source_text == target_text)
}

fn copy_dir_recursive(source: &Path, target: &Path) -> Result<(), String> {
    fs::create_dir_all(target)
        .map_err(|err| format!("failed to create {}: {err}", target.display()))?;
    for entry in
        fs::read_dir(source).map_err(|err| format!("failed to read {}: {err}", source.display()))?
    {
        let entry = entry.map_err(|err| format!("failed to read directory entry: {err}"))?;
        let source_path = entry.path();
        let target_path = target.join(entry.file_name());
        let file_type = entry
            .file_type()
            .map_err(|err| format!("failed to inspect {}: {err}", source_path.display()))?;
        if file_type.is_dir() {
            copy_dir_recursive(&source_path, &target_path)?;
        } else if file_type.is_file() {
            fs::copy(&source_path, &target_path).map_err(|err| {
                format!(
                    "failed to copy {} to {}: {err}",
                    source_path.display(),
                    target_path.display()
                )
            })?;
        }
    }
    Ok(())
}

fn install_agent_hook(options: &SetupAgentOptions) -> Result<(), String> {
    validate_agent_hook_install(options)?;
    let hook_path = options.hook_json_path();
    upsert_mimir_hook(&hook_path, options.agent)?;
    if options.agent == SetupAgentKind::Codex {
        if let Some(config_path) = options.codex_config_path() {
            ensure_codex_hooks_feature(&config_path)?;
        }
    }
    Ok(())
}

fn validate_agent_hook_install(options: &SetupAgentOptions) -> Result<(), String> {
    if options.agent == SetupAgentKind::Codex {
        if let Some(config_path) = options.codex_config_path() {
            validate_codex_hooks_not_disabled(&config_path)?;
        }
    }
    Ok(())
}

fn remove_agent_hook(options: &SetupAgentOptions) -> Result<(), String> {
    let hook_path = options.hook_json_path();
    if hook_path.exists() {
        remove_mimir_hook(&hook_path)?;
    }
    Ok(())
}

fn validate_codex_hooks_not_disabled(path: &Path) -> Result<(), String> {
    if !path.exists() {
        return Ok(());
    }
    let text = fs::read_to_string(path)
        .map_err(|err| format!("failed to read {}: {err}", path.display()))?;
    if codex_hooks_feature_disabled(&text) {
        return Err(format!(
            "codex hooks are explicitly disabled in {}; set codex_hooks = true before installing the Mimir hook",
            path.display()
        ));
    }
    Ok(())
}

fn upsert_mimir_hook(path: &Path, agent: SetupAgentKind) -> Result<bool, String> {
    let mut value = read_hook_json(path)?;
    let missing_specs = required_mimir_hook_specs(agent)
        .iter()
        .copied()
        .filter(|spec| !event_has_mimir_hook(&value, spec.event))
        .collect::<Vec<_>>();
    if missing_specs.is_empty() {
        return Ok(false);
    }
    let root = value
        .as_object_mut()
        .ok_or_else(|| format!("hook file must be a JSON object: {}", path.display()))?;
    let hooks = root
        .entry("hooks")
        .or_insert_with(|| serde_json::Value::Object(serde_json::Map::new()))
        .as_object_mut()
        .ok_or_else(|| format!("hook file `hooks` must be an object: {}", path.display()))?;
    for spec in missing_specs {
        let event_hooks = hooks
            .entry(spec.event)
            .or_insert_with(|| serde_json::Value::Array(Vec::new()))
            .as_array_mut()
            .ok_or_else(|| {
                format!(
                    "hook file `hooks.{}` must be an array: {}",
                    spec.event,
                    path.display()
                )
            })?;
        event_hooks.push(mimir_hook_group(spec.matcher));
    }
    write_pretty_json(path, &value)?;
    Ok(true)
}

fn remove_mimir_hook(path: &Path) -> Result<(), String> {
    let mut value = read_hook_json(path)?;
    let mut changed = false;
    let Some(hooks) = value
        .get_mut("hooks")
        .and_then(serde_json::Value::as_object_mut)
    else {
        return Ok(());
    };
    let event_names = hooks.keys().cloned().collect::<Vec<_>>();
    for event_name in event_names {
        let Some(event_hooks) = hooks
            .get_mut(&event_name)
            .and_then(serde_json::Value::as_array_mut)
        else {
            continue;
        };
        for group in event_hooks.iter_mut() {
            if let Some(handlers) = group
                .get_mut("hooks")
                .and_then(serde_json::Value::as_array_mut)
            {
                let before = handlers.len();
                handlers.retain(|handler| !is_mimir_hook_handler(handler));
                changed |= handlers.len() != before;
            }
        }
        let before_groups = event_hooks.len();
        event_hooks.retain(|group| {
            group
                .get("hooks")
                .and_then(serde_json::Value::as_array)
                .is_none_or(|hooks| !hooks.is_empty())
        });
        changed |= event_hooks.len() != before_groups;
        if event_hooks.is_empty() {
            hooks.remove(&event_name);
        }
    }
    if changed {
        write_pretty_json(path, &value)?;
    }
    Ok(())
}

fn read_hook_json(path: &Path) -> Result<serde_json::Value, String> {
    if !path.exists() {
        return Ok(serde_json::json!({}));
    }
    let text = fs::read_to_string(path)
        .map_err(|err| format!("failed to read {}: {err}", path.display()))?;
    serde_json::from_str(&text).map_err(|err| format!("failed to parse {}: {err}", path.display()))
}

fn write_pretty_json(path: &Path, value: &serde_json::Value) -> Result<(), String> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)
            .map_err(|err| format!("failed to create {}: {err}", parent.display()))?;
    }
    let text = serde_json::to_string_pretty(value)
        .map_err(|err| format!("failed to render {}: {err}", path.display()))?;
    fs::write(path, format!("{text}\n"))
        .map_err(|err| format!("failed to write {}: {err}", path.display()))
}

#[derive(Debug, Clone, Copy)]
struct MimirHookSpec {
    event: &'static str,
    matcher: &'static str,
}

const CODEX_MIMIR_HOOK_SPECS: &[MimirHookSpec] = &[MimirHookSpec {
    event: "SessionStart",
    matcher: "startup|resume",
}];

const CLAUDE_MIMIR_HOOK_SPECS: &[MimirHookSpec] = &[
    MimirHookSpec {
        event: "SessionStart",
        matcher: "startup|resume|compact",
    },
    MimirHookSpec {
        event: "PreCompact",
        matcher: "manual|auto",
    },
];

fn required_mimir_hook_specs(agent: SetupAgentKind) -> &'static [MimirHookSpec] {
    match agent {
        SetupAgentKind::Claude => CLAUDE_MIMIR_HOOK_SPECS,
        SetupAgentKind::Codex => CODEX_MIMIR_HOOK_SPECS,
    }
}

fn mimir_hook_group(matcher: &'static str) -> serde_json::Value {
    serde_json::json!({
        "matcher": matcher,
        "hooks": [
            {
                "type": "command",
                "command": "mimir hook-context"
            }
        ]
    })
}

fn has_mimir_hook(value: &serde_json::Value, agent: SetupAgentKind) -> bool {
    required_mimir_hook_specs(agent)
        .iter()
        .all(|spec| event_has_mimir_hook(value, spec.event))
}

fn event_has_mimir_hook(value: &serde_json::Value, event: &str) -> bool {
    value
        .get("hooks")
        .and_then(|hooks| hooks.get(event))
        .and_then(serde_json::Value::as_array)
        .is_some_and(|groups| {
            groups.iter().any(|group| {
                group
                    .get("hooks")
                    .and_then(serde_json::Value::as_array)
                    .is_some_and(|handlers| handlers.iter().any(is_mimir_hook_handler))
            })
        })
}

fn missing_mimir_hook_reason(
    value: &serde_json::Value,
    agent: SetupAgentKind,
) -> Option<&'static str> {
    if !event_has_mimir_hook(value, "SessionStart") {
        return Some("mimir_session_start_hook_missing");
    }
    if agent == SetupAgentKind::Claude && !event_has_mimir_hook(value, "PreCompact") {
        return Some("mimir_precompact_hook_missing");
    }
    None
}

fn is_mimir_hook_handler(value: &serde_json::Value) -> bool {
    value.get("type").and_then(serde_json::Value::as_str) == Some("command")
        && value.get("command").and_then(serde_json::Value::as_str) == Some("mimir hook-context")
}

fn ensure_codex_hooks_feature(path: &Path) -> Result<(), String> {
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)
            .map_err(|err| format!("failed to create {}: {err}", parent.display()))?;
    }
    if !path.exists() {
        fs::write(path, "[features]\ncodex_hooks = true\n")
            .map_err(|err| format!("failed to write {}: {err}", path.display()))?;
        return Ok(());
    }

    let text = fs::read_to_string(path)
        .map_err(|err| format!("failed to read {}: {err}", path.display()))?;
    if codex_hooks_feature_enabled(&text) {
        return Ok(());
    }
    if codex_hooks_feature_disabled(&text) {
        return Err(format!(
            "codex hooks are explicitly disabled in {}; set codex_hooks = true before installing the Mimir hook",
            path.display()
        ));
    }
    let updated = insert_codex_hooks_feature(&text);
    fs::write(path, updated).map_err(|err| format!("failed to write {}: {err}", path.display()))
}

fn codex_hooks_feature_enabled(text: &str) -> bool {
    text.lines()
        .map(str::trim)
        .any(|line| line == "codex_hooks = true")
}

fn codex_hooks_feature_disabled(text: &str) -> bool {
    text.lines()
        .map(str::trim)
        .any(|line| line == "codex_hooks = false")
}

fn insert_codex_hooks_feature(text: &str) -> String {
    let mut lines: Vec<String> = text.lines().map(str::to_string).collect();
    if let Some(index) = lines.iter().position(|line| line.trim() == "[features]") {
        lines.insert(index + 1, "codex_hooks = true".to_string());
        let mut updated = lines.join("\n");
        updated.push('\n');
        return updated;
    }
    let mut updated = text.to_string();
    if !updated.ends_with('\n') {
        updated.push('\n');
    }
    updated.push_str("\n[features]\ncodex_hooks = true\n");
    updated
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum SetupSurfaceState {
    Installed,
    Missing,
    Partial,
}

impl SetupSurfaceState {
    fn as_str(self) -> &'static str {
        match self {
            Self::Installed => "installed",
            Self::Missing => "missing",
            Self::Partial => "partial",
        }
    }
}

#[derive(Debug, Clone)]
struct SetupSurfaceStatus {
    state: SetupSurfaceState,
    reason: &'static str,
    path: PathBuf,
    config_path: Option<PathBuf>,
}

fn render_setup_agent_status(options: &SetupAgentOptions) -> String {
    let mut output = String::new();
    output.push_str("agent=");
    output.push_str(options.agent.as_str());
    output.push('\n');
    output.push_str("scope=");
    output.push_str(options.scope.as_str());
    output.push('\n');
    if options.dry_run {
        output.push_str("mode=dry-run\n");
    }
    if options.features.skill {
        let status = skill_setup_status(options);
        output.push_str("skill=");
        output.push_str(status.state.as_str());
        output.push_str(" path=");
        output.push_str(&status.path.display().to_string());
        output.push_str(" reason=");
        output.push_str(status.reason);
        if let Some(action) = dry_run_action(options, status.state) {
            output.push_str(" action=");
            output.push_str(action);
        }
        output.push('\n');
    }
    if options.features.hook {
        let status = hook_setup_status(options);
        output.push_str("hook=");
        output.push_str(status.state.as_str());
        output.push_str(" path=");
        output.push_str(&status.path.display().to_string());
        output.push_str(" reason=");
        output.push_str(status.reason);
        if let Some(config_path) = status.config_path {
            output.push_str(" config_path=");
            output.push_str(&config_path.display().to_string());
        }
        if let Some(action) = dry_run_action(options, status.state) {
            output.push_str(" action=");
            output.push_str(action);
        }
        output.push('\n');
    }
    output
}

fn render_setup_agent_doctor(options: &SetupAgentOptions) -> String {
    let skill = options.features.skill.then(|| skill_setup_status(options));
    let hook = options.features.hook.then(|| hook_setup_status(options));
    let ready = skill
        .as_ref()
        .is_none_or(|status| status.state == SetupSurfaceState::Installed)
        && hook
            .as_ref()
            .is_none_or(|status| status.state == SetupSurfaceState::Installed);

    let mut output = render_setup_agent_status(options);
    output.push_str("doctor_status=");
    output.push_str(if ready { "ready" } else { "action_required" });
    output.push('\n');
    if let Some(config_path) = options.codex_config_path() {
        output.push_str("codex_config_status=");
        output.push_str(codex_config_feature_status(&config_path));
        output.push('\n');
    }
    output.push_str("status_command=");
    output.push_str(&setup_agent_command_line("status", options, None));
    output.push('\n');
    output.push_str("install_command=");
    output.push_str(&setup_agent_command_line(
        "install",
        options,
        options.setup_dir.as_deref(),
    ));
    output.push('\n');
    output.push_str("remove_command=");
    output.push_str(&setup_agent_command_line("remove", options, None));
    output.push('\n');
    output.push_str("context_command=mimir context");
    if options.scope == SetupAgentScope::Project {
        output.push_str(" --project-root ");
        output.push_str(&shell_path_arg(&options.project_root));
    }
    output.push('\n');
    output.push_str("checkpoint_command=");
    output.push_str("mimir checkpoint --title \"Short title\" \"Memory note\"");
    output.push('\n');
    output.push_str("next_action=");
    if ready {
        output.push_str("none");
    } else {
        output.push_str(&setup_agent_command_line(
            "install",
            options,
            options.setup_dir.as_deref(),
        ));
    }
    output.push('\n');
    output
}

fn setup_agent_command_line(
    action: &str,
    options: &SetupAgentOptions,
    setup_dir: Option<&Path>,
) -> String {
    let mut command = format!(
        "mimir setup-agent {action} --agent {} --scope {}",
        options.agent.as_str(),
        options.scope.as_str()
    );
    if action == "install" {
        command.push_str(" --from ");
        command.push_str(
            &setup_dir.map_or_else(|| "$MIMIR_AGENT_SETUP_DIR".to_string(), shell_path_arg),
        );
    }
    match options.scope {
        SetupAgentScope::Project => {
            command.push_str(" --project-root ");
            command.push_str(&shell_path_arg(&options.project_root));
        }
        SetupAgentScope::User => {
            command.push_str(" --home ");
            command.push_str(&shell_path_arg(&options.home_dir));
        }
    }
    command
}

fn shell_path_arg(path: &Path) -> String {
    let value = path.display().to_string();
    if value
        .chars()
        .all(|ch| ch.is_ascii_alphanumeric() || matches!(ch, '/' | '.' | '_' | '-' | ':' | '+'))
    {
        return value;
    }
    let escaped = value.replace('\'', "'\\''");
    format!("'{escaped}'")
}

fn codex_config_feature_status(path: &Path) -> &'static str {
    let Ok(text) = fs::read_to_string(path) else {
        return "missing";
    };
    if codex_hooks_feature_enabled(&text) {
        "enabled"
    } else if codex_hooks_feature_disabled(&text) {
        "disabled"
    } else {
        "missing"
    }
}

fn skill_setup_status(options: &SetupAgentOptions) -> SetupSurfaceStatus {
    let path = options.skill_target_dir();
    let skill_file = path.join("SKILL.md");
    let (state, reason) = if skill_file.is_file() {
        match skill_target_owned_by_mimir(&path) {
            Ok(true) => (SetupSurfaceState::Installed, "mimir_skill_present"),
            Ok(false) => (SetupSurfaceState::Partial, "non_mimir_skill_present"),
            Err(_) => (SetupSurfaceState::Partial, "skill_file_unreadable"),
        }
    } else if path.exists() {
        (
            SetupSurfaceState::Partial,
            "skill_directory_without_skill_file",
        )
    } else {
        (SetupSurfaceState::Missing, "skill_file_missing")
    };
    SetupSurfaceStatus {
        state,
        reason,
        path,
        config_path: None,
    }
}

fn hook_setup_status(options: &SetupAgentOptions) -> SetupSurfaceStatus {
    let path = options.hook_json_path();
    if !path.exists() {
        return SetupSurfaceStatus {
            state: SetupSurfaceState::Missing,
            reason: "hook_file_missing",
            path,
            config_path: options.codex_config_path(),
        };
    }
    let Ok(hook_value) = read_hook_json(&path) else {
        return SetupSurfaceStatus {
            state: SetupSurfaceState::Partial,
            reason: "hook_json_invalid",
            path,
            config_path: options.codex_config_path(),
        };
    };
    if !has_mimir_hook(&hook_value, options.agent) {
        let reason =
            missing_mimir_hook_reason(&hook_value, options.agent).unwrap_or("mimir_hook_missing");
        return SetupSurfaceStatus {
            state: if reason == "mimir_session_start_hook_missing" {
                SetupSurfaceState::Missing
            } else {
                SetupSurfaceState::Partial
            },
            reason,
            path,
            config_path: options.codex_config_path(),
        };
    }
    if let Some(config_path) = options.codex_config_path() {
        let Ok(text) = fs::read_to_string(&config_path) else {
            return SetupSurfaceStatus {
                state: SetupSurfaceState::Partial,
                reason: "codex_hooks_feature_missing",
                path,
                config_path: Some(config_path),
            };
        };
        if codex_hooks_feature_enabled(&text) {
            return SetupSurfaceStatus {
                state: SetupSurfaceState::Installed,
                reason: "mimir_hook_present",
                path,
                config_path: Some(config_path),
            };
        }
        let reason = if codex_hooks_feature_disabled(&text) {
            "codex_hooks_feature_disabled"
        } else {
            "codex_hooks_feature_missing"
        };
        return SetupSurfaceStatus {
            state: SetupSurfaceState::Partial,
            reason,
            path,
            config_path: Some(config_path),
        };
    }
    SetupSurfaceStatus {
        state: SetupSurfaceState::Installed,
        reason: "mimir_hook_present",
        path,
        config_path: None,
    }
}

fn dry_run_action(options: &SetupAgentOptions, state: SetupSurfaceState) -> Option<&'static str> {
    if !options.dry_run {
        return None;
    }
    match options.action {
        SetupAgentAction::Status | SetupAgentAction::Doctor => Some("none"),
        SetupAgentAction::Install => match state {
            SetupSurfaceState::Installed => Some("none"),
            SetupSurfaceState::Missing | SetupSurfaceState::Partial => Some("would_install"),
        },
        SetupAgentAction::Remove => match state {
            SetupSurfaceState::Installed | SetupSurfaceState::Partial => Some("would_remove"),
            SetupSurfaceState::Missing => Some("none"),
        },
    }
}

fn checkpoint_command(
    args: &[String],
    env: &BTreeMap<String, String>,
) -> Result<String, HarnessError> {
    let mut title = None;
    let mut list = false;
    let mut body_parts = Vec::new();
    let mut iter = args.iter();
    while let Some(arg) = iter.next() {
        match arg.as_str() {
            "-h" | "--help" => return Ok(checkpoint_usage()),
            "--list" => list = true,
            "--title" => {
                let value = iter.next().ok_or_else(|| HarnessError::MissingFlagValue {
                    flag: "--title".to_string(),
                })?;
                title = Some(value.clone());
            }
            other if other.starts_with('-') => {
                return Err(HarnessError::UnknownFlag {
                    flag: other.to_string(),
                });
            }
            _ => body_parts.push(arg.clone()),
        }
    }

    let session_drafts_dir = checkpoint_session_dir(env)?;
    if list {
        let mut output = String::new();
        for note in list_checkpoint_notes(&session_drafts_dir)? {
            output.push_str(&note.display().to_string());
            output.push('\n');
        }
        return Ok(output);
    }

    let body = checkpoint_body(&body_parts)?;
    let metadata = CheckpointNoteMetadata {
        session_id: env.get("MIMIR_SESSION_ID").cloned(),
        agent: env.get("MIMIR_AGENT").cloned(),
        project: env.get("MIMIR_PROJECT").cloned(),
        operator: None,
    };
    let note = write_checkpoint_note(
        &session_drafts_dir,
        title.as_deref(),
        &body,
        &metadata,
        std::time::SystemTime::now(),
    )?;
    Ok(format!("{}\n", note.path.display()))
}

fn checkpoint_usage() -> String {
    "Usage: mimir checkpoint [--title <title>] [--list] [text...]\n\
     \n\
     Writes a session-local checkpoint note into MIMIR_SESSION_DRAFTS_DIR.\n\
     Run inside a wrapped `mimir <agent>` session.\n"
        .to_string()
}

fn checkpoint_session_dir(env: &BTreeMap<String, String>) -> Result<PathBuf, HarnessError> {
    env.get("MIMIR_SESSION_DRAFTS_DIR")
        .filter(|value| !value.trim().is_empty())
        .map(Path::new)
        .map(Path::to_path_buf)
        .ok_or(HarnessError::CheckpointSessionDraftsDirMissing)
}

fn checkpoint_body(body_parts: &[String]) -> Result<String, HarnessError> {
    if body_parts.is_empty() {
        if io::stdin().is_terminal() {
            return Err(HarnessError::CheckpointEmpty);
        }
        let mut input = String::new();
        io::stdin()
            .read_to_string(&mut input)
            .map_err(|source| HarnessError::DraftWrite {
                path: PathBuf::from("stdin"),
                source,
            })?;
        if input.trim().is_empty() {
            return Err(HarnessError::CheckpointEmpty);
        }
        Ok(input)
    } else {
        Ok(body_parts.join(" "))
    }
}

fn report_argument_error(error: &HarnessError) -> ExitCode {
    eprintln!("mimir: {error}");
    eprintln!("{USAGE}");
    ExitCode::from(2)
}

fn report_spawn_error(error: &HarnessError) -> ExitCode {
    let exit = match error {
        HarnessError::Spawn { source, .. } if source.kind() == std::io::ErrorKind::NotFound => 127,
        _ => 2,
    };
    eprintln!("mimir: {error}");
    ExitCode::from(exit)
}

fn exit_code_from_status(code: Option<i32>) -> ExitCode {
    match code {
        Some(code) => match u8::try_from(code) {
            Ok(code) => ExitCode::from(code),
            Err(_) => ExitCode::from(1),
        },
        _ => ExitCode::from(1),
    }
}