ai-session-search 1.0.0-rc.4

Local-first search, inspection, export, recovery, and MCP recall across supported AI coding-agent sessions.
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
// SPDX-FileCopyrightText: 2026 Andrew Hundt
// SPDX-FileCopyrightText: 2026 Nisarg Patel
// SPDX-License-Identifier: Apache-2.0

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::num::NonZeroUsize;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "lowercase")]
pub enum Provider {
    Claude,
    #[serde(rename = "claude-desktop")]
    #[clap(name = "claude-desktop", alias = "claude_desktop")]
    ClaudeDesktop,
    Codex,
    Cursor,
    Antigravity,
    Pi,
    #[serde(rename = "prime-agent")]
    #[clap(name = "prime-agent", alias = "prime_agent")]
    PrimeAgent,
    #[serde(rename = "aistudio")]
    #[clap(name = "aistudio", alias = "ai-studio")]
    AiStudio,
    #[serde(rename = "gemini-cli")]
    #[clap(name = "gemini-cli", alias = "gemini_cli")]
    GeminiCli,
}

impl Provider {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Claude => "claude",
            Self::ClaudeDesktop => "claude-desktop",
            Self::Codex => "codex",
            Self::Cursor => "cursor",
            Self::Antigravity => "antigravity",
            Self::Pi => "pi",
            Self::PrimeAgent => "prime-agent",
            Self::AiStudio => "aistudio",
            Self::GeminiCli => "gemini-cli",
        }
    }

    /// Human-readable session-source name for CLI, MCP, and documentation surfaces.
    pub const fn display_name(self) -> &'static str {
        match self {
            Self::Claude => "Claude Code",
            Self::ClaudeDesktop => "Claude Desktop local agent",
            Self::Codex => "Codex",
            Self::Cursor => "Cursor",
            Self::Antigravity => "Antigravity",
            Self::Pi => "Pi coding agent",
            Self::PrimeAgent => "Prime Agent",
            Self::AiStudio => "Google AI Studio",
            Self::GeminiCli => "Gemini CLI",
        }
    }

    /// Whether this source has a native CLI command that reopens a recorded session.
    pub const fn supports_native_resume(self) -> bool {
        matches!(
            self,
            Self::Claude | Self::Codex | Self::Pi | Self::PrimeAgent
        )
    }

    /// Parse a `provider` value read back from the index. These columns are written from
    /// [`Provider::as_str`], so a value this build cannot parse was written by a newer aise whose
    /// provider set is larger (a variant ships without a schema-generation bump). The message
    /// carries the recovery because only this decoder knows it: upgrading, never a reindex, which
    /// enumerates the same rows and fails identically. Callers must surface the failure rather
    /// than silently changing row ownership; the open path normally refuses such an index first
    /// (`SchemaState::UnknownProviders`).
    pub fn from_db_str(value: &str) -> Result<Self, String> {
        value.parse().map_err(|_| {
            format!(
                "unrecognized provider in index: {value:?}; this aise {} does not recognize it, \
                 so a newer aise wrote the index: upgrade aise (`aise package update`) and retry",
                env!("CARGO_PKG_VERSION")
            )
        })
    }
}

impl std::fmt::Display for Provider {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for Provider {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().as_str() {
            "claude" => Ok(Self::Claude),
            "claude-desktop" | "claude_desktop" | "claudedesktop" => Ok(Self::ClaudeDesktop),
            "codex" => Ok(Self::Codex),
            "cursor" => Ok(Self::Cursor),
            "antigravity" => Ok(Self::Antigravity),
            "pi" => Ok(Self::Pi),
            "prime-agent" | "prime_agent" | "primeagent" => Ok(Self::PrimeAgent),
            "aistudio" | "ai-studio" | "ai_studio" => Ok(Self::AiStudio),
            "gemini-cli" | "gemini_cli" | "geminicli" => Ok(Self::GeminiCli),
            // Canonical spellings only: the hyphen/underscore aliases accepted above are
            // conveniences, so listing them here would imply four names for one provider.
            other => Err(format!(
                "unsupported provider: {other} — must be one of \"claude\", \"claude-desktop\", \"codex\", \"cursor\", \"antigravity\", \"pi\", \"prime-agent\", \"aistudio\", \"gemini-cli\""
            )),
        }
    }
}

/// Normalized, closed message-role vocabulary shared by every provider adapter.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "lowercase")]
#[clap(rename_all = "lowercase")]
pub enum Role {
    User,
    Assistant,
    Tool,
    Slash,
    Compaction,
}

impl Role {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::User => "user",
            Self::Assistant => "assistant",
            Self::Tool => "tool",
            Self::Slash => "slash",
            Self::Compaction => "compaction",
        }
    }

    /// Parse a `role` value read back from the index. Written from [`Role::as_str`], so a failure
    /// means index corruption or a variant added without a migration; the recovery is a full
    /// reparse, and the message carries it because only this decoder knows that. Callers must
    /// surface the failure rather than silently changing message authorship.
    pub fn from_db_str(value: &str) -> Result<Self, String> {
        value.parse().map_err(|_| {
            format!(
                "unrecognized role in index: {value:?}; stop AISE processes, then run \
                 `aise reindex --full` and retry"
            )
        })
    }
}

impl std::fmt::Display for Role {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for Role {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().as_str() {
            "user" => Ok(Self::User),
            "assistant" => Ok(Self::Assistant),
            "tool" => Ok(Self::Tool),
            "slash" => Ok(Self::Slash),
            "compaction" => Ok(Self::Compaction),
            // Name the accepted values: this error reaches Python callers directly, where there is
            // no schema enum or clap suggestion to fall back on.
            other => Err(format!(
                "unknown role: {other} — must be one of \"user\", \"assistant\", \"tool\", \"slash\", \"compaction\""
            )),
        }
    }
}

/// How a request selects message classes in SQL. Two shapes rather than one because they are
/// not equivalent over rows whose stored `kind` is outside the current enum: `AllExcept`
/// keeps them, `Only` does not. See [`MessageFilters::kind_predicate`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum KindPredicate {
    /// Caller named no set: return everything except these classes.
    AllExcept(Vec<MessageKind>),
    /// Caller named a set: return exactly these classes.
    Only(Vec<MessageKind>),
}

/// A single conversation turn persisted per session (the unit of message-level analytics).
///
/// PATTERN: adding a variant here is the whole cost of adding a message class. It reaches the
/// MCP schema through `mcp_server::message_kind_values`, the CLI through clap, and the default
/// result set through [`MessageKind::default_search_set`], all derived from these variants.
/// Do not pair a new variant with an `include_<variant>` flag or an `is_<variant>` column.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "snake_case")]
#[clap(rename_all = "kebab-case")]
pub enum MessageKind {
    Conversation,
    Compaction,
    ToolCall,
    ToolResult,
    /// A message the harness injected into the transcript, addressed to the agent rather
    /// than written by the user or the model: Stop-hook feedback, PreToolUse blocks,
    /// local-command caveats and stdout, task notifications. Not user prose, so it is
    /// excluded from results by default, but it is the ONLY record of what a hook told an
    /// agent and what the agent did next, so it is stored rather than discarded.
    HarnessNotice,
    Unknown,
}

/// Content-matching strategy shared by programmatic message-search clients.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "lowercase")]
#[clap(rename_all = "lowercase")]
pub enum MessageSearchMode {
    #[default]
    Literal,
    Regex,
    Fuzzy,
}

impl MessageSearchMode {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Literal => "literal",
            Self::Regex => "regex",
            Self::Fuzzy => "fuzzy",
        }
    }
}

impl std::str::FromStr for MessageSearchMode {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().as_str() {
            "literal" => Ok(Self::Literal),
            "regex" => Ok(Self::Regex),
            "fuzzy" => Ok(Self::Fuzzy),
            other => Err(format!(
                "unknown message search mode: {other}; expected literal, regex, or fuzzy"
            )),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "snake_case")]
#[clap(rename_all = "kebab-case")]
pub enum SearchField {
    Content,
    ToolName,
    ToolArgument,
}

impl SearchField {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Content => "content",
            Self::ToolName => "tool_name",
            Self::ToolArgument => "tool_argument",
        }
    }
}

impl std::str::FromStr for SearchField {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().replace('-', "_").as_str() {
            "content" => Ok(Self::Content),
            "tool_name" => Ok(Self::ToolName),
            "tool_argument" => Ok(Self::ToolArgument),
            other => Err(format!(
                "unknown message search field: {other} — must be one of \"content\", \"tool_name\", \"tool_argument\""
            )),
        }
    }
}

impl MessageKind {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Conversation => "conversation",
            Self::Compaction => "compaction",
            Self::ToolCall => "tool_call",
            Self::ToolResult => "tool_result",
            Self::HarnessNotice => "harness_notice",
            Self::Unknown => "unknown",
        }
    }

    /// Classes returned when a caller names no set: everything the user or the model wrote,
    /// plus tool traffic. `HarnessNotice` is excluded because it is the harness talking to the
    /// agent, so including it by default would change every existing result and skew the
    /// user-prose analytics (`corrections`, `repeats`, `vocab`) built on message content.
    pub fn default_search_set() -> Vec<Self> {
        use clap::ValueEnum;
        Self::value_variants()
            .iter()
            .copied()
            .filter(|kind| *kind != Self::HarnessNotice)
            .collect()
    }

    /// Every class [`MessageFilters::kind_predicate`] can leave out when the caller names no
    /// set: `HarnessNotice` always, `Compaction` under `no_compaction`. Together a small share of
    /// any real index (2 % of 2.69 M rows measured), which is what lets a count over the default
    /// predicate be written as `count(everything) - count(these)`; the storage layer keeps a
    /// partial index over exactly this set for the second term. Kept in ascending name order so
    /// the SQL rendered from it is stable.
    pub const EXCLUDABLE_BY_DEFAULT: &'static [Self] = &[Self::Compaction, Self::HarnessNotice];

    /// Parse a `kind` value read back from the index, mapping anything unrecognized to
    /// [`MessageKind::Unknown`].
    ///
    /// DELIBERATELY UNLIKE [`Provider::from_db_str`] and [`Role::from_db_str`], which
    /// `debug_assert!` on an unrecognized spelling because theirs can only come from corruption.
    /// This column is different in kind: `Unknown` is a real variant that exists precisely to
    /// absorb spellings this build does not know, and older index versions legitimately store
    /// values outside the current [`MessageKind::as_str`] set — reading them is exactly what the
    /// self-healing migration path does. Asserting here turns "opened an older index" into a
    /// panic; a proven case is `cli_search_self_heals_v4_hybrid_missing_trigram_from_intact_messages`
    /// against rows storing `kind = 'message'`.
    ///
    /// What this still fixes is the duplication: delegating to [`std::str::FromStr`] removes the second,
    /// hand-maintained match list where a newly added variant could silently fall through to
    /// `Unknown` forever. The round-trip test over every variant is what guards that.
    pub fn from_db_str(value: &str) -> Self {
        value.parse().unwrap_or(Self::Unknown)
    }
}

impl std::fmt::Display for MessageKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for MessageKind {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().replace('-', "_").as_str() {
            "conversation" => Ok(Self::Conversation),
            "compaction" => Ok(Self::Compaction),
            "tool_call" => Ok(Self::ToolCall),
            "tool_result" => Ok(Self::ToolResult),
            "harness_notice" => Ok(Self::HarnessNotice),
            "unknown" => Ok(Self::Unknown),
            other => Err(format!(
                "unknown message kind: {other} — must be one of \"conversation\", \"compaction\", \"tool_call\", \"tool_result\", \"harness_notice\", \"unknown\""
            )),
        }
    }
}

/// Who authored message content, independently of its transcript [`Role`] and [`MessageKind`].
///
/// A provider may record an agent delegation prompt as `role = user`, or a harness may inject a
/// user-role notice. Keeping authorship orthogonal prevents those storage roles from becoming
/// incorrect evidence about a person's behavior.
#[derive(
    Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize, clap::ValueEnum,
)]
#[serde(rename_all = "snake_case")]
#[clap(rename_all = "kebab-case")]
pub enum MessageAuthorship {
    Human,
    Agent,
    Harness,
    Generated,
    Mixed,
    #[default]
    Unknown,
}

impl MessageAuthorship {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Human => "human",
            Self::Agent => "agent",
            Self::Harness => "harness",
            Self::Generated => "generated",
            Self::Mixed => "mixed",
            Self::Unknown => "unknown",
        }
    }

    pub fn from_db_str(value: &str) -> Self {
        value.parse().unwrap_or(Self::Unknown)
    }
}

impl std::str::FromStr for MessageAuthorship {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().replace('-', "_").as_str() {
            "human" => Ok(Self::Human),
            "agent" => Ok(Self::Agent),
            "harness" => Ok(Self::Harness),
            "generated" => Ok(Self::Generated),
            "mixed" => Ok(Self::Mixed),
            "unknown" => Ok(Self::Unknown),
            other => Err(format!(
                "unknown message authorship: {other} — must be one of \"human\", \"agent\", \"harness\", \"generated\", \"mixed\", \"unknown\""
            )),
        }
    }
}

/// Authorship of one structurally identified region of a mixed message.
///
/// `Mixed` is deliberately absent: nested ambiguity is represented by `Unknown`, keeping
/// validation and match attribution linear in the number of parts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContentPartAuthorship {
    Human,
    Agent,
    Harness,
    Generated,
    Unknown,
}

impl ContentPartAuthorship {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Human => "human",
            Self::Agent => "agent",
            Self::Harness => "harness",
            Self::Generated => "generated",
            Self::Unknown => "unknown",
        }
    }

    pub fn from_db_str(value: &str) -> Self {
        value.parse().unwrap_or(Self::Unknown)
    }
}

impl std::str::FromStr for ContentPartAuthorship {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().replace('-', "_").as_str() {
            "human" => Ok(Self::Human),
            "agent" => Ok(Self::Agent),
            "harness" => Ok(Self::Harness),
            "generated" => Ok(Self::Generated),
            "unknown" => Ok(Self::Unknown),
            other => Err(format!(
                "unknown content-part authorship: {other} — must be one of \"human\", \"agent\", \"harness\", \"generated\", \"unknown\""
            )),
        }
    }
}

/// Structured origin of a mixed-message region; this is not a display label or an arbitrary
/// provider string.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ContentPartOrigin {
    DirectInput,
    QuotedContent,
    HarnessContext,
    GeneratedSummary,
    ToolPayload,
    Unknown,
}

impl ContentPartOrigin {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::DirectInput => "direct_input",
            Self::QuotedContent => "quoted_content",
            Self::HarnessContext => "harness_context",
            Self::GeneratedSummary => "generated_summary",
            Self::ToolPayload => "tool_payload",
            Self::Unknown => "unknown",
        }
    }

    pub fn from_db_str(value: &str) -> Self {
        value.parse().unwrap_or(Self::Unknown)
    }
}

impl std::str::FromStr for ContentPartOrigin {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().replace('-', "_").as_str() {
            "direct_input" => Ok(Self::DirectInput),
            "quoted_content" => Ok(Self::QuotedContent),
            "harness_context" => Ok(Self::HarnessContext),
            "generated_summary" => Ok(Self::GeneratedSummary),
            "tool_payload" => Ok(Self::ToolPayload),
            "unknown" => Ok(Self::Unknown),
            other => Err(format!(
                "unknown content-part origin: {other} — must be one of \"direct_input\", \"quoted_content\", \"harness_context\", \"generated_summary\", \"tool_payload\", \"unknown\""
            )),
        }
    }
}

/// One contiguous region of a mixed message.
///
/// Offsets are half-open Unicode-scalar positions, matching the public message-search character
/// coordinates rather than UTF-8 byte indexes. Parts are stored without another copy of the text.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MessageContentPart {
    pub ordinal: u32,
    pub start_char: usize,
    pub end_char: usize,
    pub authorship: ContentPartAuthorship,
    pub origin: ContentPartOrigin,
}

/// Closed authority that owns a provider-native event identity.
///
/// Related harness surfaces share an authority only when their source format can carry the same
/// native identity. The additional identity `scope` prevents unrelated ID domains owned by that
/// authority from colliding.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MessageCorrelationAuthority {
    Anthropic,
    OpenAi,
    Cursor,
    Google,
    Pi,
    PrimeAgent,
}

impl MessageCorrelationAuthority {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Anthropic => "anthropic",
            Self::OpenAi => "open_ai",
            Self::Cursor => "cursor",
            Self::Google => "google",
            Self::Pi => "pi",
            Self::PrimeAgent => "prime_agent",
        }
    }

    pub fn from_db_str(value: &str) -> Option<Self> {
        value.parse().ok()
    }
}

impl std::str::FromStr for MessageCorrelationAuthority {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().replace('-', "_").as_str() {
            "anthropic" => Ok(Self::Anthropic),
            "open_ai" | "openai" => Ok(Self::OpenAi),
            "cursor" => Ok(Self::Cursor),
            "google" => Ok(Self::Google),
            "pi" => Ok(Self::Pi),
            "prime_agent" | "primeagent" => Ok(Self::PrimeAgent),
            other => Err(format!(
                "unknown message correlation authority: {other} — must be one of \"anthropic\", \"open_ai\", \"cursor\", \"google\", \"pi\", \"prime_agent\""
            )),
        }
    }
}

/// Stable identity shared by an original source event and every known rendered/mirrored copy.
///
/// Hash/equality deliberately cover only identity. Whether a stored row is the original or a copy
/// is separate metadata and must never split one correlation group.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MessageCorrelationIdentity {
    pub authority: MessageCorrelationAuthority,
    pub scope: String,
    pub id: String,
}

/// How this stored row relates to its correlation group.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum MessageRecordRelation {
    Original,
    Mirror,
    #[default]
    Unknown,
}

impl MessageRecordRelation {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Original => "original",
            Self::Mirror => "mirror",
            Self::Unknown => "unknown",
        }
    }

    pub fn from_db_str(value: &str) -> Option<Self> {
        value.parse().ok()
    }
}

impl std::str::FromStr for MessageRecordRelation {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().as_str() {
            "original" => Ok(Self::Original),
            "mirror" => Ok(Self::Mirror),
            "unknown" => Ok(Self::Unknown),
            other => Err(format!(
                "unknown correlated record relation: {other} — must be one of \"original\", \"mirror\", \"unknown\""
            )),
        }
    }
}

#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MessageProvenance {
    pub authorship: MessageAuthorship,
    pub record_relation: MessageRecordRelation,
    pub correlation_identity: Option<MessageCorrelationIdentity>,
    pub content_parts: Vec<MessageContentPart>,
}

impl MessageProvenance {
    /// Validate in one pass: `O(S)` time and `O(1)` additional memory for `S` content parts.
    pub fn validate(&self, content: &str) -> Result<(), String> {
        if let Some(identity) = &self.correlation_identity {
            if identity.scope.trim().is_empty() {
                return Err("message correlation scope must not be empty".to_string());
            }
            if identity.id.trim().is_empty() {
                return Err("message correlation id must not be empty".to_string());
            }
        }

        if self.authorship != MessageAuthorship::Mixed {
            if !self.content_parts.is_empty() {
                return Err(
                    "content_parts are allowed only when message authorship is \"mixed\""
                        .to_string(),
                );
            }
            return Ok(());
        }
        if self.content_parts.is_empty() {
            return Err("mixed message authorship requires content_parts".to_string());
        }

        let content_chars = content.chars().count();
        let mut expected_start = 0;
        let mut first_authorship = None;
        let mut has_distinct_authorship = false;
        for (index, part) in self.content_parts.iter().enumerate() {
            if part.ordinal as usize != index {
                return Err(format!(
                    "content part ordinal {} must equal its zero-based position {index}",
                    part.ordinal
                ));
            }
            if part.start_char != expected_start {
                return Err(format!(
                    "content part {index} must start at character {expected_start}, not {}",
                    part.start_char
                ));
            }
            if part.end_char <= part.start_char {
                return Err(format!(
                    "content part {index} must have a non-empty half-open character range"
                ));
            }
            if part.end_char > content_chars {
                return Err(format!(
                    "content part {index} ends at character {}, beyond message length {content_chars}",
                    part.end_char
                ));
            }
            if let Some(first) = first_authorship {
                has_distinct_authorship |= first != part.authorship;
            } else {
                first_authorship = Some(part.authorship);
            }
            expected_start = part.end_char;
        }
        if expected_start != content_chars {
            return Err(format!(
                "content parts end at character {expected_start}, before message length {content_chars}; represent gaps as explicit \"unknown\" parts"
            ));
        }
        if !has_distinct_authorship {
            return Err(
                "mixed message authorship requires at least two distinct part authorships"
                    .to_string(),
            );
        }
        Ok(())
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Message {
    pub seq: i64,
    pub role: Role,
    pub ts: Option<DateTime<Utc>>,
    pub tool_name: Option<String>,
    pub kind: MessageKind,
    pub tool_call_id: Option<String>,
    pub is_compaction: bool,
    pub content: String,
    #[serde(default)]
    pub provenance: MessageProvenance,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SessionRecord {
    pub id: String,
    pub provider: Provider,
    pub provider_session_id: String,
    pub title: Option<String>,
    pub summary: Option<String>,
    pub cwd: Option<String>,
    pub repo_root: Option<String>,
    pub created_at: Option<DateTime<Utc>>,
    pub updated_at: Option<DateTime<Utc>>,
    pub last_message_at: Option<DateTime<Utc>>,
    pub preview_text: String,
    pub source_path: String,
    pub message_count: Option<i64>,
    pub parse_version: String,
    pub raw_metadata_json: Option<String>,
    pub parse_warning: Option<String>,
    pub discovery_source: String,
    /// The [`SessionRecord::id`] of the session that spawned this one, when this session is a
    /// subagent run.
    ///
    /// `None` for an ordinary top-level session. Every provider marks subagent runs
    /// differently (claude by a `subagents` directory under its parent, codex in a
    /// `thread_spawn` payload, cursor and pi by directory layout), so detection stays
    /// provider-specific while this field is the one shape they all produce. Typed rather than
    /// buried in `raw_metadata_json` so "every subagent of this session" is a query, which is
    /// what made codex's richer spawn data unusable.
    ///
    /// Holds the whole id, provider prefix included, so it reads as the same value the parent
    /// row carries in `id` and needs no rule about stripping a prefix before comparing.
    ///
    /// PATTERN: this names a row that need not exist. Providers keep a spawned run's transcript
    /// after the parent conversation is rotated away — in one project directory, 549 runs
    /// referenced 69 distinct parents and not one of those parent transcripts was still on
    /// disk. Recovering exactly that otherwise-unreachable work is a reason this is indexed, so
    /// do NOT add `references sessions(id)` to this column: the constraint would reject every
    /// such run at insert, and a cascade would delete the runs when a parent is reindexed away.
    pub parent_session_id: Option<String>,
    /// Human-meaningful name for the spawned agent when the provider records one: claude's
    /// `agentType` (`Explore`, `general-purpose`), codex's `agent_nickname`, or the agent's
    /// own directory or file name where that is all a provider records. Display and grouping
    /// only; the link is `parent_session_id`.
    pub agent_label: Option<String>,
}

/// Opaque keyset cursor for a bounded session-analysis document scan.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(transparent)]
pub struct AnalysisCursor(String);

impl AnalysisCursor {
    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub(crate) fn after(session_id: String) -> Self {
        Self(session_id)
    }
}

/// Provider-normalized input document for outward analysis pipelines.
///
/// The historical `user_*` field spelling denotes attributable human input, not every stored row
/// whose transcript role is `user`: agent delegation, harness, generated, mirrored, and unknown
/// content is excluded, while only human-authored parts of mixed messages are retained.
#[derive(Debug, Clone, Serialize)]
pub struct AnalysisDocument {
    pub session: SessionRecord,
    pub user_text: String,
    pub first_user_text: Option<String>,
    pub message_count: i64,
    pub user_message_count: i64,
}

/// One bounded keyset page of provider-normalized analysis documents.
#[derive(Debug, Clone, Serialize)]
pub struct AnalysisDocumentPage {
    pub documents: Vec<AnalysisDocument>,
    pub next_cursor: Option<AnalysisCursor>,
}

/// A single file-mutating tool call (`Write`/`Edit`/`MultiEdit`/`NotebookEdit`)
/// extracted from an assistant turn. Threaded through [`ParsedSession`] like
/// [`Message`], persisted to the `file_edits` table, and replayed to reconstruct
/// historical file content (`files extract`).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileEdit {
    /// Monotonic order within the session (independent of message seq).
    pub seq: i64,
    pub ts: Option<DateTime<Utc>>,
    /// Originating tool name (`Write`|`Edit`|`MultiEdit`|`NotebookEdit`).
    pub tool: String,
    pub file_path: String,
    /// Basename of `file_path`, denormalized for fast glob/search.
    pub file_name: String,
    /// Full file content — present only for `Write` (a full snapshot / replay base).
    pub new_content: Option<String>,
    /// `old_string`→`new_string` replacements for `Edit`/`MultiEdit`; empty otherwise.
    pub edits: Vec<EditOp>,
}

/// One `old_string`→`new_string` replacement from an `Edit`/`MultiEdit` tool call.
/// `replace_all` mirrors Claude's `Edit` flag: when true the replacement is applied to
/// every occurrence, otherwise only the first (which is also the only one, since a
/// non-`replace_all` `Edit` requires `old_string` to be unique).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EditOp {
    pub old: String,
    pub new: String,
    /// Replace every occurrence (Claude `Edit`/`MultiEdit` `replace_all: true`).
    #[serde(default)]
    pub replace_all: bool,
}

impl EditOp {
    /// Construct a first-occurrence (non-`replace_all`) edit.
    pub fn new(old: impl Into<String>, new: impl Into<String>) -> Self {
        Self {
            old: old.into(),
            new: new.into(),
            replace_all: false,
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParsedSession {
    pub session: SessionRecord,
    pub transcript_text: String,
    /// Per-message rows persisted to the `messages` table.
    pub messages: Vec<Message>,
    /// File-mutating tool calls persisted to the `file_edits` table (file-version recovery).
    pub file_edits: Vec<FileEdit>,
}

#[derive(Debug, Clone)]
pub struct SourceFile {
    pub provider: Provider,
    pub path: std::path::PathBuf,
    pub mtime_ns: i64,
    pub size_bytes: i64,
}

/// Who started a session — the session-level counterpart of [`MessageKind`], selected the same
/// way through a set rather than a flag per class.
///
/// The two spellings are the providers' own. Codex stores this exact enum on its session
/// metadata as `thread_source`, with the values `user` and `subagent` (167 and 247 of the 414
/// rollouts this was checked against). `subagent` is what every provider that marks the concept
/// calls it: codex's `thread_source` value and `source.subagent` key, the `subagents`
/// directory claude and cursor both write, claude's `subagent_type` Task parameter, and
/// gemini-cli's `subagent_N` author labels. `user` is the counterpart because it is the one
/// reading no provider contradicts — and because a subagent IS an agent, so naming the other
/// class `agent` would make `[agent]` unpredictable, while nothing about a subagent is a user.
///
/// Derived from [`SessionRecord::parent_session_id`] rather than stored, so the classes are
/// exhaustive by construction and no row can fall outside them. That is why selection needs no
/// `AllExcept` shape the way [`KindPredicate`] does: there are no rows of an unknown class.
///
/// PATTERN: adding a variant here is the whole cost of adding a session class. It reaches the
/// CLI through clap's `ValueEnum`, the MCP schema through `mcp_server::session_kind_values`,
/// and the default result set through [`SessionKind::default_search_set`], all derived from
/// these variants. Do not pair a new variant with an `include_<variant>` boolean — one was
/// tried for `MessageKind::HarnessNotice` and reverted; see [`MessageFilters::kinds`].
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, clap::ValueEnum)]
#[serde(rename_all = "snake_case")]
#[clap(rename_all = "kebab-case")]
pub enum SessionKind {
    /// A session a person started. Codex's `thread_source: "user"`; claude's
    /// `isSidechain: false`, which held on 143,220 of 143,224 records in top-level transcripts.
    User,
    /// A run some other session spawned, with [`SessionRecord::parent_session_id`] naming the
    /// spawner and [`SessionRecord::agent_label`] naming the kind of agent. Codex's
    /// `thread_source: "subagent"`; claude's `isSidechain: true`. On this machine they
    /// outnumber user-started claude sessions roughly five to one, which is why selecting by
    /// class is worth a parameter.
    Subagent,
}

impl SessionKind {
    pub fn as_str(self) -> &'static str {
        match self {
            Self::User => "user",
            Self::Subagent => "subagent",
        }
    }

    /// Every class, in declaration order. Derived from the enum so a new variant is included
    /// without editing a second list.
    pub fn all() -> Vec<Self> {
        use clap::ValueEnum;
        Self::value_variants().to_vec()
    }

    /// The classes a request returns when it names none: all of them.
    ///
    /// Unlike [`MessageKind::default_search_set`], which drops harness notices because they are
    /// harness bookkeeping rather than prose, a spawned run holds real work — it is the record
    /// of what a subagent was asked and what it found. Hiding that by default would answer
    /// "what did I do about X" with only half the evidence. A class that IS noise by default
    /// belongs here as an exclusion, the same way `HarnessNotice` is one.
    pub fn default_search_set() -> Vec<Self> {
        Self::all()
    }

    /// SQL that is true for exactly this class, over a `sessions` row bound to `alias`.
    /// Colocated with the variant so adding a class cannot leave its predicate unwritten.
    pub fn sql_predicate(self, alias: &str) -> String {
        match self {
            Self::User => format!("{alias}.parent_session_id is null"),
            Self::Subagent => format!("{alias}.parent_session_id is not null"),
        }
    }
}

impl std::fmt::Display for SessionKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for SessionKind {
    type Err = String;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value.to_ascii_lowercase().replace('-', "_").as_str() {
            "user" => Ok(Self::User),
            "subagent" => Ok(Self::Subagent),
            other => Err(format!(
                "unknown session kind: {other} — must be one of \"user\" (a session you started) or \"subagent\" (a run one of those spawned)"
            )),
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct SearchFilters {
    pub provider: Option<Provider>,
    /// Which classes of session to return. `None` selects the default set
    /// ([`SessionKind::default_search_set`]), which is every class.
    ///
    /// PATTERN: this set is the ONLY mechanism for session-class selection, mirroring
    /// [`MessageFilters::kinds`] one level up. Adding a class means adding a [`SessionKind`]
    /// variant, never an `include_<class>` boolean or a `<class>_only` flag beside it.
    pub session_kinds: Option<Vec<SessionKind>>,
    pub path_prefix: Option<String>,
    pub exclude_path_prefixes: Vec<String>,
    pub exclude_session_ids: Vec<String>,
    /// Return only sessions spawned by this exact session id. The link
    /// [`SessionRecord::parent_session_id`] stores is typed precisely so "every subagent of
    /// this session" is an equality match rather than a JSON scan.
    pub parent_session_id: Option<String>,
    /// Inclusive query start. Session retrieval keeps rows whose known indexed end
    /// (`updated_at`, falling back to `created_at`) is on or after this instant.
    pub since: Option<DateTime<Utc>>,
    /// Inclusive query end. Session retrieval keeps rows whose known indexed start
    /// (`created_at`, falling back to `updated_at`) is on or before this instant.
    pub until: Option<DateTime<Utc>>,
    pub limit: usize,
    pub warnings_only: bool,
}

impl SearchFilters {
    /// The session classes this request returns, with the default applied. Every reader goes
    /// through here so a caller-named set and the default cannot diverge.
    pub fn effective_session_kinds(&self) -> Vec<SessionKind> {
        self.session_kinds
            .clone()
            .unwrap_or_else(SessionKind::default_search_set)
    }

    /// Reject resolved filter combinations that cannot match any session.
    pub fn validate(&self) -> anyhow::Result<()> {
        let only_user_sessions = self.session_kinds.as_ref().is_some_and(|kinds| {
            !kinds.is_empty() && kinds.iter().all(|kind| *kind == SessionKind::User)
        });
        if self.parent_session_id.is_some() && only_user_sessions {
            anyhow::bail!(
                "session_kinds cannot contain only \"user\" when parent_session_id is set; pass [\"subagent\"], omit session_kinds to use both classes, or omit parent_session_id to search user-started sessions"
            );
        }
        Ok(())
    }
}

/// Product-level population selection for session graph, taxonomy, and phrase analysis.
///
/// The bounded strategy names the existing canonical-session-ID order explicitly. It is not a
/// recency sample, message limit, or claim of representativeness.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum AnalysisSessionSelection {
    /// Analyze every session accepted by the request scope.
    #[default]
    AllEligible,
    /// Analyze the first `max_sessions` accepted sessions in canonical session-ID order.
    FirstCanonicalSessions { max_sessions: NonZeroUsize },
}

impl AnalysisSessionSelection {
    pub(crate) const fn max_sessions(self) -> Option<NonZeroUsize> {
        match self {
            Self::AllEligible => None,
            Self::FirstCanonicalSessions { max_sessions } => Some(max_sessions),
        }
    }
}

/// Session scope plus an explicit population strategy for one longitudinal analysis run.
#[derive(Debug, Clone)]
pub struct AnalysisRequest {
    scope: SearchFilters,
    selection: AnalysisSessionSelection,
}

impl AnalysisRequest {
    /// Construct an analysis request without accepting the generic session-list limit by accident.
    pub fn new(scope: SearchFilters, selection: AnalysisSessionSelection) -> anyhow::Result<Self> {
        scope.validate()?;
        if scope.limit != 0 {
            anyhow::bail!(
                "analysis does not accept SearchFilters.limit={}; set it to 0 and choose \
                 AnalysisSessionSelection::FirstCanonicalSessions {{ max_sessions }} for the \
                 explicitly labeled canonical-order prefix, or AllEligible for every session in \
                 scope",
                scope.limit
            );
        }
        Ok(Self { scope, selection })
    }

    /// Structural session filters with the generic list limit fixed at zero.
    pub const fn scope(&self) -> &SearchFilters {
        &self.scope
    }

    /// Explicit population strategy applied after structural session filtering.
    pub const fn selection(&self) -> AnalysisSessionSelection {
        self.selection
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct SessionWithTranscript {
    #[serde(flatten)]
    pub session: SessionRecord,
    pub transcript_text: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct SearchHit {
    #[serde(flatten)]
    pub session: SessionRecord,
    pub score: i64,
    pub match_source: String,
    pub match_snippet: String,
}

/// Filters for message-level search (`messages search`, analytics). Exact/regex `limit == 0`
/// means unlimited; fuzzy validation requires a positive finite page.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct MessageFilters {
    pub role: Option<Role>,
    /// Which semantic message classes to return. `None` selects the default set
    /// ([`MessageKind::default_search_set`]): every class except `HarnessNotice`, which is
    /// harness output rather than user or model prose.
    ///
    /// PATTERN: this set is the ONLY mechanism for class selection. Adding a class means
    /// adding a `MessageKind` variant, never an `include_<class>` boolean beside it. A
    /// boolean was tried and reverted: it duplicated this field, needed tie-breaking code so
    /// `kind=harness_notice` would not self-cancel into an empty result, and would have cost
    /// one parameter per future class. Related: `no_compaction` below is legacy sugar that
    /// resolves through [`MessageFilters::effective_kinds`], and the `is_compaction` column in
    /// `db.rs` is the same redundancy left in the schema.
    pub kinds: Option<Vec<MessageKind>>,
    /// The query searches only `field`: message content, canonical `tool_name`, or the canonical
    /// tool argument selected by [`MessageFilters::argument_path`].
    pub field: Option<SearchField>,
    /// RFC 6901 JSON pointer relative to the canonical tool-call `args` value.
    pub argument_path: Option<String>,
    /// Restrict to these indexed session sources. `None` selects every provider; callers must not
    /// construct `Some(vec![])`, because an empty public provider set is rejected before SQL.
    pub providers: Option<Vec<Provider>>,
    /// Exact session id, used after CLI commands resolve a user-supplied id/prefix.
    /// This avoids substring filters accidentally merging sessions in `messages get`
    /// and `messages timeline`.
    pub session_id: Option<String>,
    /// Restrict to messages whose session's `cwd`, `repo_root`, or source transcript starts with this
    /// prefix — the message-level analogue of [`SearchFilters::path_prefix`]. Applied
    /// as a subquery against `sessions` in `append_message_filters` (the `sessions`
    /// table is tiny relative to `messages`, so no dedicated index is needed).
    pub path_prefix: Option<String>,
    /// Exclude messages whose session's `cwd`, `repo_root`, or source transcript path starts
    /// with any of these normalized prefixes. Applied before limits/context expansion.
    pub exclude_path_prefixes: Vec<String>,
    /// Restrict by session cwd or repository root only. New typed requests use this instead of
    /// the legacy broad `path_prefix` while prerelease adapters are cut over.
    #[doc(hidden)]
    pub workspace_path_prefix: Option<String>,
    /// Restrict by transcript storage path only.
    #[doc(hidden)]
    pub transcript_path_prefix: Option<String>,
    #[doc(hidden)]
    pub exclude_workspace_path_prefixes: Vec<String>,
    #[doc(hidden)]
    pub exclude_transcript_path_prefixes: Vec<String>,
    /// Exclude exact session ids. Applied before limits/context expansion.
    pub exclude_session_ids: Vec<String>,
    pub since: Option<DateTime<Utc>>,
    pub until: Option<DateTime<Utc>>,
    /// Lower inclusive message sequence bound. Only meaningful within one or more scoped
    /// sessions because `seq` is local to each session.
    pub seq_from: Option<i64>,
    /// Upper inclusive message sequence bound. Only meaningful within one or more scoped
    /// sessions because `seq` is local to each session.
    pub seq_to: Option<i64>,
    /// How the separate query string is interpreted. Literal is case-insensitive; Regex
    /// uses Rust regex syntax, and Fuzzy exhaustively scores the structurally filtered corpus with
    /// Nucleo's fzf-style sequence score. Fuzzy is sequence matching, not edit distance.
    pub match_mode: MessageSearchMode,
    /// Optional case-insensitive substring filter on a tool message's canonical `tool_name`,
    /// independent of `field` (e.g. `exec` matches Codex `exec_command`; `edit` matches Claude
    /// `Edit` and `MultiEdit`).
    pub tool: Option<String>,
    /// Drop `Compaction` from the effective class set. Retained as a convenience over
    /// [`MessageFilters::kinds`]; both resolve through [`MessageFilters::effective_kinds`], so
    /// they cannot disagree.
    pub no_compaction: bool,
    /// Which session classes the messages may come from. `None` = every class, matching what
    /// `messages search` and `list` already return by default.
    ///
    /// Mirrors [`SearchFilters::session_kinds`] rather than introducing a second spelling, and is
    /// a SET for the reason stated on [`SessionKind`]: an `include_subagent` boolean was the
    /// shape explicitly rejected there. `Some(vec![])` selects no class and therefore matches
    /// nothing, exactly as it already does on `search`.
    ///
    /// `corrections` is the one operation that narrows this by default -- see
    /// `crate::db::Db::find_corrections`, which forces `User` for the same reason it forces
    /// `Role::User`.
    pub session_kinds: Option<Vec<SessionKind>>,
    pub limit: usize,
    pub offset: usize,
}

impl MessageFilters {
    /// Validate invariants shared by CLI, MCP, Rust, and language bindings.
    ///
    /// `query` is interpreted according to [`MessageFilters::match_mode`].
    pub fn validate(&self, query: &str) -> anyhow::Result<()> {
        use anyhow::{bail, ensure};

        if self.match_mode == MessageSearchMode::Fuzzy {
            ensure!(
                query.chars().take(3).count() >= 3,
                "fuzzy search requires at least 3 characters; use literal search for shorter text"
            );
            ensure!(
                !query.trim().is_empty(),
                "fuzzy query must contain a non-whitespace character"
            );
            ensure!(
                self.limit > 0,
                "fuzzy search requires a finite non-zero limit; literal search supports unlimited results"
            );
        }
        ensure!(
            !query.is_empty() || self.match_mode == MessageSearchMode::Literal,
            "match_mode={} requires a non-empty query",
            self.match_mode.as_str()
        );
        let offset = i64::try_from(self.offset)
            .map_err(|_| anyhow::anyhow!("offset exceeds SQLite's signed 64-bit limit"))?;
        if self.limit > 0 {
            let limit = i64::try_from(self.limit)
                .map_err(|_| anyhow::anyhow!("limit exceeds SQLite's signed 64-bit limit"))?;
            offset.checked_add(limit).ok_or_else(|| {
                anyhow::anyhow!("offset + limit exceeds SQLite's signed 64-bit limit")
            })?;
        }

        if self.seq_from.is_some() || self.seq_to.is_some() {
            if self.seq_from.is_some_and(|seq| seq < 0) || self.seq_to.is_some_and(|seq| seq < 0) {
                bail!("seq_from and seq_to must be non-negative");
            }
            if let (Some(from), Some(to)) = (self.seq_from, self.seq_to) {
                ensure!(from <= to, "seq_from must be <= seq_to");
            }
        }

        let field = self.field.unwrap_or(SearchField::Content);
        if field == SearchField::ToolArgument {
            let pointer = self
                .argument_path
                .as_deref()
                .ok_or_else(|| anyhow::anyhow!("tool-argument search requires argument_path"))?;
            crate::message_search::JsonPointer::new(pointer.to_string()).map_err(|error| {
                anyhow::anyhow!("argument_path must be an RFC 6901 JSON pointer: {error}")
            })?;
            // A named set may restrict further, but it must leave tool_call reachable:
            // searching a tool argument in a set that excludes tool calls matches nothing.
            ensure!(
                self.kinds
                    .as_ref()
                    .is_none_or(|kinds| kinds.contains(&MessageKind::ToolCall)),
                "tool-argument search requires tool_call among the selected kinds"
            );
        } else if self.argument_path.is_some() {
            bail!("argument_path requires field=tool_argument");
        }
        Ok(())
    }

    /// True when at least one structural predicate (role / provider / session ID / path / time window /
    /// tool / no-compaction) restricts the SQL row set BEFORE content matching. `regex`, `rank`
    /// and `limit` are NOT structural — they filter/order content, not the scanned corpus. Used
    /// by `search_messages` to decide whether the content prefilter/scorer is worth querying:
    /// when a structural filter already narrows the corpus to a small slice, a direct scan of
    /// that slice beats intersecting against the whole-corpus trigram index.
    pub fn narrows_corpus(&self) -> bool {
        self.role.is_some()
            || self.kinds.is_some()
            || self.providers.is_some()
            || self.session_id.is_some()
            || self.path_prefix.is_some()
            || !self.exclude_path_prefixes.is_empty()
            || self.workspace_path_prefix.is_some()
            || self.transcript_path_prefix.is_some()
            || !self.exclude_workspace_path_prefixes.is_empty()
            || !self.exclude_transcript_path_prefixes.is_empty()
            || !self.exclude_session_ids.is_empty()
            || self.since.is_some()
            || self.until.is_some()
            || self.seq_from.is_some()
            || self.seq_to.is_some()
            || self.tool.is_some()
            || self.no_compaction
            || self.kinds.is_some()
    }

    /// The message classes this filter actually returns, resolved once so every caller and the
    /// SQL builder agree. `kinds` names the set (defaulting to
    /// [`MessageKind::default_search_set`]); `no_compaction` then removes one member. Because
    /// both flow through here, a request cannot ask for a class and exclude it at the same
    /// time, which is what made the earlier per-class booleans return silently empty results.
    pub fn effective_kinds(&self) -> Vec<MessageKind> {
        let mut kinds = self
            .kinds
            .clone()
            .unwrap_or_else(MessageKind::default_search_set);
        if self.no_compaction {
            kinds.retain(|kind| *kind != MessageKind::Compaction);
        }
        kinds
    }

    /// How to express class selection in SQL.
    ///
    /// PATTERN: naming no set must EXCLUDE the unwanted classes, never INCLUDE the known ones.
    /// An inclusion list silently drops any row whose stored `kind` is not a current enum
    /// variant, which is every row written by an older or newer build. That was shipped
    /// briefly and caught by a test inserting `kind = 'message'`: it reintroduced the same
    /// silent omission this filter exists to remove. An explicit set is an inclusion because
    /// the caller named exactly what they want.
    pub fn kind_predicate(&self) -> KindPredicate {
        if self.kinds.is_some() {
            return KindPredicate::Only(self.effective_kinds());
        }
        let mut excluded = vec![MessageKind::HarnessNotice];
        if self.no_compaction {
            excluded.push(MessageKind::Compaction);
        }
        KindPredicate::AllExcept(excluded)
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct MessageHit {
    pub session_id: String,
    pub provider: Provider,
    pub seq: i64,
    pub role: Role,
    pub kind: MessageKind,
    pub ts: Option<DateTime<Utc>>,
    /// The tool that produced a `Role::Tool` message (e.g. `Bash`, `exec_command`), else None.
    pub tool_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fuzzy_score: Option<u32>,
    pub content: String,
}

/// Lightweight per-session metadata used to enrich message hits with human-readable
/// context (working dir / repo / title) in the MCP `search_messages` response, so an
/// agent can interpret and group results without a follow-up `get_session` per hit.
/// Kept off [`MessageHit`] so the CLI table rendering is unchanged; the MCP layer joins
/// it on via [`crate::db::Db::session_metadata`].
#[derive(Debug, Clone, Default, Serialize)]
pub struct SessionMeta {
    pub provider_session_id: Option<String>,
    pub cwd: Option<String>,
    pub repo_root: Option<String>,
    pub title: Option<String>,
    pub updated_at: Option<DateTime<Utc>>,
    pub last_message_at: Option<DateTime<Utc>>,
    pub message_count: Option<i64>,
    pub parse_warning: Option<String>,
    /// The spawning session, mirroring [`SessionRecord::parent_session_id`], or `None` for a
    /// root run. Carried here because a hit's session id alone cannot say whether it came from a
    /// root conversation, a subagent run, or a compaction fragment, and answering that with a
    /// follow-up query per session id defeats the deduplicated join this struct exists to be.
    pub parent_session_id: Option<String>,
    /// The spawned agent's name, mirroring [`SessionRecord::agent_label`]. Display and grouping
    /// only; the link is `parent_session_id`.
    pub agent_label: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct ProviderParserHealth {
    pub provider: Provider,
    pub expected_parse_version: String,
    pub indexed_sessions: i64,
    pub current_sessions: i64,
    pub stale_sessions: i64,
}

#[derive(Debug, Clone, Serialize)]
pub struct ParserHealth {
    pub schema_version: i64,
    pub expected_schema_version: i64,
    pub schema_current: bool,
    pub indexed_sessions: i64,
    pub current_sessions: i64,
    pub stale_sessions: i64,
    pub parse_warnings: i64,
    pub providers: Vec<ProviderParserHealth>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum IndexSnapshotAvailability {
    Unavailable,
    Usable,
}

impl IndexSnapshotAvailability {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Unavailable => "unavailable",
            Self::Usable => "usable",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct IndexSnapshotStatus {
    pub availability: IndexSnapshotAvailability,
    pub last_successful_refresh_at: Option<DateTime<Utc>>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum IndexRefreshState {
    NotStarted,
    Indexing,
    Fresh,
    Postponed,
    FailedWithRecovery,
}

impl IndexRefreshState {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::NotStarted => "not_started",
            Self::Indexing => "indexing",
            Self::Fresh => "fresh",
            Self::Postponed => "postponed",
            Self::FailedWithRecovery => "failed_with_recovery",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum IndexRefreshTrigger {
    IntegrationInstall,
    CommandLine,
    Mcp,
}

impl IndexRefreshTrigger {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::IntegrationInstall => "integration_install",
            Self::CommandLine => "command_line",
            Self::Mcp => "mcp",
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct IndexRefreshStatus {
    pub state: IndexRefreshState,
    pub started_by: Option<IndexRefreshTrigger>,
    pub started_at: Option<DateTime<Utc>>,
    pub finished_at: Option<DateTime<Utc>>,
    pub files_discovered: Option<usize>,
    pub files_processed: Option<usize>,
    pub sessions_updated: Option<usize>,
    pub retry_after_ms: Option<u64>,
    pub message: Option<String>,
    pub next_command: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct IndexReadinessStatus {
    pub snapshot: IndexSnapshotStatus,
    pub refresh: IndexRefreshStatus,
}

impl IndexReadinessStatus {
    pub const fn not_started() -> Self {
        Self {
            snapshot: IndexSnapshotStatus {
                availability: IndexSnapshotAvailability::Unavailable,
                last_successful_refresh_at: None,
            },
            refresh: IndexRefreshStatus {
                state: IndexRefreshState::NotStarted,
                started_by: None,
                started_at: None,
                finished_at: None,
                files_discovered: None,
                files_processed: None,
                sessions_updated: None,
                retry_after_ms: None,
                message: None,
                next_command: None,
            },
        }
    }
}

#[derive(Debug, Clone, Serialize)]
pub struct IndexStatus {
    pub parser_health: ParserHealth,
    /// Stale sessions whose source files are currently enabled and discoverable.
    pub repairable_stale_sessions: i64,
    /// Stale sessions retained in the index whose source files are not currently discoverable.
    pub unavailable_stale_sessions: i64,
    /// Discovered files that produced no session row at all, counted by set difference against
    /// the indexed source paths. This is deliberately NOT `discovered_files - indexed_sessions`:
    /// retained sessions make indexed exceed discovered, so that subtraction measures something
    /// else. A non-zero value means search results are incomplete for those sources.
    pub unindexed_files: i64,
    pub repair_commands: Vec<String>,
    /// Orthogonal snapshot usability and refresh lifecycle state. A usable snapshot can be served
    /// while refresh is indexing, postponed, or failed.
    pub readiness: IndexReadinessStatus,
}

#[derive(Debug, Clone, Serialize)]
pub struct SessionTimeProfile {
    pub messages: i64,
    pub timestamped_messages: i64,
    pub undated_messages: i64,
    pub first_timestamp: Option<DateTime<Utc>>,
    pub last_timestamp: Option<DateTime<Utc>>,
    pub observed_span_seconds: Option<i64>,
    pub max_message_gap_seconds: Option<i64>,
    pub tool_calls: i64,
    pub tool_results: i64,
}

/// Cost breakdown for `messages search --receipt-level summary|full` (MCP `receipt_level`,
/// serialized as `search_explanation`). For literal/regex search, `candidates` is the
/// prefilter output that requires verification. For fuzzy search, it is every row that matched
/// during complete-corpus scoring, independent of the requested result page.
#[derive(Debug, Clone, Serialize)]
pub struct SearchExplain {
    /// Trigram query derived from literal text or regex literals. `None` means the query has no
    /// >=3-char literal anchor, so it must scan the structurally-filtered corpus.
    pub prefilter: Option<String>,
    /// Rows requiring literal/regex verification after prefiltering, or rows that matched complete
    /// fuzzy scoring. `None` when no candidate count is available.
    pub candidates: Option<i64>,
    /// Why a prefilter was skipped or was not applicable, including complete fuzzy scoring.
    pub prefilter_skipped: Option<String>,
    /// Rows matching the structural filters (role/provider/session/date) — the
    /// selectivity denominator.
    pub corpus: i64,
}

impl SearchExplain {
    /// One-line (two for content search) human-readable selectivity summary the CLI writes to
    /// stderr for `messages search --receipt-level summary|full`, so it never pollutes the
    /// parseable stdout. It is a presentation of the four structured fields, which are what
    /// `--format json`, Python, and MCP carry (the MCP output schema deliberately has no
    /// `summary`). `has_content_query` distinguishes a query with no usable >=3-char anchor from
    /// an empty search (structural filters only).
    pub fn summary(&self, has_content_query: bool) -> String {
        match (&self.prefilter, self.candidates) {
            (None, Some(candidates)) if has_content_query && self.prefilter_skipped.is_some() => {
                format!(
                    "[explain] {} / {} corpus rows matched after {}",
                    candidates,
                    self.corpus,
                    self.prefilter_skipped
                        .as_deref()
                        .unwrap_or("content scoring")
                )
            }
            (Some(prefilter), Some(candidates)) => {
                let pct = if self.corpus > 0 {
                    100.0 * candidates as f64 / self.corpus as f64
                } else {
                    0.0
                };
                let hint = if pct >= 50.0 {
                    "  — low selectivity; anchor the regex on a rarer literal substring"
                } else {
                    ""
                };
                format!(
                    "[explain] trigram prefilter: {prefilter}\n\
                     [explain] candidates: {candidates} / {} corpus rows ({pct:.1}%) to verify{hint}",
                    self.corpus
                )
            }
            (Some(prefilter), None) if has_content_query && self.prefilter_skipped.is_some() => {
                format!(
                    "[explain] trigram prefilter available: {prefilter}\n\
                 [explain] skipped trigram prefilter: {}; direct scan of {} corpus rows",
                    self.prefilter_skipped.as_deref().unwrap_or("not used"),
                    self.corpus
                )
            }
            // A field verified in SQLite with no prefilter at all (a literal or regex tool-name
            // search): the query scanned the corpus, so there is no candidate count to compare it
            // against, and the reason is stated rather than the anchor advice, which would point
            // at an index that field does not have.
            (None, None) if has_content_query && self.prefilter_skipped.is_some() => format!(
                "[explain] {}; scanned {} corpus rows",
                self.prefilter_skipped.as_deref().unwrap_or("no prefilter"),
                self.corpus
            ),
            _ if has_content_query => format!(
                "[explain] query has no >=3-char literal anchor → full scan of {} corpus rows",
                self.corpus
            ),
            _ => format!(
                "[explain] {} corpus rows; no content query was provided",
                self.corpus
            ),
        }
    }
}

/// A message classified by one of the selected capability's rules.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MessageClassificationMatch {
    pub session_id: String,
    /// Session-local message sequence used with `session_id` for exact focused recovery.
    pub message_seq: i64,
    pub provider: Provider,
    pub ts: Option<DateTime<Utc>>,
    /// Which compiled classification policy produced this match. Only the name: version and
    /// digest are reported once per run in `MessageClassificationReport::policies` rather than
    /// repeated on every row.
    pub policy_name: String,
    pub category: String,
    /// The exact substring that matched, not the rule that matched it: this is
    /// `Regex::find(..).as_str()` from the classifier (`db.rs`), so for the rule
    /// `\byou forgot\b` the value is `you forgot`.
    pub matched_text: String,
    /// Exact Unicode-scalar coordinates of `matched_text` in `content`.
    pub match_start_char: usize,
    pub match_end_char_exclusive: usize,
    pub content: String,
}

/// Aggregate slash-command usage frequency.
#[derive(Debug, Clone, Serialize)]
pub struct PlanningCount {
    pub command: String,
    pub count: i64,
    pub unique_sessions: i64,
    pub unique_projects: i64,
}

/// Structured filters for the `files` query surface (search / cross-ref).
/// `pattern` is a glob (`*`/`?`) over the basename, or over the full path when it
/// contains a `/`. `limit == 0` means unlimited; `offset` skips rows in the surface's
/// deterministic order.
#[derive(Debug, Clone, Default)]
pub struct FileQuery {
    pub pattern: Option<String>,
    /// Restrict to one indexed session source.
    pub provider: Option<Provider>,
    /// Exact canonical session ID. Prefer this when chaining from session/message search output.
    pub session_id: Option<String>,
    /// Restrict to sessions whose cwd, repo root, or source transcript starts with this prefix.
    pub path_prefix: Option<String>,
    /// Exclude edits from sessions whose cwd, repo root, or source transcript starts with a prefix.
    /// Applied before grouping, reconstruction selection, and result limits.
    pub exclude_path_prefixes: Vec<String>,
    /// Exclude exact canonical session IDs before grouping, reconstruction selection, and limits.
    pub exclude_session_ids: Vec<String>,
    pub since: Option<DateTime<Utc>>,
    pub until: Option<DateTime<Utc>>,
    pub min_edits: Option<i64>,
    pub max_edits: Option<i64>,
    /// Maximum rows returned by file search, cross-reference, and history. Zero explicitly means
    /// unlimited and may materialize the complete filtered result.
    pub limit: usize,
    /// Rows skipped after the surface's documented deterministic ordering.
    pub offset: usize,
}

/// One aggregate row per file across the filtered edit set (`files search`).
#[derive(Debug, Clone, Serialize)]
pub struct FileEditSummary {
    pub file_path: String,
    pub file_name: String,
    pub edits: i64,
    pub sessions: i64,
    pub last_edited: Option<DateTime<Utc>>,
}

/// One reconstructed version (edit) of a file within a session (`files history`).
#[derive(Debug, Clone, Serialize)]
pub struct FileVersion {
    pub session_id: String,
    pub provider: Provider,
    pub version: i64,
    pub tool: String,
    pub ts: Option<DateTime<Utc>>,
    pub lines: i64,
    pub file_path: String,
}

/// A file ↔ session linkage with that pair's edit count (`files cross-ref`).
#[derive(Debug, Clone, Serialize)]
pub struct FileCrossRef {
    pub file_path: String,
    pub session_id: String,
    pub provider: Provider,
    pub edits: i64,
}

#[derive(Debug, Clone, Serialize)]
pub struct ProviderHealth {
    pub provider: Provider,
    pub enabled: bool,
    pub cli_available: bool,
    pub roots: Vec<String>,
    pub discovered_files: usize,
    pub indexed_sessions: i64,
    /// Discovered files for this provider that produced no session row. `discovered_files` and
    /// `indexed_sessions` above come from different subsystems (filesystem discovery and the
    /// index) and are NOT two ends of one subtraction: retained sessions make the second exceed
    /// the first. This field is the reconciliation, computed by set difference.
    pub unindexed_files: i64,
    pub expected_parse_version: String,
    pub current_sessions: i64,
    pub stale_sessions: i64,
    pub repairable_stale_sessions: i64,
    pub unavailable_stale_sessions: i64,
    pub resume_command: Option<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct DiagnosticStatus {
    pub db_path: String,
    #[serde(flatten)]
    pub index_status: IndexStatus,
    pub discovery_warnings: Vec<crate::source::ProviderDiscoveryWarning>,
    pub providers: Vec<ProviderHealth>,
}

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

    #[test]
    fn message_provenance_accepts_complete_unicode_scalar_parts_and_scoped_correlation() {
        let provenance = MessageProvenance {
            authorship: MessageAuthorship::Mixed,
            record_relation: MessageRecordRelation::Mirror,
            correlation_identity: Some(MessageCorrelationIdentity {
                authority: MessageCorrelationAuthority::Google,
                scope: "gemini_conversation_event".to_string(),
                id: "event-7".to_string(),
            }),
            content_parts: vec![
                MessageContentPart {
                    ordinal: 0,
                    start_char: 0,
                    end_char: 2,
                    authorship: ContentPartAuthorship::Human,
                    origin: ContentPartOrigin::DirectInput,
                },
                MessageContentPart {
                    ordinal: 1,
                    start_char: 2,
                    end_char: 4,
                    authorship: ContentPartAuthorship::Generated,
                    origin: ContentPartOrigin::QuotedContent,
                },
            ],
        };

        // Four Unicode scalars but seven UTF-8 bytes proves these are character, not byte, ranges.
        assert_eq!("aé日🙂".chars().count(), 4);
        assert!(provenance.validate("aé日🙂").is_ok());
    }

    #[test]
    fn message_provenance_rejects_ambiguous_parts_and_correlation_identities() {
        let valid_parts = vec![
            MessageContentPart {
                ordinal: 0,
                start_char: 0,
                end_char: 2,
                authorship: ContentPartAuthorship::Human,
                origin: ContentPartOrigin::DirectInput,
            },
            MessageContentPart {
                ordinal: 1,
                start_char: 2,
                end_char: 4,
                authorship: ContentPartAuthorship::Generated,
                origin: ContentPartOrigin::QuotedContent,
            },
        ];
        for (provenance, expected) in [
            (
                MessageProvenance {
                    authorship: MessageAuthorship::Human,
                    content_parts: valid_parts.clone(),
                    ..Default::default()
                },
                "only when message authorship is \"mixed\"",
            ),
            (
                MessageProvenance {
                    authorship: MessageAuthorship::Mixed,
                    ..Default::default()
                },
                "requires content_parts",
            ),
            (
                MessageProvenance {
                    authorship: MessageAuthorship::Mixed,
                    content_parts: vec![MessageContentPart {
                        ordinal: 0,
                        start_char: 0,
                        end_char: 4,
                        authorship: ContentPartAuthorship::Human,
                        origin: ContentPartOrigin::DirectInput,
                    }],
                    ..Default::default()
                },
                "at least two distinct",
            ),
            (
                MessageProvenance {
                    authorship: MessageAuthorship::Mixed,
                    content_parts: vec![
                        valid_parts[0].clone(),
                        MessageContentPart {
                            start_char: 3,
                            ..valid_parts[1].clone()
                        },
                    ],
                    ..Default::default()
                },
                "must start at character 2",
            ),
            (
                MessageProvenance {
                    authorship: MessageAuthorship::Unknown,
                    correlation_identity: Some(MessageCorrelationIdentity {
                        authority: MessageCorrelationAuthority::Google,
                        scope: " ".to_string(),
                        id: "event".to_string(),
                    }),
                    content_parts: Vec::new(),
                    ..Default::default()
                },
                "scope must not be empty",
            ),
        ] {
            let error = provenance.validate("aé日🙂").unwrap_err();
            assert!(error.contains(expected), "{error}");
        }
    }

    #[test]
    fn correlation_authority_prevents_equal_native_ids_from_collapsing() {
        let first = MessageCorrelationIdentity {
            authority: MessageCorrelationAuthority::OpenAi,
            scope: "response_item".to_string(),
            id: "shared".to_string(),
        };
        let second = MessageCorrelationIdentity {
            authority: MessageCorrelationAuthority::Google,
            ..first.clone()
        };
        assert_ne!(first, second);
    }

    #[test]
    fn original_and_mirror_rows_share_one_correlation_identity() {
        let identity = MessageCorrelationIdentity {
            authority: MessageCorrelationAuthority::Google,
            scope: "conversation_event".to_string(),
            id: "event-7".to_string(),
        };
        let original = MessageProvenance {
            record_relation: MessageRecordRelation::Original,
            correlation_identity: Some(identity.clone()),
            ..Default::default()
        };
        let mirror = MessageProvenance {
            record_relation: MessageRecordRelation::Mirror,
            correlation_identity: Some(identity.clone()),
            ..Default::default()
        };

        assert_ne!(original, mirror);
        assert_eq!(original.correlation_identity, mirror.correlation_identity);
    }

    #[test]
    fn known_mirror_relation_does_not_require_a_correlation_identity() {
        let provenance = MessageProvenance {
            record_relation: MessageRecordRelation::Mirror,
            ..Default::default()
        };
        assert!(provenance.validate("replayed transcript").is_ok());
    }

    /// Naming no set must EXCLUDE unwanted classes, never INCLUDE the known ones. An inclusion
    /// list drops every row whose stored `kind` is outside the current enum, which is any row
    /// written by another build. That shipped briefly and was caught by an existing test
    /// inserting `kind = 'message'`; this pins the distinction so it cannot return.
    #[test]
    fn default_class_selection_excludes_rather_than_enumerates() {
        let default = MessageFilters::default();
        assert_eq!(
            default.kind_predicate(),
            KindPredicate::AllExcept(vec![MessageKind::HarnessNotice]),
            "the default must be an exclusion so unrecognized kinds survive"
        );

        let no_compaction = MessageFilters {
            no_compaction: true,
            ..Default::default()
        };
        assert_eq!(
            no_compaction.kind_predicate(),
            KindPredicate::AllExcept(vec![MessageKind::HarnessNotice, MessageKind::Compaction]),
            "no_compaction narrows the same exclusion rather than switching to an inclusion"
        );
    }

    /// The storage layer keeps a partial index over exactly `EXCLUDABLE_BY_DEFAULT` so a count
    /// over the default class predicate never reads message rows. Every class an exclusion
    /// predicate can name must be in that set, or the count silently returns to a table scan.
    #[test]
    fn every_default_exclusion_is_covered_by_the_excludable_set() {
        for no_compaction in [false, true] {
            let filters = MessageFilters {
                no_compaction,
                ..Default::default()
            };
            let KindPredicate::AllExcept(excluded) = filters.kind_predicate() else {
                panic!("naming no set must produce an exclusion");
            };
            for kind in excluded {
                assert!(
                    MessageKind::EXCLUDABLE_BY_DEFAULT.contains(&kind),
                    "{kind:?} is excluded by default but missing from EXCLUDABLE_BY_DEFAULT"
                );
            }
        }
        let mut names: Vec<&str> = MessageKind::EXCLUDABLE_BY_DEFAULT
            .iter()
            .map(MessageKind::as_str)
            .collect();
        let rendered = names.clone();
        names.sort_unstable();
        assert_eq!(rendered, names, "kept sorted so rendered SQL is stable");
    }

    /// The session-class spellings are the providers' own, not invented here, and the
    /// alternatives considered are locked out rather than silently accepted.
    ///
    /// Evidence, gathered from live data before choosing:
    /// - codex stores this exact enum as `thread_source` on its session metadata, valued
    ///   `user` (167 rollouts) and `subagent` (247) — the same partition, already named.
    /// - `subagent` is unanimous across every provider that marks the concept: codex's
    ///   `thread_source` value and `source.subagent` key, the `subagents` directory claude and
    ///   cursor both write, claude's `subagent_type` Task parameter (in 15 of 401 transcripts
    ///   scanned), and gemini-cli's `subagent_N` author labels.
    ///
    /// `agent` is rejected on purpose: a subagent IS an agent, so `[agent]` could be read as
    /// "every session" or "every session that is not a subagent", and the caller cannot tell
    /// which from the name. Nothing about a subagent is a user, so `user` has no such reading.
    /// `top_level` is rejected as a different vocabulary from its sibling — it answers where a
    /// session sits rather than who started it. Asserting they do NOT parse, because a
    /// presence-only test would let either come back beside the good spelling.
    #[test]
    fn session_class_spellings_match_the_providers_and_exclude_the_ambiguous_ones() {
        use std::str::FromStr;

        assert_eq!(SessionKind::User.as_str(), "user");
        assert_eq!(SessionKind::Subagent.as_str(), "subagent");
        for kind in SessionKind::all() {
            assert_eq!(
                SessionKind::from_str(kind.as_str()),
                Ok(kind),
                "every spelling this emits must be one it accepts"
            );
        }

        for rejected in [
            "agent",
            "top_level",
            "top-level",
            "sidechain",
            "child",
            "spawned",
        ] {
            let error = SessionKind::from_str(rejected)
                .expect_err(&format!("{rejected} must not be an accepted spelling"));
            assert!(
                error.contains("user") && error.contains("subagent"),
                "a rejection must name both accepted values: {error}"
            );
        }

        // Case and separator tolerance, matching MessageKind's parser.
        assert_eq!(SessionKind::from_str("SubAgent"), Ok(SessionKind::Subagent));
    }

    /// `..SearchFilters::default()` is used throughout the db, service, and tail-parse tests,
    /// which is only sound while the default filters nothing: a field defaulting to a
    /// restrictive value would silently narrow every one of those tests rather than failing
    /// one of them.
    ///
    /// The destructuring is the point. Adding a field to [`SearchFilters`] breaks THIS test to
    /// compile — "pattern does not mention field" — which is the signal the explicit
    /// field-by-field literals in those tests used to give, concentrated in the one place that
    /// can say what to do about it: assert here what the new field's default selects, then
    /// leave the other tests alone.
    #[test]
    fn the_default_session_filter_selects_every_session() {
        let SearchFilters {
            provider,
            session_kinds,
            path_prefix,
            exclude_path_prefixes,
            exclude_session_ids,
            parent_session_id,
            since,
            until,
            limit,
            warnings_only,
        } = SearchFilters::default();

        assert_eq!(provider, None, "no provider named means every provider");
        assert_eq!(session_kinds, None, "no class named means every class");
        assert_eq!(path_prefix, None);
        assert!(exclude_path_prefixes.is_empty());
        assert!(exclude_session_ids.is_empty());
        assert_eq!(parent_session_id, None, "not restricted to one spawner");
        assert_eq!(since, None);
        assert_eq!(until, None);
        assert_eq!(limit, 0, "0 is the unbounded page; callers state their own");
        assert!(!warnings_only);
    }

    /// The default returns both classes. Subagent runs are real work — 4,051 of them against
    /// 858 user-started claude sessions here — so unlike `MessageKind::HarnessNotice`, which
    /// is harness bookkeeping and stays out by default, hiding these would answer "what did I
    /// do about X" with half the evidence.
    #[test]
    fn the_default_session_class_set_is_every_class() {
        assert_eq!(
            SearchFilters::default().effective_session_kinds(),
            vec![SessionKind::User, SessionKind::Subagent]
        );
        assert_eq!(SessionKind::default_search_set(), SessionKind::all());

        // A named set is honored exactly, including the empty one.
        let only_runs = SearchFilters {
            session_kinds: Some(vec![SessionKind::Subagent]),
            ..SearchFilters::default()
        };
        assert_eq!(
            only_runs.effective_session_kinds(),
            vec![SessionKind::Subagent]
        );
        let none = SearchFilters {
            session_kinds: Some(Vec::new()),
            ..SearchFilters::default()
        };
        assert!(
            none.effective_session_kinds().is_empty(),
            "an empty set is a caller who deselected every class, not an absent one"
        );
    }

    /// A named set is an inclusion, because the caller stated exactly what they want.
    #[test]
    fn a_named_class_set_selects_exactly_those_classes() {
        let notices = MessageFilters {
            kinds: Some(vec![MessageKind::HarnessNotice]),
            ..Default::default()
        };
        assert_eq!(
            notices.kind_predicate(),
            KindPredicate::Only(vec![MessageKind::HarnessNotice]),
            "asking for harness notices must return them, not self-cancel against the default"
        );

        // no_compaction still narrows a named set, so the two cannot disagree.
        let conflict = MessageFilters {
            kinds: Some(vec![MessageKind::Compaction]),
            no_compaction: true,
            ..Default::default()
        };
        assert_eq!(
            conflict.kind_predicate(),
            KindPredicate::Only(Vec::new()),
            "an emptied set is reported as empty so the caller gets an error, not silence"
        );
    }

    /// The default set is derived, so a new variant joins it automatically and a deliberate
    /// exclusion is visible in the diff rather than hidden in a hand-written list.
    #[test]
    fn the_default_search_set_is_every_class_except_harness_notices() {
        let set = MessageKind::default_search_set();
        assert!(!set.contains(&MessageKind::HarnessNotice));
        for kind in [
            MessageKind::Conversation,
            MessageKind::Compaction,
            MessageKind::ToolCall,
            MessageKind::ToolResult,
            MessageKind::Unknown,
        ] {
            assert!(
                set.contains(&kind),
                "{kind:?} must be searchable by default"
            );
        }
    }

    /// Every typed value this crate writes as a string parses back as the same value.
    ///
    /// `as_str` is what the database, JSON output, and CLI display carry, and `from_str` is what
    /// reads them back, so the pair is one contract exercised from two directions. Only two of the
    /// enums had a round-trip test; the rest were covered from whichever side their feature needed,
    /// which leaves a spelling that renders one way and parses another undetected until a stored
    /// row or a piped value fails to decode.
    #[test]
    fn every_typed_value_parses_back_from_the_string_it_renders() {
        use std::str::FromStr;

        macro_rules! assert_round_trips {
            ($($type:ty => [$($variant:expr),+ $(,)?]),+ $(,)?) => {
                $($(
                    let rendered = $variant.as_str();
                    assert_eq!(
                        <$type>::from_str(rendered).ok(),
                        Some($variant),
                        "{} renders as {rendered:?}, which parses back as something else",
                        stringify!($variant),
                    );
                )+)+
            };
        }

        assert_round_trips! {
            Provider => [
                Provider::Claude, Provider::ClaudeDesktop, Provider::Codex, Provider::Cursor,
                Provider::Antigravity, Provider::Pi, Provider::PrimeAgent, Provider::AiStudio,
                Provider::GeminiCli,
            ],
            Role => [Role::User, Role::Assistant, Role::Tool, Role::Slash, Role::Compaction],
            MessageSearchMode => [
                MessageSearchMode::Literal, MessageSearchMode::Regex, MessageSearchMode::Fuzzy,
            ],
            SearchField => [
                SearchField::Content, SearchField::ToolName, SearchField::ToolArgument,
            ],
            MessageKind => [
                MessageKind::Conversation, MessageKind::Compaction, MessageKind::ToolCall,
                MessageKind::ToolResult, MessageKind::HarnessNotice, MessageKind::Unknown,
            ],
            MessageAuthorship => [
                MessageAuthorship::Human, MessageAuthorship::Agent, MessageAuthorship::Harness,
                MessageAuthorship::Generated, MessageAuthorship::Mixed, MessageAuthorship::Unknown,
            ],
            ContentPartAuthorship => [
                ContentPartAuthorship::Human, ContentPartAuthorship::Agent,
                ContentPartAuthorship::Harness, ContentPartAuthorship::Generated,
                ContentPartAuthorship::Unknown,
            ],
            ContentPartOrigin => [
                ContentPartOrigin::DirectInput, ContentPartOrigin::QuotedContent,
                ContentPartOrigin::HarnessContext, ContentPartOrigin::GeneratedSummary,
                ContentPartOrigin::ToolPayload, ContentPartOrigin::Unknown,
            ],
            MessageCorrelationAuthority => [
                MessageCorrelationAuthority::Anthropic, MessageCorrelationAuthority::OpenAi,
                MessageCorrelationAuthority::Cursor, MessageCorrelationAuthority::Google,
                MessageCorrelationAuthority::Pi, MessageCorrelationAuthority::PrimeAgent,
            ],
            MessageRecordRelation => [
                MessageRecordRelation::Original, MessageRecordRelation::Mirror,
                MessageRecordRelation::Unknown,
            ],
            SessionKind => [SessionKind::User, SessionKind::Subagent],
        }
    }

    #[test]
    fn every_message_kind_round_trips_through_its_database_spelling() {
        use clap::ValueEnum;

        for kind in MessageKind::value_variants() {
            assert_eq!(
                MessageKind::from_db_str(kind.as_str()),
                *kind,
                "{} must not decode as a different kind after a database read",
                kind.as_str()
            );
        }
    }

    #[test]
    fn unrecognized_provider_spelling_is_rejected_in_every_build() {
        let error = Provider::from_db_str("not_a_provider").unwrap_err();
        assert!(
            error.contains("not_a_provider"),
            "the error must identify the corrupt stored value: {error}"
        );
    }

    #[test]
    fn unrecognized_role_spelling_is_rejected_in_every_build() {
        let error = Role::from_db_str("not_a_role").unwrap_err();
        assert!(
            error.contains("not_a_role"),
            "the error must identify the corrupt stored value: {error}"
        );
    }

    // `MessageKind` is the deliberate exception to the rule above, and this test pins the
    // exception so nobody "fixes" it into consistency. An earlier attempt to give it the same
    // `debug_assert!` broke
    // `cli_search_self_heals_v4_hybrid_missing_trigram_from_intact_messages`, which reads rows
    // storing `kind = 'message'`: older indexes legitimately hold spellings this build does not
    // know, and absorbing them is what `Unknown` is FOR. Panicking would turn "opened an older
    // index" into a crash on the very path that exists to heal it.
    #[test]
    fn unrecognized_message_kind_decodes_quietly_because_older_indexes_hold_other_spellings() {
        assert_eq!(
            MessageKind::from_db_str("message"),
            MessageKind::Unknown,
            "a legacy on-disk spelling must decode, not panic"
        );
        assert_eq!(
            MessageKind::from_db_str(MessageKind::Unknown.as_str()),
            MessageKind::Unknown,
            "\"unknown\" is itself a legitimate stored spelling"
        );
    }

    #[test]
    fn programmatic_search_field_accepts_cli_spelling_but_serializes_canonically() {
        assert_eq!(
            "tool-argument".parse::<SearchField>().unwrap(),
            SearchField::ToolArgument
        );
        assert_eq!(
            "tool_argument".parse::<SearchField>().unwrap(),
            SearchField::ToolArgument
        );
        assert_eq!(SearchField::ToolArgument.as_str(), "tool_argument");
        assert_eq!(
            serde_json::to_string(&SearchField::ToolArgument).unwrap(),
            "\"tool_argument\""
        );
    }

    #[test]
    fn every_parsed_vocabulary_names_its_accepted_values_when_rejecting_input() {
        // These `FromStr` errors surface directly to Python callers, who have neither the MCP
        // schema's enum nor clap's `[possible values: ...]` to fall back on, so the accepted set
        // has to live in the message itself. Asserting all four together keeps a newly added
        // vocabulary from silently reverting to a bare rejection.
        for (rejected, accepted) in [
            (
                "usr".parse::<Role>().unwrap_err(),
                vec!["user", "assistant", "tool", "slash", "compaction"],
            ),
            (
                "contnet".parse::<SearchField>().unwrap_err(),
                vec!["content", "tool_name", "tool_argument"],
            ),
            (
                "chatgpt".parse::<Provider>().unwrap_err(),
                vec![
                    "claude",
                    "claude-desktop",
                    "codex",
                    "cursor",
                    "antigravity",
                    "pi",
                    "aistudio",
                    "gemini-cli",
                ],
            ),
            (
                "converstaion".parse::<MessageKind>().unwrap_err(),
                vec![
                    "conversation",
                    "compaction",
                    "tool_call",
                    "tool_result",
                    "unknown",
                ],
            ),
        ] {
            assert!(
                rejected.contains("must be one of"),
                "no accepted-value list in: {rejected}"
            );
            for value in accepted {
                assert!(
                    rejected.contains(&format!("{value:?}")),
                    "{value} missing from: {rejected}"
                );
            }
        }
    }

    #[test]
    fn fuzzy_message_validation_rejects_short_queries_before_database_work() {
        let filters = MessageFilters {
            match_mode: MessageSearchMode::Fuzzy,
            limit: 5,
            ..Default::default()
        };

        for query in ["", "a", "éx"] {
            let error = filters.validate(query).unwrap_err().to_string();
            assert!(
                error.contains("at least 3 characters"),
                "unexpected error for {query:?}: {error}"
            );
            assert!(error.contains("literal"), "{error}");
        }
        assert!(filters.validate("éx!").is_ok());
    }

    #[test]
    fn fuzzy_message_validation_requires_a_finite_page_without_an_arbitrary_window_cap() {
        let unlimited = MessageFilters {
            match_mode: MessageSearchMode::Fuzzy,
            ..Default::default()
        };
        assert!(unlimited
            .validate("query")
            .unwrap_err()
            .to_string()
            .contains("finite non-zero limit"));

        let large_valid_page = MessageFilters {
            match_mode: MessageSearchMode::Fuzzy,
            offset: 10_000,
            limit: 1,
            ..Default::default()
        };
        assert!(large_valid_page.validate("query").is_ok());

        let overflowing = MessageFilters {
            match_mode: MessageSearchMode::Fuzzy,
            offset: usize::MAX,
            limit: 1,
            ..Default::default()
        };
        assert!(overflowing
            .validate("query")
            .unwrap_err()
            .to_string()
            .contains("signed 64-bit"));

        let whitespace = MessageFilters {
            match_mode: MessageSearchMode::Fuzzy,
            limit: 5,
            ..Default::default()
        };
        assert!(whitespace
            .validate(" \t\n")
            .unwrap_err()
            .to_string()
            .contains("non-whitespace"));
    }

    #[test]
    fn legacy_tool_argument_filter_rejects_malformed_json_pointer_escapes() {
        let filters = MessageFilters {
            field: Some(SearchField::ToolArgument),
            argument_path: Some("/~bad".into()),
            ..Default::default()
        };
        let error = filters.validate("needle").unwrap_err().to_string();
        assert!(error.contains("RFC 6901"), "{error}");
        assert!(error.contains("'~' must be followed"), "{error}");
    }

    #[test]
    fn message_page_validation_rejects_values_sqlite_cannot_bind() {
        let filters = MessageFilters {
            offset: i64::MAX as usize,
            limit: 1,
            ..Default::default()
        };
        assert!(filters
            .validate("")
            .unwrap_err()
            .to_string()
            .contains("offset + limit"));

        if let Some(too_large) = usize::try_from(i64::MAX)
            .ok()
            .and_then(|maximum| maximum.checked_add(1))
        {
            let filters = MessageFilters {
                offset: too_large,
                ..Default::default()
            };
            assert!(filters
                .validate("")
                .unwrap_err()
                .to_string()
                .contains("offset exceeds"));
        }
    }

    #[test]
    fn every_provider_has_a_concrete_display_name_and_resume_contract() {
        let providers = crate::source::PROVIDERS;
        assert!(providers
            .iter()
            .all(|provider| !provider.display_name().trim().is_empty()));
        assert_eq!(
            providers
                .into_iter()
                .filter(|provider| provider.supports_native_resume())
                .map(Provider::as_str)
                .collect::<Vec<_>>(),
            ["claude", "codex", "pi", "prime-agent"]
        );
    }

    #[test]
    fn explain_summary_reports_prefilter_and_selectivity_pct() {
        let ex = SearchExplain {
            prefilter: Some("\"abc\"".to_string()),
            candidates: Some(80),
            prefilter_skipped: None,
            corpus: 100,
        };
        let s = ex.summary(true);
        assert!(s.contains("trigram prefilter: \"abc\""), "{s}");
        assert!(s.contains("80 / 100 corpus rows (80.0%)"), "{s}");
        // 80% candidates is non-selective → the slow-query hint must fire.
        assert!(s.contains("low selectivity"), "{s}");
    }

    #[test]
    fn explain_summary_omits_hint_when_prefilter_is_selective() {
        let ex = SearchExplain {
            prefilter: Some("\"rareword\"".to_string()),
            candidates: Some(2),
            prefilter_skipped: None,
            corpus: 1000,
        };
        let s = ex.summary(true);
        assert!(s.contains("2 / 1000 corpus rows (0.2%)"), "{s}");
        assert!(
            !s.contains("low selectivity"),
            "selective query gets no hint: {s}"
        );
    }

    #[test]
    fn explain_summary_flags_regex_without_literal_anchor() {
        let ex = SearchExplain {
            prefilter: None,
            candidates: None,
            prefilter_skipped: None,
            corpus: 500,
        };
        let s = ex.summary(true);
        assert!(s.contains("no >=3-char literal anchor"), "{s}");
        assert!(s.contains("full scan of 500 corpus rows"), "{s}");
    }

    #[test]
    fn explain_summary_notes_no_content_query_for_empty_searches() {
        let ex = SearchExplain {
            prefilter: None,
            candidates: None,
            prefilter_skipped: None,
            corpus: 42,
        };
        let s = ex.summary(false);
        assert!(s.contains("42 corpus rows"), "{s}");
        assert!(s.contains("no content query was provided"), "{s}");
    }

    #[test]
    fn explain_summary_handles_empty_corpus_without_dividing_by_zero() {
        let ex = SearchExplain {
            prefilter: Some("\"x\"".to_string()),
            candidates: Some(0),
            prefilter_skipped: None,
            corpus: 0,
        };
        let s = ex.summary(true);
        assert!(s.contains("0 / 0 corpus rows (0.0%)"), "{s}");
    }

    #[test]
    fn explain_summary_reports_intentional_prefilter_skip() {
        let ex = SearchExplain {
            prefilter: Some("\"rare\"".to_string()),
            candidates: None,
            prefilter_skipped: Some("corpus below configured threshold".to_string()),
            corpus: 25,
        };
        let s = ex.summary(true);
        assert!(s.contains("trigram prefilter available"), "{s}");
        assert!(s.contains("skipped trigram prefilter"), "{s}");
        assert!(s.contains("direct scan of 25 corpus rows"), "{s}");
    }

    /// A tool-name literal or regex search verifies in SQLite with no prefilter and no candidate
    /// count; the line names that and the corpus, and never the trigram-anchor advice, which
    /// would point at an index the field does not have.
    #[test]
    fn explain_summary_names_a_verified_scan_without_inventing_a_candidate_count() {
        let ex = SearchExplain {
            prefilter: None,
            candidates: None,
            prefilter_skipped: Some("tool name verified in SQLite without a prefilter".to_string()),
            corpus: 3,
        };
        let s = ex.summary(true);
        assert!(
            s.contains("tool name verified in SQLite without a prefilter"),
            "{s}"
        );
        assert!(s.contains("scanned 3 corpus rows"), "{s}");
        assert!(!s.contains("anchor"), "{s}");
        assert!(!s.contains("%"), "{s}");
    }
}