tokmesh-core 0.1.5

Core library for tokmesh — session parsing, scanning, aggregation, pricing
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
//! OpenCode session parser
//!
//! Parses messages from:
//! - SQLite database (OpenCode 1.2+): ~/.local/share/opencode/opencode.db
//! - Legacy JSON files: ~/.local/share/opencode/storage/message/
//!
//! The SQLite message schema — and the driver that reads it — is shared with
//! the other clients that adopted it; see `super::opencode_schema`. This
//! module keeps OpenCode's own legacy JSON file parser and its JSON-to-SQLite
//! migration cache.

// The message payload type and the SQLite driver are shared with every other
// client that adopted OpenCode's schema.
use super::opencode_schema::{
    parse_opencode_schema_sqlite, reported_cost, rescan_opencode_schema_sqlite,
    scan_opencode_schema_sqlite, set_workspace_from_root, OpenCodeIncrementalState,
    OpenCodeSchemaConfig, OpenCodeSchemaMessage as OpenCodeMessage, OpenCodeSchemaScan,
};
use super::utils::read_file_or_none;
use super::{normalize_opencode_agent_name, UnifiedMessage};
use crate::{provider_identity, TokenBreakdown};
#[cfg(test)]
use rusqlite::Connection;
use serde::{Deserialize, Serialize};
use std::path::Path;

pub fn parse_opencode_sqlite(db_path: &Path) -> Vec<UnifiedMessage> {
    parse_opencode_schema_sqlite(db_path, OpenCodeSchemaConfig::opencode())
}

/// Full scan that also records where a later scan can resume from.
pub(crate) fn scan_opencode_sqlite(db_path: &Path) -> OpenCodeSchemaScan {
    scan_opencode_schema_sqlite(db_path, OpenCodeSchemaConfig::opencode())
}

/// Resume from `cached_state`, reading only the rows that changed. `None`
/// means the caller has to fall back to [`scan_opencode_sqlite`].
pub(crate) fn rescan_opencode_sqlite(
    db_path: &Path,
    cached_state: &OpenCodeIncrementalState,
    cached_messages: Vec<UnifiedMessage>,
) -> Option<OpenCodeSchemaScan> {
    rescan_opencode_schema_sqlite(
        db_path,
        OpenCodeSchemaConfig::opencode(),
        cached_state,
        cached_messages,
    )
}

pub fn parse_opencode_file(path: &Path) -> Option<UnifiedMessage> {
    let data = read_file_or_none(path)?;
    let mut bytes = data;

    let msg: OpenCodeMessage = simd_json::from_slice(&mut bytes).ok()?;

    // OpenCode JSON files (v1) always carry an explicit role, so require it to
    // be "assistant" here. Missing-role acceptance (is_assistant) is reserved
    // for the v2 `session_message` SQLite path, whose SQL already filters
    // `type = 'assistant'`; applying it to files would count a role-less or
    // malformed file as assistant usage (previously it was skipped when the
    // required `role` field failed to deserialize).
    if msg.role.as_deref() != Some("assistant") {
        return None;
    }

    let workspace_root = msg
        .path
        .as_ref()
        .and_then(|path| path.root.as_deref())
        .map(str::to_string);
    // Resolve model + provider before moving any fields out of `msg`, since
    // both borrow the whole struct to fall back onto the nested `model` object.
    let model_id = msg.resolve_model_id()?;
    let provider_id = msg
        .resolve_provider_id()
        .unwrap_or_else(|| "unknown".to_string());
    let provider_id = provider_identity::canonical_provider(&provider_id).unwrap_or(provider_id);

    let tokens = msg.tokens?;
    // Legacy JSON files carry a complete `cache` object; a missing or partial
    // one has always dropped the message rather than counting it as zero.
    let cache = tokens.cache?;
    let (cache_read, cache_write) = (cache.read?, cache.write?);
    let time = msg.time?;
    let agent_or_mode = msg.mode.or(msg.agent);
    let agent = agent_or_mode.map(|a| normalize_opencode_agent_name(&a));

    let session_id = msg.session_id.unwrap_or_else(|| "unknown".to_string());

    // Embedded message ids are globally stable across the legacy JSON and
    // SQLite representations, so keep them unnamespaced for overlap and fork
    // dedup. A filename is only unique inside its session directory; make the
    // no-id fallback path-scoped so same-named files in separate sessions do
    // not silently collapse (#1198). Canonicalization also keeps one physical
    // file reached through two path spellings on one key. If a non-UTF-8 path
    // cannot be represented, leave the key absent: retaining both candidates
    // is safer than inventing a lossy identity that can undercount.
    let dedup_key = msg.id.or_else(|| legacy_json_path_dedup_key(path));
    let cost = reported_cost(msg.cost).unwrap_or(0.0);

    let mut unified = UnifiedMessage::new_with_agent(
        "opencode",
        model_id,
        provider_id,
        session_id,
        time.created as i64,
        TokenBreakdown {
            input: tokens.input.max(0),
            output: tokens.output.max(0),
            cache_read: cache_read.max(0),
            cache_write: cache_write.max(0),
            reasoning: tokens.reasoning.unwrap_or(0).max(0),
        },
        cost,
        agent,
    );
    unified.duration_ms = time.completed.and_then(|completed| {
        let duration = completed - time.created;
        (duration.is_finite() && duration > 0.0).then_some(duration as i64)
    });
    unified.dedup_key = dedup_key;
    set_workspace_from_root(&mut unified, workspace_root.as_deref());
    // OpenCode computes per-message cost at request time from its own pricing
    // data (models.dev), so a positive `cost` is authoritative and must survive
    // tokmesh's LiteLLM repricing pass. A zero cost usually means OpenCode
    // itself had no pricing for the model — leave it `Unknown` so
    // `apply_pricing_if_available` can still estimate.
    if unified.cost > 0.0 {
        unified.mark_provider_reported_cost();
    }
    Some(unified)
}

fn legacy_json_path_dedup_key(path: &Path) -> Option<String> {
    let canonical = std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf());
    canonical
        .to_str()
        .map(|path| format!("legacy-json-path:{path}"))
}

// =============================================================================
// Migration cache: skip redundant legacy JSON scanning after full migration
// =============================================================================

const MIGRATION_CACHE_FILENAME: &str = "opencode-migration.json";

/// Persisted migration status for OpenCode JSON → SQLite migration.
/// Stored at <config_dir>/cache/opencode-migration.json.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct OpenCodeMigrationCache {
    /// True when every legacy JSON message was already present in SQLite.
    pub migration_complete: bool,
    /// Number of JSON files in the message directory at detection time.
    pub json_file_count: u64,
    /// Modification time of the JSON directory (Unix seconds) at detection time.
    pub json_dir_mtime_secs: u64,
    /// When this entry was written (Unix seconds).
    pub checked_at_secs: u64,
}

fn migration_cache_dir() -> std::path::PathBuf {
    crate::paths::get_cache_dir()
}

fn migration_cache_path() -> std::path::PathBuf {
    migration_cache_dir().join(MIGRATION_CACHE_FILENAME)
}

fn legacy_migration_cache_paths() -> Vec<std::path::PathBuf> {
    Vec::new()
}

/// Load the migration cache from disk. Returns `None` if the file is missing or
/// unparseable.
pub fn load_opencode_migration_cache() -> Option<OpenCodeMigrationCache> {
    let canonical = migration_cache_path();
    match std::fs::read_to_string(&canonical) {
        Ok(content) => serde_json::from_str(&content).ok(),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            legacy_migration_cache_paths().into_iter().find_map(|path| {
                let content = std::fs::read_to_string(path).ok()?;
                serde_json::from_str(&content).ok()
            })
        }
        Err(_) => None,
    }
}

/// Persist the migration cache atomically (write to temp file, then rename).
pub fn save_opencode_migration_cache(cache: &OpenCodeMigrationCache) {
    use std::io::Write as _;

    let dir = migration_cache_dir();
    if std::fs::create_dir_all(&dir).is_err() {
        return;
    }

    let content = match serde_json::to_string(cache) {
        Ok(c) => c,
        Err(_) => return,
    };

    let final_path = migration_cache_path();
    let nanos = std::time::SystemTime::now()
        .duration_since(std::time::SystemTime::UNIX_EPOCH)
        .map(|d| d.as_nanos() as u64)
        .unwrap_or(0);
    let tmp_name = format!(".opencode-migration.{}.{:x}.tmp", std::process::id(), nanos);
    let tmp_path = dir.join(tmp_name);

    // INVARIANT: All cache writes use atomic temp-file rename. NEVER delete
    // the canonical cache file before writing — a partial save or process
    // crash between delete and rename would lose the cache. The temp-file
    // pattern makes corruption-on-crash impossible.
    let result = (|| -> std::io::Result<()> {
        let mut file = std::fs::File::create(&tmp_path)?;
        file.write_all(content.as_bytes())?;
        file.sync_all()?;
        crate::fs_atomic::replace_file(&tmp_path, &final_path)?;
        Ok(())
    })();

    if result.is_err() {
        let _ = std::fs::remove_file(&tmp_path);
    }
}

/// Return the modification time of `json_dir` as Unix seconds, or `None` on
/// error (directory absent, permissions, etc.).
pub fn get_json_dir_mtime(json_dir: &Path) -> Option<u64> {
    std::fs::metadata(json_dir)
        .ok()?
        .modified()
        .ok()?
        .duration_since(std::time::SystemTime::UNIX_EPOCH)
        .ok()
        .map(|d| d.as_secs())
}

/// Current Unix timestamp in seconds.
pub fn now_secs() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::SystemTime::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

#[cfg(test)]
mod tests {
    use super::*;
    fn create_opencode_sqlite_db(db_path: &Path) -> Connection {
        let conn = Connection::open(db_path).unwrap();
        conn.execute_batch(
            "CREATE TABLE message (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL,
                data TEXT NOT NULL
            );",
        )
        .unwrap();
        conn
    }

    /// Build a database shaped like OpenCode v2 (`opencode-next.db`): an empty
    /// `message` table plus the `session_message` + `session` tables that hold
    /// the real per-message data. Mirrors the columns tokmesh actually reads.
    fn create_opencode_v2_sqlite_db(db_path: &Path) -> Connection {
        let conn = Connection::open(db_path).unwrap();
        conn.execute_batch(
            "CREATE TABLE message (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL,
                data TEXT NOT NULL
            );
            CREATE TABLE session (
                id TEXT PRIMARY KEY,
                directory TEXT NOT NULL,
                title TEXT
            );
            CREATE TABLE session_message (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL,
                type TEXT NOT NULL,
                data TEXT NOT NULL
            );",
        )
        .unwrap();
        conn
    }

    /// Build a database shaped like current OpenCode v2: `session_v2` carries
    /// metadata while `session_message` holds the assistant usage payloads.
    fn create_opencode_session_v2_sqlite_db(db_path: &Path) -> Connection {
        let conn = Connection::open(db_path).unwrap();
        conn.execute_batch(
            "CREATE TABLE session_v2 (
                id TEXT PRIMARY KEY,
                directory TEXT NOT NULL,
                title TEXT
            );
            CREATE TABLE session_message (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL,
                type TEXT NOT NULL,
                data TEXT NOT NULL
            );",
        )
        .unwrap();
        conn
    }

    /// A representative v2 assistant payload: no `role` field, model + provider
    /// nested under `$.model`, integer timestamps.
    const V2_ASSISTANT_DATA: &str = r#"{
        "time": { "created": 1783882279705, "completed": 1783882279943 },
        "agent": "build",
        "model": { "id": "claude-sonnet-4", "providerID": "anthropic", "variant": "default" },
        "content": [],
        "finish": "stop",
        "cost": 0.0123,
        "tokens": {
            "input": 5519,
            "output": 20,
            "reasoning": 23,
            "cache": { "read": 100, "write": 50 }
        }
    }"#;

    #[test]
    fn test_deserialize_v2_message_resolves_nested_model() {
        let mut bytes = V2_ASSISTANT_DATA.as_bytes().to_vec();
        let msg: OpenCodeMessage = simd_json::from_slice(&mut bytes).unwrap();

        assert_eq!(msg.role, None, "v2 payloads carry no role field");
        assert!(msg.is_assistant(), "missing role defaults to assistant");
        assert_eq!(msg.resolve_model_id().as_deref(), Some("claude-sonnet-4"));
        assert_eq!(msg.resolve_provider_id().as_deref(), Some("anthropic"));
        assert_eq!(msg.agent.as_deref(), Some("build"));
    }

    #[test]
    fn test_top_level_model_id_takes_precedence_over_nested() {
        let json = r#"{
            "role": "assistant",
            "modelID": "top-level-model",
            "providerID": "top-level-provider",
            "model": { "id": "nested-model", "providerID": "nested-provider" },
            "tokens": { "input": 1, "output": 1, "reasoning": 0, "cache": { "read": 0, "write": 0 } },
            "time": { "created": 1700000000000.0 }
        }"#;
        let mut bytes = json.as_bytes().to_vec();
        let msg: OpenCodeMessage = simd_json::from_slice(&mut bytes).unwrap();

        assert_eq!(msg.resolve_model_id().as_deref(), Some("top-level-model"));
        assert_eq!(
            msg.resolve_provider_id().as_deref(),
            Some("top-level-provider")
        );
    }

    #[test]
    fn test_parse_v2_session_message_reads_tokens_and_workspace() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode-next.db");

        let conn = create_opencode_v2_sqlite_db(&db_path);
        conn.execute(
            "INSERT INTO session (id, directory) VALUES (?1, ?2)",
            rusqlite::params!["ses_v2", "/Users/alice/opencode-v2-repo"],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["msg_v2_001", "ses_v2", "assistant", V2_ASSISTANT_DATA],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(messages.len(), 1, "v2 assistant row should be parsed");
        let msg = &messages[0];
        assert_eq!(msg.model_id, "claude-sonnet-4");
        assert_eq!(msg.provider_id, "anthropic");
        assert_eq!(msg.tokens.input, 5519);
        assert_eq!(msg.tokens.output, 20);
        assert_eq!(msg.tokens.reasoning, 23);
        assert_eq!(msg.tokens.cache_read, 100);
        assert_eq!(msg.tokens.cache_write, 50);
        assert_eq!(msg.duration_ms, Some(238));
        assert_eq!(
            msg.workspace_key.as_deref(),
            Some("/Users/alice/opencode-v2-repo"),
            "workspace should come from session.directory"
        );
        assert_eq!(msg.workspace_label.as_deref(), Some("opencode-v2-repo"));
        assert_eq!(
            msg.dedup_key.as_deref(),
            Some("msg_v2_001"),
            "v2 dedup_key falls back to the session_message row id"
        );
        assert_eq!(
            msg.cost_source,
            crate::sessions::CostSource::ProviderReported
        );
    }
    #[test]
    fn test_parse_v2_session_v2_message_reads_tokens_and_metadata() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_opencode_session_v2_sqlite_db(&db_path);
        conn.execute(
            "INSERT INTO session_v2 (id, directory, title) VALUES (?1, ?2, ?3)",
            rusqlite::params![
                "ses_current_v2",
                "/Users/alice/current-opencode-repo",
                "Current OpenCode session"
            ],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params![
                "msg_current_v2",
                "ses_current_v2",
                "assistant",
                V2_ASSISTANT_DATA
            ],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(messages.len(), 1, "current session_v2 rows should parse");
        let msg = &messages[0];
        assert_eq!(
            msg.workspace_key.as_deref(),
            Some("/Users/alice/current-opencode-repo")
        );
        assert_eq!(
            msg.workspace_label.as_deref(),
            Some("current-opencode-repo")
        );
        assert_eq!(
            msg.session_title.as_deref(),
            Some("Current OpenCode session")
        );
        assert_eq!(msg.dedup_key.as_deref(), Some("msg_current_v2"));
    }

    #[test]
    fn test_parse_v2_session_message_without_metadata_table() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = Connection::open(&db_path).unwrap();
        conn.execute_batch(
            "CREATE TABLE session_message (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL,
                type TEXT NOT NULL,
                data TEXT NOT NULL
            );",
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params![
                "msg_without_metadata",
                "ses_without_metadata",
                "assistant",
                V2_ASSISTANT_DATA
            ],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(
            messages.len(),
            1,
            "usage should parse without session metadata"
        );
        assert_eq!(messages[0].workspace_key, None);
        assert_eq!(messages[0].session_title, None);
        assert_eq!(
            messages[0].dedup_key.as_deref(),
            Some("msg_without_metadata")
        );
    }

    #[test]
    fn test_parse_v2_skips_non_assistant_and_tokenless_rows() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode-next.db");

        let conn = create_opencode_v2_sqlite_db(&db_path);
        let user_data = r#"{ "time": { "created": 1783882279705 }, "content": [] }"#;
        let tokenless = r#"{ "time": { "created": 1783882279705 }, "model": { "id": "m", "providerID": "p" } }"#;
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["msg_ok", "ses_v2", "assistant", V2_ASSISTANT_DATA],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["msg_user", "ses_v2", "user", user_data],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["msg_synthetic", "ses_v2", "synthetic", V2_ASSISTANT_DATA],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["msg_no_tokens", "ses_v2", "assistant", tokenless],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(
            messages.len(),
            1,
            "only the assistant row with tokens should parse"
        );
        assert_eq!(messages[0].dedup_key.as_deref(), Some("msg_ok"));
    }

    #[test]
    fn test_parse_v2_negative_tokens_clamped() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode-next.db");

        let conn = create_opencode_v2_sqlite_db(&db_path);
        let negative = r#"{
            "time": { "created": 1783882279705 },
            "model": { "id": "claude-sonnet-4", "providerID": "anthropic" },
            "cost": -1.0,
            "tokens": { "input": -100, "output": -50, "reasoning": -25, "cache": { "read": -200, "write": -10 } }
        }"#;
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["msg_neg", "ses_v2", "assistant", negative],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(messages.len(), 1);
        let msg = &messages[0];
        assert_eq!(msg.tokens.input, 0);
        assert_eq!(msg.tokens.output, 0);
        assert_eq!(msg.tokens.reasoning, 0);
        assert_eq!(msg.tokens.cache_read, 0);
        assert_eq!(msg.tokens.cache_write, 0);
        assert!(msg.cost >= 0.0);
    }

    #[test]
    fn test_parse_v2_deduplicates_forked_session_message_history() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode-next.db");

        let conn = create_opencode_v2_sqlite_db(&db_path);
        // Same payload copied into a forked session must collapse to one entry.
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["root_row", "root_session", "assistant", V2_ASSISTANT_DATA],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["fork_row", "fork_session", "assistant", V2_ASSISTANT_DATA],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(
            messages.len(),
            1,
            "forked copies of the same assistant turn collapse inside v2 parsing"
        );
    }

    #[test]
    fn test_distinct_embedded_ids_are_not_merged_despite_fingerprint_collision() {
        // Two genuinely different assistant messages can share every fingerprint
        // field (timestamp, model, tokens, cost, agent). When both carry an
        // embedded `$.id` and the ids DIFFER, they are distinct messages -- not
        // fork copies -- and must be kept separate rather than collapsed.
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode-next.db");
        let conn = create_opencode_v2_sqlite_db(&db_path);

        let payload = |id: &str| {
            format!(
                r#"{{
                    "id": "{id}",
                    "time": {{ "created": 1783882279705, "completed": 1783882279943 }},
                    "agent": "build",
                    "model": {{ "id": "claude-sonnet-4", "providerID": "anthropic" }},
                    "cost": 0.0123,
                    "tokens": {{ "input": 10, "output": 5, "reasoning": 0, "cache": {{ "read": 0, "write": 0 }} }}
                }}"#
            )
        };

        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["row_a", "ses_v2", "assistant", payload("msg_a")],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["row_b", "ses_v2", "assistant", payload("msg_b")],
        )
        .unwrap();
        // A true fork of msg_a (same embedded id, different session/row) must
        // still collapse into msg_a rather than becoming a third entry.
        conn.execute(
            "INSERT INTO session_message (id, session_id, type, data) VALUES (?1, ?2, ?3, ?4)",
            rusqlite::params!["row_a_fork", "fork_session", "assistant", payload("msg_a")],
        )
        .unwrap();
        drop(conn);

        let mut dedup_keys: Vec<String> = parse_opencode_sqlite(&db_path)
            .into_iter()
            .filter_map(|m| m.dedup_key)
            .collect();
        dedup_keys.sort();
        assert_eq!(
            dedup_keys,
            vec!["msg_a".to_string(), "msg_b".to_string()],
            "distinct embedded ids stay separate; a same-id fork collapses"
        );
    }

    #[test]
    fn test_parse_opencode_structure() {
        let json = r#"{
            "id": "msg_123",
            "sessionID": "ses_456",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.05,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 100,
                "cache": { "read": 200, "write": 50 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        let mut bytes = json.as_bytes().to_vec();
        let msg: OpenCodeMessage = simd_json::from_slice(&mut bytes).unwrap();

        assert_eq!(msg.model_id, Some("claude-sonnet-4".to_string()));
        assert_eq!(msg.tokens.unwrap().input, 1000);
        assert_eq!(msg.agent, None);
    }

    #[test]
    fn test_parse_opencode_with_agent() {
        let json = r#"{
            "id": "msg_123",
            "sessionID": "ses_456",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "agent": "OmO",
            "cost": 0.05,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 100,
                "cache": { "read": 200, "write": 50 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        let mut bytes = json.as_bytes().to_vec();
        let msg: OpenCodeMessage = simd_json::from_slice(&mut bytes).unwrap();

        assert_eq!(msg.agent, Some("OmO".to_string()));
    }

    /// Verify negative token values are clamped to 0 (defense-in-depth for PR #147)
    #[test]
    fn test_negative_values_clamped_to_zero() {
        use std::io::Write;

        let json = r#"{
            "id": "msg_negative",
            "sessionID": "ses_negative",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": -0.05,
            "tokens": {
                "input": -100,
                "output": -50,
                "reasoning": -25,
                "cache": { "read": -200, "write": -10 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        let mut temp_file = tempfile::Builder::new().suffix(".json").tempfile().unwrap();
        temp_file.write_all(json.as_bytes()).unwrap();

        let result = parse_opencode_file(temp_file.path());
        assert!(result.is_some(), "Should parse file with negative values");

        let msg = result.unwrap();
        assert_eq!(msg.tokens.input, 0, "Negative input should be clamped to 0");
        assert_eq!(
            msg.tokens.output, 0,
            "Negative output should be clamped to 0"
        );
        assert_eq!(
            msg.tokens.cache_read, 0,
            "Negative cache_read should be clamped to 0"
        );
        assert_eq!(
            msg.tokens.cache_write, 0,
            "Negative cache_write should be clamped to 0"
        );
        assert_eq!(
            msg.tokens.reasoning, 0,
            "Negative reasoning should be clamped to 0"
        );
        assert!(
            msg.cost >= 0.0,
            "Negative cost should be clamped to 0.0, got {}",
            msg.cost
        );
    }

    #[test]
    fn test_parse_opencode_file_requires_explicit_assistant_role() {
        use std::io::Write;
        // Regression: making `role` optional for the v2 SQLite path must NOT
        // loosen file parsing. A file without a `role` (or a non-assistant one)
        // is not assistant usage and must be skipped -- the missing-role =>
        // assistant shortcut applies only to the type-filtered session_message
        // SQLite query, never to JSON files.
        let role_less = r#"{
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": { "input": 10, "output": 5, "reasoning": 0, "cache": { "read": 0, "write": 0 } },
            "time": { "created": 1700000000000.0 }
        }"#;
        let mut f1 = tempfile::Builder::new().suffix(".json").tempfile().unwrap();
        f1.write_all(role_less.as_bytes()).unwrap();
        assert!(
            parse_opencode_file(f1.path()).is_none(),
            "a role-less OpenCode JSON file must not be counted as assistant usage"
        );

        let user_role = r#"{
            "role": "user",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": { "input": 10, "output": 5, "reasoning": 0, "cache": { "read": 0, "write": 0 } },
            "time": { "created": 1700000000000.0 }
        }"#;
        let mut f2 = tempfile::Builder::new().suffix(".json").tempfile().unwrap();
        f2.write_all(user_role.as_bytes()).unwrap();
        assert!(
            parse_opencode_file(f2.path()).is_none(),
            "a non-assistant OpenCode JSON file must be skipped"
        );
    }

    /// JSON dedup_key uses msg.id when present
    #[test]
    fn test_dedup_key_from_json_message_id() {
        use std::io::Write;

        let json = r#"{
            "id": "msg_dedup_001",
            "sessionID": "ses_001",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.01,
            "tokens": {
                "input": 100,
                "output": 50,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        let mut temp_file = tempfile::Builder::new().suffix(".json").tempfile().unwrap();
        temp_file.write_all(json.as_bytes()).unwrap();

        let msg = parse_opencode_file(temp_file.path()).expect("Should parse");
        assert_eq!(
            msg.dedup_key,
            Some("msg_dedup_001".to_string()),
            "dedup_key should use msg.id from JSON"
        );
    }

    #[test]
    fn test_parse_opencode_file_sets_duration_from_completed_time() {
        use std::io::Write;

        let json = r#"{
            "id": "msg_timed",
            "sessionID": "ses_001",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.01,
            "tokens": {
                "input": 100,
                "output": 50,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0, "completed": 1700000001234.0 }
        }"#;

        let mut temp_file = tempfile::Builder::new().suffix(".json").tempfile().unwrap();
        temp_file.write_all(json.as_bytes()).unwrap();

        let msg = parse_opencode_file(temp_file.path()).expect("Should parse");
        assert_eq!(msg.duration_ms, Some(1234));
    }

    /// JSON dedup_key falls back to a path-scoped identity when msg.id is absent.
    #[test]
    fn test_dedup_key_falls_back_to_canonical_file_path() {
        let json = r#"{
            "sessionID": "ses_001",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.01,
            "tokens": {
                "input": 100,
                "output": 50,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("msg_fallback_999.json");
        std::fs::write(&file_path, json).unwrap();

        let msg = parse_opencode_file(&file_path).expect("Should parse");
        assert_eq!(
            msg.dedup_key,
            legacy_json_path_dedup_key(&file_path),
            "an id-less message must use the file's canonical location"
        );
        assert!(msg
            .dedup_key
            .as_deref()
            .is_some_and(|key| key.starts_with("legacy-json-path:")));
    }

    #[test]
    fn same_named_idless_files_in_different_sessions_have_distinct_keys() {
        let json = r#"{
            "sessionID": "embedded-session-is-not-the-fallback",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": {
                "input": 100,
                "output": 50,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;
        let root = tempfile::tempdir().unwrap();
        let first = root.path().join("session-a/same-name.json");
        let second = root.path().join("session-b/same-name.json");
        std::fs::create_dir_all(first.parent().unwrap()).unwrap();
        std::fs::create_dir_all(second.parent().unwrap()).unwrap();
        std::fs::write(&first, json).unwrap();
        std::fs::write(&second, json).unwrap();

        let first = parse_opencode_file(&first).unwrap();
        let second = parse_opencode_file(&second).unwrap();

        assert_ne!(first.dedup_key, second.dedup_key);
        assert!(first.dedup_key.is_some());
        assert!(second.dedup_key.is_some());
    }

    /// Non-assistant messages are skipped (no dedup_key produced)
    #[test]
    fn test_dedup_key_skips_non_assistant() {
        let json = r#"{
            "id": "msg_user_001",
            "sessionID": "ses_001",
            "role": "user",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": {
                "input": 100,
                "output": 50,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("msg_user_001.json");
        std::fs::write(&file_path, json).unwrap();

        let result = parse_opencode_file(&file_path);
        assert!(result.is_none(), "User messages should be skipped");
    }

    /// SQLite dedup_key falls back to the database row id when the message has no embedded id.
    #[test]
    fn test_sqlite_dedup_key_falls_back_to_row_id() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test_opencode.db");

        let conn = create_opencode_sqlite_db(&db_path);

        let data_json = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.05,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 0,
                "cache": { "read": 200, "write": 50 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_sqlite_001", "ses_001", data_json],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(messages.len(), 1);
        assert_eq!(
            messages[0].dedup_key,
            Some("msg_sqlite_001".to_string()),
            "SQLite dedup_key should fall back to the row id when no embedded id exists"
        );
        assert_eq!(messages[0].model_id, "claude-sonnet-4");
        assert_eq!(messages[0].tokens.input, 1000);
    }

    #[test]
    fn test_parse_opencode_file_marks_positive_cost_as_provider_reported() {
        use std::io::Write;
        let json = r#"{
            "id": "msg_cost_001",
            "sessionID": "ses_cost",
            "role": "assistant",
            "modelID": "z-ai/glm-4.6",
            "providerID": "openrouter",
            "cost": 0.0025158,
            "tokens": {
                "input": 2675,
                "output": 28,
                "reasoning": 1,
                "cache": { "read": 7700, "write": 0 }
            },
            "time": { "created": 1765915142201.0 }
        }"#;

        let mut temp_file = tempfile::Builder::new().suffix(".json").tempfile().unwrap();
        temp_file.write_all(json.as_bytes()).unwrap();

        let msg = parse_opencode_file(temp_file.path()).expect("Should parse");
        assert_eq!(
            msg.cost_source,
            crate::sessions::CostSource::ProviderReported,
            "positive embedded cost must survive the LiteLLM repricing pass"
        );
        assert!((msg.cost - 0.0025158).abs() < 1e-12);
    }

    #[test]
    fn test_parse_opencode_file_keeps_zero_cost_unknown_for_estimation() {
        use std::io::Write;
        let json = r#"{
            "id": "msg_cost_002",
            "sessionID": "ses_cost",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.0,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        let mut temp_file = tempfile::Builder::new().suffix(".json").tempfile().unwrap();
        temp_file.write_all(json.as_bytes()).unwrap();

        let msg = parse_opencode_file(temp_file.path()).expect("Should parse");
        assert_eq!(
            msg.cost_source,
            crate::sessions::CostSource::Unknown,
            "zero cost means OpenCode had no pricing — leave repricing enabled"
        );
    }

    #[test]
    fn test_parse_opencode_sqlite_marks_positive_cost_as_provider_reported() {
        let dir = tempfile::TempDir::new().unwrap();
        let db_path = dir.path().join("test_opencode_cost.db");
        let conn = create_opencode_sqlite_db(&db_path);

        let costed = r#"{
            "role": "assistant",
            "modelID": "z-ai/glm-4.6",
            "providerID": "openrouter",
            "cost": 0.0025158,
            "tokens": {
                "input": 2675,
                "output": 28,
                "reasoning": 1,
                "cache": { "read": 7700, "write": 0 }
            },
            "time": { "created": 1765915142201.0 }
        }"#;
        let free = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.0,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_costed", "ses_cost", costed],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_free", "ses_cost", free],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(messages.len(), 2);

        let costed_msg = messages
            .iter()
            .find(|m| m.dedup_key.as_deref() == Some("msg_costed"))
            .unwrap();
        assert_eq!(
            costed_msg.cost_source,
            crate::sessions::CostSource::ProviderReported
        );

        let free_msg = messages
            .iter()
            .find(|m| m.dedup_key.as_deref() == Some("msg_free"))
            .unwrap();
        assert_eq!(free_msg.cost_source, crate::sessions::CostSource::Unknown);
    }

    #[test]
    fn test_parse_opencode_file_uses_explicit_path_root_as_workspace() {
        let json = r#"{
            "id": "msg_workspace_001",
            "sessionID": "ses_001",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.01,
            "tokens": {
                "input": 100,
                "output": 50,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0 },
            "path": { "root": "/Users/alice/opencode-json-repo" }
        }"#;

        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("msg_workspace_001.json");
        std::fs::write(&file_path, json).unwrap();

        let msg = parse_opencode_file(&file_path).expect("Should parse");
        assert_eq!(
            msg.workspace_key.as_deref(),
            Some("/Users/alice/opencode-json-repo")
        );
        assert_eq!(msg.workspace_label.as_deref(), Some("opencode-json-repo"));
    }

    #[test]
    fn test_parse_opencode_file_ignores_non_object_path_without_rejecting_message() {
        let json = r#"{
            "id": "msg_path_string_001",
            "sessionID": "ses_001",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.01,
            "tokens": {
                "input": 100,
                "output": 50,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0 },
            "path": "/Users/alice/not-object"
        }"#;

        let dir = tempfile::tempdir().unwrap();
        let file_path = dir.path().join("msg_path_string_001.json");
        std::fs::write(&file_path, json).unwrap();

        let msg = parse_opencode_file(&file_path).expect("Should parse");
        assert_eq!(msg.workspace_key, None);
        assert_eq!(msg.workspace_label, None);
    }

    #[test]
    fn test_parse_opencode_sqlite_uses_session_directory_as_workspace() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test_opencode.db");

        let conn = create_opencode_sqlite_db(&db_path);
        conn.execute_batch(
            "CREATE TABLE session (
                id TEXT PRIMARY KEY,
                directory TEXT NOT NULL,
                title TEXT
            );",
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session (id, directory) VALUES (?1, ?2)",
            rusqlite::params!["ses_001", "/Users/alice/opencode-sqlite-repo"],
        )
        .unwrap();

        let data_json = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.05,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 0,
                "cache": { "read": 200, "write": 50 }
            },
            "time": { "created": 1700000000000.0 }
        }"#;

        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_sqlite_workspace", "ses_001", data_json],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(messages.len(), 1);
        assert_eq!(
            messages[0].workspace_key.as_deref(),
            Some("/Users/alice/opencode-sqlite-repo")
        );
        assert_eq!(
            messages[0].workspace_label.as_deref(),
            Some("opencode-sqlite-repo")
        );
    }

    #[test]
    fn test_parse_opencode_sqlite_legacy_fallback_uses_path_root_when_session_table_missing() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test_opencode.db");

        let conn = create_opencode_sqlite_db(&db_path);

        let data_json = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.05,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 0,
                "cache": { "read": 200, "write": 50 }
            },
            "time": { "created": 1700000000000.0 },
            "path": { "root": "/Users/alice/legacy-fallback-repo" }
        }"#;

        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_sqlite_legacy_workspace", "ses_001", data_json],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(messages.len(), 1);
        assert_eq!(
            messages[0].workspace_key.as_deref(),
            Some("/Users/alice/legacy-fallback-repo")
        );
        assert_eq!(
            messages[0].workspace_label.as_deref(),
            Some("legacy-fallback-repo")
        );
        assert_eq!(messages[0].tokens.input, 1000);
    }

    #[test]
    fn test_parse_opencode_sqlite_duplicate_workspace_conflict_is_unknown() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test_opencode.db");

        let conn = create_opencode_sqlite_db(&db_path);
        conn.execute_batch(
            "CREATE TABLE session (
                id TEXT PRIMARY KEY,
                directory TEXT NOT NULL,
                title TEXT
            );",
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session (id, directory) VALUES (?1, ?2)",
            rusqlite::params!["ses_root", "/Users/alice/root-workspace"],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO session (id, directory) VALUES (?1, ?2)",
            rusqlite::params!["ses_fork", "/Users/alice/fork-workspace"],
        )
        .unwrap();

        let data_json = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.05,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 0,
                "cache": { "read": 200, "write": 50 }
            },
            "time": { "created": 1700000000000.0, "completed": 1700000000500.0 },
            "mode": "build"
        }"#;

        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["z_root_copy", "ses_root", data_json],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["a_fork_copy", "ses_fork", data_json],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(messages.len(), 1);
        assert_eq!(messages[0].workspace_key, None);
        assert_eq!(messages[0].workspace_label, None);
        assert_eq!(messages[0].tokens.input, 1000);
    }

    /// SQLite prefers the embedded message id when present so JSON/SQLite overlap keeps deduplicating.
    #[test]
    fn test_sqlite_dedup_key_prefers_embedded_message_id() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test_opencode.db");

        let conn = create_opencode_sqlite_db(&db_path);

        let valid = r#"{
            "id": "embedded_msg_001",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": { "input": 100, "output": 50, "reasoning": 0, "cache": { "read": 0, "write": 0 } },
            "time": { "created": 1700000000000.0 }
        }"#;

        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["row_msg_001", "ses_001", valid],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(messages.len(), 1);
        assert_eq!(
            messages[0].dedup_key,
            Some("embedded_msg_001".to_string()),
            "SQLite dedup_key should prefer the embedded message id for cross-source overlap"
        );
    }

    /// SQLite skips rows without tokens or with non-assistant role
    #[test]
    fn test_sqlite_skips_invalid_rows() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test_opencode.db");

        let conn = create_opencode_sqlite_db(&db_path);

        let valid = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": { "input": 100, "output": 50, "reasoning": 0, "cache": { "read": 0, "write": 0 } },
            "time": { "created": 1700000000000.0 }
        }"#;

        let user_msg = r#"{
            "role": "user",
            "modelID": "claude-sonnet-4",
            "time": { "created": 1700000000000.0 }
        }"#;

        let no_tokens = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "time": { "created": 1700000000000.0 }
        }"#;

        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_valid", "ses_001", valid],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_user", "ses_001", user_msg],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_no_tokens", "ses_001", no_tokens],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(
            messages.len(),
            1,
            "Should only parse valid assistant message"
        );
        assert_eq!(messages[0].dedup_key, Some("msg_valid".to_string()));
    }

    /// Forked SQLite sessions should not count copied history more than once.
    #[test]
    fn test_sqlite_deduplicates_forked_history_rows() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test_opencode.db");
        let conn = create_opencode_sqlite_db(&db_path);

        let root_msg = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.05,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 25,
                "cache": { "read": 200, "write": 50 }
            },
            "time": { "created": 1700000000000.0, "completed": 1700000000500.0 },
            "mode": "build"
        }"#;

        let new_msg = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.08,
            "tokens": {
                "input": 1300,
                "output": 650,
                "reasoning": 40,
                "cache": { "read": 100, "write": 0 }
            },
            "time": { "created": 1700000001000.0, "completed": 1700000001500.0 },
            "mode": "build"
        }"#;

        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["root_row", "root_session", root_msg],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["fork_copy_row", "fork_session", root_msg],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["fork_new_row", "fork_session", new_msg],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(
            messages.len(),
            2,
            "Forked copies of the same assistant history should collapse inside SQLite parsing"
        );
        assert_eq!(messages[0].tokens.input, 1000);
        assert_eq!(messages[1].tokens.input, 1300);
    }

    /// Same-timestamp messages with different payloads should remain distinct.
    #[test]
    fn test_sqlite_same_timestamp_distinct_payloads_survive() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("test_opencode.db");
        let conn = create_opencode_sqlite_db(&db_path);

        let first = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.05,
            "tokens": {
                "input": 1000,
                "output": 500,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0, "completed": 1700000000100.0 },
            "mode": "build"
        }"#;

        let second = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "cost": 0.05,
            "tokens": {
                "input": 1500,
                "output": 750,
                "reasoning": 0,
                "cache": { "read": 0, "write": 0 }
            },
            "time": { "created": 1700000000000.0, "completed": 1700000000100.0 },
            "mode": "build"
        }"#;

        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["row_one", "session_one", first],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["row_two", "session_two", second],
        )
        .unwrap();
        drop(conn);

        let messages = parse_opencode_sqlite(&db_path);
        assert_eq!(
            messages.len(),
            2,
            "Distinct assistant calls should survive even when they share the same creation timestamp"
        );
    }

    /// Cross-source dedup: matching IDs between SQLite and JSON should deduplicate
    #[test]
    fn test_cross_source_dedup_by_message_id() {
        use std::collections::HashSet;

        let dir = tempfile::tempdir().unwrap();

        // --- SQLite source ---
        let db_path = dir.path().join("opencode.db");
        let conn = Connection::open(&db_path).unwrap();
        conn.execute_batch(
            "CREATE TABLE message (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL,
                data TEXT NOT NULL
            );",
        )
        .unwrap();

        let shared_data_json = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": { "input": 500, "output": 200, "reasoning": 0, "cache": { "read": 0, "write": 0 } },
            "time": { "created": 1700000000000.0 }
        }"#;
        let sqlite_only_data_json = r#"{
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": { "input": 700, "output": 250, "reasoning": 0, "cache": { "read": 0, "write": 0 } },
            "time": { "created": 1700000001000.0 }
        }"#;

        // Insert two messages into SQLite
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_shared_001", "ses_001", shared_data_json],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, ?2, ?3)",
            rusqlite::params!["msg_sqlite_only", "ses_001", sqlite_only_data_json],
        )
        .unwrap();
        drop(conn);

        // --- JSON source ---
        let json_dir = dir.path().join("json");
        std::fs::create_dir_all(&json_dir).unwrap();

        // Duplicate of SQLite msg_shared_001
        let json_shared = r#"{
            "id": "msg_shared_001",
            "sessionID": "ses_001",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": { "input": 500, "output": 200, "reasoning": 0, "cache": { "read": 0, "write": 0 } },
            "time": { "created": 1700000000000.0 }
        }"#;
        std::fs::write(json_dir.join("msg_shared_001.json"), json_shared).unwrap();

        // JSON-only message (not in SQLite)
        let json_only = r#"{
            "id": "msg_json_only",
            "sessionID": "ses_001",
            "role": "assistant",
            "modelID": "claude-sonnet-4",
            "providerID": "anthropic",
            "tokens": { "input": 100, "output": 50, "reasoning": 0, "cache": { "read": 0, "write": 0 } },
            "time": { "created": 1700000000000.0 }
        }"#;
        std::fs::write(json_dir.join("msg_json_only.json"), json_only).unwrap();

        // --- Simulate the dedup logic from lib.rs ---
        let sqlite_messages = parse_opencode_sqlite(&db_path);
        assert_eq!(sqlite_messages.len(), 2);

        // Build seen set from SQLite (same as lib.rs)
        let mut seen: HashSet<String> = HashSet::new();
        for msg in &sqlite_messages {
            if let Some(ref key) = msg.dedup_key {
                seen.insert(key.clone());
            }
        }
        assert_eq!(seen.len(), 2);

        // Parse JSON files
        let json_msg_shared = parse_opencode_file(&json_dir.join("msg_shared_001.json")).unwrap();
        let json_msg_only = parse_opencode_file(&json_dir.join("msg_json_only.json")).unwrap();

        // Filter JSON through seen set (same logic as lib.rs)
        let json_messages = vec![json_msg_shared, json_msg_only];
        let deduped: Vec<UnifiedMessage> = json_messages
            .into_iter()
            .filter(|msg| {
                msg.dedup_key
                    .as_ref()
                    .is_none_or(|key| seen.insert(key.clone()))
            })
            .collect();

        // msg_shared_001 should be filtered (duplicate), msg_json_only should survive
        assert_eq!(
            deduped.len(),
            1,
            "Only the JSON-only message should survive dedup"
        );
        assert_eq!(
            deduped[0].dedup_key,
            Some("msg_json_only".to_string()),
            "Surviving message should be the JSON-only one"
        );

        // Total unique messages = 2 from SQLite + 1 from JSON
        let total = sqlite_messages.len() + deduped.len();
        assert_eq!(total, 3, "Should have 3 unique messages total");
    }

    // -------------------------------------------------------------------------
    // Migration cache tests
    // -------------------------------------------------------------------------

    /// Round-trip: save then load returns identical data.
    #[test]
    fn test_migration_cache_round_trip() {
        let dir = tempfile::tempdir().unwrap();
        // Point the cache at a temp dir by overriding via a temporary env var is
        // impractical here; instead we test the structs and serde directly.
        let cache = OpenCodeMigrationCache {
            migration_complete: true,
            json_file_count: 42,
            json_dir_mtime_secs: 1_700_000_000,
            checked_at_secs: 1_700_100_000,
        };

        let json = serde_json::to_string(&cache).unwrap();
        let loaded: OpenCodeMigrationCache = serde_json::from_str(&json).unwrap();
        assert_eq!(loaded, cache);

        // Ensure the JSON contains all expected keys
        assert!(json.contains("migration_complete"));
        assert!(json.contains("json_file_count"));
        assert!(json.contains("json_dir_mtime_secs"));
        assert!(json.contains("checked_at_secs"));

        drop(dir);
    }

    /// Cache is valid when file count and mtime are unchanged.
    #[test]
    fn test_migration_cache_valid_when_unchanged() {
        let dir = tempfile::tempdir().unwrap();
        let json_dir = dir.path().join("message");
        std::fs::create_dir_all(&json_dir).unwrap();

        // Write a dummy file so the directory exists and has a stable mtime
        std::fs::write(json_dir.join("msg.json"), b"{}").unwrap();

        let current_mtime = get_json_dir_mtime(&json_dir).expect("should stat dir");
        let current_file_count = 1u64;

        let cache = OpenCodeMigrationCache {
            migration_complete: true,
            json_file_count: current_file_count,
            json_dir_mtime_secs: current_mtime, // same mtime
            checked_at_secs: now_secs(),
        };

        // Simulate the validity check from lib.rs
        let is_valid = cache.migration_complete
            && current_file_count == cache.json_file_count
            && get_json_dir_mtime(&json_dir).is_some_and(|m| m <= cache.json_dir_mtime_secs);

        assert!(is_valid, "Cache should be valid when count and mtime match");
    }

    /// Cache is invalid when file count has changed.
    #[test]
    fn test_migration_cache_invalid_when_file_count_changes() {
        let dir = tempfile::tempdir().unwrap();
        let json_dir = dir.path().join("message");
        std::fs::create_dir_all(&json_dir).unwrap();
        std::fs::write(json_dir.join("msg1.json"), b"{}").unwrap();

        let current_mtime = get_json_dir_mtime(&json_dir).unwrap();

        let cache = OpenCodeMigrationCache {
            migration_complete: true,
            json_file_count: 1,
            json_dir_mtime_secs: current_mtime,
            checked_at_secs: now_secs(),
        };

        // Simulate: a new file was added → current_file_count = 2
        let current_file_count = 2u64; // changed
        let is_valid = cache.migration_complete
            && current_file_count == cache.json_file_count
            && get_json_dir_mtime(&json_dir).is_some_and(|m| m <= cache.json_dir_mtime_secs);

        assert!(!is_valid, "Cache should be invalid when file count changes");
    }

    /// Cache is invalid when directory mtime is newer than cached value.
    #[test]
    fn test_migration_cache_invalid_when_mtime_newer() {
        let dir = tempfile::tempdir().unwrap();
        let json_dir = dir.path().join("message");
        std::fs::create_dir_all(&json_dir).unwrap();
        std::fs::write(json_dir.join("msg.json"), b"{}").unwrap();

        let current_mtime = get_json_dir_mtime(&json_dir).unwrap();

        // Simulate: cache recorded an older mtime → directory is now newer
        let stale_mtime = current_mtime.saturating_sub(1);
        let cache = OpenCodeMigrationCache {
            migration_complete: true,
            json_file_count: 1,
            json_dir_mtime_secs: stale_mtime, // older than current
            checked_at_secs: now_secs(),
        };

        let is_valid = cache.migration_complete
            && 1u64 == cache.json_file_count
            && get_json_dir_mtime(&json_dir).is_some_and(|m| m <= cache.json_dir_mtime_secs);

        assert!(
            !is_valid,
            "Cache should be invalid when directory mtime is newer than cached value"
        );
    }

    /// Cache is not loaded when the file is missing (load returns None).
    #[test]
    fn test_migration_cache_missing_returns_none() {
        // load_opencode_migration_cache reads from ~/.cache/tokmesh/opencode-migration.json
        // We can't easily override the path in a unit test, but we can verify that
        // serde_json::from_str returns None for invalid input (simulating missing file).
        let result: Option<OpenCodeMigrationCache> = serde_json::from_str("").ok();
        assert!(
            result.is_none(),
            "Empty/missing content should produce None"
        );
    }

    /// migration_complete=false disables the cache even if count/mtime match.
    #[test]
    fn test_migration_cache_not_skipped_when_incomplete() {
        let dir = tempfile::tempdir().unwrap();
        let json_dir = dir.path().join("message");
        std::fs::create_dir_all(&json_dir).unwrap();
        std::fs::write(json_dir.join("msg.json"), b"{}").unwrap();

        let current_mtime = get_json_dir_mtime(&json_dir).unwrap();

        let cache = OpenCodeMigrationCache {
            migration_complete: false, // migration not complete
            json_file_count: 1,
            json_dir_mtime_secs: current_mtime,
            checked_at_secs: now_secs(),
        };

        let is_valid = cache.migration_complete
            && 1u64 == cache.json_file_count
            && get_json_dir_mtime(&json_dir).is_some_and(|m| m <= cache.json_dir_mtime_secs);

        assert!(
            !is_valid,
            "Cache should not allow skipping when migration_complete=false"
        );
    }

    // =========================================================================
    // Incremental SQLite scan
    // =========================================================================

    /// A `message` table with the columns a modern OpenCode database actually
    /// has. The existing fixtures above deliberately keep the minimal column
    /// set, which is itself a case the incremental lane has to refuse.
    fn create_timed_v1_db(db_path: &Path) -> Connection {
        let conn = Connection::open(db_path).unwrap();
        conn.execute_batch(
            "CREATE TABLE session (
                id TEXT PRIMARY KEY,
                directory TEXT NOT NULL,
                title TEXT,
                time_updated INTEGER NOT NULL
            );
            CREATE TABLE message (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL,
                time_created INTEGER NOT NULL,
                time_updated INTEGER NOT NULL,
                data TEXT NOT NULL
            );
            INSERT INTO session (id, directory, title, time_updated)
            VALUES ('ses_1', '/tmp/project', 'A session', 500);",
        )
        .unwrap();
        conn
    }

    fn timed_v1_payload(id: &str, output: i64) -> String {
        format!(
            r#"{{
                "id": "{id}",
                "role": "assistant",
                "sessionID": "ses_1",
                "modelID": "claude-sonnet-4",
                "providerID": "anthropic",
                "cost": 0.5,
                "tokens": {{
                    "input": 10,
                    "output": {output},
                    "reasoning": 0,
                    "cache": {{ "read": 0, "write": 0 }}
                }},
                "time": {{ "created": 1783882279705, "completed": 1783882279943 }}
            }}"#
        )
    }

    fn timed_v1_payload_without_id(output: i64) -> String {
        format!(
            r#"{{
                "role": "assistant",
                "sessionID": "ses_1",
                "modelID": "claude-sonnet-4",
                "providerID": "anthropic",
                "cost": 0.5,
                "tokens": {{
                    "input": 10,
                    "output": {output},
                    "reasoning": 0,
                    "cache": {{ "read": 0, "write": 0 }}
                }},
                "time": {{ "created": 1783882279705, "completed": 1783882279943 }}
            }}"#
        )
    }

    fn insert_timed_v1_message(conn: &Connection, id: &str, created: i64, output: i64) {
        conn.execute(
            "INSERT INTO message (id, session_id, time_created, time_updated, data)
             VALUES (?1, 'ses_1', ?2, ?2, ?3)",
            rusqlite::params![id, created, timed_v1_payload(id, output)],
        )
        .unwrap();
    }

    /// Rewrite a row's payload and stamp it as updated at `updated`, which is
    /// what OpenCode does to a message long after inserting it.
    fn touch_timed_v1_message(conn: &Connection, id: &str, updated: i64, output: i64) {
        let changed = conn
            .execute(
                "UPDATE message SET data = ?2, time_updated = ?3 WHERE id = ?1",
                rusqlite::params![id, timed_v1_payload(id, output), updated],
            )
            .unwrap();
        assert_eq!(changed, 1, "fixture row {id} should exist");
    }

    fn by_dedup_key(messages: &[UnifiedMessage]) -> Vec<UnifiedMessage> {
        let mut sorted = messages.to_vec();
        sorted.sort_by(|left, right| left.dedup_key.cmp(&right.dedup_key));
        sorted
    }

    fn output_tokens(messages: &[UnifiedMessage], dedup_key: &str) -> i64 {
        messages
            .iter()
            .find(|message| message.dedup_key.as_deref() == Some(dedup_key))
            .unwrap_or_else(|| panic!("{dedup_key} should be present"))
            .tokens
            .output
    }

    /// SplitMix64 keeps this stress test deterministic without adding a
    /// production dependency just to choose fixture mutations.
    fn next_mutation_word(state: &mut u64) -> u64 {
        *state = state.wrapping_add(0x9e37_79b9_7f4a_7c15);
        let mut word = *state;
        word = (word ^ (word >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9);
        word = (word ^ (word >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb);
        word ^ (word >> 31)
    }

    fn mutation_index(state: &mut u64, len: usize) -> usize {
        (next_mutation_word(state) % len as u64) as usize
    }

    struct RandomizedFixtureRow {
        row_id: String,
        dedup_key: String,
        qualified: bool,
    }

    #[test]
    #[serial_test::serial]
    fn test_randomized_incremental_mutations_match_a_full_parse() {
        const SEEDS: usize = 48;
        const MUTATIONS_PER_SEED: usize = 4;
        const MUTATION_NAMES: [&str; 5] = ["insert", "delete", "rewrite", "re-key", "disqualify"];

        let mut mutation_counts = [0_usize; MUTATION_NAMES.len()];
        let mut resumed = 0_usize;
        let mut fell_back = 0_usize;

        for seed in 0..SEEDS {
            let dir = tempfile::tempdir().unwrap();
            let db_path = dir.path().join("opencode.db");
            let conn = create_timed_v1_db(&db_path);
            let mut rows = Vec::new();
            for row_index in 0..6 {
                let row_id = format!("row_{row_index}");
                let dedup_key = format!("msg_{row_index}");
                conn.execute(
                    "INSERT INTO message (id, session_id, time_created, time_updated, data)
                     VALUES (?1, 'ses_1', ?2, ?2, ?3)",
                    rusqlite::params![
                        row_id,
                        1_000 + row_index,
                        timed_v1_payload(&dedup_key, 10 + row_index)
                    ],
                )
                .unwrap();
                rows.push(RandomizedFixtureRow {
                    row_id,
                    dedup_key,
                    qualified: true,
                });
            }
            drop(conn);

            let baseline = scan_opencode_sqlite(&db_path);
            let state = baseline
                .incremental
                .clone()
                .expect("the timed fixture must produce an incremental mark");
            let mut random = (seed as u64 + 1).wrapping_mul(0xd134_2543_de82_ef95);
            let conn = Connection::open(&db_path).unwrap();

            for step in 0..MUTATIONS_PER_SEED {
                let mutation = mutation_index(&mut random, MUTATION_NAMES.len());
                mutation_counts[mutation] += 1;
                let updated = 10_000 + step as i64;

                match mutation {
                    0 => {
                        let row_id = format!("inserted_row_{seed}_{step}");
                        let dedup_key = format!("inserted_msg_{seed}_{step}");
                        let output = (next_mutation_word(&mut random) % 900 + 1) as i64;
                        conn.execute(
                            "INSERT INTO message (id, session_id, time_created, time_updated, data)
                             VALUES (?1, 'ses_1', ?2, ?2, ?3)",
                            rusqlite::params![
                                row_id,
                                updated,
                                timed_v1_payload(&dedup_key, output)
                            ],
                        )
                        .unwrap();
                        rows.push(RandomizedFixtureRow {
                            row_id,
                            dedup_key,
                            qualified: true,
                        });
                    }
                    1 => {
                        let row = rows.remove(mutation_index(&mut random, rows.len()));
                        let changed = conn
                            .execute(
                                "DELETE FROM message WHERE id = ?1",
                                rusqlite::params![row.row_id],
                            )
                            .unwrap();
                        assert_eq!(changed, 1, "seed {seed}: delete target should exist");
                    }
                    2 => {
                        let row_index = mutation_index(&mut random, rows.len());
                        let output = (next_mutation_word(&mut random) % 900 + 1) as i64;
                        let row = &mut rows[row_index];
                        let changed = conn
                            .execute(
                                "UPDATE message SET data = ?2, time_updated = ?3 WHERE id = ?1",
                                rusqlite::params![
                                    row.row_id,
                                    timed_v1_payload(&row.dedup_key, output),
                                    updated
                                ],
                            )
                            .unwrap();
                        assert_eq!(changed, 1, "seed {seed}: rewrite target should exist");
                        row.qualified = true;
                    }
                    3 => {
                        let row_index = mutation_index(&mut random, rows.len());
                        let collision_keys: Vec<String> = rows
                            .iter()
                            .enumerate()
                            .filter(|(index, row)| *index != row_index && row.qualified)
                            .map(|(_, row)| row.dedup_key.clone())
                            .collect();
                        let use_existing_key =
                            next_mutation_word(&mut random) & 3 == 0 && !collision_keys.is_empty();
                        let dedup_key = if use_existing_key {
                            collision_keys[mutation_index(&mut random, collision_keys.len())]
                                .clone()
                        } else {
                            format!("rekeyed_msg_{seed}_{step}")
                        };
                        let output = (next_mutation_word(&mut random) % 900 + 1) as i64;
                        let row = &mut rows[row_index];
                        let changed = conn
                            .execute(
                                "UPDATE message SET data = ?2, time_updated = ?3 WHERE id = ?1",
                                rusqlite::params![
                                    row.row_id,
                                    timed_v1_payload(&dedup_key, output),
                                    updated
                                ],
                            )
                            .unwrap();
                        assert_eq!(changed, 1, "seed {seed}: re-key target should exist");
                        row.dedup_key = dedup_key;
                        row.qualified = true;
                    }
                    4 => {
                        let row_index = mutation_index(&mut random, rows.len());
                        let row = &mut rows[row_index];
                        let payload = format!(
                            r#"{{"id":"{}","sessionID":"ses_1","role":"user"}}"#,
                            row.dedup_key
                        );
                        disqualify_timed_v1_message(&conn, &row.row_id, updated, &payload);
                        row.qualified = false;
                    }
                    _ => unreachable!(),
                }
            }
            drop(conn);

            let effective_warm = match rescan_opencode_sqlite(&db_path, &state, baseline.messages) {
                Some(warm) => {
                    resumed += 1;
                    warm.messages
                }
                None => {
                    fell_back += 1;
                    scan_opencode_sqlite(&db_path).messages
                }
            };
            let full = scan_opencode_sqlite(&db_path);
            assert_eq!(
                by_dedup_key(&effective_warm),
                by_dedup_key(&full.messages),
                "seed {seed}: a warm rescan or its conservative fallback must match a full parse"
            );
        }

        for (name, count) in MUTATION_NAMES.into_iter().zip(mutation_counts) {
            assert!(count > 0, "the deterministic corpus must exercise {name}");
        }
        assert!(
            resumed >= SEEDS * 3 / 4,
            "the optimization must remain useful across mixed mutations: resumed {resumed}/{SEEDS}"
        );
        assert!(
            fell_back > 0,
            "the corpus must also exercise a conservative full-scan fallback"
        );
    }

    #[test]
    fn test_incremental_rescan_matches_a_full_parse_of_the_same_state() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        insert_timed_v1_message(&conn, "msg_b", 2_000, 22);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        assert_eq!(cold.messages.len(), 2);
        let state = cold
            .incremental
            .clone()
            .expect("a table with the time columns is resumable");

        let conn = Connection::open(&db_path).unwrap();
        touch_timed_v1_message(&conn, "msg_a", 9_000, 111);
        conn.execute(
            "INSERT INTO message (id, session_id, time_created, time_updated, data)
             VALUES ('msg_c', 'ses_1', 500, 9500, ?1)",
            rusqlite::params![timed_v1_payload("msg_c", 33)],
        )
        .unwrap();
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("an insert-only delta stays incremental");
        let full = scan_opencode_sqlite(&db_path);

        assert_eq!(
            by_dedup_key(&warm.messages),
            by_dedup_key(&full.messages),
            "a warm incremental scan and a cold full parse must agree"
        );
        assert_eq!(warm.messages.len(), 3);
        assert_eq!(output_tokens(&warm.messages, "msg_a"), 111);
        assert_eq!(output_tokens(&warm.messages, "msg_c"), 33);
    }

    #[test]
    fn same_millisecond_rewrite_matches_full_scan() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&path);
        insert_timed_v1_message(&conn, "msg_a", 2_000, 11);
        let cold = scan_opencode_sqlite(&path);
        let state = cold.incremental.clone().unwrap();
        touch_timed_v1_message(&conn, "msg_a", 2_000, 999);
        let warm = rescan_opencode_sqlite(&path, &state, cold.messages)
            .unwrap_or_else(|| scan_opencode_sqlite(&path));
        let full = scan_opencode_sqlite(&path);
        assert_eq!(output_tokens(&full.messages, "msg_a"), 999);
        assert_eq!(output_tokens(&warm.messages, "msg_a"), 999);
    }

    #[test]
    fn test_incremental_rescan_reads_a_row_rewritten_long_after_it_was_inserted() {
        // The row that changes is the *oldest* one and sorts first by id, so a
        // mark keyed on the row id -- the Codex `consumed_offset` analogue --
        // would skip it. On a real database 99.98% of rows are rewritten after
        // insert, so that mark would under-report nearly everything.
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_aaa", 1_000, 11);
        insert_timed_v1_message(&conn, "msg_zzz", 2_000, 22);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();
        assert_eq!(output_tokens(&cold.messages, "msg_aaa"), 11);

        let conn = Connection::open(&db_path).unwrap();
        touch_timed_v1_message(&conn, "msg_aaa", 9_000, 999);
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("an in-place rewrite is not a deletion");

        assert_eq!(
            output_tokens(&warm.messages, "msg_aaa"),
            999,
            "a row rewritten after insert must reach the incremental scan"
        );
        assert_eq!(warm.messages.len(), 2, "a rewrite must not duplicate a row");
        assert_eq!(
            by_dedup_key(&warm.messages),
            by_dedup_key(&scan_opencode_sqlite(&db_path).messages)
        );
    }

    #[test]
    fn test_incremental_rescan_removes_a_deleted_row_by_provenance() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        insert_timed_v1_message(&conn, "msg_b", 2_000, 22);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();

        // OpenCode cascades a session delete onto its messages. The row
        // inventory makes that absence explicit even though no delta query can
        // return the deleted row.
        let conn = Connection::open(&db_path).unwrap();
        conn.execute("DELETE FROM message WHERE id = 'msg_b'", [])
            .unwrap();
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("an ordinary deletion is exact from row provenance");
        let full = scan_opencode_sqlite(&db_path);
        assert_eq!(by_dedup_key(&warm.messages), by_dedup_key(&full.messages));
        assert_eq!(full.messages.len(), 1);
        assert_eq!(full.messages[0].dedup_key.as_deref(), Some("msg_a"));
    }

    #[test]
    fn test_incremental_rescan_handles_a_delete_masked_by_an_insert() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        insert_timed_v1_message(&conn, "msg_b", 2_000, 22);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();

        let conn = Connection::open(&db_path).unwrap();
        conn.execute("DELETE FROM message WHERE id = 'msg_b'", [])
            .unwrap();
        // The replacement's creation time moves backwards, so the old
        // count/high-water inference sees neither an insert nor a deletion.
        // Its own update marker is current, and row provenance identifies both
        // physical changes directly.
        conn.execute(
            "INSERT INTO message (id, session_id, time_created, time_updated, data)
             VALUES ('msg_c', 'ses_1', 500, 9500, ?1)",
            rusqlite::params![timed_v1_payload("msg_c", 33)],
        )
        .unwrap();
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("row identity distinguishes the deletion from the insert");
        let full = scan_opencode_sqlite(&db_path);
        assert_eq!(by_dedup_key(&warm.messages), by_dedup_key(&full.messages));
    }

    /// Rewrite a row so the usage queries stop selecting it, without changing
    /// the row count. `payload` replaces the whole `data` object.
    fn disqualify_timed_v1_message(conn: &Connection, id: &str, updated: i64, payload: &str) {
        let changed = conn
            .execute(
                "UPDATE message SET data = ?2, time_updated = ?3 WHERE id = ?1",
                rusqlite::params![id, payload, updated],
            )
            .unwrap();
        assert_eq!(changed, 1, "fixture row {id} should exist");
    }

    /// A rename touches the session row and nothing else, so the message
    /// high-water does not move and the incremental scan reads no rows. Without
    /// a metadata refresh the cached messages keep the old title forever, and a
    /// cold parse disagrees with the cache indefinitely.
    #[test]
    fn test_incremental_rescan_picks_up_a_session_renamed_without_new_messages() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        insert_timed_v1_message(&conn, "msg_b", 2_000, 22);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();
        assert_eq!(cold.messages[0].session_title.as_deref(), Some("A session"));

        // Rename the session and move its directory. No message row changes.
        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "UPDATE session SET title = 'Renamed session', directory = '/tmp/moved',
                    time_updated = 9000 WHERE id = 'ses_1'",
            [],
        )
        .unwrap();
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("a rename must not cost a full re-parse");
        let full = scan_opencode_sqlite(&db_path);

        assert_eq!(
            by_dedup_key(&warm.messages),
            by_dedup_key(&full.messages),
            "a warm rescan must agree with a cold parse after a rename"
        );
        for message in &warm.messages {
            assert_eq!(message.session_title.as_deref(), Some("Renamed session"));
        }
    }

    /// Both generations scan into one message list and a cached message does
    /// not record which produced it, so a session id present in both metadata
    /// tables cannot be re-stamped without one generation overwriting the
    /// other's title and workspace. A full scan is the correct answer there.
    #[test]
    fn test_incremental_rescan_refuses_a_session_id_shared_by_both_schemas() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        // A half-migrated database: the same session id in the v2 table too.
        conn.execute_batch(
            "CREATE TABLE session_v2 (
                id TEXT PRIMARY KEY,
                directory TEXT NOT NULL,
                title TEXT,
                time_updated INTEGER NOT NULL
            );
            CREATE TABLE session_message (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL,
                type TEXT NOT NULL,
                time_created INTEGER NOT NULL,
                time_updated INTEGER NOT NULL,
                data TEXT NOT NULL
            );
            INSERT INTO session_v2 (id, directory, title, time_updated)
            VALUES ('ses_1', '/tmp/v2', 'V2 title', 500);",
        )
        .unwrap();
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let Some(state) = cold.incremental.clone() else {
            // Refusing to mark at all is also a safe answer here.
            return;
        };

        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "UPDATE session_v2 SET title = 'V2 renamed', time_updated = 9000 WHERE id = 'ses_1'",
            [],
        )
        .unwrap();
        conn.execute(
            "UPDATE session SET title = 'V1 renamed', time_updated = 9000 WHERE id = 'ses_1'",
            [],
        )
        .unwrap();
        drop(conn);

        assert!(
            rescan_opencode_sqlite(&db_path, &state, cold.messages).is_none(),
            "a session id in both metadata tables must force a full re-parse"
        );
    }

    /// A row whose embedded `$.id` changes has a new dedup key. Row provenance
    /// links both identities to the same physical source, so the old message is
    /// removed and the new one replaces it rather than being appended beside it.
    ///
    /// Two shapes, because the merge's content digest only catches one of them:
    /// an identity-only rewrite has the same digest as the cached message, but
    /// one that also changes usage does not.
    #[test]
    fn test_incremental_rescan_replaces_a_row_whose_embedded_id_changes() {
        for (label, output) in [("identity only", 11), ("identity and usage", 99)] {
            let dir = tempfile::tempdir().unwrap();
            let db_path = dir.path().join("opencode.db");
            let conn = create_timed_v1_db(&db_path);
            insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
            drop(conn);

            let cold = scan_opencode_sqlite(&db_path);
            let state = cold.incremental.clone().unwrap();
            assert_eq!(cold.messages.len(), 1);
            assert_eq!(cold.messages[0].dedup_key.as_deref(), Some("msg_a"));

            // The row keeps its SQLite id but starts carrying its own id, which
            // is the key the parser prefers.
            let conn = Connection::open(&db_path).unwrap();
            conn.execute(
                "UPDATE message SET data = ?1, time_updated = 9000 WHERE id = 'msg_a'",
                rusqlite::params![timed_v1_payload("embedded_a", output)],
            )
            .unwrap();
            drop(conn);

            let full = scan_opencode_sqlite(&db_path);
            assert_eq!(full.messages.len(), 1, "{label}: a cold parse sees one row");

            let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
                .unwrap_or_else(|| panic!("{label}: a key change should stay incremental"));
            assert_eq!(
                by_dedup_key(&warm.messages),
                by_dedup_key(&full.messages),
                "{label}: a warm scan must replace the old physical row"
            );
        }
    }

    #[test]
    fn test_incremental_rescan_replaces_an_embedded_id_with_the_row_id() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        conn.execute(
            "INSERT INTO message (id, session_id, time_created, time_updated, data)
             VALUES ('row_a', 'ses_1', 1000, 1000, ?1)",
            rusqlite::params![timed_v1_payload("embedded_a", 11)],
        )
        .unwrap();
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();
        assert_eq!(cold.messages[0].dedup_key.as_deref(), Some("embedded_a"));

        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "UPDATE message SET data = ?1, time_updated = 9000 WHERE id = 'row_a'",
            rusqlite::params![timed_v1_payload_without_id(22)],
        )
        .unwrap();
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("losing an embedded id should stay incremental");
        let full = scan_opencode_sqlite(&db_path);
        assert_eq!(by_dedup_key(&warm.messages), by_dedup_key(&full.messages));
        assert_eq!(warm.messages[0].dedup_key.as_deref(), Some("row_a"));
        assert_eq!(warm.messages[0].tokens.output, 22);
    }

    #[test]
    fn test_incremental_rescan_removes_a_row_that_lost_its_tokens() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        insert_timed_v1_message(&conn, "msg_b", 2_000, 22);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();
        assert_eq!(cold.messages.len(), 2);

        let conn = Connection::open(&db_path).unwrap();
        disqualify_timed_v1_message(
            &conn,
            "msg_b",
            9_000,
            r#"{"id": "msg_b", "sessionID": "ses_1", "role": "assistant"}"#,
        );
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("the parser's rejected outcome removes the old message");
        let full = scan_opencode_sqlite(&db_path);
        assert_eq!(by_dedup_key(&warm.messages), by_dedup_key(&full.messages));
        assert_eq!(full.messages.len(), 1);
        assert_eq!(full.messages[0].dedup_key.as_deref(), Some("msg_a"));
    }

    #[test]
    fn test_incremental_rescan_removes_a_row_that_stopped_being_an_assistant_turn() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        insert_timed_v1_message(&conn, "msg_b", 2_000, 22);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();

        let conn = Connection::open(&db_path).unwrap();
        disqualify_timed_v1_message(
            &conn,
            "msg_b",
            9_000,
            r#"{"id": "msg_b", "sessionID": "ses_1", "role": "user",
                "tokens": {"input": 1, "output": 2, "cache": {"read": 0, "write": 0}}}"#,
        );
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("the changed-row parser owns qualification semantics");
        let full = scan_opencode_sqlite(&db_path);
        assert_eq!(by_dedup_key(&warm.messages), by_dedup_key(&full.messages));
    }

    #[test]
    fn test_incremental_rescan_uses_parser_qualification_for_changed_rows() {
        // SQL still sees an assistant row with a tokens object. The parser
        // rejects it because the model identity disappeared. A duplicated SQL
        // qualification predicate cannot express that rule without drifting.
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();

        let conn = Connection::open(&db_path).unwrap();
        disqualify_timed_v1_message(
            &conn,
            "msg_a",
            9_000,
            r#"{"id":"msg_a","sessionID":"ses_1","role":"assistant",
                "tokens":{"input":1,"output":2,"cache":{"read":0,"write":0}},
                "time":{"created":1783882279705}}"#,
        );
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("a parser rejection should be an explicit row outcome");
        assert!(warm.messages.is_empty());
        assert!(scan_opencode_sqlite(&db_path).messages.is_empty());
    }

    #[test]
    fn test_incremental_rescan_keeps_going_when_a_never_counted_row_changes() {
        // The guard above must not fire on ordinary traffic. User turns are
        // rewritten constantly -- 92,002 of the 92,028 non-assistant rows on a
        // real 14 GB database carry `time_updated > time_created` -- and none
        // of them ever backed a cached message, so none of them is evidence
        // that the cache went stale.
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        conn.execute(
            "INSERT INTO message (id, session_id, time_created, time_updated, data)
             VALUES ('msg_user', 'ses_1', 2_000, 2_000, ?1)",
            rusqlite::params![r#"{"id": "msg_user", "sessionID": "ses_1", "role": "user"}"#],
        )
        .unwrap();
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();
        assert_eq!(cold.messages.len(), 1, "only the assistant turn is usage");

        let conn = Connection::open(&db_path).unwrap();
        disqualify_timed_v1_message(
            &conn,
            "msg_user",
            9_000,
            r#"{"id": "msg_user", "sessionID": "ses_1", "role": "user", "text": "edited"}"#,
        );
        drop(conn);

        let warm = rescan_opencode_sqlite(&db_path, &state, cold.messages)
            .expect("a rewritten user turn must not cost a full re-parse");
        assert_eq!(
            by_dedup_key(&warm.messages),
            by_dedup_key(&scan_opencode_sqlite(&db_path).messages)
        );
    }

    #[test]
    fn test_a_table_without_the_time_columns_records_no_mark() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_opencode_sqlite_db(&db_path);
        conn.execute(
            "INSERT INTO message (id, session_id, data) VALUES (?1, 'ses_1', ?2)",
            rusqlite::params!["msg_a", timed_v1_payload("msg_a", 11)],
        )
        .unwrap();
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        assert_eq!(cold.messages.len(), 1);
        assert!(
            cold.incremental.is_none(),
            "rows that cannot be rescanned incrementally must not be marked as if they could"
        );
    }

    #[test]
    fn test_incremental_rescan_refuses_a_row_that_took_part_in_a_merge() {
        // Forked history puts one message id on two rows, and the fingerprint
        // dedup collapses them into a single entry. That entry is the only
        // trace the second row left, so re-reading either side cannot
        // reconstruct what the other contributed -- the scan has to re-parse.
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        for (row_id, created) in [("msg_fork_a", 1_000_i64), ("msg_fork_b", 1_500)] {
            conn.execute(
                "INSERT INTO message (id, session_id, time_created, time_updated, data)
                 VALUES (?1, 'ses_1', ?2, ?2, ?3)",
                rusqlite::params![row_id, created, timed_v1_payload("msg_shared", 11)],
            )
            .unwrap();
        }
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        assert_eq!(
            cold.messages.len(),
            1,
            "forked copies collapse to one entry"
        );
        let state = cold.incremental.clone().unwrap();
        assert!(
            state.merged_dedup_keys.contains(&"msg_shared".to_string()),
            "the collapse has to be recorded for the next scan to notice it"
        );

        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "UPDATE message SET data = ?2, time_updated = ?3 WHERE id = ?1",
            rusqlite::params!["msg_fork_b", timed_v1_payload("msg_shared", 77), 9_000],
        )
        .unwrap();
        drop(conn);

        assert!(
            rescan_opencode_sqlite(&db_path, &state, cold.messages).is_none(),
            "a rewritten fork copy must re-parse rather than guess at the collapse"
        );
        assert_eq!(scan_opencode_sqlite(&db_path).messages.len(), 2);
    }

    #[test]
    fn test_incremental_rescan_refuses_a_new_row_that_would_collapse() {
        // A fork created after the mark copies completed turns verbatim. A full
        // scan collapses each copy into the original; appending them would
        // count every copied turn twice.
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = create_timed_v1_db(&db_path);
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        assert_eq!(cold.messages.len(), 1);
        let state = cold.incremental.clone().unwrap();
        assert!(state.merged_dedup_keys.is_empty());

        let conn = Connection::open(&db_path).unwrap();
        conn.execute(
            "INSERT INTO message (id, session_id, time_created, time_updated, data)
             VALUES ('msg_a_copy', 'ses_1', 9_500, 9_500, ?1)",
            rusqlite::params![timed_v1_payload("msg_a", 11)],
        )
        .unwrap();
        drop(conn);

        assert!(
            rescan_opencode_sqlite(&db_path, &state, cold.messages).is_none(),
            "a copied turn must re-parse rather than be appended beside its original"
        );
        assert_eq!(
            scan_opencode_sqlite(&db_path).messages.len(),
            1,
            "the full parse still collapses the copy"
        );
    }

    #[test]
    fn test_incremental_rescan_refuses_a_mark_from_a_different_schema_variant() {
        let dir = tempfile::tempdir().unwrap();
        let db_path = dir.path().join("opencode.db");
        let conn = Connection::open(&db_path).unwrap();
        conn.execute_batch(
            "CREATE TABLE message (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL,
                time_created INTEGER NOT NULL,
                time_updated INTEGER NOT NULL,
                data TEXT NOT NULL
            );",
        )
        .unwrap();
        insert_timed_v1_message(&conn, "msg_a", 1_000, 11);
        drop(conn);

        let cold = scan_opencode_sqlite(&db_path);
        let state = cold.incremental.clone().unwrap();
        assert_eq!(cold.messages[0].workspace_key, None);

        // The session metadata table appears, so a full scan would now pick the
        // joining variant and resolve a workspace the cached rows never had.
        let conn = Connection::open(&db_path).unwrap();
        conn.execute_batch(
            "CREATE TABLE session (
                id TEXT PRIMARY KEY,
                directory TEXT NOT NULL,
                title TEXT
            );
            INSERT INTO session (id, directory, title)
            VALUES ('ses_1', '/tmp/project', 'A session');",
        )
        .unwrap();
        drop(conn);

        assert!(
            rescan_opencode_sqlite(&db_path, &state, cold.messages).is_none(),
            "a variant change must invalidate the mark"
        );
        assert!(scan_opencode_sqlite(&db_path).messages[0]
            .workspace_key
            .is_some());
    }
}

#[cfg(test)]
mod integration_tests {
    use super::*;
    use std::path::PathBuf;

    #[test]
    #[ignore] // Run manually with: cargo test integration -- --ignored
    fn test_parse_real_sqlite_db() {
        let home = std::env::var("HOME").unwrap();
        let db_path = PathBuf::from(format!("{}/.local/share/opencode/opencode.db", home));

        if !db_path.exists() {
            println!("Skipping: OpenCode database not found at {:?}", db_path);
            return;
        }

        let messages = parse_opencode_sqlite(&db_path);
        println!("Parsed {} messages from SQLite", messages.len());

        if !messages.is_empty() {
            let first = &messages[0];
            println!(
                "First message: model={}, provider={}, tokens={:?}",
                first.model_id, first.provider_id, first.tokens
            );
        }

        assert!(
            !messages.is_empty(),
            "Expected to parse some messages from SQLite"
        );
    }
}