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
// SPDX-FileCopyrightText: 2026 Andrew Hundt
// SPDX-FileCopyrightText: 2026 Nisarg Patel
// SPDX-FileCopyrightText: 2026 Thomas Funk
// SPDX-License-Identifier: Apache-2.0

use std::collections::HashMap;
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};

use anyhow::Result;
use chrono::{DateTime, Utc};
use serde_json::{json, Value};

use crate::files::{FileMutationPayload, PendingFileMutations};
use crate::models::{
    ContentPartAuthorship, ContentPartOrigin, EditOp, FileEdit, MessageContentPart,
    MessageCorrelationAuthority, ParsedSession, Provider, SessionRecord, SourceFile,
};
use crate::providers::{
    spawn::{self, SpawnOrigin},
    walk_roots, ProviderDiscovery, ProviderPathWarning,
};
use crate::util::{
    apply_user_role_authorship, extract_text, find_repo_root, format_transcript_line,
    minimal_record, normalize_path, observe_session_end, observe_session_start, parse_datetime,
    preview_from_text, substantive_text, truncate_for_display, RawMessage,
    UserRoleAuthorshipEvidence,
};

/// The workflow engine's own log, written beside the agent transcripts it spawned. It records
/// each agent's return value rather than a conversation, so it is metadata, not a session.
const WORKFLOW_JOURNAL_FILE: &str = "journal.jsonl";

/// Filename prefix claude gives every subagent transcript, in both layouts it writes them in.
const SUBAGENT_FILE_PREFIX: &str = "agent-";

pub struct ClaudeAdapter {
    roots: Vec<PathBuf>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ClaudeSourceKind {
    CodeJsonl,
    DesktopLocalAgent,
}

#[derive(Debug, Default)]
struct ClaudeDesktopMetadata {
    session_id: Option<String>,
    cli_session_id: Option<String>,
    cwd: Option<String>,
    created_at: Option<DateTime<Utc>>,
    updated_at: Option<DateTime<Utc>>,
    title: Option<String>,
    initial_message: Option<String>,
    sidecar_path: Option<PathBuf>,
}

impl ClaudeAdapter {
    pub fn new(roots: Vec<PathBuf>) -> Self {
        Self { roots }
    }

    pub fn discover(&self) -> Vec<SourceFile> {
        self.discover_with_warnings().sources
    }

    pub(crate) fn discover_with_warnings(&self) -> ProviderDiscovery {
        let mut files = Vec::new();
        let mut walked = walk_roots(&self.roots, None);
        for entry in walked.entries {
            let path = &entry.path;
            if path.extension().and_then(|ext| ext.to_str()) != Some("jsonl") {
                continue;
            }
            if path
                .components()
                .any(|component| component.as_os_str() == "memory")
            {
                continue;
            }
            // Subagent transcripts — `<parent>/subagents/agent-<id>.jsonl` and the flat
            // `agent-<id>.jsonl` beside their parent — are sessions of their own; see
            // `ClaudeSpawn` for why, and how their identity is kept off the parent's row.
            // The workflow engine's `journal.jsonl` sits among them and records agent
            // return values rather than a conversation, so it is not a session.
            if path.file_name().and_then(|name| name.to_str()) == Some(WORKFLOW_JOURNAL_FILE)
                && spawn::subagents_dir_origin(path).is_some()
            {
                continue;
            }
            let source_kind = ClaudeSourceKind::from_path(path);
            let sidecar = if is_claude_desktop_audit(path) {
                claude_desktop_sidecar_path(path)
            } else {
                claude_subagent_sidecar_path(path)
            };
            files.push(SourceFile {
                provider: source_kind.provider(),
                path: entry.path,
                mtime_ns: mtime_ns(&entry.metadata),
                size_bytes: entry.metadata.len() as i64,
            });
            // Both transcript kinds that carry a JSON sidecar fold it in, so editing
            // metadata beside an unchanged transcript still re-parses the session.
            if let (Some(sidecar), Some(source)) = (sidecar, files.last_mut()) {
                if let Some(warning) = fold_sidecar(source, &sidecar) {
                    walked.warnings.push(warning);
                }
            }
        }
        ProviderDiscovery {
            sources: files,
            warnings: walked.warnings,
        }
    }

    pub fn parse(&self, source: &SourceFile) -> ParsedSession {
        match self.parse_inner(&source.path) {
            Ok(parsed) => parsed,
            Err(err) => minimal_record(
                ClaudeSourceKind::from_path(&source.path).provider(),
                &source.path,
                err.to_string(),
            ),
        }
    }

    fn parse_inner(&self, path: &Path) -> Result<ParsedSession> {
        let file = std::fs::File::open(path)?;
        self.parse_reader(std::io::BufReader::new(file), path)
    }

    /// Parse claude session lines from any reader. `parse_inner` calls this over the file; the
    /// incremental tail parser ([`crate::tail`]) calls it over an in-memory byte slice of the
    /// appended region. Keeping the per-line logic in ONE place (no tail-specific copy) is what
    /// lets a differential test assert a tail parse equals a full parse.
    ///
    /// Streams line-by-line via the reader instead of loading the whole file into a String
    /// (task #241): a 536MB append-only session previously needed ~1.5GB transient RAM for the
    /// `read_to_string` String plus a second `lines().count()` pass. We hold only the current
    /// line and tally `line_count` in this single pass. `BufRead::lines()` yields the same line
    /// content/count as `str::lines()` (verified for `\n`, `\r\n`, trailing/no-trailing newline)
    /// and reads each line via [`crate::util::lines_replacing_invalid_utf8`]: a stray non-UTF-8
    /// byte becomes U+FFFD rather than aborting the parse, so one bad byte never loses the session.
    pub fn parse_reader<R: std::io::BufRead>(
        &self,
        reader: R,
        path: &Path,
    ) -> Result<ParsedSession> {
        let source_kind = ClaudeSourceKind::from_path(path);
        let desktop = match source_kind {
            ClaudeSourceKind::CodeJsonl => ClaudeDesktopMetadata::default(),
            ClaudeSourceKind::DesktopLocalAgent => claude_desktop_metadata(path),
        };
        let mut line_count: usize = 0;
        let mut malformed_line_count: usize = 0;
        let mut valid_record_count: usize = 0;
        let mut provider_session_id = path
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or("unknown")
            .to_string();
        // The session id identifies THIS file and is bound once. Desktop metadata is
        // authoritative when present; otherwise the first record carrying one wins.
        // Rebinding on every line lets any later record retarget the whole file, and the
        // `on conflict(id) do update` upsert in db.rs would then overwrite the row that id
        // belongs to instead of storing this session. See codex.rs for the same guard, where
        // an unguarded rebind cost 65 of 414 sessions.
        let mut session_id_bound = false;
        // A subagent transcript's records all carry its PARENT's sessionId, so no record in it
        // may bind this session's id. Hold the binding shut and compose the id after the loop,
        // where the parent is known; the parent becomes metadata, not this session's identity.
        let spawn = ClaudeSpawn::for_path(path);
        let subagent = spawn.is_subagent();
        if subagent {
            session_id_bound = true;
        }
        let sidecar = claude_subagent_sidecar(path);
        let mut parent_session_id: Option<String> = None;
        let mut agent_id: Option<String> = None;
        if let Some(session_id) = desktop.session_id.as_deref() {
            provider_session_id = session_id.to_string();
            session_id_bound = true;
        } else if source_kind == ClaudeSourceKind::DesktopLocalAgent {
            if let Some(session_id) = claude_desktop_session_id_from_path(path) {
                provider_session_id = session_id;
                session_id_bound = true;
            }
        }
        let mut cwd = desktop.cwd.clone();
        let mut created_at: Option<DateTime<Utc>> = desktop.created_at;
        let mut updated_at: Option<DateTime<Utc>> = desktop.updated_at;
        let mut messages = Vec::new();
        let mut transcript_lines = Vec::new();
        let mut last_prompt = desktop.initial_message.clone();
        let mut file_edits: Vec<FileEdit> = Vec::new();
        let mut pending_file_mutations = PendingFileMutations::default();
        // tool_use_id -> tool name, so a later tool_result (which references the call by
        // id, not name) can be tagged with the tool it came from.
        let mut tool_use_names: HashMap<String, String> = HashMap::new();

        for line in crate::util::lines_replacing_invalid_utf8(reader) {
            let line = line?;
            line_count += 1;
            if line.trim().is_empty() {
                continue;
            }
            let value: Value = match serde_json::from_str(&line) {
                Ok(value) => value,
                Err(_) => {
                    malformed_line_count += 1;
                    continue;
                }
            };
            valid_record_count += 1;
            if !session_id_bound {
                // `session_id` keeps precedence over `sessionId` within one record, as before.
                if let Some(session_id) = value
                    .get("session_id")
                    .or_else(|| value.get("sessionId"))
                    .and_then(Value::as_str)
                {
                    provider_session_id = session_id.to_string();
                    session_id_bound = true;
                }
            }
            // For a subagent transcript the same field names the PARENT, which is worth
            // keeping: it is what links the subagent's work back to the session that spawned
            // it. Captured once, from the first record that carries each value.
            if subagent {
                if parent_session_id.is_none() {
                    parent_session_id = value
                        .get("sessionId")
                        .or_else(|| value.get("session_id"))
                        .and_then(Value::as_str)
                        .map(ToOwned::to_owned);
                }
                if agent_id.is_none() {
                    agent_id = value
                        .get("agentId")
                        .and_then(Value::as_str)
                        .map(ToOwned::to_owned);
                }
            }
            if value.get("type").and_then(Value::as_str) == Some("last-prompt") {
                if let Some(prompt) = value.get("lastPrompt").and_then(Value::as_str) {
                    let prompt = prompt.trim();
                    if substantive_text(prompt) {
                        last_prompt = Some(prompt.to_string());
                    }
                }
            }
            if cwd.is_none() {
                cwd = value
                    .get("cwd")
                    .and_then(Value::as_str)
                    .map(ToOwned::to_owned);
            }

            let timestamp = claude_timestamp(&value, source_kind);
            let event_id = value
                .get("uuid")
                .and_then(Value::as_str)
                .filter(|id| !id.trim().is_empty());

            let mut role = value
                .get("type")
                .and_then(Value::as_str)
                .map(str::to_string);
            let mut text = String::new();
            let mut tool_result = false;
            let mut tool_name: Option<String> = None;
            let mut tool_call_id: Option<String> = None;
            let mut mixed_user_content = None;

            if let Some(message) = value.get("message") {
                role = message
                    .get("role")
                    .and_then(Value::as_str)
                    .map(str::to_string)
                    .or(role);
                text = extract_text(message);
                if role.as_deref() == Some("user") {
                    mixed_user_content = claude_mixed_user_content(message, subagent);
                }
                // Capture file-mutating tool calls before any text-based skip/continue,
                // so edits inside assistant turns with empty/skipped text are still recorded.
                stage_file_edits(message, timestamp, &mut pending_file_mutations);
                collect_file_edit_results(message, &mut pending_file_mutations, &mut file_edits);
                collect_tool_use_names(message, &mut tool_use_names);
                append_tool_use_messages(message, timestamp, &mut messages);
                // One scan for the first tool_result block: `tool_result` is true when a block
                // EXISTS (even without a `tool_use_id`), and the name is tagged from that same
                // block's id when present. Was two scans (`is_tool_result` + `tool_result_id`).
                if let Some(block) = first_tool_result_block(message) {
                    tool_result = true;
                    if let Some(id) = block.get("tool_use_id").and_then(Value::as_str) {
                        tool_call_id = Some(id.to_string());
                        tool_name = tool_use_names.get(id).cloned();
                    }
                }
            } else if let Some(message) = value.get("content").and_then(Value::as_str) {
                text = message.to_string();
            }

            // Harness notices (Stop-hook feedback, PreToolUse blocks, local-command caveats,
            // task notifications) are the only record of what a hook told an agent and what it
            // did next, so they are stored and tagged rather than dropped. Every query excludes
            // them by default, so user prose and the analytics built on it are unaffected.
            if is_harness_notice(&value, &text) {
                let notice = text.trim();
                if !notice.is_empty() {
                    let mut raw_message = RawMessage::harness_notice(notice.to_string(), timestamp);
                    if let Some(event_id) = event_id {
                        raw_message = raw_message.with_native_event_identity(
                            MessageCorrelationAuthority::Anthropic,
                            event_id,
                        );
                    }
                    messages.push(raw_message);
                }
                continue;
            }
            if should_skip_message(&value, &text) {
                continue;
            }
            // Compaction summaries are `/compact` output that Claude records as role:user
            // with `isCompactSummary: true` — a continuation digest, not a real prompt.
            let is_compaction = value
                .get("isCompactSummary")
                .and_then(Value::as_bool)
                .unwrap_or(false);
            let text = strip_command_markup(&text);

            match role.as_deref() {
                Some("user") | Some("assistant") => {
                    if let Some(mixed) = mixed_user_content {
                        observe_session_start(&mut created_at, timestamp);
                        observe_session_end(&mut updated_at, timestamp);
                        let mut raw_message =
                            RawMessage::message("user", mixed.content, timestamp, None);
                        if let Some(parts) = mixed.parts {
                            raw_message = raw_message.with_content_parts(parts);
                        }
                        if let Some(event_id) = event_id {
                            raw_message = raw_message.with_native_event_identity(
                                MessageCorrelationAuthority::Anthropic,
                                event_id,
                            );
                        }
                        messages.push(raw_message);
                        if !mixed.transcript_text.is_empty() {
                            transcript_lines.push(format_transcript_line(
                                "user",
                                timestamp,
                                &mixed.transcript_text,
                            ));
                        }
                        continue;
                    }
                    let text = text.trim().to_string();
                    if text.is_empty() {
                        continue;
                    }
                    // Compaction digest → `compaction` role; tool output (claude records
                    // tool results as role:user) → `tool`. Both are searchable but excluded
                    // from user/correction/planning analytics and the human transcript
                    // (kept separate from the conversation, like other providers' tool output).
                    if is_compaction {
                        let mut raw_message =
                            RawMessage::message("compaction", text, timestamp, None);
                        if let Some(event_id) = event_id {
                            raw_message = raw_message.with_native_event_identity(
                                MessageCorrelationAuthority::Anthropic,
                                event_id,
                            );
                        }
                        messages.push(raw_message);
                        continue;
                    }
                    if tool_result {
                        let mut raw_message = RawMessage::tool_result_with_name(
                            tool_name,
                            text,
                            tool_call_id.as_deref(),
                            timestamp,
                        );
                        if let Some(event_id) = event_id {
                            raw_message = raw_message.with_native_event_identity(
                                MessageCorrelationAuthority::Anthropic,
                                event_id,
                            );
                        }
                        messages.push(raw_message);
                        continue;
                    }
                    observe_session_start(&mut created_at, timestamp);
                    observe_session_end(&mut updated_at, timestamp);
                    let mut raw_message = RawMessage::message(
                        role.unwrap_or_default(),
                        text.clone(),
                        timestamp,
                        None,
                    );
                    if let Some(event_id) = event_id {
                        raw_message = raw_message.with_native_event_identity(
                            MessageCorrelationAuthority::Anthropic,
                            event_id,
                        );
                    }
                    messages.push(raw_message);
                    transcript_lines.push(format_transcript_line(
                        messages.last().map(RawMessage::role).unwrap_or("message"),
                        timestamp,
                        &text,
                    ));
                }
                _ => {}
            }
        }

        let first_user = messages
            .iter()
            .find(|message| message.role() == "user" && substantive_text(message.content()))
            .map(|message| message.content().to_string());
        let last_user = messages
            .iter()
            .rev()
            .find(|message| message.role() == "user" && substantive_text(message.content()))
            .map(|message| message.content().to_string());
        let title = sidecar
            .as_ref()
            .and_then(ClaudeSubagentSidecar::description)
            .map(|text| truncate_for_display(text, 100))
            .filter(|text| substantive_text(text))
            .or_else(|| desktop.title.clone())
            .filter(|text| substantive_text(text))
            .or_else(|| {
                last_prompt
                    .clone()
                    .filter(|text| substantive_text(text))
                    .map(|text| truncate_for_display(&text, 100))
            })
            .or_else(|| {
                last_user
                    .clone()
                    .map(|text| truncate_for_display(&text, 100))
            })
            .or_else(|| {
                first_user
                    .clone()
                    .map(|text| truncate_for_display(&text, 100))
            });
        let preview = last_prompt
            .clone()
            .or_else(|| last_user.clone())
            .or_else(|| first_user.clone())
            .map(|text| preview_from_text(&text))
            .unwrap_or_else(|| "(no preview available)".to_string());
        let repo_root = cwd.as_deref().and_then(find_repo_root);
        let mut raw_metadata = json!({
            "line_count": line_count,
            "valid_record_count": valid_record_count,
            "session_path": normalize_path(path),
        });
        if malformed_line_count > 0 {
            raw_metadata["malformed_line_count"] = json!(malformed_line_count);
        }
        if let Some(path) = desktop.sidecar_path.as_deref() {
            raw_metadata["metadata_path"] = json!(normalize_path(path));
        }
        if let Some(cli_session_id) = desktop.cli_session_id.as_deref() {
            raw_metadata["cli_session_id"] = json!(cli_session_id);
        }
        if let Some(sidecar) = sidecar.as_ref() {
            // Whole, not key-by-key: `toolUseId`, `spawnDepth`, `worktreePath`,
            // `worktreeBranch`, `model` and `stoppedByUser` all appear on live sidecars, and
            // enumerating them here would silently drop whichever key claude adds next.
            raw_metadata["subagent"] = sidecar.value.clone();
            raw_metadata["metadata_path"] = json!(normalize_path(&sidecar.path));
        }
        let raw_metadata_json = Some(serde_json::to_string(&raw_metadata)?);

        let parse_warning = super::malformed_jsonl_warning(malformed_line_count);

        // A subagent's identity is its parent's id plus what distinguishes the run under that
        // parent. Neither half alone works: the records name only the parent, and the agent id
        // repeats across parents. `Nested` takes the parent from the path because that is what
        // the run suffix is relative to — the two sources agreed on all 4,051 live transcripts,
        // so preferring the path costs nothing and keeps id and parent consistent by
        // construction.
        let provider = source_kind.provider();
        let (provider_session_id, parent_session_id) = match &spawn {
            ClaudeSpawn::TopLevel => (provider_session_id, None),
            ClaudeSpawn::Nested(origin) => {
                (origin.session_id(), Some(origin.parent_link(provider)))
            }
            ClaudeSpawn::BesideParent { run_suffix } => match parent_session_id {
                Some(parent) => (
                    format!("{parent}/{run_suffix}"),
                    Some(spawn::parent_link(provider, &parent)),
                ),
                // No record named a parent, so this run stands under its own name. Unique
                // among the siblings of one project directory, which is where these sit.
                None => (run_suffix.clone(), None),
            },
        };

        let session = SessionRecord {
            id: format!("{provider}:{provider_session_id}"),
            provider,
            provider_session_id,
            title,
            summary: first_user.map(|text| truncate_for_display(&text, 180)),
            cwd,
            repo_root,
            created_at,
            updated_at,
            last_message_at: updated_at,
            preview_text: preview,
            source_path: normalize_path(path),
            message_count: Some(messages.len() as i64),
            parse_version: source_kind.parse_version().to_string(),
            raw_metadata_json,
            parse_warning,
            discovery_source: source_kind.discovery_source().to_string(),
            // Only a subagent run has a parent, resolved above.
            parent_session_id,
            // The sidecar's `agentType` is the name a reader recognizes; the agent id is the
            // fallback for the 2,243 transcripts that have no sidecar.
            agent_label: sidecar
                .as_ref()
                .and_then(ClaudeSubagentSidecar::agent_type)
                .map(ToOwned::to_owned)
                .or(agent_id),
        };

        let mut messages = crate::util::to_messages_with_tools_in_scope(messages, &session.id);
        apply_user_role_authorship(
            &mut messages,
            if subagent {
                UserRoleAuthorshipEvidence::AgentDelegationEvent
            } else {
                UserRoleAuthorshipEvidence::HumanInputEvent
            },
        );
        Ok(ParsedSession {
            session,
            transcript_text: transcript_lines.join("\n\n"),
            messages,
            file_edits,
        })
    }
}

struct ClaudeMixedUserContent {
    content: String,
    parts: Option<Vec<MessageContentPart>>,
    transcript_text: String,
}

fn claude_mixed_user_content(message: &Value, subagent: bool) -> Option<ClaudeMixedUserContent> {
    let blocks = message.get("content")?.as_array()?;
    let has_tool_result = blocks
        .iter()
        .any(|block| block.get("type").and_then(Value::as_str) == Some("tool_result"));
    let has_direct_text = blocks.iter().any(|block| {
        block.get("type").and_then(Value::as_str) != Some("tool_result")
            && !extract_text(block).trim().is_empty()
    });
    if !(has_tool_result && has_direct_text) {
        return None;
    }

    let direct_authorship = if subagent {
        ContentPartAuthorship::Agent
    } else {
        ContentPartAuthorship::Human
    };
    let mut content = String::new();
    let mut parts: Vec<MessageContentPart> = Vec::new();
    let mut transcript_parts = Vec::new();
    for block in blocks {
        let text = extract_text(block);
        let text = text.trim();
        if text.is_empty() {
            continue;
        }
        let is_tool_result = block.get("type").and_then(Value::as_str) == Some("tool_result");
        if !content.is_empty() {
            content.push('\n');
            if let Some(previous) = parts.last_mut() {
                previous.end_char += 1;
            }
        }
        let start_char = content.chars().count();
        content.push_str(text);
        let end_char = content.chars().count();
        let (authorship, origin) = if is_tool_result {
            (
                ContentPartAuthorship::Generated,
                ContentPartOrigin::ToolPayload,
            )
        } else {
            transcript_parts.push(text.to_string());
            (direct_authorship, ContentPartOrigin::DirectInput)
        };
        parts.push(MessageContentPart {
            ordinal: parts.len() as u32,
            start_char,
            end_char,
            authorship,
            origin,
        });
    }
    // One linear pass over the retained partitions avoids fabricating `Mixed` when a structural
    // block (such as a tool reference) has no searchable text. No text or part is copied here.
    let has_distinct_authorship = parts.first().is_some_and(|first| {
        parts
            .iter()
            .skip(1)
            .any(|part| part.authorship != first.authorship)
    });
    Some(ClaudeMixedUserContent {
        content,
        parts: has_distinct_authorship.then_some(parts),
        transcript_text: transcript_parts.join("\n"),
    })
}

impl ClaudeSourceKind {
    fn from_path(path: &Path) -> Self {
        if is_claude_desktop_audit(path) {
            Self::DesktopLocalAgent
        } else {
            Self::CodeJsonl
        }
    }

    fn parse_version(self) -> &'static str {
        match self {
            Self::CodeJsonl => crate::util::provider_parse_version(Provider::Claude),
            Self::DesktopLocalAgent => crate::util::provider_parse_version(Provider::ClaudeDesktop),
        }
    }

    fn discovery_source(self) -> &'static str {
        match self {
            Self::CodeJsonl => "jsonl",
            Self::DesktopLocalAgent => "claude-desktop-local-agent-audit-jsonl",
        }
    }

    fn provider(self) -> Provider {
        match self {
            Self::CodeJsonl => Provider::Claude,
            Self::DesktopLocalAgent => Provider::ClaudeDesktop,
        }
    }
}

fn claude_timestamp(value: &Value, source_kind: ClaudeSourceKind) -> Option<DateTime<Utc>> {
    let primary = match source_kind {
        ClaudeSourceKind::CodeJsonl => "timestamp",
        ClaudeSourceKind::DesktopLocalAgent => "_audit_timestamp",
    };
    value
        .get(primary)
        .and_then(Value::as_str)
        .and_then(parse_datetime)
        .or_else(|| {
            value
                .get("timestamp")
                .and_then(Value::as_str)
                .and_then(parse_datetime)
        })
}

pub(crate) fn is_claude_desktop_audit(path: &Path) -> bool {
    path.file_name().and_then(|name| name.to_str()) == Some("audit.jsonl")
        && path
            .components()
            .any(|component| component.as_os_str() == "local-agent-mode-sessions")
}

fn claude_desktop_session_id_from_path(path: &Path) -> Option<String> {
    let name = path.parent()?.file_name()?.to_str()?;
    Some(
        name.strip_prefix("local_")
            .filter(|id| !id.is_empty())
            .unwrap_or(name)
            .to_string(),
    )
}

fn claude_desktop_sidecar_path(path: &Path) -> Option<PathBuf> {
    let session_dir = path.parent()?;
    let session_dir_name = session_dir.file_name()?.to_str()?;
    Some(
        session_dir
            .parent()?
            .join(format!("{session_dir_name}.json")),
    )
}

fn claude_desktop_metadata(path: &Path) -> ClaudeDesktopMetadata {
    let Some(sidecar_path) = claude_desktop_sidecar_path(path) else {
        return ClaudeDesktopMetadata::default();
    };
    let Ok(raw) = fs::read_to_string(&sidecar_path) else {
        return ClaudeDesktopMetadata {
            sidecar_path: Some(sidecar_path),
            ..ClaudeDesktopMetadata::default()
        };
    };
    let Ok(value) = serde_json::from_str::<Value>(&raw) else {
        return ClaudeDesktopMetadata {
            sidecar_path: Some(sidecar_path),
            ..ClaudeDesktopMetadata::default()
        };
    };
    let get_str = |key: &str| value.get(key).and_then(Value::as_str).map(str::to_string);
    ClaudeDesktopMetadata {
        session_id: get_str("sessionId").or_else(|| {
            get_str("session_id").or_else(|| claude_desktop_session_id_from_path(path))
        }),
        cli_session_id: get_str("cliSessionId"),
        cwd: get_str("cwd"),
        created_at: value
            .get("createdAt")
            .and_then(Value::as_str)
            .and_then(parse_datetime),
        updated_at: value
            .get("lastActivityAt")
            .or_else(|| value.get("updatedAt"))
            .and_then(Value::as_str)
            .and_then(parse_datetime),
        title: get_str("title").map(|text| truncate_for_display(&text, 100)),
        initial_message: get_str("initialMessage"),
        sidecar_path: Some(sidecar_path),
    }
}

/// Stage file-mutating calls by native id until their corresponding result proves completion.
fn stage_file_edits(
    message: &Value,
    ts: Option<DateTime<Utc>>,
    pending: &mut PendingFileMutations,
) {
    let Some(content) = message.get("content").and_then(Value::as_array) else {
        return;
    };
    for block in content {
        if block.get("type").and_then(Value::as_str) != Some("tool_use") {
            continue;
        }
        let name = block
            .get("name")
            .and_then(Value::as_str)
            .unwrap_or_default();
        pending.stage(
            block.get("id").and_then(Value::as_str),
            ts,
            name,
            tool_use_payload(name, block.get("input")),
        );
    }
}

/// Extract mutations immediately for provider formats whose edit record is authoritative itself.
/// Claude Code does not use this path: it stages calls above until a correlated result arrives.
pub(crate) fn collect_file_edits(
    message: &Value,
    ts: Option<DateTime<Utc>>,
    next_seq: &mut i64,
    out: &mut Vec<FileEdit>,
) {
    let Some(content) = message.get("content").and_then(Value::as_array) else {
        return;
    };
    for block in content {
        if block.get("type").and_then(Value::as_str) != Some("tool_use") {
            continue;
        }
        let name = block
            .get("name")
            .and_then(Value::as_str)
            .unwrap_or_default();
        let Some((file_path, new_content, edits)) = tool_use_payload(name, block.get("input"))
        else {
            continue;
        };
        out.push(FileEdit {
            seq: *next_seq,
            ts,
            tool: name.to_string(),
            file_name: crate::util::file_basename(&file_path),
            file_path,
            new_content,
            edits,
        });
        *next_seq += 1;
    }
}

fn collect_file_edit_results(
    message: &Value,
    pending: &mut PendingFileMutations,
    out: &mut Vec<FileEdit>,
) {
    let Some(content) = message.get("content").and_then(Value::as_array) else {
        return;
    };
    for block in content {
        if block.get("type").and_then(Value::as_str) != Some("tool_result") {
            continue;
        }
        pending.finish(
            block.get("tool_use_id").and_then(Value::as_str),
            // Observed Claude Code records use both `is_error:false` and an omitted flag for
            // successful results; only `is_error:true` is an explicit failure.
            block.get("is_error").and_then(Value::as_bool) != Some(true),
            out,
        );
    }
}

/// Map a single file-mutating tool call to `(file_path, full_content?, edits)`.
/// `Write` yields a full-content snapshot; `Edit`/`MultiEdit` yield delta ops (carrying
/// the `replace_all` flag); `NotebookEdit` is recorded (path only) so it appears in
/// history/cross-ref, but carries no replayable delta (cell reconstruction is out of scope).
fn tool_use_payload(name: &str, input: Option<&Value>) -> Option<FileMutationPayload> {
    let input = input?;
    let str_field = |key: &str| input.get(key).and_then(Value::as_str).map(str::to_string);
    match name {
        "Write" => {
            let file_path = str_field("file_path")?;
            let content = str_field("content").unwrap_or_default();
            Some((file_path, Some(content), Vec::new()))
        }
        "Edit" => {
            let file_path = str_field("file_path")?;
            let old = str_field("old_string").unwrap_or_default();
            let new = str_field("new_string").unwrap_or_default();
            let replace_all = input
                .get("replace_all")
                .and_then(Value::as_bool)
                .unwrap_or(false);
            Some((
                file_path,
                None,
                vec![EditOp {
                    old,
                    new,
                    replace_all,
                }],
            ))
        }
        "MultiEdit" => {
            let file_path = str_field("file_path")?;
            let edits = input
                .get("edits")
                .and_then(Value::as_array)
                .map(|items| {
                    items
                        .iter()
                        .filter_map(|item| {
                            let old = item.get("old_string").and_then(Value::as_str)?;
                            let new = item.get("new_string").and_then(Value::as_str)?;
                            let replace_all = item
                                .get("replace_all")
                                .and_then(Value::as_bool)
                                .unwrap_or(false);
                            Some(EditOp {
                                old: old.to_string(),
                                new: new.to_string(),
                                replace_all,
                            })
                        })
                        .collect()
                })
                .unwrap_or_default();
            Some((file_path, None, edits))
        }
        "NotebookEdit" => {
            let file_path = str_field("notebook_path").or_else(|| str_field("file_path"))?;
            Some((file_path, None, Vec::new()))
        }
        // Cursor's primary edit tool: a unified-diff patch. Recorded path-only (the diff is
        // not a replayable Write/Edit delta), so it shows up in files search/history/cross-ref
        // but is not reconstructable via `files extract`.
        "ApplyPatch" => {
            let file_path = str_field("path").or_else(|| str_field("file_path"))?;
            Some((file_path, None, Vec::new()))
        }
        _ => None,
    }
}

fn tag_content<'a>(text: &'a str, tag: &str) -> &'a str {
    let open = &format!("<{tag}>");
    let close = &format!("</{tag}>");
    text.find(open.as_str())
        .map(|i| &text[i + open.len()..])
        .and_then(|s| s.find(close.as_str()).map(|j| &s[..j]))
        .unwrap_or("")
}

/// For slash-command invocations, return `"<command-name> <command-args>"` so the
/// command identity survives the markup strip. Keeping the leading `/name` is what
/// lets `classify_role` mark the turn `Role::Slash` and lets planning aggregation
/// recover the command via `slash_command_token` — dropping it (as the previous
/// args-only strip did) misclassified every real slash command as a plain user
/// message and undercounted planning usage. Messages without the markup pass through
/// unchanged; no-arg invocations are dropped earlier by `should_skip_message`.
fn strip_command_markup(text: &str) -> String {
    if !text.contains("<command-name>") {
        return text.to_string();
    }
    let mut name = tag_content(text, "command-name").trim().to_string();
    if !name.is_empty() && !name.starts_with('/') {
        name.insert(0, '/');
    }
    let args = tag_content(text, "command-args").trim();
    match (name.is_empty(), args.is_empty()) {
        (true, _) => args.to_string(),
        (false, true) => name,
        (false, false) => format!("{name} {args}"),
    }
}

/// The first `tool_result` content block of a (role:user) message, if any. Single scan shared by
/// [`is_tool_result`] (block EXISTS → classify as `tool`) and [`tool_result_id`] (the block's
/// `tool_use_id`, which may be absent even when the block exists).
pub(crate) fn first_tool_result_block(message: &Value) -> Option<&Value> {
    message
        .get("content")
        .and_then(Value::as_array)?
        .iter()
        .find(|block| block.get("type").and_then(Value::as_str) == Some("tool_result"))
}

/// True when a (role:user) message is actually a tool result — its `content` array
/// carries a `tool_result` block. Claude Code records tool output this way, so these
/// must be classified `tool`, not `user`, to keep user/correction analytics clean. NOTE: a
/// `tool_result` block may carry no `tool_use_id`; it is STILL a tool result (only the name
/// tag is unavailable), so this must not collapse to `tool_result_id(...).is_some()`.
pub(crate) fn is_tool_result(message: &Value) -> bool {
    first_tool_result_block(message).is_some()
}

/// Record `tool_use_id -> tool name` for every `tool_use` block in an assistant message.
/// A later `tool_result` references its call by id but does not repeat the tool name, so
/// this map lets the tool-output message be tagged with the tool it came from.
pub(crate) fn collect_tool_use_names(message: &Value, out: &mut HashMap<String, String>) {
    let Some(content) = message.get("content").and_then(Value::as_array) else {
        return;
    };
    for block in content {
        if block.get("type").and_then(Value::as_str) != Some("tool_use") {
            continue;
        }
        if let (Some(id), Some(name)) = (
            block.get("id").and_then(Value::as_str),
            block.get("name").and_then(Value::as_str),
        ) {
            out.insert(id.to_string(), name.to_string());
        }
    }
}

/// Index assistant `tool_use` inputs as searchable tool messages. Tool results remain separate
/// rows, so users can search both what the agent called and what came back.
pub(crate) fn append_tool_use_messages(
    message: &Value,
    ts: Option<DateTime<Utc>>,
    out: &mut Vec<RawMessage>,
) {
    let Some(content) = message.get("content").and_then(Value::as_array) else {
        return;
    };
    for block in content {
        if block.get("type").and_then(Value::as_str) != Some("tool_use") {
            continue;
        }
        let Some(name) = block.get("name").and_then(Value::as_str) else {
            continue;
        };
        let args = block.get("input").cloned().unwrap_or(Value::Null);
        out.push(RawMessage::tool_call(
            name,
            args,
            block.get("id").and_then(Value::as_str),
            ts,
        ));
    }
}

/// The `tool_use_id` of the first `tool_result` block in a (role:user) message — the key
/// to look up the originating tool's name in the [`collect_tool_use_names`] map.
pub(crate) fn tool_result_id(message: &Value) -> Option<&str> {
    first_tool_result_block(message)
        .and_then(|block| block.get("tool_use_id").and_then(Value::as_str))
}

/// Whether this record is the harness talking to the agent rather than the user or model.
///
/// These were previously dropped outright, which also removed the only evidence of Stop-hook
/// denials and PreToolUse blocks: 82 of 82 "CANNOT STOP" records in one session carried
/// `isMeta` and none was searchable. They are now stored as `MessageKind::HarnessNotice` and
/// excluded from results by default, which keeps user prose and its analytics unchanged while
/// leaving the evidence reachable.
fn is_harness_notice(value: &Value, text: &str) -> bool {
    let normalized = text.trim();
    // Claude Code's own marker for bookkeeping injected as role:user: local-command caveats,
    // hook feedback ("Stop hook feedback: …"), system notices.
    if value
        .get("isMeta")
        .and_then(Value::as_bool)
        .unwrap_or(false)
    {
        return true;
    }
    // Background-task completion notices are injected as role:user (userType "external")
    // WITHOUT an isMeta flag, so the check above misses them.
    if normalized.starts_with("<task-notification") {
        return true;
    }
    // Local-command machinery recorded as role:user — slash-command stdout/stderr and caveats
    // (e.g. `/model` "Set model to …" stdout, `/compact` PreCompact hook output). Harness
    // output, not user prompts; counting them as prose would pollute corrections, repeat
    // mining, and user search. The empty type:"system" stdout variant is already ignored
    // (non user/assistant role); this catches the type:"user" content-string form.
    normalized.starts_with("<local-command-stdout>")
        || normalized.starts_with("<local-command-stderr>")
        || normalized.starts_with("<local-command-caveat>")
}

/// How a claude transcript relates to the session that spawned it.
///
/// Subagent transcripts are indexed as sessions in their own right, because what a subagent did
/// is worth searching — 4,051 of them on this machine against 858 top-level claude sessions.
/// They need an identity that is neither the parent's nor the bare agent id:
///
/// - Every record inside carries the PARENT's `sessionId`, never the run's own, so binding the
///   id from content points the row at the parent and the `on conflict(id) do update` upsert in
///   db.rs overwrites the parent with the subagent's content. Four parent rows were found
///   damaged this way on live data.
/// - The agent id is unique only within one parent: `agent-a0e105ee7f1fe2c65` appears under two
///   different parents here, so the bare id merges two runs into one row.
///
/// Both variants therefore resolve to a parent-qualified id; see [`SpawnOrigin`].
///
/// Decided from the path so discovery stays a directory walk without opening files. The
/// authoritative in-file marker is `isSidechain: true`, which every observed transcript carries
/// alongside an `agentId`; the parse reads the `agentId` for the label.
#[derive(Debug, Clone, PartialEq, Eq)]
enum ClaudeSpawn {
    /// A top-level session: no parent, and its id comes from its own records as before.
    TopLevel,
    /// A run under `<parent-session-id>/subagents/`, where the path names both the parent and
    /// what distinguishes this run from its siblings. 4,047 of the 4,051 here.
    Nested(SpawnOrigin),
    /// A run stored directly beside its parent as `<project>/agent-<id>.jsonl`, 4 files here.
    /// The path names no parent, so the parent comes from the records instead.
    BesideParent { run_suffix: String },
}

impl ClaudeSpawn {
    fn for_path(path: &Path) -> Self {
        let Some(stem) = path.file_stem().and_then(|stem| stem.to_str()) else {
            return Self::TopLevel;
        };
        if !stem.starts_with(SUBAGENT_FILE_PREFIX) {
            return Self::TopLevel;
        }
        match spawn::subagents_dir_origin(path) {
            Some(origin) => Self::Nested(origin),
            None => Self::BesideParent {
                run_suffix: stem.to_string(),
            },
        }
    }

    fn is_subagent(&self) -> bool {
        !matches!(self, Self::TopLevel)
    }
}

/// The `agent-<id>.meta.json` claude writes beside a subagent transcript, holding the only
/// record of what the agent was asked to do and what kind of agent it was. Present for 1,808
/// of the 4,051 transcripts here, so everything it supplies has a fallback.
struct ClaudeSubagentSidecar {
    path: PathBuf,
    /// The sidecar object verbatim. Kept whole rather than destructured into named fields, so a
    /// key added upstream reaches `raw_metadata_json` without a change here; `agent_type` and
    /// `description` are read out of it for the typed label and the title.
    value: Value,
}

impl ClaudeSubagentSidecar {
    /// The name a reader recognizes for this kind of agent: `Explore`, `general-purpose`,
    /// `gsd-executor`. Codex's `agent_nickname` is the same idea.
    fn agent_type(&self) -> Option<&str> {
        self.value.get("agentType").and_then(Value::as_str)
    }

    /// The task the spawning session gave this agent, written by the spawner — a better title
    /// than the agent's own first turn, which is that same task restated at length.
    fn description(&self) -> Option<&str> {
        self.value.get("description").and_then(Value::as_str)
    }
}

/// Where a subagent transcript's sidecar would be: `agent-<id>.jsonl` → `agent-<id>.meta.json`.
/// `None` for anything that is not a subagent transcript, so ordinary sessions cost no stat.
fn claude_subagent_sidecar_path(path: &Path) -> Option<PathBuf> {
    ClaudeSpawn::for_path(path)
        .is_subagent()
        .then(|| path.with_extension("meta.json"))
}

fn claude_subagent_sidecar(path: &Path) -> Option<ClaudeSubagentSidecar> {
    let path = claude_subagent_sidecar_path(path)?;
    let raw = fs::read_to_string(&path).ok()?;
    let value = serde_json::from_str::<Value>(&raw).ok()?;
    Some(ClaudeSubagentSidecar { path, value })
}

fn mtime_ns(metadata: &fs::Metadata) -> i64 {
    metadata
        .modified()
        .ok()
        .and_then(|value| value.duration_since(std::time::UNIX_EPOCH).ok())
        .map(|value| value.as_nanos() as i64)
        .unwrap_or_default()
}

/// Fold a sidecar's mtime and size into the transcript's [`SourceFile`], so editing metadata
/// stored beside an unchanged transcript still re-parses the session. Without this the indexer
/// compares the transcript alone, sees no change, and keeps the stale row.
fn fold_sidecar(source: &mut SourceFile, sidecar: &Path) -> Option<ProviderPathWarning> {
    match sidecar.metadata() {
        Ok(metadata) => {
            source.mtime_ns = source.mtime_ns.max(mtime_ns(&metadata));
            source.size_bytes = source.size_bytes.saturating_add(metadata.len() as i64);
            None
        }
        Err(error) if error.kind() == ErrorKind::NotFound => None,
        Err(error) => Some(ProviderPathWarning {
            path: sidecar.to_path_buf(),
            operation: "read_sidecar",
            message: error.to_string(),
        }),
    }
}

fn should_skip_message(value: &Value, text: &str) -> bool {
    let normalized = text.trim();
    let _ = value;
    // Skip slash command invocations that carry no args — pure UI bookkeeping.
    // Invocations with args pass through; strip_command_markup keeps the command token and args.
    (normalized.contains("<command-name>")
        && tag_content(normalized, "command-args").trim().is_empty())
        || normalized.eq_ignore_ascii_case("resume cancelled")
}

#[cfg(test)]
mod tests {
    use super::{is_harness_notice, should_skip_message, ClaudeAdapter};
    use crate::models::Provider;
    use serde_json::json;
    use tempfile::tempdir;

    // These four cases were previously asserted against `should_skip_message`, i.e. that the
    // records were discarded. They are now stored as `MessageKind::HarnessNotice` and excluded
    // at query time instead, because discarding them also removed the only evidence of what a
    // hook told an agent: 82 of 82 "CANNOT STOP" records in one session were unsearchable.
    // Each test asserts the record is CLASSIFIED as a notice and NOT skipped, so a regression
    // to dropping fails here rather than silently emptying a category.

    /// A subagent transcript is worth searching, so it is indexed as a session of its own. It
    /// needs its own identity to be stored at all: every record inside carries the PARENT's
    /// `sessionId`, so binding from content points the row at the parent and the on-conflict
    /// upsert overwrites the parent's row with the subagent's content. Four such files were
    /// found colliding this way on live data, each reported as unindexed with no cause until
    /// `doctor --explain-unindexed` named the holder.
    #[test]
    fn a_subagent_transcript_keeps_its_own_id_and_records_its_parent() {
        let temp = tempdir().unwrap();
        let project = temp.path().join("-tmp-proj");
        std::fs::create_dir_all(&project).unwrap();
        let parent = "7dc41785-a360-4c22-8db9-db6e37e9db23";
        std::fs::write(
            project.join(format!("{parent}.jsonl")),
            format!(
                r#"{{"type":"user","sessionId":"{parent}","cwd":"/tmp/proj","message":{{"role":"user","content":"the real session"}}}}
"#
            ),
        )
        .unwrap();
        // Real shape: isSidechain, an agentId, and the PARENT's sessionId on every record.
        std::fs::write(
            project.join("agent-0ccb8736.jsonl"),
            format!(
                r#"{{"type":"user","isSidechain":true,"agentId":"0ccb8736","sessionId":"{parent}","userType":"external","cwd":"/tmp/proj","message":{{"role":"user","content":"subagent turn"}}}}
"#
            ),
        )
        .unwrap();

        let adapter = ClaudeAdapter::new(vec![temp.path().to_path_buf()]);
        let sources = adapter.discover();
        assert_eq!(sources.len(), 2, "both are indexed: {sources:#?}");

        let subagent = sources
            .iter()
            .find(|s| s.path.ends_with("agent-0ccb8736.jsonl"))
            .expect("the subagent transcript is discovered, not skipped");
        let parsed = adapter.parse(subagent);
        // Parent-qualified rather than the bare `agent-0ccb8736`: taking the parent's id
        // overwrites the parent's row, and the bare agent id merges two runs that share it
        // under different parents (one such pair among 4,051 live transcripts). Both halves
        // are needed, so the id carries both.
        assert_eq!(
            parsed.session.provider_session_id,
            format!("{parent}/agent-0ccb8736")
        );

        // Typed fields, not `raw_metadata_json` keys: the link has to be queryable, and every
        // provider produces this same shape. It holds the parent row's whole `id`, so the two
        // records show the same string and no prefix rule stands between them.
        assert_eq!(
            parsed.session.parent_session_id.as_deref(),
            Some(format!("claude:{parent}").as_str()),
            "the link back to the spawning session is what makes subagent work useful"
        );
        assert_eq!(parsed.session.agent_label.as_deref(), Some("0ccb8736"));
        assert_eq!(
            parsed.messages[0].provenance.authorship,
            crate::models::MessageAuthorship::Agent,
            "a user-role turn in a structurally identified subagent transcript is agent delegation"
        );

        // The parent still parses to itself, unaffected.
        let parent_source = sources
            .iter()
            .find(|s| s.path.ends_with(format!("{parent}.jsonl")))
            .unwrap();
        let parsed_parent = adapter.parse(parent_source);
        assert_eq!(parsed_parent.session.provider_session_id, parent);
        assert_eq!(
            parsed_parent.messages[0].provenance.authorship,
            crate::models::MessageAuthorship::Human
        );
    }

    #[test]
    fn mixed_user_text_and_tool_result_preserve_searchable_parts_and_human_transcript() {
        let temp = tempdir().unwrap();
        let path = temp.path().join("mixed-session.jsonl");
        std::fs::write(
            &path,
            r#"{"type":"user","uuid":"mixed-1","sessionId":"mixed-session","message":{"role":"user","content":[{"type":"text","text":"before α"},{"type":"tool_result","tool_use_id":"tool-1","content":"output β"},{"type":"text","text":"after γ"}]}}"#,
        )
        .unwrap();

        let adapter = ClaudeAdapter::new(vec![temp.path().to_path_buf()]);
        let parsed = adapter.parse(&adapter.discover()[0]);

        assert_eq!(parsed.messages.len(), 1);
        let message = &parsed.messages[0];
        assert_eq!(message.role, crate::models::Role::User);
        assert_eq!(message.kind, crate::models::MessageKind::Conversation);
        assert_eq!(
            message.provenance.authorship,
            crate::models::MessageAuthorship::Mixed
        );
        assert_eq!(message.content, "before α\noutput β\nafter γ");
        assert_eq!(
            message
                .provenance
                .content_parts
                .iter()
                .map(|part| part.authorship)
                .collect::<Vec<_>>(),
            vec![
                crate::models::ContentPartAuthorship::Human,
                crate::models::ContentPartAuthorship::Generated,
                crate::models::ContentPartAuthorship::Human,
            ]
        );
        message
            .provenance
            .validate(&message.content)
            .expect("mixed parts exactly cover the Unicode-scalar content");
        assert!(
            message.tool_call_id.is_none(),
            "one mixed record cannot use one tool-call id as the identity of all parts"
        );
        assert!(parsed.transcript_text.contains("before α"));
        assert!(parsed.transcript_text.contains("after γ"));
        assert!(!parsed.transcript_text.contains("output β"));
    }

    #[test]
    fn subagent_tool_reference_without_text_does_not_fabricate_mixed_authorship() {
        let temp = tempdir().unwrap();
        let parent = "2eb8e351-15b9-4f06-b8f3-cf610843600a";
        let subagents = temp.path().join(parent).join("subagents");
        std::fs::create_dir_all(&subagents).unwrap();
        let path = subagents.join("agent-acompact-3d346a6bf6f19caa.jsonl");
        std::fs::write(
            &path,
            format!(
                r#"{{"type":"user","isSidechain":true,"agentId":"acompact-3d346a6bf6f19caa","sessionId":"{parent}","message":{{"role":"user","content":[{{"type":"tool_result","content":[{{"type":"tool_reference","tool_name":"example"}}]}},{{"type":"text","text":"Tool loaded."}}]}}}}"#
            ),
        )
        .unwrap();

        let adapter = ClaudeAdapter::new(vec![temp.path().to_path_buf()]);
        let parsed = adapter.parse(&adapter.discover()[0]);

        assert_eq!(parsed.messages.len(), 1);
        let message = &parsed.messages[0];
        assert_eq!(message.content, "Tool loaded.");
        assert_eq!(message.role, crate::models::Role::User);
        assert_eq!(message.kind, crate::models::MessageKind::Conversation);
        assert_eq!(
            message.provenance.authorship,
            crate::models::MessageAuthorship::Agent,
            "a non-text tool reference contributes no searchable authored region"
        );
        assert!(
            message.provenance.content_parts.is_empty(),
            "a single surviving authorship must not be represented as mixed"
        );
        message
            .provenance
            .validate(&message.content)
            .expect("the exact live subagent shape must remain persistable");
    }

    /// Most subagent transcripts live in a `subagents` directory under their parent — 4,047 of
    /// the 4,051 on this machine — and were skipped outright by discovery.
    ///
    /// The id has to be parent-qualified rather than the bare `agent-<id>`, because that id is
    /// unique only within one parent: `agent-a0e105ee7f1fe2c65` appears under two different
    /// parents on live data. Under the bare id the `on conflict(id) do update` upsert in db.rs
    /// keeps whichever was written last and the other run is silently gone. This asserts which
    /// parent each row belongs to, not merely that the two ids differ — swapping them would
    /// preserve distinctness and pass a weaker check.
    #[test]
    fn two_subagents_sharing_an_agent_id_under_different_parents_both_survive() {
        let temp = tempdir().unwrap();
        let project = temp.path().join("-tmp-proj");
        let shared_agent = "a0e105ee7f1fe2c65";
        let parents = [
            "a2f3f693-e77f-4212-9e71-2b2331565fd4",
            "f2c5b7c7-c4bc-4ab3-bb21-7c381637b8ce",
        ];
        for parent in parents {
            let subagents = project.join(parent).join("subagents");
            std::fs::create_dir_all(&subagents).unwrap();
            std::fs::write(
                project.join(format!("{parent}.jsonl")),
                format!(
                    r#"{{"type":"user","sessionId":"{parent}","cwd":"/tmp/proj","message":{{"role":"user","content":"parent {parent}"}}}}
"#
                ),
            )
            .unwrap();
            // Real shape: isSidechain, the subagent's agentId, and the PARENT's sessionId.
            std::fs::write(
                subagents.join(format!("agent-{shared_agent}.jsonl")),
                format!(
                    r#"{{"type":"user","isSidechain":true,"agentId":"{shared_agent}","sessionId":"{parent}","userType":"external","cwd":"/tmp/proj","message":{{"role":"user","content":"work for {parent}"}}}}
"#
                ),
            )
            .unwrap();
        }

        let adapter = ClaudeAdapter::new(vec![temp.path().to_path_buf()]);
        let sources = adapter.discover();
        assert_eq!(
            sources.len(),
            4,
            "two parents and two subagent runs are all sessions: {sources:#?}"
        );

        for parent in parents {
            let source = sources
                .iter()
                .find(|source| {
                    source.path.ends_with(format!("agent-{shared_agent}.jsonl"))
                        && source.path.to_string_lossy().contains(parent)
                })
                .expect("each parent's subagent transcript is discovered");
            let parsed = adapter.parse(source);
            assert_eq!(
                parsed.session.provider_session_id,
                format!("{parent}/agent-{shared_agent}"),
                "the run must be identified by its parent plus what distinguishes it there"
            );
            assert_eq!(
                parsed.session.parent_session_id.as_deref(),
                Some(format!("claude:{parent}").as_str())
            );
            assert!(
                parsed
                    .transcript_text
                    .contains(&format!("work for {parent}")),
                "each row must hold its OWN transcript, not the other run's"
            );
        }

        // The two ids are what the db keys on, so state the consequence directly.
        let ids: Vec<String> = sources
            .iter()
            .map(|source| adapter.parse(source).session.id)
            .collect();
        let unique: std::collections::HashSet<&String> = ids.iter().collect();
        assert_eq!(
            unique.len(),
            ids.len(),
            "colliding ids overwrite rows: {ids:?}"
        );
    }

    /// Workflow agents nest one level deeper (`subagents/workflows/wf_<id>/agent-<id>.jsonl`).
    /// The workflow's own `journal.jsonl` sits beside them but is the workflow engine's record
    /// of agent return values, not a conversation, so it is not a session.
    #[test]
    fn a_workflow_agent_is_indexed_and_its_journal_is_not() {
        let temp = tempdir().unwrap();
        let parent = "77f26fc7-6ca3-4a98-a8b5-32f1963941ab";
        let workflow = temp
            .path()
            .join("-tmp-proj")
            .join(parent)
            .join("subagents")
            .join("workflows")
            .join("wf_4b4d88ab-f99");
        std::fs::create_dir_all(&workflow).unwrap();
        std::fs::write(
            workflow.join("agent-ae4f8452cb555e0bd.jsonl"),
            format!(
                r#"{{"type":"user","isSidechain":true,"agentId":"ae4f8452cb555e0bd","sessionId":"{parent}","cwd":"/tmp/proj","message":{{"role":"user","content":"review the diff"}}}}
"#
            ),
        )
        .unwrap();
        std::fs::write(
            workflow.join("journal.jsonl"),
            r#"{"agentId":"ae4f8452cb555e0bd","result":"done"}
"#,
        )
        .unwrap();

        let adapter = ClaudeAdapter::new(vec![temp.path().to_path_buf()]);
        let sources = adapter.discover();
        assert_eq!(
            sources.len(),
            1,
            "only the agent transcript is a session: {sources:#?}"
        );
        let parsed = adapter.parse(&sources[0]);
        assert_eq!(
            parsed.session.provider_session_id,
            format!("{parent}/workflows/wf_4b4d88ab-f99/agent-ae4f8452cb555e0bd"),
            "the workflow is what distinguishes sibling runs, so it stays in the id"
        );
        assert_eq!(
            parsed.session.parent_session_id.as_deref(),
            Some(format!("claude:{parent}").as_str())
        );
    }

    /// A subagent transcript can have an `agent-<id>.meta.json` sidecar beside it — 1,808 of
    /// 4,051 do. It holds the only description of what the agent was asked to do and the only
    /// name for what kind of agent it was, so it supplies the title and the label. Keys beyond
    /// those two are kept verbatim rather than enumerated, so a key added upstream survives
    /// without a code change here.
    #[test]
    fn a_subagent_sidecar_supplies_the_title_the_label_and_its_other_keys() {
        let temp = tempdir().unwrap();
        let parent = "7e745098-c299-4cf5-bdbe-5cdb1fb5a62d";
        let subagents = temp.path().join("-tmp-proj").join(parent).join("subagents");
        std::fs::create_dir_all(&subagents).unwrap();
        std::fs::write(
            subagents.join("agent-a068fa115f7299f0c.jsonl"),
            format!(
                r#"{{"type":"user","isSidechain":true,"agentId":"a068fa115f7299f0c","sessionId":"{parent}","cwd":"/tmp/proj","message":{{"role":"user","content":"Read these files in full"}}}}
"#
            ),
        )
        .unwrap();
        std::fs::write(
            subagents.join("agent-a068fa115f7299f0c.meta.json"),
            r#"{"agentType":"Explore","description":"Read cship and starship config files in full","spawnDepth":1,"model":"claude-haiku-4-5-20251001"}"#,
        )
        .unwrap();

        let adapter = ClaudeAdapter::new(vec![temp.path().to_path_buf()]);
        let sources = adapter.discover();
        assert_eq!(sources.len(), 1, "the sidecar is metadata, not a session");
        let parsed = adapter.parse(&sources[0]);

        assert_eq!(
            parsed.session.agent_label.as_deref(),
            Some("Explore"),
            "agentType is the name a reader recognizes; the agentId is already in the session id"
        );
        assert_eq!(
            parsed.session.title.as_deref(),
            Some("Read cship and starship config files in full"),
            "the spawner's description of the task is a better title than the agent's own first turn"
        );
        let raw: serde_json::Value =
            serde_json::from_str(parsed.session.raw_metadata_json.as_deref().unwrap()).unwrap();
        assert_eq!(raw["subagent"]["spawnDepth"], json!(1));
        assert_eq!(raw["subagent"]["model"], json!("claude-haiku-4-5-20251001"));
        assert!(
            raw["metadata_path"]
                .as_str()
                .is_some_and(|path| path.ends_with("agent-a068fa115f7299f0c.meta.json")),
            "the sidecar path is recorded like the desktop sidecar's: {raw}"
        );
    }

    /// Only 1,808 of 4,051 transcripts have a sidecar, so the label falls back to the agent id
    /// the records carry rather than going empty.
    #[test]
    fn a_subagent_without_a_sidecar_labels_itself_with_its_agent_id() {
        let temp = tempdir().unwrap();
        let parent = "7e745098-c299-4cf5-bdbe-5cdb1fb5a62d";
        let subagents = temp.path().join("-tmp-proj").join(parent).join("subagents");
        std::fs::create_dir_all(&subagents).unwrap();
        std::fs::write(
            subagents.join("agent-a068fa115f7299f0c.jsonl"),
            format!(
                r#"{{"type":"user","isSidechain":true,"agentId":"a068fa115f7299f0c","sessionId":"{parent}","cwd":"/tmp/proj","message":{{"role":"user","content":"find the caller"}}}}
"#
            ),
        )
        .unwrap();

        let adapter = ClaudeAdapter::new(vec![temp.path().to_path_buf()]);
        let sources = adapter.discover();
        let parsed = adapter.parse(&sources[0]);
        assert_eq!(
            parsed.session.agent_label.as_deref(),
            Some("a068fa115f7299f0c")
        );
        assert_eq!(
            parsed.session.title.as_deref(),
            Some("find the caller"),
            "with no description to use, the title comes from the transcript as it always has"
        );
    }

    #[test]
    fn classifies_local_command_output_as_harness_notice() {
        // `/model`, `/compact`-hook etc. record their stdout/stderr as a role:user message
        // (type:"user", content is a bare string). Harness output, not a prompt.
        let value = json!({ "type": "user", "message": {"role": "user"} });
        for text in [
            "<local-command-stdout>Set model to Opus 4.8 and saved as your default</local-command-stdout>",
            "<local-command-stderr>boom</local-command-stderr>",
            "<local-command-caveat>note</local-command-caveat>",
        ] {
            assert!(is_harness_notice(&value, text), "not classified: {text}");
            assert!(!should_skip_message(&value, text), "discarded: {text}");
        }
        // A real prompt that merely mentions the tag name (not leading with it) stays prose.
        let prose = "what does <local-command-stdout> mean when it shows up in the logs";
        assert!(!is_harness_notice(&value, prose));
        assert!(!should_skip_message(&value, prose));
    }

    #[test]
    fn classifies_local_command_caveat_meta_messages_as_harness_notice() {
        let text = "<local-command-caveat>Caveat: The messages below were generated by the user while running local commands.</local-command-caveat>";
        let value = json!({
            "isMeta": true,
            "message": { "role": "user", "content": text }
        });
        assert!(is_harness_notice(&value, text));
        assert!(!should_skip_message(&value, text));
    }

    #[test]
    fn keeps_normal_user_messages() {
        let value = json!({
            "isMeta": false,
            "message": {
                "role": "user",
                "content": "real prompt"
            }
        });
        assert!(!is_harness_notice(&value, "real prompt"));
        assert!(!should_skip_message(&value, "real prompt"));
    }

    #[test]
    fn classifies_hook_feedback_as_harness_notice() {
        // The exact record class that was unsearchable: hook output injected as a meta
        // role:user message with arbitrary text.
        let text = "Stop hook feedback: 🛑 CANNOT STOP — incomplete tasks: 1. #24";
        let value = json!({ "isMeta": true, "message": {"role": "user", "content": text} });
        assert!(is_harness_notice(&value, text));
        assert!(!should_skip_message(&value, text));
    }

    #[test]
    fn classifies_background_task_notifications_as_harness_notice() {
        // Background-task completion notices are injected as role:user with userType
        // "external" and NO isMeta flag, so they are matched on their leading tag instead.
        let text = "<task-notification>\n<task-id>bbawn9c36</task-id>\n<tool-use-id>toolu_01</tool-use-id>\n<output-file>/tmp/out.txt</output-file>\nAgent completed.";
        let value = json!({ "isMeta": false, "message": {"role": "user", "content": text} });
        assert!(is_harness_notice(&value, text));
        assert!(!should_skip_message(&value, text));
        // Leading whitespace must not defeat the match.
        let padded = format!("\n  {text}");
        assert!(is_harness_notice(&value, &padded));
        // A real prompt that merely mentions the word stays prose.
        let prose = "the task-notification format is confusing, can you explain it";
        assert!(!is_harness_notice(&value, prose));
        assert!(!should_skip_message(&value, prose));
    }

    #[test]
    fn skips_no_arg_slash_commands() {
        for cmd in &[
            "/exit", "/resume", "/clear", "/compact", "/mcp", "/config", "/help",
        ] {
            let text = format!("<command-name>{cmd}</command-name><command-message>{cmd}</command-message><command-args></command-args>");
            let value = json!({ "isMeta": false });
            assert!(
                should_skip_message(&value, &text),
                "should skip {cmd} (no args)"
            );
        }
    }

    #[test]
    fn keeps_slash_commands_with_args() {
        let text = "<command-name>/review-url</command-name><command-message>review-url</command-message><command-args>https://example.com/review/1</command-args>";
        let value = json!({ "isMeta": false });
        assert!(!should_skip_message(&value, text));
    }

    #[test]
    fn strip_command_markup_preserves_command_name_and_args() {
        // The command name is kept so the turn classifies as Role::Slash and planning
        // aggregation can recover `/review-url` via slash_command_token.
        let text = "<command-name>/review-url</command-name><command-message>review-url</command-message><command-args>https://example.com/review/1</command-args>";
        let stripped = super::strip_command_markup(text);
        assert_eq!(stripped, "/review-url https://example.com/review/1");
        assert_eq!(
            crate::util::classify_role("user", &stripped),
            crate::models::Role::Slash
        );
        assert_eq!(
            crate::util::slash_command_token(&stripped).as_deref(),
            Some("/review-url")
        );
    }

    #[test]
    fn strip_command_markup_normalizes_missing_leading_slash() {
        let text = "<command-name>effort</command-name><command-message>effort</command-message><command-args>max</command-args>";
        assert_eq!(super::strip_command_markup(text), "/effort max");
    }

    #[test]
    fn strip_command_markup_leaves_normal_messages() {
        assert_eq!(
            super::strip_command_markup("fix the bug in db.rs"),
            "fix the bug in db.rs"
        );
    }

    #[test]
    fn detects_tool_result_messages() {
        // role:user but a tool_result block → tool output, not a real user message.
        let tr = json!({
            "role": "user",
            "content": [{"type": "tool_result", "tool_use_id": "x", "content": "build failed: error"}]
        });
        assert!(super::is_tool_result(&tr));
        // A genuine user prompt is not a tool result.
        let user = json!({"role": "user", "content": [{"type": "text", "text": "fix the build"}]});
        assert!(!super::is_tool_result(&user));
        // Plain string content (no blocks) is not a tool result.
        let plain = json!({"role": "user", "content": "just text"});
        assert!(!super::is_tool_result(&plain));
    }

    #[test]
    fn tool_result_is_tagged_with_originating_tool_name() {
        use std::collections::HashMap;
        // An assistant turn issues a tool call — id -> name is recorded.
        let assistant = json!({
            "role": "assistant",
            "content": [
                {"type": "tool_use", "id": "toolu_1", "name": "Bash", "input": {"command": "ls"}},
                {"type": "text", "text": "running it"}
            ]
        });
        let mut names = HashMap::new();
        super::collect_tool_use_names(&assistant, &mut names);
        assert_eq!(names.get("toolu_1").map(String::as_str), Some("Bash"));
        // The following user tool_result references that call by id, so it can be tagged.
        let result = json!({
            "role": "user",
            "content": [{"type": "tool_result", "tool_use_id": "toolu_1", "content": "a\nb"}]
        });
        let id = super::tool_result_id(&result).expect("tool_use_id present");
        assert_eq!(id, "toolu_1");
        assert_eq!(names.get(id).map(String::as_str), Some("Bash"));
        // A tool_result whose call id is unknown yields no tool name (rather than panicking).
        let orphan = json!({
            "role": "user",
            "content": [{"type": "tool_result", "tool_use_id": "missing", "content": "x"}]
        });
        let oid = super::tool_result_id(&orphan).expect("tool_use_id present");
        assert!(!names.contains_key(oid));
    }

    /// A `tool_result` block that omits `tool_use_id` is STILL a tool result — it must classify
    /// as `tool` (so it stays out of user/correction analytics); only the name tag is unavailable.
    /// Regression guard: an earlier "single scan" optimization collapsed this to
    /// `tool_result_id(...).is_some()`, which dropped the no-id case and mislabeled such messages
    /// as `user`.
    #[test]
    fn tool_result_without_id_is_still_a_tool_result() {
        let no_id = json!({
            "role": "user",
            "content": [{"type": "tool_result", "content": "done"}]
        });
        assert!(super::is_tool_result(&no_id));
        assert!(
            super::tool_result_id(&no_id).is_none(),
            "no id available, but the block still exists"
        );
        let with_id = json!({
            "role": "user",
            "content": [{"type": "tool_result", "tool_use_id": "tu_1", "content": "done"}]
        });
        assert!(super::is_tool_result(&with_id));
        assert_eq!(super::tool_result_id(&with_id), Some("tu_1"));
    }

    /// Differential guard for the streaming-parse refactor (task #241): the streaming
    /// `BufReader` path must produce byte-identical `ParsedSession` output (messages,
    /// transcript_text, file_edits, AND the `line_count` metadata) versus the prior
    /// whole-file `fs::read_to_string` + `raw.lines()` implementation. The fixture
    /// deliberately exercises the line-count edge cases the streaming path could regress:
    /// a leading blank line, an interior blank line, a malformed (non-JSON) line, and a
    /// final line WITHOUT a trailing newline. `str::lines()` and `BufRead::lines()` count
    /// these identically (verified), so `line_count` must stay 7.
    #[test]
    fn streaming_parse_output_is_stable() {
        use super::ClaudeAdapter;
        use crate::models::Provider;
        use std::fs;
        use tempfile::tempdir;

        let temp = tempdir().unwrap();
        let root = temp.path().to_path_buf();
        let session_id = "11111111-2222-3333-4444-555555555555";
        let file = root.join(format!("{session_id}.jsonl"));
        // Line layout (7 lines, no trailing newline on the last):
        //   1: blank          2: user prompt       3: malformed JSON (skipped)
        //   4: assistant + Edit tool_use            5: user tool_result
        //   6: blank          7: final user prompt (NO trailing \n)
        let content = concat!(
            "\n",
            r#"{"sessionId":"11111111-2222-3333-4444-555555555555","type":"user","cwd":"/tmp/proj","timestamp":"2026-06-25T07:00:00.000Z","message":{"role":"user","content":[{"type":"text","text":"first prompt"}]}}"#,
            "\n",
            "{not valid json\n",
            r#"{"type":"assistant","timestamp":"2026-06-25T07:00:01.000Z","message":{"role":"assistant","content":[{"type":"text","text":"on it"},{"type":"tool_use","id":"tu_1","name":"Edit","input":{"file_path":"/tmp/proj/a.rs","old_string":"x","new_string":"y"}}]}}"#,
            "\n",
            r#"{"type":"user","timestamp":"2026-06-25T07:00:02.000Z","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"tu_1","content":"edited"}]}}"#,
            "\n",
            "\n",
            r#"{"type":"user","timestamp":"2026-06-25T07:00:03.000Z","message":{"role":"user","content":[{"type":"text","text":"second prompt"}]}}"#,
        );
        fs::write(&file, content).unwrap();

        let adapter = ClaudeAdapter::new(vec![root]);
        let sources = adapter.discover();
        assert_eq!(sources.len(), 1);
        let parsed = adapter.parse(&sources[0]);

        let tool_call = parsed
            .messages
            .iter()
            .find(|message| message.kind == crate::models::MessageKind::ToolCall)
            .expect("Claude tool_use indexed as a tool call");
        let tool_result = parsed
            .messages
            .iter()
            .find(|message| message.kind == crate::models::MessageKind::ToolResult)
            .expect("Claude tool_result indexed as a tool result");
        assert_eq!(tool_call.tool_call_id.as_deref(), Some("tu_1"));
        assert_eq!(tool_result.tool_call_id.as_deref(), Some("tu_1"));

        // line_count must reflect every physical line (incl. blanks + malformed), = 7.
        assert!(
            parsed
                .session
                .raw_metadata_json
                .as_deref()
                .unwrap()
                .contains("\"line_count\":7"),
            "line_count must be 7, got: {:?}",
            parsed.session.raw_metadata_json
        );
        // Full structural snapshot (source_path stripped — it is an absolute tempdir path).
        assert_eq!(parsed.session.provider, Provider::Claude);
        assert_eq!(parsed.session.message_count, Some(5));
        assert_eq!(
            parsed.session.parse_warning.as_deref(),
            Some("skipped 1 malformed JSONL record")
        );
        let roles: Vec<&str> = parsed.messages.iter().map(|m| m.role.as_str()).collect();
        assert_eq!(roles, vec!["user", "tool", "assistant", "tool", "user"]);
        let contents: Vec<&str> = parsed.messages.iter().map(|m| m.content.as_str()).collect();
        assert_eq!(
            contents,
            vec![
                "first prompt",
                r#"{"args":{"file_path":"/tmp/proj/a.rs","new_string":"y","old_string":"x"},"kind":"tool_call","tool_name":"Edit"}"#,
                "on it",
                "edited",
                "second prompt"
            ]
        );
        // tool_result is tagged with the originating tool name.
        assert_eq!(parsed.messages[1].tool_name.as_deref(), Some("Edit"));
        assert_eq!(parsed.messages[3].tool_name.as_deref(), Some("Edit"));
        // Transcript excludes the tool output; carries the conversation turns in order.
        assert!(parsed.transcript_text.contains("first prompt"));
        assert!(parsed.transcript_text.contains("on it"));
        assert!(parsed.transcript_text.contains("second prompt"));
        assert!(!parsed.transcript_text.contains("edited"));
        // The Edit tool_use produced exactly one file edit.
        assert_eq!(parsed.file_edits.len(), 1);
        assert_eq!(parsed.file_edits[0].file_path, "/tmp/proj/a.rs");
        assert_eq!(parsed.file_edits[0].tool, "Edit");
        assert_eq!(parsed.session.cwd.as_deref(), Some("/tmp/proj"));
        assert_eq!(parsed.session.title.as_deref(), Some("second prompt"));
    }

    #[test]
    fn file_edits_commit_only_after_correlated_successful_tool_results() {
        let temp = tempdir().unwrap();
        let root = temp.path().to_path_buf();
        let session_id = "22222222-3333-4444-5555-666666666666";
        let file = root.join(format!("{session_id}.jsonl"));
        std::fs::write(
            &file,
            r#"{"sessionId":"22222222-3333-4444-5555-666666666666","type":"user","cwd":"/tmp/proj","message":{"role":"user","content":"recover the file"}}
{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","id":"write-ok","name":"Write","input":{"file_path":"/tmp/proj/a.rs","content":"alpha"}}]}}
{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"write-ok","content":"written"}]}}
{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","id":"edit-ok","name":"Edit","input":{"file_path":"/tmp/proj/a.rs","old_string":"alpha","new_string":"beta"}}]}}
{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"edit-ok","is_error":false,"content":"edited"}]}}
{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","id":"edit-failed","name":"Edit","input":{"file_path":"/tmp/proj/a.rs","old_string":"beta","new_string":"corrupt"}}]}}
{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"edit-failed","is_error":true,"content":"old_string not found"}]}}
{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","id":"multi-ok","name":"MultiEdit","input":{"file_path":"/tmp/proj/a.rs","edits":[{"old_string":"beta","new_string":"gamma"},{"old_string":"gamma","new_string":"delta"}]}}]}}
{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"multi-ok","is_error":false,"content":"edited"}]}}
{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","id":"write-later","name":"Write","input":{"file_path":"/tmp/proj/a.rs","content":"snapshot"}}]}}
{"type":"user","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"write-later","is_error":false,"content":"written"}]}}
{"type":"assistant","message":{"role":"assistant","content":[{"type":"tool_use","id":"edit-unresolved","name":"Edit","input":{"file_path":"/tmp/proj/a.rs","old_string":"snapshot","new_string":"not observed"}}]}}
"#,
        )
        .unwrap();

        let adapter = ClaudeAdapter::new(vec![root]);
        let parsed = adapter.parse(&adapter.discover()[0]);
        assert_eq!(
            parsed
                .file_edits
                .iter()
                .map(|edit| (edit.seq, edit.tool.as_str()))
                .collect::<Vec<_>>(),
            [(0, "Write"), (1, "Edit"), (2, "MultiEdit"), (3, "Write")]
        );
        assert_eq!(
            crate::files::reconstruct(&parsed.file_edits, 1).as_deref(),
            Some("alpha")
        );
        assert_eq!(
            crate::files::reconstruct(&parsed.file_edits, 2).as_deref(),
            Some("beta")
        );
        assert_eq!(
            crate::files::reconstruct(&parsed.file_edits, 3).as_deref(),
            Some("delta")
        );
        assert_eq!(
            crate::files::reconstruct(&parsed.file_edits, 4).as_deref(),
            Some("snapshot")
        );
    }

    /// Bytes that are not valid UTF-8 must never panic or abort the parse — they are decoded
    /// lossily (U+FFFD). A valid JSON line carrying a stray non-UTF-8 byte in a string value KEEPS
    /// its message (byte → U+FFFD); a line that is not valid JSON even after lossy decoding is
    /// simply skipped, like any other unparseable line. (Previously a single bad byte made
    /// `read_to_string`/`lines()` error and reduced the ENTIRE session to a minimal record.)
    #[test]
    fn non_utf8_bytes_are_recovered_lossily_not_dropped() {
        use super::ClaudeAdapter;
        use std::fs;
        use tempfile::tempdir;

        let temp = tempdir().unwrap();
        let root = temp.path().to_path_buf();
        let file = root.join("66666666-7777-8888-9999-000000000000.jsonl");
        // Line 1: a valid claude user message whose text holds a raw 0xFF byte (invalid UTF-8).
        // Line 2: bytes that are not valid JSON even after lossy decoding (skipped, like garbage).
        let mut bytes = br#"{"type":"user","sessionId":"s","timestamp":"2026-06-01T10:00:00Z","cwd":"/p","message":{"role":"user","content":[{"type":"text","text":"hi "#.to_vec();
        bytes.push(0xFF);
        bytes.extend_from_slice(br#" there"}]}}"#);
        bytes.push(b'\n');
        bytes.extend_from_slice(&[b'{', 0xFE, b'}', b'\n']);
        fs::write(&file, &bytes).unwrap();

        let adapter = ClaudeAdapter::new(vec![root]);
        let sources = adapter.discover();
        assert_eq!(sources.len(), 1);
        let parsed = adapter.parse(&sources[0]);
        // The valid line's message survived (not dropped by one bad byte); the byte became U+FFFD.
        assert_eq!(
            parsed.messages.len(),
            1,
            "the valid line is recovered, not lost to one bad byte"
        );
        let content = &parsed.messages[0].content;
        assert!(
            content.contains('\u{FFFD}'),
            "the invalid byte became the U+FFFD replacement char: {content:?}"
        );
        assert!(
            content.contains("hi") && content.contains("there"),
            "surrounding text is preserved: {content:?}"
        );
        assert_eq!(parsed.session.message_count, Some(1));
    }

    #[test]
    fn discovers_and_parses_claude_desktop_local_agent_audit_jsonl() {
        use super::ClaudeAdapter;
        use std::fs;
        use tempfile::tempdir;

        let temp = tempdir().unwrap();
        let root = temp.path().join("Claude/local-agent-mode-sessions");
        let parent = root.join("install-id/account-id");
        let session_dir = parent.join("local_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee");
        fs::create_dir_all(&session_dir).unwrap();
        fs::write(
            parent.join("local_aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee.json"),
            r#"{
              "sessionId":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee",
              "cliSessionId":"cli-session-1",
              "cwd":"/tmp/desktop-proj",
              "createdAt":"2026-03-29T19:14:00.000Z",
              "lastActivityAt":"2026-03-29T19:16:00.000Z",
              "title":"Desktop Agent Session",
              "initialMessage":"first desktop request"
            }"#,
        )
        .unwrap();
        fs::write(
            session_dir.join("audit.jsonl"),
            concat!(
                r#"{"type":"user","uuid":"u1","session_id":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","message":{"role":"user","content":"first desktop request"},"_audit_timestamp":"2026-03-29T19:14:24.689Z"}"#,
                "\n",
                "{not json\n",
                r#"{"type":"assistant","uuid":"a1","session_id":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","message":{"role":"assistant","content":[{"type":"text","text":"desktop answer"},{"type":"tool_use","id":"tu_1","name":"Write","input":{"file_path":"/tmp/desktop-proj/out.txt","content":"hello"}}]},"_audit_timestamp":"2026-03-29T19:14:30.000Z"}"#,
                "\n",
                r#"{"type":"user","uuid":"u2","session_id":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","message":{"role":"user","content":[{"type":"tool_result","tool_use_id":"tu_1","content":"wrote file"}]},"_audit_timestamp":"2026-03-29T19:14:31.000Z"}"#,
                "\n",
            ),
        )
        .unwrap();

        let adapter = ClaudeAdapter::new(vec![root]);
        let sources = adapter.discover();
        assert_eq!(sources.len(), 1);
        assert_eq!(
            sources[0].path.file_name().and_then(|n| n.to_str()),
            Some("audit.jsonl")
        );

        let parsed = adapter.parse(&sources[0]);
        assert_eq!(
            parsed.session.id,
            "claude-desktop:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
        );
        assert_eq!(parsed.session.provider, Provider::ClaudeDesktop);
        assert_eq!(
            parsed.session.provider_session_id,
            "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
        );
        assert_eq!(parsed.session.cwd.as_deref(), Some("/tmp/desktop-proj"));
        assert_eq!(
            parsed.session.title.as_deref(),
            Some("Desktop Agent Session")
        );
        assert_eq!(
            parsed.session.summary.as_deref(),
            Some("first desktop request")
        );
        assert_eq!(
            parsed.session.discovery_source,
            "claude-desktop-local-agent-audit-jsonl"
        );
        assert_eq!(
            parsed.session.parse_version,
            "claude-desktop-local-agent-v4"
        );
        assert_eq!(parsed.session.message_count, Some(4));
        let event_ids: Vec<Option<&str>> = parsed
            .messages
            .iter()
            .map(|message| {
                message
                    .provenance
                    .correlation_identity
                    .as_ref()
                    .map(|identity| identity.id.as_str())
            })
            .collect();
        assert_eq!(event_ids, vec![Some("u1"), None, Some("a1"), Some("u2")]);
        for identity in parsed
            .messages
            .iter()
            .filter_map(|message| message.provenance.correlation_identity.as_ref())
        {
            assert_eq!(
                identity.authority,
                crate::models::MessageCorrelationAuthority::Anthropic
            );
            assert_eq!(
                identity.scope,
                "claude-desktop:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
            );
        }
        assert!(
            parsed
                .session
                .raw_metadata_json
                .as_deref()
                .unwrap()
                .contains("\"malformed_line_count\":1"),
            "raw metadata should record skipped malformed lines: {:?}",
            parsed.session.raw_metadata_json
        );
        let roles: Vec<&str> = parsed.messages.iter().map(|m| m.role.as_str()).collect();
        assert_eq!(roles, vec!["user", "tool", "assistant", "tool"]);
        assert_eq!(parsed.messages[1].tool_name.as_deref(), Some("Write"));
        assert_eq!(parsed.messages[3].tool_name.as_deref(), Some("Write"));
        assert_eq!(parsed.file_edits.len(), 1);
        assert_eq!(parsed.file_edits[0].file_path, "/tmp/desktop-proj/out.txt");
        assert!(parsed.transcript_text.contains("first desktop request"));
        assert!(parsed.transcript_text.contains("desktop answer"));
        assert!(!parsed.transcript_text.contains("wrote file"));
    }

    #[test]
    fn claude_desktop_local_agent_without_sidecar_still_indexes_audit_messages() {
        use super::ClaudeAdapter;
        use std::fs;
        use tempfile::tempdir;

        let temp = tempdir().unwrap();
        let root = temp.path().join("local-agent-mode-sessions");
        let session_dir = root.join("install/account/local_ffffffff-1111-2222-3333-444444444444");
        fs::create_dir_all(&session_dir).unwrap();
        fs::write(
            session_dir.join("audit.jsonl"),
            concat!(
                r#"{"type":"user","session_id":"ffffffff-1111-2222-3333-444444444444","message":{"role":"user","content":"sidecar missing but parse me"},"_audit_timestamp":"2026-04-01T00:00:00.000Z"}"#,
                "\n",
                r#"{"type":"event","session_id":"ffffffff-1111-2222-3333-444444444444","payload":{"unknown":true},"_audit_timestamp":"2026-04-01T00:00:01.000Z"}"#,
                "\n",
            ),
        )
        .unwrap();

        let adapter = ClaudeAdapter::new(vec![root]);
        let sources = adapter.discover();
        assert_eq!(sources.len(), 1);
        let parsed = adapter.parse(&sources[0]);
        assert_eq!(
            parsed.session.id,
            "claude-desktop:ffffffff-1111-2222-3333-444444444444"
        );
        assert_eq!(parsed.session.provider, Provider::ClaudeDesktop);
        assert_eq!(parsed.session.cwd, None);
        assert_eq!(
            parsed.session.title.as_deref(),
            Some("sidecar missing but parse me")
        );
        assert_eq!(parsed.session.message_count, Some(1));
        assert_eq!(parsed.messages[0].content, "sidecar missing but parse me");
    }
}