unlost 0.17.1

Unlost - Local-first code memory for a workspace.
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
use crate::cli::OutputFormat;
use chrono::TimeZone;
use indicatif::{ProgressBar, ProgressStyle};
use std::collections::BTreeSet;
use std::time::Duration;

const WRAP_WIDTH: usize = 80;
const DORMANCY_THRESHOLD_MS: i64 = 7 * 24 * 60 * 60 * 1000;
/// Two notes within this gap are part of the same cluster.
const CLUSTER_GAP_MS: i64 = 4 * 60 * 60 * 1000;

// ── Public entry point ──────────────────────────────────────────────────────

#[allow(clippy::too_many_arguments)]
pub async fn run(
    topic: Vec<String>,
    limit: usize,
    since: Option<String>,
    no_llm: bool,
    llm_model: Option<String>,
    output: OutputFormat,
    embed_model: String,
    embed_cache_dir: Option<String>,
    timeline: bool,
) -> anyhow::Result<()> {
    let query = topic.join(" ");
    let query = query.trim().to_string();

    let since_ms = match since {
        Some(ref s) => crate::util::parse_time_filter(s)?,
        None => None,
    };

    let cwd = std::env::current_dir()?;
    let ws = crate::workspace::get_or_create_workspace_paths(&cwd)?;

    // No topic = discovery mode: show emergent themes from recent capsules.
    if query.is_empty() {
        return run_themes(&ws, since_ms, output).await;
    }

    let spinner = if let Some(target) = crate::narrative::spinner_draw_target(output) {
        let pb = ProgressBar::new_spinner();
        pb.set_draw_target(target);
        pb.set_style(
            ProgressStyle::with_template("{spinner:.cyan} {msg:.dim}")
                .unwrap()
                .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"),
        );
        pb.enable_steady_tick(Duration::from_millis(80));
        pb.set_message("pulling on the thread...");
        Some(pb)
    } else {
        None
    };

    let embedder = crate::embed::load_embedder(
        &embed_model,
        embed_cache_dir.as_deref().map(std::path::PathBuf::from),
        false,
    )
    .await?;

    let framed = crate::storage::frame_query_for_command(
        &query,
        crate::storage::QueryIntent::Trace,
    );

    let mut hits = crate::storage::query_capsules_cross_workspace(
        &framed,
        embedder,
        &ws,
        10,
        limit,
    )
    .await;

    if let Some(since_ms) = since_ms {
        hits.retain(|h| h.ts_ms >= since_ms);
    }

    if let Some(ref spinner) = spinner {
        spinner.finish_and_clear();
    }

    if hits.is_empty() {
        println!("unlost: no moments found for this topic in any workspace.");
        return Ok(());
    }

    // Sort chronologically for analysis; renderers handle display order.
    hits.sort_by_key(|h| h.ts_ms);

    let mut view = ThreadView::from_hits(&hits, &ws);

    // Collect all hit ids so we can exclude them from neighbor scans.
    let thread_hit_ids: BTreeSet<String> = hits.iter().map(|h| h.id.clone()).collect();
    view.enrich_spatial_context(&ws, &thread_hit_ids).await;

    let llm_spinner = if !no_llm {
        if let Some(target) = crate::narrative::spinner_draw_target(output) {
            let pb = ProgressBar::new_spinner();
            pb.set_draw_target(target);
            pb.set_style(
                ProgressStyle::with_template("{spinner:.cyan} {msg:.dim}")
                    .unwrap()
                    .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"),
            );
            pb.enable_steady_tick(Duration::from_millis(80));
            pb.set_message("reading between the lines...");
            Some(pb)
        } else {
            None
        }
    } else {
        None
    };

    let (narrative, narrative_warning) = if no_llm {
        (None, None)
    } else {
        match crate::narrative::llm_thread_narrative(llm_model.as_deref(), &query, &view).await {
            Ok(n) => (Some(n), None),
            Err(e) => {
                tracing::debug!(error = %e, "thread narrative unavailable");
                (
                    None,
                    Some(
                        "No LLM configured; showing extracted notes only. Configure with `unlost config llm ollama --model <model>`."
                            .to_string(),
                    ),
                )
            }
        }
    };

    if let Some(ref sp) = llm_spinner {
        sp.finish_and_clear();
    }

    let rendered = if timeline {
        render_timeline(&query, narrative.as_deref(), narrative_warning.as_deref(), &view, output)
    } else {
        render_trail(&query, narrative.as_deref(), narrative_warning.as_deref(), &view, output)
    };
    print!("{rendered}");

    Ok(())
}

// ── ThreadView: pre-analyzed structure ──────────────────────────────────────

/// A cluster of nearby notes (within CLUSTER_GAP_MS of each other).
pub struct Cluster {
    pub notes: Vec<DisplayNote>,
    pub earliest_ts: i64,
    pub latest_ts: i64,
    pub provenance: String,
    /// Distinct source_pointer URIs found in this cluster (for linking back).
    pub source_links: Vec<String>,
    /// What the broader session was about (from checkpoint narrative).
    pub session_context: Option<String>,
    /// Other topics that were being discussed nearby in time (not in this thread).
    pub nearby_topics: Vec<String>,
}

pub struct DisplayNote {
    pub decision: String,
    pub rationale: Option<String>,
    pub failure_mode: Option<String>,
    pub symbols: Vec<String>,
    pub echoes: usize,
    pub ts_ms: i64,
}

pub struct LongGap {
    pub from_ts: i64,
    pub to_ts: i64,
    pub days: i64,
}

pub struct ThreadView {
    /// All clusters, sorted chronologically (oldest first).
    pub clusters: Vec<Cluster>,
    /// Gaps >=7d between consecutive clusters, chronological.
    pub long_gaps: Vec<LongGap>,
    /// Total moments before dedup.
    pub total_moments: usize,
    /// Total notes after dedup.
    pub total_notes: usize,
    pub earliest_ts: i64,
    pub latest_ts: i64,
    pub span_days: i64,
    pub project_count: usize,
}

impl ThreadView {
    pub fn from_hits(hits: &[crate::CapsuleHit], ws: &crate::WorkspacePaths) -> Self {
        let project_ids: BTreeSet<&str> = hits
            .iter()
            .filter_map(|h| h.origin_workspace_id.as_deref())
            .collect();
        let project_count = project_ids.len().max(1);

        // Group into clusters by temporal proximity.
        let raw_clusters = cluster_hits(hits);
        let mut clusters: Vec<Cluster> = Vec::new();
        let mut total_notes = 0usize;

        for raw in &raw_clusters {
            let folded = fold_similar(raw);
            total_notes += folded.len();

            let prov = cluster_provenance(raw, ws);
            let earliest = raw.first().unwrap().ts_ms;
            let latest = raw.last().unwrap().ts_ms;

            let mut source_links: Vec<String> = raw
                .iter()
                .filter_map(|h| h.meta.source_pointer.as_deref())
                .filter(|s| !s.trim().is_empty())
                .map(str::to_string)
                .collect::<BTreeSet<_>>()
                .into_iter()
                .collect();
            source_links.truncate(3);

            clusters.push(Cluster {
                notes: folded,
                earliest_ts: earliest,
                latest_ts: latest,
                provenance: prov,
                source_links,
                session_context: None,
                nearby_topics: Vec::new(),
            });
        }

        // Find long gaps between consecutive clusters.
        let mut long_gaps: Vec<LongGap> = Vec::new();
        for i in 1..clusters.len() {
            let from_ts = clusters[i - 1].latest_ts;
            let to_ts = clusters[i].earliest_ts;
            let gap = to_ts - from_ts;
            if gap >= DORMANCY_THRESHOLD_MS {
                let days = (gap / (24 * 60 * 60 * 1000)).max(1);
                long_gaps.push(LongGap { from_ts, to_ts, days });
            }
        }

        let earliest_ts = hits.first().unwrap().ts_ms;
        let latest_ts = hits.last().unwrap().ts_ms;
        let span_days = ((latest_ts - earliest_ts) / (24 * 60 * 60 * 1000)).max(0);

        ThreadView {
            clusters,
            long_gaps,
            total_moments: hits.len(),
            total_notes,
            earliest_ts,
            latest_ts,
            span_days,
            project_count,
        }
    }

    /// Enrich clusters with spatial context: what session they belonged to
    /// and what other topics were being discussed nearby.
    pub async fn enrich_spatial_context(
        &mut self,
        ws: &crate::WorkspacePaths,
        thread_hit_ids: &BTreeSet<String>,
    ) {
        const NEIGHBOR_WINDOW_MS: i64 = 30 * 60 * 1000; // ±30 minutes

        for cluster in &mut self.clusters {
            // 1. Checkpoint: find the session narrative covering this cluster's time range.
            if let Ok(db) = lancedb::connect(ws.db_dir.to_string_lossy().as_ref())
                .execute()
                .await
            {
                if let Ok(checkpoints) =
                    crate::storage_checkpoint::get_checkpoints_in_range(
                        &db,
                        &ws.id,
                        cluster.earliest_ts,
                        cluster.latest_ts,
                    )
                    .await
                {
                    // Take the first checkpoint that covers this time range.
                    if let Some(cp) = checkpoints.first() {
                        cluster.session_context = extract_session_topic(&cp.narrative);
                    }
                }
            }

            // 2. Nearby topics: scan a time window around the cluster for
            //    capsules not in the thread.
            let since = cluster.earliest_ts - NEIGHBOR_WINDOW_MS;
            let until = cluster.latest_ts + NEIGHBOR_WINDOW_MS;
            if let Ok(nearby) = crate::storage::scan_capsules_lancedb(
                ws,
                20,
                None,
                None,
                None,
                Some(since),
                Some(until),
            )
            .await
            {
                let mut topics: Vec<String> = Vec::new();
                let mut seen = BTreeSet::new();
                for hit in &nearby {
                    // Skip notes already in the thread.
                    if thread_hit_ids.contains(&hit.id) {
                        continue;
                    }
                    // Skip git/changelog/init — those are facts, not conversation.
                    if matches!(hit.meta.source.as_str(), "git" | "changelog" | "init") {
                        continue;
                    }
                    let text = hit.capsule.decision.trim();
                    if text.is_empty() || text.len() < 20 {
                        continue;
                    }
                    if is_placeholder_decision(text) {
                        continue;
                    }
                    // Skip low-signal neighbor patterns.
                    let lower = text.to_ascii_lowercase();
                    if lower.starts_with("no action")
                        || lower.starts_with("no actionable")
                        || lower.starts_with("no specific")
                        || lower.starts_with("user simply")
                        || lower.starts_with("user rejects")
                        || lower.starts_with("user disputes")
                        || lower.starts_with("user is unsure")
                        || lower.starts_with("acknowledge")
                        || lower.contains("conversation ended")
                        || lower.contains("conversation effectively")
                        || lower.contains("no further request")
                        || lower.contains("end interaction")
                        || lower.contains("close interaction")
                        || lower.contains("awaiting context")
                        || lower.starts_with("none;")
                        || lower.starts_with("none.")
                    {
                        continue;
                    }
                    // Skip single-word non-space decisions under 30 chars.
                    if !text.contains(' ') && text.len() < 30 {
                        continue;
                    }
                    let short = truncate(text, 60);
                    // Deduplicate similar neighbor topics.
                    let key = short.to_ascii_lowercase();
                    if seen.contains(&key) {
                        continue;
                    }
                    // Skip if this topic overlaps with any note in the cluster.
                    let dominated = cluster
                        .notes
                        .iter()
                        .any(|n| similar_notes_loose(&short, &n.decision));
                    if dominated {
                        continue;
                    }
                    seen.insert(key);
                    topics.push(short);
                    if topics.len() >= 3 {
                        break;
                    }
                }
                cluster.nearby_topics = topics;
            }
        }
    }
}

/// Extract the "WHAT WAS WORKED ON" section from a checkpoint narrative,
/// or fall back to the first non-empty line.
fn extract_session_topic(narrative: &str) -> Option<String> {
    let narrative = narrative.trim();
    if narrative.is_empty() {
        return None;
    }
    // Look for the structured section header.
    let marker = "WHAT WAS WORKED ON";
    if let Some(start) = narrative.find(marker) {
        let after = &narrative[start + marker.len()..];
        // Skip the header line itself and any blank lines.
        let content: String = after
            .lines()
            .skip_while(|l| l.trim().is_empty() || l.trim() == marker)
            .take(2)
            .map(|l| l.trim())
            .filter(|l| !l.is_empty())
            .collect::<Vec<_>>()
            .join(" ");
        if !content.is_empty() {
            return Some(truncate(&content, 120));
        }
    }
    // Fallback: first non-empty line.
    for line in narrative.lines() {
        let l = line.trim();
        if !l.is_empty() && !l.chars().all(|c| c == '' || c == '-' || c == '=') {
            return Some(truncate(l, 120));
        }
    }
    None
}

fn cluster_hits<'a>(hits: &'a [crate::CapsuleHit]) -> Vec<Vec<&'a crate::CapsuleHit>> {
    let mut clusters: Vec<Vec<&crate::CapsuleHit>> = Vec::new();
    let mut current: Vec<&crate::CapsuleHit> = Vec::new();

    for hit in hits {
        if current.is_empty() {
            current.push(hit);
            continue;
        }
        let prev_ts = current.last().unwrap().ts_ms;
        if hit.ts_ms - prev_ts <= CLUSTER_GAP_MS {
            current.push(hit);
        } else {
            clusters.push(std::mem::take(&mut current));
            current.push(hit);
        }
    }
    if !current.is_empty() {
        clusters.push(current);
    }
    clusters
}

fn fold_similar(hits: &[&crate::CapsuleHit]) -> Vec<DisplayNote> {
    let mut notes: Vec<DisplayNote> = Vec::new();
    for hit in hits {
        let text = note_text(hit);
        if is_placeholder_decision(&text) {
            continue;
        }
        if let Some(existing) = notes.iter_mut().find(|n| similar_notes(&text, &n.decision)) {
            existing.echoes += 1;
        } else {
            let cap = &hit.capsule;
            let rationale = if cap.rationale.trim().is_empty() {
                None
            } else {
                Some(truncate(
                    &humanize_rationale(&first_sentence(cap.rationale.trim())),
                    80,
                ))
            };
            let failure_mode = match cap.failure_mode {
                crate::types::FailureMode::None => None,
                crate::types::FailureMode::Drift => Some("drift".into()),
                crate::types::FailureMode::Rediscovery => Some("rediscovery".into()),
                crate::types::FailureMode::DecisionConflict => Some("decision conflict".into()),
                crate::types::FailureMode::RetrySpiral => Some("retry spiral".into()),
                crate::types::FailureMode::FalseProgress => Some("false progress".into()),
                crate::types::FailureMode::UnboundedHorizon => Some("unbounded horizon".into()),
            };
            let symbols: Vec<String> = cap
                .symbols
                .iter()
                .filter(|s| s.contains('/') || s.contains('.'))
                .take(2)
                .cloned()
                .collect();
            notes.push(DisplayNote {
                decision: text,
                rationale,
                failure_mode,
                symbols,
                echoes: 0,
                ts_ms: hit.ts_ms,
            });
        }
    }
    notes
}

fn cluster_provenance(hits: &[&crate::CapsuleHit], ws: &crate::WorkspacePaths) -> String {
    // Collect unique provenances across all notes in this cluster.
    let mut labels: BTreeSet<String> = BTreeSet::new();
    for hit in hits {
        if let Some(label) = provenance_label(hit, ws) {
            labels.insert(label);
        }
    }
    if labels.len() == 1 {
        labels.into_iter().next().unwrap()
    } else if labels.is_empty() {
        workspace_label_from_root(&ws.root).unwrap_or_default()
    } else {
        // Multiple sources within the cluster — just show workspace.
        let ws_name = workspace_label_from_root(&ws.root).unwrap_or_default();
        let sources: Vec<&String> = labels.iter().collect();
        if sources.iter().all(|s| s.starts_with(&ws_name)) {
            ws_name
        } else {
            labels.into_iter().collect::<Vec<_>>().join(", ")
        }
    }
}

// ── Trail renderer (default) ────────────────────────────────────────────────

/// Default view: narrative-first thread. The story is the content;
/// anchor clusters are quote blocks that ground it. Use --timeline for
/// the full reverse-chronological cluster dump.
fn render_trail(
    topic: &str,
    narrative: Option<&str>,
    narrative_warning: Option<&str>,
    view: &ThreadView,
    output: OutputFormat,
) -> String {
    let mut out = String::new();

    render_header(&mut out, topic, view, output);
    render_narrative_block(&mut out, narrative, narrative_warning, output);

    // Pick at most 3 anchor clusters: the turning points worth showing.
    let anchors = pick_thread_anchors(view);
    let anchors_total = view.clusters.len();
    let anchors_shown = anchors.len();

    for (i, idx) in anchors.iter().enumerate() {
        let cluster = &view.clusters[*idx];

        // Anchor role label, picked per position not per chronology.
        let role = anchor_role(i, anchors_shown, *idx, anchors_total);

        // Time-distance phrase relative to "today" / latest cluster.
        let when_phrase = relative_when(cluster.earliest_ts, view.latest_ts);

        out.push('\n');
        render_anchor_header(&mut out, role, &when_phrase, cluster, output);

        // Spatial context: what session this was part of, what else was nearby
        render_spatial_context_compact(&mut out, cluster, output);

        // Show up to 2 representative notes per anchor (the longest = most signal).
        let notes_to_show = pick_representative_notes(&cluster.notes, 2);
        for note in &notes_to_show {
            render_note_anchor(&mut out, note, output);
        }

        // Source links — dim, under the notes
        render_source_links_inline(&mut out, cluster, output);
    }

    // Hint: more notes available in --timeline
    let hidden_clusters = anchors_total.saturating_sub(anchors_shown);
    if hidden_clusters > 0 {
        out.push('\n');
        let hint = format!(
            "  {} more cluster{} in `unlost thread \"{}\" --timeline`",
            hidden_clusters,
            if hidden_clusters == 1 { "" } else { "s" },
            topic,
        );
        if is_ansi(output) {
            out.push_str(&format!("\x1b[2;3m{}\x1b[0m\n", hint));
        } else {
            out.push_str(&format!("{}\n", hint));
        }
    }

    out.push('\n');
    out
}

/// Pick at most 3 cluster indices that represent the thread's turning points.
/// Strategy: always include most-recent and origin; if there's room, pick the
/// "pivot" — the cluster with the most notes, a failure mode, or sitting just
/// after the longest gap.
fn pick_thread_anchors(view: &ThreadView) -> Vec<usize> {
    let n = view.clusters.len();
    if n == 0 {
        return Vec::new();
    }
    if n <= 3 {
        return (0..n).rev().collect(); // newest first
    }

    let newest = n - 1;
    let origin = 0;

    // Find the pivot among middle clusters: largest gap-after-previous wins,
    // tiebreak by note count.
    let mut best_pivot: Option<(usize, i64, usize)> = None;
    for i in 1..n - 1 {
        let gap = view.clusters[i].earliest_ts - view.clusters[i - 1].latest_ts;
        let notes = view.clusters[i].notes.len();
        let score = (gap, notes);
        if best_pivot.map(|(_, g, n2)| (gap, notes) > (g, n2)).unwrap_or(true) {
            best_pivot = Some((i, score.0, score.1));
        }
    }

    let mut picks = vec![newest, origin];
    if let Some((idx, _, _)) = best_pivot {
        picks.push(idx);
    }
    picks.sort_by(|a, b| b.cmp(a)); // newest first
    picks.dedup();
    picks
}

/// Decide what to label this anchor based on its position in the chosen set.
fn anchor_role(position: usize, total_shown: usize, cluster_idx: usize, total_clusters: usize) -> &'static str {
    let is_newest = cluster_idx == total_clusters - 1;
    let is_origin = cluster_idx == 0;
    if total_shown == 1 {
        ""
    } else if is_newest {
        "where it landed"
    } else if is_origin {
        "where it started"
    } else {
        let _ = position;
        "the turn"
    }
}

fn relative_when(ts_ms: i64, latest_ts: i64) -> String {
    let days = ((latest_ts - ts_ms) / (24 * 60 * 60 * 1000)).max(0);
    if days == 0 {
        "today".to_string()
    } else if days == 1 {
        "yesterday".to_string()
    } else if days < 7 {
        format!("{} days ago", days)
    } else if days < 30 {
        let weeks = (days / 7).max(1);
        format!("{} week{} ago", weeks, if weeks == 1 { "" } else { "s" })
    } else if days < 90 {
        let weeks = days / 7;
        format!("{} weeks ago", weeks)
    } else {
        let months = (days / 30).max(3);
        format!("{} months ago", months)
    }
}

fn render_anchor_header(
    out: &mut String,
    role: &str,
    when_phrase: &str,
    cluster: &Cluster,
    output: OutputFormat,
) {
    let date_str = fmt_day_label(cluster.earliest_ts);

    if is_ansi(output) {
        if !role.is_empty() {
            out.push_str(&format!("\x1b[36m{}\x1b[0m  ", role));
        }
        out.push_str(&format!("\x1b[1;97m{}\x1b[0m", date_str));
        if !when_phrase.is_empty() {
            out.push_str(&format!("  \x1b[2m({})\x1b[0m", when_phrase));
        }
        out.push('\n');
        if !cluster.provenance.is_empty() {
            push_wrapped_ansi(
                out,
                "  \x1b[2;36m",
                "\x1b[0m",
                "  \x1b[2;36m",
                "\x1b[0m",
                &cluster.provenance,
                WRAP_WIDTH - 2,
            );
        }
    } else {
        if !role.is_empty() {
            out.push_str(&format!("{}  ", role));
        }
        out.push_str(&date_str);
        if !when_phrase.is_empty() {
            out.push_str(&format!("  ({})", when_phrase));
        }
        out.push('\n');
        if !cluster.provenance.is_empty() {
            push_wrapped_plain(
                out,
                "  ",
                "  ",
                &cluster.provenance,
                WRAP_WIDTH - 2,
            );
        }
    }
    out.push('\n');
}

/// Render spatial context as a single short line (not stacked).
fn render_spatial_context_compact(out: &mut String, cluster: &Cluster, output: OutputFormat) {
    if let Some(ref ctx) = cluster.session_context {
        let line = format!("inside: {}", truncate(ctx, 72));
        if is_ansi(output) {
            push_wrapped_ansi(
                out,
                "  \x1b[2;3m",
                "\x1b[0m",
                "  \x1b[2;3m",
                "\x1b[0m",
                &line,
                WRAP_WIDTH - 4,
            );
        } else {
            push_wrapped_plain(out, "  ", "  ", &line, WRAP_WIDTH - 4);
        }
        out.push('\n');
    }
    if !cluster.nearby_topics.is_empty() {
        // Single line, comma-separated, hard-truncated.
        let joined = cluster
            .nearby_topics
            .iter()
            .map(|t| truncate(t, 40))
            .collect::<Vec<_>>()
            .join(", ");
        let line = format!("alongside: {}", truncate(&joined, 100));
        if is_ansi(output) {
            push_wrapped_ansi(
                out,
                "  \x1b[2;3m",
                "\x1b[0m",
                "  \x1b[2;3m",
                "\x1b[0m",
                &line,
                WRAP_WIDTH - 4,
            );
        } else {
            push_wrapped_plain(out, "  ", "  ", &line, WRAP_WIDTH - 4);
        }
        out.push('\n');
    }
}

/// Pick up to `max` representative notes from a cluster: longest decisions
/// (more signal), preferring ones with failure modes.
fn pick_representative_notes(notes: &[DisplayNote], max: usize) -> Vec<&DisplayNote> {
    let mut scored: Vec<(usize, &DisplayNote)> = notes
        .iter()
        .map(|n| {
            let mut score = n.decision.len();
            if n.failure_mode.is_some() {
                score += 1000;
            }
            score += n.echoes * 20;
            (score, n)
        })
        .collect();
    scored.sort_by(|a, b| b.0.cmp(&a.0));
    scored.into_iter().take(max).map(|(_, n)| n).collect()
}

/// Render a note inside an anchor — quote-style, indented with a bar.
fn render_note_anchor(out: &mut String, note: &DisplayNote, output: OutputFormat) {
    let text = truncate(&note.decision, 200);
    let echo_suffix = if note.echoes > 0 {
        if note.echoes == 1 {
            " (+1)".to_string()
        } else {
            format!(" (+{})", note.echoes)
        }
    } else {
        String::new()
    };
    let fm_prefix = if note.failure_mode.is_some() { "" } else { "" };
    let body = format!("{fm_prefix}{text}{echo_suffix}");

    if is_ansi(output) {
        push_wrapped_ansi(
            out,
            "  \x1b[36m│\x1b[0m \x1b[36m",
            "\x1b[0m",
            "  \x1b[36m│\x1b[0m \x1b[36m",
            "\x1b[0m",
            &body,
            WRAP_WIDTH - 4,
        );
    } else {
        push_wrapped_plain(out, "", "", &body, WRAP_WIDTH - 4);
    }
    out.push('\n');
}

fn render_source_links_inline(out: &mut String, cluster: &Cluster, output: OutputFormat) {
    if cluster.source_links.is_empty() {
        return;
    }
    // First link only — keep it tight.
    let link = &cluster.source_links[0];
    if is_ansi(output) {
        out.push_str(&format!("  \x1b[2;36m→ {}\x1b[0m\n", link));
    } else {
        out.push_str(&format!("{}\n", link));
    }
}

// ── Timeline renderer (--timeline) ──────────────────────────────────────────

fn render_timeline(
    topic: &str,
    narrative: Option<&str>,
    narrative_warning: Option<&str>,
    view: &ThreadView,
    output: OutputFormat,
) -> String {
    let mut out = String::new();

    render_header(&mut out, topic, view, output);
    render_narrative_block(&mut out, narrative, narrative_warning, output);

    // Timeline: most recent first (reverse chronological).
    let cluster_count = view.clusters.len();
    for (ci, cluster) in view.clusters.iter().rev().enumerate() {
        // Gap from the previous (newer) cluster
        if ci > 0 {
            let newer_cluster = &view.clusters[cluster_count - ci];
            let gap_ms = newer_cluster.earliest_ts - cluster.latest_ts;
            if gap_ms >= DORMANCY_THRESHOLD_MS {
                let label = gap_label(gap_ms);
                if is_ansi(output) {
                    out.push_str(&format!("\n\x1b[2m        ~ {}\x1b[0m\n", label));
                } else {
                    out.push_str(&format!("\n        ~ {}\n", label));
                }
            }
        }

        let date_str = fmt_day_label(cluster.earliest_ts);
        out.push('\n');
        if is_ansi(output) {
            out.push_str(&format!("\x1b[1;97m{}\x1b[0m", date_str));
            out.push('\n');
            if !cluster.provenance.is_empty() {
                push_wrapped_ansi(
                    &mut out,
                    "  \x1b[2;36m",
                    "\x1b[0m",
                    "  \x1b[2;36m",
                    "\x1b[0m",
                    &cluster.provenance,
                    WRAP_WIDTH - 2,
                );
            }
        } else {
            out.push_str(&date_str);
            out.push('\n');
            if !cluster.provenance.is_empty() {
                push_wrapped_plain(
                    &mut out,
                    "  ",
                    "  ",
                    &cluster.provenance,
                    WRAP_WIDTH - 2,
                );
            }
        }
        out.push('\n');

        render_source_links(&mut out, cluster, output);
        render_spatial_context(&mut out, cluster, output);

        for (ni, note) in cluster.notes.iter().rev().enumerate() {
            render_note(&mut out, note, ni + 1, output);
        }
    }

    out.push('\n');
    out
}

// ── Shared rendering helpers ────────────────────────────────────────────────

fn render_header(out: &mut String, topic: &str, view: &ThreadView, output: OutputFormat) {
    let topic = topic.trim();
    if is_ansi(output) {
        out.push_str(&format!("\x1b[1m{}\x1b[0m\n", topic));
    } else {
        out.push_str(topic);
        out.push('\n');
    }

    let project_label = if view.project_count > 1 {
        format!(" across {} projects", view.project_count)
    } else {
        String::new()
    };

    let earliest = fmt_range_date(view.earliest_ts);
    let latest = fmt_range_date(view.latest_ts);

    let meta = format!(
        "{} moment{} · {} note{}{} · {} back to {}{}",
        view.total_moments,
        if view.total_moments == 1 { "" } else { "s" },
        view.total_notes,
        if view.total_notes == 1 { "" } else { "s" },
        project_label,
        latest,
        earliest,
        span_suffix(view.span_days),
    );

    if is_ansi(output) {
        out.push_str(&format!("\x1b[2m{}\x1b[0m\n", meta));
    } else {
        out.push_str(&meta);
        out.push('\n');
    }
}

fn render_narrative_block(
    out: &mut String,
    narrative: Option<&str>,
    warning: Option<&str>,
    output: OutputFormat,
) {
    if let Some(n) = narrative {
        out.push('\n');
        let rendered = crate::narrative::render_narrative(output, n);
        for line in rendered.lines() {
            out.push_str(line);
            out.push('\n');
        }
    } else if let Some(w) = warning {
        out.push('\n');
        if is_ansi(output) {
            push_wrapped_ansi(out, "\x1b[2m", "\x1b[0m", "\x1b[2m", "\x1b[0m", w, WRAP_WIDTH);
        } else {
            push_wrapped_plain(out, "", "", w, WRAP_WIDTH);
        }
        out.push('\n');
    }
}

fn render_spatial_context(out: &mut String, cluster: &Cluster, output: OutputFormat) {
    // Session context — what the broader conversation was about
    if let Some(ref ctx) = cluster.session_context {
        if is_ansi(output) {
            push_wrapped_ansi(
                out,
                "  \x1b[2;3mpart of: ",
                "\x1b[0m\n",
                "          \x1b[2;3m",
                "\x1b[0m\n",
                ctx,
                WRAP_WIDTH - 10,
            );
        } else {
            push_wrapped_plain(out, "  part of: ", "          ", ctx, WRAP_WIDTH - 10);
            out.push('\n');
        }
    }

    // Nearby topics — what else was being discussed at the same time
    if !cluster.nearby_topics.is_empty() {
        let label = if cluster.nearby_topics.len() == 1 {
            "  also discussing: "
        } else {
            "  also discussing: "
        };
        for (i, topic) in cluster.nearby_topics.iter().enumerate() {
            let prefix = if i == 0 { label } else { "                    " };
            if is_ansi(output) {
                out.push_str(&format!("\x1b[2m{}{}\x1b[0m\n", prefix, truncate(topic, 58)));
            } else {
                out.push_str(&format!("{}{}\n", prefix, truncate(topic, 58)));
            }
        }
    }
}

fn render_source_links(out: &mut String, cluster: &Cluster, output: OutputFormat) {
    if cluster.source_links.is_empty() {
        return;
    }
    for link in &cluster.source_links {
        if is_ansi(output) {
            out.push_str(&format!("  \x1b[2;36m{}\x1b[0m\n", link));
        } else {
            out.push_str(&format!("  {}\n", link));
        }
    }
}

/// Full multi-line note for --timeline view.
fn render_note(out: &mut String, note: &DisplayNote, index: usize, output: OutputFormat) {
    // Decision — the primary signal, cyan tint
    if is_ansi(output) {
        push_wrapped_ansi(
            out,
            &format!("\n  \x1b[2m{:>2}\x1b[0m  \x1b[36m", index),
            "\x1b[0m",
            "      \x1b[36m",
            "\x1b[0m",
            &note.decision,
            WRAP_WIDTH - 6,
        );
    } else {
        push_wrapped_plain(
            out,
            &format!("\n  {:>2}  ", index),
            "      ",
            &note.decision,
            WRAP_WIDTH - 6,
        );
    }

    // Rationale — dim
    if let Some(ref rationale) = note.rationale {
        if is_ansi(output) {
            push_wrapped_ansi(
                out,
                "\n      \x1b[2m",
                "\x1b[0m",
                "      \x1b[2m",
                "\x1b[0m",
                rationale,
                WRAP_WIDTH - 6,
            );
        } else {
            push_wrapped_plain(out, "\n      ", "      ", rationale, WRAP_WIDTH - 6);
        }
    }

    // Failure mode — yellow
    if let Some(ref fm) = note.failure_mode {
        if is_ansi(output) {
            out.push_str(&format!("\n      \x1b[33m▲ {}\x1b[0m", fm));
        } else {
            out.push_str(&format!("\n{}", fm));
        }
    }

    // Symbols — dim green
    if !note.symbols.is_empty() {
        let sym_str = note.symbols.join("  ");
        if is_ansi(output) {
            out.push_str(&format!("\n      \x1b[2;32m{}\x1b[0m", sym_str));
        } else {
            out.push_str(&format!("\n      {}", sym_str));
        }
    }

    // Echoes — dim italic
    if note.echoes > 0 {
        let line = if note.echoes == 1 {
            "same idea appears once more".to_string()
        } else {
            format!("same idea appears {} more times", note.echoes)
        };
        if is_ansi(output) {
            out.push_str(&format!("\n      \x1b[2;3m{}\x1b[0m", line));
        } else {
            out.push_str(&format!("\n      {}", line));
        }
    }

    out.push('\n');
}

fn is_ansi(output: OutputFormat) -> bool {
    output == OutputFormat::Ansi && std::env::var_os("NO_COLOR").is_none()
}

// ── ThreadView to LLM context ───────────────────────────────────────────────

impl ThreadView {
    /// Build the structured context string for the LLM, giving it pre-analyzed
    /// clusters, gaps, and motifs instead of raw hit rows.
    pub fn to_llm_context(&self) -> String {
        let mut ctx = String::new();

        ctx.push_str(&format!(
            "Thread: {} moments, {} notes, {} clusters, spanning {} days\n\n",
            self.total_moments,
            self.total_notes,
            self.clusters.len(),
            self.span_days,
        ));

        for (ci, cluster) in self.clusters.iter().enumerate() {
            let date = fmt_range_date(cluster.earliest_ts);
            let gap_ctx = if ci > 0 {
                let prev = &self.clusters[ci - 1];
                let gap_days =
                    (cluster.earliest_ts - prev.latest_ts) / (24 * 60 * 60 * 1000);
                if gap_days >= 90 {
                    format!(" (returned after {} months)", (gap_days / 30).max(3))
                } else if gap_days >= 30 {
                    format!(" (returned after {} weeks)", (gap_days / 7).max(4))
                } else if gap_days >= 7 {
                    format!(" (gap: {} days)", gap_days)
                } else {
                    String::new()
                }
            } else {
                String::new()
            };

            ctx.push_str(&format!(
                "Cluster {} · {}{} · {}\n",
                ci + 1,
                date,
                gap_ctx,
                cluster.provenance,
            ));
            if let Some(ref session_ctx) = cluster.session_context {
                ctx.push_str(&format!("  session_topic: {}\n", session_ctx));
            }
            if !cluster.nearby_topics.is_empty() {
                ctx.push_str(&format!(
                    "  also_discussing: {}\n",
                    cluster.nearby_topics.join("; "),
                ));
            }
            for note in &cluster.notes {
                ctx.push_str(&format!("  decision: {}\n", note.decision));
                if let Some(ref r) = note.rationale {
                    ctx.push_str(&format!("  rationale: {}\n", r));
                }
                if let Some(ref fm) = note.failure_mode {
                    ctx.push_str(&format!("  failure_mode: {}\n", fm));
                }
                if !note.symbols.is_empty() {
                    ctx.push_str(&format!("  symbols: {}\n", note.symbols.join(", ")));
                }
                if note.echoes > 0 {
                    ctx.push_str(&format!("  echoes: {} similar notes folded\n", note.echoes));
                }
            }
            ctx.push('\n');
        }

        if !self.long_gaps.is_empty() {
            ctx.push_str("Long gaps:\n");
            for g in &self.long_gaps {
                ctx.push_str(&format!(
                    "  {}{} ({} days)\n",
                    fmt_range_date(g.from_ts),
                    fmt_range_date(g.to_ts),
                    g.days,
                ));
            }
            ctx.push('\n');
        }

        ctx
    }
}

// ── Text helpers ────────────────────────────────────────────────────────────

fn provenance_label(hit: &crate::CapsuleHit, ws: &crate::WorkspacePaths) -> Option<String> {
    let workspace = hit
        .origin_workspace_id
        .as_deref()
        .and_then(crate::workspace::workspace_label_by_id)
        .or_else(|| workspace_label_from_root(&ws.root))?;

    let source = hit
        .meta
        .source_pointer
        .as_deref()
        .and_then(crate::workspace::resolve_source_label)
        .or_else(|| fallback_source_label(hit));

    match source {
        Some(source) if !source.trim().is_empty() => Some(format!("{workspace} · {source}")),
        _ => Some(workspace),
    }
}

fn workspace_label_from_root(root: &std::path::Path) -> Option<String> {
    root.file_name()
        .and_then(|n| n.to_str())
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .map(str::to_string)
}

fn fallback_source_label(hit: &crate::CapsuleHit) -> Option<String> {
    let source = hit.meta.source.trim();
    let path = hit.meta.request_path.trim();
    if source.is_empty() {
        return None;
    }
    // Map internal source/path combos to human-readable labels.
    if source == "changelog" {
        return if path.is_empty() {
            Some("CHANGELOG".into())
        } else {
            Some(format!("CHANGELOG {path}"))
        };
    }
    if source == "git" {
        return if path.is_empty() {
            Some("git".into())
        } else {
            Some(format!("git {path}"))
        };
    }
    if source == "record" {
        // /opencode/record, /companion, /v1/chat/completions, etc.
        if path.contains("opencode") || path.contains("companion") {
            return Some("OpenCode".into());
        }
        if path.contains("claude") {
            return Some("Claude".into());
        }
        if path.contains("copilot") {
            return Some("Copilot".into());
        }
        if path.contains("chat/completions") {
            return Some("chat".into());
        }
        return None; // Just show workspace, not "record"
    }
    if source == "init" {
        return Some("init".into());
    }
    None
}

fn note_text(hit: &crate::CapsuleHit) -> String {
    let decision = hit.capsule.decision.trim();
    let text = if decision.is_empty() {
        hit.capsule.intent.trim().to_string()
    } else {
        decision.to_string()
    };
    humanize_note_text(&text)
}

/// Returns true if a decision looks like a machine-generated placeholder
/// that carries no real signal.
fn is_placeholder_decision(s: &str) -> bool {
    let lower = s.trim().to_ascii_lowercase();
    let placeholders = [
        "proceed",
        "defer",
        "pause",
        "continue",
        "no_action_required",
        "accepted/implemented",
        "awaiting_commit_instruction",
        "awaiting_user_instruction",
        "proceed_with_planned_action",
        "awaiting context to determine next action",
        "no specific task",
    ];
    for p in placeholders {
        if lower == p || lower.starts_with(p) {
            return true;
        }
    }
    // Single word with underscores = likely a status marker
    if !lower.contains(' ') && lower.contains('_') {
        return true;
    }
    false
}

fn span_suffix(days: i64) -> String {
    if days >= 90 {
        format!(" · {}-month arc", (days / 30).max(3))
    } else if days >= 30 {
        format!(" · {}-week arc", (days / 7).max(4))
    } else if days >= 7 {
        format!(" · {}-day arc", days)
    } else {
        String::new()
    }
}

fn gap_label(gap_ms: i64) -> String {
    let days = (gap_ms / (24 * 60 * 60 * 1000)).max(1);
    if days >= 90 {
        format!("{} months earlier · long return", (days / 30).max(3))
    } else if days >= 30 {
        format!("{} weeks earlier · shelved for a while", (days / 7).max(4))
    } else {
        format!("{} days earlier", days)
    }
}

fn fmt_range_date(ts_ms: i64) -> String {
    chrono::Utc
        .timestamp_millis_opt(ts_ms)
        .single()
        .map(|dt| dt.format("%Y-%m-%d").to_string())
        .unwrap_or_else(|| ts_ms.to_string())
}

fn fmt_day_label(ts_ms: i64) -> String {
    chrono::Utc
        .timestamp_millis_opt(ts_ms)
        .single()
        .map(|dt| dt.format("%b %d").to_string())
        .unwrap_or_else(|| ts_ms.to_string())
}

// ── Word wrapping ───────────────────────────────────────────────────────────

fn push_wrapped_plain(
    out: &mut String,
    first_prefix: &str,
    cont_prefix: &str,
    text: &str,
    width: usize,
) {
    let lines = wrap_words(text, width);
    for (i, line) in lines.iter().enumerate() {
        if i == 0 {
            out.push_str(first_prefix);
        } else {
            out.push('\n');
            out.push_str(cont_prefix);
        }
        out.push_str(line);
    }
}

fn push_wrapped_ansi(
    out: &mut String,
    first_prefix: &str,
    first_suffix: &str,
    cont_prefix: &str,
    cont_suffix: &str,
    text: &str,
    width: usize,
) {
    let lines = wrap_words(text, width);
    for (i, line) in lines.iter().enumerate() {
        if i == 0 {
            out.push_str(first_prefix);
            out.push_str(line);
            out.push_str(first_suffix);
        } else {
            out.push('\n');
            out.push_str(cont_prefix);
            out.push_str(line);
            out.push_str(cont_suffix);
        }
    }
}

fn wrap_words(text: &str, width: usize) -> Vec<String> {
    if width == 0 {
        return vec![text.to_string()];
    }
    let mut lines: Vec<String> = Vec::new();
    let mut current = String::new();
    let mut current_len = 0usize;
    for word in text.split_whitespace() {
        let word_len = word.chars().count();
        if current.is_empty() {
            current.push_str(word);
            current_len = word_len;
        } else if current_len + 1 + word_len <= width {
            current.push(' ');
            current.push_str(word);
            current_len += 1 + word_len;
        } else {
            lines.push(current);
            current = word.to_string();
            current_len = word_len;
        }
    }
    if !current.is_empty() {
        lines.push(current);
    }
    if lines.is_empty() {
        lines.push(String::new());
    }
    lines
}

// ── Similarity ──────────────────────────────────────────────────────────────

/// Looser similarity check for excluding neighbor topics that overlap with cluster notes.
fn similar_notes_loose(a: &str, b: &str) -> bool {
    let a_tokens = note_tokens(a);
    let b_tokens = note_tokens(b);
    if a_tokens.is_empty() || b_tokens.is_empty() {
        return false;
    }
    let intersection = a_tokens.intersection(&b_tokens).count() as f32;
    let smaller = (a_tokens.len().min(b_tokens.len())) as f32;
    // If more than 30% of the smaller set's tokens appear in the other, it's dominated.
    (intersection / smaller) >= 0.3
}

fn similar_notes(a: &str, b: &str) -> bool {
    let a_tokens = note_tokens(a);
    let b_tokens = note_tokens(b);
    if a_tokens.is_empty() || b_tokens.is_empty() {
        return false;
    }
    let intersection = a_tokens.intersection(&b_tokens).count() as f32;
    let union = a_tokens.union(&b_tokens).count() as f32;
    (intersection / union) >= 0.48
}

fn note_tokens(s: &str) -> BTreeSet<String> {
    s.split(|c: char| !c.is_alphanumeric())
        .map(str::to_ascii_lowercase)
        .filter(|t| t.len() >= 4)
        .collect()
}

// ── Text normalization ──────────────────────────────────────────────────────

fn truncate(s: &str, max: usize) -> String {
    if s.chars().count() <= max {
        s.to_string()
    } else {
        let end = s
            .char_indices()
            .nth(max.saturating_sub(1))
            .map(|(i, _)| i)
            .unwrap_or(s.len());
        format!("{}", &s[..end])
    }
}

fn first_sentence(s: &str) -> String {
    let bytes = s.as_bytes();
    for i in 0..bytes.len() {
        if bytes[i] == b'.' {
            let after = i + 1;
            if after >= bytes.len() || bytes[after] == b' ' || bytes[after] == b'\n' {
                let candidate = s[..=i].trim();
                if !candidate.is_empty() {
                    return candidate.to_string();
                }
            }
        }
    }
    s.to_string()
}

fn humanize_rationale(s: &str) -> String {
    let s = s.trim();
    let replacements = [
        ("The user wants ", "Wanted "),
        ("The user wanted ", "Wanted "),
        ("The user needs ", "Needed "),
        ("The user needed ", "Needed "),
        ("The user expects ", "Expected "),
        ("The user expected ", "Expected "),
        ("The user expresses ", "Expressed "),
        ("The user expressed ", "Expressed "),
        ("The user is assessing ", "Assessing "),
        ("The user was assessing ", "Assessing "),
        ("The user agrees ", "Agreed "),
        ("The user agreed ", "Agreed "),
        ("The user highlighted ", "Flagged "),
        ("The user asked ", "Asked "),
        ("The user is interested in ", "Wanted to understand "),
        ("The user was interested in ", "Wanted to understand "),
        ("The user seeks to understand ", "Wanted to understand "),
        ("The user sought to understand ", "Wanted to understand "),
        ("User initiated ", "Started "),
        ("User initiates ", "Started "),
        ("User seeks to understand ", "Wanted to understand "),
        ("User seeks ", "Wanted "),
        ("User is interested in ", "Wanted to understand "),
        ("User was interested in ", "Wanted to understand "),
        ("User wants ", "Wanted "),
        ("User wanted ", "Wanted "),
        ("User needs ", "Needed "),
        ("User needed ", "Needed "),
        ("User expects ", "Expected "),
        ("User expected ", "Expected "),
        ("User expresses ", "Expressed "),
        ("User expressed ", "Expressed "),
        ("User agrees ", "Agreed "),
        ("User agreed ", "Agreed "),
        ("User highlighted ", "Flagged "),
        ("User asked ", "Asked "),
        ("The conversation focuses on understanding how ", "Wanted to understand how "),
        ("The conversation focused on understanding how ", "Wanted to understand how "),
        ("The conversation focuses on ", "Looked at "),
        ("The conversation focused on ", "Looked at "),
    ];
    for (from, to) in replacements {
        if let Some(rest) = s.strip_prefix(from) {
            return polish_rationale(format!("{to}{rest}"));
        }
    }
    polish_rationale(s.to_string())
}

fn polish_rationale(s: String) -> String {
    s.replace(" but wants ", " but wanted ")
        .replace("user expects", "expected")
        .replace("User expects", "Expected")
        .replace(" and seeks clarity ", " and needed clarity ")
        .replace(" seeks clarity ", " needed clarity ")
        .replace("indicating a need for", "pointing toward")
        .replace("their knowledge or project", "the design")
        .replace("Wanted to understand analyzing ", "Wanted better ways to analyze ")
        .replace("Wanted to understand expanding ", "Wanted to expand ")
        .replace("Needed code-level", "Needed a code-level")
}

fn humanize_note_text(s: &str) -> String {
    let s = s.trim();
    let replacements = [
        ("User requests ", "Requested "),
        ("User requested ", "Requested "),
        ("User initiates ", "Started "),
        ("User initiated ", "Started "),
        ("The user requests ", "Requested "),
        ("The user requested ", "Requested "),
    ];
    for (from, to) in replacements {
        if let Some(rest) = s.strip_prefix(from) {
            return format!("{to}{rest}");
        }
    }
    s.to_string()
}

// ── Themes (discovery mode) ─────────────────────────────────────────────────
//
// When `unlost thread` is run without a topic, surface the implicit structure
// of what's been on the user's mind: recurring decision motifs across recent
// capsules. No LLM, no folder structure — themes emerge from token overlap.

const THEMES_DEFAULT_SINCE_MS: i64 = 180 * 24 * 60 * 60 * 1000; // 6 months
const THEMES_SCAN_LIMIT: usize = 1500;

/// A discovered theme: a recurring set of concepts that appears across multiple
/// capsules, with a representative label and timing info.
struct Theme {
    /// Distinct word stems that define this theme (used for the label).
    tokens: BTreeSet<String>,
    /// Capsules belonging to this theme.
    members: Vec<ThemeMember>,
    /// Span in days from earliest to latest member.
    span_days: i64,
    /// Latest timestamp in the theme.
    latest_ts: i64,
    /// Earliest timestamp in the theme.
    earliest_ts: i64,
}

struct ThemeMember {
    decision: String,
    ts_ms: i64,
    project: Option<String>,
}

async fn run_themes(
    ws: &crate::WorkspacePaths,
    since_ms: Option<i64>,
    output: OutputFormat,
) -> anyhow::Result<()> {
    let spinner = if let Some(target) = crate::narrative::spinner_draw_target(output) {
        let pb = ProgressBar::new_spinner();
        pb.set_draw_target(target);
        pb.set_style(
            ProgressStyle::with_template("{spinner:.cyan} {msg:.dim}")
                .unwrap()
                .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"),
        );
        pb.enable_steady_tick(Duration::from_millis(80));
        pb.set_message("looking for what's been on your mind...");
        Some(pb)
    } else {
        None
    };

    let since = since_ms.unwrap_or_else(|| crate::workspace::now_ms() - THEMES_DEFAULT_SINCE_MS);

    let hits = match crate::storage::scan_capsules_lancedb(
        ws,
        THEMES_SCAN_LIMIT,
        None,
        None,
        None,
        Some(since),
        None,
    )
    .await
    {
        Ok(h) => h,
        Err(e) => {
            if let Some(sp) = spinner {
                sp.finish_and_clear();
            }
            eprintln!("unlost: could not scan capsules: {e}");
            return Ok(());
        }
    };

    if let Some(sp) = spinner {
        sp.finish_and_clear();
    }

    let themes = compute_themes(&hits);

    let rendered = render_themes(&themes, since, output);
    print!("{rendered}");
    Ok(())
}

/// Cluster capsules by token-overlap on their decision text, producing themes.
/// Greedy single-pass: each capsule joins the first existing theme it overlaps
/// with above the threshold, otherwise starts a new theme.
fn compute_themes(hits: &[crate::CapsuleHit]) -> Vec<Theme> {
    let mut themes: Vec<Theme> = Vec::new();
    let mut total_processed = 0usize;

    for hit in hits {
        let text = hit.capsule.decision.trim();
        if text.is_empty() || is_placeholder_decision(text) {
            continue;
        }
        let lower = text.to_ascii_lowercase();
        if lower.starts_with("no action")
            || lower.starts_with("no actionable")
            || lower.starts_with("acknowledge")
            || lower.starts_with("acknowledged")
            || lower.starts_with("user acknowledg")
            || lower.starts_with("agent acknowledg")
            || lower.contains("conversation ended")
            || lower.contains("end interaction")
            || lower.contains("close interaction")
            || lower.contains("awaiting context")
            || lower.starts_with("none;")
            || lower.starts_with("none.")
            || lower.starts_with("user disputes")
            || lower.starts_with("user rejects")
            || lower.starts_with("user is unsure")
            || lower.starts_with("user simply")
        {
            continue;
        }
        // Skip git/changelog — they're facts, not thoughts.
        if matches!(hit.meta.source.as_str(), "git" | "changelog" | "init") {
            continue;
        }
        let tokens = theme_tokens(text);
        if tokens.len() < 2 {
            continue;
        }
        total_processed += 1;

        let project = hit
            .origin_workspace_id
            .as_deref()
            .and_then(crate::workspace::workspace_label_by_id);

        let member = ThemeMember {
            decision: text.to_string(),
            ts_ms: hit.ts_ms,
            project,
        };

        // Find best matching theme (highest overlap above threshold).
        let mut best: Option<(usize, f32)> = None;
        for (i, theme) in themes.iter().enumerate() {
            let inter = tokens.intersection(&theme.tokens).count() as f32;
            let smaller = (tokens.len().min(theme.tokens.len())) as f32;
            let overlap = inter / smaller;
            if overlap >= 0.45 {
                if best.map(|(_, s)| overlap > s).unwrap_or(true) {
                    best = Some((i, overlap));
                }
            }
        }

        if let Some((idx, _)) = best {
            // Merge into existing theme. Keep theme's defining tokens intersection-ish
            // so they remain tight, but add high-frequency newcomers.
            for t in &tokens {
                themes[idx].tokens.insert(t.clone());
            }
            themes[idx].earliest_ts = themes[idx].earliest_ts.min(member.ts_ms);
            themes[idx].latest_ts = themes[idx].latest_ts.max(member.ts_ms);
            themes[idx].members.push(member);
        } else {
            themes.push(Theme {
                tokens,
                earliest_ts: member.ts_ms,
                latest_ts: member.ts_ms,
                span_days: 0,
                members: vec![member],
            });
        }
    }

    let _ = total_processed;

    // Finalize span_days and filter weak themes.
    // A real theme should have ≥3 members and at least 2 distinct days of activity
    // (otherwise it's just one session with multiple turns on the same topic).
    themes.retain(|t| {
        if t.members.len() < 3 {
            return false;
        }
        let day = |ts: i64| ts / (24 * 60 * 60 * 1000);
        let distinct_days: BTreeSet<i64> = t.members.iter().map(|m| day(m.ts_ms)).collect();
        distinct_days.len() >= 2
    });
    for t in &mut themes {
        t.span_days = ((t.latest_ts - t.earliest_ts) / (24 * 60 * 60 * 1000)).max(0);
    }

    // Rank by composite score: recurrence + span + recency.
    let now = crate::workspace::now_ms();
    themes.sort_by(|a, b| {
        let sa = theme_score(a, now);
        let sb = theme_score(b, now);
        sb.partial_cmp(&sa).unwrap_or(std::cmp::Ordering::Equal)
    });

    themes.truncate(10);
    themes
}

fn theme_score(t: &Theme, now_ms: i64) -> f32 {
    // Recurrence: how many times this theme appeared (saturates via ln).
    let recurrence = (t.members.len() as f32).ln_1p();
    // Span: themes that lived across many days are inherently meaningful.
    let span = ((t.span_days as f32 + 1.0).ln() * 1.0).min(5.0);
    // Recency: gentle decay, so a 6-month-old long-running theme still surfaces.
    let age_days = ((now_ms - t.latest_ts).max(0) / (24 * 60 * 60 * 1000)) as f32;
    let recency = (-age_days / 90.0).exp(); // half-life ~62 days
    // Multi-project bonus: themes that span projects suggest cross-cutting concerns.
    let projects: BTreeSet<&str> = t
        .members
        .iter()
        .filter_map(|m| m.project.as_deref())
        .collect();
    let project_bonus = if projects.len() > 1 { 1.0 } else { 0.0 };
    recurrence * 1.5 + span * 1.2 + recency * 0.8 + project_bonus
}

/// Extract significant word stems from a decision string, filtering stopwords.
fn theme_tokens(s: &str) -> BTreeSet<String> {
    const STOPWORDS: &[&str] = &[
        // Common English filler
        "this", "that", "with", "from", "into", "have", "will", "their", "them",
        "they", "would", "could", "should", "about", "which", "where", "when",
        "what", "while", "than", "then", "been", "being", "more", "most",
        "some", "such", "only", "also", "very", "much", "many", "make", "made",
        "over", "under", "after", "before", "between", "through", "during",
        "without", "within", "across", "against", "among", "around",
        // Generic words that appear in capsule machinery
        "user", "users", "thing", "things", "stuff", "item", "items",
        "want", "wants", "wanted", "wanting",
        "need", "needs", "needed", "needing",
        "consider", "considering", "considered",
        "propose", "proposed", "proposing", "proposal",
        "explore", "exploring", "explored", "exploration",
        "investigate", "investigating", "investigated", "investigation",
        "review", "reviewing", "reviewed",
        "implement", "implementing", "implemented", "implementation",
        "adopt", "adopting", "adopted",
        "discuss", "discussing", "discussed",
        "clarify", "clarifying", "clarified", "clarification",
        "assess", "assessing", "assessed", "assessment",
        "request", "requesting", "requested",
        "suggest", "suggesting", "suggested", "suggestion",
        "complete", "completed", "completing", "completion",
        "focus", "focusing", "focused",
        "ensure", "ensuring", "ensured",
        "address", "addressing", "addressed",
        // Generic structure/UX nouns
        "section", "sections", "header", "headers", "label", "labels",
        "output", "outputs", "input", "inputs", "value", "values",
        "field", "fields", "option", "options", "default", "defaults",
        "format", "formats", "structure", "structures",
        "approach", "approaches", "decision", "decisions",
        "agent", "agents", "system", "systems",
        "feature", "features", "behavior", "behaviors",
        "support", "supported", "supporting",
        "current", "currently", "existing",
        "general", "generic", "specific", "particular",
        "additional", "different", "various", "multiple",
        // Meta-evaluation tokens
        "irrelevant", "relevant", "useful", "important",
        "closest", "nearest", "similar",
        "snapshot", "snapshots", "document", "documents", "documented",
        "incorporate", "incorporated", "incorporating",
        "artifact", "artifacts", "recorded", "recording",
        // Acknowledgment / status noise
        "acknowledge", "acknowledged", "acknowledging", "acknowledgment",
        "affirm", "affirmed", "affirming", "affirmation", "affirmative",
        "confirm", "confirmed", "confirming", "confirmation",
        "prior", "previous", "previously", "earlier",
        "requirement", "requirements", "request", "requests",
        "expectation", "expectations", "available", "placeholder",
        "outdated", "diagnostic", "diagnostics",
        "detected", "detect", "detecting", "detection",
        "metadata", "data", "info", "information",
        "update", "updated", "updating", "change", "changes",
        "next", "prepare", "preparation", "prepared", "preparing",
        "inclusion", "filtering", "include", "included", "exclude", "excluded",
        "codebase", "directory", "directories",
        "consistently", "accordingly", "automatically", "explicitly",
        "context", "contexts", "source", "sources",
        "improvement", "improvements", "improve", "improving", "improved",
        "statement", "statements", "subscription", "subscriptions",
        "event", "events", "logging", "capturing", "capture",
        "feasibility", "configuration", "configurations",
        "provided", "providing", "provides",
        "commit", "commits", "committed", "committing",
        "dependencie", "dependencies", "dependency", "bump", "bumped",
    ];
    s.split(|c: char| !c.is_alphanumeric())
        .filter_map(|t| {
            let t = t.trim().to_ascii_lowercase();
            if t.len() < 4 || t.chars().all(|c| c.is_ascii_digit()) {
                return None;
            }
            if STOPWORDS.contains(&t.as_str()) {
                return None;
            }
            // Light stemming: drop trailing s for tokens >5 chars.
            let stem = if t.len() > 5 && t.ends_with('s') && !t.ends_with("ss") {
                t[..t.len() - 1].to_string()
            } else {
                t
            };
            Some(stem)
        })
        .collect()
}

/// Build a human-readable theme label from its defining tokens.
/// Prefers technical/proper-noun tokens (mixed case, snake_case, kebab-case),
/// then falls back to frequency-ranked common tokens.
fn theme_label(theme: &Theme) -> String {
    // Token freq + best original-case spelling for each token.
    let mut freq: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
    let mut original_case: std::collections::HashMap<String, String> = std::collections::HashMap::new();

    for m in &theme.members {
        // Walk original text to preserve case.
        for raw_word in m.decision.split(|c: char| !c.is_alphanumeric() && c != '_' && c != '-') {
            let raw = raw_word.trim();
            if raw.is_empty() {
                continue;
            }
            let lower = raw.to_ascii_lowercase();
            // Apply the same filter as theme_tokens.
            let toks = theme_tokens(raw);
            if toks.is_empty() {
                continue;
            }
            let stem = toks.iter().next().unwrap().clone();
            *freq.entry(stem.clone()).or_default() += 1;
            // Keep the most "interesting" original case: prefer mixed case, then snake/kebab.
            let cur = original_case.entry(stem).or_insert_with(|| raw.to_string());
            if score_case(raw) > score_case(cur) {
                *cur = raw.to_string();
            }
            let _ = lower;
        }
    }

    // Score tokens: coverage + length + technical-look bonus.
    let n = theme.members.len();
    let mut scored: Vec<(String, f32)> = freq
        .into_iter()
        .filter(|(_, c)| *c >= 2)
        .map(|(t, c)| {
            let coverage = c as f32 / n as f32;
            let length_bonus = (t.len() as f32).min(12.0) * 0.08;
            let original = original_case.get(&t).cloned().unwrap_or_else(|| t.clone());
            let tech_bonus = score_case(&original) as f32 * 0.5;
            (t, coverage + length_bonus + tech_bonus)
        })
        .collect();
    scored.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

    let picked: Vec<String> = scored
        .into_iter()
        .take(3)
        .map(|(t, _)| original_case.get(&t).cloned().unwrap_or(t))
        .collect();
    if picked.is_empty() {
        theme
            .members
            .first()
            .map(|m| truncate(&m.decision, 48))
            .unwrap_or_default()
    } else {
        picked.join(" · ")
    }
}

/// Score a word's "case interestingness": proper nouns and code identifiers win.
fn score_case(s: &str) -> u8 {
    let has_underscore = s.contains('_');
    let has_dash = s.contains('-');
    let has_upper = s.chars().any(|c| c.is_ascii_uppercase());
    let has_lower = s.chars().any(|c| c.is_ascii_lowercase());
    let mixed_case = has_upper && has_lower;
    let starts_upper = s.chars().next().map(|c| c.is_ascii_uppercase()).unwrap_or(false);
    let mut score: u8 = 0;
    if has_underscore || has_dash {
        score += 3;
    }
    if mixed_case && !starts_upper {
        // camelCase
        score += 3;
    } else if starts_upper && has_lower {
        // ProperCase
        score += 2;
    }
    score
}

fn render_themes(themes: &[Theme], since_ms: i64, output: OutputFormat) -> String {
    let mut out = String::new();

    // Header — window phrased as "the last N months/weeks"
    let now = crate::workspace::now_ms();
    let window_phrase = window_phrase_from_since(since_ms, now);

    if is_ansi(output) {
        out.push_str("\x1b[1mwhat's been on your mind\x1b[0m\n");
        out.push_str(&format!(
            "\x1b[2m{} theme{} surfaced from the last {}\x1b[0m\n",
            themes.len(),
            if themes.len() == 1 { "" } else { "s" },
            window_phrase,
        ));
    } else {
        out.push_str("what's been on your mind\n");
        out.push_str(&format!(
            "{} theme{} surfaced from the last {}\n",
            themes.len(),
            if themes.len() == 1 { "" } else { "s" },
            window_phrase,
        ));
    }

    if themes.is_empty() {
        out.push('\n');
        if is_ansi(output) {
            out.push_str("\x1b[2m  Nothing recurring yet. Try a longer window: `unlost thread --since 1y`\x1b[0m\n");
        } else {
            out.push_str("  Nothing recurring yet. Try a longer window: `unlost thread --since 1y`\n");
        }
        return out;
    }

    let now = crate::workspace::now_ms();

    for (i, theme) in themes.iter().enumerate() {
        out.push('\n');
        let label = theme_label(theme);
        let last_when = relative_when_from_now(theme.latest_ts, now);
        let projects: BTreeSet<&str> = theme
            .members
            .iter()
            .filter_map(|m| m.project.as_deref())
            .collect();
        let project_suffix = if projects.is_empty() {
            String::new()
        } else if projects.len() == 1 {
            format!(" · {}", projects.iter().next().unwrap())
        } else {
            format!(" · {} projects", projects.len())
        };

        // Theme header line: rank + label + meta
        if is_ansi(output) {
            out.push_str(&format!(
                "  \x1b[2m{:>2}.\x1b[0m  \x1b[1;36m{}\x1b[0m\n",
                i + 1,
                label,
            ));
            out.push_str(&format!(
                "      \x1b[2m{} appearance{}{}  ·  last seen {}\x1b[0m\n",
                theme.members.len(),
                if theme.members.len() == 1 { "" } else { "s" },
                project_suffix,
                last_when,
            ));
        } else {
            out.push_str(&format!("  {:>2}.  {}\n", i + 1, label));
            out.push_str(&format!(
                "      {} appearance{}{}  ·  last seen {}\n",
                theme.members.len(),
                if theme.members.len() == 1 { "" } else { "s" },
                project_suffix,
                last_when,
            ));
        }

        // Suggested command — clickable-feel cyan
        if is_ansi(output) {
            out.push_str(&format!(
                "      \x1b[2;36m→ unlost thread \"{}\"\x1b[0m\n",
                label,
            ));
        } else {
            out.push_str(&format!("      → unlost thread \"{}\"\n", label));
        }
    }

    out.push('\n');
    if is_ansi(output) {
        out.push_str("\x1b[2;3m  Pick one and dive in, or widen the window: `unlost thread --since 1y`\x1b[0m\n");
    } else {
        out.push_str("  Pick one and dive in, or widen the window: `unlost thread --since 1y`\n");
    }

    out
}

fn window_phrase_from_since(since_ms: i64, now_ms: i64) -> String {
    let days = ((now_ms - since_ms) / (24 * 60 * 60 * 1000)).max(1);
    if days < 14 {
        format!("{} days", days)
    } else if days < 60 {
        let weeks = (days / 7).max(2);
        format!("{} weeks", weeks)
    } else if days < 730 {
        let months = (days as f32 / 30.0).round().max(2.0) as i64;
        format!("{} months", months)
    } else {
        let years = (days as f32 / 365.0).round().max(2.0) as i64;
        format!("{} years", years)
    }
}

fn relative_when_from_now(ts_ms: i64, now_ms: i64) -> String {
    let days = ((now_ms - ts_ms) / (24 * 60 * 60 * 1000)).max(0);
    if days == 0 {
        "today".to_string()
    } else if days == 1 {
        "yesterday".to_string()
    } else if days < 7 {
        format!("{} days ago", days)
    } else if days < 30 {
        let weeks = (days / 7).max(1);
        format!("{} week{} ago", weeks, if weeks == 1 { "" } else { "s" })
    } else if days < 90 {
        let weeks = days / 7;
        format!("{} weeks ago", weeks)
    } else {
        let months = (days / 30).max(3);
        format!("{} months ago", months)
    }
}