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
//! Claude hooks shim.
//!
//! Invoked by Claude hooks (UserPromptSubmit, Stop) via stdin JSON.
//! Reads hook event, dispatches to Flow.check() or transcript ingestion.
//!
//! Cursor state is persisted per-session to avoid re-processing transcript lines.

use crate::companion::flow::{
    AgentKind, CheckEvent, Flow, FlowConfig, RecordTurnEvent, UsageEvent,
};
use crate::workspace::get_or_create_workspace_paths;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::io::{BufRead, IsTerminal, Read, Write};
use std::path::{Path, PathBuf};

// ============================================================================
// Hook input types (from Claude Code)
// ============================================================================

#[derive(Debug, Deserialize)]
struct HookInput {
    hook_event_name: String,
    session_id: String,
    cwd: String,
    #[serde(default)]
    transcript_path: Option<String>,
    /// For UserPromptSubmit
    #[serde(default)]
    prompt: Option<String>,
}

// ============================================================================
// Hook output types (to Claude Code)
// ============================================================================

#[derive(Debug, Serialize)]
struct HookOutput {
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "hookSpecificOutput")]
    hook_specific_output: Option<HookSpecificOutput>,
}

#[derive(Debug, Serialize)]
struct HookSpecificOutput {
    #[serde(rename = "hookEventName")]
    hook_event_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(rename = "additionalContext")]
    additional_context: Option<String>,
}

// ============================================================================
// Transcript parsing types
// ============================================================================

#[derive(Debug, Deserialize)]
struct TranscriptLine {
    #[serde(rename = "type")]
    line_type: Option<String>,
    #[serde(rename = "isSidechain")]
    is_sidechain: Option<bool>,
    uuid: Option<String>,
    message: Option<TranscriptMessage>,
    #[serde(flatten)]
    extra: std::collections::HashMap<String, serde_json::Value>,
}

#[derive(Debug, Deserialize)]
struct TranscriptMessage {
    role: Option<String>,
    content: Option<serde_json::Value>,
    model: Option<String>,
    usage: Option<TranscriptUsage>,
}

#[derive(Debug, Deserialize)]
struct TranscriptUsage {
    input_tokens: Option<i64>,
    output_tokens: Option<i64>,
    cache_read_input_tokens: Option<i64>,
    cache_creation_input_tokens: Option<i64>,
}

// ============================================================================
// Cursor state (persisted per session)
// ============================================================================

#[derive(Debug, Serialize, Deserialize, Default)]
struct CursorState {
    byte_offset: u64,
    last_uuid: Option<String>,
}

fn cursor_path(workspace_id: &str, session_id: &str) -> PathBuf {
    crate::workspace::unlost_workspace_dir(workspace_id)
        .join("claude")
        .join(format!("{}.cursor", session_id))
}

fn legacy_cursor_path(workspace_id: &str, session_id: &str) -> PathBuf {
    crate::workspace::unlost_workspace_dir(workspace_id)
        .join("claudecode")
        .join(format!("{}.cursor", session_id))
}

fn load_cursor(workspace_id: &str, session_id: &str) -> CursorState {
    let path = cursor_path(workspace_id, session_id);
    if let Ok(data) = std::fs::read_to_string(&path) {
        return serde_json::from_str(&data).unwrap_or_default();
    }
    // Legacy location (pre-claude rename).
    let legacy = legacy_cursor_path(workspace_id, session_id);
    if let Ok(data) = std::fs::read_to_string(&legacy) {
        return serde_json::from_str(&data).unwrap_or_default();
    }
    CursorState::default()
}

fn save_cursor(workspace_id: &str, session_id: &str, cursor: &CursorState) {
    let path = cursor_path(workspace_id, session_id);
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    if let Ok(data) = serde_json::to_string(cursor) {
        let _ = std::fs::write(&path, data);
    }
}

fn turnkeys_path(workspace_id: &str, session_id: &str) -> PathBuf {
    crate::workspace::unlost_workspace_dir(workspace_id)
        .join("claude")
        .join(format!("{}.turnkeys", session_id))
}

fn legacy_turnkeys_path(workspace_id: &str, session_id: &str) -> PathBuf {
    crate::workspace::unlost_workspace_dir(workspace_id)
        .join("claudecode")
        .join(format!("{}.turnkeys", session_id))
}

fn load_turnkeys(workspace_id: &str, session_id: &str) -> HashSet<String> {
    let path = turnkeys_path(workspace_id, session_id);
    let data = match std::fs::read_to_string(&path) {
        Ok(s) => s,
        Err(_) => {
            // Legacy location (pre-claude rename).
            let legacy = legacy_turnkeys_path(workspace_id, session_id);
            match std::fs::read_to_string(&legacy) {
                Ok(s) => s,
                Err(_) => return HashSet::new(),
            }
        }
    };
    data.lines()
        .map(|l| l.trim())
        .filter(|l| !l.is_empty())
        .map(|l| l.to_string())
        .collect()
}

fn append_turnkeys(workspace_id: &str, session_id: &str, keys: &[String]) {
    if keys.is_empty() {
        return;
    }
    let path = turnkeys_path(workspace_id, session_id);
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    let mut f = match std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(&path)
    {
        Ok(f) => f,
        Err(_) => return,
    };
    for k in keys {
        if k.trim().is_empty() {
            continue;
        }
        let _ = writeln!(f, "{}", k.trim());
    }
}

// ============================================================================
// Transcript parsing
// ============================================================================

/// A parsed turn from the transcript (user prompt + assistant response)
struct ParsedTurn {
    user_text: String,
    assistant_text: String,
    usage: Option<UsageEvent>,
    touched_paths: Vec<String>,
    user_uuid: Option<String>,
    assistant_uuid: Option<String>,
    timestamp_ms: i64,
}

fn turn_key(turn: &ParsedTurn) -> Option<String> {
    let u = turn.user_uuid.as_deref()?.trim();
    if u.is_empty() {
        return None;
    }
    let a = turn.assistant_uuid.as_deref().unwrap_or("").trim();
    Some(format!("{u}:{a}"))
}

/// Build a `claude+jsonl://` URI for a parsed transcript turn.
/// Returns None when we have neither a usable path nor a user UUID.
/// See `internal/SOURCE_POINTERS.md` for the scheme registry.
fn build_claude_source_pointer(transcript_path: &Path, turn: &ParsedTurn) -> Option<String> {
    let path_str = transcript_path.to_str()?;
    if path_str.is_empty() {
        return None;
    }
    let uuid = turn
        .user_uuid
        .as_deref()
        .map(str::trim)
        .filter(|s| !s.is_empty());
    match uuid {
        Some(u) => Some(format!("claude+jsonl://{path_str}#turn={u}")),
        None => Some(format!("claude+jsonl://{path_str}")),
    }
}

fn truncate_for_recording(mut s: String, max_bytes: usize) -> String {
    if s.len() <= max_bytes {
        return s;
    }
    // Best-effort truncation without worrying about UTF-8 boundaries too much;
    // Claude transcripts are typically UTF-8 and mostly ASCII.
    s.truncate(max_bytes);
    s.push_str("\n...(truncated)");
    s
}

fn value_to_compact_text(v: &serde_json::Value, max_bytes: usize) -> String {
    match v {
        serde_json::Value::String(s) => truncate_for_recording(s.clone(), max_bytes),
        serde_json::Value::Array(a) => {
            // Some tool results are arrays of text blocks.
            let mut out = String::new();
            for item in a {
                if let Some(obj) = item.as_object() {
                    if obj.get("type").and_then(|t| t.as_str()) == Some("text") {
                        if let Some(t) = obj.get("text").and_then(|t| t.as_str()) {
                            if !out.is_empty() {
                                out.push('\n');
                            }
                            out.push_str(t);
                        }
                    }
                }
            }
            if out.trim().is_empty() {
                truncate_for_recording(v.to_string(), max_bytes)
            } else {
                truncate_for_recording(out, max_bytes)
            }
        }
        _ => truncate_for_recording(v.to_string(), max_bytes),
    }
}

fn extract_tool_result_blocks_text(content: &serde_json::Value) -> Option<String> {
    // Claude logs represent tool results as a `user` message whose content includes
    // blocks of type `tool_result`.
    let serde_json::Value::Array(blocks) = content else {
        return None;
    };

    let mut parts: Vec<String> = Vec::new();
    for block in blocks {
        let Some(obj) = block.as_object() else {
            continue;
        };
        if obj.get("type").and_then(|t| t.as_str()) != Some("tool_result") {
            continue;
        }

        let is_error = obj
            .get("is_error")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        let tool_use_id = obj
            .get("tool_use_id")
            .and_then(|v| v.as_str())
            .unwrap_or("");
        let raw_content = obj.get("content").unwrap_or(&serde_json::Value::Null);
        let text = value_to_compact_text(raw_content, 12 * 1024);
        if text.trim().is_empty() {
            continue;
        }

        let mut s = String::new();
        if is_error {
            s.push_str("Tool result (error)");
        } else {
            s.push_str("Tool result");
        }
        if !tool_use_id.is_empty() {
            s.push_str(" ");
            s.push_str(tool_use_id);
        }
        s.push_str(":\n");
        s.push_str(text.trim());
        parts.push(s);
    }

    if parts.is_empty() {
        None
    } else {
        Some(parts.join("\n\n"))
    }
}

fn normalize_touched_path(p: &str, cwd: &Path) -> Option<String> {
    let mut s = p.trim();
    if s.is_empty() {
        return None;
    }

    // Normalize slashes.
    let owned;
    if s.contains('\\') {
        owned = s.replace('\\', "/");
        s = &owned;
    }

    // Strip leading ./
    let s = s.strip_prefix("./").unwrap_or(s);

    // If absolute and under cwd, strip cwd prefix.
    if let Ok(abs) = std::path::Path::new(s).canonicalize() {
        if let Ok(cwd_abs) = cwd.canonicalize() {
            if let Ok(rel) = abs.strip_prefix(&cwd_abs) {
                let rel = rel.to_string_lossy().replace('\\', "/");
                let rel = rel.trim_start_matches('/').to_string();
                if !rel.is_empty() {
                    return Some(rel);
                }
            }
        }
    }

    Some(s.trim_start_matches('/').to_string())
}

fn looks_like_path(s: &str) -> bool {
    // Heuristic: avoid grabbing arbitrary prose.
    // Accept common repo-ish patterns.
    let s = s.trim();
    if s.is_empty() {
        return false;
    }
    if s.len() > 260 {
        return false;
    }
    if s.contains('\n') || s.contains('\r') {
        return false;
    }
    if s.contains(' ') {
        return false;
    }
    if !(s.contains('/') || s.contains('\\')) {
        return false;
    }
    true
}

fn collect_touched_paths_from_value(v: &serde_json::Value, cwd: &Path, out: &mut HashSet<String>) {
    match v {
        serde_json::Value::String(s) => {
            if looks_like_path(s) {
                if let Some(p) = normalize_touched_path(s, cwd) {
                    out.insert(p);
                }
            }
        }
        serde_json::Value::Array(a) => {
            for x in a {
                collect_touched_paths_from_value(x, cwd, out);
            }
        }
        serde_json::Value::Object(m) => {
            // Prefer keys commonly used for file paths.
            for k in [
                "path",
                "file",
                "file_path",
                "filepath",
                "filename",
                "target",
                "target_file",
            ] {
                if let Some(val) = m.get(k) {
                    collect_touched_paths_from_value(val, cwd, out);
                }
            }
            // Also walk everything; snapshots vary.
            for (_k, val) in m.iter() {
                collect_touched_paths_from_value(val, cwd, out);
            }
        }
        _ => {}
    }
}

fn extract_touched_paths_from_content(content: &serde_json::Value, cwd: &Path) -> Vec<String> {
    let mut out: HashSet<String> = HashSet::new();

    // Content may be an array of blocks.
    if let serde_json::Value::Array(blocks) = content {
        for block in blocks {
            if let Some(obj) = block.as_object() {
                let ty = obj.get("type").and_then(|t| t.as_str()).unwrap_or("");
                if ty == "tool_use" || ty == "tool_result" {
                    if let Some(v) = obj.get("input") {
                        collect_touched_paths_from_value(v, cwd, &mut out);
                    }
                    if let Some(v) = obj.get("result") {
                        collect_touched_paths_from_value(v, cwd, &mut out);
                    }
                }
            }
        }
    }

    let mut v = out.into_iter().collect::<Vec<_>>();
    v.sort();
    v.truncate(64);
    v
}

/// Extract text content from a message's content field
fn extract_text_content(content: &serde_json::Value) -> String {
    match content {
        // Simple string content
        serde_json::Value::String(s) => s.clone(),
        // Array of content blocks
        serde_json::Value::Array(blocks) => {
            let mut texts = Vec::new();
            for block in blocks {
                if let Some(obj) = block.as_object() {
                    // Only extract "text" type blocks, ignore tool_use, tool_result, thinking
                    if obj.get("type").and_then(|t| t.as_str()) == Some("text") {
                        if let Some(text) = obj.get("text").and_then(|t| t.as_str()) {
                            texts.push(text.to_string());
                        }
                    }
                }
            }
            texts.join("\n")
        }
        _ => String::new(),
    }
}

/// Check if a user message is a tool_result (which we skip)
fn is_tool_result_message(content: &serde_json::Value) -> bool {
    if let serde_json::Value::Array(blocks) = content {
        for block in blocks {
            if let Some(obj) = block.as_object() {
                if obj.get("type").and_then(|t| t.as_str()) == Some("tool_result") {
                    return true;
                }
            }
        }
    }
    false
}

/// Parse transcript from cursor position, extract new turns
fn parse_transcript_from_cursor(
    transcript_path: &Path,
    cursor: &CursorState,
    cwd: &Path,
) -> anyhow::Result<(Vec<ParsedTurn>, CursorState)> {
    let file = std::fs::File::open(transcript_path)?;
    let metadata = file.metadata()?;
    let file_size = metadata.len();

    // If file is smaller than cursor, it was truncated/rotated - start from beginning
    let start_offset = if cursor.byte_offset > file_size {
        0
    } else {
        cursor.byte_offset
    };

    let mut reader = std::io::BufReader::new(file);
    reader.seek_relative(start_offset as i64)?;

    let mut turns = Vec::new();
    let mut current_user_text: Option<String> = None;
    let mut current_user_uuid: Option<String> = None;
    let mut current_assistant_text = String::new();
    let mut current_last_assistant_uuid: Option<String> = None;
    let mut current_timestamp_ms = 0i64;

    // Best-effort: collect touched paths during a turn.
    let mut pending_touched: HashSet<String> = HashSet::new();

    let mut current_usage: Option<UsageEvent> = None;
    let mut last_uuid: Option<String> = cursor.last_uuid.clone();
    let mut new_offset = start_offset;
    // Cursor uses byte_offset as the primary mechanism. Only use last_uuid as a
    // resync aid when we start from the beginning (e.g. file truncation/rotation).
    let mut seen_last_uuid = start_offset > 0 || cursor.last_uuid.is_none();

    for line in reader.lines() {
        let line = line?;
        new_offset += line.len() as u64 + 1; // +1 for newline

        if line.trim().is_empty() {
            continue;
        }

        let parsed: TranscriptLine = match serde_json::from_str(&line) {
            Ok(p) => p,
            Err(_) => continue,
        };

        // Extract timestamp if available
        if let Some(ts) = parsed.extra.get("time").and_then(|v| v.as_i64()) {
            current_timestamp_ms = ts;
        }

        // Best-effort: some tool outputs include structured fields outside `message.content`
        // (e.g. `toolUseResult.filePath`). Collect any path-like strings.
        for (_k, v) in parsed.extra.iter() {
            collect_touched_paths_from_value(v, cwd, &mut pending_touched);
        }

        // Skip non-message lines (file-history-snapshot, etc.)
        let line_type = match &parsed.line_type {
            Some(t) => t.as_str(),
            None => continue,
        };

        if line_type == "file-history-snapshot" {
            for (_k, v) in parsed.extra.iter() {
                collect_touched_paths_from_value(v, cwd, &mut pending_touched);
            }
            if let Some(uuid) = &parsed.uuid {
                last_uuid = Some(uuid.clone());
            }
            continue;
        }

        // Skip sidechains (prompt suggestions, background agents)
        if parsed.is_sidechain == Some(true) {
            continue;
        }

        // Skip until we pass the last processed UUID
        if let Some(ref last) = cursor.last_uuid {
            if !seen_last_uuid {
                if parsed.uuid.as_ref() == Some(last) {
                    seen_last_uuid = true;
                }
                continue;
            }
        }

        let message = match &parsed.message {
            Some(m) => m,
            None => continue,
        };

        let role = match &message.role {
            Some(r) => r.as_str(),
            None => continue,
        };

        let content = match &message.content {
            Some(c) => c,
            None => continue,
        };

        for p in extract_touched_paths_from_content(content, cwd) {
            pending_touched.insert(p);
        }

        match (line_type, role) {
            ("user", "user") => {
                // Tool results are logged as `user` messages; keep them as part of the
                // assistant-side context so we don't lose error details.
                if is_tool_result_message(content) {
                    if current_user_text.is_some() {
                        if let Some(tool_text) = extract_tool_result_blocks_text(content) {
                            if !current_assistant_text.is_empty() {
                                current_assistant_text.push('\n');
                                current_assistant_text.push('\n');
                            }
                            current_assistant_text.push_str(&tool_text);
                        }
                    }
                    if let Some(uuid) = &parsed.uuid {
                        last_uuid = Some(uuid.clone());
                    }
                    continue;
                }

                let text = extract_text_content(content);
                if text.trim().is_empty() {
                    continue;
                }

                // If we have a pending turn, save it
                if let Some(user_text) = current_user_text.take() {
                    // Record even if assistant text is empty; user-only turns are
                    // valuable for friction detection and future context.
                    turns.push(ParsedTurn {
                        user_text,
                        assistant_text: std::mem::take(&mut current_assistant_text),
                        usage: current_usage.take(),
                        touched_paths: pending_touched.drain().collect(),
                        user_uuid: current_user_uuid.take(),
                        assistant_uuid: current_last_assistant_uuid.take(),
                        timestamp_ms: current_timestamp_ms,
                    });
                }

                current_user_text = Some(text);
                current_user_uuid = parsed.uuid.clone();
                current_assistant_text.clear();
                current_last_assistant_uuid = None;
                current_usage = None;
                // Don't reset current_timestamp_ms here, keep the last seen one for the turn
            }
            ("assistant", "assistant") => {
                let text = extract_text_content(content);
                if !text.trim().is_empty() {
                    if !current_assistant_text.is_empty() {
                        current_assistant_text.push('\n');
                    }
                    current_assistant_text.push_str(&text);
                }

                if parsed.uuid.is_some() {
                    current_last_assistant_uuid = parsed.uuid.clone();
                }

                // Capture usage from assistant messages
                if let Some(usage) = &message.usage {
                    current_usage = Some(UsageEvent {
                        provider_id: Some("anthropic".to_string()),
                        model_id: message.model.clone(),
                        cost: None,
                        tokens_input: usage.input_tokens,
                        tokens_output: usage.output_tokens,
                        tokens_reasoning: None,
                        tokens_cache_read: usage.cache_read_input_tokens,
                        tokens_cache_write: usage.cache_creation_input_tokens,
                    });
                }
            }
            _ => {}
        }

        if let Some(uuid) = &parsed.uuid {
            last_uuid = Some(uuid.clone());
        }
    }

    // Don't forget the last pending turn
    if let Some(user_text) = current_user_text {
        turns.push(ParsedTurn {
            user_text,
            assistant_text: current_assistant_text,
            usage: current_usage,
            touched_paths: pending_touched.drain().collect(),
            user_uuid: current_user_uuid,
            assistant_uuid: current_last_assistant_uuid,
            timestamp_ms: current_timestamp_ms,
        });
    }

    let new_cursor = CursorState {
        byte_offset: new_offset,
        last_uuid,
    };

    Ok((turns, new_cursor))
}

// ============================================================================
// Main entry point
// ============================================================================

pub async fn run(embed_model: String, embed_cache_dir: Option<String>) -> anyhow::Result<()> {
    // Read hook input from stdin
    let mut input = String::new();
    std::io::stdin().read_to_string(&mut input)?;

    let hook_input: HookInput = serde_json::from_str(&input)?;

    tracing::info!(
        hook_event = %hook_input.hook_event_name,
        session_id = %hook_input.session_id,
        cwd = %hook_input.cwd,
        "claude hook invoked"
    );

    let config = FlowConfig {
        embed_model: embed_model.clone(),
        embed_cache_dir: embed_cache_dir.clone(),
        extraction_mode: crate::types::ExtractionMode::Hybrid, // hooks are for live, so default to hybrid
    };
    let mut flow = Flow::new(config);

    // Track whether we need to drain (only for Stop hook which records turns)
    let needs_drain = hook_input.hook_event_name == "Stop";

    match hook_input.hook_event_name.as_str() {
        "UserPromptSubmit" => {
            handle_user_prompt_submit(&mut flow, &hook_input).await?;
        }
        "Stop" => {
            handle_stop(
                &mut flow,
                &hook_input,
                &embed_model,
                embed_cache_dir.as_deref(),
            )
            .await?;
        }
        _ => {
            tracing::debug!(event = %hook_input.hook_event_name, "ignoring unhandled hook event");
        }
    }

    // For Stop hook, drain the background worker to ensure capsules are persisted
    // before the process exits. This is safe because Stop hook runs with async:true.
    if needs_drain {
        flow.drain().await;
    }

    Ok(())
}

async fn handle_user_prompt_submit(flow: &mut Flow, input: &HookInput) -> anyhow::Result<()> {
    let prompt = match &input.prompt {
        Some(p) => p,
        None => {
            tracing::warn!("UserPromptSubmit missing prompt field");
            return Ok(());
        }
    };

    let event = CheckEvent {
        directory: input.cwd.clone(),
        text: prompt.clone(),
        agent_kind: AgentKind::Claude,
        agent_session_id: Some(input.session_id.clone()),
    };

    let result = flow.check_turn(event).await;

    // Output hook response
    let output = if let Some(note) = result.note {
        HookOutput {
            hook_specific_output: Some(HookSpecificOutput {
                hook_event_name: "UserPromptSubmit".to_string(),
                additional_context: Some(note),
            }),
        }
    } else {
        HookOutput {
            hook_specific_output: None,
        }
    };

    let json = serde_json::to_string(&output)?;
    println!("{}", json);
    std::io::stdout().flush()?;

    Ok(())
}

async fn handle_stop(
    flow: &mut Flow,
    input: &HookInput,
    embed_model: &str,
    embed_cache_dir: Option<&str>,
) -> anyhow::Result<()> {
    let transcript_path = match &input.transcript_path {
        Some(p) => PathBuf::from(p),
        None => {
            tracing::warn!("Stop hook missing transcript_path");
            return Ok(());
        }
    };

    if !transcript_path.exists() {
        tracing::warn!(path = %transcript_path.display(), "transcript file not found");
        return Ok(());
    }

    // Get workspace for this cwd
    let cwd_path = std::path::Path::new(&input.cwd);
    let ws = match get_or_create_workspace_paths(cwd_path) {
        Ok(w) => w,
        Err(e) => {
            tracing::warn!(error = %e, "failed to get workspace paths");
            return Ok(());
        }
    };

    // Load cursor and parse new turns
    let cursor = load_cursor(&ws.id, &input.session_id);

    // If byte_offset == 0 and last_uuid is None, this is the first Stop hook call
    // for this session_id — a new session has started. Spawn a background checkpoint
    // for any previous sessions in this workspace (we don't know which session was
    // last, so we create an unscoped workspace checkpoint).
    if cursor.byte_offset == 0 && cursor.last_uuid.is_none() {
        let ws_clone = ws.clone();
        tokio::spawn(async move {
            match crate::storage_checkpoint::maybe_create_checkpoint(
                &ws_clone,
                None, // unscoped: covers all recent unchecked capsules
                "new_session_claude",
                None,
            )
            .await
            {
                Err(e) => tracing::debug!("checkpoint generation failed (non-fatal): {e}"),
                Ok(Err(reason)) => tracing::debug!("checkpoint skipped: {reason:?}"),
                Ok(Ok(_)) => {}
            }
        });
    }

    let cwd_path = std::path::Path::new(&input.cwd);
    let (turns, new_cursor) = parse_transcript_from_cursor(&transcript_path, &cursor, cwd_path)?;

    tracing::info!(
        turns_count = turns.len(),
        old_offset = cursor.byte_offset,
        new_offset = new_cursor.byte_offset,
        "parsed transcript"
    );

    // Record each turn (best-effort de-dupe by transcript UUID keys).
    // Also collect all touched paths across this batch so we can trigger
    // incremental ingestion of CHANGELOG.md and git tags below.
    let mut seen = load_turnkeys(&ws.id, &input.session_id);
    let mut new_keys: Vec<String> = Vec::new();
    let mut all_touched: Vec<String> = Vec::new();
    // Collect assistant texts to detect PR creation after the turns loop.
    let mut all_assistant_texts: Vec<String> = Vec::new();

    for turn in turns {
        all_touched.extend(turn.touched_paths.iter().cloned());
        all_assistant_texts.push(turn.assistant_text.clone());

        if let Some(k) = turn_key(&turn) {
            if seen.contains(&k) {
                continue;
            }
            seen.insert(k.clone());
            new_keys.push(k);
        }
        let source_pointer = build_claude_source_pointer(&transcript_path, &turn);
        let event = RecordTurnEvent {
            directory: input.cwd.clone(),
            user_text: turn.user_text,
            assistant_text: turn.assistant_text,
            touched_paths: turn.touched_paths,
            tool_calls: vec![],
            agent_kind: AgentKind::Claude,
            agent_session_id: Some(input.session_id.clone()),
            usage: turn.usage,
            grounding_note: None,
            source_ts_ms: None,
            source_pointer,
        };

        let result = flow.record_turn(event).await;
        if let Some(error) = result.error {
            tracing::warn!(error = %error, "record_turn failed");
        }
    }

    append_turnkeys(&ws.id, &input.session_id, &new_keys);

    // Save cursor
    save_cursor(&ws.id, &input.session_id, &new_cursor);

    // Stealth PR comment: scan all assistant texts for a GitHub PR URL created this batch.
    // If found, spawn `unlost pr-comment` in the background without blocking the Stop hook.
    {
        let combined = all_assistant_texts.join("\n");
        if let Some(pr_url) =
            crate::companion::shims::opencode_stdio::extract_github_pr_url_pub(&combined)
        {
            let session_id = input.session_id.clone();
            let directory = input.cwd.clone();
            let em = embed_model.to_string();
            let ec = embed_cache_dir.map(|s| s.to_string());
            tokio::spawn(async move {
                tracing::info!(pr_url, "claude: detected PR creation — posting unlost comment");
                tokio::time::sleep(std::time::Duration::from_secs(3)).await;
                if let Err(e) = crate::companion::shims::opencode_stdio::spawn_pr_comment_pub(
                    &pr_url,
                    Some(session_id.as_str()),
                    &directory,
                    &em,
                    ec.as_deref(),
                )
                .await
                {
                    tracing::warn!(error = ?e, pr_url, "claude: pr-comment spawn failed");
                }
            });
        }
    }

    // ── Incremental ingest: changelog and git tags ────────────────────────────
    // Run after every Stop hook so that:
    //   • If CHANGELOG.md was edited this session, new versions are captured
    //     immediately rather than waiting for the next `unlost replay`.
    //   • Any new git tags pushed/created this session are ingested as boundary
    //     capsules so future queries can reconstruct session ranges.
    // Both operations are idempotent (skip already-ingested items) and zero-LLM,
    // so the cost is negligible when there is nothing new.
    if let Some(repo_root) = crate::workspace::git_toplevel(cwd_path) {
        // Only bother loading the embedder if there might be something new.
        let changelog_path = repo_root.join("CHANGELOG.md");
        let changelog_touched = all_touched
            .iter()
            .any(|p| p.ends_with("CHANGELOG.md") || p == "CHANGELOG.md");
        // For tags: always check — tags are created with `git tag`, not file edits.
        // The check is O(n_tags) file-read, fast even with hundreds of tags.
        let embedder = crate::embed::load_embedder(
            embed_model,
            embed_cache_dir.map(std::path::PathBuf::from),
            false,
        )
        .await;
        if let Ok(ref embedder) = embedder {
            let use_color = std::io::IsTerminal::is_terminal(&std::io::stdout())
                && std::env::var_os("NO_COLOR").is_none();
            // Always check for new tags — lightweight operation.
            let _ = crate::git::ingest_git_tags(&ws, &repo_root, embedder, use_color).await;
            // Only re-read and re-parse the changelog when it was actually touched,
            // or when there are versions not yet ingested (first run after editing).
            if changelog_touched || changelog_path.exists() {
                let _ =
                    crate::changelog::ingest_changelog(&ws, &changelog_path, embedder, use_color)
                        .await;
            }
        }
    }

    Ok(())
}

// ============================================================================
// Cost warning helper
// ============================================================================

/// Models known to be expensive (reasoning models, large frontier models).
fn is_expensive_model(model: &str) -> bool {
    let m = model.to_lowercase();
    // OpenAI expensive models
    if m.contains("o1") || m.contains("o3") || m.contains("gpt-4o") && !m.contains("mini") {
        return true;
    }
    // Anthropic expensive models
    if m.contains("opus") || (m.contains("sonnet") && !m.contains("3-5") && !m.contains("3.5")) {
        return true;
    }
    false
}

/// Suggest a cheaper alternative model for the same provider.
fn suggest_cheaper_model(
    provider: &str,
    current_model: &str,
) -> Option<(&'static str, &'static str)> {
    let m = current_model.to_lowercase();

    match provider {
        "openai" => {
            if m.contains("o1") || m.contains("o3") || (m.contains("gpt-4") && !m.contains("mini"))
            {
                return Some((
                    "gpt-4o-mini",
                    "unlost config llm openai --model gpt-4o-mini",
                ));
            }
        }
        "anthropic" => {
            if m.contains("opus") || (m.contains("sonnet") && !m.contains("haiku")) {
                return Some((
                    "claude-3-5-haiku-20241022",
                    "unlost config llm anthropic --model claude-3-5-haiku-20241022",
                ));
            }
        }
        _ => {}
    }
    None
}

/// Print a cost warning before replay starts.
fn print_cost_warning(turn_count: usize, mode: crate::types::ExtractionMode, use_color: bool) {
    use crate::config::LlmConfig;
    use crate::workspace::load_workspace_config;

    let cfg = load_workspace_config();

    let (provider, model) = match &cfg.llm {
        Some(LlmConfig::Openai { model, .. }) => ("openai", model.as_str()),
        Some(LlmConfig::Anthropic { model, .. }) => ("anthropic", model.as_str()),
        Some(LlmConfig::Ollama { model, .. }) => ("ollama", model.as_str()),
        Some(LlmConfig::Custom { model, .. }) => ("custom", model.as_str()),
        None => {
            if use_color {
                println!("\x1b[33m!\x1b[0m No LLM configured. Run: unlost config llm --help");
            } else {
                println!("! No LLM configured. Run: unlost config llm --help");
            }
            return;
        }
    };

    // Always show what we're about to do
    if use_color {
        println!(
            "\x1b[36mi\x1b[0m Replaying ~{} turns using \x1b[1m{}/{}\x1b[0m",
            turn_count, provider, model
        );
        match mode {
            crate::types::ExtractionMode::Hybrid => {
                println!(
                    "  \x1b[34m*\x1b[0m Hybrid Mode: Local search indexing for all turns; LLM analysis only for pivotal moments."
                );
            }
            crate::types::ExtractionMode::Full => {
                println!(
                    "  \x1b[33m!\x1b[0m Full Extraction: Extracting every turn (highest quality, highest cost)."
                );
            }
            _ => {}
        }
    } else {
        println!(
            "Replaying ~{} turns using {}/{}",
            turn_count, provider, model
        );
        match mode {
            crate::types::ExtractionMode::Hybrid => {
                println!(
                    "  * Hybrid Mode: Local search indexing for all turns; LLM analysis only for pivotal moments."
                );
            }
            crate::types::ExtractionMode::Full => {
                println!(
                    "  ! Full Extraction: Extracting every turn (highest quality, highest cost)."
                );
            }
            _ => {}
        }
    }

    // Warn about expensive models and suggest alternatives
    if is_expensive_model(model) {
        if let Some((suggested, cmd)) = suggest_cheaper_model(provider, model) {
            if use_color {
                println!(
                    "\x1b[33m!\x1b[0m This model may be expensive for bulk replay. Consider using \x1b[1m{}\x1b[0m:",
                    suggested
                );
                println!("  {}", cmd);
            } else {
                println!(
                    "! This model may be expensive for bulk replay. Consider using {}:",
                    suggested
                );
                println!("  {}", cmd);
            }
        }
    }

    println!();
}

/// Quick turn count from a transcript file (without full parsing).
fn count_turns_in_transcript(path: &Path) -> usize {
    use std::io::BufRead;

    let file = match std::fs::File::open(path) {
        Ok(f) => f,
        Err(_) => return 0,
    };

    let reader = std::io::BufReader::new(file);
    let mut user_count = 0;

    for line in reader.lines() {
        let line = match line {
            Ok(l) => l,
            Err(_) => continue,
        };
        // Quick heuristic: count lines with "type":"user" and "role":"user"
        if line.contains("\"type\":\"user\"") && line.contains("\"role\":\"user\"") {
            user_count += 1;
        }
    }

    user_count
}

pub async fn replay(
    path: String,
    transcript_path: String,
    session_id: Option<String>,
    from_start: bool,
    dedupe: bool,
    clear: bool,
    extraction_mode: crate::types::ExtractionMode,
    embed_model: String,
    embed_cache_dir: Option<String>,
    git_grounding: bool,
) -> anyhow::Result<()> {
    let dir_path = Path::new(&path);
    let transcript_path = PathBuf::from(transcript_path);

    let ws = get_or_create_workspace_paths(dir_path)?;

    if clear {
        clear_replay_data(&ws, "claude", session_id.as_deref())?;
    }

    let repo_root = crate::workspace::git_toplevel(dir_path);
    let use_color = std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none();

    // Count turns for cost warning
    let turn_count = count_turns_in_transcript(&transcript_path);
    if turn_count > 0 && extraction_mode != crate::types::ExtractionMode::None {
        print_cost_warning(turn_count, extraction_mode, use_color);
    }

    // Collect transcript files to process
    let transcript_files: Vec<PathBuf> = if transcript_path.is_file() {
        vec![transcript_path.to_path_buf()]
    } else {
        // Directory: collect all .jsonl files
        let mut files: Vec<PathBuf> = std::fs::read_dir(&transcript_path)?
            .filter_map(|entry| entry.ok())
            .filter(|entry| {
                entry
                    .path()
                    .extension()
                    .map(|ext| ext == "jsonl")
                    .unwrap_or(false)
            })
            .map(|entry| entry.path())
            .collect();
        files.sort();
        if files.is_empty() {
            anyhow::bail!(
                "No .jsonl files found in directory {}",
                transcript_path.display()
            );
        }
        files
    };

    // If directory provided without explicit session_id, we need to process each file with its own session
    let multiple_sessions = transcript_path.is_dir() && session_id.is_none();

    let use_color = std::io::stdout().is_terminal() && std::env::var_os("NO_COLOR").is_none();

    // Count total turns across all files for cost warning
    let total_turns: usize = transcript_files
        .iter()
        .map(|f| count_turns_in_transcript(f))
        .sum();
    if total_turns > 0 && extraction_mode != crate::types::ExtractionMode::None {
        print_cost_warning(total_turns, extraction_mode, use_color);
    }

    // Spinner + progress
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
    let files_done = Arc::new(AtomicU64::new(0));
    let done = Arc::new(AtomicBool::new(false));

    let spinner = if use_color {
        use indicatif::{ProgressBar, ProgressStyle};
        let pb = ProgressBar::new_spinner();
        pb.set_style(
            ProgressStyle::with_template("{spinner:.cyan} {msg}")
                .unwrap()
                .tick_chars("⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏"),
        );
        Some(pb)
    } else {
        None
    };

    let total_files = transcript_files.len() as u64;
    let spinner_task = if let Some(ref pb) = spinner {
        let pb = pb.clone();
        let files_done = files_done.clone();
        let done = done.clone();
        Some(tokio::spawn(async move {
            while !done.load(Ordering::Relaxed) {
                tokio::time::sleep(tokio::time::Duration::from_millis(80)).await;
                let done_count = files_done.load(Ordering::Relaxed);
                pb.set_message(format!(
                    "Replaying {} sessions ({} done)",
                    total_files, done_count
                ));
                pb.tick();
            }
        }))
    } else {
        None
    };

    // Process files in parallel (bounded concurrency)
    use futures_util::future::join_all;

    let max_concurrency = std::thread::available_parallelism()
        .map(|n| n.get())
        .unwrap_or(4)
        .min(8);
    let semaphore = Arc::new(tokio::sync::Semaphore::new(max_concurrency));

    let mut handles = Vec::new();

    for file_path in transcript_files.clone() {
        let ws_id = ws.id.clone();
        let dir_path = dir_path.to_path_buf();
        let path = path.clone();
        let session_id = session_id.clone();
        let transcript_path = transcript_path.clone();
        let embed_model = embed_model.clone();
        let embed_cache_dir = embed_cache_dir.clone();
        let files_done = files_done.clone();
        let semaphore = semaphore.clone();
        let repo_root_clone = repo_root.clone();

        let handle = tokio::spawn(async move {
            let _permit = semaphore
                .acquire_owned()
                .await
                .map_err(|_| anyhow::anyhow!("replay semaphore closed"))?;

            let config = FlowConfig {
                embed_model,
                embed_cache_dir,
                extraction_mode,
            };
            let mut flow = Flow::new(config);

            let sid = if multiple_sessions {
                // Derive session_id from filename for directory mode
                file_path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .map(|s| s.to_string())
                    .ok_or_else(|| {
                        anyhow::anyhow!(
                            "could not infer session_id from filename: {}",
                            file_path.display()
                        )
                    })?
            } else if let Some(ref s) = session_id {
                s.clone()
            } else {
                transcript_path
                    .file_stem()
                    .and_then(|s| s.to_str())
                    .map(|s| s.to_string())
                    .ok_or_else(|| {
                        anyhow::anyhow!(
                            "missing --session-id and could not infer from transcript filename"
                        )
                    })?
            };

            let cursor = if from_start {
                CursorState {
                    byte_offset: 0,
                    last_uuid: None,
                }
            } else {
                load_cursor(&ws_id, &sid)
            };

            let (turns, new_cursor) = parse_transcript_from_cursor(&file_path, &cursor, &dir_path)?;

            // If git grounding is enabled, fetch commits for the session range
            let commits = if git_grounding {
                if let Some(ref root) = repo_root_clone {
                    let min_ts = turns
                        .iter()
                        .filter(|t| t.timestamp_ms > 0)
                        .map(|t| t.timestamp_ms)
                        .min()
                        .unwrap_or(0);
                    let max_ts = turns
                        .iter()
                        .filter(|t| t.timestamp_ms > 0)
                        .map(|t| t.timestamp_ms)
                        .max()
                        .unwrap_or(0);

                    if min_ts > 0 {
                        // Look up to 15 minutes after the last turn
                        crate::git::get_commits_for_range(root, min_ts, max_ts + 15 * 60 * 1000)
                            .ok()
                    } else {
                        None
                    }
                } else {
                    None
                }
            } else {
                None
            };

            let mut recorded = 0usize;

            let mut seen = if dedupe {
                load_turnkeys(&ws_id, &sid)
            } else {
                HashSet::new()
            };
            let mut new_keys: Vec<String> = Vec::new();

            for turn in turns {
                if dedupe {
                    if let Some(k) = turn_key(&turn) {
                        if seen.contains(&k) {
                            continue;
                        }
                        seen.insert(k.clone());
                        new_keys.push(k);
                    }
                }

                // Grounding from git logs
                let git_note = if let Some(ref available) = commits {
                    if turn.timestamp_ms > 0 {
                        let matches = crate::git::find_corresponding_commits(
                            turn.timestamp_ms,
                            &turn.touched_paths,
                            available,
                            5 * 60 * 1000, // 5 minute window
                        );
                        if !matches.is_empty() {
                            let hashes: Vec<_> = matches.iter().map(|c| &c.hash[..7]).collect();
                            Some(format!("Verified via git: {}", hashes.join(", ")))
                        } else {
                            None
                        }
                    } else {
                        None
                    }
                } else {
                    None
                };

                // Grounding from unfault-core semantics of touched files
                let sem_note = if !turn.touched_paths.is_empty() {
                    crate::workspace::semantic_grounding_for_paths(
                        std::path::Path::new(&path),
                        &turn.touched_paths,
                    )
                } else {
                    None
                };

                let grounding_note = match (git_note, sem_note) {
                    (Some(g), Some(s)) => Some(format!("{} | {}", g, s)),
                    (Some(g), None) => Some(g),
                    (None, Some(s)) => Some(s),
                    (None, None) => None,
                };

                let source_pointer = build_claude_source_pointer(&file_path, &turn);
                let event = RecordTurnEvent {
                    directory: path.clone(),
                    user_text: turn.user_text,
                    assistant_text: turn.assistant_text,
                    touched_paths: turn.touched_paths,
                    tool_calls: vec![],
                    agent_kind: AgentKind::Claude,
                    agent_session_id: Some(sid.clone()),
                    usage: turn.usage,
                    grounding_note,
                    source_ts_ms: if turn.timestamp_ms > 0 {
                        Some(turn.timestamp_ms)
                    } else {
                        None
                    },
                    source_pointer,
                };
                let result = flow.record_turn(event).await;
                if result.error.is_none() {
                    recorded += 1;
                }
            }

            // Wait for background flush jobs before marking session done
            flow.drain().await;

            if dedupe {
                append_turnkeys(&ws_id, &sid, &new_keys);
            }
            save_cursor(&ws_id, &sid, &new_cursor);

            files_done.fetch_add(1, Ordering::Relaxed);

            Ok::<_, anyhow::Error>((file_path, recorded, flow.llm_calls().await))
        });

        handles.push(handle);
    }

    // Wait for all tasks to complete
    let results = join_all(handles).await;

    done.store(true, Ordering::Relaxed);

    if let Some(task) = spinner_task {
        let _ = task.await;
    }
    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }

    // Aggregate results
    let mut grand_recorded = 0usize;
    let mut total_llm_calls = 0u64;

    for result in results {
        match result {
            Ok(Ok((_file_path, recorded, llm_calls))) => {
                grand_recorded += recorded;
                total_llm_calls += llm_calls;
            }
            Ok(Err(e)) => {
                eprintln!("✗ Error processing file: {}", e);
            }
            Err(e) => {
                eprintln!("✗ Task panicked: {}", e);
            }
        }
    }

    println!();
    if use_color {
        print!(
            "\x1b[1;32m✓\x1b[0m Replay complete: \x1b[1;36m{}\x1b[0m turns indexed locally",
            grand_recorded
        );
        if total_llm_calls > 0 && grand_recorded > 0 {
            let pct = (total_llm_calls as f64 / grand_recorded as f64) * 100.0;
            let saved = 100.0 - pct;
            if saved > 0.1 {
                print!(
                    ". The LLM was only needed for \x1b[1;33m{}\x1b[0m pivotal moments (saving \x1b[1;36m{:.1}%\x1b[0m in API calls)",
                    total_llm_calls, saved
                );
            } else {
                print!(
                    ". The LLM analyzed \x1b[1;33m{}\x1b[0m pivotal moments",
                    total_llm_calls
                );
            }
        }
        println!();
    } else {
        print!("Replay complete: {} turns indexed locally", grand_recorded);
        if total_llm_calls > 0 && grand_recorded > 0 {
            let pct = (total_llm_calls as f64 / grand_recorded as f64) * 100.0;
            let saved = 100.0 - pct;
            if saved > 0.1 {
                print!(
                    ". The LLM was only needed for {} pivotal moments (saving {:.1}% in API calls)",
                    total_llm_calls, saved
                );
            } else {
                print!(". The LLM analyzed {} pivotal moments", total_llm_calls);
            }
        }
        println!();
    }

    // Always ingest git history and CHANGELOG.md after replaying agent sessions —
    // zero extra cost, deduplicates on re-run, and makes `unlost brief` significantly
    // more useful by grounding it with shipped-decision history.
    if let Some(ref root) = repo_root {
        let embedder = crate::embed::load_embedder(
            &embed_model,
            embed_cache_dir.as_deref().map(std::path::PathBuf::from),
            false,
        )
        .await;
        if let Ok(embedder) = embedder {
            let _ = crate::git::ingest_git_commits(&ws, root, &embedder, 500, use_color).await;
            let _ = crate::git::ingest_git_tags(&ws, root, &embedder, use_color).await;
            let changelog_path = root.join("CHANGELOG.md");
            let _ = crate::changelog::ingest_changelog(&ws, &changelog_path, &embedder, use_color)
                .await;
        }
    }

    Ok(())
}

fn clear_replay_data(
    ws: &crate::WorkspacePaths,
    _kind: &str,
    _session_id: Option<&str>,
) -> anyhow::Result<()> {
    println!("Clearing existing replay data for workspace: {}", ws.id);

    // 1. Clear LanceDB
    if ws.db_dir.exists() {
        std::fs::remove_dir_all(&ws.db_dir)?;
    }

    // 2. Clear JSONL
    if ws.capsules_jsonl.exists() {
        std::fs::remove_file(&ws.capsules_jsonl)?;
    }

    // 3. Clear replayed trackers (cursors and turnkeys)
    let claude_dir = crate::workspace::unlost_workspace_dir(&ws.id).join("claude");
    if claude_dir.exists() {
        std::fs::remove_dir_all(&claude_dir)?;
    }
    let legacy_dir = crate::workspace::unlost_workspace_dir(&ws.id).join("claudecode");
    if legacy_dir.exists() {
        std::fs::remove_dir_all(&legacy_dir)?;
    }

    Ok(())
}

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

    fn mk_user(uuid: &str, text: &str) -> String {
        format!(
            r#"{{"type":"user","isSidechain":false,"uuid":"{uuid}","message":{{"role":"user","content":[{{"type":"text","text":{text_json}}}]}}}}"#,
            uuid = uuid,
            text_json = serde_json::to_string(text).unwrap()
        )
    }

    fn mk_assistant(uuid: &str, text: &str) -> String {
        format!(
            r#"{{"type":"assistant","isSidechain":false,"uuid":"{uuid}","message":{{"role":"assistant","model":"claude","content":[{{"type":"text","text":{text_json}}}]}}}}"#,
            uuid = uuid,
            text_json = serde_json::to_string(text).unwrap()
        )
    }

    fn mk_tool_result(uuid: &str, tool_use_id: &str, content: &str, is_error: bool) -> String {
        format!(
            r#"{{"type":"user","isSidechain":false,"uuid":"{uuid}","message":{{"role":"user","content":[{{"type":"tool_result","tool_use_id":{tool_use_id_json},"content":{content_json},"is_error":{is_error}}}]}}}}"#,
            uuid = uuid,
            tool_use_id_json = serde_json::to_string(tool_use_id).unwrap(),
            content_json = serde_json::to_string(content).unwrap(),
            is_error = if is_error { "true" } else { "false" }
        )
    }

    #[test]
    fn test_parse_resumes_from_byte_offset_without_uuid_gate() {
        let td = tempfile::tempdir().unwrap();
        let p = td.path().join("t.jsonl");

        {
            let mut f = std::fs::File::create(&p).unwrap();
            writeln!(f, "{}", mk_user("u1", "hello")).unwrap();
            writeln!(f, "{}", mk_assistant("a1", "hi")).unwrap();
        }

        let cwd = td.path();
        let (t1, c1) = parse_transcript_from_cursor(&p, &CursorState::default(), cwd).unwrap();
        assert_eq!(t1.len(), 1);
        assert_eq!(t1[0].user_text, "hello");
        assert_eq!(t1[0].assistant_text, "hi");

        {
            let mut f = std::fs::OpenOptions::new().append(true).open(&p).unwrap();
            writeln!(f, "{}", mk_user("u2", "next")).unwrap();
            writeln!(f, "{}", mk_assistant("a2", "ok")).unwrap();
        }

        let (t2, _c2) = parse_transcript_from_cursor(&p, &c1, cwd).unwrap();
        assert_eq!(t2.len(), 1);
        assert_eq!(t2[0].user_text, "next");
        assert_eq!(t2[0].assistant_text, "ok");
    }

    #[test]
    fn test_tool_result_is_preserved_in_assistant_context() {
        let td = tempfile::tempdir().unwrap();
        let p = td.path().join("t.jsonl");

        {
            let mut f = std::fs::File::create(&p).unwrap();
            writeln!(f, "{}", mk_user("u1", "run tests")).unwrap();
            writeln!(
                f,
                "{}",
                mk_tool_result("tr1", "toolu_1", "Exit code 127\\npython: not found", true)
            )
            .unwrap();
            writeln!(f, "{}", mk_assistant("a1", "switch to python3")).unwrap();
        }

        let cwd = td.path();
        let (t, _c) = parse_transcript_from_cursor(&p, &CursorState::default(), cwd).unwrap();
        assert_eq!(t.len(), 1);
        assert_eq!(t[0].user_text, "run tests");
        assert!(t[0].assistant_text.contains("Tool result (error)"));
        assert!(t[0].assistant_text.contains("Exit code 127"));
        assert!(t[0].assistant_text.contains("switch to python3"));
    }

    #[test]
    fn test_user_only_turn_is_recorded() {
        let td = tempfile::tempdir().unwrap();
        let p = td.path().join("t.jsonl");

        {
            let mut f = std::fs::File::create(&p).unwrap();
            writeln!(f, "{}", mk_user("u1", "i'm not happy")).unwrap();
        }

        let cwd = td.path();
        let (t, _c) = parse_transcript_from_cursor(&p, &CursorState::default(), cwd).unwrap();
        assert_eq!(t.len(), 1);
        assert_eq!(t[0].user_text, "i'm not happy");
        assert!(t[0].assistant_text.trim().is_empty());
    }
}