chasm-cli 2.0.0

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

use crate::error::{CsmError, Result};
use crate::models::{
    ChatRequest, ChatSession, ChatSessionIndex, ChatSessionIndexEntry, ChatSessionTiming,
    ModelCacheEntry, StateCacheEntry,
};
use crate::workspace::{get_empty_window_sessions_path, get_workspace_storage_path};
use base64::{engine::general_purpose::STANDARD as BASE64, Engine};
use once_cell::sync::Lazy;
use regex::Regex;
use rusqlite::Connection;
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use sysinfo::System;

/// A single issue detected during workspace session diagnostics
#[derive(Debug, Clone)]
pub struct SessionIssue {
    /// The session file stem (UUID)
    pub session_id: String,
    /// Category of issue
    pub kind: SessionIssueKind,
    /// Human-readable description
    pub detail: String,
}

/// Categories of session issues that can be detected and auto-fixed
#[derive(Debug, Clone, PartialEq)]
pub enum SessionIssueKind {
    /// JSONL file has multiple lines (operations not compacted)
    MultiLineJsonl,
    /// JSONL first line contains concatenated JSON objects (missing newlines)
    ConcatenatedJsonl,
    /// Index entry has lastResponseState = 2 (Cancelled), blocks VS Code loading
    CancelledState,
    /// Last request's modelState.value is 2 (Cancelled) or missing in file content
    CancelledModelState,
    /// File exists on disk but is not in the VS Code index
    OrphanedSession,
    /// Index entry references a file that no longer exists on disk
    StaleIndexEntry,
    /// Session is missing required VS Code compat fields
    MissingCompatFields,
    /// Both .json and .jsonl exist for the same session ID
    DuplicateFormat,
    /// Legacy .json file is corrupted — contains only structural chars ({}, whitespace)
    SkeletonJson,
}

impl std::fmt::Display for SessionIssueKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SessionIssueKind::MultiLineJsonl => write!(f, "multi-line JSONL"),
            SessionIssueKind::ConcatenatedJsonl => write!(f, "concatenated JSONL"),
            SessionIssueKind::CancelledState => write!(f, "cancelled state"),
            SessionIssueKind::CancelledModelState => write!(f, "cancelled modelState in file"),
            SessionIssueKind::OrphanedSession => write!(f, "orphaned session"),
            SessionIssueKind::StaleIndexEntry => write!(f, "stale index entry"),
            SessionIssueKind::MissingCompatFields => write!(f, "missing compat fields"),
            SessionIssueKind::DuplicateFormat => write!(f, "duplicate .json/.jsonl"),
            SessionIssueKind::SkeletonJson => write!(f, "skeleton .json (corrupt)"),
        }
    }
}

/// Summary of issues found in a single workspace
#[derive(Debug, Clone, Default)]
pub struct WorkspaceDiagnosis {
    /// Project path (if known)
    pub project_path: Option<String>,
    /// Workspace hash
    pub workspace_hash: String,
    /// Total sessions on disk
    pub sessions_on_disk: usize,
    /// Total sessions in index
    pub sessions_in_index: usize,
    /// All detected issues
    pub issues: Vec<SessionIssue>,
}

impl WorkspaceDiagnosis {
    pub fn is_healthy(&self) -> bool {
        self.issues.is_empty()
    }

    pub fn issue_count_by_kind(&self, kind: &SessionIssueKind) -> usize {
        self.issues.iter().filter(|i| &i.kind == kind).count()
    }
}

/// Diagnose a workspace for session issues without modifying anything.
/// Returns a structured report of all detected problems.
pub fn diagnose_workspace_sessions(
    workspace_id: &str,
    chat_sessions_dir: &Path,
) -> Result<WorkspaceDiagnosis> {
    let mut diagnosis = WorkspaceDiagnosis {
        workspace_hash: workspace_id.to_string(),
        ..Default::default()
    };

    if !chat_sessions_dir.exists() {
        return Ok(diagnosis);
    }

    // Collect session files on disk
    let mut jsonl_sessions: HashSet<String> = HashSet::new();
    let mut json_sessions: HashSet<String> = HashSet::new();
    let mut all_session_ids: HashSet<String> = HashSet::new();

    for entry in std::fs::read_dir(chat_sessions_dir)? {
        let entry = entry?;
        let path = entry.path();
        let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
        let stem = path
            .file_stem()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_default();

        match ext {
            "jsonl" => {
                jsonl_sessions.insert(stem.clone());
                all_session_ids.insert(stem);
            }
            "json" if !path.to_string_lossy().ends_with(".bak") => {
                json_sessions.insert(stem.clone());
                all_session_ids.insert(stem);
            }
            _ => {}
        }
    }
    diagnosis.sessions_on_disk = all_session_ids.len();

    // Check for duplicate .json/.jsonl files
    for id in &jsonl_sessions {
        if json_sessions.contains(id) {
            diagnosis.issues.push(SessionIssue {
                session_id: id.clone(),
                kind: SessionIssueKind::DuplicateFormat,
                detail: format!("Both {id}.json and {id}.jsonl exist"),
            });
        }
    }

    // Check JSONL files for content issues
    for id in &jsonl_sessions {
        let path = chat_sessions_dir.join(format!("{id}.jsonl"));
        if let Ok(content) = std::fs::read_to_string(&path) {
            let line_count = content.lines().count();

            if line_count > 1 {
                let size_mb = content.len() / (1024 * 1024);
                diagnosis.issues.push(SessionIssue {
                    session_id: id.clone(),
                    kind: SessionIssueKind::MultiLineJsonl,
                    detail: format!("{line_count} lines, ~{size_mb} MB — needs compaction"),
                });
            }

            // Check first line for concatenation
            if let Some(first_line) = content.lines().next() {
                if first_line.contains("}{\"kind\":") {
                    diagnosis.issues.push(SessionIssue {
                        session_id: id.clone(),
                        kind: SessionIssueKind::ConcatenatedJsonl,
                        detail: "First line has concatenated JSON objects".to_string(),
                    });
                }
            }

            // Check for missing compat fields (only single-line files worth checking)
            if line_count == 1 {
                if let Some(first_line) = content.lines().next() {
                    if let Ok(obj) = serde_json::from_str::<serde_json::Value>(first_line) {
                        let is_kind_0 = obj
                            .get("kind")
                            .and_then(|k| k.as_u64())
                            .map(|k| k == 0)
                            .unwrap_or(false);

                        if is_kind_0 {
                            if let Some(v) = obj.get("v") {
                                let missing_fields: Vec<&str> = [
                                    "hasPendingEdits",
                                    "pendingRequests",
                                    "inputState",
                                    "sessionId",
                                    "version",
                                ]
                                .iter()
                                .filter(|f| v.get(**f).is_none())
                                .copied()
                                .collect();

                                if !missing_fields.is_empty() {
                                    diagnosis.issues.push(SessionIssue {
                                        session_id: id.clone(),
                                        kind: SessionIssueKind::MissingCompatFields,
                                        detail: format!("Missing: {}", missing_fields.join(", ")),
                                    });
                                }

                                // Check for cancelled modelState in file content
                                if let Some(requests) = v.get("requests").and_then(|r| r.as_array())
                                {
                                    if let Some(last_req) = requests.last() {
                                        let model_state_value = last_req
                                            .get("modelState")
                                            .and_then(|ms| ms.get("value"))
                                            .and_then(|v| v.as_u64());
                                        match model_state_value {
                                            Some(1) => {} // Complete — valid
                                            Some(v) => {
                                                diagnosis.issues.push(SessionIssue {
                                                    session_id: id.clone(),
                                                    kind: SessionIssueKind::CancelledModelState,
                                                    detail: format!("Last request modelState.value={} (not Complete) in file content", v),
                                                });
                                            }
                                            None => {
                                                diagnosis.issues.push(SessionIssue {
                                                    session_id: id.clone(),
                                                    kind: SessionIssueKind::CancelledModelState,
                                                    detail: "Last request missing modelState in file content".to_string(),
                                                });
                                            }
                                        }
                                    }
                                }

                                // Check hasPendingEdits — true blocks session loading
                                if v.get("hasPendingEdits")
                                    .and_then(|v| v.as_bool())
                                    .unwrap_or(false)
                                    == true
                                {
                                    diagnosis.issues.push(SessionIssue {
                                        session_id: id.clone(),
                                        kind: SessionIssueKind::MissingCompatFields,
                                        detail: "hasPendingEdits is true (blocks session loading)"
                                            .to_string(),
                                    });
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    // Check .json files for skeleton corruption
    for id in &json_sessions {
        // Skip if a .jsonl already exists (it takes precedence)
        if jsonl_sessions.contains(id) {
            continue;
        }
        let path = chat_sessions_dir.join(format!("{id}.json"));
        if let Ok(content) = std::fs::read_to_string(&path) {
            if is_skeleton_json(&content) {
                diagnosis.issues.push(SessionIssue {
                    session_id: id.clone(),
                    kind: SessionIssueKind::SkeletonJson,
                    detail: format!(
                        "Legacy .json is corrupt — only structural chars remain ({} bytes)",
                        content.len()
                    ),
                });
            }
        }
    }

    // Check index for stale entries, orphans, and cancelled state
    let db_path = get_workspace_storage_db(workspace_id)?;
    if db_path.exists() {
        if let Ok(index) = read_chat_session_index(&db_path) {
            diagnosis.sessions_in_index = index.entries.len();

            // Stale index entries (in index but no file on disk)
            for (id, _entry) in &index.entries {
                if !all_session_ids.contains(id) {
                    diagnosis.issues.push(SessionIssue {
                        session_id: id.clone(),
                        kind: SessionIssueKind::StaleIndexEntry,
                        detail: "In index but no file on disk".to_string(),
                    });
                }
            }

            // Cancelled state entries
            for (id, entry) in &index.entries {
                if entry.last_response_state == 2 {
                    diagnosis.issues.push(SessionIssue {
                        session_id: id.clone(),
                        kind: SessionIssueKind::CancelledState,
                        detail: "lastResponseState=2 (Cancelled) — blocks VS Code loading"
                            .to_string(),
                    });
                }
            }

            // Orphaned sessions (on disk but not in index)
            let indexed_ids: HashSet<&String> = index.entries.keys().collect();
            for id in &all_session_ids {
                if !indexed_ids.contains(id) {
                    diagnosis.issues.push(SessionIssue {
                        session_id: id.clone(),
                        kind: SessionIssueKind::OrphanedSession,
                        detail: "File on disk but not in VS Code index".to_string(),
                    });
                }
            }
        }
    }

    Ok(diagnosis)
}

/// Regex to match any Unicode escape sequence (valid or not)
static UNICODE_ESCAPE_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"\\u[0-9a-fA-F]{4}").unwrap());

/// VS Code session format version - helps identify which parsing strategy to use
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VsCodeSessionFormat {
    /// Legacy JSON format (VS Code < 1.109.0)
    /// Single JSON object with ChatSession structure
    LegacyJson,
    /// JSONL format (VS Code >= 1.109.0, January 2026+)
    /// JSON Lines with event sourcing: kind 0 (initial), kind 1 (delta), kind 2 (replace/splice)
    JsonLines,
}

/// Session schema version - tracks the internal structure version
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum SessionSchemaVersion {
    /// Version 1 - Original format (basic fields)
    V1 = 1,
    /// Version 2 - Added more metadata fields
    V2 = 2,
    /// Version 3 - Current format with full request/response structure
    V3 = 3,
    /// Unknown version
    Unknown = 0,
}

impl SessionSchemaVersion {
    /// Create from version number
    pub fn from_version(v: u32) -> Self {
        match v {
            1 => Self::V1,
            2 => Self::V2,
            3 => Self::V3,
            _ => Self::Unknown,
        }
    }

    /// Get version number
    pub fn version_number(&self) -> u32 {
        match self {
            Self::V1 => 1,
            Self::V2 => 2,
            Self::V3 => 3,
            Self::Unknown => 0,
        }
    }

    /// Get description
    pub fn description(&self) -> &'static str {
        match self {
            Self::V1 => "v1 (basic)",
            Self::V2 => "v2 (extended metadata)",
            Self::V3 => "v3 (full structure)",
            Self::Unknown => "unknown",
        }
    }
}

impl std::fmt::Display for SessionSchemaVersion {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

/// Result of session format detection
#[derive(Debug, Clone)]
pub struct SessionFormatInfo {
    /// File format (JSON or JSONL)
    pub format: VsCodeSessionFormat,
    /// Schema version detected from content
    pub schema_version: SessionSchemaVersion,
    /// Confidence level (0.0 - 1.0)
    pub confidence: f32,
    /// Detection method used
    pub detection_method: &'static str,
}

impl VsCodeSessionFormat {
    /// Detect format from file path (by extension)
    pub fn from_path(path: &Path) -> Self {
        match path.extension().and_then(|e| e.to_str()) {
            Some("jsonl") => Self::JsonLines,
            _ => Self::LegacyJson,
        }
    }

    /// Detect format from content by analyzing structure
    pub fn from_content(content: &str) -> Self {
        let trimmed = content.trim();

        // JSONL: Multiple lines starting with { or first line has {"kind":
        if trimmed.starts_with("{\"kind\":") || trimmed.starts_with("{ \"kind\":") {
            return Self::JsonLines;
        }

        // Count lines that look like JSON objects
        let mut json_object_lines = 0;
        let mut total_non_empty_lines = 0;

        for line in trimmed.lines().take(10) {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }
            total_non_empty_lines += 1;

            // Check if line is a JSON object with "kind" field (JSONL marker)
            if line.starts_with('{') && line.contains("\"kind\"") {
                json_object_lines += 1;
            }
        }

        // If multiple lines look like JSONL entries, it's JSONL
        if json_object_lines >= 2
            || (json_object_lines == 1 && total_non_empty_lines == 1 && trimmed.contains("\n{"))
        {
            return Self::JsonLines;
        }

        // Check if it's a single JSON object (legacy format)
        if trimmed.starts_with('{') && trimmed.ends_with('}') {
            // Look for ChatSession structure markers
            if trimmed.contains("\"sessionId\"")
                || trimmed.contains("\"creationDate\"")
                || trimmed.contains("\"requests\"")
            {
                return Self::LegacyJson;
            }
        }

        // Default to legacy JSON if unclear
        Self::LegacyJson
    }

    /// Get minimum VS Code version that uses this format
    pub fn min_vscode_version(&self) -> &'static str {
        match self {
            Self::LegacyJson => "1.0.0",
            Self::JsonLines => "1.109.0",
        }
    }

    /// Get human-readable format description
    pub fn description(&self) -> &'static str {
        match self {
            Self::LegacyJson => "Legacy JSON (single object)",
            Self::JsonLines => "JSON Lines (event-sourced, VS Code 1.109.0+)",
        }
    }

    /// Get short format name
    pub fn short_name(&self) -> &'static str {
        match self {
            Self::LegacyJson => "json",
            Self::JsonLines => "jsonl",
        }
    }
}

impl std::fmt::Display for VsCodeSessionFormat {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.description())
    }
}

/// Sanitize JSON content by replacing lone surrogates with replacement character.
/// VS Code sometimes writes invalid JSON with lone Unicode surrogates (e.g., \udde0).
fn sanitize_json_unicode(content: &str) -> String {
    // Process all \uXXXX sequences and fix lone surrogates
    let mut result = String::with_capacity(content.len());
    let mut last_end = 0;

    // Collect all matches first to avoid borrowing issues
    let matches: Vec<_> = UNICODE_ESCAPE_RE.find_iter(content).collect();

    for (i, mat) in matches.iter().enumerate() {
        let start = mat.start();
        let end = mat.end();

        // Add content before this match
        result.push_str(&content[last_end..start]);

        // Parse the hex value from the match itself (always ASCII \uXXXX)
        let hex_str = &mat.as_str()[2..]; // Skip the \u prefix
        if let Ok(code_point) = u16::from_str_radix(hex_str, 16) {
            // Check if it's a high surrogate (D800-DBFF)
            if (0xD800..=0xDBFF).contains(&code_point) {
                // Check if next match is immediately following and is a low surrogate
                let is_valid_pair = if let Some(next_mat) = matches.get(i + 1) {
                    // Must be immediately adjacent (no gap)
                    if next_mat.start() == end {
                        let next_hex = &next_mat.as_str()[2..];
                        if let Ok(next_cp) = u16::from_str_radix(next_hex, 16) {
                            (0xDC00..=0xDFFF).contains(&next_cp)
                        } else {
                            false
                        }
                    } else {
                        false
                    }
                } else {
                    false
                };

                if is_valid_pair {
                    // Valid surrogate pair, keep the high surrogate
                    result.push_str(mat.as_str());
                } else {
                    // Lone high surrogate - replace with replacement char
                    result.push_str("\\uFFFD");
                }
            }
            // Check if it's a low surrogate (DC00-DFFF)
            else if (0xDC00..=0xDFFF).contains(&code_point) {
                // Check if previous match was immediately before and was a high surrogate
                let is_valid_pair = if i > 0 {
                    if let Some(prev_mat) = matches.get(i - 1) {
                        // Must be immediately adjacent (no gap)
                        if prev_mat.end() == start {
                            let prev_hex = &prev_mat.as_str()[2..];
                            if let Ok(prev_cp) = u16::from_str_radix(prev_hex, 16) {
                                (0xD800..=0xDBFF).contains(&prev_cp)
                            } else {
                                false
                            }
                        } else {
                            false
                        }
                    } else {
                        false
                    }
                } else {
                    false
                };

                if is_valid_pair {
                    // Part of valid surrogate pair, keep it
                    result.push_str(mat.as_str());
                } else {
                    // Lone low surrogate - replace with replacement char
                    result.push_str("\\uFFFD");
                }
            }
            // Normal code point
            else {
                result.push_str(mat.as_str());
            }
        } else {
            // Invalid hex - keep as is
            result.push_str(mat.as_str());
        }
        last_end = end;
    }

    // Add remaining content
    result.push_str(&content[last_end..]);
    result
}

/// Try to parse JSON, sanitizing invalid Unicode if needed
pub fn parse_session_json(content: &str) -> std::result::Result<ChatSession, serde_json::Error> {
    match serde_json::from_str::<ChatSession>(content) {
        Ok(session) => Ok(session),
        Err(e) => {
            // If parsing fails due to Unicode issue, try sanitizing
            if e.to_string().contains("surrogate") || e.to_string().contains("escape") {
                let sanitized = sanitize_json_unicode(content);
                serde_json::from_str::<ChatSession>(&sanitized)
            } else {
                Err(e)
            }
        }
    }
}

/// JSONL entry kinds for VS Code 1.109.0+ session format
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum JsonlKind {
    /// Initial session state (kind: 0)
    Initial = 0,
    /// Delta update to specific keys (kind: 1)  
    Delta = 1,
    /// Array replace/splice operation (kind: 2)
    /// Optional 'i' field specifies splice index (truncate at i, then extend)
    ArraySplice = 2,
}

/// Parse a JSONL (JSON Lines) session file (VS Code 1.109.0+ format)
/// Each line is a JSON object with 'kind' field indicating the type:
/// - kind 0: Initial session metadata with 'v' containing ChatSession-like structure
/// - kind 1: Delta update with 'k' (keys path) and 'v' (value)
/// - kind 2: Array replace/splice with 'k' (path), 'v' (items), optional 'i' (splice index)
pub fn parse_session_jsonl(content: &str) -> std::result::Result<ChatSession, serde_json::Error> {
    // Pre-process: split concatenated JSON objects that lack newline separators
    let content = split_concatenated_jsonl(content);

    let mut session = ChatSession {
        version: 3,
        session_id: None,
        creation_date: 0,
        last_message_date: 0,
        is_imported: false,
        initial_location: "panel".to_string(),
        custom_title: None,
        requester_username: None,
        requester_avatar_icon_uri: None,
        responder_username: None,
        responder_avatar_icon_uri: None,
        requests: Vec::new(),
    };

    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }

        // Parse each line as a JSON object
        let entry: serde_json::Value = match serde_json::from_str(line) {
            Ok(v) => v,
            Err(_) => {
                // Try sanitizing Unicode
                let sanitized = sanitize_json_unicode(line);
                serde_json::from_str(&sanitized)?
            }
        };

        let kind = entry.get("kind").and_then(|k| k.as_u64()).unwrap_or(0);

        match kind {
            0 => {
                // Initial state - 'v' contains the session metadata
                if let Some(v) = entry.get("v") {
                    // Parse version
                    if let Some(version) = v.get("version").and_then(|x| x.as_u64()) {
                        session.version = version as u32;
                    }
                    // Parse session ID
                    if let Some(sid) = v.get("sessionId").and_then(|x| x.as_str()) {
                        session.session_id = Some(sid.to_string());
                    }
                    // Parse creation date
                    if let Some(cd) = v.get("creationDate").and_then(|x| x.as_i64()) {
                        session.creation_date = cd;
                    }
                    // Parse initial location
                    if let Some(loc) = v.get("initialLocation").and_then(|x| x.as_str()) {
                        session.initial_location = loc.to_string();
                    }
                    // Parse responder username
                    if let Some(ru) = v.get("responderUsername").and_then(|x| x.as_str()) {
                        session.responder_username = Some(ru.to_string());
                    }
                    // Parse custom title
                    if let Some(title) = v.get("customTitle").and_then(|x| x.as_str()) {
                        session.custom_title = Some(title.to_string());
                    }
                    // Parse hasPendingEdits as imported marker
                    if let Some(imported) = v.get("isImported").and_then(|x| x.as_bool()) {
                        session.is_imported = imported;
                    }
                    // Parse requests array if present
                    if let Some(requests) = v.get("requests") {
                        if let Ok(reqs) =
                            serde_json::from_value::<Vec<ChatRequest>>(requests.clone())
                        {
                            session.requests = reqs;
                            // Compute last_message_date from the latest request timestamp
                            if let Some(latest_ts) =
                                session.requests.iter().filter_map(|r| r.timestamp).max()
                            {
                                session.last_message_date = latest_ts;
                            }
                        }
                    }
                    // Fall back to creationDate if no request timestamps found
                    if session.last_message_date == 0 {
                        session.last_message_date = session.creation_date;
                    }
                }
            }
            1 => {
                // Delta update - 'k' is array of key path, 'v' is the value
                if let (Some(keys), Some(value)) = (entry.get("k"), entry.get("v")) {
                    if let Some(keys_arr) = keys.as_array() {
                        // Handle top-level session keys
                        if keys_arr.len() == 1 {
                            if let Some(key) = keys_arr[0].as_str() {
                                match key {
                                    "customTitle" => {
                                        if let Some(title) = value.as_str() {
                                            session.custom_title = Some(title.to_string());
                                        }
                                    }
                                    "lastMessageDate" => {
                                        if let Some(date) = value.as_i64() {
                                            session.last_message_date = date;
                                        }
                                    }
                                    "hasPendingEdits" | "isImported" => {
                                        // Session-level boolean updates, safe to ignore for now
                                    }
                                    _ => {} // Ignore unknown keys
                                }
                            }
                        }
                        // Handle nested request field updates: ["requests", idx, field]
                        else if keys_arr.len() == 3 {
                            if let (Some("requests"), Some(idx), Some(field)) = (
                                keys_arr[0].as_str(),
                                keys_arr[1].as_u64().map(|i| i as usize),
                                keys_arr[2].as_str(),
                            ) {
                                // Auto-grow requests array to accommodate the referenced index.
                                // VS Code emits events for request indices before a formal
                                // kind:2 k=["requests"] append, so we must create placeholder
                                // requests as needed.
                                while idx >= session.requests.len() {
                                    session.requests.push(ChatRequest::default());
                                }
                                match field {
                                    "response" => {
                                        session.requests[idx].response = Some(value.clone());
                                    }
                                    "result" => {
                                        session.requests[idx].result = Some(value.clone());
                                    }
                                    "followups" => {
                                        session.requests[idx].followups =
                                            serde_json::from_value(value.clone()).ok();
                                    }
                                    "isCanceled" => {
                                        session.requests[idx].is_canceled = value.as_bool();
                                    }
                                    "contentReferences" => {
                                        session.requests[idx].content_references =
                                            serde_json::from_value(value.clone()).ok();
                                    }
                                    "codeCitations" => {
                                        session.requests[idx].code_citations =
                                            serde_json::from_value(value.clone()).ok();
                                    }
                                    "modelState" => {
                                        session.requests[idx].model_state = Some(value.clone());
                                    }
                                    "modelId" => {
                                        session.requests[idx].model_id =
                                            value.as_str().map(|s| s.to_string());
                                    }
                                    "agent" => {
                                        session.requests[idx].agent = Some(value.clone());
                                    }
                                    "variableData" => {
                                        session.requests[idx].variable_data = Some(value.clone());
                                    }
                                    _ => {} // Ignore unknown request fields
                                }
                            }
                        }
                    }
                }
            }
            2 => {
                // Array splice operation - 'k' is the key path, 'v' is the new array items
                // Optional 'i' field is the splice start index (truncate at i, then extend)
                // Without 'i', items are appended to the end of the array
                if let (Some(keys), Some(value)) = (entry.get("k"), entry.get("v")) {
                    let splice_index = entry.get("i").and_then(|i| i.as_u64()).map(|i| i as usize);
                    if let Some(keys_arr) = keys.as_array() {
                        // Top-level requests: k=["requests"], v=[requests_array]
                        if keys_arr.len() == 1 {
                            if let Some("requests") = keys_arr[0].as_str() {
                                if let Some(items) = value.as_array() {
                                    if let Some(idx) = splice_index {
                                        // Splice: truncate at index i, then extend with new items
                                        session.requests.truncate(idx);
                                    }
                                    // Without 'i': append to end (no truncation)
                                    for item in items {
                                        if let Ok(req) =
                                            serde_json::from_value::<ChatRequest>(item.clone())
                                        {
                                            session.requests.push(req);
                                        }
                                    }
                                    // Update last message date from latest request
                                    if let Some(last_req) = session.requests.last() {
                                        if let Some(ts) = last_req.timestamp {
                                            session.last_message_date = ts;
                                        }
                                    }
                                }
                            }
                        }
                        // Nested array replace/splice: k=["requests", idx, "response"], v=[parts]
                        else if keys_arr.len() == 3 {
                            if let (Some("requests"), Some(req_idx), Some(field)) = (
                                keys_arr[0].as_str(),
                                keys_arr[1].as_u64().map(|i| i as usize),
                                keys_arr[2].as_str(),
                            ) {
                                // Auto-grow requests array for the referenced index
                                while req_idx >= session.requests.len() {
                                    session.requests.push(ChatRequest::default());
                                }
                                match field {
                                    "response" => {
                                        // Response is stored as a JSON Value (array)
                                        if let Some(idx) = splice_index {
                                            // Splice: keep items before index i, replace rest
                                            if let Some(existing) =
                                                session.requests[req_idx].response.as_ref()
                                            {
                                                if let Some(existing_arr) = existing.as_array() {
                                                    let mut new_arr: Vec<serde_json::Value> =
                                                        existing_arr[..idx.min(existing_arr.len())]
                                                            .to_vec();
                                                    if let Some(new_items) = value.as_array() {
                                                        new_arr.extend(new_items.iter().cloned());
                                                    }
                                                    session.requests[req_idx].response =
                                                        Some(serde_json::Value::Array(new_arr));
                                                } else {
                                                    session.requests[req_idx].response =
                                                        Some(value.clone());
                                                }
                                            } else {
                                                session.requests[req_idx].response =
                                                    Some(value.clone());
                                            }
                                        } else {
                                            // No splice index: append to existing response array
                                            if let Some(existing) =
                                                session.requests[req_idx].response.as_ref()
                                            {
                                                if let Some(existing_arr) = existing.as_array() {
                                                    let mut new_arr = existing_arr.clone();
                                                    if let Some(new_items) = value.as_array() {
                                                        new_arr.extend(new_items.iter().cloned());
                                                    }
                                                    session.requests[req_idx].response =
                                                        Some(serde_json::Value::Array(new_arr));
                                                } else {
                                                    session.requests[req_idx].response =
                                                        Some(value.clone());
                                                }
                                            } else {
                                                session.requests[req_idx].response =
                                                    Some(value.clone());
                                            }
                                        }
                                    }
                                    "contentReferences" => {
                                        session.requests[req_idx].content_references =
                                            serde_json::from_value(value.clone()).ok();
                                    }
                                    _ => {} // Ignore unknown fields
                                }
                            }
                        }
                    }
                }
            }
            _ => {} // Unknown kind, skip
        }
    }

    Ok(session)
}

/// Check if a file extension indicates a session file (.json, .jsonl, or .backup)
pub fn is_session_file_extension(ext: &std::ffi::OsStr) -> bool {
    ext == "json" || ext == "jsonl" || ext == "backup"
}

/// Detect session format and version from content
pub fn detect_session_format(content: &str) -> SessionFormatInfo {
    let format = VsCodeSessionFormat::from_content(content);
    let trimmed = content.trim();

    // Detect schema version based on format
    let (schema_version, confidence, method) = match format {
        VsCodeSessionFormat::JsonLines => {
            // For JSONL, check the first line's "v" object for version
            if let Some(first_line) = trimmed.lines().next() {
                if let Ok(entry) = serde_json::from_str::<serde_json::Value>(first_line) {
                    if let Some(v) = entry.get("v") {
                        if let Some(ver) = v.get("version").and_then(|x| x.as_u64()) {
                            (
                                SessionSchemaVersion::from_version(ver as u32),
                                0.95,
                                "jsonl-version-field",
                            )
                        } else {
                            // No version field, likely v3 (current default)
                            (SessionSchemaVersion::V3, 0.7, "jsonl-default")
                        }
                    } else {
                        (SessionSchemaVersion::V3, 0.6, "jsonl-no-v-field")
                    }
                } else {
                    (SessionSchemaVersion::Unknown, 0.3, "jsonl-parse-error")
                }
            } else {
                (SessionSchemaVersion::Unknown, 0.2, "jsonl-empty")
            }
        }
        VsCodeSessionFormat::LegacyJson => {
            // For JSON, directly check the version field
            if let Ok(json) = serde_json::from_str::<serde_json::Value>(trimmed) {
                if let Some(ver) = json.get("version").and_then(|x| x.as_u64()) {
                    (
                        SessionSchemaVersion::from_version(ver as u32),
                        0.95,
                        "json-version-field",
                    )
                } else {
                    // Infer from structure
                    if json.get("requests").is_some() && json.get("sessionId").is_some() {
                        (SessionSchemaVersion::V3, 0.8, "json-structure-inference")
                    } else if json.get("messages").is_some() {
                        (SessionSchemaVersion::V1, 0.7, "json-legacy-structure")
                    } else {
                        (SessionSchemaVersion::Unknown, 0.4, "json-unknown-structure")
                    }
                }
            } else {
                // Try sanitizing and parsing again
                let sanitized = sanitize_json_unicode(trimmed);
                if let Ok(json) = serde_json::from_str::<serde_json::Value>(&sanitized) {
                    if let Some(ver) = json.get("version").and_then(|x| x.as_u64()) {
                        (
                            SessionSchemaVersion::from_version(ver as u32),
                            0.9,
                            "json-version-after-sanitize",
                        )
                    } else {
                        (SessionSchemaVersion::V3, 0.6, "json-default-after-sanitize")
                    }
                } else {
                    (SessionSchemaVersion::Unknown, 0.2, "json-parse-error")
                }
            }
        }
    };

    SessionFormatInfo {
        format,
        schema_version,
        confidence,
        detection_method: method,
    }
}

/// Parse session content with automatic format detection
pub fn parse_session_auto(
    content: &str,
) -> std::result::Result<(ChatSession, SessionFormatInfo), serde_json::Error> {
    let format_info = detect_session_format(content);

    let session = match format_info.format {
        VsCodeSessionFormat::JsonLines => parse_session_jsonl(content)?,
        VsCodeSessionFormat::LegacyJson => parse_session_json(content)?,
    };

    Ok((session, format_info))
}

/// Parse a session file, automatically detecting format from content (not just extension)
pub fn parse_session_file(path: &Path) -> std::result::Result<ChatSession, serde_json::Error> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| serde_json::Error::io(std::io::Error::other(e.to_string())))?;

    // Use content-based auto-detection
    let (session, _format_info) = parse_session_auto(&content)?;
    Ok(session)
}

/// Get the path to the workspace storage database
pub fn get_workspace_storage_db(workspace_id: &str) -> Result<PathBuf> {
    let storage_path = get_workspace_storage_path()?;
    Ok(storage_path.join(workspace_id).join("state.vscdb"))
}

/// Read the chat session index from VS Code storage
pub fn read_chat_session_index(db_path: &Path) -> Result<ChatSessionIndex> {
    let conn = Connection::open(db_path)?;

    let result: std::result::Result<String, rusqlite::Error> = conn.query_row(
        "SELECT value FROM ItemTable WHERE key = ?",
        ["chat.ChatSessionStore.index"],
        |row| row.get(0),
    );

    match result {
        Ok(json_str) => serde_json::from_str(&json_str)
            .map_err(|e| CsmError::InvalidSessionFormat(e.to_string())),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(ChatSessionIndex::default()),
        Err(e) => Err(CsmError::SqliteError(e)),
    }
}

/// Write the chat session index to VS Code storage
pub fn write_chat_session_index(db_path: &Path, index: &ChatSessionIndex) -> Result<()> {
    let conn = Connection::open(db_path)?;
    let json_str = serde_json::to_string(index)?;

    // Check if the key exists
    let exists: bool = conn.query_row(
        "SELECT COUNT(*) > 0 FROM ItemTable WHERE key = ?",
        ["chat.ChatSessionStore.index"],
        |row| row.get(0),
    )?;

    if exists {
        conn.execute(
            "UPDATE ItemTable SET value = ? WHERE key = ?",
            [&json_str, "chat.ChatSessionStore.index"],
        )?;
    } else {
        conn.execute(
            "INSERT INTO ItemTable (key, value) VALUES (?, ?)",
            ["chat.ChatSessionStore.index", &json_str],
        )?;
    }

    Ok(())
}

// ── Generic DB key read/write ──────────────────────────────────────────────

/// Read a JSON value from the VS Code state DB by key
pub fn read_db_json(db_path: &Path, key: &str) -> Result<Option<serde_json::Value>> {
    let conn = Connection::open(db_path)?;
    let result: std::result::Result<String, rusqlite::Error> =
        conn.query_row("SELECT value FROM ItemTable WHERE key = ?", [key], |row| {
            row.get(0)
        });
    match result {
        Ok(json_str) => {
            let v = serde_json::from_str(&json_str)
                .map_err(|e| CsmError::InvalidSessionFormat(e.to_string()))?;
            Ok(Some(v))
        }
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
        Err(e) => Err(CsmError::SqliteError(e)),
    }
}

/// Write a JSON value to the VS Code state DB (upsert)
fn write_db_json(db_path: &Path, key: &str, value: &serde_json::Value) -> Result<()> {
    let conn = Connection::open(db_path)?;
    let json_str = serde_json::to_string(value)?;
    conn.execute(
        "INSERT OR REPLACE INTO ItemTable (key, value) VALUES (?, ?)",
        rusqlite::params![key, json_str],
    )?;
    Ok(())
}

// ── Session resource URI helpers ───────────────────────────────────────────

/// Build the `vscode-chat-session://local/{base64(sessionId)}` resource URI
/// that VS Code uses to identify sessions in model cache and state cache.
pub fn session_resource_uri(session_id: &str) -> String {
    let b64 = BASE64.encode(session_id.as_bytes());
    format!("vscode-chat-session://local/{}", b64)
}

/// Extract a session ID from a `vscode-chat-session://` resource URI.
/// Returns `None` if the URI doesn't match the expected format.
pub fn session_id_from_resource_uri(uri: &str) -> Option<String> {
    let prefix = "vscode-chat-session://local/";
    if let Some(b64) = uri.strip_prefix(prefix) {
        BASE64
            .decode(b64)
            .ok()
            .and_then(|bytes| String::from_utf8(bytes).ok())
    } else {
        None
    }
}

// ── Model cache (agentSessions.model.cache) ────────────────────────────────

const MODEL_CACHE_KEY: &str = "agentSessions.model.cache";

/// Read the `agentSessions.model.cache` from VS Code storage.
/// Returns an empty Vec if the key doesn't exist.
pub fn read_model_cache(db_path: &Path) -> Result<Vec<ModelCacheEntry>> {
    match read_db_json(db_path, MODEL_CACHE_KEY)? {
        Some(v) => serde_json::from_value(v)
            .map_err(|e| CsmError::InvalidSessionFormat(format!("model cache: {}", e))),
        None => Ok(Vec::new()),
    }
}

/// Write the `agentSessions.model.cache` to VS Code storage.
pub fn write_model_cache(db_path: &Path, cache: &[ModelCacheEntry]) -> Result<()> {
    let v = serde_json::to_value(cache)?;
    write_db_json(db_path, MODEL_CACHE_KEY, &v)
}

/// Rebuild the model cache from the session index. This makes sessions visible
/// in the Chat panel sidebar. Only non-empty sessions get entries (VS Code
/// hides empty ones).
pub fn rebuild_model_cache(db_path: &Path, index: &ChatSessionIndex) -> Result<usize> {
    let mut cache: Vec<ModelCacheEntry> = Vec::new();

    for (session_id, entry) in &index.entries {
        // Only include non-empty sessions — empty ones are hidden in the sidebar
        if entry.is_empty {
            continue;
        }

        let timing = entry.timing.clone().unwrap_or(ChatSessionTiming {
            created: entry.last_message_date,
            last_request_started: Some(entry.last_message_date),
            last_request_ended: Some(entry.last_message_date),
        });

        cache.push(ModelCacheEntry {
            provider_type: "local".to_string(),
            provider_label: "Local".to_string(),
            resource: session_resource_uri(session_id),
            icon: "vm".to_string(),
            label: entry.title.clone(),
            status: 1,
            timing,
            initial_location: entry.initial_location.clone(),
            has_pending_edits: false,
            is_empty: false,
            is_external: entry.is_external.unwrap_or(false),
            last_response_state: 1, // Complete
        });
    }

    let count = cache.len();
    write_model_cache(db_path, &cache)?;
    Ok(count)
}

// ── State cache (agentSessions.state.cache) ────────────────────────────────

const STATE_CACHE_KEY: &str = "agentSessions.state.cache";

/// Read the `agentSessions.state.cache` from VS Code storage.
pub fn read_state_cache(db_path: &Path) -> Result<Vec<StateCacheEntry>> {
    match read_db_json(db_path, STATE_CACHE_KEY)? {
        Some(v) => serde_json::from_value(v)
            .map_err(|e| CsmError::InvalidSessionFormat(format!("state cache: {}", e))),
        None => Ok(Vec::new()),
    }
}

/// Write the `agentSessions.state.cache` to VS Code storage.
pub fn write_state_cache(db_path: &Path, cache: &[StateCacheEntry]) -> Result<()> {
    let v = serde_json::to_value(cache)?;
    write_db_json(db_path, STATE_CACHE_KEY, &v)
}

/// Remove state cache entries whose resource URIs reference sessions that no
/// longer exist on disk. Returns the number of stale entries removed.
pub fn cleanup_state_cache(db_path: &Path, valid_session_ids: &HashSet<String>) -> Result<usize> {
    let entries = read_state_cache(db_path)?;
    let valid_resources: HashSet<String> = valid_session_ids
        .iter()
        .map(|id| session_resource_uri(id))
        .collect();

    let before = entries.len();
    let cleaned: Vec<StateCacheEntry> = entries
        .into_iter()
        .filter(|e| valid_resources.contains(&e.resource))
        .collect();
    let removed = before - cleaned.len();

    if removed > 0 {
        write_state_cache(db_path, &cleaned)?;
    }

    Ok(removed)
}

// ── Memento (memento/interactive-session-view-copilot) ──────────────────────

const MEMENTO_KEY: &str = "memento/interactive-session-view-copilot";

/// Read the Copilot Chat memento (tracks the last-active session).
pub fn read_session_memento(db_path: &Path) -> Result<Option<serde_json::Value>> {
    read_db_json(db_path, MEMENTO_KEY)
}

/// Write the Copilot Chat memento.
pub fn write_session_memento(db_path: &Path, value: &serde_json::Value) -> Result<()> {
    write_db_json(db_path, MEMENTO_KEY, value)
}

/// Fix the memento so it points to a session that actually exists.
/// If the current memento references a deleted/non-existent session, update it
/// to the most recently active valid session. Returns `true` if the memento was
/// changed.
pub fn fix_session_memento(
    db_path: &Path,
    valid_session_ids: &HashSet<String>,
    preferred_session_id: Option<&str>,
) -> Result<bool> {
    let memento = read_session_memento(db_path)?;

    let current_sid = memento
        .as_ref()
        .and_then(|v| v.get("sessionId"))
        .and_then(|v| v.as_str())
        .map(|s| s.to_string());

    // Check if current memento already points to a valid session
    if let Some(ref sid) = current_sid {
        if valid_session_ids.contains(sid) {
            return Ok(false); // Already valid
        }
    }

    // Pick a session to point to: prefer the explicit choice, otherwise pick any valid one
    let target = preferred_session_id
        .filter(|id| valid_session_ids.contains(*id))
        .or_else(|| valid_session_ids.iter().next().map(|s| s.as_str()));

    if let Some(target_id) = target {
        let mut new_memento = memento.unwrap_or(serde_json::json!({}));
        if let Some(obj) = new_memento.as_object_mut() {
            obj.insert(
                "sessionId".to_string(),
                serde_json::Value::String(target_id.to_string()),
            );
        }
        write_session_memento(db_path, &new_memento)?;
        Ok(true)
    } else {
        Ok(false) // No valid sessions to point to
    }
}

// ── .json.bak recovery ─────────────────────────────────────────────────────

/// Count the number of requests in a session's `v.requests` array from a JSONL
/// file (reads only the first kind:0 line).
fn count_jsonl_requests(path: &Path) -> Result<usize> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| CsmError::InvalidSessionFormat(format!("Read error: {}", e)))?;
    let first_line = content.lines().next().unwrap_or("");
    let parsed: serde_json::Value = serde_json::from_str(first_line)
        .map_err(|e| CsmError::InvalidSessionFormat(format!("Parse error: {}", e)))?;

    let count = parsed
        .get("v")
        .or_else(|| Some(&parsed)) // bare JSON (non-JSONL) may not have "v" wrapper
        .and_then(|v| v.get("requests"))
        .and_then(|r| r.as_array())
        .map(|a| a.len())
        .unwrap_or(0);

    Ok(count)
}

/// Count the number of requests in a `.json.bak` (or `.json`) file.
fn count_json_bak_requests(path: &Path) -> Result<usize> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| CsmError::InvalidSessionFormat(format!("Read error: {}", e)))?;
    let parsed: serde_json::Value = serde_json::from_str(&content)
        .map_err(|e| CsmError::InvalidSessionFormat(format!("Parse error: {}", e)))?;

    let count = parsed
        .get("requests")
        .and_then(|r| r.as_array())
        .map(|a| a.len())
        .unwrap_or(0);

    Ok(count)
}

/// Migrate old-format inputState fields from top-level to a nested `inputState`
/// object. VS Code version 3 expects `inputState` as a sub-object with keys
/// `attachments`, `mode`, `inputText`, `selections`, `contrib`.
///
/// Old format (pre-v3): `{ "attachments": [...], "mode": {...}, "inputText": "...", ... }`
/// New format (v3):     `{ "inputState": { "attachments": [...], "mode": {...}, ... } }`
pub fn migrate_old_input_state(state: &mut serde_json::Value) {
    if let Some(obj) = state.as_object_mut() {
        // Only migrate if inputState doesn't already exist AND old top-level fields do
        if obj.contains_key("inputState") {
            return;
        }

        let old_keys = [
            "attachments",
            "mode",
            "inputText",
            "selections",
            "contrib",
            "selectedModel",
        ];
        let has_old = old_keys.iter().any(|k| obj.contains_key(*k));

        if has_old {
            let mut input_state = serde_json::Map::new();

            // Move each old key into the nested object (with defaults)
            input_state.insert(
                "attachments".to_string(),
                obj.remove("attachments").unwrap_or(serde_json::json!([])),
            );
            input_state.insert(
                "mode".to_string(),
                obj.remove("mode")
                    .unwrap_or(serde_json::json!({"id": "agent", "kind": "agent"})),
            );
            input_state.insert(
                "inputText".to_string(),
                obj.remove("inputText").unwrap_or(serde_json::json!("")),
            );
            input_state.insert(
                "selections".to_string(),
                obj.remove("selections").unwrap_or(serde_json::json!([])),
            );
            input_state.insert(
                "contrib".to_string(),
                obj.remove("contrib").unwrap_or(serde_json::json!({})),
            );

            // selectedModel is optional, only include if present
            if let Some(model) = obj.remove("selectedModel") {
                input_state.insert("selectedModel".to_string(), model);
            }

            obj.insert(
                "inputState".to_string(),
                serde_json::Value::Object(input_state),
            );
        }
    }
}

/// Recover sessions from `.json.bak` files when the corresponding `.jsonl` has
/// fewer requests (indicating a truncated migration/compaction). For each .jsonl
/// that has a co-located .json.bak with more requests, rebuilds the .jsonl from
/// the backup data.
///
/// Returns the number of sessions recovered from backups.
pub fn recover_from_json_bak(chat_sessions_dir: &Path) -> Result<usize> {
    if !chat_sessions_dir.exists() {
        return Ok(0);
    }

    let mut recovered = 0;

    // Collect all .json.bak files
    let mut bak_files: Vec<PathBuf> = Vec::new();
    for entry in std::fs::read_dir(chat_sessions_dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.to_string_lossy().ends_with(".json.bak") {
            bak_files.push(path);
        }
    }

    for bak_path in &bak_files {
        // Derive session ID and .jsonl path
        let bak_name = bak_path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string();
        let session_id = bak_name.trim_end_matches(".json.bak");
        let jsonl_path = chat_sessions_dir.join(format!("{}.jsonl", session_id));

        // Get request counts
        let bak_count = match count_json_bak_requests(bak_path) {
            Ok(c) => c,
            Err(_) => continue, // Skip unparseable backups
        };

        if bak_count == 0 {
            continue; // Backup has no data, skip
        }

        let jsonl_count = if jsonl_path.exists() {
            count_jsonl_requests(&jsonl_path).unwrap_or(0)
        } else {
            0 // No .jsonl at all — definitely recover from backup
        };

        if bak_count <= jsonl_count {
            continue; // .jsonl already has equal or more data
        }

        // .json.bak has more requests — recover from it
        println!(
            "   [*] .json.bak has {} requests vs .jsonl has {} for {}",
            bak_count, jsonl_count, session_id
        );

        // Read the full backup
        let bak_content = match std::fs::read_to_string(bak_path) {
            Ok(c) => c,
            Err(e) => {
                println!("   [WARN] Failed to read .json.bak {}: {}", session_id, e);
                continue;
            }
        };
        let mut full_data: serde_json::Value = match serde_json::from_str(&bak_content) {
            Ok(v) => v,
            Err(e) => {
                println!("   [WARN] Failed to parse .json.bak {}: {}", session_id, e);
                continue;
            }
        };

        // Clean up: build ISerializableChatData3 format
        if let Some(obj) = full_data.as_object_mut() {
            // Ensure version 3
            obj.insert("version".to_string(), serde_json::json!(3));

            // Ensure sessionId
            if !obj.contains_key("sessionId") {
                obj.insert("sessionId".to_string(), serde_json::json!(session_id));
            }

            // Force safe values
            obj.insert("hasPendingEdits".to_string(), serde_json::json!(false));
            obj.insert("pendingRequests".to_string(), serde_json::json!([]));

            // Ensure responderUsername
            if !obj.contains_key("responderUsername") {
                obj.insert(
                    "responderUsername".to_string(),
                    serde_json::json!("GitHub Copilot"),
                );
            }

            // Migrate old inputState format
            migrate_old_input_state(&mut full_data);

            // Fix modelState values in requests
            fix_request_model_states(&mut full_data);
        }

        // Backup existing .jsonl if present
        if jsonl_path.exists() {
            let pre_fix_bak = jsonl_path.with_extension("jsonl.pre_bak_recovery");
            if let Err(e) = std::fs::copy(&jsonl_path, &pre_fix_bak) {
                println!(
                    "   [WARN] Failed to backup .jsonl before recovery {}: {}",
                    session_id, e
                );
                continue;
            }
        }

        // Write new JSONL kind:0
        let jsonl_obj = serde_json::json!({"kind": 0, "v": full_data});
        let jsonl_str = serde_json::to_string(&jsonl_obj).map_err(|e| {
            CsmError::InvalidSessionFormat(format!("Failed to serialize recovered session: {}", e))
        })?;
        std::fs::write(&jsonl_path, format!("{}\n", jsonl_str))?;

        println!(
            "   [OK] Recovered {} from .json.bak ({}{} requests)",
            session_id, jsonl_count, bak_count
        );
        recovered += 1;
    }

    Ok(recovered)
}

/// Recover sessions from `.jsonl.bak` files when the backup is larger than the
/// corresponding `.jsonl` file — indicating the live session was truncated.
///
/// For each `.jsonl.bak` whose byte size exceeds the active `.jsonl`:
/// 1. Backs up the active file to `.jsonl.pre-restore`
/// 2. Copies the `.jsonl.bak` over the active `.jsonl`
///
/// This handles the common scenario where VS Code or compaction overwrites a
/// session with a truncated version while the backup retains the full data.
///
/// Returns `(restored_count, total_bytes_recovered)`.
pub fn recover_from_jsonl_bak(chat_sessions_dir: &Path, dry_run: bool) -> Result<(usize, u64)> {
    if !chat_sessions_dir.exists() {
        return Ok((0, 0));
    }

    let mut restored = 0usize;
    let mut bytes_recovered = 0u64;

    // Collect all .jsonl.bak files
    let mut bak_files: Vec<PathBuf> = Vec::new();
    for entry in std::fs::read_dir(chat_sessions_dir)? {
        let entry = entry?;
        let path = entry.path();
        if path.to_string_lossy().ends_with(".jsonl.bak") {
            bak_files.push(path);
        }
    }

    for bak_path in &bak_files {
        let bak_name = bak_path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string();
        let session_id = bak_name.trim_end_matches(".jsonl.bak");
        let jsonl_path = chat_sessions_dir.join(format!("{}.jsonl", session_id));

        // Only act when the .jsonl exists AND the backup is strictly larger
        if !jsonl_path.exists() {
            continue;
        }

        let orig_size = match std::fs::metadata(&jsonl_path) {
            Ok(m) => m.len(),
            Err(_) => continue,
        };
        let bak_size = match std::fs::metadata(bak_path) {
            Ok(m) => m.len(),
            Err(_) => continue,
        };

        if bak_size <= orig_size {
            continue; // Backup is not larger, nothing to recover
        }

        let delta = bak_size - orig_size;
        let orig_kb = orig_size as f64 / 1024.0;
        let bak_kb = bak_size as f64 / 1024.0;

        if dry_run {
            println!(
                "   [*] Would restore {} ({:.1}KB → {:.1}KB, +{:.1}KB)",
                session_id,
                orig_kb,
                bak_kb,
                delta as f64 / 1024.0
            );
        } else {
            // Safety backup of current file
            let pre_restore = jsonl_path.with_extension("jsonl.pre-restore");
            if let Err(e) = std::fs::copy(&jsonl_path, &pre_restore) {
                println!(
                    "   [WARN] Failed to create safety backup for {}: {}",
                    session_id, e
                );
                continue;
            }

            // Restore from backup
            if let Err(e) = std::fs::copy(bak_path, &jsonl_path) {
                println!(
                    "   [WARN] Failed to restore {} from .jsonl.bak: {}",
                    session_id, e
                );
                // Try to roll back
                let _ = std::fs::copy(&pre_restore, &jsonl_path);
                continue;
            }

            println!(
                "   [OK] Restored {} from .jsonl.bak ({:.1}KB → {:.1}KB, +{:.1}KB recovered)",
                session_id,
                orig_kb,
                bak_kb,
                delta as f64 / 1024.0
            );
        }

        restored += 1;
        bytes_recovered += delta;
    }

    Ok((restored, bytes_recovered))
}

/// Detail about a single session backup recovery action.
#[derive(Debug, Clone)]
pub struct BackupRecoveryAction {
    /// Session ID (UUID portion of filename)
    pub session_id: String,
    /// Source file used for recovery (the backup with more requests)
    pub source_file: String,
    /// Number of requests in the current .jsonl
    pub current_requests: usize,
    /// Number of requests in the best backup
    pub recovered_requests: usize,
    /// Size of the current .jsonl in bytes
    pub current_size: u64,
    /// Size of the best backup in bytes
    pub recovered_size: u64,
    /// Whether the source was a different format (e.g. .json → .jsonl conversion)
    pub converted: bool,
}

/// Comprehensive session recovery from ALL backup file variants.
///
/// For each session ID found in `chat_sessions_dir`, examines:
/// - `.jsonl.bak` (VS Code JSONL backup)
/// - `.jsonl.pre-restore` (chasm safety backup)
/// - `.jsonl.pre_bak_recovery` (earlier chasm recovery backup)
/// - `.json` (old JSON format — may contain more requests than current JSONL)
/// - `.json.bak` (old JSON format backup)
///
/// Selects the file with the **most requests** (not just largest size) and
/// restores it as the active `.jsonl`, converting from JSON format if needed.
///
/// Returns a list of recovery actions taken (or that would be taken in dry-run).
pub fn recover_from_all_backups(
    chat_sessions_dir: &Path,
    dry_run: bool,
) -> Result<Vec<BackupRecoveryAction>> {
    use std::collections::HashMap;

    if !chat_sessions_dir.exists() {
        return Ok(Vec::new());
    }

    // Collect all files grouped by session ID (first 36 chars = UUID)
    let mut session_files: HashMap<String, Vec<(String, PathBuf)>> = HashMap::new();
    for entry in std::fs::read_dir(chat_sessions_dir)? {
        let entry = entry?;
        let path = entry.path();
        if !path.is_file() {
            continue;
        }
        let fname = path
            .file_name()
            .unwrap_or_default()
            .to_string_lossy()
            .to_string();
        // Skip markdown, non-session files
        if fname.ends_with(".md") || fname.len() < 36 {
            continue;
        }
        // Session ID is the first 36 characters (UUID format)
        let sid = fname[..36].to_string();
        // Only include JSON/JSONL files (including .bak, .pre-restore variants)
        if fname.contains(".json") {
            session_files.entry(sid).or_default().push((fname, path));
        }
    }

    let mut actions = Vec::new();

    for (sid, files) in &session_files {
        // Find the current active .jsonl file
        let current_jsonl_name = format!("{}.jsonl", sid);
        let current_jsonl_path = chat_sessions_dir.join(&current_jsonl_name);

        // Parse the current .jsonl to get its request count
        let current_requests = if current_jsonl_path.exists() {
            match parse_session_file(&current_jsonl_path) {
                Ok(session) => session.requests.len(),
                Err(_) => 0,
            }
        } else {
            0
        };
        let current_size = if current_jsonl_path.exists() {
            std::fs::metadata(&current_jsonl_path)
                .map(|m| m.len())
                .unwrap_or(0)
        } else {
            0
        };

        // Find the backup with the most requests
        let mut best_requests = current_requests;
        let mut best_file: Option<(&str, &Path)> = None;

        for (fname, fpath) in files {
            // Skip the current active file
            if fname == &current_jsonl_name {
                continue;
            }
            // Skip files that are clearly recovery markers (tiny files)
            let size = std::fs::metadata(fpath).map(|m| m.len()).unwrap_or(0);
            if size < 100 {
                continue;
            }
            // Parse to count requests
            match parse_session_file(fpath) {
                Ok(session) => {
                    let req_count = session.requests.len();
                    if req_count > best_requests {
                        best_requests = req_count;
                        best_file = Some((fname.as_str(), fpath.as_path()));
                    }
                }
                Err(_) => {
                    // Can't parse — skip
                }
            }
        }

        if let Some((best_name, best_path)) = best_file {
            let best_size = std::fs::metadata(best_path).map(|m| m.len()).unwrap_or(0);
            let is_json_source = !best_name.contains(".jsonl");

            if !dry_run {
                // Safety backup of current file
                if current_jsonl_path.exists() {
                    let pre_restore = current_jsonl_path.with_extension("jsonl.pre-restore");
                    // Don't overwrite existing pre-restore (keep earliest backup)
                    if !pre_restore.exists() {
                        if let Err(e) = std::fs::copy(&current_jsonl_path, &pre_restore) {
                            eprintln!(
                                "   [WARN] Failed to create safety backup for {}: {}",
                                sid, e
                            );
                            continue;
                        }
                    }
                }

                if is_json_source {
                    // Convert JSON → JSONL: parse and re-serialize as kind:0 JSONL entry
                    match parse_session_file(best_path) {
                        Ok(session) => {
                            // Read the raw JSON to preserve all fields
                            let raw_content =
                                std::fs::read_to_string(best_path).unwrap_or_default();
                            let raw_value: serde_json::Value =
                                serde_json::from_str(&raw_content).unwrap_or_default();
                            let jsonl_entry = serde_json::json!({"kind": 0, "v": raw_value});
                            if let Err(e) = std::fs::write(
                                &current_jsonl_path,
                                serde_json::to_string(&jsonl_entry).unwrap_or_default() + "\n",
                            ) {
                                eprintln!(
                                    "   [WARN] Failed to write converted JSONL for {}: {}",
                                    sid, e
                                );
                                continue;
                            }
                            // Update the session_id if missing in the converted file
                            let _ = session;
                        }
                        Err(e) => {
                            eprintln!("   [WARN] Failed to parse JSON backup for {}: {}", sid, e);
                            continue;
                        }
                    }
                } else {
                    // JSONL → JSONL: direct copy
                    if let Err(e) = std::fs::copy(best_path, &current_jsonl_path) {
                        eprintln!(
                            "   [WARN] Failed to restore {} from {}: {}",
                            sid, best_name, e
                        );
                        continue;
                    }
                }
            }

            actions.push(BackupRecoveryAction {
                session_id: sid.clone(),
                source_file: best_name.to_string(),
                current_requests,
                recovered_requests: best_requests,
                current_size,
                recovered_size: best_size,
                converted: is_json_source,
            });
        }
    }

    // Sort by session ID for deterministic output
    actions.sort_by(|a, b| a.session_id.cmp(&b.session_id));

    Ok(actions)
}

/// Fix modelState values in a session's requests array.
/// - Pending (value=0) or Cancelled (value=2) → set to Cancelled (3) with completedAt
/// - Terminal states (1, 3, 4) without completedAt → add completedAt from request timestamp
fn fix_request_model_states(session_data: &mut serde_json::Value) {
    let requests = match session_data
        .get_mut("requests")
        .and_then(|r| r.as_array_mut())
    {
        Some(r) => r,
        None => return,
    };

    for req in requests.iter_mut() {
        let timestamp = req
            .get("timestamp")
            .and_then(|t| t.as_i64())
            .unwrap_or_else(|| {
                std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_millis() as i64
            });

        if let Some(ms) = req.get_mut("modelState") {
            if let Some(val) = ms.get("value").and_then(|v| v.as_u64()) {
                match val {
                    0 | 2 => {
                        // Pending or Cancelled → force to Cancelled with completedAt
                        *ms = serde_json::json!({
                            "value": 3,
                            "completedAt": timestamp
                        });
                    }
                    1 | 3 | 4 => {
                        // Terminal states — ensure completedAt exists
                        if ms.get("completedAt").is_none() {
                            if let Some(ms_obj) = ms.as_object_mut() {
                                ms_obj.insert(
                                    "completedAt".to_string(),
                                    serde_json::json!(timestamp),
                                );
                            }
                        }
                    }
                    _ => {}
                }
            }
        }
    }
}

/// Add a session to the VS Code index
pub fn add_session_to_index(
    db_path: &Path,
    session_id: &str,
    title: &str,
    last_message_date_ms: i64,
    _is_imported: bool,
    initial_location: &str,
    is_empty: bool,
) -> Result<()> {
    let mut index = read_chat_session_index(db_path)?;

    index.entries.insert(
        session_id.to_string(),
        ChatSessionIndexEntry {
            session_id: session_id.to_string(),
            title: title.to_string(),
            last_message_date: last_message_date_ms,
            timing: Some(ChatSessionTiming {
                created: last_message_date_ms,
                last_request_started: Some(last_message_date_ms),
                last_request_ended: Some(last_message_date_ms),
            }),
            last_response_state: 1, // ResponseModelState.Complete
            initial_location: initial_location.to_string(),
            is_empty,
            is_imported: Some(_is_imported),
            has_pending_edits: Some(false),
            is_external: Some(false),
        },
    );

    write_chat_session_index(db_path, &index)
}

/// Remove a session from the VS Code index
#[allow(dead_code)]
pub fn remove_session_from_index(db_path: &Path, session_id: &str) -> Result<bool> {
    let mut index = read_chat_session_index(db_path)?;
    let removed = index.entries.remove(session_id).is_some();
    if removed {
        write_chat_session_index(db_path, &index)?;
    }
    Ok(removed)
}

/// Sync the VS Code index with sessions on disk (remove stale entries, add missing ones)
/// When both .json and .jsonl exist for the same session ID, prefers .jsonl.
pub fn sync_session_index(
    workspace_id: &str,
    chat_sessions_dir: &Path,
    force: bool,
) -> Result<(usize, usize)> {
    let db_path = get_workspace_storage_db(workspace_id)?;

    if !db_path.exists() {
        return Err(CsmError::WorkspaceNotFound(format!(
            "Database not found: {}",
            db_path.display()
        )));
    }

    // Check if VS Code is running
    if !force && is_vscode_running() {
        return Err(CsmError::VSCodeRunning);
    }

    // Get current index
    let mut index = read_chat_session_index(&db_path)?;

    // Get session files on disk
    let mut files_on_disk: std::collections::HashSet<String> = std::collections::HashSet::new();
    if chat_sessions_dir.exists() {
        for entry in std::fs::read_dir(chat_sessions_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path
                .extension()
                .map(is_session_file_extension)
                .unwrap_or(false)
            {
                if let Some(stem) = path.file_stem() {
                    files_on_disk.insert(stem.to_string_lossy().to_string());
                }
            }
        }
    }

    // Remove stale entries (in index but not on disk)
    let stale_ids: Vec<String> = index
        .entries
        .keys()
        .filter(|id| !files_on_disk.contains(*id))
        .cloned()
        .collect();

    let removed = stale_ids.len();
    for id in &stale_ids {
        index.entries.remove(id);
    }

    // Add/update sessions from disk
    // Collect files, preferring .jsonl over .json for the same session ID
    let mut session_files: std::collections::HashMap<String, PathBuf> =
        std::collections::HashMap::new();
    for entry in std::fs::read_dir(chat_sessions_dir)? {
        let entry = entry?;
        let path = entry.path();
        if path
            .extension()
            .map(is_session_file_extension)
            .unwrap_or(false)
        {
            if let Some(stem) = path.file_stem() {
                let stem_str = stem.to_string_lossy().to_string();
                let is_jsonl = path.extension().is_some_and(|e| e == "jsonl");
                // Insert if no entry yet, or if this is .jsonl (preferred over .json)
                if !session_files.contains_key(&stem_str) || is_jsonl {
                    session_files.insert(stem_str, path);
                }
            }
        }
    }

    let mut added = 0;
    for (_, path) in &session_files {
        if let Ok(session) = parse_session_file(path) {
            let session_id = session.session_id.clone().unwrap_or_else(|| {
                path.file_stem()
                    .map(|s| s.to_string_lossy().to_string())
                    .unwrap_or_else(|| uuid::Uuid::new_v4().to_string())
            });

            let title = session.title();
            let is_empty = session.is_empty();
            let last_message_date = session.last_message_date;
            let initial_location = session.initial_location.clone();

            index.entries.insert(
                session_id.clone(),
                ChatSessionIndexEntry {
                    session_id,
                    title,
                    last_message_date,
                    timing: Some(ChatSessionTiming {
                        created: session.creation_date,
                        last_request_started: Some(last_message_date),
                        last_request_ended: Some(last_message_date),
                    }),
                    last_response_state: 1, // ResponseModelState.Complete
                    initial_location,
                    is_empty,
                    is_imported: Some(false),
                    has_pending_edits: Some(false),
                    is_external: Some(false),
                },
            );
            added += 1;
        }
    }

    // Write the synced index
    write_chat_session_index(&db_path, &index)?;

    Ok((added, removed))
}

/// Register all sessions from a directory into the VS Code index
pub fn register_all_sessions_from_directory(
    workspace_id: &str,
    chat_sessions_dir: &Path,
    force: bool,
) -> Result<usize> {
    let db_path = get_workspace_storage_db(workspace_id)?;

    if !db_path.exists() {
        return Err(CsmError::WorkspaceNotFound(format!(
            "Database not found: {}",
            db_path.display()
        )));
    }

    // Check if VS Code is running
    if !force && is_vscode_running() {
        return Err(CsmError::VSCodeRunning);
    }

    // Use sync to ensure index matches disk
    let (added, removed) = sync_session_index(workspace_id, chat_sessions_dir, force)?;

    // Print individual session info
    for entry in std::fs::read_dir(chat_sessions_dir)? {
        let entry = entry?;
        let path = entry.path();

        if path
            .extension()
            .map(is_session_file_extension)
            .unwrap_or(false)
        {
            if let Ok(session) = parse_session_file(&path) {
                let session_id = session.session_id.clone().unwrap_or_else(|| {
                    path.file_stem()
                        .map(|s| s.to_string_lossy().to_string())
                        .unwrap_or_else(|| uuid::Uuid::new_v4().to_string())
                });

                let title = session.title();

                println!(
                    "[OK] Registered: {} ({}...)",
                    title,
                    &session_id[..12.min(session_id.len())]
                );
            }
        }
    }

    if removed > 0 {
        println!("[OK] Removed {} stale index entries", removed);
    }

    Ok(added)
}

/// Check if VS Code is currently running
pub fn is_vscode_running() -> bool {
    let mut sys = System::new();
    sys.refresh_processes();

    for process in sys.processes().values() {
        let name = process.name().to_lowercase();
        if name.contains("code") && !name.contains("codec") {
            return true;
        }
    }

    false
}

/// Close VS Code gracefully and wait for it to exit.
/// Returns the list of workspace folders that were open (for reopening).
pub fn close_vscode_and_wait(timeout_secs: u64) -> Result<()> {
    use sysinfo::{ProcessRefreshKind, RefreshKind, Signal};

    if !is_vscode_running() {
        return Ok(());
    }

    // Send SIGTERM (graceful close) to all Code processes
    let mut sys = System::new_with_specifics(
        RefreshKind::new().with_processes(ProcessRefreshKind::everything()),
    );
    sys.refresh_processes();

    let mut signaled = 0u32;
    for (pid, process) in sys.processes() {
        let name = process.name().to_lowercase();
        if name.contains("code") && !name.contains("codec") {
            // On Windows, kill() sends TerminateProcess; there's no graceful
            // SIGTERM equivalent via sysinfo. But the main electron process
            // handles WM_CLOSE. We use the `taskkill` approach on Windows for
            // a graceful close.
            #[cfg(windows)]
            {
                let _ = std::process::Command::new("taskkill")
                    .args(["/PID", &pid.as_u32().to_string()])
                    .stdout(std::process::Stdio::null())
                    .stderr(std::process::Stdio::null())
                    .status();
                signaled += 1;
            }
            #[cfg(not(windows))]
            {
                if process.kill_with(Signal::Term).unwrap_or(false) {
                    signaled += 1;
                }
            }
        }
    }

    if signaled == 0 {
        return Ok(());
    }

    // Wait for all Code processes to exit
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(timeout_secs);
    loop {
        std::thread::sleep(std::time::Duration::from_millis(500));
        if !is_vscode_running() {
            // Extra wait for file locks to release
            std::thread::sleep(std::time::Duration::from_secs(1));
            return Ok(());
        }
        if std::time::Instant::now() >= deadline {
            // Force kill remaining processes
            let mut sys2 = System::new_with_specifics(
                RefreshKind::new().with_processes(ProcessRefreshKind::everything()),
            );
            sys2.refresh_processes();
            for (_pid, process) in sys2.processes() {
                let name = process.name().to_lowercase();
                if name.contains("code") && !name.contains("codec") {
                    process.kill();
                }
            }
            std::thread::sleep(std::time::Duration::from_secs(1));
            return Ok(());
        }
    }
}

/// Reopen VS Code, optionally at a specific path.
pub fn reopen_vscode(project_path: Option<&str>) -> Result<()> {
    let mut cmd = std::process::Command::new("code");
    if let Some(path) = project_path {
        cmd.arg(path);
    }
    cmd.stdout(std::process::Stdio::null())
        .stderr(std::process::Stdio::null())
        .spawn()?;
    Ok(())
}

/// Backup workspace sessions to a timestamped directory
pub fn backup_workspace_sessions(workspace_dir: &Path) -> Result<Option<PathBuf>> {
    let chat_sessions_dir = workspace_dir.join("chatSessions");

    if !chat_sessions_dir.exists() {
        return Ok(None);
    }

    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs();

    let backup_dir = workspace_dir.join(format!("chatSessions-backup-{}", timestamp));

    // Copy directory recursively
    copy_dir_all(&chat_sessions_dir, &backup_dir)?;

    Ok(Some(backup_dir))
}

/// Recursively copy a directory
fn copy_dir_all(src: &Path, dst: &Path) -> Result<()> {
    std::fs::create_dir_all(dst)?;

    for entry in std::fs::read_dir(src)? {
        let entry = entry?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());

        if src_path.is_dir() {
            copy_dir_all(&src_path, &dst_path)?;
        } else {
            std::fs::copy(&src_path, &dst_path)?;
        }
    }

    Ok(())
}

// =============================================================================
// Empty Window Sessions (ALL SESSIONS)
// =============================================================================

/// Read all empty window chat sessions (not tied to any workspace)
/// These appear in VS Code's "ALL SESSIONS" panel
pub fn read_empty_window_sessions() -> Result<Vec<ChatSession>> {
    let sessions_path = get_empty_window_sessions_path()?;

    if !sessions_path.exists() {
        return Ok(Vec::new());
    }

    let mut sessions = Vec::new();

    for entry in std::fs::read_dir(&sessions_path)? {
        let entry = entry?;
        let path = entry.path();

        if path.extension().is_some_and(is_session_file_extension) {
            if let Ok(session) = parse_session_file(&path) {
                sessions.push(session);
            }
        }
    }

    // Sort by last message date (most recent first)
    sessions.sort_by(|a, b| b.last_message_date.cmp(&a.last_message_date));

    Ok(sessions)
}

/// Get a specific empty window session by ID
#[allow(dead_code)]
pub fn get_empty_window_session(session_id: &str) -> Result<Option<ChatSession>> {
    let sessions_path = get_empty_window_sessions_path()?;
    let session_path = sessions_path.join(format!("{}.json", session_id));

    if !session_path.exists() {
        return Ok(None);
    }

    let content = std::fs::read_to_string(&session_path)?;
    let session: ChatSession = serde_json::from_str(&content)
        .map_err(|e| CsmError::InvalidSessionFormat(e.to_string()))?;

    Ok(Some(session))
}

/// Write an empty window session
#[allow(dead_code)]
pub fn write_empty_window_session(session: &ChatSession) -> Result<PathBuf> {
    let sessions_path = get_empty_window_sessions_path()?;

    // Create directory if it doesn't exist
    std::fs::create_dir_all(&sessions_path)?;

    let session_id = session.session_id.as_deref().unwrap_or("unknown");
    let session_path = sessions_path.join(format!("{}.json", session_id));
    let content = serde_json::to_string_pretty(session)?;
    std::fs::write(&session_path, content)?;

    Ok(session_path)
}

/// Delete an empty window session
#[allow(dead_code)]
pub fn delete_empty_window_session(session_id: &str) -> Result<bool> {
    let sessions_path = get_empty_window_sessions_path()?;
    let session_path = sessions_path.join(format!("{}.json", session_id));

    if session_path.exists() {
        std::fs::remove_file(&session_path)?;
        Ok(true)
    } else {
        Ok(false)
    }
}

/// Count empty window sessions
pub fn count_empty_window_sessions() -> Result<usize> {
    let sessions_path = get_empty_window_sessions_path()?;

    if !sessions_path.exists() {
        return Ok(0);
    }

    let count = std::fs::read_dir(&sessions_path)?
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().is_some_and(is_session_file_extension))
        .count();

    Ok(count)
}

/// Compact a JSONL session file by replaying all operations into a single kind:0 snapshot.
/// This works at the raw JSON level, preserving all fields VS Code expects.
/// Returns the path to the compacted file.
///
/// Handles a common corruption pattern where VS Code appends delta operations
/// to line 0 without newline separators (e.g., `}{"kind":1,...}{"kind":2,...}`).
pub fn compact_session_jsonl(path: &Path) -> Result<PathBuf> {
    let content = std::fs::read_to_string(path).map_err(|e| {
        CsmError::InvalidSessionFormat(format!("Failed to read {}: {}", path.display(), e))
    })?;

    // Pre-process: split concatenated JSON objects that lack newline separators.
    // VS Code sometimes appends delta ops to line 0 without a \n, producing:
    //   {"kind":0,"v":{...}}{"kind":1,...}{"kind":2,...}\n{"kind":1,...}\n...
    // We fix this by inserting newlines at every `}{"kind":` boundary.
    let content = split_concatenated_jsonl(&content);

    let mut lines = content.lines();

    // First line must be kind:0 (initial snapshot)
    let first_line = lines
        .next()
        .ok_or_else(|| CsmError::InvalidSessionFormat("Empty JSONL file".to_string()))?;

    let first_entry: serde_json::Value = match serde_json::from_str(first_line.trim()) {
        Ok(v) => v,
        Err(_) => {
            // Try sanitizing Unicode (lone surrogates, etc.)
            let sanitized = sanitize_json_unicode(first_line.trim());
            serde_json::from_str(&sanitized).map_err(|e| {
                CsmError::InvalidSessionFormat(format!("Invalid JSON on line 1: {}", e))
            })?
        }
    };

    let kind = first_entry
        .get("kind")
        .and_then(|k| k.as_u64())
        .unwrap_or(99);
    if kind != 0 {
        return Err(CsmError::InvalidSessionFormat(
            "First JSONL line must be kind:0".to_string(),
        ));
    }

    // Extract the session state from the "v" field
    let mut state = first_entry
        .get("v")
        .cloned()
        .ok_or_else(|| CsmError::InvalidSessionFormat("kind:0 missing 'v' field".to_string()))?;

    // Replay all subsequent operations
    for line in lines {
        let line = line.trim();
        if line.is_empty() {
            continue;
        }

        let entry: serde_json::Value = match serde_json::from_str(line) {
            Ok(v) => v,
            Err(_) => continue, // Skip malformed lines
        };

        let op_kind = entry.get("kind").and_then(|k| k.as_u64()).unwrap_or(99);

        match op_kind {
            1 => {
                // Delta update: k=["path","to","field"], v=value
                if let (Some(keys), Some(value)) = (entry.get("k"), entry.get("v")) {
                    if let Some(keys_arr) = keys.as_array() {
                        apply_delta(&mut state, keys_arr, value.clone());
                    }
                }
            }
            2 => {
                // Array replace/splice: k=["path","to","array"], v=[items], i=splice_index
                if let (Some(keys), Some(value)) = (entry.get("k"), entry.get("v")) {
                    let splice_index = entry.get("i").and_then(|i| i.as_u64()).map(|i| i as usize);
                    if let Some(keys_arr) = keys.as_array() {
                        apply_splice(&mut state, keys_arr, value.clone(), splice_index);
                    }
                }
            }
            _ => {} // Skip unknown kinds
        }
    }

    // Inject any missing fields that VS Code's latest format requires
    let session_id = path
        .file_stem()
        .and_then(|s| s.to_str())
        .map(|s| s.to_string());
    ensure_vscode_compat_fields(&mut state, session_id.as_deref());

    // Write the compacted file: single kind:0 line with the final state
    let compact_entry = serde_json::json!({"kind": 0, "v": state});
    let compact_content = serde_json::to_string(&compact_entry)
        .map_err(|e| CsmError::InvalidSessionFormat(format!("Failed to serialize: {}", e)))?;

    // Backup the original file
    let backup_path = path.with_extension("jsonl.bak");
    std::fs::rename(path, &backup_path)?;

    // Write the compacted file (trailing newline prevents concatenation
    // if VS Code later appends delta operations)
    std::fs::write(path, format!("{}\n", compact_content))?;

    Ok(backup_path)
}

/// Trim a session JSONL file by keeping only the last `keep` requests.
///
/// Very long chat sessions (100+ requests) can grow to 50-100+ MB, causing VS Code
/// to fail loading them. This function compacts the session first (if needed), then
/// removes old requests from the `requests` array, keeping only the most recent ones.
///
/// The full session is preserved as a `.jsonl.bak` backup. A trimmed summary is
/// injected as the first request message so the user knows context was archived.
///
/// Returns `(original_count, kept_count, original_mb, new_mb)`.
pub fn trim_session_jsonl(path: &Path, keep: usize) -> Result<(usize, usize, f64, f64)> {
    let content = std::fs::read_to_string(path).map_err(|e| {
        CsmError::InvalidSessionFormat(format!("Failed to read {}: {}", path.display(), e))
    })?;

    let original_size = content.len() as f64 / (1024.0 * 1024.0);

    // Always handle concatenated JSON objects first, then check line count
    let content = split_concatenated_jsonl(&content);
    let line_count = content.lines().filter(|l| !l.trim().is_empty()).count();

    // If multi-line (concatenated objects or delta ops), compact first
    let content = if line_count > 1 {
        // Write the split content so compact can process it
        std::fs::write(path, &content)?;
        compact_session_jsonl(path)?;
        std::fs::read_to_string(path).map_err(|e| {
            CsmError::InvalidSessionFormat(format!("Failed to read compacted file: {}", e))
        })?
    } else {
        content
    };

    let first_line = content
        .lines()
        .next()
        .ok_or_else(|| CsmError::InvalidSessionFormat("Empty JSONL file".to_string()))?;

    let mut entry: serde_json::Value = serde_json::from_str(first_line.trim())
        .map_err(|_| {
            let sanitized = sanitize_json_unicode(first_line.trim());
            serde_json::from_str::<serde_json::Value>(&sanitized)
                .map_err(|e| CsmError::InvalidSessionFormat(format!("Invalid JSON: {}", e)))
        })
        .unwrap_or_else(|e| e.unwrap());

    let kind = entry.get("kind").and_then(|k| k.as_u64()).unwrap_or(99);
    if kind != 0 {
        return Err(
            CsmError::InvalidSessionFormat("First JSONL line must be kind:0".to_string()).into(),
        );
    }

    // Get the requests array
    let requests = match entry
        .get("v")
        .and_then(|v| v.get("requests"))
        .and_then(|r| r.as_array())
    {
        Some(r) => r.clone(),
        None => {
            return Err(CsmError::InvalidSessionFormat(
                "Session has no requests array".to_string(),
            )
            .into());
        }
    };

    let original_count = requests.len();

    if original_count <= keep {
        // Still strip bloated content even if not reducing request count
        strip_bloated_content(&mut entry);

        let trimmed_content = serde_json::to_string(&entry)
            .map_err(|e| CsmError::InvalidSessionFormat(format!("Failed to serialize: {}", e)))?;
        let new_size = trimmed_content.len() as f64 / (1024.0 * 1024.0);

        // Only rewrite if we actually reduced size
        if new_size < original_size * 0.9 {
            let backup_path = path.with_extension("jsonl.bak");
            if !backup_path.exists() {
                std::fs::copy(path, &backup_path)?;
            }
            std::fs::write(path, format!("{}\n", trimmed_content))?;
        }

        return Ok((original_count, original_count, original_size, new_size));
    }

    // Keep only the last `keep` requests
    let kept_requests: Vec<serde_json::Value> = requests[original_count - keep..].to_vec();

    // Use only the kept requests — no injected trim notice.
    // Injecting synthetic requests with non-standard agent/structure fields
    // can cause VS Code's session deserializer to reject the entire session.
    let final_requests = kept_requests;

    // Replace the requests array in the entry
    if let Some(v) = entry.get_mut("v") {
        if let Some(obj) = v.as_object_mut() {
            obj.insert("requests".to_string(), serde_json::json!(final_requests));
        }
    }

    // Strip bloated metadata, tool invocations, textEditGroups, thinking tokens
    strip_bloated_content(&mut entry);

    // Ensure compat fields
    let session_id = path
        .file_stem()
        .and_then(|s| s.to_str())
        .map(|s| s.to_string());
    if let Some(v) = entry.get_mut("v") {
        ensure_vscode_compat_fields(v, session_id.as_deref());
    }

    let trimmed_content = serde_json::to_string(&entry)
        .map_err(|e| CsmError::InvalidSessionFormat(format!("Failed to serialize: {}", e)))?;

    let new_size = trimmed_content.len() as f64 / (1024.0 * 1024.0);

    // Backup original (if not already backed up by compact)
    let backup_path = path.with_extension("jsonl.bak");
    if !backup_path.exists() {
        std::fs::copy(path, &backup_path)?;
    }

    // Write the trimmed file (trailing newline prevents concatenation)
    std::fs::write(path, format!("{}\n", trimmed_content))?;

    Ok((original_count, keep, original_size, new_size))
}

/// Strip bloated content from a session entry to reduce file size.
///
/// VS Code sessions accumulate large metadata that isn't needed for session display:
/// - `result.metadata`: Can be 100KB-1.5MB per request (Copilot internal state)
/// - `editedFileEvents`: Redundant file edit tracking
/// - `chatEdits`: File edit diffs
/// - `textEditGroup` response items: 80-120KB each with full file diffs
/// - `thinking` response items: Model thinking tokens (can be 400+ per request)
/// - `toolInvocationSerialized`: Tool call metadata (usually already stripped by compact)
/// - `toolSpecificData`: Duplicate data in tool invocations
///
/// This function strips or truncates all of these while preserving the conversation
/// content (markdownContent responses and user messages).
fn strip_bloated_content(entry: &mut serde_json::Value) {
    let requests = match entry
        .get_mut("v")
        .and_then(|v| v.get_mut("requests"))
        .and_then(|r| r.as_array_mut())
    {
        Some(r) => r,
        None => return,
    };

    for req in requests.iter_mut() {
        let obj = match req.as_object_mut() {
            Some(o) => o,
            None => continue,
        };

        // Strip result.metadata (100KB-1.5MB per request)
        if let Some(result) = obj.get_mut("result") {
            if let Some(result_obj) = result.as_object_mut() {
                if let Some(meta) = result_obj.get("metadata") {
                    let meta_str = serde_json::to_string(meta).unwrap_or_default();
                    if meta_str.len() > 1000 {
                        result_obj.insert(
                            "metadata".to_string(),
                            serde_json::Value::Object(serde_json::Map::new()),
                        );
                    }
                }
            }
        }

        // Strip editedFileEvents
        obj.remove("editedFileEvents");

        // Strip chatEdits
        obj.remove("chatEdits");

        // Truncate contentReferences to max 3
        if let Some(refs) = obj.get_mut("contentReferences") {
            if let Some(arr) = refs.as_array_mut() {
                if arr.len() > 3 {
                    arr.truncate(3);
                }
            }
        }

        // Process response items
        if let Some(response) = obj.get_mut("response") {
            if let Some(resp_arr) = response.as_array_mut() {
                // Remove non-essential response kinds
                resp_arr.retain(|r| {
                    let kind = r.get("kind").and_then(|k| k.as_str()).unwrap_or("");
                    !matches!(
                        kind,
                        "toolInvocationSerialized"
                            | "progressMessage"
                            | "confirmationWidget"
                            | "codeblockUri"
                            | "progressTaskSerialized"
                            | "undoStop"
                            | "mcpServersStarting"
                            | "confirmation"
                    )
                });

                // Truncate textEditGroup items (strip edit diffs, keep URI ref)
                for r in resp_arr.iter_mut() {
                    let kind = r
                        .get("kind")
                        .and_then(|k| k.as_str())
                        .unwrap_or("")
                        .to_string();

                    if kind == "textEditGroup" {
                        if let Some(edits) = r.get_mut("edits") {
                            if let Some(arr) = edits.as_array_mut() {
                                if serde_json::to_string(arr).unwrap_or_default().len() > 2000 {
                                    arr.clear();
                                }
                            }
                        }
                    }

                    // Truncate thinking tokens
                    if kind == "thinking" {
                        if let Some(val) = r.get_mut("value") {
                            if let Some(s) = val.as_str() {
                                if s.len() > 500 {
                                    *val = serde_json::Value::String(format!(
                                        "{}... [truncated]",
                                        &s[..500]
                                    ));
                                }
                            }
                        }
                        if let Some(thought) = r.get_mut("thought") {
                            if let Some(thought_val) = thought.get_mut("value") {
                                if let Some(s) = thought_val.as_str() {
                                    if s.len() > 500 {
                                        *thought_val = serde_json::Value::String(format!(
                                            "{}... [truncated]",
                                            &s[..500]
                                        ));
                                    }
                                }
                            }
                        }
                    }

                    // Truncate large markdownContent
                    if kind == "markdownContent" {
                        if let Some(content) = r.get_mut("content") {
                            if let Some(val) = content.get_mut("value") {
                                if let Some(s) = val.as_str() {
                                    if s.len() > 20000 {
                                        *val = serde_json::Value::String(format!(
                                            "{}\n\n---\n*[Chasm: Content truncated for loading performance]*",
                                            &s[..20000]
                                        ));
                                    }
                                }
                            }
                        }
                    }
                }

                // Limit thinking items to last 5 per request
                let mut thinking_count = 0;
                let mut indices_to_remove = Vec::new();
                for (i, r) in resp_arr.iter().enumerate().rev() {
                    let kind = r.get("kind").and_then(|k| k.as_str()).unwrap_or("");
                    if kind == "thinking" {
                        thinking_count += 1;
                        if thinking_count > 5 {
                            indices_to_remove.push(i);
                        }
                    }
                }
                for idx in indices_to_remove {
                    resp_arr.remove(idx);
                }

                // Strip toolSpecificData from any remaining tool invocations
                for r in resp_arr.iter_mut() {
                    if let Some(obj) = r.as_object_mut() {
                        obj.remove("toolSpecificData");
                    }
                }

                // Fix response items missing `kind` field — wrap raw MarkdownString
                // objects as proper markdownContent response items.
                // VS Code sometimes serializes MarkdownString directly instead of
                // wrapping it in { kind: "markdownContent", content: MarkdownString }.
                // Without the `kind` discriminator, VS Code's deserializer fails.
                let fixed: Vec<serde_json::Value> = resp_arr
                    .drain(..)
                    .map(|item| {
                        if item.get("kind").is_none() {
                            // Check if it looks like a MarkdownString (has `value` or `supportHtml`)
                            if item.get("value").is_some() || item.get("supportHtml").is_some() {
                                serde_json::json!({
                                    "kind": "markdownContent",
                                    "content": item
                                })
                            } else {
                                item
                            }
                        } else {
                            item
                        }
                    })
                    .collect();
                *resp_arr = fixed;
            }
        }
    }
}

/// Split concatenated JSON objects in JSONL content that lack newline separators.
///
/// VS Code sometimes appends delta operations (kind:1, kind:2) onto the end of
/// a JSONL line without inserting a newline first. This produces invalid JSONL like:
///   `{"kind":0,"v":{...}}{"kind":1,...}{"kind":2,...}`
///
/// This function inserts newlines at every `}{"kind":` boundary to restore valid JSONL.
/// The pattern `}{"kind":` cannot appear inside JSON string values because `{"kind":`
/// would need to be escaped as `{\"kind\":` within a JSON string.
pub fn split_concatenated_jsonl(content: &str) -> String {
    // Fast path: if content has no concatenated objects, return as-is
    if !content.contains("}{\"kind\":") {
        return content.to_string();
    }

    content.replace("}{\"kind\":", "}\n{\"kind\":")
}

/// Apply a delta update (kind:1) to a JSON value at the given key path.
fn apply_delta(root: &mut serde_json::Value, keys: &[serde_json::Value], value: serde_json::Value) {
    if keys.is_empty() {
        return;
    }

    // Navigate to the parent
    let mut current = root;
    for key in &keys[..keys.len() - 1] {
        if let Some(k) = key.as_str() {
            if !current.get(k).is_some() {
                current[k] = serde_json::Value::Object(serde_json::Map::new());
            }
            current = &mut current[k];
        } else if let Some(idx) = key.as_u64() {
            if let Some(arr) = current.as_array_mut() {
                // Auto-grow array with null placeholders if index is beyond current length.
                // VS Code's event sourcing may reference indices before they are formally
                // added via a kind:2 splice.
                while (idx as usize) >= arr.len() {
                    arr.push(serde_json::Value::Object(serde_json::Map::new()));
                }
                current = &mut arr[idx as usize];
            } else {
                return;
            }
        }
    }

    // Set the final key
    if let Some(last_key) = keys.last() {
        if let Some(k) = last_key.as_str() {
            current[k] = value;
        } else if let Some(idx) = last_key.as_u64() {
            if let Some(arr) = current.as_array_mut() {
                while (idx as usize) >= arr.len() {
                    arr.push(serde_json::Value::Null);
                }
                arr[idx as usize] = value;
            }
        }
    }
}

/// Apply an array replace/splice operation (kind:2) to a JSON value at the given key path.
/// When `splice_index` is `Some(i)`, truncates the target array at index `i` before extending.
/// When `splice_index` is `None`, replaces the entire array with the new items.
fn apply_splice(
    root: &mut serde_json::Value,
    keys: &[serde_json::Value],
    items: serde_json::Value,
    splice_index: Option<usize>,
) {
    if keys.is_empty() {
        return;
    }

    // Navigate to the target array
    let mut current = root;
    for key in keys {
        if let Some(k) = key.as_str() {
            if !current.get(k).is_some() {
                current[k] = serde_json::json!([]);
            }
            current = &mut current[k];
        } else if let Some(idx) = key.as_u64() {
            if let Some(arr) = current.as_array_mut() {
                // Auto-grow array if index is beyond current length
                while (idx as usize) >= arr.len() {
                    arr.push(serde_json::Value::Object(serde_json::Map::new()));
                }
                current = &mut arr[idx as usize];
            } else {
                return;
            }
        }
    }

    // Splice or replace items in the target array
    if let Some(target_arr) = current.as_array_mut() {
        if let Some(idx) = splice_index {
            // Splice: truncate at index, then extend with new items
            target_arr.truncate(idx);
        } else {
            // Full replacement: clear the array
            target_arr.clear();
        }
        if let Some(new_items) = items.as_array() {
            target_arr.extend(new_items.iter().cloned());
        }
    }
}

/// Ensure a JSONL `kind:0` snapshot's `v` object has all fields required by
/// VS Code's latest session format (1.109.0+ / version 3). Missing fields are
/// injected with sensible defaults so sessions load reliably after recovery,
/// conversion, or compaction.
///
/// Required fields that VS Code now expects:
/// - `version` (u32, default 3)
/// - `sessionId` (string, extracted from filename or generated)
/// - `responderUsername` (string, default "GitHub Copilot")
/// - `hasPendingEdits` (bool, default false)
/// - `pendingRequests` (array, default [])
/// - `inputState` (object with mode, attachments, etc.)
pub fn ensure_vscode_compat_fields(state: &mut serde_json::Value, session_id: Option<&str>) {
    // Migrate old-format inputState (top-level attachments/mode/etc.) to nested object.
    // Must run BEFORE the inputState existence check below.
    migrate_old_input_state(state);

    if let Some(obj) = state.as_object_mut() {
        // version
        if !obj.contains_key("version") {
            obj.insert("version".to_string(), serde_json::json!(3));
        }

        // sessionId — use provided ID, or try to read from existing field
        if !obj.contains_key("sessionId") {
            if let Some(id) = session_id {
                obj.insert("sessionId".to_string(), serde_json::json!(id));
            }
        }

        // responderUsername
        if !obj.contains_key("responderUsername") {
            obj.insert(
                "responderUsername".to_string(),
                serde_json::json!("GitHub Copilot"),
            );
        }

        // hasPendingEdits — ALWAYS force to false for recovered/compacted sessions.
        // Sessions with hasPendingEdits:true cause VS Code to attempt restoring
        // stale file edits on load, which fails if files have changed since the
        // original session, preventing the session from loading entirely.
        obj.insert("hasPendingEdits".to_string(), serde_json::json!(false));

        // pendingRequests — ALWAYS force to empty for recovered/compacted sessions.
        // Stale pending requests can also block session loading.
        obj.insert("pendingRequests".to_string(), serde_json::json!([]));

        // inputState — VS Code expects this to exist with at least mode + attachments
        if !obj.contains_key("inputState") {
            obj.insert(
                "inputState".to_string(),
                serde_json::json!({
                    "attachments": [],
                    "mode": { "id": "agent", "kind": "agent" },
                    "inputText": "",
                    "selections": [],
                    "contrib": { "chatDynamicVariableModel": [] }
                }),
            );
        }
    }
}

/// Detect whether a legacy .json file is a "skeleton" — corrupted to contain only
/// structural characters ({}, [], commas, colons, whitespace) with all actual data stripped.
/// These files parse as valid JSON but contain no useful session content.
pub fn is_skeleton_json(content: &str) -> bool {
    // Must be non-trivial size to be a skeleton (tiny files might just be empty sessions)
    if content.len() < 100 {
        return false;
    }

    // Count structural vs data characters
    let structural_chars: usize = content
        .chars()
        .filter(|c| {
            matches!(
                c,
                '{' | '}' | '[' | ']' | ',' | ':' | ' ' | '\n' | '\r' | '\t' | '"'
            )
        })
        .count();

    let total_chars = content.len();
    let structural_ratio = structural_chars as f64 / total_chars as f64;

    // A skeleton file is >80% structural characters. Normal sessions have lots of
    // text content (messages, code, etc.) so the ratio is much lower.
    if structural_ratio < 0.80 {
        return false;
    }

    // Additionally verify: parse as JSON and check that requests array is empty or
    // contains only empty objects
    if let Ok(parsed) = serde_json::from_str::<serde_json::Value>(content) {
        // Check if requests exist and are all empty
        if let Some(requests) = parsed.get("requests").and_then(|r| r.as_array()) {
            let all_empty = requests.iter().all(|req| {
                // A skeleton request has no "message" text or empty message content
                let msg = req
                    .get("message")
                    .and_then(|m| m.get("text"))
                    .and_then(|t| t.as_str());
                msg.map_or(true, |s| s.is_empty())
            });
            return all_empty;
        }
        // No requests array at all — also skeleton-like
        return true;
    }

    // Couldn't parse but high structural ratio — still likely skeleton
    structural_ratio > 0.85
}

/// Convert a skeleton .json file to a valid minimal .jsonl file.
/// Preserves title and timestamp from the index entry if available.
/// The original .json file is renamed to `.json.corrupt` (non-destructive).
/// Returns the path to the new .jsonl file, or None if conversion was skipped.
pub fn convert_skeleton_json_to_jsonl(
    json_path: &Path,
    title: Option<&str>,
    last_message_date: Option<i64>,
) -> Result<Option<PathBuf>> {
    let content = std::fs::read_to_string(json_path)
        .map_err(|e| CsmError::InvalidSessionFormat(format!("Read error: {}", e)))?;

    if !is_skeleton_json(&content) {
        return Ok(None);
    }

    let session_id = json_path
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("unknown")
        .to_string();

    let title = title.unwrap_or("Recovered Session");
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as i64;
    let timestamp = last_message_date.unwrap_or(now);

    // Build a valid minimal kind:0 JSONL entry
    let jsonl_entry = serde_json::json!({
        "kind": 0,
        "v": {
            "sessionId": session_id,
            "title": title,
            "lastMessageDate": timestamp,
            "requests": [],
            "version": 4,
            "hasPendingEdits": false,
            "pendingRequests": [],
            "inputState": {
                "attachments": [],
                "mode": { "id": "agent", "kind": "agent" },
                "inputText": "",
                "selections": [],
                "contrib": { "chatDynamicVariableModel": [] }
            },
            "responderUsername": "GitHub Copilot",
            "isImported": false,
            "initialLocation": "panel"
        }
    });

    let jsonl_path = json_path.with_extension("jsonl");
    let corrupt_path = json_path.with_extension("json.corrupt");

    // Don't overwrite an existing .jsonl
    if jsonl_path.exists() {
        // Just rename the skeleton to .corrupt
        std::fs::rename(json_path, &corrupt_path)?;
        return Ok(None);
    }

    // Write the new .jsonl file
    std::fs::write(
        &jsonl_path,
        serde_json::to_string(&jsonl_entry)
            .map_err(|e| CsmError::InvalidSessionFormat(format!("Serialize error: {}", e)))?,
    )?;

    // Rename original to .json.corrupt (non-destructive)
    std::fs::rename(json_path, &corrupt_path)?;

    Ok(Some(jsonl_path))
}

/// Fix cancelled `modelState` values in a compacted (single-line) JSONL session file.
///
/// VS Code determines `lastResponseState` from the file content, not the index.
/// If the last request's `modelState.value` is `2` (Cancelled) or missing entirely,
/// VS Code refuses to load the session. This function:
/// 1. Finds the last request in the `requests` array
/// 2. If `modelState.value` is `2` (Cancelled), changes it to `1` (Complete)
/// 3. If `modelState` is missing entirely, adds `{"value":1,"completedAt":<now>}`
///
/// Returns `true` if the file was modified.
pub fn fix_cancelled_model_state(path: &Path) -> Result<bool> {
    let content = std::fs::read_to_string(path)
        .map_err(|e| CsmError::InvalidSessionFormat(format!("Read error: {}", e)))?;

    let lines: Vec<&str> = content.lines().collect();

    if lines.is_empty() {
        return Ok(false);
    }

    // For multi-line JSONL, we need to scan all lines to find the LAST modelState
    // delta for the highest request index. For single-line (compacted), we modify
    // the kind:0 snapshot directly.
    if lines.len() == 1 {
        // Compacted single-line JSONL: modify the kind:0 snapshot
        let mut entry: serde_json::Value = serde_json::from_str(lines[0].trim())
            .map_err(|e| CsmError::InvalidSessionFormat(format!("Invalid JSON: {}", e)))?;

        let is_kind_0 = entry
            .get("kind")
            .and_then(|k| k.as_u64())
            .map(|k| k == 0)
            .unwrap_or(false);

        if !is_kind_0 {
            return Ok(false);
        }

        let requests = match entry
            .get_mut("v")
            .and_then(|v| v.get_mut("requests"))
            .and_then(|r| r.as_array_mut())
        {
            Some(r) if !r.is_empty() => r,
            _ => return Ok(false),
        };

        let last_req = requests.last_mut().unwrap();
        let model_state = last_req.get("modelState");

        let needs_fix = match model_state {
            Some(ms) => {
                // Any value other than 1 (Complete) needs repair:
                // 0 = NotStarted/Unknown, 2 = Cancelled, 4 = InProgress
                ms.get("value").and_then(|v| v.as_u64()) != Some(1)
            }
            None => true, // Missing modelState = never completed
        };

        if !needs_fix {
            return Ok(false);
        }

        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;

        last_req.as_object_mut().unwrap().insert(
            "modelState".to_string(),
            serde_json::json!({"value": 1, "completedAt": now}),
        );

        let patched = serde_json::to_string(&entry)
            .map_err(|e| CsmError::InvalidSessionFormat(format!("Serialize error: {}", e)))?;
        // Trailing newline prevents concatenation if VS Code appends deltas
        std::fs::write(path, format!("{}\n", patched))?;
        return Ok(true);
    }

    // Multi-line JSONL: find the highest request index referenced across all lines,
    // then check if the last modelState delta for that index has value=2 or is missing.
    // If so, append a corrective delta.
    let mut highest_req_idx: Option<usize> = None;
    let mut last_model_state_value: Option<u64> = None;

    // Check kind:0 snapshot for request count
    if let Ok(first_entry) = serde_json::from_str::<serde_json::Value>(lines[0].trim()) {
        if let Some(requests) = first_entry
            .get("v")
            .and_then(|v| v.get("requests"))
            .and_then(|r| r.as_array())
        {
            if !requests.is_empty() {
                let last_idx = requests.len() - 1;
                highest_req_idx = Some(last_idx);
                // Check modelState in the snapshot's last request
                if let Some(ms) = requests[last_idx].get("modelState") {
                    last_model_state_value = ms.get("value").and_then(|v| v.as_u64());
                }
            }
        }
    }

    // Scan deltas for higher request indices and modelState updates
    static REQ_IDX_RE: Lazy<Regex> = Lazy::new(|| Regex::new(r#""k":\["requests",(\d+)"#).unwrap());

    for line in &lines[1..] {
        if let Some(caps) = REQ_IDX_RE.captures(line) {
            if let Ok(idx) = caps[1].parse::<usize>() {
                if highest_req_idx.is_none() || idx > highest_req_idx.unwrap() {
                    highest_req_idx = Some(idx);
                    last_model_state_value = None; // Reset for new highest
                }
                // Track modelState for the highest request index
                if Some(idx) == highest_req_idx && line.contains("\"modelState\"") {
                    if let Ok(entry) = serde_json::from_str::<serde_json::Value>(line.trim()) {
                        last_model_state_value = entry
                            .get("v")
                            .and_then(|v| v.get("value"))
                            .and_then(|v| v.as_u64());
                    }
                }
            }
        }
    }

    let req_idx = match highest_req_idx {
        Some(idx) => idx,
        None => return Ok(false),
    };

    let needs_fix = match last_model_state_value {
        Some(1) => false, // Already complete
        _ => true,        // 0=NotStarted, 2=Cancelled, 4=InProgress, None=missing
    };

    if !needs_fix {
        return Ok(false);
    }

    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_millis() as u64;

    let fix_delta = format!(
        "\n{{\"kind\":1,\"k\":[\"requests\",{},\"modelState\"],\"v\":{{\"value\":1,\"completedAt\":{}}}}}",
        req_idx, now
    );

    use std::io::Write;
    let mut file = std::fs::OpenOptions::new().append(true).open(path)?;
    file.write_all(fix_delta.as_bytes())?;

    Ok(true)
}

/// Repair workspace sessions: compact large JSONL files and fix the index.
/// Returns (compacted_count, index_fixed_count).
pub fn repair_workspace_sessions(
    workspace_id: &str,
    chat_sessions_dir: &Path,
    force: bool,
) -> Result<(usize, usize)> {
    let db_path = get_workspace_storage_db(workspace_id)?;

    if !db_path.exists() {
        return Err(CsmError::WorkspaceNotFound(format!(
            "Database not found: {}",
            db_path.display()
        )));
    }

    if !force && is_vscode_running() {
        return Err(CsmError::VSCodeRunning);
    }

    let mut compacted = 0;
    let mut fields_fixed = 0;

    if chat_sessions_dir.exists() {
        // Pass 0.5a: Recover from .json.bak when .jsonl has fewer requests
        match recover_from_json_bak(chat_sessions_dir) {
            Ok(n) if n > 0 => {
                println!("   [OK] Recovered {} session(s) from .json.bak backups", n);
            }
            _ => {}
        }

        // Pass 0.5b: Recover from .jsonl.bak when backup is larger than active file
        match recover_from_jsonl_bak(chat_sessions_dir, false) {
            Ok((n, bytes)) if n > 0 => {
                println!(
                    "   [OK] Restored {} session(s) from .jsonl.bak ({:.1}MB recovered)",
                    n,
                    bytes as f64 / (1024.0 * 1024.0)
                );
            }
            _ => {}
        }

        // Pass 1: Compact large JSONL files and fix missing fields
        for entry in std::fs::read_dir(chat_sessions_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "jsonl") {
                let metadata = std::fs::metadata(&path)?;
                let size_mb = metadata.len() / (1024 * 1024);

                let raw_content = std::fs::read_to_string(&path)
                    .map_err(|e| CsmError::InvalidSessionFormat(format!("Read error: {}", e)))?;

                // Pre-process: split concatenated JSON objects that lack newline
                // separators. VS Code sometimes appends delta ops to line 0 without
                // a \n, producing: {"kind":0,...}{"kind":1,...}
                // If splitting changes the content, rewrite the file first.
                let content = split_concatenated_jsonl(&raw_content);
                if content != raw_content {
                    std::fs::write(&path, content.as_bytes())?;
                    let stem = path
                        .file_stem()
                        .map(|s| s.to_string_lossy().to_string())
                        .unwrap_or_default();
                    println!("   [OK] Fixed concatenated JSONL objects: {}", stem);
                }
                let line_count = content.lines().count();

                if line_count > 1 {
                    // Compact multi-line JSONL (has operations to replay)
                    let stem = path
                        .file_stem()
                        .map(|s| s.to_string_lossy().to_string())
                        .unwrap_or_default();
                    println!(
                        "   Compacting {} ({} lines, {}MB)...",
                        stem, line_count, size_mb
                    );

                    match compact_session_jsonl(&path) {
                        Ok(backup_path) => {
                            let new_size = std::fs::metadata(&path)
                                .map(|m| m.len() / (1024 * 1024))
                                .unwrap_or(0);
                            println!(
                                "   [OK] Compacted: {}MB -> {}MB (backup: {})",
                                size_mb,
                                new_size,
                                backup_path
                                    .file_name()
                                    .unwrap_or_default()
                                    .to_string_lossy()
                            );
                            compacted += 1;
                        }
                        Err(e) => {
                            println!("   [WARN] Failed to compact {}: {}", stem, e);
                        }
                    }
                } else {
                    // Single-line JSONL — check for missing VS Code fields
                    if let Some(first_line) = content.lines().next() {
                        if let Ok(mut obj) = serde_json::from_str::<serde_json::Value>(first_line) {
                            let is_kind_0 = obj
                                .get("kind")
                                .and_then(|k| k.as_u64())
                                .map(|k| k == 0)
                                .unwrap_or(false);

                            if is_kind_0 {
                                if let Some(v) = obj.get("v") {
                                    // Check if fields are missing OR have wrong values.
                                    // hasPendingEdits must be false — true prevents session loading
                                    // because VS Code tries to restore stale file edits that fail.
                                    let needs_fix = !v.get("inputState").is_some()
                                        || !v.get("sessionId").is_some()
                                        || v.get("hasPendingEdits")
                                            .and_then(|v| v.as_bool())
                                            .unwrap_or(true)
                                            != false
                                        || v.get("pendingRequests")
                                            .and_then(|v| v.as_array())
                                            .map(|a| !a.is_empty())
                                            .unwrap_or(true);

                                    if needs_fix {
                                        let session_id = path
                                            .file_stem()
                                            .and_then(|s| s.to_str())
                                            .map(|s| s.to_string());
                                        if let Some(v_mut) = obj.get_mut("v") {
                                            ensure_vscode_compat_fields(
                                                v_mut,
                                                session_id.as_deref(),
                                            );
                                        }
                                        let patched = serde_json::to_string(&obj).map_err(|e| {
                                            CsmError::InvalidSessionFormat(format!(
                                                "Failed to serialize: {}",
                                                e
                                            ))
                                        })?;
                                        // Trailing newline prevents concatenation
                                        std::fs::write(&path, format!("{}\n", patched))?;
                                        let stem = path
                                            .file_stem()
                                            .map(|s| s.to_string_lossy().to_string())
                                            .unwrap_or_default();
                                        println!("   [OK] Fixed VS Code compat fields: {}", stem);
                                        fields_fixed += 1;
                                    } else if !content.ends_with('\n') {
                                        // All compat fields correct but missing trailing newline
                                        std::fs::write(&path, format!("{}\n", first_line))?;
                                        let stem = path
                                            .file_stem()
                                            .map(|s| s.to_string_lossy().to_string())
                                            .unwrap_or_default();
                                        println!(
                                            "   [OK] Fixed missing trailing newline: {}",
                                            stem
                                        );
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    // Pass 1.5: Convert skeleton .json files to valid .jsonl.
    // Skeleton files are legacy .json files where all data has been stripped,
    // leaving only structural characters ({}, [], whitespace). We convert them
    // to valid minimal .jsonl, preserving title/timestamp from the index,
    // and rename the original to .json.corrupt (non-destructive).
    let mut skeletons_converted = 0;
    if chat_sessions_dir.exists() {
        // Read current index to get titles/timestamps for converted sessions
        let index_entries: std::collections::HashMap<String, (String, Option<i64>)> =
            if let Ok(index) = read_chat_session_index(&db_path) {
                index
                    .entries
                    .iter()
                    .map(|(id, e)| (id.clone(), (e.title.clone(), Some(e.last_message_date))))
                    .collect()
            } else {
                std::collections::HashMap::new()
            };

        // Collect .json files that don't have a corresponding .jsonl
        let mut jsonl_stems: HashSet<String> = HashSet::new();
        for entry in std::fs::read_dir(chat_sessions_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "jsonl") {
                if let Some(stem) = path.file_stem() {
                    jsonl_stems.insert(stem.to_string_lossy().to_string());
                }
            }
        }

        for entry in std::fs::read_dir(chat_sessions_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "json")
                && !path.to_string_lossy().ends_with(".bak")
                && !path.to_string_lossy().ends_with(".corrupt")
            {
                let stem = path
                    .file_stem()
                    .map(|s| s.to_string_lossy().to_string())
                    .unwrap_or_default();

                // Skip if .jsonl already exists
                if jsonl_stems.contains(&stem) {
                    continue;
                }

                let (title, timestamp) = index_entries
                    .get(&stem)
                    .map(|(t, ts)| (t.as_str(), *ts))
                    .unwrap_or(("Recovered Session", None));

                match convert_skeleton_json_to_jsonl(&path, Some(title), timestamp) {
                    Ok(Some(jsonl_path)) => {
                        println!(
                            "   [OK] Converted skeleton .json → .jsonl: {} (\"{}\")",
                            stem, title
                        );
                        // Track the new .jsonl so subsequent passes process it
                        jsonl_stems.insert(stem);
                        skeletons_converted += 1;
                        let _ = jsonl_path; // used implicitly via jsonl_stems
                    }
                    Ok(None) => {} // Not a skeleton or skipped
                    Err(e) => {
                        println!("   [WARN] Failed to convert skeleton {}: {}", stem, e);
                    }
                }
            }
        }
    }

    // Pass 2: Fix cancelled modelState in all JSONL files.
    // VS Code reads modelState from file content (not the index) to determine
    // lastResponseState. If the last request has modelState.value=2 (Cancelled)
    // or is missing entirely, VS Code refuses to load the session.
    let mut cancelled_fixed = 0;
    if chat_sessions_dir.exists() {
        for entry in std::fs::read_dir(chat_sessions_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.extension().is_some_and(|e| e == "jsonl") {
                match fix_cancelled_model_state(&path) {
                    Ok(true) => {
                        let stem = path
                            .file_stem()
                            .map(|s| s.to_string_lossy().to_string())
                            .unwrap_or_default();
                        println!("   [OK] Fixed cancelled modelState: {}", stem);
                        cancelled_fixed += 1;
                    }
                    Ok(false) => {} // No fix needed
                    Err(e) => {
                        let stem = path
                            .file_stem()
                            .map(|s| s.to_string_lossy().to_string())
                            .unwrap_or_default();
                        println!("   [WARN] Failed to fix modelState for {}: {}", stem, e);
                    }
                }
            }
        }
    }

    // Pass 3: Rebuild the index with correct metadata
    let (index_fixed, _) = sync_session_index(workspace_id, chat_sessions_dir, force)?;

    if fields_fixed > 0 {
        println!(
            "   [OK] Injected missing VS Code fields into {} session(s)",
            fields_fixed
        );
    }
    if skeletons_converted > 0 {
        println!(
            "   [OK] Converted {} skeleton .json file(s) to .jsonl",
            skeletons_converted
        );
    }
    if cancelled_fixed > 0 {
        println!(
            "   [OK] Fixed cancelled modelState in {} session(s)",
            cancelled_fixed
        );
    }

    Ok((compacted, index_fixed))
}