tokmesh-cli 0.1.1

CLI and TUI for tokmesh — AI token usage analytics
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
use anyhow::Result;
use colored::Colorize;
use std::collections::HashMap;
use std::io::Write;
use std::process::{Command, Output, Stdio};
use unicode_normalization::UnicodeNormalization;

use super::apple_fm;
use std::path::PathBuf;
use std::time::Duration;
use tokmesh_core::content_extractor::SessionContent;
use tokmesh_core::content_extractor::{extract_session_content, metadata_only_content};
use tokmesh_core::pricing::PricingService;
use tokmesh_core::wiki::{WikiDb, WikiEntry};
use tokmesh_core::{parse_local_clients, LocalParseOptions, ParsedMessage, TokenBreakdown};

pub struct ReportOptions {
    pub json: bool,
    pub since: Option<String>,
    pub until: Option<String>,
    pub workspace: Option<String>,
    pub client: Option<String>,
    pub no_summarize: bool,
    pub summarizer: String,
    pub rebuild: bool,
    pub home_dir: Option<String>,
    pub scanner_settings: tokmesh_core::scanner::ScannerSettings,
    pub today: bool,
    pub week: bool,
    pub month: bool,
    pub full: bool,
}

pub fn run_report(opts: ReportOptions) -> Result<()> {
    let wiki_path = WikiDb::default_path();
    let db =
        WikiDb::open(&wiki_path).map_err(|e| anyhow::anyhow!("Failed to open wiki DB: {}", e))?;

    populate_wiki_from_sessions(&db, &opts)?;

    let (since_ts, until_ts) = parse_date_range(&opts.since, &opts.until);

    if opts.rebuild {
        let count = db
            .reset_summaries_in_range(since_ts, until_ts)
            .map_err(|e| anyhow::anyhow!("{}", e))?;
        eprintln!("  Reset {} session summaries", count.to_string().cyan());
    }

    let unsummarized = if opts.no_summarize {
        Vec::new()
    } else {
        db.get_unsummarized_session_ids_in_range(since_ts, until_ts)
            .map_err(|e| anyhow::anyhow!("{}", e))?
    };

    if !unsummarized.is_empty() {
        let session_paths = build_session_path_index(&opts);
        run_summarizer(&db, &unsummarized, &opts.summarizer, &session_paths)?;
    }

    let entries = db
        .query_entries(
            since_ts,
            until_ts,
            opts.workspace.as_deref(),
            opts.client.as_deref(),
        )
        .map_err(|e| anyhow::anyhow!("{}", e))?;

    let needs_grouping = entries
        .iter()
        .any(|e| e.title.is_some() && e.task_group.is_none());
    if needs_grouping && !opts.no_summarize {
        run_task_grouping(&db, &entries, &opts.summarizer)?;
        let entries = db
            .query_entries(
                since_ts,
                until_ts,
                opts.workspace.as_deref(),
                opts.client.as_deref(),
            )
            .map_err(|e| anyhow::anyhow!("{}", e))?;

        if opts.json {
            let json = serde_json::to_string_pretty(&entries)?;
            println!("{}", json);
        } else {
            let is_multi_day = opts.week || opts.month || (opts.since.is_some() && !opts.today);
            print_report_table(&entries, &db, is_multi_day, opts.full)?;
        }
    } else if opts.json {
        let json = serde_json::to_string_pretty(&entries)?;
        println!("{}", json);
    } else {
        let is_multi_day = opts.week || opts.month || (opts.since.is_some() && !opts.today);
        print_report_table(&entries, &db, is_multi_day, opts.full)?;
    }

    Ok(())
}

fn populate_wiki_from_sessions(db: &WikiDb, opts: &ReportOptions) -> Result<()> {
    let existing = db
        .get_existing_session_ids()
        .map_err(|e| anyhow::anyhow!("{}", e))?;

    let parsed = parse_local_clients(LocalParseOptions {
        home_dir: opts.home_dir.clone(),
        use_env_roots: opts.home_dir.is_none(),
        clients: None,
        since: None,
        until: None,
        year: None,
        scanner_settings: opts.scanner_settings.clone(),
    })
    .map_err(|e| anyhow::anyhow!("{}", e))?;

    let pricing = load_pricing_service();

    let mut session_map: HashMap<String, SessionAgg> = HashMap::new();

    for msg in &parsed.messages {
        let agg = session_map
            .entry(msg.session_id.clone())
            .or_insert_with(|| SessionAgg {
                client: msg.client.clone(),
                workspace: msg.workspace_key.clone(),
                workspace_label: msg.workspace_label.clone(),
                created_at: msg.timestamp,
                last_active: msg.timestamp,
                total_input: 0,
                total_output: 0,
                total_cache_read: 0,
                total_cost: 0.0,
                models: HashMap::new(),
                message_count: 0,
            });

        agg.last_active = agg.last_active.max(msg.timestamp);
        agg.created_at = agg.created_at.min(msg.timestamp);
        // saturating: per-message token fields from a corrupt source can be
        // clamped to i64::MAX (see tokmesh-core), so plain `+=` can overflow.
        agg.total_input = agg.total_input.saturating_add(msg.input);
        agg.total_output = agg.total_output.saturating_add(msg.output);
        agg.total_cache_read = agg.total_cache_read.saturating_add(msg.cache_read);
        agg.total_cost += compute_msg_cost(msg, pricing.as_deref());
        // NOTE: the wiki `report` view intentionally groups on the raw model_id
        // and does not apply `modelAliases` folding (nor the grouping
        // normalization every other report uses). Wiki entries are persisted
        // append-only — previously-recorded sessions are not rewritten (see the
        // `existing.contains` skip below) — so folding here would leave a mix of
        // raw and canonical names across sessions recorded before vs after
        // aliases were configured. To fold in a future change, wrap the key with
        // `tokmesh_core::normalize_model_for_grouping(&msg.model_id)` here and in
        // the by-model/daily/session/JSON surfaces.
        *agg.models.entry(msg.model_id.clone()).or_insert(0) += 1;
        agg.message_count += msg.message_count;
    }

    let mut new_count = 0;
    for (session_id, agg) in &session_map {
        if existing.contains(session_id) {
            continue;
        }

        let models_used: Vec<String> = agg.models.keys().cloned().collect();
        let duration_minutes = (agg.last_active - agg.created_at) / 60;

        let entry = WikiEntry {
            session_id: session_id.clone(),
            client: agg.client.clone(),
            workspace: agg.workspace.clone(),
            workspace_label: agg.workspace_label.clone(),
            created_at: agg.created_at,
            last_active: agg.last_active,
            title: None,
            task_category: None,
            description: None,
            complexity: None,
            task_group: None,
            total_input_tokens: agg.total_input,
            total_output_tokens: agg.total_output,
            total_cache_read: agg.total_cache_read,
            total_cost: agg.total_cost,
            models_used,
            message_count: agg.message_count,
            duration_minutes,
            summarized_at: None,
            fm_version: None,
        };

        db.upsert_entry(&entry)
            .map_err(|e| anyhow::anyhow!("{}", e))?;
        new_count += 1;
    }

    if new_count > 0 {
        eprintln!(
            "  {} new sessions added to wiki",
            new_count.to_string().cyan()
        );
    }

    Ok(())
}

fn run_summarizer(
    db: &WikiDb,
    session_ids: &[String],
    backend: &str,
    session_paths: &SessionPathIndex,
) -> Result<()> {
    let mut payloads: Vec<serde_json::Value> = Vec::new();
    for sid in session_ids {
        if let Ok(Some(entry)) = db.get_entry(sid) {
            let content = extract_content_for_session(&entry, session_paths);
            payloads.push(serde_json::json!({
                "session_id": entry.session_id,
                "client": entry.client,
                "workspace": entry.workspace.unwrap_or_default(),
                "first_user_message": content.first_user_message,
                "models_used": entry.models_used,
                "total_tokens": entry.total_input_tokens.saturating_add(entry.total_output_tokens),
                "duration_minutes": entry.duration_minutes,
                "message_count": entry.message_count,
            }));
        }
    }

    if payloads.is_empty() {
        return Ok(());
    }

    eprintln!(
        "  Summarizing {} sessions with {}...",
        payloads.len().to_string().cyan(),
        backend.cyan()
    );

    // apple-fm runs each session as a self-contained on-device generation, so a
    // single giant chunk would suppress the per-batch progress indicator below
    // (it's gated on `batch_size < payloads.len()`). Use a modest batch size so
    // the "\r  Batch i/total" line fires and 100+ sequential on-device calls
    // show visible progress instead of hanging silent until the very end.
    // Re-fetching the model + rebuilding the schema per small batch is cheap;
    // the generation dominates.
    let batch_size = match backend {
        "apple-fm" => 8,
        _ => 20,
    };

    let mut total_summarized = 0;
    // Count how many summaries actually came from Apple FM vs the heuristic
    // fallback, so a silent total-fallback (e.g. FM unavailable, or every
    // generation erroring) is visible rather than reported as plain "N
    // summarized". Only meaningful for the apple-fm backend; CLI backends leave
    // fm_version null by design.
    let mut fm_generated = 0;
    for (batch_idx, chunk) in payloads.chunks(batch_size).enumerate() {
        if batch_size < payloads.len() {
            eprint!(
                "\r  Batch {}/{} ({} done)...",
                batch_idx + 1,
                payloads.len().div_ceil(batch_size),
                total_summarized
            );
        }

        let results = match backend {
            "apple-fm" => run_apple_fm_summarizer(chunk)?,
            "claude" | "codex" | "gemini" | "kiro" => run_cli_summarizer(backend, chunk)?,
            other => {
                return Err(anyhow::anyhow!(
                    "Unknown summarizer backend: '{}'. Valid options: apple-fm, claude, codex, gemini, kiro",
                    other
                ));
            }
        };

        for result in &results {
            let session_id = result["session_id"].as_str().unwrap_or_default();
            let title = result["title"].as_str().unwrap_or("Untitled");
            let category = result["task_category"].as_str().unwrap_or("other");
            let description = result["description"].as_str().unwrap_or("");
            let complexity = result["complexity"].as_str().unwrap_or("moderate");
            let fm_version = result["fm_version"].as_str();
            if fm_version == Some("apple-fm-on-device") {
                fm_generated += 1;
            }

            db.update_summary(
                session_id,
                title,
                category,
                description,
                complexity,
                fm_version,
            )
            .map_err(|e| anyhow::anyhow!("Failed to save summary for {}: {}", session_id, e))?;
        }

        total_summarized += results.len();
    }

    if backend == "apple-fm" {
        let heuristic = total_summarized.saturating_sub(fm_generated);
        eprintln!(
            "\n  {} {} sessions summarized ({} via Apple FM, {} heuristic)",
            "".green(),
            total_summarized,
            fm_generated,
            heuristic
        );
    } else {
        eprintln!(
            "\n  {} {} sessions summarized",
            "".green(),
            total_summarized
        );
    }

    Ok(())
}

const GROUPING_SYSTEM_PROMPT: &str = r#"You are a task grouping assistant. Given a list of coding session titles, group them into high-level project tasks (2-5 words each).

Rules:
- Group related sessions under a single short label (e.g. "Kiro Auth", "Tokmesh Report", "System Config")
- Each group should represent a coherent project or feature area
- Sessions that don't fit any group get their own group name
- Aim for 3-8 groups total. Fewer is better.

Respond ONLY with a JSON array where each element has: session_id, task_group"#;

fn run_task_grouping(db: &WikiDb, entries: &[WikiEntry], backend: &str) -> Result<()> {
    let summarized: Vec<&WikiEntry> = entries
        .iter()
        .filter(|e| e.title.is_some() && e.task_group.is_none())
        .collect();

    if summarized.is_empty() {
        return Ok(());
    }

    // Non-CLI backends (apple-fm and any future on-device backend) have no LLM
    // grouping path. Rather than skip — which leaves every task_group null and
    // makes the report collapse sessions by EXACT title — cluster titles
    // deterministically in Rust. This merges near-duplicate titles ("Enhance API
    // Security" / "Enhance API security with JWT auth middleware") into a single
    // labeled group while keeping unrelated titles apart.
    if !matches!(backend, "claude" | "codex" | "gemini" | "kiro") {
        let assignments = cluster_titles(&summarized);
        let group_count = assignments
            .iter()
            .map(|(_, label)| label.as_str())
            .collect::<std::collections::HashSet<_>>()
            .len();
        for (session_id, label) in &assignments {
            db.update_task_group(session_id, label).map_err(|e| {
                anyhow::anyhow!("Failed to save task_group for {}: {}", session_id, e)
            })?;
        }
        eprintln!(
            "  {} grouped {} sessions into {} tasks",
            "".green(),
            summarized.len(),
            group_count
        );
        return Ok(());
    }

    eprint!(
        "  Grouping {} sessions into tasks...",
        summarized.len().to_string().cyan()
    );

    let mut parts = Vec::new();
    parts.push("Group these coding sessions by project/feature:\n".to_string());
    for (i, entry) in summarized.iter().enumerate() {
        parts.push(format!(
            "  {} (id: {}): {} [{}]",
            i + 1,
            entry.session_id,
            entry.title.as_deref().unwrap_or("?"),
            entry.workspace.as_deref().unwrap_or("?"),
        ));
    }
    parts.push("\nRespond with a JSON array.".to_string());
    let prompt = parts.join("\n");

    let cmd = match backend {
        "claude" => {
            let mut c = Command::new("claude");
            c.args(["-p", "--output-format", "text"])
                .arg(format!("System: {}\n\n{}", GROUPING_SYSTEM_PROMPT, prompt));
            c
        }
        "codex" => {
            let mut c = Command::new("codex");
            c.args(["exec"])
                .arg(format!("{}\n\n{}", GROUPING_SYSTEM_PROMPT, prompt));
            c
        }
        "gemini" => {
            let mut c = Command::new("gemini");
            c.args(["-p"])
                .arg(format!("{}\n\n{}", GROUPING_SYSTEM_PROMPT, prompt));
            c
        }
        "kiro" => {
            let mut c = Command::new("kiro-cli");
            c.args(["chat", "--no-interactive"])
                .arg(format!("{}\n\n{}", GROUPING_SYSTEM_PROMPT, prompt));
            c
        }
        // Non-CLI backends were already handled by the title-clustering path
        // above (which early-returns), so only the four CLI backends reach here.
        other => unreachable!("non-CLI backend '{}' must be handled by clustering", other),
    };

    // A timed-out (or otherwise un-spawnable) backend must degrade gracefully:
    // skip grouping and continue the report rather than aborting it.
    let output = match run_command_with_timeout(cmd, BACKEND_TIMEOUT, None) {
        Ok(output) => output,
        Err(e) => {
            eprintln!("\n  {} grouping failed: {}", "".yellow(), e);
            return Ok(());
        }
    };

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        eprintln!("\n  {} grouping failed: {}", "".yellow(), stderr.trim());
        return Ok(());
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let json_str = extract_json_array(&stdout);

    match serde_json::from_str::<Vec<serde_json::Value>>(json_str) {
        Ok(results) => {
            for result in &results {
                let session_id = result["session_id"].as_str().unwrap_or_default();
                let task_group = result["task_group"].as_str().unwrap_or_default();
                if !session_id.is_empty() && !task_group.is_empty() {
                    db.update_task_group(session_id, task_group).map_err(|e| {
                        anyhow::anyhow!("Failed to save task_group for {}: {}", session_id, e)
                    })?;
                }
            }
            eprintln!(" {}", "".green());
        }
        Err(e) => {
            eprintln!(
                "\n  {} Failed to parse grouping response: {}",
                "".yellow(),
                e
            );
        }
    }

    Ok(())
}

/// Generic verbs and stopwords stripped from titles before clustering. These
/// carry no signal about *which* project/feature a session touched (every other
/// session "adds" or "fixes" something), so keeping them would make unrelated
/// titles look similar.
const CLUSTER_STOPWORDS: &[&str] = &[
    "add",
    "fix",
    "fixes",
    "fixed",
    "update",
    "updates",
    "refactor",
    "improve",
    "implement",
    "enhance",
    "create",
    "remove",
    "the",
    "a",
    "an",
    "to",
    "for",
    "with",
    "and",
    "of",
    "in",
    "on",
    "via",
];

/// Reduce a title to its set of SIGNIFICANT tokens: lowercase, strip
/// punctuation/ellipsis, collapse whitespace, drop generic verbs/stopwords.
/// The returned tokens are deduplicated and sorted so two titles with the same
/// significant words (in any order) produce equal sets.
fn is_combining_mark(c: char) -> bool {
    matches!(
        c as u32,
        0x0300..=0x036f | 0x1ab0..=0x1aff | 0x1dc0..=0x1dff | 0x20d0..=0x20ff | 0xfe20..=0xfe2f
    )
}

fn significant_tokens(title: &str) -> Vec<String> {
    let mut tokens: Vec<String> = title
        // Normalize to NFC FIRST so canonically-equivalent inputs (precomposed
        // "é" vs base "e" + combining acute) collapse to the same code points
        // before lowercasing and combining-mark stripping. Without this, an NFC
        // title kept the precomposed letter while an NFD one had its combining
        // mark stripped, tokenizing identical titles differently and splitting
        // them across clusters.
        .nfc()
        .collect::<String>()
        .to_lowercase()
        .chars()
        // Lowercase before filtering: Unicode lowercase can expand a character
        // into a base letter plus combining mark (e.g. "İ" -> "i" + dot).
        // Dropping combining marks here keeps case-only variants token-equal.
        // Other punctuation/ellipsis maps to spaces, stripping trailing "…" or
        // "..." that the on-device model often emits.
        .filter_map(|c| {
            if c.is_alphanumeric() {
                Some(c)
            } else if is_combining_mark(c) {
                None
            } else {
                Some(' ')
            }
        })
        .collect::<String>()
        .split_whitespace()
        .map(|t| t.to_string())
        .filter(|t| !CLUSTER_STOPWORDS.contains(&t.as_str()))
        .collect();
    tokens.sort();
    tokens.dedup();
    tokens
}

/// Normalize a title for exact-equality grouping of token-empty titles:
/// full Unicode lowercase with whitespace collapsed. Used only to keep
/// identical all-stopword titles together while keeping distinct ones apart.
fn normalized_title(title: &str) -> String {
    title
        // Match significant_tokens: normalize to NFC before lowercasing so
        // canonically-equivalent all-stopword titles share an exact key.
        .nfc()
        .collect::<String>()
        .to_lowercase()
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}

/// Similarity threshold for treating two titles as the same task. Applied to
/// the overlap coefficient (shared / size-of-smaller-set), which — unlike a raw
/// shared-token count — scales with how much of the smaller title is covered.
const CLUSTER_SIMILARITY_THRESHOLD: f64 = 0.6;

/// Two token sets are considered the same task when they overlap strongly,
/// measured by the OVERLAP COEFFICIENT: `shared / min(|a|, |b|)`.
///
/// This deliberately replaces the old "share ≥ 2 tokens" rule, which fired on
/// any two long-but-unrelated titles that happened to share two common words
/// (e.g. "add" + "service") and — combined with union-growing cluster
/// signatures — let a single cluster transitively swallow everything.
///
/// The overlap coefficient stays high (1.0) when a short title is fully
/// contained in a longer one ("Enhance API Security" ⊂ "Enhance API security
/// with JWT auth middleware"), so genuine near-duplicates still merge, while two
/// large sets that share only a couple of incidental tokens score low and stay
/// apart.
fn tokens_overlap(a: &[String], b: &[String]) -> bool {
    if a.is_empty() || b.is_empty() {
        return false;
    }
    let shared = a.iter().filter(|t| b.contains(t)).count();
    let smaller = a.len().min(b.len());
    // A single-token set scores a perfect 1.0 against any longer title that
    // merely contains that token, so a generic summary like "API" / "Fix API"
    // would cluster with every unrelated "Add API auth", "Update API billing",
    // etc. Require at least TWO shared tokens whenever the smaller set has only
    // one token, so singletons need real signal before merging.
    if smaller <= 1 {
        return shared >= 2;
    }
    (shared as f64 / smaller as f64) >= CLUSTER_SIMILARITY_THRESHOLD
}

/// Deterministically cluster summarized entries by title similarity and return
/// `(session_id, group_label)` for every entry. Greedy O(n²) clustering — n is
/// small (one report's worth of sessions). Each cluster is labeled with its most
/// frequent original title, tie-broken by shortest, so the label is a real
/// human-readable title rather than a synthetic key.
fn cluster_titles(entries: &[&WikiEntry]) -> Vec<(String, String)> {
    struct Cluster {
        // Token sets of EVERY member, kept separately rather than unioned into a
        // single signature. A candidate joins if it overlaps ANY member, which
        // preserves transitive grouping of genuine near-duplicates WITHOUT the
        // union ballooning into a catch-all that absorbs unrelated titles.
        member_tokens: Vec<Vec<String>>,
        // Exact normalized title shared by a token-empty cluster (all-stopword
        // titles). `None` for tokened clusters.
        empty_key: Option<String>,
        members: Vec<usize>,
    }

    // Precompute significant tokens + normalized title once per entry.
    let prepared: Vec<(usize, Vec<String>, String)> = entries
        .iter()
        .enumerate()
        .map(|(i, e)| {
            let raw = e.title.as_deref().unwrap_or("");
            (i, significant_tokens(raw), normalized_title(raw))
        })
        .collect();

    let mut clusters: Vec<Cluster> = Vec::new();
    for (idx, tokens, norm) in &prepared {
        // Find the first existing cluster this entry overlaps strongly with.
        let mut placed = false;
        for cluster in clusters.iter_mut() {
            let matches = if tokens.is_empty() {
                // Token-empty titles (all stopwords) carry no signal to cluster
                // on, so they only merge with an identical normalized title.
                // This stops every generic/stopword-only title from collapsing
                // into one arbitrary blob.
                cluster.empty_key.as_deref() == Some(norm.as_str())
            } else {
                // Tokened entries never join an empty cluster; they match if they
                // overlap ANY existing member of the cluster.
                cluster.empty_key.is_none()
                    && cluster
                        .member_tokens
                        .iter()
                        .any(|m| tokens_overlap(tokens, m))
            };
            if matches {
                cluster.members.push(*idx);
                cluster.member_tokens.push(tokens.clone());
                placed = true;
                break;
            }
        }
        if !placed {
            clusters.push(Cluster {
                member_tokens: vec![tokens.clone()],
                empty_key: if tokens.is_empty() {
                    Some(norm.clone())
                } else {
                    None
                },
                members: vec![*idx],
            });
        }
    }

    // Consolidation pass: the single greedy pass above is order-dependent — an
    // entry compared before its eventual neighbor was seen can land in its own
    // cluster even though it overlaps a member of another cluster. Repeatedly
    // merge any two clusters that have a pair of overlapping members (or, for
    // token-empty clusters, an identical normalized title) until a fixpoint,
    // making the final grouping independent of input order. n is small, so the
    // O(n²)-per-round loop is cheap. Crucially, merging only happens on a real
    // member-to-member overlap, so it cannot chain unrelated clusters together.
    loop {
        let mut merged_any = false;
        'outer: for i in 0..clusters.len() {
            for j in (i + 1)..clusters.len() {
                let overlap = match (&clusters[i].empty_key, &clusters[j].empty_key) {
                    // Token-empty clusters merge only with an identical title.
                    (Some(ki), Some(kj)) => ki == kj,
                    // A token-empty cluster never merges with a tokened one.
                    (Some(_), None) | (None, Some(_)) => false,
                    // Tokened clusters merge if any member pair overlaps.
                    (None, None) => clusters[i].member_tokens.iter().any(|mi| {
                        clusters[j]
                            .member_tokens
                            .iter()
                            .any(|mj| tokens_overlap(mi, mj))
                    }),
                };
                if overlap {
                    let other = clusters.remove(j);
                    clusters[i].members.extend(other.members);
                    clusters[i].member_tokens.extend(other.member_tokens);
                    merged_any = true;
                    break 'outer;
                }
            }
        }
        if !merged_any {
            break;
        }
    }

    let mut assignments = Vec::new();
    for cluster in &clusters {
        let label = cluster_label(entries, &cluster.members);
        for &idx in &cluster.members {
            assignments.push((entries[idx].session_id.clone(), label.clone()));
        }
    }
    assignments
}

/// Pick a human-readable label for a cluster: the most frequent original title,
/// tie-broken by shortest (char count), then lexicographically for full
/// determinism.
fn cluster_label(entries: &[&WikiEntry], members: &[usize]) -> String {
    let mut counts: HashMap<&str, usize> = HashMap::new();
    for &idx in members {
        let title = entries[idx]
            .title
            .as_deref()
            .map(str::trim)
            .filter(|t| !t.is_empty())
            .unwrap_or("(unsummarized)");
        *counts.entry(title).or_insert(0) += 1;
    }
    counts
        .into_iter()
        .max_by(|(at, ac), (bt, bc)| {
            ac.cmp(bc)
                // Higher frequency wins; on a tie prefer the SHORTER title, then
                // lexicographically smaller, so the label is stable run-to-run.
                .then_with(|| bt.chars().count().cmp(&at.chars().count()))
                .then_with(|| bt.cmp(at))
        })
        .map(|(title, _)| title.to_string())
        .unwrap_or_else(|| "(unsummarized)".to_string())
}

fn run_apple_fm_summarizer(payloads: &[serde_json::Value]) -> Result<Vec<serde_json::Value>> {
    // Build typed inputs from the JSON payloads.
    let inputs: Vec<apple_fm::SessionInput> = payloads
        .iter()
        .map(|p| apple_fm::SessionInput {
            session_id: p["session_id"].as_str().unwrap_or_default().to_string(),
            client: p["client"].as_str().unwrap_or_default().to_string(),
            workspace: p["workspace"].as_str().unwrap_or_default().to_string(),
            first_user_message: p["first_user_message"]
                .as_str()
                .filter(|s| !s.is_empty())
                .map(|s| s.to_string()),
            models_used: p["models_used"]
                .as_array()
                .map(|arr| {
                    arr.iter()
                        .filter_map(|m| m.as_str().map(|s| s.to_string()))
                        .collect()
                })
                .unwrap_or_default(),
            total_tokens: p["total_tokens"].as_i64().unwrap_or(0),
            duration_minutes: p["duration_minutes"].as_i64().unwrap_or(0),
            message_count: p["message_count"].as_i64().unwrap_or(0),
        })
        .collect();

    // `summarize` returns `Some` only when Apple Intelligence is available and
    // the feature is enabled on macOS. In every other case (unavailable,
    // feature-off, or non-macOS) it returns `None` and we apply the Rust
    // heuristic to all sessions. apple-fm therefore stays the default backend
    // and degrades gracefully cross-platform — it never errors out the report.
    let summaries: Vec<apple_fm::SessionSummary> = match apple_fm::summarize(&inputs) {
        Some(v) => v,
        None => inputs.iter().map(apple_fm::heuristic_classify).collect(),
    };

    // Provenance is carried PER summary (`s.fm_version`): even when Apple FM is
    // available, an individual generation that fails/times out is backfilled with
    // the heuristic and must not be recorded as `apple-fm-on-device`.
    let results = summaries
        .into_iter()
        .map(|s| {
            serde_json::json!({
                "session_id": s.session_id,
                "title": s.title,
                "task_category": s.task_category,
                "description": s.description,
                "complexity": s.complexity,
                "fm_version": s.fm_version,
            })
        })
        .collect();

    Ok(results)
}

const SUMMARIZER_SYSTEM_PROMPT: &str = r#"You are a coding session classifier. Given metadata about an AI coding session, produce a structured summary.

Rules:
- title: 3-8 word description of what was done (imperative mood, e.g. "Add JWT auth middleware")
- task_category: exactly one of: feature, bugfix, refactor, research, debug, review, docs, config, other
- description: 1-2 sentences explaining what happened in the session
- complexity: exactly one of: trivial, moderate, complex

Respond ONLY with a JSON array where each element has: session_id, title, task_category, description, complexity."#;

fn build_cli_prompt(payloads: &[serde_json::Value]) -> String {
    let mut parts = Vec::new();
    parts.push("Classify these coding sessions:\n".to_string());
    for (i, p) in payloads.iter().enumerate() {
        parts.push(format!(
            "Session {} (id: {}):\n  Workspace: {}\n  Client: {}\n  Models: {}\n  Tokens: {}\n  Duration: {} min\n  Messages: {}\n  First message: {}\n",
            i + 1,
            p["session_id"].as_str().unwrap_or("?"),
            p["workspace"].as_str().unwrap_or("?"),
            p["client"].as_str().unwrap_or("?"),
            p["models_used"],
            p["total_tokens"],
            p["duration_minutes"],
            p["message_count"],
            p["first_user_message"].as_str().unwrap_or("(none)").chars().take(200).collect::<String>(),
        ));
    }
    parts.push("Respond with a JSON array.".to_string());
    parts.join("\n")
}

fn run_cli_summarizer(
    backend: &str,
    payloads: &[serde_json::Value],
) -> Result<Vec<serde_json::Value>> {
    let prompt = build_cli_prompt(payloads);

    let cmd = match backend {
        "claude" => {
            let mut c = Command::new("claude");
            c.args(["-p", "--output-format", "text"]).arg(format!(
                "System: {}\n\n{}",
                SUMMARIZER_SYSTEM_PROMPT, prompt
            ));
            c
        }
        "codex" => {
            let mut c = Command::new("codex");
            c.args(["exec"])
                .arg(format!("{}\n\n{}", SUMMARIZER_SYSTEM_PROMPT, prompt));
            c
        }
        "gemini" => {
            let mut c = Command::new("gemini");
            c.args(["-p"])
                .arg(format!("{}\n\n{}", SUMMARIZER_SYSTEM_PROMPT, prompt));
            c
        }
        "kiro" => {
            let mut c = Command::new("kiro-cli");
            c.args(["chat", "--no-interactive"])
                .arg(format!("{}\n\n{}", SUMMARIZER_SYSTEM_PROMPT, prompt));
            c
        }
        _ => return Ok(Vec::new()),
    };

    // A timed-out (or un-spawnable) backend must degrade gracefully: log it and
    // return no summaries so the caller continues, matching the non-zero-exit
    // path below.
    let output = match run_command_with_timeout(cmd, BACKEND_TIMEOUT, None) {
        Ok(output) => output,
        Err(e) => {
            eprintln!("  {} {} summarizer failed: {}", "".yellow(), backend, e);
            return Ok(Vec::new());
        }
    };

    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr);
        eprintln!(
            "  {} {} summarizer failed: {}",
            "".yellow(),
            backend,
            stderr.trim()
        );
        return Ok(Vec::new());
    }

    let stdout = String::from_utf8_lossy(&output.stdout);
    let json_str = extract_json_array(&stdout);

    match serde_json::from_str::<Vec<serde_json::Value>>(json_str) {
        Ok(results) => Ok(results),
        Err(e) => {
            eprintln!(
                "  {} Failed to parse {} response: {}",
                "".yellow(),
                backend,
                e
            );
            Ok(Vec::new())
        }
    }
}

/// Upper bound on how long any LLM summarizer subprocess may run before we
/// kill it. Deliberately generous (5 min) so legitimate batched LLM calls
/// never trip it, but it bounds a true hang (auth prompt, network stall) so
/// `tokmesh report` can never block forever.
const BACKEND_TIMEOUT: Duration = Duration::from_secs(300);

/// Spawn `cmd`, optionally write `stdin_bytes` to its stdin, and wait up to
/// `timeout` for it to finish.
///
/// Mirrors the pure-std spawn + reader-thread + `try_wait()` deadline + kill
/// approach used by `run_capture_command` in `main.rs` (no extra dependency).
/// Both stdout and stderr are drained on dedicated threads so a chatty backend
/// cannot deadlock on a full pipe buffer while we poll for exit.
///
/// On timeout the child is killed and an `io::Error` of kind `TimedOut` is
/// returned, which callers treat as a recoverable "skip this backend" signal.
fn run_command_with_timeout(
    mut cmd: Command,
    timeout: Duration,
    stdin_bytes: Option<&[u8]>,
) -> std::io::Result<Output> {
    use std::io::Read;
    use std::thread;
    use std::time::Instant;

    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());
    if stdin_bytes.is_some() {
        cmd.stdin(Stdio::piped());
    }

    let mut child = cmd.spawn()?;

    // Write to stdin (if requested) before draining output, then drop the
    // handle so the child sees EOF.
    if let Some(bytes) = stdin_bytes {
        if let Some(mut stdin) = child.stdin.take() {
            stdin.write_all(bytes)?;
        }
    }

    let mut stdout = child
        .stdout
        .take()
        .ok_or_else(|| std::io::Error::other("failed to capture subprocess stdout"))?;
    let mut stderr = child
        .stderr
        .take()
        .ok_or_else(|| std::io::Error::other("failed to capture subprocess stderr"))?;

    let stdout_handle = thread::spawn(move || -> std::io::Result<Vec<u8>> {
        let mut buf = Vec::new();
        stdout.read_to_end(&mut buf)?;
        Ok(buf)
    });
    let stderr_handle = thread::spawn(move || -> std::io::Result<Vec<u8>> {
        let mut buf = Vec::new();
        stderr.read_to_end(&mut buf)?;
        Ok(buf)
    });

    let deadline = Instant::now() + timeout;
    let status = loop {
        if let Some(status) = child.try_wait()? {
            break status;
        }
        if Instant::now() >= deadline {
            let _ = child.kill();
            let _ = child.wait();
            return Err(std::io::Error::new(
                std::io::ErrorKind::TimedOut,
                "summarizer backend timed out",
            ));
        }
        thread::sleep(Duration::from_millis(25));
    };

    let stdout = stdout_handle
        .join()
        .map_err(|_| std::io::Error::other("subprocess stdout reader thread panicked"))??;
    let stderr = stderr_handle
        .join()
        .map_err(|_| std::io::Error::other("subprocess stderr reader thread panicked"))??;

    Ok(Output {
        status,
        stdout,
        stderr,
    })
}

fn extract_json_array(text: &str) -> &str {
    if let Some(start) = text.find('[') {
        if let Some(end) = text.rfind(']') {
            return &text[start..=end];
        }
    }
    text
}

fn print_report_table(
    entries: &[WikiEntry],
    _db: &WikiDb,
    is_multi_day: bool,
    full: bool,
) -> Result<()> {
    if entries.is_empty() {
        println!("No sessions found for the given filters.");
        return Ok(());
    }

    let total_cost: f64 = entries.iter().map(|e| e.total_cost).sum();
    let total_tokens: i64 = entries
        .iter()
        .map(|e| e.total_input_tokens.saturating_add(e.total_output_tokens))
        .fold(0i64, i64::saturating_add);
    let total_sessions = entries.len();
    let summarized = entries.iter().filter(|e| e.title.is_some()).count();

    println!();
    println!(
        "  {} sessions | {} summarized | ${:.2} total | {} tokens",
        total_sessions.to_string().cyan(),
        summarized.to_string().green(),
        total_cost,
        format_tokens(total_tokens).yellow(),
    );
    println!();

    let mut by_model: HashMap<&str, (f64, i64, usize)> = HashMap::new();
    for entry in entries {
        if entry.models_used.is_empty() {
            continue;
        }
        for model in &entry.models_used {
            let agg = by_model.entry(model.as_str()).or_insert((0.0, 0, 0));
            agg.0 += entry.total_cost / entry.models_used.len() as f64;
            agg.1 = agg.1.saturating_add(
                entry
                    .total_input_tokens
                    .saturating_add(entry.total_output_tokens)
                    / entry.models_used.len() as i64,
            );
            agg.2 += 1;
        }
    }

    let mut models: Vec<_> = by_model.iter().collect();
    models.sort_by(|a, b| b.1 .0.total_cmp(&a.1 .0));

    println!(
        "  {:<30} {:>8} {:>12} {:>8}",
        "Model", "Sessions", "Tokens", "Cost"
    );
    println!("  {}", "".repeat(62));
    for (model, (cost, tokens, count)) in &models {
        println!(
            "  {:<30} {:>8} {:>12} {:>8}",
            model,
            count,
            format_tokens(*tokens),
            format!("${:.2}", cost),
        );
    }
    println!("  {}", "".repeat(62));
    println!(
        "  {:<30} {:>8} {:>12} {:>8}",
        "TOTAL",
        total_sessions,
        format_tokens(total_tokens),
        format!("${:.2}", total_cost),
    );
    println!();

    let mut by_group: HashMap<&str, (f64, i64, usize, Vec<&str>)> = HashMap::new();
    for entry in entries {
        let group = entry
            .task_group
            .as_deref()
            .unwrap_or(entry.title.as_deref().unwrap_or("(unsummarized)"));
        let title = entry.title.as_deref().unwrap_or("(unsummarized)");
        let agg = by_group.entry(group).or_insert((0.0, 0, 0, Vec::new()));
        agg.0 += entry.total_cost;
        agg.1 = agg.1.saturating_add(
            entry
                .total_input_tokens
                .saturating_add(entry.total_output_tokens),
        );
        agg.2 += 1;
        if !agg.3.contains(&title) {
            agg.3.push(title);
        }
    }

    let mut groups: Vec<_> = by_group.iter().collect();
    groups.sort_by(|a, b| b.1 .0.total_cmp(&a.1 .0));

    println!(
        "  {:<40} {:>5} {:>10} {:>8}",
        "Task Group", "Sess", "Tokens", "Cost"
    );
    println!("  {}", "".repeat(67));
    for (group, (cost, tokens, count, titles)) in groups.iter().take(15) {
        let display_group: String = if group.chars().count() > 40 {
            format!("{}", group.chars().take(39).collect::<String>())
        } else {
            group.to_string()
        };
        println!(
            "  {:<40} {:>5} {:>10} {:>8}",
            display_group.bold(),
            count,
            format_tokens(*tokens),
            format!("${:.2}", cost),
        );
        if *count > 1 {
            for t in titles.iter().take(3) {
                let display_t: String = if t.chars().count() > 38 {
                    t.chars().take(38).collect::<String>()
                } else {
                    t.to_string()
                };
                println!("    {}", display_t.dimmed());
            }
            if titles.len() > 3 {
                println!("    … +{} more", titles.len() - 3);
            }
        }
    }
    if groups.len() > 15 {
        let rest_count: usize = groups.iter().skip(15).map(|(_, v)| v.2).sum();
        let rest_cost: f64 = groups.iter().skip(15).map(|(_, v)| v.0).sum();
        let rest_tokens: i64 = groups.iter().skip(15).map(|(_, v)| v.1).sum();
        println!(
            "  {:<40} {:>5} {:>10} {:>8}",
            format!("… +{} more", groups.len() - 15),
            rest_count,
            format_tokens(rest_tokens),
            format!("${:.2}", rest_cost),
        );
    }
    println!("  {}", "".repeat(67));
    println!();

    if is_multi_day {
        print_daily_breakdown(entries, full);
    } else {
        print_session_list(entries, full);
    }

    Ok(())
}

fn print_daily_breakdown(entries: &[WikiEntry], full: bool) {
    use std::collections::BTreeMap;

    let mut by_date: BTreeMap<String, (f64, i64, usize, Vec<&WikiEntry>)> = BTreeMap::new();
    for entry in entries {
        let date_key = tokmesh_core::bucket_timezone().date_of_ms(entry.created_at);
        let date_key = if date_key.is_empty() {
            "unknown".to_string()
        } else {
            date_key
        };

        let agg = by_date.entry(date_key).or_insert((0.0, 0, 0, Vec::new()));
        agg.0 += entry.total_cost;
        agg.1 = agg.1.saturating_add(
            entry
                .total_input_tokens
                .saturating_add(entry.total_output_tokens),
        );
        agg.2 += 1;
        agg.3.push(entry);
    }

    let mut dates: Vec<_> = by_date.iter().collect();
    dates.sort_by(|a, b| b.0.cmp(a.0));

    println!("  Daily breakdown:");
    println!("  {}", "".repeat(72));
    for (date, (cost, tokens, count, sessions)) in &dates {
        println!(
            "  {} {:>3} sessions  {:>10} tokens  {:>8}",
            date.cyan(),
            count,
            format_tokens(*tokens),
            format!("${:.2}", cost),
        );
        let daily_limit = if full { sessions.len() } else { 5 };
        for s in sessions.iter().take(daily_limit) {
            let title = s.title.as_deref().unwrap_or("(pending)");
            let model = s.models_used.first().map(|m| m.as_str()).unwrap_or("-");
            let display_title: String = if title.chars().count() > 40 {
                title.chars().take(40).collect::<String>()
            } else {
                title.to_string()
            };
            println!(
                "    {:>6} {:<18} {}",
                format!("${:.2}", s.total_cost),
                model.dimmed(),
                display_title,
            );
        }
        if sessions.len() > 5 {
            println!("    … +{} more sessions", sessions.len() - 5);
        }
    }
    println!();
}

fn print_session_list(entries: &[WikiEntry], full: bool) {
    let list_limit = if full { entries.len() } else { 10 };
    let recent: Vec<&WikiEntry> = entries.iter().take(list_limit).collect();
    if !recent.is_empty() {
        println!("  Sessions:");
        println!("  {}", "".repeat(80));
        for entry in recent {
            let date = tokmesh_core::bucket_timezone()
                .naive_datetime_of_ms(entry.created_at)
                .map(|dt| dt.format("%H:%M").to_string())
                .unwrap_or_else(|| "??:??".to_string());

            let title = entry.title.as_deref().unwrap_or("(pending summarization)");
            let model = entry.models_used.first().map(|s| s.as_str()).unwrap_or("-");
            let cost = format!("${:.2}", entry.total_cost);

            println!(
                "  {} {:>6} {:<20} {}",
                date.dimmed(),
                cost,
                model.dimmed(),
                title,
            );
        }
        if !full && entries.len() > 10 {
            println!("    … +{} more sessions", entries.len() - 10);
        }
        println!();
    }
}

/// Maps every locally-discovered session to the on-disk file(s) its content can
/// be extracted from, so the summarizer payload carries the real first user
/// message instead of metadata only.
///
/// File-keyed clients (claude, codex, gemini) live as one transcript file per
/// session, indexed here by `(client, session_id)`. The client is part of the
/// key so cross-client `session_id` collisions can't feed one client's file to
/// another client's extractor. OpenCode sessions live as rows inside a shared
/// SQLite database, so every opencode database is kept as a candidate and the
/// extractor selects the matching session internally.
#[derive(Default)]
struct SessionPathIndex {
    by_client_session: HashMap<(String, String), Vec<PathBuf>>,
    opencode_dbs: Vec<PathBuf>,
}

impl SessionPathIndex {
    /// Candidate file(s) to feed the dispatcher for `(client, session_id)`.
    fn candidates_for(&self, client: &str, session_id: &str) -> Vec<PathBuf> {
        if client == "opencode" {
            return self.opencode_dbs.clone();
        }
        self.by_client_session
            .get(&(client.to_string(), session_id.to_string()))
            .cloned()
            .unwrap_or_default()
    }
}

/// Scan local client data once and index every session file by
/// `(client, session_id)` plus the OpenCode databases, so per-session content
/// extraction never has to re-walk the filesystem. Scanning is best-effort: any
/// client the summarizer can't extract simply yields no candidates and falls
/// back to metadata-only.
///
/// The `session_id` used as the key must match how the wiki populates its
/// entries. For most clients that is the file stem, but Gemini transcripts derive
/// the id from the in-file `sessionId`/`session_id` field, so they are keyed by
/// the parsed id (with the stem kept as a fallback alias).
fn build_session_path_index(opts: &ReportOptions) -> SessionPathIndex {
    let home_dir = opts
        .home_dir
        .clone()
        .or_else(|| std::env::var("HOME").ok())
        .unwrap_or_default();
    let use_env_roots = opts.home_dir.is_none();

    let scan = tokmesh_core::scanner::scan_all_clients_with_scanner_settings(
        &home_dir,
        &[],
        use_env_roots,
        &opts.scanner_settings,
    );

    let mut by_client_session: HashMap<(String, String), Vec<PathBuf>> = HashMap::new();
    for (client, path) in scan.all_files() {
        let client_str = client.as_str().to_string();
        let mut session_ids: Vec<String> = Vec::new();

        if client == tokmesh_core::ClientId::Gemini {
            // Gemini's wiki session_id comes from inside the file, not the stem.
            if let Some(id) = tokmesh_core::sessions::gemini::gemini_session_id_for_file(&path) {
                session_ids.push(id);
            }
        }
        // Always keep the file stem as a key/alias so stem-keyed clients work and
        // Gemini lookups still resolve if the wiki used the stem.
        if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) {
            session_ids.push(stem.to_string());
        }

        session_ids.sort();
        session_ids.dedup();
        for session_id in session_ids {
            by_client_session
                .entry((client_str.clone(), session_id))
                .or_default()
                .push(path.clone());
        }
    }

    SessionPathIndex {
        by_client_session,
        opencode_dbs: scan.opencode_dbs.clone(),
    }
}

/// Resolve a session's real content by dispatching to the correct per-client
/// extractor over its on-disk file(s). Falls back to metadata-only when the
/// client is unsupported or no candidate file yields a first user message.
fn extract_content_for_session(
    entry: &WikiEntry,
    session_paths: &SessionPathIndex,
) -> SessionContent {
    let candidates = session_paths.candidates_for(&entry.client, &entry.session_id);
    if candidates.is_empty() {
        return metadata_only_content(&entry.session_id, &entry.client);
    }
    extract_session_content(&entry.client, &entry.session_id, &candidates)
}

fn parse_date_range(since: &Option<String>, until: &Option<String>) -> (Option<i64>, Option<i64>) {
    parse_date_range_with_timezone(since, until, tokmesh_core::bucket_timezone())
}

fn parse_date_range_with_timezone(
    since: &Option<String>,
    until: &Option<String>,
    timezone: tokmesh_core::BucketTimezone,
) -> (Option<i64>, Option<i64>) {
    let since_ts = since
        .as_ref()
        .and_then(|s| chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").ok())
        .and_then(|date| timezone.start_of_day_ms(date));
    let until_ts = until
        .as_ref()
        .and_then(|s| chrono::NaiveDate::parse_from_str(s, "%Y-%m-%d").ok())
        .and_then(|d| d.succ_opt())
        .and_then(|next| timezone.start_of_day_ms(next).map(|ms| ms - 1));
    (since_ts, until_ts)
}

/// Loads the canonical pricing dataset for cost attribution, preferring a fresh
/// fetch but falling back to any cached dataset so reports still work offline.
/// Returns `None` only when no pricing data is available at all.
fn load_pricing_service() -> Option<std::sync::Arc<PricingService>> {
    let fresh = tokio::runtime::Runtime::new()
        .ok()
        .and_then(|rt| rt.block_on(async { PricingService::get_or_init().await.ok() }));
    fresh.or_else(|| PricingService::load_cached_any_age().map(std::sync::Arc::new))
}

/// Computes a message's cost using the canonical [`PricingService`], honoring
/// per-model rates and every billed token type (input/output/cache read/cache
/// write/reasoning). Returns 0.0 when no pricing dataset is available.
fn compute_msg_cost(msg: &ParsedMessage, pricing: Option<&PricingService>) -> f64 {
    let Some(pricing) = pricing else {
        return 0.0;
    };
    pricing.calculate_cost_with_provider(
        &msg.model_id,
        Some(&msg.provider_id),
        &TokenBreakdown {
            input: msg.input,
            output: msg.output,
            cache_read: msg.cache_read,
            cache_write: msg.cache_write,
            reasoning: msg.reasoning,
        },
    )
}

fn format_tokens(tokens: i64) -> String {
    if tokens >= 1_000_000_000 {
        format!("{:.1}B", tokens as f64 / 1_000_000_000.0)
    } else if tokens >= 1_000_000 {
        format!("{:.1}M", tokens as f64 / 1_000_000.0)
    } else if tokens >= 1_000 {
        format!("{:.0}K", tokens as f64 / 1_000.0)
    } else {
        tokens.to_string()
    }
}

struct SessionAgg {
    client: String,
    workspace: Option<String>,
    workspace_label: Option<String>,
    created_at: i64,
    last_active: i64,
    total_input: i64,
    total_output: i64,
    total_cache_read: i64,
    total_cost: f64,
    models: HashMap<String, i32>,
    message_count: i32,
}

#[cfg(test)]
mod tests {
    use super::*;
    use tokmesh_core::pricing::{ModelPricing, PricingService};

    fn test_pricing_service() -> PricingService {
        let mut litellm = HashMap::new();
        litellm.insert(
            "claude-haiku-4".to_string(),
            ModelPricing {
                input_cost_per_token: Some(0.000004),
                output_cost_per_token: Some(0.000006),
                cache_read_input_token_cost: Some(0.000001),
                ..Default::default()
            },
        );
        PricingService::new(litellm, HashMap::new())
    }

    fn parsed_message(model_id: &str) -> ParsedMessage {
        ParsedMessage {
            client: "claude".to_string(),
            model_id: model_id.to_string(),
            provider_id: "anthropic".to_string(),
            session_id: "s1".to_string(),
            workspace_key: None,
            workspace_label: None,
            timestamp: 0,
            date: "2026-01-01".to_string(),
            input: 1_000,
            output: 500,
            cache_read: 2_000,
            cache_write: 0,
            reasoning: 0,
            duration_ms: None,
            message_count: 1,
            agent: None,
        }
    }

    #[test]
    fn compute_msg_cost_matches_canonical_pricing_service() {
        let pricing = test_pricing_service();
        let msg = parsed_message("claude-haiku-4");

        let report_cost = compute_msg_cost(&msg, Some(&pricing));
        let canonical = pricing.calculate_cost_with_provider(
            &msg.model_id,
            Some(&msg.provider_id),
            &TokenBreakdown {
                input: msg.input,
                output: msg.output,
                cache_read: msg.cache_read,
                cache_write: msg.cache_write,
                reasoning: msg.reasoning,
            },
        );

        // The report must price exactly what PricingService yields — no
        // hardcoded flat rates, no fuzzy matching.
        assert_eq!(report_cost, canonical);
        assert!(
            canonical > 0.0,
            "expected a positive cost for a known model"
        );
    }

    #[test]
    fn parse_date_range_buckets_in_pinned_timezone() {
        use chrono::{TimeZone, Utc};

        let day = "2026-03-08";
        let timezone = tokmesh_core::parse_bucket_timezone("Pacific/Kiritimati").unwrap();
        let (since, until) =
            parse_date_range_with_timezone(&Some(day.into()), &Some(day.into()), timezone);
        let expected_since = Utc
            .with_ymd_and_hms(2026, 3, 7, 10, 0, 0)
            .single()
            .unwrap()
            .timestamp_millis();
        let expected_until = Utc
            .with_ymd_and_hms(2026, 3, 8, 10, 0, 0)
            .single()
            .unwrap()
            .timestamp_millis()
            - 1;

        assert_eq!(since, Some(expected_since));
        assert_eq!(until, Some(expected_until));
    }

    #[test]
    fn compute_msg_cost_without_pricing_is_zero() {
        let msg = parsed_message("claude-haiku-4");
        assert_eq!(compute_msg_cost(&msg, None), 0.0);
    }

    fn titled_entry(session_id: &str, title: &str) -> WikiEntry {
        WikiEntry {
            session_id: session_id.to_string(),
            client: "apple-fm".to_string(),
            workspace: None,
            workspace_label: None,
            created_at: 0,
            last_active: 0,
            title: Some(title.to_string()),
            task_category: None,
            description: None,
            complexity: None,
            task_group: None,
            total_input_tokens: 0,
            total_output_tokens: 0,
            total_cache_read: 0,
            total_cost: 0.0,
            models_used: Vec::new(),
            message_count: 0,
            duration_minutes: 0,
            summarized_at: None,
            fm_version: None,
        }
    }

    #[test]
    fn significant_tokens_normalizes_and_strips_stopwords() {
        // Lowercase, punctuation/ellipsis stripped, generic verbs dropped,
        // result sorted + deduped.
        assert_eq!(
            significant_tokens("Add JWT auth middleware…"),
            vec!["auth", "jwt", "middleware"]
        );
        assert_eq!(
            significant_tokens("Enhance API Security"),
            vec!["api", "security"]
        );
        // Trailing "..." and mixed case collapse to the same key.
        assert_eq!(
            significant_tokens("Fix the API Security..."),
            vec!["api", "security"]
        );
    }

    #[test]
    fn cluster_titles_merges_near_duplicates() {
        let entries = [
            titled_entry("a", "Enhance API Security"),
            titled_entry("b", "Enhance API security with JWT auth middleware"),
            titled_entry("c", "Add JWT auth middleware"),
        ];
        let refs: Vec<&WikiEntry> = entries.iter().collect();
        let assignments = cluster_titles(&refs);

        let label_of = |sid: &str| {
            assignments
                .iter()
                .find(|(s, _)| s == sid)
                .map(|(_, l)| l.clone())
                .unwrap()
        };

        // a & b share "api" + "security" → same cluster.
        assert_eq!(label_of("a"), label_of("b"));
        // b & c share "auth" + "jwt" + "middleware" → all three collapse via b.
        assert_eq!(label_of("b"), label_of("c"));

        let distinct: std::collections::HashSet<_> =
            assignments.iter().map(|(_, l)| l.clone()).collect();
        assert_eq!(distinct.len(), 1, "all three should merge into one task");
    }

    #[test]
    fn cluster_titles_keeps_unrelated_apart() {
        let entries = [
            titled_entry("a", "Add JWT auth middleware"),
            titled_entry("b", "Update database migration scripts"),
            titled_entry("c", "Refactor pricing service cache"),
        ];
        let refs: Vec<&WikiEntry> = entries.iter().collect();
        let assignments = cluster_titles(&refs);

        let distinct: std::collections::HashSet<_> =
            assignments.iter().map(|(_, l)| l.clone()).collect();
        assert_eq!(
            distinct.len(),
            3,
            "unrelated titles must stay in separate groups"
        );
    }

    #[test]
    fn cluster_label_prefers_most_frequent_then_shortest() {
        // Two identical long titles + one shorter variant: frequency wins, so the
        // repeated long title is the label even though a shorter one exists.
        let entries = [
            titled_entry("a", "Add JWT auth middleware"),
            titled_entry("b", "Add JWT auth middleware"),
            titled_entry("c", "JWT auth"),
        ];
        let refs: Vec<&WikiEntry> = entries.iter().collect();
        let assignments = cluster_titles(&refs);
        let label = &assignments[0].1;
        assert_eq!(label, "Add JWT auth middleware");
    }

    #[test]
    fn tokens_overlap_uses_ratio_not_absolute_count() {
        // Two long, unrelated titles that incidentally share TWO tokens
        // ("add" is a stopword, so the shared pair here is "service" + "api").
        // Under the old `shared >= 2` rule these merged; the overlap coefficient
        // (2 / 5 = 0.4 < 0.6) correctly keeps them apart.
        let a = significant_tokens("Add pricing service api cache layer");
        let b = significant_tokens("Add billing service api webhook handler");
        let shared: Vec<_> = a.iter().filter(|t| b.contains(t)).collect();
        assert_eq!(
            shared.len(),
            2,
            "fixture must share exactly two tokens to exercise the old rule"
        );
        assert!(
            !tokens_overlap(&a, &b),
            "two shared tokens out of five must NOT merge under the ratio rule"
        );

        // A short title fully contained in a long one still merges (coeff 1.0).
        let short = significant_tokens("pricing service");
        assert!(tokens_overlap(&short, &a));
    }

    #[test]
    fn tokens_overlap_singleton_does_not_overcluster() {
        // A title reducing to a SINGLE significant token must not merge with
        // every unrelated longer title that happens to contain that token —
        // the overlap coefficient alone would score 1.0 here.
        let single = significant_tokens("Fix API"); // -> ["api"]
        assert_eq!(single, vec!["api".to_string()]);
        let auth = significant_tokens("Add API auth"); // -> ["api", "auth"]
        let billing = significant_tokens("Update API billing"); // -> ["api", "billing"]
        assert!(
            !tokens_overlap(&single, &auth),
            "single shared token must not merge a singleton title"
        );
        assert!(
            !tokens_overlap(&single, &billing),
            "single shared token must not merge a singleton title"
        );
        // Two unrelated singletons that share their one token must also stay apart.
        let other_single = significant_tokens("API"); // -> ["api"]
        assert!(!tokens_overlap(&single, &other_single));
        // Genuinely related multi-token titles still cluster.
        let related_long = significant_tokens("Add API security with JWT auth middleware");
        let related_short = significant_tokens("Enhance API auth"); // -> ["api", "auth"]
        assert!(
            tokens_overlap(&related_short, &related_long),
            "two shared tokens out of two must still merge"
        );
    }

    #[test]
    fn cluster_titles_does_not_overcluster_singletons() {
        // "Fix API" (singleton "api") must NOT swallow unrelated API titles.
        let entries = [
            titled_entry("a", "Fix API"),
            titled_entry("b", "Add API auth"),
            titled_entry("c", "Update API billing"),
        ];
        let refs: Vec<&WikiEntry> = entries.iter().collect();
        let assignments = cluster_titles(&refs);
        let distinct: std::collections::HashSet<_> =
            assignments.iter().map(|(_, l)| l.clone()).collect();
        assert_eq!(
            distinct.len(),
            3,
            "a singleton-token title must not cluster with unrelated longer titles"
        );
    }

    #[test]
    fn significant_tokens_normalizes_nfc_nfd_equivalents() {
        // Precomposed "café" (NFC, U+00E9) and decomposed "café" (NFD,
        // "e" + U+0301 combining acute) are canonically equivalent and must
        // tokenize identically once normalized to NFC before stripping marks.
        let nfc = "Caf\u{00e9} module"; // café
        let nfd = "Cafe\u{0301} module"; // cafe + combining acute
        assert_ne!(nfc, nfd, "fixture must use distinct byte sequences");
        assert_eq!(significant_tokens(nfc), significant_tokens(nfd));
        assert!(significant_tokens(nfc).contains(&"café".to_string()));
    }

    #[test]
    fn cluster_titles_merges_nfc_nfd_equivalents() {
        let entries = [
            titled_entry("a", "Refactor Caf\u{00e9} Strat\u{00e9}gie"), // NFC
            titled_entry("b", "Refactor Cafe\u{0301} Strate\u{0301}gie"), // NFD
        ];
        let refs: Vec<&WikiEntry> = entries.iter().collect();
        let assignments = cluster_titles(&refs);
        let distinct: std::collections::HashSet<_> =
            assignments.iter().map(|(_, l)| l.clone()).collect();
        assert_eq!(
            distinct.len(),
            1,
            "canonically-equivalent titles must merge regardless of NFC/NFD form"
        );
    }

    #[test]
    fn cluster_titles_does_not_transitively_absorb_unrelated() {
        // Chain of titles where each adjacent pair shares two incidental tokens
        // but the ends are unrelated. The old union-growing signature plus the
        // `shared >= 2` rule made all of these collapse into one blob. With the
        // ratio rule and member-based matching they stay apart.
        let entries = [
            titled_entry("a", "Add pricing service api cache"),
            titled_entry("b", "Add billing service api webhook"),
            titled_entry("c", "Add billing report export csv"),
        ];
        let refs: Vec<&WikiEntry> = entries.iter().collect();
        let assignments = cluster_titles(&refs);
        let distinct: std::collections::HashSet<_> =
            assignments.iter().map(|(_, l)| l.clone()).collect();
        assert_eq!(
            distinct.len(),
            3,
            "incidental two-token overlaps must not chain unrelated titles into one cluster"
        );
    }

    #[test]
    fn cluster_titles_separates_distinct_stopword_only_titles() {
        // Titles made entirely of stopwords/generic verbs reduce to no
        // significant tokens. The old code lumped ALL of them into one arbitrary
        // group; now each distinct normalized title is its own singleton while
        // identical ones still collapse.
        let entries = [
            titled_entry("a", "Fix and update"),
            titled_entry("b", "Refactor and improve"),
            titled_entry("c", "Fix and update"),
        ];
        // Sanity: these really are token-empty so we exercise the empty path.
        assert!(significant_tokens("Fix and update").is_empty());
        assert!(significant_tokens("Refactor and improve").is_empty());

        let refs: Vec<&WikiEntry> = entries.iter().collect();
        let assignments = cluster_titles(&refs);
        let label_of = |sid: &str| {
            assignments
                .iter()
                .find(|(s, _)| s == sid)
                .map(|(_, l)| l.clone())
                .unwrap()
        };
        // Identical stopword-only titles merge; distinct ones do not.
        assert_eq!(label_of("a"), label_of("c"));
        assert_ne!(label_of("a"), label_of("b"));
        let distinct: std::collections::HashSet<_> =
            assignments.iter().map(|(_, l)| l.clone()).collect();
        assert_eq!(distinct.len(), 2);
    }

    #[test]
    fn significant_tokens_folds_non_ascii_case() {
        // Full Unicode lowercasing: "Café" and "café" must produce the same
        // token. to_ascii_lowercase left the accented capital untouched, so the
        // two titles would have clustered apart.
        assert_eq!(
            significant_tokens("Café Münchën Stratégie"),
            significant_tokens("café münchën stratégie")
        );
        assert!(significant_tokens("Café").contains(&"café".to_string()));
        assert_eq!(
            significant_tokens("İstanbul API"),
            significant_tokens("i\u{307}stanbul api")
        );
        assert!(significant_tokens("İstanbul").contains(&"istanbul".to_string()));
    }

    #[test]
    fn cluster_titles_merges_non_ascii_case_variants() {
        let entries = [
            titled_entry("a", "Refactor Café Stratégie module"),
            titled_entry("b", "Refactor café stratégie module"),
        ];
        let refs: Vec<&WikiEntry> = entries.iter().collect();
        let assignments = cluster_titles(&refs);
        let distinct: std::collections::HashSet<_> =
            assignments.iter().map(|(_, l)| l.clone()).collect();
        assert_eq!(distinct.len(), 1, "case-only differences must merge");
    }

    #[test]
    fn cluster_titles_groups_exact_duplicates() {
        // The degenerate case from the report: many identical titles must
        // collapse into exactly one group.
        let entries: Vec<WikiEntry> = (0..51)
            .map(|i| titled_entry(&format!("s{i}"), "Add JWT auth middleware"))
            .collect();
        let refs: Vec<&WikiEntry> = entries.iter().collect();
        let assignments = cluster_titles(&refs);
        let distinct: std::collections::HashSet<_> =
            assignments.iter().map(|(_, l)| l.clone()).collect();
        assert_eq!(distinct.len(), 1);
        assert_eq!(assignments.len(), 51);
    }

    fn entry_for(session_id: &str, client: &str) -> WikiEntry {
        let mut e = titled_entry(session_id, "ignored");
        e.client = client.to_string();
        e.title = None;
        e
    }

    #[test]
    fn extract_content_for_session_reads_real_claude_first_message() {
        // A claudecode transcript on disk, keyed by file stem == session_id.
        let dir = tempfile::tempdir().unwrap();
        let session_id = "sess-claude-1";
        let path = dir.path().join(format!("{session_id}.jsonl"));
        std::fs::write(
            &path,
            r#"{"type":"user","message":{"role":"user","content":[{"type":"text","text":"Fix the login bug"}]}}
"#,
        )
        .unwrap();

        let mut by_client_session: HashMap<(String, String), Vec<PathBuf>> = HashMap::new();
        by_client_session.insert(("claude".to_string(), session_id.to_string()), vec![path]);
        let index = SessionPathIndex {
            by_client_session,
            opencode_dbs: Vec::new(),
        };

        let entry = entry_for(session_id, "claude");
        let content = extract_content_for_session(&entry, &index);

        // The dispatcher must have reached the real claudecode extractor and
        // surfaced the actual first user message — not metadata-only.
        assert_eq!(
            content.first_user_message.as_deref(),
            Some("Fix the login bug")
        );
        assert_eq!(content.client, "claude");
    }

    #[test]
    fn extract_content_for_session_unknown_client_falls_back_to_metadata_only() {
        // An unsupported client has no dedicated extractor: must degrade to
        // metadata-only (None) without error or panic, even if a stray file
        // happens to share the session id.
        let dir = tempfile::tempdir().unwrap();
        let session_id = "sess-unknown-1";
        let path = dir.path().join(format!("{session_id}.jsonl"));
        std::fs::write(&path, "garbage\n").unwrap();

        let mut by_client_session: HashMap<(String, String), Vec<PathBuf>> = HashMap::new();
        by_client_session.insert(
            (
                "some-unsupported-client".to_string(),
                session_id.to_string(),
            ),
            vec![path],
        );
        let index = SessionPathIndex {
            by_client_session,
            opencode_dbs: Vec::new(),
        };

        let entry = entry_for(session_id, "some-unsupported-client");
        let content = extract_content_for_session(&entry, &index);

        assert!(content.first_user_message.is_none());
        assert_eq!(content.client, "some-unsupported-client");
    }

    #[test]
    fn extract_content_for_session_missing_file_falls_back_to_metadata_only() {
        // Supported client but no candidate file on disk: never panic, return
        // metadata-only.
        let index = SessionPathIndex::default();
        let entry = entry_for("does-not-exist", "claude");
        let content = extract_content_for_session(&entry, &index);
        assert!(content.first_user_message.is_none());
        assert_eq!(content.client, "claude");
    }

    #[test]
    fn session_path_index_isolates_clients_with_same_session_id() {
        // Two different clients share a session_id. Keying by (client, id) must
        // route each lookup to that client's own file, never the other's.
        let dir = tempfile::tempdir().unwrap();
        let claude_path = dir.path().join("claude.jsonl");
        let codex_path = dir.path().join("codex.jsonl");
        std::fs::write(&claude_path, "claude-bytes").unwrap();
        std::fs::write(&codex_path, "codex-bytes").unwrap();

        let mut by_client_session: HashMap<(String, String), Vec<PathBuf>> = HashMap::new();
        by_client_session.insert(
            ("claude".to_string(), "shared".to_string()),
            vec![claude_path.clone()],
        );
        by_client_session.insert(
            ("codex".to_string(), "shared".to_string()),
            vec![codex_path.clone()],
        );
        let index = SessionPathIndex {
            by_client_session,
            opencode_dbs: Vec::new(),
        };

        assert_eq!(index.candidates_for("claude", "shared"), vec![claude_path]);
        assert_eq!(index.candidates_for("codex", "shared"), vec![codex_path]);
        // Lookup for a client without a matching key returns nothing.
        assert!(index.candidates_for("gemini", "shared").is_empty());
    }

    #[test]
    fn build_session_path_index_keys_gemini_by_inner_session_id() {
        // A Gemini chat recording's wiki session_id is the in-file `sessionId`,
        // not the filename stem. The index must key by that inner id so the
        // summarizer lookup resolves and surfaces the real first prompt.
        let home = tempfile::tempdir().unwrap();
        let chats = home
            .path()
            .join(".gemini")
            .join("tmp")
            .join("projhash")
            .join("chats");
        std::fs::create_dir_all(&chats).unwrap();
        let inner_id = "b8d9ab56-e7da-4dca-abc1-eb61158bed4f";
        let file = chats.join("session-2026-06-08T19-53-b8d9ab56.json");
        std::fs::write(
            &file,
            format!(
                r#"{{"sessionId":"{inner_id}","messages":[{{"type":"user","content":"Hello Gemini"}}]}}"#
            ),
        )
        .unwrap();

        let opts = ReportOptions {
            json: false,
            since: None,
            until: None,
            workspace: None,
            client: None,
            no_summarize: false,
            summarizer: String::new(),
            rebuild: false,
            home_dir: Some(home.path().to_string_lossy().into_owned()),
            scanner_settings: Default::default(),
            today: false,
            week: false,
            month: false,
            full: false,
        };
        let index = build_session_path_index(&opts);

        // Lookup by the wiki session_id (inner id) must find the file.
        let candidates = index.candidates_for("gemini", inner_id);
        assert!(
            candidates.iter().any(|p| p == &file),
            "expected gemini index keyed by inner sessionId, candidates={candidates:?}"
        );

        let entry = entry_for(inner_id, "gemini");
        let content = extract_content_for_session(&entry, &index);
        assert_eq!(content.first_user_message.as_deref(), Some("Hello Gemini"));
    }
}