nexus-chat-core 0.1.9

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

use crate::provider::ModelPricing;

/// Stored per-model preferences.
#[derive(Debug, Clone)]
pub struct ModelPref {
    pub id: String,
    pub favorite: bool,
    pub last_used: Option<String>,
    pub reasoning: Option<String>,
}

/// A space: an isolated collection of sessions with its own memory/instructions
/// (stored as files on disk, see `space.rs`). `name` doubles as the directory name.
#[derive(Debug, Clone)]
pub struct Space {
    pub id: String,
    pub name: String,
    pub created_at: String,
}

/// One chat session (conversation) within a space.
#[derive(Debug, Clone)]
pub struct Session {
    pub id: String,
    pub title: String,
    pub model: String,
    /// Short human-readable id (kebab slug), generated by the model. `None` until
    /// generated — display falls back to a prefix of the uuid.
    pub slug: Option<String>,
    pub created_at: String,
    /// Caveman-compressed digest of the session's earlier messages, if it's
    /// ever been auto-compacted. `None` until the first compaction.
    pub compact_summary: Option<String>,
    /// How many of the session's raw messages (in `created_at` order) are
    /// folded into `compact_summary`. Messages after this point are still
    /// sent verbatim; 0 means nothing has been compacted yet.
    pub compact_through: i64,
    /// `/web` answer mode: force search-first, inline-cited replies.
    pub web_mode: bool,
    /// `/swarm` mode: turns replace a single reply with a multi-persona
    /// roundtable (see `swarm_personas`).
    pub swarm_mode: bool,
    /// `"chat"` or `"research"` — determines what's available.
    pub kind: String,
    /// If this is a research session spawned from an existing chat, the
    /// original session's id.
    pub research_parent_id: Option<String>,
}

/// One row of a session's `/swarm` roster: a model + a personality blurb.
#[derive(Debug, Clone)]
pub struct Persona {
    pub name: String,
    pub model: String,
    pub blurb: String,
}

/// A standing research watch: runs a topic's research on an interval.
#[derive(Debug, Clone)]
pub struct Watch {
    pub id: String,
    pub space_id: String,
    pub topic: String,
    pub interval_hours: i64,
    pub session_id: String,
    pub last_run_at: Option<String>,
}

/// A file imported into a space's fileset. `status` is "ok", "no text
/// (scanned?)", "unsupported", or "error: …"; extraction text lives in the
/// `cache.file_chunks` FTS table. `status`/`mtime` are this device's derived
/// index state from `cache.file_index_state` — a cold cache reports
/// "not indexed"/0 until the next rescan re-derives them.
#[derive(Debug, Clone)]
pub struct FileRow {
    pub id: String,
    pub name: String,
    pub hash: String,
    pub size: i64,
    pub status: String,
    /// Unix mtime of the disk file when last indexed; lets rescans skip
    /// reading/hashing files whose (size, mtime) haven't changed.
    pub mtime: i64,
}

/// One message in a session. `model`/`reasoning`/`tokens`/`secs`/`cost` are
/// populated for assistant replies (None for user/system messages).
#[derive(Debug, Clone)]
pub struct Message {
    pub role: String,
    pub content: String,
    pub model: Option<String>,
    pub reasoning: Option<String>,
    pub tokens: Option<i64>,
    pub secs: Option<f64>,
    /// USD cost of the request that produced this reply: provider-reported
    /// when available, otherwise a cache-aware catalog estimate. `None` when
    /// neither source is available.
    pub cost: Option<f64>,
    /// Past-tense flavour phrase for the completion line, e.g. "Vibed".
    pub phrase: Option<String>,
    /// Which `/swarm` persona produced this reply, if any (`None` for
    /// ordinary messages and for a swarm turn's final synthesis reply).
    pub persona: Option<String>,
    /// RFC3339 timestamp of the row (None for in-memory-only messages that
    /// were never persisted, e.g. incognito streams).
    pub created_at: Option<String>,
}

/// Name of the always-present, undeletable space that sessions default into.
pub const DEFAULT_SPACE: &str = "default";

/// Schema version of the durable db, tracked via `PRAGMA user_version`.
/// Legacy dbs (never versioned) are 0 and get one-time column adds plus the
/// device-local table move into `cache.db`; fresh dbs are created complete
/// and stamped 1 immediately.
const SCHEMA_VERSION: i64 = 1;

/// Columns added since the v1 schema. Fresh dbs declare them inline; legacy
/// dbs get them via `user_version`-gated `ALTER TABLE` adds guarded by
/// `PRAGMA table_info` (the only tolerated "duplicate" is an existing
/// column — real errors propagate). `files.mtime` is deliberately absent:
/// it moved to `cache.file_index_state` and stays a dead column on legacy
/// dbs (see the roadmap, Phase 1).
const LEGACY_COLUMN_ADDS: &[(&str, &str, &str)] = &[
    (
        "messages",
        "model",
        "ALTER TABLE messages ADD COLUMN model TEXT",
    ),
    (
        "messages",
        "reasoning",
        "ALTER TABLE messages ADD COLUMN reasoning TEXT",
    ),
    (
        "messages",
        "tokens",
        "ALTER TABLE messages ADD COLUMN tokens INTEGER",
    ),
    (
        "messages",
        "secs",
        "ALTER TABLE messages ADD COLUMN secs REAL",
    ),
    (
        "messages",
        "cost",
        "ALTER TABLE messages ADD COLUMN cost REAL",
    ),
    (
        "messages",
        "phrase",
        "ALTER TABLE messages ADD COLUMN phrase TEXT",
    ),
    (
        "messages",
        "persona",
        "ALTER TABLE messages ADD COLUMN persona TEXT",
    ),
    (
        "model_prefs",
        "reasoning",
        "ALTER TABLE model_prefs ADD COLUMN reasoning TEXT",
    ),
    (
        "model_prefs",
        "updated_at",
        "ALTER TABLE model_prefs ADD COLUMN updated_at TEXT",
    ),
    (
        "sessions",
        "slug",
        "ALTER TABLE sessions ADD COLUMN slug TEXT",
    ),
    (
        "sessions",
        "space_id",
        "ALTER TABLE sessions ADD COLUMN space_id TEXT",
    ),
    (
        "sessions",
        "compact_summary",
        "ALTER TABLE sessions ADD COLUMN compact_summary TEXT",
    ),
    (
        "sessions",
        "compact_through",
        "ALTER TABLE sessions ADD COLUMN compact_through INTEGER NOT NULL DEFAULT 0",
    ),
    (
        "sessions",
        "web_mode",
        "ALTER TABLE sessions ADD COLUMN web_mode INTEGER NOT NULL DEFAULT 0",
    ),
    (
        "sessions",
        "swarm_mode",
        "ALTER TABLE sessions ADD COLUMN swarm_mode INTEGER NOT NULL DEFAULT 0",
    ),
    (
        "sessions",
        "kind",
        "ALTER TABLE sessions ADD COLUMN kind TEXT NOT NULL DEFAULT 'chat'",
    ),
    (
        "sessions",
        "research_parent_id",
        "ALTER TABLE sessions ADD COLUMN research_parent_id TEXT",
    ),
    (
        "session_sources",
        "flag",
        "ALTER TABLE session_sources ADD COLUMN flag TEXT",
    ),
    (
        "session_sources",
        "updated_at",
        "ALTER TABLE session_sources ADD COLUMN updated_at TEXT",
    ),
    (
        "usage_log",
        "cost_is_provider",
        "ALTER TABLE usage_log ADD COLUMN cost_is_provider INTEGER",
    ),
    (
        "usage_log",
        "sync_id",
        "ALTER TABLE usage_log ADD COLUMN sync_id TEXT",
    ),
    (
        "usage_log",
        "updated_at",
        "ALTER TABLE usage_log ADD COLUMN updated_at TEXT",
    ),
    (
        "app_settings",
        "scope",
        "ALTER TABLE app_settings ADD COLUMN scope TEXT NOT NULL DEFAULT 'sync'",
    ),
    (
        "app_settings",
        "updated_at",
        "ALTER TABLE app_settings ADD COLUMN updated_at TEXT",
    ),
    (
        "spaces",
        "updated_at",
        "ALTER TABLE spaces ADD COLUMN updated_at TEXT",
    ),
    (
        "files",
        "updated_at",
        "ALTER TABLE files ADD COLUMN updated_at TEXT",
    ),
    (
        "citations",
        "sync_id",
        "ALTER TABLE citations ADD COLUMN sync_id TEXT",
    ),
    (
        "watches",
        "updated_at",
        "ALTER TABLE watches ADD COLUMN updated_at TEXT",
    ),
];

/// The device-local cache db living next to the durable db: `cache.db` in
/// the same directory as `nexus.db`. Disposable — derived index state
/// (chunks, embeddings, fetched pages, price catalog) that rebuilds on
/// demand, so backups exclude it and restores drop it.
pub fn cache_path_for(db_path: &std::path::Path) -> std::path::PathBuf {
    db_path
        .parent()
        .filter(|p| !p.as_os_str().is_empty())
        .map_or_else(
            || std::path::PathBuf::from("cache.db"),
            |p| p.join("cache.db"),
        )
}

/// Open a connection to a durable db with its sibling `cache.db` attached
/// as schema `cache`. Cross-db queries (file chunks, fetched pages, price
/// catalog) run on one connection through the `cache.` prefix; cache-only
/// queries use unqualified names so they also work on a standalone
/// cache-only connection (the schema fallback resolves them). Tool
/// connections use this directly; `Db::open` wraps it in migrations.
pub fn open_attached(db_path: &std::path::Path) -> Result<Connection> {
    let conn =
        Connection::open(db_path).with_context(|| format!("opening db {}", db_path.display()))?;
    let cache = cache_path_for(db_path);
    let escaped = cache.display().to_string().replace('\'', "''");
    conn.execute_batch(&format!("ATTACH DATABASE '{escaped}' AS cache"))
        .with_context(|| format!("attaching cache db {}", cache.display()))?;
    migrate_cache(&conn, "cache")?;
    Ok(conn)
}

/// Create the device-local cache schema on `conn` under the given schema
/// name — `cache` on a main-db connection from `open_attached`, or `main`
/// on a standalone cache-only connection.
pub fn migrate_cache(conn: &Connection, schema: &str) -> Result<()> {
    conn.execute_batch(&format!(
        "CREATE TABLE IF NOT EXISTS {schema}.web_cache (
            url_norm   TEXT PRIMARY KEY,
            url        TEXT NOT NULL,
            title      TEXT,
            text       TEXT NOT NULL,
            fetched_at TEXT NOT NULL
        );
        CREATE VIRTUAL TABLE IF NOT EXISTS {schema}.file_chunks USING fts5(
            file_id UNINDEXED,
            seq UNINDEXED,
            location UNINDEXED,
            text
        );
        CREATE TABLE IF NOT EXISTS {schema}.chunk_embeddings (
            file_id TEXT NOT NULL,
            seq INTEGER NOT NULL,
            vec BLOB NOT NULL,
            PRIMARY KEY (file_id, seq)
        );
        CREATE TABLE IF NOT EXISTS {schema}.model_prices (
            model_id TEXT PRIMARY KEY,
            backend TEXT NOT NULL,
            prompt_price REAL NOT NULL,
            completion_price REAL NOT NULL,
            cache_read_price REAL,
            cache_write_price REAL,
            updated_at TEXT NOT NULL
        );
        CREATE TABLE IF NOT EXISTS {schema}.file_index_state (
            file_id TEXT PRIMARY KEY,
            mtime INTEGER NOT NULL DEFAULT 0,
            status TEXT NOT NULL DEFAULT '',
            updated_at TEXT NOT NULL
        );",
    ))?;
    Ok(())
}

/// Whether `table` in the **main** schema has `column` — the guard for
/// legacy column adds. Explicitly main-scoped: `PRAGMA table_info` would
/// otherwise resolve names across the attached `cache` schema too.
fn has_column(conn: &Connection, table: &str, column: &str) -> Result<bool> {
    let mut stmt = conn.prepare(&format!("PRAGMA main.table_info({table})"))?;
    let rows = stmt.query_map([], |r| r.get::<_, String>(1))?;
    for row in rows {
        if row? == column {
            return Ok(true);
        }
    }
    Ok(false)
}

// ponytail: rusqlite is synchronous and called inline on the UI task. Writes are
// tiny single-user local inserts, so no spawn_blocking. Move to a blocking pool
// only if the db ever lives on slow/remote storage.
pub struct Db {
    conn: Connection,
}

impl Db {
    pub fn open(path: &std::path::Path) -> Result<Self> {
        let conn = open_attached(path).with_context(|| format!("opening db {}", path.display()))?;
        let mut db = Self { conn };
        db.migrate()?;
        Ok(db)
    }

    #[cfg(any(test, feature = "test-helpers"))]
    pub fn open_in_memory() -> Result<Self> {
        let conn = Connection::open_in_memory()?;
        conn.execute_batch("ATTACH DATABASE ':memory:' AS cache")?;
        migrate_cache(&conn, "cache")?;
        let mut db = Db { conn };
        db.migrate()?;
        Ok(db)
    }

    #[cfg(test)]
    pub fn conn_for_test(&self) -> &Connection {
        &self.conn
    }

    /// Bump a versioned row's `updated_at` — the LWW version the sync
    /// engine compares (RFC3339, so lexical order = time order). Every
    /// mutation path on a versioned table must go through here or inline
    /// the same bump.
    fn touch(&self, table: &str, id: &str) -> Result<()> {
        self.conn.execute(
            &format!("UPDATE {table} SET updated_at = ?1 WHERE id = ?2"),
            (Utc::now().to_rfc3339(), id),
        )?;
        Ok(())
    }

    /// Record that a syncable row was physically deleted. Application
    /// tables stay clean (no soft-delete columns); the merge engine
    /// propagates deletes from `sync_tombstones`. `row_id` is the row's
    /// sync identity — its uuid id, or the `sync_id` for AUTOINCREMENT
    /// tables.
    fn tombstone(&self, table: &str, row_id: &str) -> Result<()> {
        self.conn.execute(
            "INSERT INTO sync_tombstones (table_name, row_id, deleted_at)
             VALUES (?1, ?2, ?3)",
            (table, row_id, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    // Long by design (schema migrations).
    #[allow(clippy::too_many_lines)]
    fn migrate(&mut self) -> Result<()> {
        self.conn.execute_batch(
            "CREATE TABLE IF NOT EXISTS sessions (
                id TEXT PRIMARY KEY,
                title TEXT NOT NULL,
                model TEXT NOT NULL,
                created_at TEXT NOT NULL,
                updated_at TEXT NOT NULL
            );
            CREATE TABLE IF NOT EXISTS messages (
                id TEXT PRIMARY KEY,
                session_id TEXT NOT NULL REFERENCES sessions(id),
                role TEXT NOT NULL,
                content TEXT NOT NULL,
                created_at TEXT NOT NULL
            );
            CREATE INDEX IF NOT EXISTS idx_messages_session
                ON messages(session_id, created_at);
            CREATE TABLE IF NOT EXISTS model_prefs (
                id TEXT PRIMARY KEY,
                favorite INTEGER NOT NULL DEFAULT 0,
                last_used TEXT,
                reasoning TEXT,
                updated_at TEXT
            );
            CREATE TABLE IF NOT EXISTS app_settings (
                key TEXT PRIMARY KEY,
                value TEXT NOT NULL,
                scope TEXT NOT NULL DEFAULT 'sync',
                updated_at TEXT
            );
            CREATE TABLE IF NOT EXISTS spaces (
                id TEXT PRIMARY KEY,
                name TEXT NOT NULL UNIQUE,
                created_at TEXT NOT NULL,
                updated_at TEXT
            );
            CREATE TABLE IF NOT EXISTS files (
                id TEXT PRIMARY KEY,
                space_id TEXT NOT NULL,
                name TEXT NOT NULL,
                hash TEXT NOT NULL,
                size INTEGER NOT NULL,
                created_at TEXT NOT NULL,
                updated_at TEXT NOT NULL,
                UNIQUE(space_id, name)
            );
            CREATE TABLE IF NOT EXISTS citations (
                id          INTEGER PRIMARY KEY AUTOINCREMENT,
                sync_id     TEXT NOT NULL,
                space_id    TEXT NOT NULL,
                report_file TEXT NOT NULL,
                url         TEXT NOT NULL,
                title       TEXT
            );
            CREATE INDEX IF NOT EXISTS idx_citations_space ON citations(space_id);
            CREATE TABLE IF NOT EXISTS session_sources (
                session_id TEXT NOT NULL,
                url_norm   TEXT NOT NULL,
                flag TEXT,
                updated_at TEXT,
                PRIMARY KEY (session_id, url_norm)
            );
            CREATE TABLE IF NOT EXISTS watches (
                id             TEXT PRIMARY KEY,
                space_id       TEXT NOT NULL,
                topic          TEXT NOT NULL,
                interval_hours INTEGER NOT NULL,
                session_id     TEXT NOT NULL,
                last_run_at    TEXT,
                updated_at     TEXT
            );
            CREATE TABLE IF NOT EXISTS swarm_personas (
                session_id TEXT NOT NULL,
                ord        INTEGER NOT NULL,
                name       TEXT NOT NULL,
                model      TEXT NOT NULL,
                persona    TEXT NOT NULL
            );
            CREATE INDEX IF NOT EXISTS idx_swarm_personas_session
                ON swarm_personas(session_id, ord);
            CREATE TABLE IF NOT EXISTS usage_log (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                sync_id TEXT NOT NULL,
                created_at TEXT NOT NULL,
                session_id TEXT,
                space_id TEXT,
                backend TEXT NOT NULL,
                model TEXT NOT NULL,
                prompt_tokens INTEGER NOT NULL,
                completion_tokens INTEGER NOT NULL,
                cache_read_tokens INTEGER NOT NULL DEFAULT 0,
                cache_creation_tokens INTEGER NOT NULL DEFAULT 0,
                cost REAL,
                cost_is_provider INTEGER,
                updated_at TEXT
            );
            CREATE INDEX IF NOT EXISTS idx_usage_log_created ON usage_log(created_at);
            CREATE INDEX IF NOT EXISTS idx_usage_log_model ON usage_log(model);
            CREATE TABLE IF NOT EXISTS sync_tombstones (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                table_name TEXT NOT NULL,
                row_id TEXT NOT NULL,
                deleted_at TEXT NOT NULL
            );
            CREATE INDEX IF NOT EXISTS idx_sync_tombstones_table ON sync_tombstones(table_name, row_id);
            CREATE TABLE IF NOT EXISTS device_meta (
                device_id TEXT PRIMARY KEY,
                created_at TEXT NOT NULL
            );
            CREATE TABLE IF NOT EXISTS sync_state (
                peer_id TEXT NOT NULL,
                table_name TEXT NOT NULL,
                pull_cursor TEXT,
                push_cursor TEXT,
                last_synced_at TEXT,
                PRIMARY KEY (peer_id, table_name)
            );",
        )?;
        // user_version-gated migrations. Legacy dbs (version 0) get the
        // column adds they may still lack — guarded by `PRAGMA table_info`,
        // the only tolerated "duplicate" — plus a one-time move of the
        // device-local tables into cache.db. Real errors propagate; nothing
        // is swallowed.
        let version: i64 = self
            .conn
            .query_row("PRAGMA user_version", [], |r| r.get(0))?;
        if version < SCHEMA_VERSION {
            for (table, column, ddl) in LEGACY_COLUMN_ADDS {
                if !has_column(&self.conn, table, column)? {
                    self.conn.execute(ddl, []).with_context(|| {
                        format!("migrating column {table}.{column} (user_version {version})")
                    })?;
                }
            }
            // Device-local ids for rows whose AUTOINCREMENT ids can't be
            // sync identity. One transaction: a large usage_log must not
            // pay a per-row fsync (minutes on a file db). Idempotent — rows
            // already stamped are skipped by the IS NULL filter.
            let backfill_tx = self.conn.transaction()?;
            for table in ["citations", "usage_log"] {
                let ids: Vec<i64> = {
                    let mut stmt = backfill_tx
                        .prepare(&format!("SELECT id FROM {table} WHERE sync_id IS NULL"))?;
                    let rows = stmt.query_map([], |r| r.get(0))?;
                    rows.collect::<rusqlite::Result<Vec<_>>>()?
                };
                let mut update = backfill_tx
                    .prepare(&format!("UPDATE {table} SET sync_id = ?1 WHERE id = ?2"))?;
                for id in ids {
                    update.execute((Uuid::new_v4().to_string(), id))?;
                }
            }
            backfill_tx.commit()?;
            // Unique indexes on the backfilled ids (fresh dbs already have
            // the columns inline; the indexes must wait until legacy dbs
            // have theirs).
            self.conn.execute_batch(
                "CREATE UNIQUE INDEX IF NOT EXISTS idx_citations_sync_id ON citations(sync_id);
                 CREATE UNIQUE INDEX IF NOT EXISTS idx_usage_log_sync_id ON usage_log(sync_id);",
            )?;
            // One-time move of the device-local tables into cache.db (they
            // were the same device's local store before the split, so the
            // copy preserves behavior exactly). Each copy is guarded by
            // table existence — fresh dbs have nothing to move.
            let now = Utc::now().to_rfc3339();
            if has_column(&self.conn, "files", "mtime")? {
                self.conn.execute(
                    "INSERT OR IGNORE INTO cache.file_index_state (file_id, mtime, status, updated_at)
                     SELECT id, mtime, status, ?1 FROM files",
                    [&now],
                )?;
            }
            if has_column(&self.conn, "web_cache", "url_norm")? {
                self.conn.execute(
                    "INSERT OR IGNORE INTO cache.web_cache (url_norm, url, title, text, fetched_at)
                     SELECT url_norm, url, title, text, fetched_at FROM web_cache",
                    [],
                )?;
            }
            if has_column(&self.conn, "chunk_embeddings", "file_id")? {
                self.conn.execute(
                    "INSERT OR IGNORE INTO cache.chunk_embeddings (file_id, seq, vec)
                     SELECT file_id, seq, vec FROM chunk_embeddings",
                    [],
                )?;
            }
            if has_column(&self.conn, "file_chunks", "file_id")? {
                self.conn.execute(
                    "INSERT OR IGNORE INTO cache.file_chunks (file_id, seq, location, text)
                     SELECT file_id, seq, location, text FROM file_chunks",
                    [],
                )?;
            }
            // model_prices may predate its cache-rate columns; copy with the
            // widest shape the legacy table actually has.
            if has_column(&self.conn, "model_prices", "model_id")? {
                let cols = if has_column(&self.conn, "model_prices", "cache_read_price")? {
                    "model_id, backend, prompt_price, completion_price,\n                        cache_read_price, cache_write_price, updated_at"
                } else {
                    "model_id, backend, prompt_price, completion_price, updated_at"
                };
                self.conn.execute(
                    &format!(
                        "INSERT OR IGNORE INTO cache.model_prices ({cols}) SELECT {cols} FROM model_prices"
                    ),
                    [],
                )?;
            }
            self.conn
                .execute_batch(&format!("PRAGMA user_version = {SCHEMA_VERSION}"))?;
        }
        // Migration: remove the message_images table — images are now embedded
        // as markdown `![alt](file)` in message content.
        let _ = self
            .conn
            .execute_batch("DROP TABLE IF EXISTS message_images;");
        // Ensure the default space exists, then backfill any session left
        // without a space (pre-spaces db, or a space that got deleted).
        let now = Utc::now().to_rfc3339();
        self.conn.execute(
            "INSERT OR IGNORE INTO spaces (id, name, created_at) VALUES (?1, ?2, ?3)",
            (Uuid::new_v4().to_string(), DEFAULT_SPACE, &now),
        )?;
        let default_id: String = self.conn.query_row(
            "SELECT id FROM spaces WHERE name = ?1",
            [DEFAULT_SPACE],
            |r| r.get(0),
        )?;
        self.conn.execute(
            "UPDATE sessions SET space_id = ?1 WHERE space_id IS NULL",
            [&default_id],
        )?;
        Ok(())
    }

    /// The default space's id (always present after `migrate`).
    pub fn default_space_id(&self) -> Result<String> {
        Ok(self.conn.query_row(
            "SELECT id FROM spaces WHERE name = ?1",
            [DEFAULT_SPACE],
            |r| r.get(0),
        )?)
    }

    pub fn create_space(&self, name: &str) -> Result<Space> {
        let id = Uuid::new_v4().to_string();
        let now = Utc::now().to_rfc3339();
        self.conn.execute(
            "INSERT INTO spaces (id, name, created_at, updated_at) VALUES (?1, ?2, ?3, ?3)",
            (&id, name, &now),
        )?;
        Ok(Space {
            id,
            name: name.to_string(),
            created_at: now,
        })
    }

    /// Spaces oldest-first (`default` was inserted first, so it naturally leads).
    pub fn list_spaces(&self) -> Result<Vec<Space>> {
        let mut stmt = self
            .conn
            .prepare("SELECT id, name, created_at FROM spaces ORDER BY created_at ASC")?;
        let rows = stmt.query_map([], |r| {
            Ok(Space {
                id: r.get(0)?,
                name: r.get(1)?,
                created_at: r.get(2)?,
            })
        })?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    pub fn rename_space(&self, id: &str, name: &str) -> Result<()> {
        self.conn.execute(
            "UPDATE spaces SET name = ?2, updated_at = ?3 WHERE id = ?1",
            (id, name, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// Delete a space, reassigning its sessions to `default` rather than
    /// deleting them — only the space's own memory/instructions are lost.
    /// The reassignment bumps every moved session's version (a mutation,
    /// sync-wise); the space itself is tombstoned.
    pub fn delete_space(&self, id: &str) -> Result<()> {
        let default_id = self.default_space_id()?;
        self.conn.execute(
            "UPDATE sessions SET space_id = ?1, updated_at = ?2 WHERE space_id = ?3",
            (&default_id, Utc::now().to_rfc3339(), id),
        )?;
        self.conn
            .execute("DELETE FROM spaces WHERE id = ?1", [id])?;
        self.tombstone("spaces", id)?;
        Ok(())
    }

    /// Number of sessions currently in a space (shown in the space picker).
    pub fn count_sessions(&self, space_id: &str) -> Result<u64> {
        let n: i64 = self.conn.query_row(
            "SELECT COUNT(*) FROM sessions WHERE space_id = ?1",
            [space_id],
            |r| r.get(0),
        )?;
        Ok(n as u64)
    }

    /// The most recent user/assistant message of a session — the session
    /// picker's preview strip, so you can see what a session is about before
    /// opening it.
    pub fn last_message_preview(&self, session_id: &str) -> Option<String> {
        let mut stmt = self
            .conn
            .prepare(
                "SELECT content FROM messages WHERE session_id = ?1 \
                 AND role IN ('user','assistant') ORDER BY id DESC LIMIT 1",
            )
            .ok()?;
        let mut rows = stmt
            .query_map([session_id], |r| r.get::<_, String>(0))
            .ok()?;
        rows.next().and_then(Result::ok)
    }

    // --- key/value app settings ---

    /// Whether an `app_settings` key is device-local rather than syncable.
    /// Local keys describe this device's capabilities or per-device state
    /// (search endpoints, secrets, the OCR stack, ui state); everything else
    /// is a user preference that should follow the user. New keys must be
    /// classified here — the default is sync, so a forgotten local key would
    /// silently sync to other devices.
    pub fn setting_is_local(key: &str) -> bool {
        matches!(
            key,
            // Device capabilities / local services: a phone has no
            // localhost SearXNG, no ollama, no tesseract, and its API keys
            // are its own.
            "searxng_url"
                | "langsearch_key"
                | "search_provider"
                | "ocr_engine"
                | "ocr_model"
                | "local_ocr_model"
                // Per-device ui/timing state.
                | "usage_range"
                | "last_update_check"
        )
    }

    pub fn set_setting(&self, key: &str, value: &str) -> Result<()> {
        let now = Utc::now().to_rfc3339();
        let scope = if Self::setting_is_local(key) {
            "local"
        } else {
            "sync"
        };
        self.conn.execute(
            "INSERT INTO app_settings (key, value, scope, updated_at)
             VALUES (?1, ?2, ?3, ?4)
             ON CONFLICT(key) DO UPDATE SET value = ?2, scope = ?3, updated_at = ?4",
            (key, value, scope, &now),
        )?;
        Ok(())
    }

    pub fn load_settings(&self) -> Result<Vec<(String, String)>> {
        let mut stmt = self.conn.prepare("SELECT key, value FROM app_settings")?;
        let rows = stmt.query_map([], |r| Ok((r.get(0)?, r.get(1)?)))?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    /// Daily throttle for the startup update check: returns `true` (and
    /// records today as the check date) when no check has run today yet,
    /// `false` when one already has. The check is a courtesy, not a
    /// service — one tiny index fetch per day is plenty.
    pub fn update_check_due(&self) -> bool {
        let today = chrono::Local::now().format("%Y-%m-%d").to_string();
        let last = self
            .conn
            .query_row(
                "SELECT value FROM app_settings WHERE key = 'last_update_check'",
                [],
                |r| r.get::<_, String>(0),
            )
            .ok();
        if last.as_deref() == Some(today.as_str()) {
            return false;
        }
        let _ = self.set_setting("last_update_check", &today);
        true
    }

    /// Set (or clear, with None) a model's reasoning effort.
    pub fn set_reasoning(&self, model_id: &str, effort: Option<&str>) -> Result<()> {
        self.conn.execute(
            "INSERT INTO model_prefs (id, reasoning, updated_at) VALUES (?1, ?2, ?3)
             ON CONFLICT(id) DO UPDATE SET reasoning = ?2, updated_at = ?3",
            (model_id, effort, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// Flip a model's favorite flag; returns the new state.
    pub fn toggle_favorite(&self, model_id: &str) -> Result<bool> {
        self.conn.execute(
            "INSERT INTO model_prefs (id, favorite, updated_at) VALUES (?1, 1, ?2)
             ON CONFLICT(id) DO UPDATE SET favorite = 1 - favorite, updated_at = ?2",
            (model_id, Utc::now().to_rfc3339()),
        )?;
        let fav: i64 = self.conn.query_row(
            "SELECT favorite FROM model_prefs WHERE id = ?1",
            [model_id],
            |r| r.get(0),
        )?;
        Ok(fav != 0)
    }

    /// Record a model as just used (for the recents ordering).
    pub fn mark_model_used(&self, model_id: &str) -> Result<()> {
        let now = Utc::now().to_rfc3339();
        self.conn.execute(
            "INSERT INTO model_prefs (id, favorite, last_used, updated_at) VALUES (?1, 0, ?2, ?2)
             ON CONFLICT(id) DO UPDATE SET last_used = ?2, updated_at = ?2",
            (model_id, &now),
        )?;
        Ok(())
    }

    /// All stored prefs: (model id, favorite, last used, reasoning effort).
    pub fn load_model_prefs(&self) -> Result<Vec<ModelPref>> {
        let mut stmt = self
            .conn
            .prepare("SELECT id, favorite, last_used, reasoning FROM model_prefs")?;
        let rows = stmt.query_map([], |r| {
            Ok(ModelPref {
                id: r.get(0)?,
                favorite: r.get::<_, i64>(1)? != 0,
                last_used: r.get(2)?,
                reasoning: r.get(3)?,
            })
        })?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    pub fn create_session(
        &self,
        title: &str,
        model: &str,
        space_id: &str,
        kind: &str,
    ) -> Result<Session> {
        let id = Uuid::new_v4().to_string();
        let now = Utc::now().to_rfc3339();
        self.conn.execute(
            "INSERT INTO sessions (id, title, model, space_id, kind, created_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?6)",
            (&id, title, model, space_id, kind, &now),
        )?;
        Ok(Session {
            id,
            title: title.to_string(),
            model: model.to_string(),
            slug: None,
            created_at: now,
            compact_summary: None,
            compact_through: 0,
            web_mode: false,
            swarm_mode: false,
            kind: kind.to_string(),
            research_parent_id: None,
        })
    }

    /// A single session by id, or `None` if it doesn't exist (e.g. deleted
    /// out from under a watch).
    pub fn get_session(&self, id: &str) -> Result<Option<Session>> {
        self.conn
            .query_row(
                "SELECT id, title, model, slug, created_at, compact_summary, compact_through, \
                 web_mode, swarm_mode, kind, research_parent_id
                 FROM sessions WHERE id = ?1",
                [id],
                |r| {
                    Ok(Session {
                        id: r.get(0)?,
                        title: r.get(1)?,
                        model: r.get(2)?,
                        slug: r.get(3)?,
                        created_at: r.get(4)?,
                        compact_summary: r.get(5)?,
                        compact_through: r.get(6)?,
                        web_mode: r.get::<_, i64>(7)? != 0,
                        swarm_mode: r.get::<_, i64>(8)? != 0,
                        kind: r.get(9)?,
                        research_parent_id: r.get(10)?,
                    })
                },
            )
            .optional()
            .map_err(Into::into)
    }

    /// Sessions in `space_id`, most-recently-updated first.
    pub fn list_sessions(&self, space_id: &str) -> Result<Vec<Session>> {
        let mut stmt = self.conn.prepare(
            "SELECT id, title, model, slug, created_at, compact_summary, compact_through, \
             web_mode, swarm_mode, kind, research_parent_id
             FROM sessions WHERE space_id = ?1 ORDER BY updated_at DESC",
        )?;
        let rows = stmt.query_map([space_id], |r| {
            Ok(Session {
                id: r.get(0)?,
                title: r.get(1)?,
                model: r.get(2)?,
                slug: r.get(3)?,
                created_at: r.get(4)?,
                compact_summary: r.get(5)?,
                compact_through: r.get(6)?,
                web_mode: r.get::<_, i64>(7)? != 0,
                swarm_mode: r.get::<_, i64>(8)? != 0,
                kind: r.get(9)?,
                research_parent_id: r.get(10)?,
            })
        })?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    /// Store an auto-compaction result: the digest plus how many raw messages
    /// it now covers.
    pub fn set_compaction(&self, session_id: &str, summary: &str, through: i64) -> Result<()> {
        self.conn.execute(
            "UPDATE sessions SET compact_summary = ?2, compact_through = ?3, updated_at = ?4
             WHERE id = ?1",
            (session_id, summary, through, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// Persist a session's `/web` answer-mode toggle.
    pub fn set_session_web_mode(&self, session_id: &str, on: bool) -> Result<()> {
        self.conn.execute(
            "UPDATE sessions SET web_mode = ?2, updated_at = ?3 WHERE id = ?1",
            (session_id, i64::from(on), Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// Persist a session's `/swarm` mode toggle.
    pub fn set_session_swarm_mode(&self, session_id: &str, on: bool) -> Result<()> {
        self.conn.execute(
            "UPDATE sessions SET swarm_mode = ?2, updated_at = ?3 WHERE id = ?1",
            (session_id, i64::from(on), Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// A session's `/swarm` roster, in display order.
    pub fn list_swarm_personas(&self, session_id: &str) -> Result<Vec<Persona>> {
        let mut stmt = self.conn.prepare(
            "SELECT name, model, persona FROM swarm_personas
             WHERE session_id = ?1 ORDER BY ord ASC",
        )?;
        let rows = stmt.query_map([session_id], |r| {
            Ok(Persona {
                name: r.get(0)?,
                model: r.get(1)?,
                blurb: r.get(2)?,
            })
        })?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    /// Replace a session's whole `/swarm` roster with `personas`, in order.
    /// The roster has no per-row LWW — saving is DELETE-all + INSERT — so
    /// the collection is versioned by bumping the owning session, and each
    /// removed slot is tombstoned for the merge engine.
    pub fn save_swarm_personas(&self, session_id: &str, personas: &[Persona]) -> Result<()> {
        let old: Vec<i64> = {
            let mut stmt = self
                .conn
                .prepare("SELECT ord FROM swarm_personas WHERE session_id = ?1")?;
            let rows = stmt.query_map([session_id], |r| r.get(0))?;
            rows.collect::<rusqlite::Result<Vec<_>>>()?
        };
        for ord in old {
            self.tombstone("swarm_personas", &format!("{session_id}:{ord}"))?;
        }
        self.conn.execute(
            "DELETE FROM swarm_personas WHERE session_id = ?1",
            [session_id],
        )?;
        for (i, p) in personas.iter().enumerate() {
            self.conn.execute(
                "INSERT INTO swarm_personas (session_id, ord, name, model, persona)
                 VALUES (?1, ?2, ?3, ?4, ?5)",
                (session_id, i as i64, &p.name, &p.model, &p.blurb),
            )?;
        }
        self.touch("sessions", session_id)
    }

    /// Set a session's `research_parent_id` after creation (e.g. when
    /// a regular chat is promoted to research and the original session is
    /// created first).
    pub fn set_research_parent(&self, id: &str, parent_id: &str) -> Result<()> {
        self.conn.execute(
            "UPDATE sessions SET research_parent_id = ?2, updated_at = ?3 WHERE id = ?1",
            (id, parent_id, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// Set a session's title and (optionally) its generated slug.
    pub fn set_session_title(&self, id: &str, title: &str, slug: Option<&str>) -> Result<()> {
        self.conn.execute(
            "UPDATE sessions SET title = ?2, slug = COALESCE(?3, slug), updated_at = ?4
             WHERE id = ?1",
            (id, title, slug, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// Delete a single message row by id — used to roll back a persisted
    /// `gate_reply` whose channel delivery failed, so a retry can't
    /// duplicate it in the transcript. Tombstoned for sync.
    pub fn delete_message(&self, id: &str) -> Result<()> {
        self.conn
            .execute("DELETE FROM messages WHERE id = ?1", [id])?;
        self.tombstone("messages", id)?;
        Ok(())
    }

    /// Delete a session and all its messages. Every removed row is
    /// tombstoned — messages are append-only union rows, so a peer must
    /// learn each one is gone, not just the session.
    pub fn delete_session(&self, id: &str) -> Result<()> {
        let ids: Vec<String> = {
            let mut stmt = self
                .conn
                .prepare("SELECT id FROM messages WHERE session_id = ?1")?;
            let rows = stmt.query_map([id], |r| r.get(0))?;
            rows.collect::<rusqlite::Result<Vec<_>>>()?
        };
        for mid in &ids {
            self.tombstone("messages", mid)?;
        }
        self.conn
            .execute("DELETE FROM messages WHERE session_id = ?1", [id])?;
        self.conn
            .execute("DELETE FROM sessions WHERE id = ?1", [id])?;
        self.tombstone("sessions", id)?;
        Ok(())
    }

    pub fn load_messages(&self, session_id: &str) -> Result<Vec<Message>> {
        let mut stmt = self.conn.prepare(
            "SELECT role, content, model, reasoning, tokens, secs, cost, phrase, persona, created_at
             FROM messages WHERE session_id = ?1 ORDER BY created_at ASC",
        )?;
        let messages = stmt
            .query_map([session_id], |r| {
                Ok(Message {
                    role: r.get(0)?,
                    content: r.get(1)?,
                    model: r.get(2)?,
                    reasoning: r.get(3)?,
                    tokens: r.get(4)?,
                    secs: r.get(5)?,
                    cost: r.get(6)?,
                    phrase: r.get(7)?,
                    persona: r.get(8)?,
                    created_at: r.get(9)?,
                })
            })?
            .collect::<rusqlite::Result<Vec<_>>>()?;
        Ok(messages)
    }

    /// Insert a user message (no model/reasoning/stats). Returns its id.
    pub fn add_user_message(&self, session_id: &str, content: &str) -> Result<String> {
        self.insert_message(
            session_id, "user", content, None, None, None, None, None, None,
        )
    }

    /// A user's reply to a survey/approval gate: rendered in the transcript
    /// like a user message but never replayed to the model (`gate_reply`
    /// role) — the survey/plan rows it answers are excluded from model
    /// history too, so bare answers ("the second option", "drop Q2") must
    /// not reach the model without their context.
    pub fn add_gate_reply_message(&self, session_id: &str, content: &str) -> Result<String> {
        self.insert_message(
            session_id,
            "gate_reply",
            content,
            None,
            None,
            None,
            None,
            None,
            None,
        )
    }

    /// Insert a tool-call transcript block: `content` is JSON
    /// `{"name","arguments","result"}`. Never sent back to the model.
    pub fn add_tool_call_message(&self, session_id: &str, content: &str) -> Result<String> {
        self.insert_message(
            session_id,
            "tool_call",
            content,
            None,
            None,
            None,
            None,
            None,
            None,
        )
    }

    /// Insert a failed-response line. It remains visible in the transcript
    /// after the status bar changes, but is never replayed to the model.
    pub fn add_error_message(&self, session_id: &str, content: &str) -> Result<String> {
        self.insert_message(
            session_id, "error", content, None, None, None, None, None, None,
        )
    }

    /// Insert a background-research stage/progress line: plain text, shown in
    /// the transcript but never sent back to the model (unlike `tool_call`
    /// rows, never replayed into `build_history` either — this is the job's
    /// own scratch work, not something the chat model did).
    pub fn add_research_stage_message(&self, session_id: &str, content: &str) -> Result<String> {
        self.insert_message(
            session_id,
            "research_stage",
            content,
            None,
            None,
            None,
            None,
            None,
            None,
        )
    }

    /// A research pipeline's plan-approval prompt: rendered like a stage row
    /// but actionable, and (like `research_stage`) never replayed to the model.
    pub fn add_research_plan_message(&self, session_id: &str, content: &str) -> Result<String> {
        self.insert_message(
            session_id,
            "research_plan",
            content,
            None,
            None,
            None,
            None,
            None,
            None,
        )
    }

    /// A research pipeline's clarifying-survey section: the scoping agent's
    /// questions awaiting a chat answer. Rendered like a stage row but
    /// actionable, and never replayed to the model.
    pub fn add_survey_message(&self, session_id: &str, content: &str) -> Result<String> {
        self.insert_message(
            session_id, "survey", content, None, None, None, None, None, None,
        )
    }

    /// Update the most recent `research_stage` row for `session_id` whose
    /// content starts with `label`, or insert one on the stage's first
    /// occurrence — keeps one transcript row per named stage instead of
    /// appending on every progress tick (e.g. every searcher finishing).
    pub fn upsert_research_stage_message(
        &self,
        session_id: &str,
        label: &str,
        detail: &str,
    ) -> Result<()> {
        let content = stage_content(label, detail);
        let existing: Option<String> = self
            .conn
            .query_row(
                "SELECT id FROM messages WHERE session_id = ?1 AND role = 'research_stage'
                   AND (content = ?2 OR content LIKE ?3)
                 ORDER BY created_at DESC LIMIT 1",
                (session_id, label, format!("{label}:%")),
                |r| r.get(0),
            )
            .ok();
        match existing {
            Some(id) => {
                let now = Utc::now().to_rfc3339();
                self.conn.execute(
                    "UPDATE messages SET content = ?2, created_at = ?3 WHERE id = ?1",
                    (&id, &content, &now),
                )?;
            }
            None => {
                self.add_research_stage_message(session_id, &content)?;
            }
        }
        Ok(())
    }

    /// See the free function of the same name. Production code (the
    /// research pipeline task) writes through its own connection; this
    /// handle exists for tests.
    #[cfg(test)]
    pub fn add_session_sources(&self, session_id: &str, url_norms: &[String]) -> Result<()> {
        add_session_sources(&self.conn, session_id, url_norms)
    }

    /// See the free function of the same name.
    #[cfg(test)]
    pub fn search_session_sources(
        &self,
        session_id: &str,
        query: &str,
    ) -> Result<Vec<(String, String)>> {
        search_session_sources(&self.conn, session_id, query)
    }

    /// Pin (`Some("pinned")`), discard (`Some("discarded")`), or clear
    /// (`None`) a session source's flag. `url_norm` must already exist in
    /// `session_sources` for this session (a no-op UPDATE otherwise — the
    /// row is created by `add_session_sources` when a source is first
    /// cited, not here). Bumps the row's version — flag changes sync.
    pub fn set_source_flag(
        &self,
        session_id: &str,
        url_norm: &str,
        flag: Option<&str>,
    ) -> Result<()> {
        self.conn.execute(
            "UPDATE session_sources SET flag = ?3, updated_at = ?4
             WHERE session_id = ?1 AND url_norm = ?2",
            (session_id, url_norm, flag, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// Insert an assistant reply with its model, reasoning trace, and stats.
    /// `cost` is the provider-reported USD total or a cache-aware catalog
    /// estimate (`None` when neither is available).
    /// Args mirror the messages table columns; ~25 call sites pass inline
    /// `None`s for unused fields, so a struct would churn all of them.
    #[allow(clippy::too_many_arguments)]
    pub fn add_assistant_message(
        &self,
        session_id: &str,
        content: &str,
        model: Option<&str>,
        reasoning: Option<&str>,
        tokens: Option<i64>,
        secs: Option<f64>,
        cost: Option<f64>,
        phrase: Option<&str>,
    ) -> Result<String> {
        self.insert_message(
            session_id,
            "assistant",
            content,
            model,
            reasoning,
            tokens,
            secs,
            cost,
            phrase,
        )
    }

    /// Insert a `/swarm` persona's round reply: an assistant message tagged
    /// with which persona (and its own model) produced it.
    pub fn add_persona_message(
        &self,
        session_id: &str,
        content: &str,
        persona_name: &str,
        model: &str,
    ) -> Result<String> {
        let id = self.insert_message(
            session_id,
            "assistant",
            content,
            Some(model),
            None,
            None,
            None,
            None,
            None,
        )?;
        self.conn.execute(
            "UPDATE messages SET persona = ?2 WHERE id = ?1",
            (&id, persona_name),
        )?;
        Ok(id)
    }

    /// Shared message-row insert; kept flat for the same reason as
    /// `add_assistant_message` — column-shaped params, many inline callers.
    #[allow(clippy::too_many_arguments)]
    pub(crate) fn insert_message(
        &self,
        session_id: &str,
        role: &str,
        content: &str,
        model: Option<&str>,
        reasoning: Option<&str>,
        tokens: Option<i64>,
        secs: Option<f64>,
        cost: Option<f64>,
        phrase: Option<&str>,
    ) -> Result<String> {
        let now = Utc::now().to_rfc3339();
        let id = Uuid::new_v4().to_string();
        self.conn.execute(
            "INSERT INTO messages
                (id, session_id, role, content, model, reasoning, tokens, secs, cost, phrase, created_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11)",
            (
                &id, session_id, role, content, model, reasoning, tokens, secs, cost, phrase, &now,
            ),
        )?;
        self.conn.execute(
            "UPDATE sessions SET updated_at = ?2 WHERE id = ?1",
            (session_id, &now),
        )?;
        Ok(id)
    }

    /// `created_at` of the message at `index` (0-based, transcript order) —
    /// used to anchor a compaction row at the boundary without loading the
    /// whole session (e.g. when the job finishes after the user switched
    /// sessions). `None` when the session has fewer than `index + 1` messages.
    pub fn message_created_at(&self, session_id: &str, index: usize) -> Result<Option<String>> {
        let mut stmt = self.conn.prepare(
            "SELECT created_at FROM messages WHERE session_id = ?1
             ORDER BY created_at ASC LIMIT 1 OFFSET ?2",
        )?;
        Ok(stmt
            .query_row((session_id, index as i64), |r| r.get(0))
            .optional()?)
    }

    /// Insert a compaction-digest row at the exact `created_at` position —
    /// the timestamp of the last message the digest covers, so reloads keep
    /// the digest at the compaction boundary (right after the raw messages
    /// it summarizes) instead of at the end of the transcript. Unlike
    /// `insert_message`, this does not bump the session's `updated_at`:
    /// compacting is bookkeeping, not new activity.
    pub fn add_compaction_message(
        &self,
        session_id: &str,
        content: &str,
        at: &str,
    ) -> Result<String> {
        let id = Uuid::new_v4().to_string();
        self.conn.execute(
            "INSERT INTO messages
                (id, session_id, role, content, model, reasoning, tokens, secs, phrase, created_at)
             VALUES (?1, ?2, 'compaction', ?3, NULL, NULL, NULL, NULL, NULL, ?4)",
            (&id, session_id, content, at),
        )?;
        Ok(id)
    }

    /// Replace the session's compaction row's content in place — a later
    /// compaction folds new messages into the same digest, so there is
    /// exactly one row per session. Returns the number of rows updated
    /// (0 = the session has no compaction row yet).
    pub fn update_compaction_message(&self, session_id: &str, content: &str) -> Result<usize> {
        Ok(self.conn.execute(
            "UPDATE messages SET content = ?2
             WHERE session_id = ?1 AND role = 'compaction'",
            (session_id, content),
        )?)
    }

    pub fn set_session_model(&self, session_id: &str, model: &str) -> Result<()> {
        self.conn.execute(
            "UPDATE sessions SET model = ?2, updated_at = ?3 WHERE id = ?1",
            (session_id, model, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    // --- space filesets ---

    /// Insert or replace a file row (unique per space+name). Returns the row id;
    /// an existing row keeps its id, so its chunks can be replaced by `file_id`.
    /// The durable `files` row keeps only identity + content stats; `status`
    /// is this device's derived index state and lives in `cache.file_index_state`
    /// (a cold cache shows "not indexed" until the next rescan re-derives it).
    pub fn upsert_file(
        &self,
        space_id: &str,
        name: &str,
        hash: &str,
        size: i64,
        status: &str,
    ) -> Result<String> {
        let now = Utc::now().to_rfc3339();
        if let Ok(existing) = self.conn.query_row(
            "SELECT id FROM files WHERE space_id = ?1 AND name = ?2",
            (space_id, name),
            |r| r.get::<_, String>(0),
        ) {
            self.conn.execute(
                "UPDATE files SET hash = ?2, size = ?3, updated_at = ?4 WHERE id = ?1",
                (&existing, hash, size, &now),
            )?;
            self.conn.execute(
                "INSERT INTO cache.file_index_state (file_id, mtime, status, updated_at)
                 VALUES (?1, 0, ?2, ?3)
                 ON CONFLICT(file_id) DO UPDATE SET status = excluded.status,
                     updated_at = excluded.updated_at",
                (&existing, status, &now),
            )?;
            return Ok(existing);
        }
        let id = Uuid::new_v4().to_string();
        self.conn.execute(
            "INSERT INTO files (id, space_id, name, hash, size, created_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)",
            (&id, space_id, name, hash, size, &now, &now),
        )?;
        self.conn.execute(
            "INSERT INTO cache.file_index_state (file_id, mtime, status, updated_at)
             VALUES (?1, 0, ?2, ?3)",
            (&id, status, &now),
        )?;
        Ok(id)
    }

    pub fn list_files(&self, space_id: &str) -> Result<Vec<FileRow>> {
        let mut stmt = self.conn.prepare(
            "SELECT files.id, files.name, files.hash, files.size,
                    COALESCE(cache.file_index_state.status, 'not indexed'),
                    COALESCE(cache.file_index_state.mtime, 0)
             FROM files
             LEFT JOIN cache.file_index_state
                 ON cache.file_index_state.file_id = files.id
             WHERE files.space_id = ?1 ORDER BY files.name ASC",
        )?;
        let rows = stmt.query_map([space_id], |r| {
            Ok(FileRow {
                id: r.get(0)?,
                name: r.get(1)?,
                hash: r.get(2)?,
                size: r.get(3)?,
                status: r.get(4)?,
                mtime: r.get(5)?,
            })
        })?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    /// Whether a file has a `file_index_state` row — i.e. this device has
    /// derived index state for it. A missing row means a cold cache (fresh
    /// restore, deleted cache.db): the rescan must re-extract rather than
    /// trust the stat skip.
    pub fn file_indexed(&self, file_id: &str) -> Result<bool> {
        Ok(self.conn.query_row(
            "SELECT EXISTS(SELECT 1 FROM cache.file_index_state WHERE file_id = ?1)",
            [file_id],
            |r| r.get(0),
        )?)
    }

    pub fn delete_file(&self, file_id: &str) -> Result<()> {
        self.conn.execute(
            "DELETE FROM cache.file_chunks WHERE file_id = ?1",
            [file_id],
        )?;
        self.conn.execute(
            "DELETE FROM cache.chunk_embeddings WHERE file_id = ?1",
            [file_id],
        )?;
        self.conn.execute(
            "DELETE FROM cache.file_index_state WHERE file_id = ?1",
            [file_id],
        )?;
        self.conn
            .execute("DELETE FROM files WHERE id = ?1", [file_id])?;
        self.tombstone("files", file_id)?;
        Ok(())
    }

    /// Record the disk mtime a file was indexed at (see `FileRow::mtime`),
    /// in `cache.file_index_state`. A missing row (cold cache) is created
    /// with the current status.
    pub fn set_file_mtime(&self, file_id: &str, mtime: i64) -> Result<()> {
        let now = Utc::now().to_rfc3339();
        self.conn.execute(
            "INSERT INTO cache.file_index_state (file_id, mtime, status, updated_at)
             VALUES (?1, ?2, '', ?3)
             ON CONFLICT(file_id) DO UPDATE SET mtime = excluded.mtime,
                 updated_at = excluded.updated_at",
            (file_id, mtime, &now),
        )?;
        Ok(())
    }

    /// Update a file's derived status (e.g. "ok", "ocr…", or an error
    /// message) in `cache.file_index_state`. A missing row (cold cache) is
    /// created with the current mtime.
    pub fn set_file_status(&self, file_id: &str, status: &str) -> Result<()> {
        let now = Utc::now().to_rfc3339();
        self.conn.execute(
            "INSERT INTO cache.file_index_state (file_id, mtime, status, updated_at)
             VALUES (?1, 0, ?2, ?3)
             ON CONFLICT(file_id) DO UPDATE SET status = excluded.status,
                 updated_at = excluded.updated_at",
            (file_id, status, &now),
        )?;
        Ok(())
    }

    pub fn rename_file(&self, file_id: &str, new_name: &str) -> Result<()> {
        self.conn.execute(
            "UPDATE files SET name = ?2, updated_at = ?3 WHERE id = ?1",
            (file_id, new_name, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// Replace all occurrences of `old_name` with `new_name` in message content
    /// within the given space. Used when OCR renames a pasted image to a
    /// descriptive filename — updates `![alt](old_name)` → `![alt](new_name)`.
    pub fn replace_file_ref_in_messages(
        &self,
        space_id: &str,
        old_name: &str,
        new_name: &str,
    ) -> Result<()> {
        self.conn.execute(
            "UPDATE messages SET content = REPLACE(content, ?1, ?2)
             WHERE session_id IN (SELECT id FROM sessions WHERE space_id = ?3)",
            (old_name, new_name, space_id),
        )?;
        Ok(())
    }

    /// Replace a file's indexed chunks. `chunks` are `(location, text)` in
    /// order. Any stored embeddings are dropped too — they described the old
    /// chunk texts, and the embedder backfills the new ones. All of this is
    /// device-local derived state in `cache.db`.
    pub fn set_file_chunks(&self, file_id: &str, chunks: &[(String, String)]) -> Result<()> {
        self.conn.execute(
            "DELETE FROM cache.file_chunks WHERE file_id = ?1",
            [file_id],
        )?;
        self.conn.execute(
            "DELETE FROM cache.chunk_embeddings WHERE file_id = ?1",
            [file_id],
        )?;
        for (seq, (location, text)) in chunks.iter().enumerate() {
            self.conn.execute(
                "INSERT INTO cache.file_chunks (file_id, seq, location, text) VALUES (?1, ?2, ?3, ?4)",
                (file_id, seq as i64, location, text),
            )?;
        }
        Ok(())
    }

    /// The underlying connection, for tests exercising the free query
    /// functions the toolbox reaches by opening the db path itself.
    #[cfg(test)]
    pub fn raw(&self) -> &Connection {
        &self.conn
    }

    /// `PRAGMA integrity_check` — the db's own self-test. Returns `"ok"`
    /// when the file is sound, or a list of problems otherwise. Used by
    /// `nexus doctor`.
    pub fn integrity_check(&self) -> Result<String> {
        self.conn
            .query_row("PRAGMA integrity_check", [], |r| r.get(0))
            .context("running integrity check")
    }

    /// A file's chunk texts as `(seq, text)`, in order — the embedder's input.
    pub fn file_chunk_texts(&self, file_id: &str) -> Result<Vec<(i64, String)>> {
        let mut stmt = self.conn.prepare(
            "SELECT CAST(seq AS INTEGER), text FROM cache.file_chunks
             WHERE file_id = ?1 ORDER BY CAST(seq AS INTEGER) ASC",
        )?;
        let rows = stmt.query_map([file_id], |r| Ok((r.get(0)?, r.get(1)?)))?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    /// See the free function of the same name.
    pub fn files_missing_embeddings(&self, space_id: &str) -> Result<Vec<String>> {
        files_missing_embeddings(&self.conn, space_id)
    }

    /// Record a research report's cited sources for the citation index.
    /// Each row gets a UUID `sync_id` — the AUTOINCREMENT `id` is only a
    /// device-local cursor.
    pub fn add_citations(
        &self,
        space_id: &str,
        report_file: &str,
        citations: &[(String, Option<String>)],
    ) -> Result<()> {
        for (url, title) in citations {
            self.conn.execute(
                "INSERT INTO citations (sync_id, space_id, report_file, url, title)
                 VALUES (?1, ?2, ?3, ?4, ?5)",
                (
                    Uuid::new_v4().to_string(),
                    space_id,
                    report_file,
                    url,
                    title,
                ),
            )?;
        }
        Ok(())
    }

    /// See the free function of the same name. Most production code reads
    /// citations through the toolbox's own connection (the free function),
    /// but this handle is also used directly by the watch diff-section
    /// lookup (`previous_citations_for_watch_session`), plus tests.
    pub fn search_citations(
        &self,
        space_id: &str,
        query: Option<&str>,
    ) -> Result<Vec<(String, String, String)>> {
        search_citations(&self.conn, space_id, query)
    }

    /// Store embedding vectors for a file's chunks as `(seq, vector)` pairs.
    pub fn set_chunk_embeddings(&self, file_id: &str, vecs: &[(i64, Vec<f32>)]) -> Result<()> {
        for (seq, v) in vecs {
            self.conn.execute(
                "INSERT OR REPLACE INTO cache.chunk_embeddings (file_id, seq, vec) VALUES (?1, ?2, ?3)",
                (file_id, seq, vec_to_blob(v)),
            )?;
        }
        Ok(())
    }

    pub fn create_watch(
        &self,
        space_id: &str,
        topic: &str,
        interval_hours: i64,
        session_id: &str,
    ) -> Result<String> {
        let id = Uuid::new_v4().to_string();
        let now = Utc::now().to_rfc3339();
        self.conn.execute(
            "INSERT INTO watches (id, space_id, topic, interval_hours, session_id, last_run_at, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, NULL, ?6)",
            (&id, space_id, topic, interval_hours, session_id, &now),
        )?;
        Ok(id)
    }

    pub fn list_watches(&self, space_id: &str) -> Result<Vec<Watch>> {
        let mut stmt = self.conn.prepare(
            "SELECT id, space_id, topic, interval_hours, session_id, last_run_at
             FROM watches WHERE space_id = ?1 ORDER BY topic",
        )?;
        let rows = stmt.query_map([space_id], |r| {
            Ok(Watch {
                id: r.get(0)?,
                space_id: r.get(1)?,
                topic: r.get(2)?,
                interval_hours: r.get(3)?,
                session_id: r.get(4)?,
                last_run_at: r.get(5)?,
            })
        })?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    /// Every watch across all spaces — used by the startup due-check, which
    /// runs before any space is necessarily "active".
    pub fn list_all_watches(&self) -> Result<Vec<Watch>> {
        let mut stmt = self.conn.prepare(
            "SELECT id, space_id, topic, interval_hours, session_id, last_run_at FROM watches",
        )?;
        let rows = stmt.query_map([], |r| {
            Ok(Watch {
                id: r.get(0)?,
                space_id: r.get(1)?,
                topic: r.get(2)?,
                interval_hours: r.get(3)?,
                session_id: r.get(4)?,
                last_run_at: r.get(5)?,
            })
        })?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    pub fn touch_watch(&self, id: &str, now_rfc3339: &str) -> Result<()> {
        self.conn.execute(
            "UPDATE watches SET last_run_at = ?2, updated_at = ?3 WHERE id = ?1",
            (id, now_rfc3339, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    /// Repoint a watch at the session its most recent re-run actually used,
    /// so the next due-check's diff-section lookup
    /// (`previous_citations_for_watch_session`) can match against it.
    pub fn set_watch_session(&self, id: &str, session_id: &str) -> Result<()> {
        self.conn.execute(
            "UPDATE watches SET session_id = ?2, updated_at = ?3 WHERE id = ?1",
            (id, session_id, Utc::now().to_rfc3339()),
        )?;
        Ok(())
    }

    pub fn delete_watch(&self, id: &str) -> Result<()> {
        self.conn
            .execute("DELETE FROM watches WHERE id = ?1", [id])?;
        self.tombstone("watches", id)?;
        Ok(())
    }
}

/// Encode an embedding as little-endian f32 bytes for a BLOB column.
pub fn vec_to_blob(v: &[f32]) -> Vec<u8> {
    v.iter().flat_map(|f| f.to_le_bytes()).collect()
}

/// Decode a BLOB back into an embedding (inverse of `vec_to_blob`).
pub fn blob_to_vec(b: &[u8]) -> Vec<f32> {
    b.chunks_exact(4)
        .map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]]))
        .collect()
}

/// Citations in `space_id` whose `url/title/report_file` contains `query`
/// (case-insensitive substring), or every row when `query` is None — as
/// `(report_file, url, title)`, newest first. Free function so the toolbox
/// can call it over its own short-lived connection.
pub fn search_citations(
    conn: &Connection,
    space_id: &str,
    query: Option<&str>,
) -> Result<Vec<(String, String, String)>> {
    let mut stmt = conn.prepare(
        "SELECT report_file, url, COALESCE(title, '') FROM citations
         WHERE space_id = ?1
           AND (?2 IS NULL OR url LIKE ?2 OR title LIKE ?2 OR report_file LIKE ?2)
         ORDER BY id DESC",
    )?;
    let pattern = query.map(|q| format!("%{q}%"));
    let rows = stmt.query_map((space_id, pattern), |r| {
        Ok((r.get(0)?, r.get(1)?, r.get(2)?))
    })?;
    Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
}

/// One transcript line for a research stage: bare label, or `label: detail`.
pub fn stage_content(label: &str, detail: &str) -> String {
    if detail.is_empty() {
        label.to_string()
    } else {
        format!("{label}: {detail}")
    }
}

/// See `Db::add_session_sources`; free so the research pipeline task can
/// call it over its own short-lived connection.
pub fn add_session_sources(
    conn: &Connection,
    session_id: &str,
    url_norms: &[String],
) -> Result<()> {
    let now = Utc::now().to_rfc3339();
    for u in url_norms {
        conn.execute(
            "INSERT OR IGNORE INTO session_sources (session_id, url_norm, updated_at)
             VALUES (?1, ?2, ?3)",
            (session_id, u, &now),
        )?;
    }
    Ok(())
}

/// Keyword-search (plain substring, case-insensitive) a session's cached
/// source bundle: `(url, text)` for every cached page whose text contains
/// `query`. Ponytail: substring, not FTS — a bundle is a handful of pages,
/// not a corpus.
pub fn search_session_sources(
    conn: &Connection,
    session_id: &str,
    query: &str,
) -> Result<Vec<(String, String)>> {
    let mut stmt = conn.prepare(
        "SELECT cache.web_cache.url, cache.web_cache.text FROM session_sources
         JOIN cache.web_cache ON cache.web_cache.url_norm = session_sources.url_norm
         WHERE session_sources.session_id = ?1",
    )?;
    let rows = stmt.query_map([session_id], |r| {
        Ok((r.get::<_, String>(0)?, r.get::<_, String>(1)?))
    })?;
    let needle = query.to_lowercase();
    Ok(rows
        .collect::<rusqlite::Result<Vec<_>>>()?
        .into_iter()
        .filter(|(_, text)| text.to_lowercase().contains(&needle))
        .collect())
}

/// URLs pinned in a session's source bundle — the Synthesizer/Writer
/// prompts list these as "prioritize these sources".
pub fn pinned_urls(conn: &Connection, session_id: &str) -> Result<Vec<String>> {
    let mut stmt = conn.prepare(
        "SELECT url_norm FROM session_sources WHERE session_id = ?1 AND flag = 'pinned'",
    )?;
    let rows = stmt.query_map([session_id], |r| r.get::<_, String>(0))?;
    Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
}

/// Distinct hostnames discarded in a session — excluded from later searcher
/// rounds the same way the global `blocked_domains` setting is.
pub fn discarded_domains(conn: &Connection, session_id: &str) -> Result<Vec<String>> {
    let mut stmt = conn.prepare(
        "SELECT url_norm FROM session_sources WHERE session_id = ?1 AND flag = 'discarded'",
    )?;
    let rows: Vec<String> = stmt
        .query_map([session_id], |r| r.get::<_, String>(0))?
        .collect::<rusqlite::Result<Vec<_>>>()?;
    let mut hosts: Vec<String> = rows
        .iter()
        .filter_map(|u| {
            reqwest::Url::parse(u)
                .ok()
                .and_then(|p| p.host_str().map(str::to_string))
        })
        .collect();
    hosts.sort();
    hosts.dedup();
    Ok(hosts)
}

/// Whether a cached fetch (`fetched_at`, rfc3339) is still usable — under
/// 24h old. An unparseable timestamp is treated as stale, not an error:
/// the caller just re-fetches live.
pub fn is_fresh(fetched_at: &str, now: chrono::DateTime<Utc>) -> bool {
    chrono::DateTime::parse_from_rfc3339(fetched_at)
        .is_ok_and(|dt| now.signed_duration_since(dt) < chrono::Duration::hours(24))
}

/// A cached fetched page: (title, text, `fetched_at` rfc3339), or None on a
/// cache miss. Free function — the toolbox opens its own short-lived
/// connection by path, same as the file-search queries. The `web_cache`
/// name is deliberately unqualified: it resolves to the attached `cache`
/// schema on a main-db connection, or to `main` on a standalone cache-only
/// connection.
pub fn cache_get(conn: &Connection, url_norm: &str) -> Result<Option<(String, String, String)>> {
    let row = conn.query_row(
        "SELECT COALESCE(title, ''), text, fetched_at FROM web_cache WHERE url_norm = ?1",
        [url_norm],
        |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?)),
    );
    match row {
        Ok(v) => Ok(Some(v)),
        Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
        Err(e) => Err(e.into()),
    }
}

/// Write (or overwrite) a fetched page into the cache, stamped now.
pub fn cache_put(
    conn: &Connection,
    url_norm: &str,
    url: &str,
    title: Option<&str>,
    text: &str,
) -> Result<()> {
    let now = Utc::now().to_rfc3339();
    conn.execute(
        "INSERT INTO web_cache (url_norm, url, title, text, fetched_at) VALUES (?1, ?2, ?3, ?4, ?5)
         ON CONFLICT(url_norm) DO UPDATE SET url = ?2, title = ?3, text = ?4, fetched_at = ?5",
        (url_norm, url, title, text, &now),
    )?;
    Ok(())
}

/// Ids of files (in one space) that have chunks but not a vector per chunk —
/// the embedder's work queue, which doubles as the pre-upgrade backfill.
/// Cross-db: `files` is durable, the chunk tables are device-local.
pub fn files_missing_embeddings(conn: &Connection, space_id: &str) -> Result<Vec<String>> {
    let mut stmt = conn.prepare(
        "SELECT files.id FROM files
         WHERE files.space_id = ?1
           AND (SELECT COUNT(*) FROM cache.file_chunks WHERE cache.file_chunks.file_id = files.id) >
               (SELECT COUNT(*) FROM cache.chunk_embeddings WHERE cache.chunk_embeddings.file_id = files.id)",
    )?;
    let rows = stmt.query_map([space_id], |r| r.get(0))?;
    Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
}

/// Cosine-ranked chunk search within one space: `(file name, location, text,
/// score)`, best first. Vectors whose dimension doesn't match the query (a
/// changed embedding model) are skipped. Brute force — thousands of chunks
/// scan in milliseconds, no ANN index needed.
pub fn semantic_chunks(
    conn: &Connection,
    space_id: &str,
    query: &[f32],
    limit: usize,
) -> Result<Vec<(String, String, String, f32)>> {
    let mut stmt = conn.prepare(
        "SELECT files.name, cache.file_chunks.location, cache.file_chunks.text,
                cache.chunk_embeddings.vec
         FROM cache.chunk_embeddings
         JOIN files ON files.id = cache.chunk_embeddings.file_id
         JOIN cache.file_chunks
             ON cache.file_chunks.file_id = cache.chunk_embeddings.file_id
            AND CAST(cache.file_chunks.seq AS INTEGER) = cache.chunk_embeddings.seq
         WHERE files.space_id = ?1",
    )?;
    let rows = stmt.query_map([space_id], |r| {
        Ok((
            r.get::<_, String>(0)?,
            r.get::<_, String>(1)?,
            r.get::<_, String>(2)?,
            r.get::<_, Vec<u8>>(3)?,
        ))
    })?;
    let mut hits: Vec<(String, String, String, f32)> = Vec::new();
    for row in rows {
        let (name, loc, text, blob) = row?;
        let v = blob_to_vec(&blob);
        if v.len() != query.len() {
            continue;
        }
        let score = cosine(query, &v);
        hits.push((name, loc, text, score));
    }
    hits.sort_by(|a, b| b.3.partial_cmp(&a.3).unwrap_or(std::cmp::Ordering::Equal));
    hits.truncate(limit);
    Ok(hits)
}

fn cosine(a: &[f32], b: &[f32]) -> f32 {
    let (mut dot, mut na, mut nb) = (0.0f32, 0.0f32, 0.0f32);
    for (x, y) in a.iter().zip(b) {
        dot += x * y;
        na += x * x;
        nb += y * y;
    }
    let denom = na.sqrt() * nb.sqrt();
    if denom == 0.0 { 0.0 } else { dot / denom }
}

// --- usage analytics ---

/// Time window for the `/usage` dashboard: which logged requests count.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum UsageRange {
    Day,
    Week,
    Month,
    #[default]
    All,
}

impl UsageRange {
    /// Cycle order for the popup's range key (`←/→`).
    pub const CYCLE: [Self; 4] = [Self::Day, Self::Week, Self::Month, Self::All];

    /// Short badge label, e.g. the `24h` in the popup title.
    pub const fn label(self) -> &'static str {
        match self {
            Self::Day => "24h",
            Self::Week => "7d",
            Self::Month => "30d",
            Self::All => "all",
        }
    }

    /// Long form for titles and empty-state messages.
    pub const fn title(self) -> &'static str {
        match self {
            Self::Day => "last 24 hours",
            Self::Week => "last 7 days",
            Self::Month => "last 30 days",
            Self::All => "all time",
        }
    }

    /// Persisted `app_settings` key value.
    pub const fn key(self) -> &'static str {
        match self {
            Self::Day => "day",
            Self::Week => "week",
            Self::Month => "month",
            Self::All => "all",
        }
    }

    /// Parse a persisted `app_settings` value; unknown keys fall back to
    /// the default (all time).
    pub fn from_key(key: &str) -> Self {
        Self::CYCLE
            .iter()
            .copied()
            .find(|r| r.key() == key)
            .unwrap_or_default()
    }

    /// The next window in cycle order.
    #[must_use]
    pub const fn next(self) -> Self {
        match self {
            Self::Day => Self::Week,
            Self::Week => Self::Month,
            Self::Month => Self::All,
            Self::All => Self::Day,
        }
    }

    /// The previous window in cycle order.
    #[must_use]
    pub const fn prev(self) -> Self {
        match self {
            Self::Day => Self::All,
            Self::Week => Self::Day,
            Self::Month => Self::Week,
            Self::All => Self::Month,
        }
    }

    /// Inclusive cutoff timestamp for SQL filtering; `None` = no filter.
    pub fn since(self) -> Option<chrono::DateTime<chrono::Utc>> {
        use chrono::{Duration, Utc};
        match self {
            Self::Day => Some(Utc::now() - Duration::hours(24)),
            Self::Week => Some(Utc::now() - Duration::days(7)),
            Self::Month => Some(Utc::now() - Duration::days(30)),
            Self::All => None,
        }
    }

    /// Empty-state message for the dashboard/status line.
    pub const fn empty_message(self) -> &'static str {
        match self {
            Self::Day => "no usage in the last 24 hours — ←/→ for a wider window",
            Self::Week => "no usage in the last 7 days — ←/→ for a wider window",
            Self::Month => "no usage in the last 30 days — ←/→ for a wider window",
            Self::All => "no usage logged yet — send a message first",
        }
    }
}

/// The bare model name used by the ``OpenRouter`` catalog's `vendor/name` ids:
/// backend prefixes (`go:`, `openai:`, `codex:`, `opencode:`) and any
/// `vendor/` part are stripped (`go:deepseek-v4-flash` → `deepseek-v4-flash`,
/// `openai:gpt-5` → `gpt-5`). Empty when the id has no name left.
pub fn price_name(model: &str) -> &str {
    let stripped = ["go:", "openai:", "codex:", "opencode:"]
        .iter()
        .find_map(|p| model.strip_prefix(p))
        .unwrap_or(model);
    stripped.rsplit('/').next().unwrap_or(stripped)
}

/// Catalog price for a usage row's model: exact `model_prices` key first,
/// then the ``OpenRouter`` `vendor/name` entry matching the bare name (same
/// cross-backend fallback as `Db::model_price`, but against an in-memory
/// snapshot so a 28k-row backfill needs no per-row SQL).
fn catalog_price<'a>(
    prices: &'a std::collections::HashMap<String, ModelPricing>,
    model: &str,
) -> Option<&'a ModelPricing> {
    if let Some(price) = prices.get(model) {
        return Some(price);
    }
    let name = price_name(model);
    if name.is_empty() {
        return None;
    }
    prices
        .iter()
        .filter(|(id, _)| {
            id.strip_suffix(name)
                .is_some_and(|rest| rest.ends_with('/'))
        })
        .min_by_key(|(id, _)| id.len()) // shortest vendor wins, deterministic
        .map(|(_, price)| price)
}

/// Price a token breakdown. Prompt totals include cache reads/writes, so each
/// cached bucket replaces (rather than adds to) the ordinary prompt rate.
fn catalog_request_cost(
    price: ModelPricing,
    prompt_tokens: u64,
    completion_tokens: u64,
    cache_read_tokens: u64,
    cache_creation_tokens: u64,
) -> f64 {
    let reads = cache_read_tokens.min(prompt_tokens);
    let writes = cache_creation_tokens.min(prompt_tokens - reads);
    let ordinary = prompt_tokens - reads - writes;
    let read_price = price.cache_read.unwrap_or(price.prompt);
    let write_price = price.cache_write.unwrap_or(price.prompt);
    (ordinary as f64 * price.prompt
        + reads as f64 * read_price
        + writes as f64 * write_price
        + completion_tokens as f64 * price.completion)
        / 1e6
}

struct CostBackfillRow {
    id: i64,
    backend: String,
    model: String,
    prompt_tokens: u64,
    completion_tokens: u64,
    cache_read_tokens: u64,
    cache_creation_tokens: u64,
    old_cost: Option<f64>,
    /// `None` identifies rows written before cost provenance was tracked.
    cost_is_provider: Option<bool>,
}

/// Lifetime token/cost totals across every logged request.
#[derive(Default)]
pub struct UsageTotals {
    pub requests: u64,
    pub prompt_tokens: u64,
    pub completion_tokens: u64,
    pub cache_read_tokens: u64,
    pub cache_creation_tokens: u64,
    /// Total USD (0 when no model had a known price).
    pub cost: f64,
}

/// One day's aggregate row from `usage_log` (CLI `--by-day`).
#[derive(Default)]
pub struct UsageDay {
    /// `YYYY-MM-DD`, from the RFC 3339 `created_at` prefix.
    pub day: String,
    pub requests: u64,
    pub prompt_tokens: u64,
    pub completion_tokens: u64,
    pub cache_read_tokens: u64,
    pub cost: f64,
}

/// One backend's aggregate row.
#[derive(Default)]
pub struct UsageByBackend {
    pub backend: String,
    pub requests: u64,
    pub prompt_tokens: u64,
    pub completion_tokens: u64,
    pub cache_read_tokens: u64,
    pub cost: f64,
}

/// One model's aggregate row.
#[derive(Default)]
pub struct UsageByModel {
    pub model: String,
    pub requests: u64,
    pub prompt_tokens: u64,
    pub completion_tokens: u64,
    pub cache_read_tokens: u64,
    pub cost: f64,
}

/// One logged request, newest first.
#[derive(Default)]
pub struct UsageRow {
    pub created_at: String,
    pub backend: String,
    pub model: String,
    pub prompt_tokens: u64,
    pub completion_tokens: u64,
    pub cache_read_tokens: u64,
    pub cost: Option<f64>,
}

impl Db {
    /// Record one completed API request's usage. Content-free — only
    /// backend/model/tokens — so it never leaks conversation text.
    #[allow(clippy::too_many_arguments)]
    pub fn log_usage(
        &self,
        backend: &str,
        model: &str,
        prompt_tokens: u64,
        completion_tokens: u64,
        cache_read_tokens: u64,
        cache_creation_tokens: u64,
        cost: Option<f64>,
        cost_is_provider: bool,
        session_id: Option<&str>,
        space_id: Option<&str>,
    ) -> Result<i64> {
        self.conn.execute(
            "INSERT INTO usage_log (sync_id, created_at, session_id, space_id, backend, model,
                prompt_tokens, completion_tokens, cache_read_tokens, cache_creation_tokens,
                cost, cost_is_provider, updated_at)
             VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)",
            (
                Uuid::new_v4().to_string(),
                Utc::now().to_rfc3339(),
                session_id,
                space_id,
                backend,
                model,
                prompt_tokens as i64,
                completion_tokens as i64,
                cache_read_tokens as i64,
                cache_creation_tokens as i64,
                cost,
                i64::from(cost_is_provider),
                Utc::now().to_rfc3339(),
            ),
        )?;
        Ok(self.conn.last_insert_rowid())
    }

    /// Update a usage row written earlier in the same request's lifecycle.
    /// `OpenCode` Zen splits accounting across two streamed events (real
    /// usage, then the provider-reported cost); the second event updates the
    /// row the first created instead of inserting a duplicate.
    #[allow(clippy::too_many_arguments)]
    pub fn update_usage(
        &self,
        row_id: i64,
        prompt_tokens: u64,
        completion_tokens: u64,
        cache_read_tokens: u64,
        cache_creation_tokens: u64,
        cost: Option<f64>,
        cost_is_provider: bool,
    ) -> Result<()> {
        self.conn.execute(
            "UPDATE usage_log SET prompt_tokens = ?1, completion_tokens = ?2,
                cache_read_tokens = ?3, cache_creation_tokens = ?4, cost = ?5,
                cost_is_provider = ?6, updated_at = ?7
             WHERE id = ?8",
            (
                prompt_tokens as i64,
                completion_tokens as i64,
                cache_read_tokens as i64,
                cache_creation_tokens as i64,
                cost,
                i64::from(cost_is_provider),
                Utc::now().to_rfc3339(),
                row_id,
            ),
        )?;
        Ok(())
    }

    /// Estimated cost of one completed request in USD at current catalog
    /// prices (`None` when no price is known). Cache reads and writes use the
    /// catalog's separate rates when present. Non-`OpenRouter` models fall
    /// back to the matching `OpenRouter` catalog entry (see `model_price`).
    pub fn request_cost(
        &self,
        model: &str,
        prompt_tokens: u64,
        completion_tokens: u64,
        cache_read_tokens: u64,
        cache_creation_tokens: u64,
    ) -> Option<f64> {
        self.model_price(model).map(|price| {
            catalog_request_cost(
                price,
                prompt_tokens,
                completion_tokens,
                cache_read_tokens,
                cache_creation_tokens,
            )
        })
    }

    /// Reconcile estimated request costs with the current `model_prices`
    /// catalog. Rows logged before pricing existed are filled, and stale
    /// estimates (legacy unit bug, price changes, or ignored cache discounts)
    /// are recomputed. Provider-reported costs are exact and never overwritten.
    /// Non-`OpenRouter` models are priced through their `OpenRouter`
    /// `vendor/name` twin, like `model_price`. Existing costs for models with
    /// no current catalog entry are left untouched.
    ///
    /// Idempotent — unchanged rows are not rewritten — so it can run after
    /// every catalog refresh and whenever the `/usage` popup opens. Returns
    /// how many rows were visited.
    pub fn backfill_usage_costs(&mut self) -> Result<usize> {
        // The catalog endpoint reports USD per token while every cost formula
        // here uses USD per 1M. Heal a legacy per-token-shaped catalog before
        // computing costs. NULL cache rates remain NULL under multiplication.
        let max_price: f64 = self.conn.query_row(
            "SELECT COALESCE(MAX(prompt_price), 0) FROM cache.model_prices",
            [],
            |r| r.get(0),
        )?;
        if max_price > 0.0 && max_price < 0.001 {
            self.conn.execute(
                "UPDATE cache.model_prices SET
                    prompt_price = prompt_price * 1e6,
                    completion_price = completion_price * 1e6,
                    cache_read_price = cache_read_price * 1e6,
                    cache_write_price = cache_write_price * 1e6",
                [],
            )?;
        }
        // Snapshot the catalog, then rewrite every usage row in one
        // transaction. 28k rows is a few ms even on a file DB.
        let mut prices: std::collections::HashMap<String, ModelPricing> =
            std::collections::HashMap::default();
        {
            let mut stmt = self.conn.prepare(
                "SELECT model_id, prompt_price, completion_price,
                        cache_read_price, cache_write_price
                 FROM cache.model_prices",
            )?;
            let rows = stmt.query_map([], |r| {
                Ok((
                    r.get::<_, String>(0)?,
                    ModelPricing {
                        prompt: r.get(1)?,
                        completion: r.get(2)?,
                        cache_read: r.get(3)?,
                        cache_write: r.get(4)?,
                    },
                ))
            })?;
            for row in rows {
                let (model, price) = row?;
                prices.insert(model, price);
            }
        }
        let rows: Vec<CostBackfillRow> = {
            let mut stmt = self.conn.prepare(
                "SELECT id, backend, model, prompt_tokens, completion_tokens,
                        cache_read_tokens, cache_creation_tokens, cost,
                        cost_is_provider
                 FROM usage_log",
            )?;
            let rows = stmt.query_map([], |r| {
                Ok(CostBackfillRow {
                    id: r.get(0)?,
                    backend: r.get(1)?,
                    model: r.get(2)?,
                    prompt_tokens: r.get::<_, i64>(3)? as u64,
                    completion_tokens: r.get::<_, i64>(4)? as u64,
                    cache_read_tokens: r.get::<_, i64>(5)? as u64,
                    cache_creation_tokens: r.get::<_, i64>(6)? as u64,
                    old_cost: r.get(7)?,
                    cost_is_provider: r.get::<_, Option<i64>>(8)?.map(|v| v != 0),
                })
            })?;
            rows.collect::<rusqlite::Result<Vec<_>>>()?
        };
        let tx = self.conn.transaction()?;
        {
            let mut update = tx.prepare(
                "UPDATE usage_log SET cost = ?2, cost_is_provider = 0, updated_at = ?3 WHERE id = ?1",
            )?;
            for row in &rows {
                // Before provenance was added, OpenCode was the only backend
                // whose costs came from the provider. Preserve those legacy
                // exact values; all new rows carry an explicit true/false bit.
                let legacy_opencode_cost = row.cost_is_provider.is_none()
                    && row.backend == "OpenCode Go"
                    && row.old_cost.is_some();
                if row.cost_is_provider == Some(true) || legacy_opencode_cost {
                    continue;
                }
                let recomputed = catalog_price(&prices, &row.model).map(|price| {
                    catalog_request_cost(
                        *price,
                        row.prompt_tokens,
                        row.completion_tokens,
                        row.cache_read_tokens,
                        row.cache_creation_tokens,
                    )
                });
                // Fill missing rows and heal stale estimates. Never erase an
                // existing cost when a model drops out of the catalog.
                let write = match (recomputed, row.old_cost) {
                    (Some(cost), None) => Some(cost),
                    (Some(cost), Some(old)) if (cost - old).abs() > 1e-12 => Some(cost),
                    _ => None,
                };
                if let Some(cost) = write {
                    update.execute((row.id, cost, Utc::now().to_rfc3339()))?;
                }
            }
        }
        tx.commit()?;
        Ok(rows.len())
    }

    /// Batch save/refresh catalog prices in one transaction. The `OpenRouter`
    /// catalog is hundreds of models; per-row autocommits (each an fsync on
    /// the UI task) made the post-load pause noticeable. The `WHERE` clause
    /// also skips rows whose price didn't move, so re-fetches write nothing.
    pub fn upsert_model_prices(&mut self, prices: &[(String, String, ModelPricing)]) -> Result<()> {
        if prices.is_empty() {
            return Ok(());
        }
        let tx = self.conn.transaction()?;
        {
            let mut stmt = tx.prepare(
                "INSERT INTO cache.model_prices (model_id, backend, prompt_price, completion_price,
                    cache_read_price, cache_write_price, updated_at)
                 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)
                 ON CONFLICT(model_id) DO UPDATE SET
                    prompt_price = excluded.prompt_price,
                    completion_price = excluded.completion_price,
                    cache_read_price = excluded.cache_read_price,
                    cache_write_price = excluded.cache_write_price,
                    updated_at = excluded.updated_at
                 WHERE cache.model_prices.prompt_price != excluded.prompt_price
                    OR cache.model_prices.completion_price != excluded.completion_price
                    OR cache.model_prices.cache_read_price IS NOT excluded.cache_read_price
                    OR cache.model_prices.cache_write_price IS NOT excluded.cache_write_price",
            )?;
            let now = Utc::now().to_rfc3339();
            for (model, backend, price) in prices {
                stmt.execute((
                    model,
                    backend,
                    price.prompt,
                    price.completion,
                    price.cache_read,
                    price.cache_write,
                    &now,
                ))?;
            }
        }
        tx.commit()?;
        Ok(())
    }

    /// Catalog prices for a model in USD per 1M tokens. Tries the exact
    /// `model_prices` row first (`OpenRouter` ids match directly); if there is
    /// none, falls back to the `OpenRouter` catalog entry for the same model —
    /// backend prefixes and the catalog's `vendor/` part are stripped. Other
    /// backends expose no pricing, so the matching `OpenRouter` list price is
    /// the best available estimate.
    pub fn model_price(&self, model: &str) -> Option<ModelPricing> {
        let read_price = |r: &rusqlite::Row| {
            Ok(ModelPricing {
                prompt: r.get(0)?,
                completion: r.get(1)?,
                cache_read: r.get(2)?,
                cache_write: r.get(3)?,
            })
        };
        if let Ok(price) = self.conn.query_row(
            "SELECT prompt_price, completion_price, cache_read_price, cache_write_price
             FROM cache.model_prices WHERE model_id = ?1",
            [model],
            read_price,
        ) {
            return Some(price);
        }
        let name = price_name(model);
        if name.is_empty() {
            return None;
        }
        // Suffix match on `vendor/name`: the shortest vendor wins when a
        // bare name appears under several vendors.
        self.conn
            .query_row(
                "SELECT prompt_price, completion_price, cache_read_price, cache_write_price
                 FROM cache.model_prices
                 WHERE backend = 'OpenRouter'
                   AND substr(model_id, -length(?1) - 1) = '/' || ?1
                 ORDER BY length(model_id) LIMIT 1",
                [name],
                read_price,
            )
            .ok()
    }

    /// Totals across logged requests, optionally limited to requests logged
    /// at or after `since` (RFC3339; `None` = all time). `created_at` is
    /// stored as fixed-width UTC RFC3339, so lexicographic comparison is a
    /// correct time filter.
    pub fn usage_totals(&self, since: Option<&str>) -> Result<UsageTotals> {
        let mut sql = String::from(
            "SELECT COUNT(*),
                    COALESCE(SUM(prompt_tokens), 0),
                    COALESCE(SUM(completion_tokens), 0),
                    COALESCE(SUM(cache_read_tokens), 0),
                    COALESCE(SUM(cache_creation_tokens), 0),
                    COALESCE(SUM(cost), 0)
             FROM usage_log",
        );
        if since.is_some() {
            sql.push_str(" WHERE created_at >= ?1");
        }
        let map = |r: &rusqlite::Row| {
            Ok(UsageTotals {
                requests: r.get::<_, i64>(0)? as u64,
                prompt_tokens: r.get::<_, i64>(1)? as u64,
                completion_tokens: r.get::<_, i64>(2)? as u64,
                cache_read_tokens: r.get::<_, i64>(3)? as u64,
                cache_creation_tokens: r.get::<_, i64>(4)? as u64,
                cost: r.get::<_, f64>(5)?,
            })
        };
        let totals = match since {
            Some(s) => self.conn.query_row(&sql, [s], map),
            None => self.conn.query_row(&sql, [], map),
        }?;
        Ok(totals)
    }

    /// Per-backend aggregates, most-used first. `since` filters the window
    /// (RFC3339 cutoff; `None` = all time).
    pub fn usage_by_backend(&self, since: Option<&str>) -> Result<Vec<UsageByBackend>> {
        let mut sql = String::from(
            "SELECT backend, COUNT(*),
                    COALESCE(SUM(prompt_tokens), 0),
                    COALESCE(SUM(completion_tokens), 0),
                    COALESCE(SUM(cache_read_tokens), 0),
                    COALESCE(SUM(cost), 0)
             FROM usage_log",
        );
        if since.is_some() {
            sql.push_str(" WHERE created_at >= ?1");
        }
        sql.push_str(" GROUP BY backend ORDER BY COUNT(*) DESC");
        let map = |r: &rusqlite::Row| {
            Ok(UsageByBackend {
                backend: r.get(0)?,
                requests: r.get::<_, i64>(1)? as u64,
                prompt_tokens: r.get::<_, i64>(2)? as u64,
                completion_tokens: r.get::<_, i64>(3)? as u64,
                cache_read_tokens: r.get::<_, i64>(4)? as u64,
                cost: r.get::<_, f64>(5)?,
            })
        };
        let mut stmt = self.conn.prepare(&sql)?;
        let rows = match since {
            Some(s) => stmt.query_map([s], map),
            None => stmt.query_map([], map),
        }?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    /// Per-model aggregates, most-used first. `since` filters the window
    /// (RFC3339 cutoff; `None` = all time).
    pub fn usage_by_model(&self, limit: u64, since: Option<&str>) -> Result<Vec<UsageByModel>> {
        let mut sql = String::from(
            "SELECT model, COUNT(*),
                    COALESCE(SUM(prompt_tokens), 0),
                    COALESCE(SUM(completion_tokens), 0),
                    COALESCE(SUM(cache_read_tokens), 0),
                    COALESCE(SUM(cost), 0)
             FROM usage_log",
        );
        if since.is_some() {
            sql.push_str(" WHERE created_at >= ?1");
            sql.push_str(" GROUP BY model ORDER BY COUNT(*) DESC LIMIT ?2");
        } else {
            sql.push_str(" GROUP BY model ORDER BY COUNT(*) DESC LIMIT ?1");
        }
        let map = |r: &rusqlite::Row| {
            Ok(UsageByModel {
                model: r.get(0)?,
                requests: r.get::<_, i64>(1)? as u64,
                prompt_tokens: r.get::<_, i64>(2)? as u64,
                completion_tokens: r.get::<_, i64>(3)? as u64,
                cache_read_tokens: r.get::<_, i64>(4)? as u64,
                cost: r.get::<_, f64>(5)?,
            })
        };
        let mut stmt = self.conn.prepare(&sql)?;
        let rows = match since {
            Some(s) => stmt.query_map(rusqlite::params![s, limit as i64], map),
            None => stmt.query_map(rusqlite::params![limit as i64], map),
        }?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    /// The most recent logged requests, newest first. `since` filters the
    /// window (RFC3339 cutoff; `None` = all time).
    pub fn usage_recent(&self, limit: u64, since: Option<&str>) -> Result<Vec<UsageRow>> {
        let mut sql = String::from(
            "SELECT created_at, backend, model, prompt_tokens, completion_tokens,
                    cache_read_tokens, cost
             FROM usage_log",
        );
        if since.is_some() {
            sql.push_str(" WHERE created_at >= ?1");
            sql.push_str(" ORDER BY id DESC LIMIT ?2");
        } else {
            sql.push_str(" ORDER BY id DESC LIMIT ?1");
        }
        let map = |r: &rusqlite::Row| {
            Ok(UsageRow {
                created_at: r.get(0)?,
                backend: r.get(1)?,
                model: r.get(2)?,
                prompt_tokens: r.get::<_, i64>(3)? as u64,
                completion_tokens: r.get::<_, i64>(4)? as u64,
                cache_read_tokens: r.get::<_, i64>(5)? as u64,
                cost: r.get(6)?,
            })
        };
        let mut stmt = self.conn.prepare(&sql)?;
        let rows = match since {
            Some(s) => stmt.query_map(rusqlite::params![s, limit as i64], map),
            None => stmt.query_map(rusqlite::params![limit as i64], map),
        }?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }

    /// Per-day aggregates (`created_at` is RFC 3339, so its first 10 chars
    /// are the date), newest day first — the CLI's `usage --by-day`.
    pub fn usage_by_day(&self, limit: u64, since: Option<&str>) -> Result<Vec<UsageDay>> {
        let mut sql = String::from(
            "SELECT substr(created_at, 1, 10) AS day, COUNT(*),
                    COALESCE(SUM(prompt_tokens), 0),
                    COALESCE(SUM(completion_tokens), 0),
                    COALESCE(SUM(cache_read_tokens), 0),
                    COALESCE(SUM(cost), 0)
             FROM usage_log",
        );
        if since.is_some() {
            sql.push_str(" WHERE created_at >= ?1");
            sql.push_str(" GROUP BY day ORDER BY day DESC LIMIT ?2");
        } else {
            sql.push_str(" GROUP BY day ORDER BY day DESC LIMIT ?1");
        }
        let map = |r: &rusqlite::Row| {
            Ok(UsageDay {
                day: r.get(0)?,
                requests: r.get::<_, i64>(1)? as u64,
                prompt_tokens: r.get::<_, i64>(2)? as u64,
                completion_tokens: r.get::<_, i64>(3)? as u64,
                cache_read_tokens: r.get::<_, i64>(4)? as u64,
                cost: r.get(5)?,
            })
        };
        let mut stmt = self.conn.prepare(&sql)?;
        let rows = match since {
            Some(s) => stmt.query_map(rusqlite::params![s, limit as i64], map),
            None => stmt.query_map(rusqlite::params![limit as i64], map),
        }?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }
}

// --- sync groundwork (Phase 3 consumes this) ---

/// One peer's sync cursor for one table. Cursors are opaque strings; for
/// append-only tables they are `(created_at, id)` tuples (so equal
/// timestamps don't collide), never naked timestamps.
/// Phase 3's merge engine reads/writes these; kept live from day one so
/// the schema and identity can't drift.
#[allow(dead_code)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SyncState {
    pub peer_id: String,
    pub table_name: String,
    pub pull_cursor: Option<String>,
    pub push_cursor: Option<String>,
    pub last_synced_at: Option<String>,
}

/// Sync identity + cursor bookkeeping for the Phase 3 merge engine.
#[allow(dead_code)]
impl Db {
    /// This device's stable id, created on first use. Sync identity for
    /// everything this device writes (tombstones, LWW tie-breaks on
    /// `updated_at + device_id` in Phase 3).
    pub fn device_id(&self) -> Result<String> {
        if let Some(id) = self
            .conn
            .query_row("SELECT device_id FROM device_meta LIMIT 1", [], |r| {
                r.get(0)
            })
            .optional()?
        {
            return Ok(id);
        }
        let id = Uuid::new_v4().to_string();
        let now = Utc::now().to_rfc3339();
        self.conn.execute(
            "INSERT INTO device_meta (device_id, created_at) VALUES (?1, ?2)",
            (&id, &now),
        )?;
        Ok(id)
    }

    /// Store (or update) a peer's cursors for one table, stamping
    /// `last_synced_at`. `None` leaves an existing cursor untouched.
    pub fn set_sync_state(
        &self,
        peer_id: &str,
        table_name: &str,
        pull_cursor: Option<&str>,
        push_cursor: Option<&str>,
    ) -> Result<()> {
        self.conn.execute(
            "INSERT INTO sync_state (peer_id, table_name, pull_cursor, push_cursor, last_synced_at)
             VALUES (?1, ?2, ?3, ?4, ?5)
             ON CONFLICT(peer_id, table_name) DO UPDATE SET
                pull_cursor = COALESCE(?3, pull_cursor),
                push_cursor = COALESCE(?4, push_cursor),
                last_synced_at = ?5",
            (
                peer_id,
                table_name,
                pull_cursor,
                push_cursor,
                Utc::now().to_rfc3339(),
            ),
        )?;
        Ok(())
    }

    pub fn load_sync_state(&self) -> Result<Vec<SyncState>> {
        let mut stmt = self.conn.prepare(
            "SELECT peer_id, table_name, pull_cursor, push_cursor, last_synced_at
             FROM sync_state ORDER BY peer_id, table_name",
        )?;
        let rows = stmt.query_map([], |r| {
            Ok(SyncState {
                peer_id: r.get(0)?,
                table_name: r.get(1)?,
                pull_cursor: r.get(2)?,
                push_cursor: r.get(3)?,
                last_synced_at: r.get(4)?,
            })
        })?;
        Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
    }
}

/// Quote a query for FTS5 MATCH: each whitespace token becomes a quoted
/// phrase (inner quotes doubled), so model-supplied text can't be an FTS
/// syntax error. Tokens are implicitly `ANDed` by FTS5.
pub fn fts_quote(query: &str) -> String {
    query
        .split_whitespace()
        .map(|t| format!("\"{}\"", t.replace('"', "\"\"")))
        .collect::<Vec<_>>()
        .join(" ")
}

/// BM25-ranked chunk search within one space: `(file name, location, snippet)`.
pub fn search_chunks(
    conn: &Connection,
    space_id: &str,
    query: &str,
    limit: usize,
) -> Result<Vec<(String, String, String)>> {
    let q = fts_quote(query);
    if q.is_empty() {
        return Ok(Vec::new());
    }
    let mut stmt = conn.prepare(
        "SELECT files.name, cache.file_chunks.location,
                snippet(file_chunks, 3, '', '', '…', 24)
         FROM cache.file_chunks JOIN files ON files.id = cache.file_chunks.file_id
         WHERE file_chunks MATCH ?1 AND files.space_id = ?2
         ORDER BY bm25(file_chunks) LIMIT ?3",
    )?;
    let rows = stmt.query_map((q, space_id, limit as i64), |r| {
        Ok((r.get(0)?, r.get(1)?, r.get(2)?))
    })?;
    Ok(rows.collect::<rusqlite::Result<Vec<_>>>()?)
}

/// A file's full extracted text (chunks re-joined in order), by display name.
pub fn file_text(conn: &Connection, space_id: &str, name: &str) -> Result<Option<String>> {
    let mut stmt = conn.prepare(
        "SELECT cache.file_chunks.text
         FROM cache.file_chunks JOIN files ON files.id = cache.file_chunks.file_id
         WHERE files.space_id = ?1 AND files.name = ?2
         ORDER BY CAST(cache.file_chunks.seq AS INTEGER) ASC",
    )?;
    let rows = stmt.query_map((space_id, name), |r| r.get::<_, String>(0))?;
    let parts = rows.collect::<rusqlite::Result<Vec<_>>>()?;
    Ok((!parts.is_empty()).then(|| parts.join("\n")))
}

pub fn count_files(conn: &Connection, space_id: &str) -> Result<u64> {
    let n: i64 = conn.query_row(
        "SELECT COUNT(*) FROM files WHERE space_id = ?1",
        [space_id],
        |r| r.get(0),
    )?;
    Ok(n as u64)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn price(prompt: f64, completion: f64) -> ModelPricing {
        ModelPricing {
            prompt,
            completion,
            cache_read: None,
            cache_write: None,
        }
    }

    fn cache_price(
        prompt: f64,
        completion: f64,
        cache_read: f64,
        cache_write: f64,
    ) -> ModelPricing {
        ModelPricing {
            prompt,
            completion,
            cache_read: Some(cache_read),
            cache_write: Some(cache_write),
        }
    }

    #[test]
    fn is_fresh_true_under_24h_false_over() {
        let now = Utc::now();
        let recent = (now - chrono::Duration::hours(1)).to_rfc3339();
        let stale = (now - chrono::Duration::hours(25)).to_rfc3339();
        assert!(is_fresh(&recent, now));
        assert!(!is_fresh(&stale, now));
        assert!(!is_fresh("not a timestamp", now)); // unparseable = not fresh
    }

    #[test]
    fn usage_log_round_trips_and_aggregates() {
        let mut db = Db::open_in_memory().unwrap();
        db.upsert_model_prices(&[(
            "anthropic/claude-3.5-sonnet".to_string(),
            "OpenRouter".to_string(),
            price(3.0, 15.0),
        )])
        .unwrap();
        assert_eq!(
            db.model_price("anthropic/claude-3.5-sonnet"),
            Some(price(3.0, 15.0))
        );
        // Re-upsert refreshes every rate rather than duplicating.
        db.upsert_model_prices(&[(
            "anthropic/claude-3.5-sonnet".to_string(),
            "OpenRouter".to_string(),
            cache_price(4.0, 16.0, 0.4, 5.0),
        )])
        .unwrap();
        assert_eq!(
            db.model_price("anthropic/claude-3.5-sonnet"),
            Some(cache_price(4.0, 16.0, 0.4, 5.0))
        );
        assert_eq!(db.model_price("unknown/model"), None);

        // 100 prompt @ $4/1M + 10 completion @ $16/1M = $0.0004 + $0.00016.
        db.log_usage(
            "OpenRouter",
            "anthropic/claude-3.5-sonnet",
            100,
            10,
            70,
            20,
            Some(0.00056),
            true,
            Some("s1"),
            Some("space-a"),
        )
        .unwrap();
        db.log_usage(
            "Codex",
            "gpt-5.1-codex",
            50,
            5,
            0,
            0,
            None,
            false,
            None,
            None,
        )
        .unwrap();

        let totals = db.usage_totals(None).unwrap();
        assert_eq!(totals.requests, 2);
        assert_eq!(totals.prompt_tokens, 150);
        assert_eq!(totals.completion_tokens, 15);
        assert_eq!(totals.cache_read_tokens, 70);
        assert_eq!(totals.cache_creation_tokens, 20);
        assert!((totals.cost - 0.00056).abs() < 1e-9);

        let by_backend = db.usage_by_backend(None).unwrap();
        assert_eq!(by_backend.len(), 2);
        assert_eq!(by_backend[0].backend, "OpenRouter"); // most-used first
        assert_eq!(by_backend[0].requests, 1);

        let by_model = db.usage_by_model(5, None).unwrap();
        assert_eq!(by_model.len(), 2);
        assert!(by_model.iter().any(|m| m.model == "gpt-5.1-codex"));

        let recent = db.usage_recent(10, None).unwrap();
        assert_eq!(recent.len(), 2);
        assert_eq!(recent[0].model, "gpt-5.1-codex"); // newest first
        assert_eq!(recent[0].cost, None);
        assert_eq!(recent[1].cache_read_tokens, 70);
    }

    #[test]
    fn usage_queries_filter_by_since_window() {
        let db = Db::open_in_memory().unwrap();
        // Rows with explicit timestamps (raw insert: log_usage stamps now).
        let insert = |created: &str| {
            db.raw().execute(
                "INSERT INTO usage_log (sync_id, created_at, session_id, backend, model,
                    prompt_tokens, completion_tokens, cache_read_tokens, cache_creation_tokens, cost)
                 VALUES (?1, ?2, NULL, 'OpenRouter', 'a/model', 100, 10, 0, 0, 0.001)",
                (uuid::Uuid::new_v4().to_string(), created),
            )
        };
        insert("2026-01-01T00:00:00+00:00").unwrap();
        insert("2026-01-02T00:00:00+00:00").unwrap();
        insert("2026-01-03T00:00:00+00:00").unwrap();

        let since = Some("2026-01-02T00:00:00+00:00");
        let totals = db.usage_totals(since).unwrap();
        assert_eq!(totals.requests, 2);
        assert_eq!(totals.prompt_tokens, 200);
        assert!((totals.cost - 0.002).abs() < 1e-12);
        assert_eq!(db.usage_totals(None).unwrap().requests, 3);
        assert_eq!(db.usage_by_backend(since).unwrap()[0].requests, 2);
        assert_eq!(db.usage_by_model(5, since).unwrap()[0].requests, 2);
        let recent = db.usage_recent(10, since).unwrap();
        assert_eq!(recent.len(), 2);
        assert_eq!(recent[0].created_at, "2026-01-03T00:00:00+00:00");
        // Boundary is inclusive: the exact-cutoff row is included.
        assert!(
            recent
                .iter()
                .any(|r| r.created_at == "2026-01-02T00:00:00+00:00")
        );
    }

    #[test]
    fn usage_range_cycles_and_persists() {
        use crate::db::UsageRange;
        assert_eq!(UsageRange::Day.next(), UsageRange::Week);
        assert_eq!(UsageRange::All.next(), UsageRange::Day);
        assert_eq!(UsageRange::Day.prev(), UsageRange::All);
        assert_eq!(UsageRange::from_key("month"), UsageRange::Month);
        assert_eq!(UsageRange::from_key("bogus"), UsageRange::All);
        assert_eq!(UsageRange::Week.key(), "week");
        assert_eq!(UsageRange::Day.title(), "last 24 hours");
        assert!(UsageRange::All.since().is_none());
        assert!(UsageRange::Day.since().is_some());
    }

    #[test]
    fn backfill_usage_costs_recomputes_history_from_catalog() {
        let mut db = Db::open_in_memory().unwrap();
        db.upsert_model_prices(&[(
            "anthropic/claude-3.5-sonnet".to_string(),
            "OpenRouter".to_string(),
            cache_price(3.0, 15.0, 0.3, 3.75),
        )])
        .unwrap();
        // Rows logged before pricing existed: one priced model (NULL cost),
        // one model with no catalog entry.
        db.log_usage(
            "OpenRouter",
            "anthropic/claude-3.5-sonnet",
            100,
            10,
            70,
            20,
            None,
            false,
            None,
            None,
        )
        .unwrap();
        db.log_usage(
            "Codex",
            "gpt-5.1-codex",
            50,
            5,
            0,
            0,
            None,
            false,
            None,
            None,
        )
        .unwrap();

        let visited = db.backfill_usage_costs().unwrap();
        assert_eq!(visited, 2);
        // 10 ordinary @ $3/M + 70 reads @ $0.30/M + 20 writes @ $3.75/M,
        // plus 10 completion @ $15/M = $0.000276.
        let totals = db.usage_totals(None).unwrap();
        assert!((totals.cost - 0.000_276).abs() < 1e-12);
        let recent = db.usage_recent(10, None).unwrap();
        assert!((recent[1].cost.unwrap() - 0.000_276).abs() < 1e-12); // priced row filled in
        assert_eq!(recent[0].cost, None); // unknown price stays unknown

        // Idempotent: a second pass leaves the values untouched.
        db.backfill_usage_costs().unwrap();
        assert!((db.usage_totals(None).unwrap().cost - 0.000_276).abs() < 1e-12);
    }

    #[test]
    fn backfill_preserves_provider_reported_cost() {
        let mut db = Db::open_in_memory().unwrap();
        db.upsert_model_prices(&[(
            "anthropic/claude-3.5-sonnet".to_string(),
            "OpenRouter".to_string(),
            cache_price(3.0, 15.0, 0.3, 3.75),
        )])
        .unwrap();
        db.log_usage(
            "OpenRouter",
            "anthropic/claude-3.5-sonnet",
            100,
            10,
            70,
            20,
            Some(0.000_321),
            true,
            None,
            None,
        )
        .unwrap();

        db.backfill_usage_costs().unwrap();

        assert_eq!(db.usage_recent(1, None).unwrap()[0].cost, Some(0.000_321));
    }

    #[test]
    fn backfill_usage_costs_heals_per_token_catalog() {
        // A legacy catalog holding the endpoint's raw per-token values
        // (deepseek-v4-flash at $0.08/M stores 8e-08) must be scaled to the
        // per-1M convention before costs are computed against it.
        let mut db = Db::open_in_memory().unwrap();
        db.upsert_model_prices(&[(
            "deepseek/deepseek-v4-flash-0731".to_string(),
            "OpenRouter".to_string(),
            price(8e-08, 1.8e-07),
        )])
        .unwrap();
        db.log_usage(
            "OpenRouter",
            "deepseek/deepseek-v4-flash-0731",
            122_221,
            672,
            118_784,
            0,
            Some(9.89864e-09), // the old, 1e6×-too-small value
            false,
            None,
            None,
        )
        .unwrap();

        db.backfill_usage_costs().unwrap();

        assert_eq!(
            db.model_price("deepseek/deepseek-v4-flash-0731"),
            Some(price(0.08, 0.18))
        );
        // 122221/1e6 × 0.08 + 672/1e6 × 0.18 ≈ $0.00989.
        let recent = db.usage_recent(10, None).unwrap();
        let cost = recent[0].cost.unwrap();
        assert!((cost - 0.009_898_6).abs() < 1e-6, "cost was {cost}");
        assert!(cost > 0.009, "cost was {cost}");
    }

    #[test]
    fn request_cost_prices_tokens_against_catalog() {
        let mut db = Db::open_in_memory().unwrap();
        assert_eq!(db.request_cost("unknown/model", 100, 10, 0, 0), None);
        db.upsert_model_prices(&[(
            "anthropic/claude-3.5-sonnet".to_string(),
            "OpenRouter".to_string(),
            cache_price(3.0, 15.0, 0.3, 3.75),
        )])
        .unwrap();
        let cost = db
            .request_cost("anthropic/claude-3.5-sonnet", 100, 10, 70, 20)
            .unwrap();
        assert!((cost - 0.000_276).abs() < 1e-12);
    }

    #[test]
    fn model_price_cross_references_openrouter_catalog_twins() {
        let mut db = Db::open_in_memory().unwrap();
        db.upsert_model_prices(&[
            (
                "deepseek/deepseek-v4-flash".to_string(),
                "OpenRouter".to_string(),
                cache_price(0.08, 0.18, 0.016, 0.08),
            ),
            (
                "openai/gpt-5".to_string(),
                "OpenRouter".to_string(),
                cache_price(1.25, 10.0, 0.125, 1.25),
            ),
        ])
        .unwrap();
        // Exact ids hit directly; other backends' prefixed/bare ids resolve
        // through the vendor/name twin.
        assert_eq!(
            db.model_price("deepseek/deepseek-v4-flash"),
            Some(cache_price(0.08, 0.18, 0.016, 0.08))
        );
        assert_eq!(
            db.model_price("go:deepseek-v4-flash"),
            Some(cache_price(0.08, 0.18, 0.016, 0.08))
        );
        assert_eq!(
            db.model_price("deepseek-v4-flash"),
            Some(cache_price(0.08, 0.18, 0.016, 0.08))
        );
        assert_eq!(
            db.model_price("openai:gpt-5"),
            Some(cache_price(1.25, 10.0, 0.125, 1.25))
        );
        assert_eq!(
            db.model_price("codex:gpt-5"),
            Some(cache_price(1.25, 10.0, 0.125, 1.25))
        );
        // No twin anywhere: unknown.
        assert_eq!(db.model_price("no-such-model-anywhere"), None);
        // The price flows into per-request costs for the other backend.
        let cost = db
            .request_cost("go:deepseek-v4-flash", 100, 10, 70, 0)
            .unwrap();
        assert!((cost - 0.000_005_32).abs() < 1e-15);
    }

    #[test]
    fn price_name_strips_backend_prefixes_and_vendors() {
        assert_eq!(price_name("go:deepseek-v4-flash"), "deepseek-v4-flash");
        assert_eq!(price_name("openai:gpt-5"), "gpt-5");
        assert_eq!(price_name("codex:gpt-5.1-codex"), "gpt-5.1-codex");
        assert_eq!(price_name("opencode:qwen3.6-plus"), "qwen3.6-plus");
        assert_eq!(
            price_name("deepseek/deepseek-v4-flash"),
            "deepseek-v4-flash"
        );
        assert_eq!(price_name("gpt-5"), "gpt-5");
    }

    #[test]
    fn backfill_prices_non_openrouter_models_via_catalog_twins() {
        let mut db = Db::open_in_memory().unwrap();
        db.upsert_model_prices(&[(
            "deepseek/deepseek-v4-flash".to_string(),
            "OpenRouter".to_string(),
            cache_price(0.08, 0.18, 0.016, 0.08),
        )])
        .unwrap();
        // OpenCode Go rows logged with no cost — the flat-fee backend has
        // no pricing of its own, so the twin's list price is the estimate.
        db.log_usage(
            "OpenCode Go",
            "go:deepseek-v4-flash",
            100,
            10,
            0,
            0,
            None,
            false,
            None,
            None,
        )
        .unwrap();

        db.backfill_usage_costs().unwrap();

        let recent = db.usage_recent(10, None).unwrap();
        let cost = recent[0].cost.unwrap();
        assert!((cost - 0.000_009_8).abs() < 1e-15, "cost was {cost}");
        assert!((db.usage_totals(None).unwrap().cost - 0.000_009_8).abs() < 1e-15);
    }

    #[test]
    fn web_cache_roundtrips_and_updates_on_rewrite() {
        let db = Db::open_in_memory().unwrap();
        assert!(cache_get(db.raw(), "example.com/a").unwrap().is_none());
        cache_put(
            db.raw(),
            "example.com/a",
            "https://example.com/a",
            Some("Title"),
            "body text",
        )
        .unwrap();
        let (title, text, fetched_at) = cache_get(db.raw(), "example.com/a").unwrap().unwrap();
        assert_eq!(title, "Title");
        assert_eq!(text, "body text");
        assert!(!fetched_at.is_empty());

        // Re-fetching overwrites the row, not duplicates it.
        cache_put(
            db.raw(),
            "example.com/a",
            "https://example.com/a",
            None,
            "new body",
        )
        .unwrap();
        let (title, text, _) = cache_get(db.raw(), "example.com/a").unwrap().unwrap();
        assert_eq!(title, "");
        assert_eq!(text, "new body");
    }

    #[test]
    fn web_mode_defaults_off_and_toggles() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        assert!(!s.web_mode);
        db.set_session_web_mode(&s.id, true).unwrap();
        assert!(db.list_sessions(&space).unwrap()[0].web_mode);
    }

    #[test]
    fn swarm_mode_defaults_off_and_toggles() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        assert!(!s.swarm_mode);
        db.set_session_swarm_mode(&s.id, true).unwrap();
        assert!(db.list_sessions(&space).unwrap()[0].swarm_mode);
        assert!(db.get_session(&s.id).unwrap().unwrap().swarm_mode);
    }

    #[test]
    fn swarm_personas_roundtrip_and_replace_all_on_save() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        assert!(db.list_swarm_personas(&s.id).unwrap().is_empty());

        let roster = vec![
            Persona {
                name: "Skeptic".into(),
                model: "a/one".into(),
                blurb: "pokes holes".into(),
            },
            Persona {
                name: "Advocate".into(),
                model: "b/two".into(),
                blurb: "user-first".into(),
            },
        ];
        db.save_swarm_personas(&s.id, &roster).unwrap();
        let loaded = db.list_swarm_personas(&s.id).unwrap();
        assert_eq!(loaded.len(), 2);
        assert_eq!(loaded[0].name, "Skeptic");
        assert_eq!(loaded[1].name, "Advocate");

        // A second save fully replaces the roster, not appends.
        db.save_swarm_personas(&s.id, &roster[..1]).unwrap();
        assert_eq!(db.list_swarm_personas(&s.id).unwrap().len(), 1);
    }

    #[test]
    fn persona_message_tags_role_assistant_with_persona_and_model() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        db.add_persona_message(&s.id, "reply text", "Skeptic", "a/one")
            .unwrap();
        let msgs = db.load_messages(&s.id).unwrap();
        assert_eq!(msgs.len(), 1);
        assert_eq!(msgs[0].role, "assistant");
        assert_eq!(msgs[0].persona.as_deref(), Some("Skeptic"));
        assert_eq!(msgs[0].model.as_deref(), Some("a/one"));

        // An ordinary assistant message has no persona tag.
        db.add_assistant_message(&s.id, "final answer", None, None, None, None, None, None)
            .unwrap();
        let msgs = db.load_messages(&s.id).unwrap();
        assert_eq!(msgs[1].persona, None);
    }

    #[test]
    fn session_sources_link_to_the_web_cache_and_are_keyword_searchable() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        cache_put(
            db.raw(),
            "https://example.com/a",
            "https://example.com/a",
            Some("A"),
            "rust borrow checker deep dive",
        )
        .unwrap();
        cache_put(
            db.raw(),
            "https://example.com/b",
            "https://example.com/b",
            Some("B"),
            "cooking pasta recipes",
        )
        .unwrap();
        db.add_session_sources(
            &s.id,
            &[
                "https://example.com/a".to_string(),
                "https://example.com/b".to_string(),
            ],
        )
        .unwrap();

        let hits = db.search_session_sources(&s.id, "borrow checker").unwrap();
        assert_eq!(hits.len(), 1);
        assert!(hits[0].1.contains("borrow checker"));

        assert!(
            db.search_session_sources(&s.id, "quantum")
                .unwrap()
                .is_empty()
        );
    }

    #[test]
    fn set_source_flag_pins_and_discards_then_clears() {
        let db = Db::open_in_memory().unwrap();
        let session_id = "sess-1";
        add_session_sources(&db.conn, session_id, &["https://a.example/x".to_string()]).unwrap();
        db.set_source_flag(session_id, "https://a.example/x", Some("pinned"))
            .unwrap();
        assert_eq!(
            pinned_urls(&db.conn, session_id).unwrap(),
            vec!["https://a.example/x".to_string()]
        );
        assert!(discarded_domains(&db.conn, session_id).unwrap().is_empty());

        db.set_source_flag(session_id, "https://a.example/x", Some("discarded"))
            .unwrap();
        assert!(pinned_urls(&db.conn, session_id).unwrap().is_empty());
        assert_eq!(
            discarded_domains(&db.conn, session_id).unwrap(),
            vec!["a.example".to_string()]
        );

        db.set_source_flag(session_id, "https://a.example/x", None)
            .unwrap();
        assert!(discarded_domains(&db.conn, session_id).unwrap().is_empty());
    }

    #[test]
    fn upsert_research_stage_message_replaces_the_same_labels_row() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        db.upsert_research_stage_message(&s.id, "searching", "round 1, 1/3")
            .unwrap();
        db.upsert_research_stage_message(&s.id, "searching", "round 1, 2/3")
            .unwrap();
        db.upsert_research_stage_message(&s.id, "planning", "")
            .unwrap();

        let msgs = db.load_messages(&s.id).unwrap();
        let searching: Vec<_> = msgs
            .iter()
            .filter(|m| m.content.starts_with("searching:"))
            .collect();
        assert_eq!(searching.len(), 1, "expected one row, updated in place");
        assert!(searching[0].content.contains("2/3"));
        assert_eq!(msgs.iter().filter(|m| m.content == "planning").count(), 1);
    }

    #[test]
    fn session_and_message_roundtrip() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db
            .create_session("hello", "openai/gpt-4o", &space, "chat")
            .unwrap();

        db.add_user_message(&s.id, "hi").unwrap();
        db.add_assistant_message(
            &s.id,
            "hello there",
            Some("openai/gpt-4o"),
            Some("let me think"),
            Some(3),
            Some(1.5),
            Some(0.0042),
            Some("Vibed"),
        )
        .unwrap();

        let msgs = db.load_messages(&s.id).unwrap();
        assert_eq!(msgs.len(), 2);
        assert_eq!(msgs[0].role, "user");
        assert_eq!(msgs[1].content, "hello there");
        assert_eq!(msgs[1].model.as_deref(), Some("openai/gpt-4o"));
        assert_eq!(msgs[1].reasoning.as_deref(), Some("let me think"));
        assert_eq!(msgs[1].tokens, Some(3));
        assert_eq!(msgs[1].cost, Some(0.0042));

        let sessions = db.list_sessions(&space).unwrap();
        assert_eq!(sessions.len(), 1);
        assert_eq!(sessions[0].id, s.id);
    }

    #[test]
    fn markdown_images_in_content_roundtrip() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        let content = "look at ![this](img.png) and ![that](other.png)";
        db.add_user_message(&s.id, content).unwrap();
        let msgs = db.load_messages(&s.id).unwrap();
        assert_eq!(msgs.len(), 1);
        assert!(msgs[0].content.contains("![this](img.png)"));
        assert!(msgs[0].content.contains("![that](other.png)"));
    }

    #[test]
    fn model_prefs_toggle_and_used() {
        let db = Db::open_in_memory().unwrap();
        assert!(db.toggle_favorite("a/one").unwrap()); // now favorite
        assert!(!db.toggle_favorite("a/one").unwrap()); // toggled off
        db.mark_model_used("a/one").unwrap();
        db.set_reasoning("a/one", Some("high")).unwrap();

        let prefs = db.load_model_prefs().unwrap();
        let p = &prefs[0];
        assert_eq!(p.id, "a/one");
        assert!(!p.favorite);
        assert!(p.last_used.is_some());
        assert_eq!(p.reasoning.as_deref(), Some("high"));
    }

    #[test]
    fn settings_roundtrip() {
        let db = Db::open_in_memory().unwrap();
        db.set_setting("temperature", "0.7").unwrap();
        db.set_setting("temperature", "0.9").unwrap(); // upsert
        let s = db.load_settings().unwrap();
        assert_eq!(s, vec![("temperature".to_string(), "0.9".to_string())]);
    }

    #[test]
    fn set_model_updates_row() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        db.set_session_model(&s.id, "c/d").unwrap();
        assert_eq!(db.list_sessions(&space).unwrap()[0].model, "c/d");
    }

    #[test]
    fn compaction_persists_and_roundtrips() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        assert_eq!(s.compact_summary, None);
        assert_eq!(s.compact_through, 0);

        db.set_compaction(&s.id, "digest of earlier turns", 6)
            .unwrap();
        let reloaded = &db.list_sessions(&space).unwrap()[0];
        assert_eq!(
            reloaded.compact_summary.as_deref(),
            Some("digest of earlier turns")
        );
        assert_eq!(reloaded.compact_through, 6);
    }

    #[test]
    fn spaces_crud_and_session_reassignment_on_delete() {
        let db = Db::open_in_memory().unwrap();
        let spaces = db.list_spaces().unwrap();
        assert_eq!(spaces.len(), 1);
        assert_eq!(spaces[0].name, DEFAULT_SPACE);

        let work = db.create_space("work").unwrap();
        let s = db.create_session("hi", "a/b", &work.id, "chat").unwrap();
        assert_eq!(db.count_sessions(&work.id).unwrap(), 1);

        db.rename_space(&work.id, "work-renamed").unwrap();
        assert!(
            db.list_spaces()
                .unwrap()
                .iter()
                .any(|s| s.name == "work-renamed")
        );

        db.delete_space(&work.id).unwrap();
        assert_eq!(db.list_spaces().unwrap().len(), 1); // work is gone
        let default_id = db.default_space_id().unwrap();
        let moved = db.list_sessions(&default_id).unwrap();
        assert!(moved.iter().any(|c| c.id == s.id)); // session survived, moved to default
    }

    #[test]
    fn chunk_embeddings_store_rank_and_invalidate() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let id = db.upsert_file(&space, "book.pdf", "h1", 10, "ok").unwrap();
        db.set_file_chunks(
            &id,
            &[
                ("page 1".into(), "cooking with fire".into()),
                ("page 2".into(), "quantum entanglement".into()),
            ],
        )
        .unwrap();

        // Blob codec roundtrip.
        let v = vec![0.25f32, -1.0, 3.5];
        assert_eq!(blob_to_vec(&vec_to_blob(&v)), v);

        // No vectors yet → file needs embedding.
        assert_eq!(
            files_missing_embeddings(&db.conn, &space).unwrap(),
            vec![id.clone()]
        );

        db.set_chunk_embeddings(&id, &[(0, vec![1.0, 0.0]), (1, vec![0.0, 1.0])])
            .unwrap();
        assert!(
            files_missing_embeddings(&db.conn, &space)
                .unwrap()
                .is_empty()
        );

        // Query near the second chunk's vector ranks it first.
        let hits = semantic_chunks(&db.conn, &space, &[0.1, 0.9], 5).unwrap();
        assert_eq!(hits[0].1, "page 2");
        assert!(hits[0].2.contains("quantum"));
        assert!(hits[0].3 > hits[1].3, "scores must be descending");

        // Dimension-mismatched vectors are skipped, not an error.
        let hits = semantic_chunks(&db.conn, &space, &[1.0, 0.0, 0.0], 5).unwrap();
        assert!(hits.is_empty());

        // Rewriting chunks invalidates stale vectors.
        db.set_file_chunks(&id, &[("page 1".into(), "new text".into())])
            .unwrap();
        assert_eq!(
            files_missing_embeddings(&db.conn, &space).unwrap(),
            vec![id.clone()]
        );
    }

    #[test]
    fn files_upsert_list_delete_roundtrip() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let id = db.upsert_file(&space, "notes.md", "h1", 10, "ok").unwrap();
        db.set_file_chunks(&id, &[("lines 1-40".into(), "hello fts world".into())])
            .unwrap();

        let files = db.list_files(&space).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].name, "notes.md");
        assert_eq!(files[0].hash, "h1");
        assert_eq!(files[0].status, "ok");

        // Re-import with a new hash keeps one row (same id or replaced) and replaces chunks.
        let id2 = db.upsert_file(&space, "notes.md", "h2", 12, "ok").unwrap();
        db.set_file_chunks(&id2, &[("lines 1-40".into(), "goodbye".into())])
            .unwrap();
        let files = db.list_files(&space).unwrap();
        assert_eq!(files.len(), 1);
        assert_eq!(files[0].hash, "h2");

        db.delete_file(&files[0].id).unwrap();
        assert!(db.list_files(&space).unwrap().is_empty());
    }

    #[test]
    fn chunk_search_ranks_and_scopes_by_space() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let other = db.create_space("other").unwrap();
        let a = db.upsert_file(&space, "a.md", "h", 1, "ok").unwrap();
        let b = db.upsert_file(&other.id, "b.md", "h", 1, "ok").unwrap();
        db.set_file_chunks(&a, &[("lines 1-40".into(), "rust borrow checker".into())])
            .unwrap();
        db.set_file_chunks(&b, &[("lines 1-40".into(), "rust in other space".into())])
            .unwrap();

        let hits = search_chunks(&db.conn, &space, "rust", 8).unwrap();
        assert_eq!(hits.len(), 1); // other space's chunk is excluded
        assert_eq!(hits[0].0, "a.md");
        assert_eq!(hits[0].1, "lines 1-40");
        assert!(hits[0].2.contains("rust"));

        // Special characters must not be an FTS syntax error.
        assert!(search_chunks(&db.conn, &space, "c++ \"quoted\" -dash", 8).is_ok());
    }

    #[test]
    fn file_text_joins_chunks_in_order() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let id = db.upsert_file(&space, "doc.txt", "h", 1, "ok").unwrap();
        db.set_file_chunks(
            &id,
            &[
                ("lines 1-2".into(), "one\ntwo".into()),
                ("lines 3-4".into(), "three\nfour".into()),
            ],
        )
        .unwrap();
        let text = file_text(&db.conn, &space, "doc.txt").unwrap().unwrap();
        assert_eq!(text, "one\ntwo\nthree\nfour");
        assert!(
            file_text(&db.conn, &space, "missing.txt")
                .unwrap()
                .is_none()
        );
        assert_eq!(count_files(&db.conn, &space).unwrap(), 1);
    }

    #[test]
    fn research_stage_messages_round_trip() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        db.add_research_stage_message(&s.id, "planning…").unwrap();
        let msgs = db.load_messages(&s.id).unwrap();
        assert_eq!(msgs.last().unwrap().role, "research_stage");
        assert_eq!(msgs.last().unwrap().content, "planning…");
    }

    #[test]
    fn survey_messages_round_trip() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        db.add_survey_message(&s.id, "For \"topic\":\n 1. Depth or breadth?")
            .unwrap();
        let msgs = db.load_messages(&s.id).unwrap();
        let last = msgs.last().unwrap();
        assert_eq!(last.role, "survey");
        assert!(last.content.contains("Depth or breadth?"));
    }

    #[test]
    fn gate_reply_round_trip() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let s = db.create_session("t", "a/b", &space, "chat").unwrap();
        db.add_gate_reply_message(&s.id, "the second option")
            .unwrap();
        let msgs = db.load_messages(&s.id).unwrap();
        let last = msgs.last().unwrap();
        assert_eq!(last.role, "gate_reply");
        assert_eq!(last.content, "the second option");
    }

    #[test]
    fn create_list_touch_delete_watch_roundtrip() {
        let db = Db::open_in_memory().unwrap();
        let id = db
            .create_watch("space-1", "rust async runtimes", 24, "sess-1")
            .unwrap();
        let watches = db.list_watches("space-1").unwrap();
        assert_eq!(watches.len(), 1);
        assert_eq!(watches[0].topic, "rust async runtimes");
        assert_eq!(watches[0].interval_hours, 24);
        assert!(watches[0].last_run_at.is_none());

        db.touch_watch(&id, "2026-07-07T00:00:00+00:00").unwrap();
        let watches = db.list_watches("space-1").unwrap();
        assert_eq!(
            watches[0].last_run_at.as_deref(),
            Some("2026-07-07T00:00:00+00:00")
        );

        db.delete_watch(&id).unwrap();
        assert!(db.list_watches("space-1").unwrap().is_empty());
    }

    #[test]
    // space_a_watches / space_b_watches differ only by the space label.
    #[allow(clippy::similar_names)]
    fn list_all_watches_returns_watches_from_all_spaces() {
        let db = Db::open_in_memory().unwrap();

        // Create watches in different spaces
        let id1 = db.create_watch("space-a", "topic-1", 24, "sess-1").unwrap();
        let id2 = db.create_watch("space-b", "topic-2", 48, "sess-2").unwrap();
        let id3 = db.create_watch("space-a", "topic-3", 12, "sess-3").unwrap();

        // list_all_watches should return watches from all spaces
        let all_watches = db.list_all_watches().unwrap();
        assert_eq!(all_watches.len(), 3);
        assert!(
            all_watches
                .iter()
                .any(|w| w.id == id1 && w.space_id == "space-a")
        );
        assert!(
            all_watches
                .iter()
                .any(|w| w.id == id2 && w.space_id == "space-b")
        );
        assert!(
            all_watches
                .iter()
                .any(|w| w.id == id3 && w.space_id == "space-a")
        );

        // list_watches for one space should only return that space's watches,
        // confirming list_all_watches is not space-scoped
        let space_a_watches = db.list_watches("space-a").unwrap();
        assert_eq!(space_a_watches.len(), 2);
        assert!(space_a_watches.iter().all(|w| w.space_id == "space-a"));

        let space_b_watches = db.list_watches("space-b").unwrap();
        assert_eq!(space_b_watches.len(), 1);
        assert!(space_b_watches.iter().all(|w| w.space_id == "space-b"));
    }

    /// Build a db shaped like the pre-split schema: `files` carries
    /// `status`/`mtime`, and the device-local tables live in `main` (the
    /// legacy CREATE TABLEs + column adds, minus anything added later).
    fn legacy_db(path: &std::path::Path, now: &str) {
        let conn = rusqlite::Connection::open(path).unwrap();
        conn.execute_batch(
            "CREATE TABLE sessions (id TEXT PRIMARY KEY, title TEXT NOT NULL,
                model TEXT NOT NULL, created_at TEXT NOT NULL, updated_at TEXT NOT NULL);
             CREATE TABLE messages (id TEXT PRIMARY KEY, session_id TEXT NOT NULL,
                role TEXT NOT NULL, content TEXT NOT NULL, created_at TEXT NOT NULL);
             CREATE TABLE model_prefs (id TEXT PRIMARY KEY,
                favorite INTEGER NOT NULL DEFAULT 0, last_used TEXT);
             CREATE TABLE app_settings (key TEXT PRIMARY KEY, value TEXT NOT NULL);
             CREATE TABLE spaces (id TEXT PRIMARY KEY, name TEXT NOT NULL UNIQUE,
                created_at TEXT NOT NULL);
             CREATE TABLE files (id TEXT PRIMARY KEY, space_id TEXT NOT NULL,
                name TEXT NOT NULL, hash TEXT NOT NULL, size INTEGER NOT NULL,
                status TEXT NOT NULL, created_at TEXT NOT NULL,
                UNIQUE(space_id, name));
             ALTER TABLE files ADD COLUMN mtime INTEGER NOT NULL DEFAULT 0;
             CREATE VIRTUAL TABLE file_chunks USING fts5(
                file_id UNINDEXED, seq UNINDEXED, location UNINDEXED, text);
             CREATE TABLE chunk_embeddings (file_id TEXT NOT NULL, seq INTEGER NOT NULL,
                vec BLOB NOT NULL, PRIMARY KEY (file_id, seq));
             CREATE TABLE web_cache (url_norm TEXT PRIMARY KEY, url TEXT NOT NULL,
                title TEXT, text TEXT NOT NULL, fetched_at TEXT NOT NULL);
             CREATE TABLE citations (id INTEGER PRIMARY KEY AUTOINCREMENT,
                space_id TEXT NOT NULL, report_file TEXT NOT NULL,
                url TEXT NOT NULL, title TEXT);
             CREATE TABLE session_sources (session_id TEXT NOT NULL,
                url_norm TEXT NOT NULL, PRIMARY KEY (session_id, url_norm));
             CREATE TABLE watches (id TEXT PRIMARY KEY, space_id TEXT NOT NULL,
                topic TEXT NOT NULL, interval_hours INTEGER NOT NULL,
                session_id TEXT NOT NULL, last_run_at TEXT);
             CREATE TABLE swarm_personas (session_id TEXT NOT NULL, ord INTEGER NOT NULL,
                name TEXT NOT NULL, model TEXT NOT NULL, persona TEXT NOT NULL);
             CREATE TABLE usage_log (id INTEGER PRIMARY KEY AUTOINCREMENT,
                created_at TEXT NOT NULL, session_id TEXT, space_id TEXT,
                backend TEXT NOT NULL, model TEXT NOT NULL,
                prompt_tokens INTEGER NOT NULL, completion_tokens INTEGER NOT NULL,
                cache_read_tokens INTEGER NOT NULL DEFAULT 0,
                cache_creation_tokens INTEGER NOT NULL DEFAULT 0, cost REAL);
             CREATE TABLE model_prices (model_id TEXT PRIMARY KEY, backend TEXT NOT NULL,
                prompt_price REAL NOT NULL, completion_price REAL NOT NULL,
                cache_read_price REAL, cache_write_price REAL, updated_at TEXT NOT NULL);",
        )
        .unwrap();
        conn.execute(
            "INSERT INTO spaces (id, name, created_at) VALUES ('sp', 'default', ?1)",
            [now],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO files (id, space_id, name, hash, size, status, created_at, mtime)
             VALUES ('f1', 'sp', 'a.txt', 'h1', 10, 'ok', ?1, 1234)",
            [now],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO file_chunks (file_id, seq, location, text)
             VALUES ('f1', 0, 'l', 'hello world')",
            [],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO chunk_embeddings (file_id, seq, vec) VALUES ('f1', 0, ?1)",
            [vec_to_blob(&[1.0, 0.0])],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO web_cache (url_norm, url, title, text, fetched_at)
             VALUES ('https://x.test/', 'https://x.test/', NULL, 'cached body', ?1)",
            [now],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO model_prices (model_id, backend, prompt_price, completion_price, updated_at)
             VALUES ('a/model', 'OpenRouter', 1.0, 2.0, ?1)",
            [now],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO usage_log (created_at, backend, model, prompt_tokens, completion_tokens)
             VALUES (?1, 'OpenRouter', 'a/model', 100, 10)",
            [now],
        )
        .unwrap();
        drop(conn);
    }

    #[test]
    // The price catalog round-trips exact decimals (1.0 in, 1.0 out).
    #[allow(clippy::float_cmp)]
    fn legacy_db_migrates_cache_tables_and_seeds_file_index_state() {
        let dir = std::env::temp_dir().join(format!("nexus-migrate-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let db_path = dir.join("nexus.db");
        legacy_db(&db_path, &Utc::now().to_rfc3339());
        let mut db = Db::open(&db_path).unwrap();

        // user_version stamped; legacy columns survive as dead columns.
        let v: i64 = db
            .raw()
            .query_row("PRAGMA user_version", [], |r| r.get(0))
            .unwrap();
        assert_eq!(v, SCHEMA_VERSION);
        assert!(has_column(db.raw(), "files", "mtime").unwrap());

        // Derived index state seeded from the legacy files row.
        let (mtime, status) = db
            .raw()
            .query_row(
                "SELECT mtime, status FROM cache.file_index_state WHERE file_id = 'f1'",
                [],
                |r| Ok((r.get::<_, i64>(0)?, r.get::<_, String>(1)?)),
            )
            .unwrap();
        assert_eq!((mtime, status.as_str()), (1234, "ok"));

        // Device-local tables moved into the sibling cache.db, reachable
        // through the attached connection.
        assert!(cache_path_for(&db_path).is_file());
        let hits = search_chunks(db.raw(), "sp", "hello", 8).unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(
            file_text(db.raw(), "sp", "a.txt").unwrap().as_deref(),
            Some("hello world")
        );
        let (_, text, _) = cache_get(db.raw(), "https://x.test/").unwrap().unwrap();
        assert_eq!(text, "cached body");
        let hits = semantic_chunks(db.raw(), "sp", &[1.0, 0.0], 4).unwrap();
        assert_eq!(hits.len(), 1);
        assert_eq!(hits[0].0, "a.txt");

        // The price catalog moved too — the backfill can price the legacy
        // usage row, and its sync_id was backfilled.
        assert_eq!(db.model_price("a/model").unwrap().prompt, 1.0);
        assert_eq!(db.backfill_usage_costs().unwrap(), 1);
        let sync: String = db
            .raw()
            .query_row("SELECT sync_id FROM usage_log WHERE id = 1", [], |r| {
                r.get(0)
            })
            .unwrap();
        assert!(!sync.is_empty());
    }

    #[test]
    fn fresh_db_creates_sibling_cache_db_with_split_tables() {
        let dir = std::env::temp_dir().join(format!("nexus-fresh-{}", uuid::Uuid::new_v4()));
        std::fs::create_dir_all(&dir).unwrap();
        let db_path = dir.join("nexus.db");
        let db = Db::open(&db_path).unwrap();
        let space = db.default_space_id().unwrap();
        let id = db.upsert_file(&space, "doc.txt", "h", 1, "ok").unwrap();
        db.set_file_chunks(&id, &[("l".into(), "needle text".into())])
            .unwrap();

        // The durable db holds no cache tables and no legacy file columns...
        assert!(!has_column(db.raw(), "web_cache", "url_norm").unwrap());
        assert!(!has_column(db.raw(), "files", "mtime").unwrap());
        // ...they live in the sibling cache.db: reachable through the
        // attached connection, and a standalone cache-only connection works.
        let cache_path = cache_path_for(&db_path);
        assert!(cache_path.is_file());
        let conn = rusqlite::Connection::open(&cache_path).unwrap();
        assert!(cache_get(&conn, "x").unwrap().is_none());
        drop(conn);
        assert_eq!(
            file_text(db.raw(), &space, "doc.txt").unwrap().as_deref(),
            Some("needle text")
        );
        assert_eq!(db.list_files(&space).unwrap()[0].status, "ok");
    }

    /// Every mutable table bumps its version (`updated_at`, RFC3339 so
    /// lexical order = time order) on every mutation path — the LWW input
    /// for the Phase 3 merge engine. Reads happen 2ms after each write so
    /// equal-microsecond timestamps can't pass a `>` check by accident.
    // Long by design (one assertion per mutation path).
    #[allow(clippy::too_many_lines)]
    #[test]
    fn every_mutable_table_bumps_updated_at_on_mutation() {
        // One closure per table; the array needs a shared fn type.
        type Mutator<'a> = &'a dyn Fn(&Db) -> Result<()>;
        let mut db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let sid = db.create_session("t", "a/b", &space, "chat").unwrap().id;
        let read = |table: &str, id: &str| -> String {
            std::thread::sleep(std::time::Duration::from_millis(2));
            db.raw()
                .query_row(
                    &format!("SELECT updated_at FROM {table} WHERE id = ?1"),
                    [id],
                    |r| r.get::<_, String>(0),
                )
                .unwrap()
        };

        // sessions: every mutation path bumps.
        let mutators: [Mutator<'_>; 6] = [
            &|db: &Db| db.set_compaction(&sid, "sum", 3),
            &|db: &Db| db.set_session_web_mode(&sid, true),
            &|db: &Db| db.set_session_swarm_mode(&sid, true),
            &|db: &Db| db.set_session_title(&sid, "new", Some("new-slug")),
            &|db: &Db| db.set_session_model(&sid, "m/x"),
            &|db: &Db| db.set_research_parent(&sid, "parent"),
        ];
        for mutate in mutators {
            let before = read("sessions", &sid);
            mutate(&db).unwrap();
            assert!(read("sessions", &sid) > before);
        }
        // The swarm roster has no per-row LWW — saving versions the session.
        let before = read("sessions", &sid);
        db.save_swarm_personas(
            &sid,
            &[Persona {
                name: "a".into(),
                model: "m".into(),
                blurb: "b".into(),
            }],
        )
        .unwrap();
        assert!(read("sessions", &sid) > before);

        // model_prefs.
        assert!(db.toggle_favorite("a/model").unwrap());
        let before = read("model_prefs", "a/model");
        db.set_reasoning("a/model", Some("high")).unwrap();
        assert!(read("model_prefs", "a/model") > before);
        let before = read("model_prefs", "a/model");
        db.mark_model_used("a/model").unwrap();
        assert!(read("model_prefs", "a/model") > before);

        // app_settings.
        let read_key = |key: &str| -> String {
            std::thread::sleep(std::time::Duration::from_millis(2));
            db.raw()
                .query_row(
                    "SELECT updated_at FROM app_settings WHERE key = ?1",
                    [key],
                    |r| r.get::<_, String>(0),
                )
                .unwrap()
        };
        db.set_setting("temperature", "0.5").unwrap();
        let before = read_key("temperature");
        db.set_setting("temperature", "0.9").unwrap();
        assert!(read_key("temperature") > before);

        // spaces.
        let sp = db.create_space("other").unwrap();
        let before = read("spaces", &sp.id);
        db.rename_space(&sp.id, "other2").unwrap();
        assert!(read("spaces", &sp.id) > before);

        // files.
        let fid = db.upsert_file(&space, "f.txt", "h", 1, "ok").unwrap();
        let before = read("files", &fid);
        db.rename_file(&fid, "g.txt").unwrap();
        assert!(read("files", &fid) > before);

        // watches.
        let wid = db.create_watch(&space, "topic", 24, &sid).unwrap();
        let before = read("watches", &wid);
        db.touch_watch(&wid, "2026-01-01T00:00:00Z").unwrap();
        assert!(read("watches", &wid) > before);
        let before = read("watches", &wid);
        db.set_watch_session(&wid, "other-session").unwrap();
        assert!(read("watches", &wid) > before);

        // session_sources: insert and flag changes both version the row.
        let url = "https://x.test/";
        add_session_sources(db.raw(), &sid, &[url.to_string()]).unwrap();
        let read_src = || -> String {
            std::thread::sleep(std::time::Duration::from_millis(2));
            db.raw()
                .query_row(
                    "SELECT updated_at FROM session_sources
                     WHERE session_id = ?1 AND url_norm = ?2",
                    (&sid, url),
                    |r| r.get::<_, String>(0),
                )
                .unwrap()
        };
        let before = read_src();
        db.set_source_flag(&sid, url, Some("pinned")).unwrap();
        assert!(read_src() > before);

        // usage_log: in-place updates bump; the backfill does too.
        let row = db
            .log_usage("OpenRouter", "a/model", 1, 2, 3, 4, None, false, None, None)
            .unwrap();
        let read_usage = |db: &Db, row: i64| -> String {
            std::thread::sleep(std::time::Duration::from_millis(2));
            db.raw()
                .query_row(
                    "SELECT updated_at FROM usage_log WHERE id = ?1",
                    [&row],
                    |r| r.get::<_, String>(0),
                )
                .unwrap()
        };
        let before = read_usage(&db, row);
        db.update_usage(row, 5, 6, 7, 8, Some(0.1), false).unwrap();
        assert!(read_usage(&db, row) > before);
        db.upsert_model_prices(&[(
            "a/model".to_string(),
            "OpenRouter".to_string(),
            price(1.0, 2.0),
        )])
        .unwrap();
        let before = read_usage(&db, row);
        assert_eq!(db.backfill_usage_costs().unwrap(), 1);
        assert!(read_usage(&db, row) > before);
    }

    #[test]
    fn delete_paths_write_tombstones_for_sync() {
        let db = Db::open_in_memory().unwrap();
        let space = db.default_space_id().unwrap();
        let sid = db.create_session("t", "a/b", &space, "chat").unwrap().id;
        let tombstones = |table: &str| -> Vec<String> {
            let mut stmt = db
                .raw()
                .prepare("SELECT row_id FROM sync_tombstones WHERE table_name = ?1 ORDER BY row_id")
                .unwrap();
            let rows = stmt.query_map([table], |r| r.get::<_, String>(0)).unwrap();
            rows.collect::<rusqlite::Result<Vec<_>>>().unwrap()
        };

        // Message delete.
        let mid = db.add_user_message(&sid, "hi").unwrap();
        db.delete_message(&mid).unwrap();
        assert_eq!(tombstones("messages"), vec![mid.clone()]);

        // Session delete tombstones the session and each of its messages.
        let sid2 = db.create_session("t2", "a/b", &space, "chat").unwrap().id;
        let m1 = db.add_user_message(&sid2, "one").unwrap();
        let m2 = db.add_user_message(&sid2, "two").unwrap();
        db.delete_session(&sid2).unwrap();
        let mut expected = vec![mid, m1, m2];
        expected.sort();
        assert_eq!(tombstones("messages"), expected);
        assert_eq!(tombstones("sessions"), vec![sid2]);

        // File delete.
        let fid = db.upsert_file(&space, "f.txt", "h", 1, "ok").unwrap();
        db.delete_file(&fid).unwrap();
        assert_eq!(tombstones("files"), vec![fid]);

        // Watch delete.
        let wid = db.create_watch(&space, "topic", 24, &sid).unwrap();
        db.delete_watch(&wid).unwrap();
        assert_eq!(tombstones("watches"), vec![wid]);

        // Space delete (its sessions are reassigned, not deleted).
        let sp = db.create_space("doomed").unwrap();
        db.delete_space(&sp.id).unwrap();
        assert_eq!(tombstones("spaces"), vec![sp.id]);

        // Roster replace: removed slots are tombstoned as `session:ord`.
        let persona = |name: &str| Persona {
            name: name.into(),
            model: "m".into(),
            blurb: "b".into(),
        };
        db.save_swarm_personas(&sid, &[persona("a"), persona("b")])
            .unwrap();
        db.save_swarm_personas(&sid, &[persona("b")]).unwrap();
        assert_eq!(
            tombstones("swarm_personas"),
            vec![format!("{sid}:0"), format!("{sid}:1")]
        );
    }

    #[test]
    fn device_id_is_stable_and_sync_state_roundtrips() {
        let db = Db::open_in_memory().unwrap();
        let a = db.device_id().unwrap();
        let b = db.device_id().unwrap();
        assert_eq!(a, b);
        assert!(!a.is_empty());

        // Cursors round-trip per (peer, table); None leaves values alone.
        db.set_sync_state(
            "peer-1",
            "sessions",
            Some("2026-01-01T00:00:00Z|id1"),
            Some("2026-01-02T00:00:00Z|id2"),
        )
        .unwrap();
        db.set_sync_state("peer-1", "sessions", None, Some("2026-01-03T00:00:00Z|id3"))
            .unwrap();
        db.set_sync_state("peer-1", "messages", Some("c1"), None)
            .unwrap();
        let states = db.load_sync_state().unwrap();
        assert_eq!(states.len(), 2);
        let s = states.iter().find(|s| s.table_name == "sessions").unwrap();
        assert_eq!(s.pull_cursor.as_deref(), Some("2026-01-01T00:00:00Z|id1"));
        assert_eq!(s.push_cursor.as_deref(), Some("2026-01-03T00:00:00Z|id3"));
        assert!(s.last_synced_at.is_some());
        let m = states.iter().find(|s| s.table_name == "messages").unwrap();
        assert_eq!(m.pull_cursor.as_deref(), Some("c1"));
    }

    #[test]
    fn settings_scope_registry_classifies_keys_and_stores_scope() {
        // Device-local keys are classified local; user prefs default to sync.
        for local in [
            "searxng_url",
            "langsearch_key",
            "search_provider",
            "ocr_engine",
            "ocr_model",
            "local_ocr_model",
            "usage_range",
            "last_update_check",
        ] {
            assert!(Db::setting_is_local(local), "{local} should be local");
        }
        for sync in [
            "temperature",
            "top_p",
            "max_tokens",
            "show_stats",
            "show_reasoning",
            "hide_hints",
            "verbosity",
            "memory_model",
            "embedding_model",
        ] {
            assert!(!Db::setting_is_local(sync), "{sync} should sync");
        }

        let db = Db::open_in_memory().unwrap();
        db.set_setting("temperature", "0.7").unwrap();
        db.set_setting("ocr_engine", "tesseract").unwrap();
        let scope = |key: &str| -> String {
            db.raw()
                .query_row(
                    "SELECT scope FROM app_settings WHERE key = ?1",
                    [key],
                    |r| r.get(0),
                )
                .unwrap()
        };
        assert_eq!(scope("temperature"), "sync");
        assert_eq!(scope("ocr_engine"), "local");
    }

    #[test]
    fn sync_ids_are_unique_for_citations_and_usage() {
        let db = Db::open_in_memory().unwrap();
        db.add_citations(
            "sp",
            "r.md",
            &[
                ("https://a.test/".to_string(), None),
                ("https://b.test/".to_string(), None),
            ],
        )
        .unwrap();
        db.log_usage("OpenRouter", "m", 1, 2, 0, 0, None, false, None, None)
            .unwrap();
        db.log_usage("OpenRouter", "m", 1, 2, 0, 0, None, false, None, None)
            .unwrap();
        let distinct: i64 = db
            .raw()
            .query_row(
                "SELECT COUNT(DISTINCT sync_id) FROM
                 (SELECT sync_id FROM citations UNION ALL SELECT sync_id FROM usage_log)",
                [],
                |r| r.get(0),
            )
            .unwrap();
        let total: i64 = db
            .raw()
            .query_row(
                "SELECT (SELECT COUNT(*) FROM citations) + (SELECT COUNT(*) FROM usage_log)",
                [],
                |r| r.get(0),
            )
            .unwrap();
        assert_eq!(distinct, total);
    }
}