path-cli 0.13.0

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

#[cfg(not(target_os = "emscripten"))]
use anyhow::Context;
use anyhow::Result;
use clap::Subcommand;
use std::path::PathBuf;

#[cfg(not(target_os = "emscripten"))]
use crate::cmd_cache::cache_ref;

#[derive(Subcommand, Debug)]
pub enum ExportTarget {
    /// Project a toolpath document into a Claude Code session
    Claude {
        /// Input: cache id (e.g. `claude-abc`) or path to a toolpath JSON file
        #[arg(short, long)]
        input: String,

        /// Target project directory. With this flag, writes the JSONL into
        /// `~/.claude/projects/<sanitized>/<session>.jsonl` so `claude -r <id>`
        /// can resume it. Defaults to cwd when no `--output` is given.
        #[arg(short, long)]
        project: Option<PathBuf>,

        /// Output JSONL to this file. Mutually exclusive with --project.
        #[arg(short, long, conflicts_with = "project")]
        output: Option<PathBuf>,
    },
    /// Project a toolpath document into a Gemini CLI session
    Gemini {
        /// Input: cache id (e.g. `claude-abc`) or path to a toolpath JSON file
        #[arg(short, long)]
        input: String,

        /// Target project directory. With this flag, writes the chat
        /// files into `~/.gemini/tmp/<slot>/chats/` so `gemini --resume
        /// <uuid>` from the same directory can resume it. Defaults to
        /// cwd when neither --project nor --output is given.
        #[arg(short, long)]
        project: Option<PathBuf>,

        /// Output the main chat file to this path. Sub-agents (if any)
        /// land in a sibling `<session-uuid>/` directory next to it.
        /// Mutually exclusive with --project.
        #[arg(short, long, conflicts_with = "project")]
        output: Option<PathBuf>,
    },
    /// Project a toolpath document into a Pi (pi.dev) session
    Pi {
        /// Input: cache id (e.g. `claude-abc`) or path to a toolpath JSON file
        #[arg(short, long)]
        input: String,

        /// Target project directory. With this flag, writes the JSONL
        /// into `~/.pi/agent/sessions/--<encoded-cwd>--/<session>.jsonl`
        /// under the encoding scheme Pi uses for project directories.
        /// Defaults to cwd when neither --project nor --output is given.
        #[arg(short, long)]
        project: Option<PathBuf>,

        /// Output JSONL to this file. Mutually exclusive with --project.
        #[arg(short, long, conflicts_with = "project")]
        output: Option<PathBuf>,
    },
    /// Project a toolpath document into a Codex CLI session
    Codex {
        /// Input: cache id (e.g. `claude-abc`) or path to a toolpath JSON file
        #[arg(short, long)]
        input: String,

        /// Target project directory. With this flag, writes the JSONL
        /// into `~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl` (Codex's
        /// date-bucketed layout). The session_meta line carries this
        /// path as `cwd`, so `codex resume <session-uuid>` invoked from
        /// the same directory will resolve it correctly. Defaults to
        /// cwd when neither --project nor --output is given.
        #[arg(short, long)]
        project: Option<PathBuf>,

        /// Output JSONL to this file. Mutually exclusive with --project.
        #[arg(short, long, conflicts_with = "project")]
        output: Option<PathBuf>,
    },
    /// Project a toolpath document into an opencode session
    Opencode {
        /// Input: cache id (e.g. `claude-abc`) or path to a toolpath JSON file
        #[arg(short, long)]
        input: String,

        /// Target project directory. With this flag, inserts session,
        /// message, and part rows into `~/.local/share/opencode/opencode.db`
        /// so the `opencode` CLI sees the session in its picker.
        /// Defaults to cwd when neither --project nor --output is given.
        #[arg(short, long)]
        project: Option<PathBuf>,

        /// Output the projected `Session` (with messages and parts)
        /// as pretty JSON to this file. Mutually exclusive with --project.
        #[arg(short, long, conflicts_with = "project")]
        output: Option<PathBuf>,
    },
    /// Upload a toolpath document to Pathbase.
    ///
    /// Default behavior depends on whether you're logged in:
    /// - Logged in (default): writes an unlisted graph under your
    ///   `pathstash` repo. Listable from your account; not publicly visible.
    /// - Not logged in: falls through to the public anonymous endpoint.
    ///   Anon graphs are not listable.
    ///
    /// Use `--repo`/`--name`/`--public` to override the pathstash default
    /// when authenticated. Use `--anon` to force the anonymous endpoint
    /// even when credentials are present.
    Pathbase {
        /// Input: cache id (e.g. `claude-abc`) or path to a toolpath JSON file
        #[arg(short, long)]
        input: String,

        /// Pathbase server URL (defaults to the stored session's server)
        #[arg(long)]
        url: Option<String>,

        /// Force the anonymous endpoint, ignoring any stored credentials
        #[arg(long, conflicts_with_all = ["repo", "public"])]
        anon: bool,

        /// Target a specific repo as `owner/name` instead of `<you>/pathstash`
        #[arg(long, value_parser = parse_repo_spec)]
        repo: Option<RepoSpec>,

        /// Human-readable display label for the uploaded graph
        /// (defaults to the toolpath document id). Free-form; not used
        /// in the URL — graphs are addressed by UUID server-side.
        #[arg(long, alias = "slug")]
        name: Option<String>,

        /// Mark the uploaded graph public (default: unlisted, addressable only by UUID)
        #[arg(long)]
        public: bool,
    },
}

/// `owner/name` pair for `--repo`.
#[derive(Debug, Clone)]
pub(crate) struct RepoSpec {
    pub(crate) owner: String,
    pub(crate) name: String,
}

pub(crate) fn parse_repo_spec(s: &str) -> std::result::Result<RepoSpec, String> {
    let (owner, name) = s
        .split_once('/')
        .ok_or_else(|| format!("expected owner/name, got `{s}`"))?;
    if owner.is_empty() || name.is_empty() {
        return Err(format!("expected owner/name, got `{s}`"));
    }
    Ok(RepoSpec {
        owner: owner.to_string(),
        name: name.to_string(),
    })
}

pub fn run(target: ExportTarget) -> Result<()> {
    match target {
        ExportTarget::Claude {
            input,
            project,
            output,
        } => run_claude(input, project, output),
        ExportTarget::Gemini {
            input,
            project,
            output,
        } => run_gemini(input, project, output),
        ExportTarget::Pi {
            input,
            project,
            output,
        } => run_pi(input, project, output),
        ExportTarget::Codex {
            input,
            project,
            output,
        } => run_codex(input, project, output),
        ExportTarget::Opencode {
            input,
            project,
            output,
        } => run_opencode(input, project, output),
        ExportTarget::Pathbase {
            input,
            url,
            anon,
            repo,
            name,
            public,
        } => run_pathbase(PathbaseExportArgs {
            input,
            url,
            anon,
            repo,
            name,
            public,
        }),
    }
}

#[derive(Debug)]
struct PathbaseExportArgs {
    input: String,
    url: Option<String>,
    anon: bool,
    repo: Option<RepoSpec>,
    name: Option<String>,
    public: bool,
}

/// Pathbase upload knobs that don't depend on where the body came from.
/// Identical to [`PathbaseExportArgs`] minus the `input` field — the body
/// is supplied by the caller (read from cache, derived in memory, …).
#[cfg(not(target_os = "emscripten"))]
#[derive(Debug)]
pub(crate) struct PathbaseUploadArgs {
    pub(crate) url: Option<String>,
    pub(crate) anon: bool,
    pub(crate) repo: Option<RepoSpec>,
    pub(crate) name: Option<String>,
    pub(crate) public: bool,
}

// ── pub(crate) project_<harness> wrappers ────────────────────────────
//
// These compose the private build + write helpers below and return the
// projected session id. They are called by `path resume`; the existing
// `run_<harness>` functions are untouched.

/// Project `path` into a Claude session under `project_dir` and return
/// the resulting session id.
#[cfg(not(target_os = "emscripten"))]
pub(crate) fn project_claude(
    path: &toolpath::v1::Path,
    project_dir: &std::path::Path,
) -> Result<String> {
    let conv = build_claude_conversation(path)?;
    let jsonl = serialize_jsonl(&conv)?;
    write_into_claude_project(&conv, &jsonl, project_dir)?;
    Ok(conv.session_id)
}

/// Project `path` into a Gemini session under `project_dir` and return
/// the resulting session UUID.
#[cfg(not(target_os = "emscripten"))]
pub(crate) fn project_gemini(
    path: &toolpath::v1::Path,
    project_dir: &std::path::Path,
) -> Result<String> {
    use toolpath_convo::ConversationProjector;
    let project_dir = std::fs::canonicalize(project_dir)
        .with_context(|| format!("resolve project path {}", project_dir.display()))?;
    let project_path = project_dir.to_string_lossy().to_string();

    let view = toolpath_convo::extract_conversation(path);
    let project_hash = toolpath_gemini::paths::project_hash(&project_path);
    let projector = toolpath_gemini::project::GeminiProjector::new()
        .with_project_hash(project_hash)
        .with_project_path(project_path.clone());
    let conv = projector
        .project(&view)
        .map_err(|e| anyhow::anyhow!("Projection failed: {}", e))?;
    if conv.session_uuid.is_empty() {
        anyhow::bail!("Projected conversation has no session UUID");
    }
    write_into_gemini_project(&conv, &project_path)?;
    Ok(conv.session_uuid)
}

/// Project `path` into a Codex session and return the resulting session id.
#[cfg(not(target_os = "emscripten"))]
pub(crate) fn project_codex(
    path: &toolpath::v1::Path,
    project_dir: &std::path::Path,
) -> Result<String> {
    use toolpath_convo::ConversationProjector;
    let project_dir = std::fs::canonicalize(project_dir)
        .with_context(|| format!("resolve project path {}", project_dir.display()))?;
    let cwd_str = project_dir.to_string_lossy().to_string();

    let view = toolpath_convo::extract_conversation(path);
    let projector = toolpath_codex::project::CodexProjector::new().with_cwd(cwd_str);
    let session = projector
        .project(&view)
        .map_err(|e| anyhow::anyhow!("Projection failed: {}", e))?;
    if session.id.is_empty() {
        anyhow::bail!("Projected session has no id");
    }
    write_into_codex_project(&session)?;
    Ok(session.id)
}

/// Project `path` into an opencode session under `project_dir` and return
/// the resulting session id.
#[cfg(not(target_os = "emscripten"))]
pub(crate) fn project_opencode(
    path: &toolpath::v1::Path,
    project_dir: &std::path::Path,
) -> Result<String> {
    let session = build_opencode_session(path, Some(project_dir))?;
    let id = session.id.clone();
    write_into_opencode_db(&session, project_dir)?;
    Ok(id)
}

/// Project `path` into a Pi session under `project_dir` and return the
/// resulting session id.
#[cfg(not(target_os = "emscripten"))]
pub(crate) fn project_pi(
    path: &toolpath::v1::Path,
    project_dir: &std::path::Path,
) -> Result<String> {
    use toolpath_convo::ConversationProjector;
    let project_dir = std::fs::canonicalize(project_dir)
        .with_context(|| format!("resolve project path {}", project_dir.display()))?;
    let cwd_str = project_dir.to_string_lossy().to_string();

    let view = toolpath_convo::extract_conversation(path);
    let projector = toolpath_pi::project::PiProjector::new().with_cwd(cwd_str.clone());
    let session = projector
        .project(&view)
        .map_err(|e| anyhow::anyhow!("Projection failed: {}", e))?;
    if session.header.id.is_empty() {
        anyhow::bail!("Projected session has no id");
    }
    write_into_pi_project(&session, &cwd_str)?;
    Ok(session.header.id)
}

fn run_claude(input: String, project: Option<PathBuf>, output: Option<PathBuf>) -> Result<()> {
    #[cfg(target_os = "emscripten")]
    {
        let _ = (input, project, output);
        anyhow::bail!("'path export claude' requires a native environment");
    }

    #[cfg(not(target_os = "emscripten"))]
    {
        let path = load_path_doc(&input)?;
        let conversation = build_claude_conversation(&path)?;
        let jsonl = serialize_jsonl(&conversation)?;

        match (project, output) {
            (Some(project_dir), None) => {
                let out_path = write_into_claude_project(&conversation, &jsonl, &project_dir)?;
                let session_id = &conversation.session_id;
                eprintln!(
                    "Exported session {} ({} entries) → {}",
                    session_id,
                    conversation.preamble.len() + conversation.entries.len(),
                    out_path.display()
                );
                eprintln!();
                eprintln!("Resume with:");
                eprintln!("  cd {} && claude -r {}", project_dir.display(), session_id);
            }
            (None, Some(out_path)) => {
                std::fs::write(&out_path, &jsonl)
                    .with_context(|| format!("write {}", out_path.display()))?;
                eprintln!("Wrote {} bytes to {}", jsonl.len(), out_path.display());
            }
            (None, None) => {
                println!("{}", jsonl);
            }
            (Some(_), Some(_)) => unreachable!("clap enforces conflicts_with"),
        }

        Ok(())
    }
}

#[cfg(not(target_os = "emscripten"))]
fn load_path_doc(input: &str) -> Result<toolpath::v1::Path> {
    let file = cache_ref(input)?;
    let json = std::fs::read_to_string(&file)
        .with_context(|| format!("Failed to read {}", file.display()))?;
    let doc = toolpath::v1::Graph::from_json(&json)
        .map_err(|e| anyhow::anyhow!("Failed to parse toolpath document: {}", e))?;
    doc.into_single_path().ok_or_else(|| {
        anyhow::anyhow!(
            "expected a single-path graph; the source graph holds zero or multiple paths"
        )
    })
}

#[cfg(not(target_os = "emscripten"))]
fn build_claude_conversation(path: &toolpath::v1::Path) -> Result<toolpath_claude::Conversation> {
    use toolpath_convo::ConversationProjector;
    let view = toolpath_convo::extract_conversation(path);
    let projector = toolpath_claude::ClaudeProjector;
    projector
        .project(&view)
        .map_err(|e| anyhow::anyhow!("Projection failed: {}", e))
}

#[cfg(not(target_os = "emscripten"))]
fn serialize_jsonl(conv: &toolpath_claude::Conversation) -> Result<String> {
    let mut lines = Vec::with_capacity(conv.preamble.len() + conv.entries.len());
    for raw in &conv.preamble {
        lines.push(serde_json::to_string(raw)?);
    }
    for entry in &conv.entries {
        lines.push(serde_json::to_string(entry)?);
    }
    Ok(lines.join("\n"))
}

#[cfg(not(target_os = "emscripten"))]
fn write_into_claude_project(
    conv: &toolpath_claude::Conversation,
    jsonl: &str,
    project_dir: &std::path::Path,
) -> Result<PathBuf> {
    let project_dir = std::fs::canonicalize(project_dir)
        .with_context(|| format!("resolve project path {}", project_dir.display()))?;
    let project_path = project_dir.to_string_lossy();

    let resolver = toolpath_claude::PathResolver::new();
    let claude_project_dir = resolver
        .project_dir(&project_path)
        .map_err(|e| anyhow::anyhow!("Cannot resolve Claude project dir: {}", e))?;

    std::fs::create_dir_all(&claude_project_dir)
        .with_context(|| format!("create {}", claude_project_dir.display()))?;

    let session_id = &conv.session_id;
    let out_path = claude_project_dir.join(format!("{}.jsonl", session_id));
    std::fs::write(&out_path, jsonl).with_context(|| format!("write {}", out_path.display()))?;
    Ok(out_path)
}

// ── Gemini ────────────────────────────────────────────────────────────

fn run_gemini(input: String, project: Option<PathBuf>, output: Option<PathBuf>) -> Result<()> {
    #[cfg(target_os = "emscripten")]
    {
        let _ = (input, project, output);
        anyhow::bail!("'path export gemini' requires a native environment");
    }

    #[cfg(not(target_os = "emscripten"))]
    {
        // Resolve the cwd-or-arg up front. With --output, this only
        // affects the projector's `projectHash` / `directories` payload
        // (so the emitted ChatFile carries values matching whichever
        // directory `gemini` would later be invoked from).
        let project_dir = match project.as_ref() {
            Some(p) => std::fs::canonicalize(p)
                .with_context(|| format!("resolve project path {}", p.display()))?,
            None => std::env::current_dir()?,
        };
        let project_path = project_dir.to_string_lossy().to_string();

        let conversation = build_gemini_conversation(&input, &project_path)?;

        match (project, output) {
            (Some(_), None) => write_into_gemini_project(&conversation, &project_path)?,
            (None, Some(out_path)) => write_to_output_path(&conversation, &out_path)?,
            (None, None) => write_to_stdout(&conversation)?,
            (Some(_), Some(_)) => unreachable!("clap enforces conflicts_with"),
        }
        Ok(())
    }
}

#[cfg(not(target_os = "emscripten"))]
fn build_gemini_conversation(
    input: &str,
    project_path: &str,
) -> Result<toolpath_gemini::types::Conversation> {
    use toolpath_convo::ConversationProjector;

    let path = load_path_doc(input)?;
    let view = toolpath_convo::extract_conversation(&path);

    // The projector bakes `projectHash` and `directories` into the
    // emitted ChatFile so they match what Gemini computes when invoked
    // from the same cwd.
    let project_hash = toolpath_gemini::paths::project_hash(project_path);
    let projector = toolpath_gemini::project::GeminiProjector::new()
        .with_project_hash(project_hash)
        .with_project_path(project_path.to_string());
    let conversation = projector
        .project(&view)
        .map_err(|e| anyhow::anyhow!("Projection failed: {}", e))?;

    if conversation.session_uuid.is_empty() {
        anyhow::bail!("Projected conversation has no session UUID — cannot place it on disk");
    }
    Ok(conversation)
}

/// `--project` mode: write the resume-ready layout under
/// `~/.gemini/tmp/<slot>/chats/`.
#[cfg(not(target_os = "emscripten"))]
fn write_into_gemini_project(
    conversation: &toolpath_gemini::types::Conversation,
    project_path: &str,
) -> Result<()> {
    let resolver = toolpath_gemini::PathResolver::new();
    let chats_dir = resolver
        .chats_dir(project_path)
        .map_err(|e| anyhow::anyhow!("Cannot resolve Gemini chats dir: {}", e))?;
    std::fs::create_dir_all(&chats_dir)
        .with_context(|| format!("create {}", chats_dir.display()))?;

    // Drop a `.project_root` marker so `list_project_dirs` and any
    // tooling that walks `tmp/` can pick us up even without a
    // `projects.json` entry.
    if let Some(slot_dir) = chats_dir.parent() {
        let marker = slot_dir.join(".project_root");
        if !marker.exists() {
            let _ = std::fs::write(&marker, format!("{}\n", project_path));
        }
    }

    let main_stem = gemini_main_stem(conversation);
    let main_path = chats_dir.join(format!("{}.json", main_stem));
    let written = write_main_and_subs(conversation, &main_path)?;

    print_summary(conversation, &written, &chats_dir);
    eprintln!();
    eprintln!("Resume with:");
    eprintln!(
        "  cd {} && gemini --resume {}",
        project_path, conversation.session_uuid
    );
    Ok(())
}

/// `--output` mode: write the main chat file to the caller-specified
/// path; sub-agents (if any) land in a sibling `<session-uuid>/` dir.
#[cfg(not(target_os = "emscripten"))]
fn write_to_output_path(
    conversation: &toolpath_gemini::types::Conversation,
    out_path: &std::path::Path,
) -> Result<()> {
    if let Some(parent) = out_path.parent()
        && !parent.as_os_str().is_empty()
    {
        std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
    }

    let written = write_main_and_subs(conversation, out_path)?;

    let parent: PathBuf = out_path
        .parent()
        .map(|p| p.to_path_buf())
        .unwrap_or_else(|| PathBuf::from("."));
    print_summary(conversation, &written, &parent);
    Ok(())
}

/// Stdout mode: pretty-print the main ChatFile JSON. Sub-agents — which
/// don't have a single-document representation in Gemini's format — are
/// dropped with a warning so the caller knows about the loss.
#[cfg(not(target_os = "emscripten"))]
fn write_to_stdout(conversation: &toolpath_gemini::types::Conversation) -> Result<()> {
    let json = serde_json::to_string_pretty(&conversation.main)?;
    println!("{}", json);
    if !conversation.sub_agents.is_empty() {
        let n = conversation.sub_agents.len();
        eprintln!(
            "warning: {} sub-agent chat{} not emitted on stdout — Gemini's format \
             stores each sub-agent in a separate file. Use --output or --project \
             to preserve them.",
            n,
            if n == 1 { "" } else { "s" },
        );
    }
    Ok(())
}

/// Write `conversation.main` to `main_path` and any sub-agents to a
/// sibling `<main_dir>/<session-uuid>/<stem>.json`. Returns every path
/// written, in order.
#[cfg(not(target_os = "emscripten"))]
fn write_main_and_subs(
    conversation: &toolpath_gemini::types::Conversation,
    main_path: &std::path::Path,
) -> Result<Vec<PathBuf>> {
    std::fs::write(main_path, serde_json::to_string_pretty(&conversation.main)?)
        .with_context(|| format!("write {}", main_path.display()))?;
    let mut written: Vec<PathBuf> = vec![main_path.to_path_buf()];

    if !conversation.sub_agents.is_empty() {
        let parent = main_path
            .parent()
            .unwrap_or_else(|| std::path::Path::new("."));
        let sub_dir = parent.join(&conversation.session_uuid);
        std::fs::create_dir_all(&sub_dir)
            .with_context(|| format!("create {}", sub_dir.display()))?;
        for (i, sub) in conversation.sub_agents.iter().enumerate() {
            let stem = if sub.session_id.is_empty() {
                format!("subagent-{}", i)
            } else {
                sub.session_id.clone()
            };
            let sub_path = sub_dir.join(format!("{}.json", stem));
            std::fs::write(&sub_path, serde_json::to_string_pretty(sub)?)
                .with_context(|| format!("write {}", sub_path.display()))?;
            written.push(sub_path);
        }
    }
    Ok(written)
}

#[cfg(not(target_os = "emscripten"))]
fn print_summary(
    conversation: &toolpath_gemini::types::Conversation,
    written: &[PathBuf],
    location: &std::path::Path,
) {
    let total_messages = conversation.main.messages.len()
        + conversation
            .sub_agents
            .iter()
            .map(|s| s.messages.len())
            .sum::<usize>();
    let sub_n = conversation.sub_agents.len();
    eprintln!(
        "Exported Gemini session {} ({} messages across main + {} sub-agent{}) → {}",
        conversation.session_uuid,
        total_messages,
        sub_n,
        if sub_n == 1 { "" } else { "s" },
        location.display()
    );
    for path in written {
        eprintln!("  wrote {}", path.display());
    }
}

/// On-disk stem for a Gemini main chat file:
/// `session-<YYYY-MM-DDTHH-MM>-<first8-of-uuid>`.
///
/// The `session-` prefix is mandatory — Gemini CLI's `--list-sessions`
/// filters on it before opening any file, so a file without it is
/// invisible to `--resume`. The short suffix matches Gemini's own
/// naming convention. Falls back to `session-<uuid>` if no timestamp is
/// available on the projected conversation.
#[cfg(not(target_os = "emscripten"))]
fn gemini_main_stem(convo: &toolpath_gemini::types::Conversation) -> String {
    let short: String = convo.session_uuid.chars().take(8).collect();
    let ts = convo
        .started_at
        .or(convo.last_activity)
        .or(convo.main.start_time)
        .or(convo.main.last_updated);
    match ts {
        Some(t) => format!("session-{}-{}", t.format("%Y-%m-%dT%H-%M"), short),
        None => format!("session-{}", convo.session_uuid),
    }
}

// ── Pi ────────────────────────────────────────────────────────────────

fn run_pi(input: String, project: Option<PathBuf>, output: Option<PathBuf>) -> Result<()> {
    #[cfg(target_os = "emscripten")]
    {
        let _ = (input, project, output);
        anyhow::bail!("'path export pi' requires a native environment");
    }

    #[cfg(not(target_os = "emscripten"))]
    {
        // Resolve cwd up front; with --output it only feeds the
        // SessionHeader.cwd (so a downstream Pi reading the file from
        // a different directory still sees the originating cwd).
        let project_dir = match project.as_ref() {
            Some(p) => std::fs::canonicalize(p)
                .with_context(|| format!("resolve project path {}", p.display()))?,
            None => std::env::current_dir()?,
        };
        let cwd_str = project_dir.to_string_lossy().to_string();

        let session = build_pi_session(&input, &cwd_str)?;

        match (project, output) {
            (Some(_), None) => write_into_pi_project(&session, &cwd_str)?,
            (None, Some(out_path)) => write_pi_to_output_path(&session, &out_path)?,
            (None, None) => write_pi_to_stdout(&session)?,
            (Some(_), Some(_)) => unreachable!("clap enforces conflicts_with"),
        }
        Ok(())
    }
}

#[cfg(not(target_os = "emscripten"))]
fn build_pi_session(input: &str, cwd: &str) -> Result<toolpath_pi::PiSession> {
    use toolpath_convo::ConversationProjector;

    let path = load_path_doc(input)?;
    let view = toolpath_convo::extract_conversation(&path);

    let projector = toolpath_pi::project::PiProjector::new().with_cwd(cwd.to_string());
    let session = projector
        .project(&view)
        .map_err(|e| anyhow::anyhow!("Projection failed: {}", e))?;

    if session.header.id.is_empty() {
        anyhow::bail!("Projected session has no id — cannot place it on disk");
    }
    Ok(session)
}

/// `--project` mode: write the resume-ready layout under
/// `~/.pi/agent/sessions/--<encoded-cwd>--/<session>.jsonl`.
#[cfg(not(target_os = "emscripten"))]
fn write_into_pi_project(session: &toolpath_pi::PiSession, cwd: &str) -> Result<()> {
    let resolver = toolpath_pi::PathResolver::new();
    let project_dir = resolver.project_dir(cwd);
    std::fs::create_dir_all(&project_dir)
        .with_context(|| format!("create {}", project_dir.display()))?;

    let stem = pi_session_stem(session);
    let out_path = project_dir.join(format!("{}.jsonl", stem));
    let bytes = serialize_pi_jsonl(session)?;
    std::fs::write(&out_path, &bytes).with_context(|| format!("write {}", out_path.display()))?;

    let entry_count = session.entries.len().saturating_sub(1); // minus header
    eprintln!(
        "Exported Pi session {} ({} entries) → {}",
        session.header.id,
        entry_count,
        out_path.display()
    );
    eprintln!();
    eprintln!("Loadable via:");
    eprintln!(
        "  path import pi --session {} --project {}",
        session.header.id, cwd
    );
    eprintln!();
    eprintln!("Open conversation with:");
    eprintln!("  pi --session {}", session.header.id);
    Ok(())
}

/// `--output` mode: write JSONL to the caller-specified path.
#[cfg(not(target_os = "emscripten"))]
fn write_pi_to_output_path(
    session: &toolpath_pi::PiSession,
    out_path: &std::path::Path,
) -> Result<()> {
    if let Some(parent) = out_path.parent()
        && !parent.as_os_str().is_empty()
    {
        std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
    }
    let bytes = serialize_pi_jsonl(session)?;
    std::fs::write(out_path, &bytes).with_context(|| format!("write {}", out_path.display()))?;
    eprintln!("Wrote {} bytes to {}", bytes.len(), out_path.display());
    Ok(())
}

/// Stdout mode.
#[cfg(not(target_os = "emscripten"))]
fn write_pi_to_stdout(session: &toolpath_pi::PiSession) -> Result<()> {
    let bytes = serialize_pi_jsonl(session)?;
    print!("{}", bytes);
    Ok(())
}

/// Stem for a Pi session JSONL filename. Pi's own files use
/// `<date>_<uuid>.jsonl`; for projected sessions the session id is
/// already unique enough, so we use it directly with a safe-character
/// fallback for unusual UUIDs.
#[cfg(not(target_os = "emscripten"))]
fn pi_session_stem(session: &toolpath_pi::PiSession) -> String {
    // Strip any dashes-replaced-as-underscores oddities; for plain
    // UUIDs / short ids, the value is already filename-safe.
    session
        .header
        .id
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
        .collect()
}

/// Serialize a `PiSession` to its JSONL on-disk shape: header line
/// followed by one entry per line. Returns the joined string with a
/// trailing newline (Pi's reader is happy with or without it; we add
/// one to match the convention real Pi sessions use).
#[cfg(not(target_os = "emscripten"))]
fn serialize_pi_jsonl(session: &toolpath_pi::PiSession) -> Result<String> {
    let mut lines: Vec<String> = Vec::with_capacity(session.entries.len());
    for entry in &session.entries {
        lines.push(serde_json::to_string(entry)?);
    }
    let mut out = lines.join("\n");
    out.push('\n');
    Ok(out)
}

// ── Codex ─────────────────────────────────────────────────────────────

fn run_codex(input: String, project: Option<PathBuf>, output: Option<PathBuf>) -> Result<()> {
    #[cfg(target_os = "emscripten")]
    {
        let _ = (input, project, output);
        anyhow::bail!("'path export codex' requires a native environment");
    }

    #[cfg(not(target_os = "emscripten"))]
    {
        // Resolve cwd up front; with --output it only feeds the
        // SessionMeta.cwd / TurnContext.cwd payload (so a `codex
        // resume` invocation from a different directory still sees the
        // originating cwd recorded in the rollout).
        let project_dir = match project.as_ref() {
            Some(p) => std::fs::canonicalize(p)
                .with_context(|| format!("resolve project path {}", p.display()))?,
            None => std::env::current_dir()?,
        };
        let cwd_str = project_dir.to_string_lossy().to_string();

        let session = build_codex_session(&input, &cwd_str)?;

        match (project, output) {
            (Some(_), None) => write_into_codex_project(&session)?,
            (None, Some(out_path)) => write_codex_to_output_path(&session, &out_path)?,
            (None, None) => write_codex_to_stdout(&session)?,
            (Some(_), Some(_)) => unreachable!("clap enforces conflicts_with"),
        }
        Ok(())
    }
}

#[cfg(not(target_os = "emscripten"))]
fn build_codex_session(input: &str, cwd: &str) -> Result<toolpath_codex::Session> {
    use toolpath_convo::ConversationProjector;

    let path = load_path_doc(input)?;
    let view = toolpath_convo::extract_conversation(&path);

    let projector = toolpath_codex::project::CodexProjector::new().with_cwd(cwd.to_string());
    let session = projector
        .project(&view)
        .map_err(|e| anyhow::anyhow!("Projection failed: {}", e))?;

    if session.id.is_empty() {
        anyhow::bail!("Projected session has no id — cannot place it on disk");
    }
    Ok(session)
}

/// `--project` mode: write the resume-ready layout under
/// `~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`. The date partitioning
/// matches Codex's own filing convention; the timestamp prefix is
/// derived from the session's first event time.
#[cfg(not(target_os = "emscripten"))]
fn write_into_codex_project(session: &toolpath_codex::Session) -> Result<()> {
    let session_ts = codex_session_timestamp(session)?;
    let resolver = toolpath_codex::PathResolver::new();
    let sessions_root = resolver
        .sessions_root()
        .map_err(|e| anyhow::anyhow!("Cannot resolve Codex sessions dir: {}", e))?;

    // sessions/YYYY/MM/DD/
    let date_dir = sessions_root
        .join(session_ts.format("%Y").to_string())
        .join(session_ts.format("%m").to_string())
        .join(session_ts.format("%d").to_string());
    std::fs::create_dir_all(&date_dir).with_context(|| format!("create {}", date_dir.display()))?;

    let stem = codex_rollout_stem(session, &session_ts);
    let out_path = date_dir.join(format!("{}.jsonl", stem));
    let bytes = serialize_codex_jsonl(session)?;
    std::fs::write(&out_path, &bytes).with_context(|| format!("write {}", out_path.display()))?;

    // `codex resume` reads from state_5.sqlite, not the filesystem;
    // without a thread row the rollout file is invisible.
    let codex_dir = resolver
        .codex_dir()
        .map_err(|e| anyhow::anyhow!("Cannot resolve ~/.codex dir: {}", e))?;
    let registration = register_codex_thread(&codex_dir, session, &out_path, &session_ts);

    eprintln!(
        "Exported Codex session {} ({} lines) → {}",
        session.id,
        session.lines.len(),
        out_path.display()
    );
    match registration {
        Ok(true) => eprintln!("  registered in {}/state_5.sqlite", codex_dir.display()),
        Ok(false) => eprintln!(
            "  warning: state_5.sqlite not found at {} — `codex resume` won't see this session",
            codex_dir.display()
        ),
        Err(e) => eprintln!(
            "  warning: failed to register thread in state_5.sqlite: {} — `codex resume` may not see this session",
            e
        ),
    }
    eprintln!();
    eprintln!("Loadable via:");
    eprintln!("  path import codex --session {}", session.id);
    eprintln!();
    eprintln!("Open conversation with:");
    eprintln!("  codex resume {}", session.id);
    Ok(())
}

/// Schema-fragile: targets the `state_5.sqlite` shape. Returns
/// `Ok(false)` when the DB doesn't exist.
#[cfg(not(target_os = "emscripten"))]
fn register_codex_thread(
    codex_dir: &std::path::Path,
    session: &toolpath_codex::Session,
    rollout_path: &std::path::Path,
    session_ts: &chrono::DateTime<chrono::Utc>,
) -> std::result::Result<bool, rusqlite::Error> {
    let db_path = codex_dir.join("state_5.sqlite");
    if !db_path.exists() {
        return Ok(false);
    }
    let conn = rusqlite::Connection::open(&db_path)?;

    let created_at = session_ts.timestamp();
    let created_at_ms = session_ts.timestamp_millis();
    let (cwd, model_provider, cli_version) = match session.meta() {
        Some(m) => (
            m.cwd.to_string_lossy().to_string(),
            m.model_provider.clone().unwrap_or_else(|| "openai".into()),
            m.cli_version,
        ),
        None => ("/".to_string(), "openai".to_string(), String::new()),
    };
    let first_user_message = first_user_message_text(session);
    let title = first_user_message.chars().take(200).collect::<String>();
    let has_user_event: i64 = if first_user_message.is_empty() { 0 } else { 1 };
    let sandbox_policy_json = serde_json::json!({
        "type": "workspace-write",
        "writable_roots": [],
        "network_access": false,
        "exclude_tmpdir_env_var": false,
        "exclude_slash_tmp": false,
    })
    .to_string();

    conn.execute(
        "INSERT OR REPLACE INTO threads (
            id, rollout_path, created_at, updated_at, source, model_provider,
            cwd, title, sandbox_policy, approval_mode, tokens_used, has_user_event,
            archived, cli_version, first_user_message, memory_mode,
            created_at_ms, updated_at_ms
         ) VALUES (
            ?1, ?2, ?3, ?4, 'cli', ?5,
            ?6, ?7, ?8, 'on-request', 0, ?9,
            0, ?10, ?11, 'enabled',
            ?12, ?13
         )",
        rusqlite::params![
            session.id,
            rollout_path.to_string_lossy(),
            created_at,
            created_at,
            model_provider,
            cwd,
            title,
            sandbox_policy_json,
            has_user_event,
            cli_version,
            first_user_message,
            created_at_ms,
            created_at_ms,
        ],
    )?;
    Ok(true)
}

#[cfg(not(target_os = "emscripten"))]
fn first_user_message_text(session: &toolpath_codex::Session) -> String {
    use toolpath_codex::types::{ResponseItem, RolloutItem};
    for line in &session.lines {
        if let RolloutItem::ResponseItem(ResponseItem::Message(m)) = line.item()
            && m.role == "user"
        {
            let t = m.text();
            if !t.is_empty() {
                return t;
            }
        }
    }
    String::new()
}

/// `--output` mode: write JSONL to the caller-specified path.
#[cfg(not(target_os = "emscripten"))]
fn write_codex_to_output_path(
    session: &toolpath_codex::Session,
    out_path: &std::path::Path,
) -> Result<()> {
    if let Some(parent) = out_path.parent()
        && !parent.as_os_str().is_empty()
    {
        std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
    }
    let bytes = serialize_codex_jsonl(session)?;
    std::fs::write(out_path, &bytes).with_context(|| format!("write {}", out_path.display()))?;
    eprintln!("Wrote {} bytes to {}", bytes.len(), out_path.display());
    Ok(())
}

/// Stdout mode.
#[cfg(not(target_os = "emscripten"))]
fn write_codex_to_stdout(session: &toolpath_codex::Session) -> Result<()> {
    let bytes = serialize_codex_jsonl(session)?;
    print!("{}", bytes);
    Ok(())
}

/// Pull the session's intended timestamp out of its session_meta line.
/// Falls back to the wall clock at projection time when the metadata
/// is missing or unparseable — Codex's directory layout requires SOME
/// date to file under.
#[cfg(not(target_os = "emscripten"))]
fn codex_session_timestamp(
    session: &toolpath_codex::Session,
) -> Result<chrono::DateTime<chrono::Utc>> {
    if let Some(meta) = session.meta()
        && let Ok(dt) = meta.timestamp.parse::<chrono::DateTime<chrono::Utc>>()
    {
        return Ok(dt);
    }
    Ok(chrono::Utc::now())
}

/// Filename stem matching Codex's own naming:
/// `rollout-YYYY-MM-DDThh-mm-ss-<session-uuid>`.
#[cfg(not(target_os = "emscripten"))]
fn codex_rollout_stem(
    session: &toolpath_codex::Session,
    ts: &chrono::DateTime<chrono::Utc>,
) -> String {
    let stamp = ts.format("%Y-%m-%dT%H-%M-%S").to_string();
    let uuid_safe: String = session
        .id
        .chars()
        .filter(|c| c.is_ascii_alphanumeric() || *c == '-' || *c == '_')
        .collect();
    format!("rollout-{}-{}", stamp, uuid_safe)
}

/// Serialize a Codex `Session` to JSONL — one [`RolloutLine`] per line,
/// trailing newline included. This matches the on-disk shape Codex's
/// rollout recorder writes.
#[cfg(not(target_os = "emscripten"))]
fn serialize_codex_jsonl(session: &toolpath_codex::Session) -> Result<String> {
    let mut lines: Vec<String> = Vec::with_capacity(session.lines.len());
    for line in &session.lines {
        lines.push(serde_json::to_string(line)?);
    }
    let mut out = lines.join("\n");
    out.push('\n');
    Ok(out)
}

// ── Opencode ──────────────────────────────────────────────────────────

fn run_opencode(input: String, project: Option<PathBuf>, output: Option<PathBuf>) -> Result<()> {
    #[cfg(target_os = "emscripten")]
    {
        let _ = (input, project, output);
        anyhow::bail!("'path export opencode' requires a native environment");
    }

    #[cfg(not(target_os = "emscripten"))]
    {
        let path = load_path_doc(&input)?;
        match (project, output) {
            (Some(project_dir), None) => {
                let session = build_opencode_session(&path, Some(&project_dir))?;
                write_into_opencode_db(&session, &project_dir)?;
            }
            (None, Some(out_path)) => {
                let session = build_opencode_session(&path, None)?;
                write_opencode_to_output_path(&session, &out_path)?;
            }
            (None, None) => {
                let cwd = std::env::current_dir().ok();
                let session = build_opencode_session(&path, cwd.as_deref())?;
                write_opencode_to_stdout(&session)?;
            }
            (Some(_), Some(_)) => unreachable!("clap enforces conflicts_with"),
        }
        Ok(())
    }
}

#[cfg(not(target_os = "emscripten"))]
fn build_opencode_session(
    path: &toolpath::v1::Path,
    project_dir: Option<&std::path::Path>,
) -> Result<toolpath_opencode::Session> {
    use toolpath_convo::ConversationProjector;
    use toolpath_opencode::project::OpencodeProjector;

    let view = toolpath_convo::extract_conversation(path);
    let mut projector = OpencodeProjector::new();
    if let Some(dir) = project_dir {
        let canonical = std::fs::canonicalize(dir).unwrap_or_else(|_| dir.to_path_buf());
        projector = projector.with_directory(canonical);
    }
    projector
        .project(&view)
        .map_err(|e| anyhow::anyhow!("Projection failed: {}", e))
}

#[cfg(not(target_os = "emscripten"))]
fn write_into_opencode_db(
    session: &toolpath_opencode::Session,
    project_dir: &std::path::Path,
) -> Result<()> {
    use toolpath_opencode::PathResolver;

    let project_dir = std::fs::canonicalize(project_dir)
        .with_context(|| format!("resolve project path {}", project_dir.display()))?;

    let resolver = PathResolver::new();
    let db_path = resolver
        .db_path()
        .map_err(|e| anyhow::anyhow!("Cannot resolve opencode db path: {}", e))?;
    if !db_path.exists() {
        anyhow::bail!(
            "opencode database not found at {} — has opencode been run on this machine?",
            db_path.display()
        );
    }

    let mut conn = rusqlite::Connection::open(&db_path)
        .with_context(|| format!("open {}", db_path.display()))?;
    let tx = conn.transaction()?;

    ensure_opencode_project(&tx, &session.project_id, &project_dir, session.time_created)?;
    insert_opencode_session(&tx, session)?;
    let mut message_count = 0_usize;
    let mut part_count = 0_usize;
    for message in &session.messages {
        insert_opencode_message(&tx, message)?;
        message_count += 1;
        for part in &message.parts {
            insert_opencode_part(&tx, part)?;
            part_count += 1;
        }
    }
    tx.commit()?;

    eprintln!(
        "Exported opencode session {} ({} messages, {} parts) → {}",
        session.id,
        message_count,
        part_count,
        db_path.display()
    );
    eprintln!();
    eprintln!("Loadable via:");
    eprintln!("  path import opencode --session {}", session.id);
    eprintln!();
    eprintln!("Open conversation with:");
    eprintln!("  opencode --session {}", session.id);
    Ok(())
}

#[cfg(not(target_os = "emscripten"))]
fn ensure_opencode_project(
    tx: &rusqlite::Transaction<'_>,
    project_id: &str,
    worktree: &std::path::Path,
    time_now: i64,
) -> Result<()> {
    use rusqlite::OptionalExtension;
    let exists: bool = tx
        .query_row(
            "SELECT 1 FROM project WHERE id = ?1",
            rusqlite::params![project_id],
            |_| Ok(true),
        )
        .optional()?
        .unwrap_or(false);
    if exists {
        return Ok(());
    }
    tx.execute(
        "INSERT INTO project (id, worktree, vcs, name, time_created, time_updated, sandboxes)
         VALUES (?1, ?2, 'git', NULL, ?3, ?3, '[]')",
        rusqlite::params![project_id, worktree.to_string_lossy(), time_now],
    )?;
    Ok(())
}

#[cfg(not(target_os = "emscripten"))]
fn insert_opencode_session(
    tx: &rusqlite::Transaction<'_>,
    session: &toolpath_opencode::Session,
) -> Result<()> {
    tx.execute(
        "INSERT OR REPLACE INTO session
            (id, project_id, workspace_id, parent_id, slug, directory, title,
             version, share_url, summary_additions, summary_deletions, summary_files,
             time_created, time_updated, time_compacting, time_archived)
         VALUES
            (?1, ?2, ?3, ?4, ?5, ?6, ?7,
             ?8, ?9, ?10, ?11, ?12,
             ?13, ?14, ?15, ?16)",
        rusqlite::params![
            session.id,
            session.project_id,
            session.workspace_id,
            session.parent_id,
            session.slug,
            session.directory.to_string_lossy(),
            session.title,
            session.version,
            session.share_url,
            session.summary_additions,
            session.summary_deletions,
            session.summary_files,
            session.time_created,
            session.time_updated,
            session.time_compacting,
            session.time_archived,
        ],
    )?;
    Ok(())
}

#[cfg(not(target_os = "emscripten"))]
fn insert_opencode_message(
    tx: &rusqlite::Transaction<'_>,
    message: &toolpath_opencode::Message,
) -> Result<()> {
    let data = serde_json::to_string(&message.data)
        .with_context(|| format!("serialize message {}", message.id))?;
    tx.execute(
        "INSERT OR REPLACE INTO message (id, session_id, time_created, time_updated, data)
         VALUES (?1, ?2, ?3, ?4, ?5)",
        rusqlite::params![
            message.id,
            message.session_id,
            message.time_created,
            message.time_updated,
            data,
        ],
    )?;
    Ok(())
}

#[cfg(not(target_os = "emscripten"))]
fn insert_opencode_part(
    tx: &rusqlite::Transaction<'_>,
    part: &toolpath_opencode::Part,
) -> Result<()> {
    let data =
        serde_json::to_string(&part.data).with_context(|| format!("serialize part {}", part.id))?;
    tx.execute(
        "INSERT OR REPLACE INTO part
            (id, message_id, session_id, time_created, time_updated, data)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
        rusqlite::params![
            part.id,
            part.message_id,
            part.session_id,
            part.time_created,
            part.time_updated,
            data,
        ],
    )?;
    Ok(())
}

#[cfg(not(target_os = "emscripten"))]
fn write_opencode_to_output_path(
    session: &toolpath_opencode::Session,
    out_path: &std::path::Path,
) -> Result<()> {
    if let Some(parent) = out_path.parent()
        && !parent.as_os_str().is_empty()
    {
        std::fs::create_dir_all(parent).with_context(|| format!("create {}", parent.display()))?;
    }
    let json = serde_json::to_string_pretty(session)?;
    std::fs::write(out_path, &json).with_context(|| format!("write {}", out_path.display()))?;
    eprintln!(
        "Wrote {} bytes to {} ({} messages)",
        json.len(),
        out_path.display(),
        session.messages.len()
    );
    Ok(())
}

#[cfg(not(target_os = "emscripten"))]
fn write_opencode_to_stdout(session: &toolpath_opencode::Session) -> Result<()> {
    let json = serde_json::to_string_pretty(session)?;
    println!("{}", json);
    Ok(())
}

// ── Pathbase ──────────────────────────────────────────────────────────

fn run_pathbase(args: PathbaseExportArgs) -> Result<()> {
    #[cfg(target_os = "emscripten")]
    {
        let _ = args;
        anyhow::bail!("'path export pathbase' requires a native environment with network access");
    }

    #[cfg(not(target_os = "emscripten"))]
    {
        use crate::cmd_pathbase::preflight_auth;

        let file = cache_ref(&args.input)?;
        let body = std::fs::read_to_string(&file)
            .with_context(|| format!("Failed to read {}", file.display()))?;
        let upload = PathbaseUploadArgs {
            url: args.url,
            anon: args.anon,
            repo: args.repo,
            name: args.name,
            public: args.public,
        };
        let base_url = resolve_upload_base_url(&upload);
        let needs_auth = upload.repo.is_some() || upload.public || upload.name.is_some();
        let auth = preflight_auth(&base_url, upload.anon, needs_auth)?;
        let summary_source = file.display().to_string();
        run_pathbase_inner(auth, base_url, upload, &body, &summary_source)
    }
}

/// Resolve the upload target URL from the CLI flag, the stored session,
/// or the default. Mirrors the order used inside `run_pathbase_inner` so
/// `cmd_share`'s pre-flight resolution agrees with the eventual upload.
#[cfg(not(target_os = "emscripten"))]
pub(crate) fn resolve_upload_base_url(args: &PathbaseUploadArgs) -> String {
    use crate::cmd_pathbase::{credentials_path, load_session, resolve_url};

    if let Some(u) = &args.url {
        return resolve_url(Some(u.clone()));
    }
    if let Ok(path) = credentials_path()
        && let Ok(Some(s)) = load_session(&path)
    {
        return s.url;
    }
    resolve_url(None)
}

#[cfg(not(target_os = "emscripten"))]
pub(crate) fn run_pathbase_inner(
    auth: crate::cmd_pathbase::AuthMode,
    base_url: String,
    args: PathbaseUploadArgs,
    body: &str,
    summary_source: &str,
) -> Result<()> {
    use crate::cmd_pathbase::{AuthMode, anon_graphs_post, graphs_post, repos_post};
    use pathbase_client::types::Visibility;

    // Validate locally so we give a clean error rather than relying on
    // the server to reject malformed payloads.
    let doc = toolpath::v1::Graph::from_json(body)
        .map_err(|e| anyhow::anyhow!("Invalid toolpath document: {}", e))?;

    let (token, username) = match auth {
        AuthMode::Anon => {
            let resp = anon_graphs_post(&base_url, body)?;
            let printable = if resp.url.starts_with("http://") || resp.url.starts_with("https://") {
                resp.url.clone()
            } else if resp.url.starts_with('/') {
                format!("{base_url}{}", resp.url)
            } else {
                format!("{base_url}/{}", resp.url)
            };
            // Summary first on stderr, then the URL on stdout — the
            // share URL is the primary product, so it's the last line
            // the user (or a script piping the output) sees.
            eprintln!(
                "Uploaded {} → anon graph {} ({} bytes)",
                summary_source,
                resp.id,
                body.len()
            );
            println!("{printable}");
            return Ok(());
        }
        AuthMode::Authed { token, username } => (token, username),
    };

    let (owner, repo) = match args.repo {
        Some(spec) => (spec.owner, spec.name),
        None => {
            // Pathstash default: own the repo "pathstash" under the username
            // we resolved during preflight. Create it on demand.
            repos_post(&base_url, &token, &username, "pathstash")?;
            (username, "pathstash".to_string())
        }
    };

    let name = args.name.or_else(|| Some(derive_name(&doc)));
    let created = graphs_post(
        &base_url,
        &token,
        &owner,
        &repo,
        name.as_deref(),
        body,
        args.public,
    )?;

    // The visibility we surface is what the server actually applied,
    // not what we requested. If server-side policy ever clamps the
    // request, we render the form the graph can actually be reached at.
    let requested = if args.public {
        Visibility::Public
    } else {
        Visibility::Unlisted
    };
    if created.visibility != requested {
        eprintln!(
            "note: requested visibility={requested} but server applied visibility={}",
            created.visibility
        );
    }
    // Summary first on stderr, URL last on stdout — same ordering as
    // the anon path so the share URL is consistently the final line.
    eprintln!(
        "Uploaded {} → {}/{}/graphs/{} ({}, {} bytes)",
        summary_source,
        owner,
        repo,
        created.id,
        created.visibility,
        body.len()
    );
    println!("{}", created.url);
    Ok(())
}

/// Default display label for a graph uploaded via `export pathbase`.
///
/// Sanitize the inner id (Path id / Graph id) into `[a-z0-9_-]`-only
/// form, lower-cased — close to a URL slug, even though the server
/// addresses graphs by UUID and never reads this back from the wire.
/// Fallback (id sanitizes to empty — non-ascii, all punctuation, …):
/// hash the canonical JSON and use a short hex prefix so re-uploads
/// of the same content produce the same display label.
#[cfg(not(target_os = "emscripten"))]
fn derive_name(doc: &toolpath::v1::Graph) -> String {
    let raw = match doc.single_path() {
        Some(p) => p.path.id.as_str(),
        None => doc.graph.id.as_str(),
    };
    let slug: String = raw
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
                c.to_ascii_lowercase()
            } else {
                '-'
            }
        })
        .collect();
    let trimmed = slug.trim_matches('-').to_string();
    if !trimmed.is_empty() {
        return trimmed;
    }

    use sha2::{Digest, Sha256};
    let bytes = serde_json::to_vec(doc).unwrap_or_default();
    let hex = format!("{:x}", Sha256::digest(&bytes));
    format!("path-{}", &hex[..12])
}

#[cfg(all(test, not(target_os = "emscripten")))]
mod tests {
    use super::*;
    use std::collections::HashMap;
    use toolpath::v1::{ArtifactChange, PathIdentity, Step, StepIdentity, StructuralChange};

    fn make_path_doc() -> toolpath::v1::Graph {
        let artifact_key = "agent://claude/test-session";

        let init_step = Step {
            step: StepIdentity {
                id: "step-001".to_string(),
                parents: vec![],
                actor: "tool:claude-code".to_string(),
                timestamp: "2024-01-01T00:00:00Z".to_string(),
            },
            change: {
                let mut m = HashMap::new();
                m.insert(
                    artifact_key.to_string(),
                    ArtifactChange {
                        raw: None,
                        structural: Some(StructuralChange {
                            change_type: "conversation.init".to_string(),
                            extra: HashMap::new(),
                        }),
                    },
                );
                m
            },
            meta: None,
        };

        let append_step = Step {
            step: StepIdentity {
                id: "step-002".to_string(),
                parents: vec!["step-001".to_string()],
                actor: "human:user".to_string(),
                timestamp: "2024-01-01T00:00:01Z".to_string(),
            },
            change: {
                let mut m = HashMap::new();
                let mut extra = HashMap::new();
                extra.insert("role".to_string(), serde_json::json!("user"));
                extra.insert("text".to_string(), serde_json::json!("Hello"));
                m.insert(
                    artifact_key.to_string(),
                    ArtifactChange {
                        raw: None,
                        structural: Some(StructuralChange {
                            change_type: "conversation.append".to_string(),
                            extra,
                        }),
                    },
                );
                m
            },
            meta: None,
        };

        let path = toolpath::v1::Path {
            path: PathIdentity {
                id: "test-path".to_string(),
                base: None,
                head: "step-002".to_string(),
                graph_ref: None,
            },
            steps: vec![init_step, append_step],
            meta: None,
        };

        toolpath::v1::Graph::from_path(path)
    }

    #[test]
    fn claude_output_to_file() {
        let temp = tempfile::tempdir().unwrap();
        let input_path = temp.path().join("input.json");
        let output_path = temp.path().join("out.jsonl");

        let doc = make_path_doc();
        std::fs::write(&input_path, serde_json::to_string(&doc).unwrap()).unwrap();

        run_claude(
            input_path.to_string_lossy().to_string(),
            None,
            Some(output_path.clone()),
        )
        .unwrap();

        let out = std::fs::read_to_string(&output_path).unwrap();
        assert!(!out.is_empty());
        for line in out.lines() {
            serde_json::from_str::<serde_json::Value>(line).unwrap();
        }
    }

    #[test]
    fn claude_rejects_multi_path_graph() {
        let temp = tempfile::tempdir().unwrap();
        let input_path = temp.path().join("input.json");
        let make_path = |id: &str| toolpath::v1::Path {
            path: PathIdentity {
                id: id.into(),
                base: None,
                head: "s1".into(),
                graph_ref: None,
            },
            steps: vec![Step {
                step: StepIdentity {
                    id: "s1".into(),
                    parents: vec![],
                    actor: "human:x".into(),
                    timestamp: "2024-01-01T00:00:00Z".into(),
                },
                change: HashMap::new(),
                meta: None,
            }],
            meta: None,
        };
        let multi = toolpath::v1::Graph {
            graph: toolpath::v1::GraphIdentity { id: "g".into() },
            paths: vec![
                toolpath::v1::PathOrRef::Path(Box::new(make_path("p1"))),
                toolpath::v1::PathOrRef::Path(Box::new(make_path("p2"))),
            ],
            meta: None,
        };
        std::fs::write(&input_path, serde_json::to_string(&multi).unwrap()).unwrap();

        let err = run_claude(input_path.to_string_lossy().to_string(), None, None).unwrap_err();
        assert!(err.to_string().contains("single-path graph"));
    }

    #[test]
    fn claude_invalid_json_errors() {
        let temp = tempfile::tempdir().unwrap();
        let input_path = temp.path().join("input.json");
        std::fs::write(&input_path, "not json").unwrap();
        let err = run_claude(input_path.to_string_lossy().to_string(), None, None).unwrap_err();
        assert!(err.to_string().contains("parse") || err.to_string().contains("Failed"));
    }

    #[test]
    fn gemini_writes_resume_ready_layout() {
        // End-to-end: a path doc whose conversation.append carries a
        // single user turn projects to a flat `session-*.json` file
        // under <slot>/chats/, with `kind: "main"` and the inner
        // `sessionId` matching the source UUID. Layout matches what
        // Gemini CLI's `--resume <uuid>` needs to find the session.
        use toolpath_gemini::{GeminiConvo, PathResolver};

        let temp = tempfile::tempdir().unwrap();
        let fake_home = temp.path().join("home");
        std::fs::create_dir_all(&fake_home).unwrap();
        let project_dir = temp.path().join("myproj");
        std::fs::create_dir_all(&project_dir).unwrap();

        // Build a minimal Path doc whose artifact key encodes the
        // session UUID we want to land in `sessionId`. The shared-derive
        // wire shape is `<provider>://<session-id>`, which extract
        // parses to populate `view.id` and `view.provider_id`.
        let session_uuid = "11111111-2222-3333-4444-555555555555";
        let artifact = format!("gemini-cli://{}", session_uuid);
        let mut extra = HashMap::new();
        extra.insert("role".into(), serde_json::json!("user"));
        extra.insert("text".into(), serde_json::json!("Hello from export"));
        let step = Step {
            step: StepIdentity {
                id: "step-001".into(),
                parents: vec![],
                actor: "human:alex".into(),
                timestamp: "2026-04-17T15:00:00Z".into(),
            },
            change: {
                let mut m = HashMap::new();
                m.insert(
                    artifact,
                    ArtifactChange {
                        raw: None,
                        structural: Some(StructuralChange {
                            change_type: "conversation.append".into(),
                            extra,
                        }),
                    },
                );
                m
            },
            meta: None,
        };
        let doc = toolpath::v1::Graph::from_path(toolpath::v1::Path {
            path: PathIdentity {
                id: "test-path".into(),
                base: None,
                head: "step-001".into(),
                graph_ref: None,
            },
            steps: vec![step],
            meta: None,
        });
        let input_path = temp.path().join("doc.json");
        std::fs::write(&input_path, serde_json::to_string(&doc).unwrap()).unwrap();

        // Override HOME so `PathResolver::new()` lands in the temp dir.
        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prior_home = std::env::var_os("HOME");
        unsafe {
            std::env::set_var("HOME", &fake_home);
        }
        let result = run_gemini(
            input_path.to_string_lossy().to_string(),
            Some(project_dir.clone()),
            None,
        );
        unsafe {
            match prior_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
        }
        result.expect("export gemini");

        // The file landed at chats/session-*.json (flat, prefixed).
        let canon_project = std::fs::canonicalize(&project_dir).unwrap();
        let resolver = PathResolver::new().with_home(&fake_home);
        let chats_dir = resolver.chats_dir(canon_project.to_str().unwrap()).unwrap();

        let session_files: Vec<PathBuf> = std::fs::read_dir(&chats_dir)
            .unwrap()
            .filter_map(|e| e.ok().map(|e| e.path()))
            .filter(|p| {
                p.is_file()
                    && p.file_name()
                        .and_then(|n| n.to_str())
                        .is_some_and(|s| s.starts_with("session-") && s.ends_with(".json"))
            })
            .collect();
        assert_eq!(session_files.len(), 1, "expected one session-*.json");

        let raw = std::fs::read_to_string(&session_files[0]).unwrap();
        let parsed: serde_json::Value = serde_json::from_str(&raw).unwrap();
        assert_eq!(parsed["sessionId"].as_str(), Some(session_uuid));
        assert_eq!(parsed["kind"].as_str(), Some("main"));

        // `--resume <uuid>` reads via inner sessionId, so verify the
        // library's resolver (which mirrors that lookup) finds it.
        let convo = GeminiConvo::with_resolver(resolver);
        let loaded = convo
            .read_conversation(canon_project.to_str().unwrap(), session_uuid)
            .expect("read back via uuid");
        assert_eq!(loaded.main.messages.len(), 1);
        assert_eq!(loaded.main.messages[0].content.text(), "Hello from export");
    }

    #[test]
    fn gemini_rejects_multi_path_graph() {
        let temp = tempfile::tempdir().unwrap();
        let input_path = temp.path().join("input.json");
        let make_path = |id: &str| toolpath::v1::Path {
            path: PathIdentity {
                id: id.into(),
                base: None,
                head: "s1".into(),
                graph_ref: None,
            },
            steps: vec![Step {
                step: StepIdentity {
                    id: "s1".into(),
                    parents: vec![],
                    actor: "human:x".into(),
                    timestamp: "2024-01-01T00:00:00Z".into(),
                },
                change: HashMap::new(),
                meta: None,
            }],
            meta: None,
        };
        let multi = toolpath::v1::Graph {
            graph: toolpath::v1::GraphIdentity { id: "g".into() },
            paths: vec![
                toolpath::v1::PathOrRef::Path(Box::new(make_path("p1"))),
                toolpath::v1::PathOrRef::Path(Box::new(make_path("p2"))),
            ],
            meta: None,
        };
        std::fs::write(&input_path, serde_json::to_string(&multi).unwrap()).unwrap();

        let project = temp.path().join("proj");
        std::fs::create_dir_all(&project).unwrap();
        let err = run_gemini(
            input_path.to_string_lossy().to_string(),
            Some(project),
            None,
        )
        .expect_err("should reject multi-path graph");
        assert!(err.to_string().contains("single-path graph"));
    }

    #[test]
    fn gemini_output_to_file_writes_main_at_path() {
        // `--output FILE` writes the main ChatFile to FILE. Sub-agents
        // (none in this fixture) would land in `<FILE_PARENT>/<uuid>/`.
        use toolpath_gemini::ChatFile;

        let temp = tempfile::tempdir().unwrap();
        let project_dir = temp.path().join("myproj");
        std::fs::create_dir_all(&project_dir).unwrap();
        let out_path = temp.path().join("out").join("session.json");

        let session_uuid = "33333333-4444-5555-6666-777777777777";
        let artifact = format!("gemini-cli://{}", session_uuid);
        let mut extra = HashMap::new();
        extra.insert("role".into(), serde_json::json!("user"));
        extra.insert("text".into(), serde_json::json!("Hello via output"));
        let step = Step {
            step: StepIdentity {
                id: "step-001".into(),
                parents: vec![],
                actor: "human:alex".into(),
                timestamp: "2026-04-17T15:00:00Z".into(),
            },
            change: {
                let mut m = HashMap::new();
                m.insert(
                    artifact,
                    ArtifactChange {
                        raw: None,
                        structural: Some(StructuralChange {
                            change_type: "conversation.append".into(),
                            extra,
                        }),
                    },
                );
                m
            },
            meta: None,
        };
        let doc = toolpath::v1::Graph::from_path(toolpath::v1::Path {
            path: PathIdentity {
                id: "test-path".into(),
                base: None,
                head: "step-001".into(),
                graph_ref: None,
            },
            steps: vec![step],
            meta: None,
        });
        let input_path = temp.path().join("doc.json");
        std::fs::write(&input_path, serde_json::to_string(&doc).unwrap()).unwrap();

        // `--output` is mutually exclusive with `--project`, so leave
        // project None. The projector still uses cwd to compute
        // `projectHash` and `directories` on the emitted ChatFile.
        run_gemini(
            input_path.to_string_lossy().to_string(),
            None,
            Some(out_path.clone()),
        )
        .expect("export gemini --output");

        // Parent dir is created automatically.
        assert!(out_path.exists(), "main file at output path missing");
        // The file is a valid Gemini ChatFile and carries the source UUID.
        let raw = std::fs::read_to_string(&out_path).unwrap();
        let parsed: ChatFile = serde_json::from_str(&raw).unwrap();
        assert_eq!(parsed.session_id, session_uuid);
        assert_eq!(parsed.kind.as_deref(), Some("main"));
        assert_eq!(parsed.messages.len(), 1);
        assert_eq!(parsed.messages[0].content.text(), "Hello via output");

        // No sub-agents in this fixture → no sibling UUID dir.
        assert!(!out_path.parent().unwrap().join(session_uuid).exists());
    }

    #[test]
    fn gemini_project_and_output_mutually_exclusive() {
        // clap's `conflicts_with` enforces this at parse time, but the
        // function itself also panics via `unreachable!` if both are
        // somehow passed. We can't easily exercise that without going
        // through clap; this test lives mostly as a reminder that the
        // contract holds — when both are provided, clap rejects the
        // invocation before `run_gemini` runs at all.
        use clap::Parser;
        #[derive(Parser, Debug)]
        struct Cli {
            #[command(subcommand)]
            cmd: ExportTarget,
        }
        let parsed = Cli::try_parse_from([
            "test",
            "gemini",
            "--input",
            "x",
            "--project",
            "/tmp/p",
            "--output",
            "/tmp/o.json",
        ]);
        assert!(
            parsed.is_err(),
            "clap must reject simultaneous --project and --output"
        );
    }

    #[test]
    fn pi_writes_resume_ready_layout() {
        // Build a Path doc with a single user turn, run `path export
        // pi --project DIR`, and confirm the JSONL lands at the
        // expected `~/.pi/agent/sessions/--<encoded>--/<id>.jsonl`
        // location and re-parses through Pi's own reader.
        use toolpath_pi::{PathResolver, PiConvo};

        let temp = tempfile::tempdir().unwrap();
        let fake_home = temp.path().join("home");
        std::fs::create_dir_all(&fake_home).unwrap();
        let project_dir = temp.path().join("myproj");
        std::fs::create_dir_all(&project_dir).unwrap();

        let session_uuid = "pi-session-test-1";
        let artifact = format!("pi://{}", session_uuid);
        let mut extra = HashMap::new();
        extra.insert("role".into(), serde_json::json!("user"));
        extra.insert("text".into(), serde_json::json!("Hello pi"));
        let step = Step {
            step: StepIdentity {
                id: "step-001".into(),
                parents: vec![],
                actor: "human:alex".into(),
                timestamp: "2026-04-17T15:00:00Z".into(),
            },
            change: {
                let mut m = HashMap::new();
                m.insert(
                    artifact,
                    ArtifactChange {
                        raw: None,
                        structural: Some(StructuralChange {
                            change_type: "conversation.append".into(),
                            extra,
                        }),
                    },
                );
                m
            },
            meta: None,
        };
        let graph = toolpath::v1::Graph::from_path(toolpath::v1::Path {
            path: PathIdentity {
                id: "test-path".into(),
                base: None,
                head: "step-001".into(),
                graph_ref: None,
            },
            steps: vec![step],
            meta: None,
        });
        let input_path = temp.path().join("doc.json");
        std::fs::write(&input_path, graph.to_json().unwrap()).unwrap();

        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prior_home = std::env::var_os("HOME");
        unsafe {
            std::env::set_var("HOME", &fake_home);
        }
        let result = run_pi(
            input_path.to_string_lossy().to_string(),
            Some(project_dir.clone()),
            None,
        );
        unsafe {
            match prior_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
        }
        result.expect("export pi");

        let canon_project = std::fs::canonicalize(&project_dir).unwrap();
        let resolver = PathResolver::new().with_home(&fake_home);
        let project_dir_path = resolver.project_dir(canon_project.to_str().unwrap());
        let expected = project_dir_path.join(format!("{}.jsonl", session_uuid));
        assert!(expected.exists(), "expected JSONL at {:?}", expected);

        let convo = PiConvo::with_resolver(resolver);
        let session = convo
            .read_session(canon_project.to_str().unwrap(), session_uuid)
            .expect("Pi reader accepts our output");
        assert_eq!(session.header.id, session_uuid);
        assert_eq!(session.header.cwd, canon_project.to_string_lossy());

        let user_texts: Vec<String> = session
            .entries
            .iter()
            .filter_map(|e| match e {
                toolpath_pi::Entry::Message {
                    message: toolpath_pi::AgentMessage::User { content, .. },
                    ..
                } => match content {
                    toolpath_pi::types::MessageContent::Text(s) => Some(s.clone()),
                    toolpath_pi::types::MessageContent::Blocks(blocks) => Some(
                        blocks
                            .iter()
                            .filter_map(|b| match b {
                                toolpath_pi::ContentBlock::Text { text, .. } => Some(text.clone()),
                                _ => None,
                            })
                            .collect::<Vec<_>>()
                            .join("\n"),
                    ),
                },
                _ => None,
            })
            .collect();
        assert_eq!(user_texts, vec!["Hello pi".to_string()]);
    }

    #[test]
    fn pi_rejects_non_single_path_graph() {
        let temp = tempfile::tempdir().unwrap();
        let input_path = temp.path().join("input.json");
        let empty_graph = serde_json::json!({
            "graph": { "id": "g1" },
            "paths": [],
        });
        std::fs::write(&input_path, empty_graph.to_string()).unwrap();

        let project = temp.path().join("proj");
        std::fs::create_dir_all(&project).unwrap();
        let err = run_pi(
            input_path.to_string_lossy().to_string(),
            Some(project),
            None,
        )
        .expect_err("should reject empty graph");
        assert!(err.to_string().contains("single-path"));
    }

    #[test]
    fn pi_output_to_file_writes_jsonl() {
        let temp = tempfile::tempdir().unwrap();
        let out_path = temp.path().join("out").join("pi.jsonl");

        let session_uuid = "pi-out-test";
        let artifact = format!("pi://{}", session_uuid);
        let mut extra = HashMap::new();
        extra.insert("role".into(), serde_json::json!("user"));
        extra.insert("text".into(), serde_json::json!("hi"));
        let step = Step {
            step: StepIdentity {
                id: "step-001".into(),
                parents: vec![],
                actor: "human:alex".into(),
                timestamp: "2026-04-17T15:00:00Z".into(),
            },
            change: {
                let mut m = HashMap::new();
                m.insert(
                    artifact,
                    ArtifactChange {
                        raw: None,
                        structural: Some(StructuralChange {
                            change_type: "conversation.append".into(),
                            extra,
                        }),
                    },
                );
                m
            },
            meta: None,
        };
        let graph = toolpath::v1::Graph::from_path(toolpath::v1::Path {
            path: PathIdentity {
                id: "test-path".into(),
                base: None,
                head: "step-001".into(),
                graph_ref: None,
            },
            steps: vec![step],
            meta: None,
        });
        let input_path = temp.path().join("doc.json");
        std::fs::write(&input_path, graph.to_json().unwrap()).unwrap();

        run_pi(
            input_path.to_string_lossy().to_string(),
            None,
            Some(out_path.clone()),
        )
        .expect("export pi --output");

        assert!(out_path.exists(), "output file missing");
        let body = std::fs::read_to_string(&out_path).unwrap();
        let lines: Vec<&str> = body.lines().collect();
        assert!(lines.len() >= 2);
        assert!(lines[0].contains("\"type\":\"session\""));
        assert!(lines[1].contains("\"role\":\"user\""));

        let session = toolpath_pi::reader::read_session_from_file(&out_path)
            .expect("Pi reader accepts the JSONL");
        assert_eq!(session.header.id, session_uuid);
    }

    #[test]
    fn codex_output_to_file_writes_jsonl() {
        let temp = tempfile::tempdir().unwrap();
        let out_path = temp.path().join("out").join("codex.jsonl");

        let session_uuid = "019dabc6-8fef-7681-a054-b5bb75fcb97d";
        let artifact = format!("codex://{}", session_uuid);
        let mut extra = HashMap::new();
        extra.insert("role".into(), serde_json::json!("user"));
        extra.insert("text".into(), serde_json::json!("hello codex"));
        let step = Step {
            step: StepIdentity {
                id: "step-001".into(),
                parents: vec![],
                actor: "human:alex".into(),
                timestamp: "2026-04-20T16:00:00Z".into(),
            },
            change: {
                let mut m = HashMap::new();
                m.insert(
                    artifact,
                    ArtifactChange {
                        raw: None,
                        structural: Some(StructuralChange {
                            change_type: "conversation.append".into(),
                            extra,
                        }),
                    },
                );
                m
            },
            meta: None,
        };
        let graph = toolpath::v1::Graph::from_path(toolpath::v1::Path {
            path: PathIdentity {
                id: "test-path".into(),
                base: None,
                head: "step-001".into(),
                graph_ref: None,
            },
            steps: vec![step],
            meta: None,
        });
        let input_path = temp.path().join("doc.json");
        std::fs::write(&input_path, graph.to_json().unwrap()).unwrap();

        run_codex(
            input_path.to_string_lossy().to_string(),
            None,
            Some(out_path.clone()),
        )
        .expect("export codex --output");

        assert!(out_path.exists(), "output file missing");
        let body = std::fs::read_to_string(&out_path).unwrap();
        let lines: Vec<&str> = body.lines().collect();
        assert_eq!(lines.len(), 4, "got {} lines: {:?}", lines.len(), lines);
        assert!(lines[0].contains("\"type\":\"session_meta\""));
        assert_eq!(
            serde_json::from_str::<serde_json::Value>(lines[0]).unwrap()["payload"]["id"].as_str(),
            Some(session_uuid)
        );
        assert!(lines[1].contains("\"type\":\"turn_context\""));
        assert!(lines[2].contains("\"type\":\"response_item\""));
        assert!(lines[3].contains("\"type\":\"event_msg\""));
        assert!(lines[3].contains("\"type\":\"user_message\""));

        let reread = toolpath_codex::RolloutReader::read_session(&out_path)
            .expect("Codex reader accepts the JSONL");
        assert_eq!(reread.id, session_uuid);
        assert_eq!(reread.lines.len(), 4);
    }

    #[test]
    fn codex_writes_into_dated_sessions_dir_with_project() {
        // `--project DIR` mode writes to
        // `~/.codex/sessions/YYYY/MM/DD/rollout-*.jsonl`. The
        // resolver's HOME-based default is overridden via $HOME.
        use toolpath_codex::PathResolver;

        let temp = tempfile::tempdir().unwrap();
        let fake_home = temp.path().join("home");
        std::fs::create_dir_all(&fake_home).unwrap();
        let project_dir = temp.path().join("myproj");
        std::fs::create_dir_all(&project_dir).unwrap();

        let session_uuid = "019dabc6-aaaa-bbbb-cccc-ddddeeeefff0";
        let artifact = format!("codex://{}", session_uuid);
        let mut extra = HashMap::new();
        extra.insert("role".into(), serde_json::json!("user"));
        extra.insert("text".into(), serde_json::json!("hi codex via project"));
        let step = Step {
            step: StepIdentity {
                id: "step-001".into(),
                parents: vec![],
                actor: "human:alex".into(),
                timestamp: "2026-05-15T10:30:00.000Z".into(),
            },
            change: {
                let mut m = HashMap::new();
                m.insert(
                    artifact,
                    ArtifactChange {
                        raw: None,
                        structural: Some(StructuralChange {
                            change_type: "conversation.append".into(),
                            extra,
                        }),
                    },
                );
                m
            },
            meta: None,
        };
        let graph = toolpath::v1::Graph::from_path(toolpath::v1::Path {
            path: PathIdentity {
                id: "test-path".into(),
                base: None,
                head: "step-001".into(),
                graph_ref: None,
            },
            steps: vec![step],
            meta: None,
        });
        let input_path = temp.path().join("doc.json");
        std::fs::write(&input_path, graph.to_json().unwrap()).unwrap();

        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prior_home = std::env::var_os("HOME");
        unsafe {
            std::env::set_var("HOME", &fake_home);
        }
        let result = run_codex(
            input_path.to_string_lossy().to_string(),
            Some(project_dir.clone()),
            None,
        );
        unsafe {
            match prior_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
        }
        result.expect("export codex --project");

        let resolver = PathResolver::new().with_home(&fake_home);
        let dated_dir = resolver
            .sessions_root()
            .unwrap()
            .join("2026")
            .join("05")
            .join("15");
        assert!(
            dated_dir.exists(),
            "expected dated sessions dir at {}",
            dated_dir.display()
        );
        let rollout_files: Vec<PathBuf> = std::fs::read_dir(&dated_dir)
            .unwrap()
            .filter_map(|e| e.ok().map(|e| e.path()))
            .filter(|p| {
                p.is_file()
                    && p.file_name()
                        .and_then(|n| n.to_str())
                        .is_some_and(|s| s.starts_with("rollout-") && s.ends_with(".jsonl"))
            })
            .collect();
        assert_eq!(rollout_files.len(), 1, "expected one rollout-*.jsonl");

        let name = rollout_files[0]
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap();
        assert!(name.contains("2026-05-15T10-30-00"), "got name: {}", name);
        assert!(
            name.contains("019dabc6"),
            "filename should embed the uuid prefix; got {}",
            name
        );

        let reread =
            toolpath_codex::RolloutReader::read_session(&rollout_files[0]).expect("read back");
        assert_eq!(reread.id, session_uuid);
    }

    #[test]
    fn codex_rejects_non_single_path_graph() {
        let temp = tempfile::tempdir().unwrap();
        let input_path = temp.path().join("input.json");
        let empty_graph = serde_json::json!({
            "graph": { "id": "g1" },
            "paths": [],
        });
        std::fs::write(&input_path, empty_graph.to_string()).unwrap();

        let project = temp.path().join("proj");
        std::fs::create_dir_all(&project).unwrap();
        let err = run_codex(
            input_path.to_string_lossy().to_string(),
            Some(project),
            None,
        )
        .expect_err("should reject empty graph");
        assert!(err.to_string().contains("single-path"));
    }

    #[test]
    fn pathbase_repo_flag_requires_login() {
        // With explicit --repo (i.e. an authenticated upload), missing
        // credentials must surface the "Not logged in" error rather than
        // silently falling through to the anonymous endpoint.
        let temp = tempfile::tempdir().unwrap();
        let input_path = temp.path().join("input.json");
        std::fs::write(
            &input_path,
            serde_json::to_string(&make_path_doc()).unwrap(),
        )
        .unwrap();

        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        unsafe {
            std::env::set_var(crate::config::CONFIG_DIR_ENV, temp.path());
        }
        let err = run_pathbase(PathbaseExportArgs {
            input: input_path.to_string_lossy().to_string(),
            url: Some("http://127.0.0.1:1".to_string()),
            anon: false,
            repo: Some(RepoSpec {
                owner: "alex".to_string(),
                name: "pathstash".to_string(),
            }),
            name: None,
            public: false,
        })
        .unwrap_err();
        unsafe {
            std::env::remove_var(crate::config::CONFIG_DIR_ENV);
        }
        assert!(
            err.to_string().contains("Not logged in"),
            "expected `Not logged in` error, got: {err}"
        );
    }

    #[test]
    fn parse_repo_spec_accepts_owner_slash_name() {
        let spec = parse_repo_spec("alex/pathstash").unwrap();
        assert_eq!(spec.owner, "alex");
        assert_eq!(spec.name, "pathstash");
    }

    #[test]
    fn parse_repo_spec_rejects_missing_slash() {
        assert!(parse_repo_spec("alex").is_err());
        assert!(parse_repo_spec("/pathstash").is_err());
        assert!(parse_repo_spec("alex/").is_err());
    }

    #[test]
    fn derive_name_uses_path_id() {
        let doc = make_path_doc();
        assert_eq!(derive_name(&doc), "test-path");
    }

    #[test]
    fn derive_name_sanitizes_non_url_safe_chars() {
        use toolpath::v1::{Graph, Path, PathIdentity};
        let doc = Graph::from_path(Path {
            path: PathIdentity {
                id: "claude/Path 42!".into(),
                base: None,
                head: "h".into(),
                graph_ref: None,
            },
            steps: vec![],
            meta: None,
        });
        assert_eq!(derive_name(&doc), "claude-path-42");
    }

    #[test]
    fn derive_name_falls_back_to_content_hash_when_id_empties_out() {
        // An id consisting entirely of non-url-safe characters sanitizes to
        // empty; we fall back to a deterministic content-hash label.
        use toolpath::v1::{Graph, Path, PathIdentity};
        let doc = Graph::from_path(Path {
            path: PathIdentity {
                id: "✨🚀🦀".into(),
                base: None,
                head: "h".into(),
                graph_ref: None,
            },
            steps: vec![],
            meta: None,
        });
        let s1 = derive_name(&doc);
        let s2 = derive_name(&doc);
        assert_eq!(s1, s2, "fallback name must be deterministic across calls");
        assert!(s1.starts_with("path-"), "got {s1}");
        assert_eq!(s1.len(), "path-".len() + 12, "got {s1}");
        assert!(
            s1.chars().skip(5).all(|c| c.is_ascii_hexdigit()),
            "got {s1}"
        );
    }

    #[test]
    fn derive_name_fallback_differs_across_documents() {
        use toolpath::v1::{Graph, Path, PathIdentity};
        let mk = |head: &str| {
            Graph::from_path(Path {
                path: PathIdentity {
                    id: "—".into(), // sanitizes to empty for both
                    base: None,
                    head: head.into(),
                    graph_ref: None,
                },
                steps: vec![],
                meta: None,
            })
        };
        assert_ne!(derive_name(&mk("a")), derive_name(&mk("b")));
    }
    #[test]
    fn opencode_output_to_file_writes_session_json() {
        let temp = tempfile::tempdir().unwrap();
        let input_path = temp.path().join("input.json");
        let out_path = temp.path().join("session.json");

        std::fs::write(
            &input_path,
            serde_json::to_string(&make_path_doc()).unwrap(),
        )
        .unwrap();

        run_opencode(
            input_path.to_string_lossy().to_string(),
            None,
            Some(out_path.clone()),
        )
        .unwrap();

        assert!(out_path.exists());
        let body = std::fs::read_to_string(&out_path).unwrap();
        let session: toolpath_opencode::Session = serde_json::from_str(&body).unwrap();
        assert!(session.id.starts_with("ses_"));
        assert_eq!(session.messages.len(), 1);
        assert!(matches!(
            session.messages[0].data,
            toolpath_opencode::MessageData::User(_)
        ));
    }

    #[test]
    fn opencode_writes_into_db_with_project() {
        let temp = tempfile::tempdir().unwrap();
        let fake_home = temp.path().join("home");
        std::fs::create_dir_all(&fake_home).unwrap();
        let project_dir = temp.path().join("myproj");
        std::fs::create_dir_all(&project_dir).unwrap();
        let data_dir = fake_home.join(".local/share/opencode");
        std::fs::create_dir_all(&data_dir).unwrap();

        let conn = rusqlite::Connection::open(data_dir.join("opencode.db")).unwrap();
        conn.execute_batch(
            r#"
            CREATE TABLE project (
              id text PRIMARY KEY, worktree text NOT NULL, vcs text, name text,
              icon_url text, icon_color text,
              time_created integer NOT NULL, time_updated integer NOT NULL,
              time_initialized integer, sandboxes text NOT NULL, commands text
            );
            CREATE TABLE session (
              id text PRIMARY KEY, project_id text NOT NULL, parent_id text,
              slug text NOT NULL, directory text NOT NULL, title text NOT NULL,
              version text NOT NULL, share_url text,
              summary_additions integer, summary_deletions integer,
              summary_files integer, summary_diffs text, revert text, permission text,
              time_created integer NOT NULL, time_updated integer NOT NULL,
              time_compacting integer, time_archived integer, workspace_id text
            );
            CREATE TABLE message (
              id text PRIMARY KEY, session_id text NOT NULL,
              time_created integer NOT NULL, time_updated integer NOT NULL,
              data text NOT NULL
            );
            CREATE TABLE part (
              id text PRIMARY KEY, message_id text NOT NULL, session_id text NOT NULL,
              time_created integer NOT NULL, time_updated integer NOT NULL,
              data text NOT NULL
            );
            "#,
        )
        .unwrap();
        drop(conn);

        let input_path = temp.path().join("input.json");
        std::fs::write(
            &input_path,
            serde_json::to_string(&make_path_doc()).unwrap(),
        )
        .unwrap();

        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prev_home = std::env::var_os("HOME");
        let prev_xdg = std::env::var_os("XDG_DATA_HOME");
        unsafe {
            std::env::set_var("HOME", &fake_home);
            std::env::remove_var("XDG_DATA_HOME");
        }
        let result = run_opencode(
            input_path.to_string_lossy().to_string(),
            Some(project_dir.clone()),
            None,
        );
        unsafe {
            match prev_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
            match prev_xdg {
                Some(v) => std::env::set_var("XDG_DATA_HOME", v),
                None => std::env::remove_var("XDG_DATA_HOME"),
            }
        }
        result.expect("export opencode --project");

        let conn = rusqlite::Connection::open(data_dir.join("opencode.db")).unwrap();
        let session_count: i64 = conn
            .query_row("SELECT COUNT(*) FROM session", [], |r| r.get(0))
            .unwrap();
        assert_eq!(session_count, 1);
        let message_count: i64 = conn
            .query_row("SELECT COUNT(*) FROM message", [], |r| r.get(0))
            .unwrap();
        assert!(message_count >= 1);
        let part_count: i64 = conn
            .query_row("SELECT COUNT(*) FROM part", [], |r| r.get(0))
            .unwrap();
        assert!(part_count >= 1);
    }

    #[test]
    fn opencode_rejects_non_single_path_graph() {
        let temp = tempfile::tempdir().unwrap();
        let input_path = temp.path().join("input.json");
        let empty_graph = serde_json::json!({
            "graph": { "id": "g1" },
            "paths": [],
        });
        std::fs::write(&input_path, empty_graph.to_string()).unwrap();

        let err = run_opencode(input_path.to_string_lossy().to_string(), None, None).unwrap_err();
        assert!(err.to_string().contains("single-path"));
    }

    // ── project_<harness> wrapper tests ──────────────────────────────

    /// Build a minimal `toolpath::v1::Path` with a single `conversation.append`
    /// step using the given `artifact_key` (e.g. `"claude-code://my-session"`).
    /// The projectors read `view.id` from the first `<provider>://<id>` artifact
    /// key they see, so this gives them a non-empty session id to work with.
    fn make_convo_path(artifact_key: &str) -> toolpath::v1::Path {
        let mut extra = HashMap::new();
        extra.insert("role".to_string(), serde_json::json!("user"));
        extra.insert("text".to_string(), serde_json::json!("hello"));
        let step = toolpath::v1::Step {
            step: toolpath::v1::StepIdentity {
                id: "s1".to_string(),
                parents: vec![],
                actor: "human:test".to_string(),
                timestamp: "2026-01-01T00:00:00Z".to_string(),
            },
            change: {
                let mut m = HashMap::new();
                m.insert(
                    artifact_key.to_string(),
                    toolpath::v1::ArtifactChange {
                        raw: None,
                        structural: Some(toolpath::v1::StructuralChange {
                            change_type: "conversation.append".to_string(),
                            extra,
                        }),
                    },
                );
                m
            },
            meta: None,
        };
        toolpath::v1::Path {
            path: toolpath::v1::PathIdentity {
                id: "test-path".to_string(),
                base: None,
                head: "s1".to_string(),
                graph_ref: None,
            },
            steps: vec![step],
            meta: None,
        }
    }

    #[test]
    fn project_claude_returns_session_id_and_writes_jsonl() {
        let temp = tempfile::tempdir().unwrap();
        let fake_home = temp.path().join("home");
        std::fs::create_dir_all(&fake_home).unwrap();
        let cwd = temp.path().join("proj");
        std::fs::create_dir_all(&cwd).unwrap();

        // Use a deterministic session id embedded in the artifact key.
        let session_id = "claude-wrapper-test-session";
        let path = make_convo_path(&format!("claude-code://{}", session_id));

        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prior_home = std::env::var_os("HOME");
        unsafe {
            std::env::set_var("HOME", &fake_home);
        }
        let result = project_claude(&path, &cwd);
        unsafe {
            match prior_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
        }

        let returned_id = result.expect("project_claude should succeed");
        assert_eq!(returned_id, session_id);

        let claude_projects = fake_home.join(".claude/projects");
        assert!(
            claude_projects.exists(),
            "claude projects dir missing under HOME"
        );
    }

    #[test]
    fn project_gemini_returns_session_id_and_writes_chat_file() {
        let temp = tempfile::tempdir().unwrap();
        let fake_home = temp.path().join("home");
        std::fs::create_dir_all(&fake_home).unwrap();
        let cwd = temp.path().join("proj");
        std::fs::create_dir_all(&cwd).unwrap();

        let session_uuid = "11111111-2222-3333-4444-aaaaaaaaaaaa";
        let path = make_convo_path(&format!("gemini-cli://{}", session_uuid));

        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prior_home = std::env::var_os("HOME");
        unsafe {
            std::env::set_var("HOME", &fake_home);
        }
        let result = project_gemini(&path, &cwd);
        unsafe {
            match prior_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
        }

        let returned_id = result.expect("project_gemini should succeed");
        assert_eq!(returned_id, session_uuid);

        let gemini_tmp = fake_home.join(".gemini/tmp");
        assert!(gemini_tmp.exists(), "gemini tmp dir missing under HOME");
    }

    #[test]
    fn project_codex_returns_session_id_and_writes_rollout() {
        let temp = tempfile::tempdir().unwrap();
        let fake_home = temp.path().join("home");
        std::fs::create_dir_all(&fake_home).unwrap();
        let cwd = temp.path().join("proj");
        std::fs::create_dir_all(&cwd).unwrap();

        let session_uuid = "019dabc6-cccc-dddd-eeee-ffffffffffff";
        let path = make_convo_path(&format!("codex://{}", session_uuid));

        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prior_home = std::env::var_os("HOME");
        unsafe {
            std::env::set_var("HOME", &fake_home);
        }
        let result = project_codex(&path, &cwd);
        unsafe {
            match prior_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
        }

        let returned_id = result.expect("project_codex should succeed");
        assert_eq!(returned_id, session_uuid);

        let codex_sessions = fake_home.join(".codex/sessions");
        assert!(codex_sessions.exists(), "codex sessions dir missing");
    }

    #[test]
    fn project_opencode_returns_session_id_and_inserts_row() {
        let temp = tempfile::tempdir().unwrap();
        let fake_home = temp.path().join("home");
        std::fs::create_dir_all(&fake_home).unwrap();
        let cwd = temp.path().join("proj");
        std::fs::create_dir_all(&cwd).unwrap();

        // Bootstrap the opencode DB (no public schema helper exists; inline
        // the same DDL used in the existing opencode_writes_into_db_with_project test).
        let data_dir = fake_home.join(".local/share/opencode");
        std::fs::create_dir_all(&data_dir).unwrap();
        let db_path = data_dir.join("opencode.db");
        {
            let conn = rusqlite::Connection::open(&db_path).unwrap();
            conn.execute_batch(
                r#"
                CREATE TABLE project (
                  id text PRIMARY KEY, worktree text NOT NULL, vcs text, name text,
                  icon_url text, icon_color text,
                  time_created integer NOT NULL, time_updated integer NOT NULL,
                  time_initialized integer, sandboxes text NOT NULL, commands text
                );
                CREATE TABLE session (
                  id text PRIMARY KEY, project_id text NOT NULL, parent_id text,
                  slug text NOT NULL, directory text NOT NULL, title text NOT NULL,
                  version text NOT NULL, share_url text,
                  summary_additions integer, summary_deletions integer,
                  summary_files integer, summary_diffs text, revert text, permission text,
                  time_created integer NOT NULL, time_updated integer NOT NULL,
                  time_compacting integer, time_archived integer, workspace_id text
                );
                CREATE TABLE message (
                  id text PRIMARY KEY, session_id text NOT NULL,
                  time_created integer NOT NULL, time_updated integer NOT NULL,
                  data text NOT NULL
                );
                CREATE TABLE part (
                  id text PRIMARY KEY, message_id text NOT NULL, session_id text NOT NULL,
                  time_created integer NOT NULL, time_updated integer NOT NULL,
                  data text NOT NULL
                );
                "#,
            )
            .unwrap();
        }

        // opencode session ids are derived from view.id via mint_session_id,
        // which adds the `ses_` prefix if not already present.
        let path = make_convo_path("opencode://ses_wrapper-test");

        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prior_home = std::env::var_os("HOME");
        let prior_xdg = std::env::var_os("XDG_DATA_HOME");
        unsafe {
            std::env::set_var("HOME", &fake_home);
            std::env::remove_var("XDG_DATA_HOME");
        }
        let result = project_opencode(&path, &cwd);
        unsafe {
            match prior_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
            match prior_xdg {
                Some(v) => std::env::set_var("XDG_DATA_HOME", v),
                None => std::env::remove_var("XDG_DATA_HOME"),
            }
        }

        let returned_id = result.expect("project_opencode should succeed");
        assert_eq!(returned_id, "ses_wrapper-test");

        let conn = rusqlite::Connection::open(&db_path).unwrap();
        let count: i64 = conn
            .query_row(
                "SELECT COUNT(*) FROM session WHERE id = ?1",
                [&returned_id],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(count, 1, "expected one session row with id {returned_id}");
    }

    #[test]
    fn project_pi_returns_session_id_and_writes_jsonl() {
        let temp = tempfile::tempdir().unwrap();
        let fake_home = temp.path().join("home");
        std::fs::create_dir_all(&fake_home).unwrap();
        let cwd = temp.path().join("proj");
        std::fs::create_dir_all(&cwd).unwrap();

        let session_id = "pi-wrapper-test-session";
        let path = make_convo_path(&format!("pi://{}", session_id));

        let _g = crate::config::TEST_ENV_LOCK
            .lock()
            .unwrap_or_else(|e| e.into_inner());
        let prior_home = std::env::var_os("HOME");
        unsafe {
            std::env::set_var("HOME", &fake_home);
        }
        let result = project_pi(&path, &cwd);
        unsafe {
            match prior_home {
                Some(v) => std::env::set_var("HOME", v),
                None => std::env::remove_var("HOME"),
            }
        }

        let returned_id = result.expect("project_pi should succeed");
        assert_eq!(returned_id, session_id);

        let pi_sessions = fake_home.join(".pi/agent/sessions");
        assert!(pi_sessions.exists(), "pi sessions dir missing");
    }
}