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
//! OpenCode replay shim.
//!
//! Replays OpenCode messages from disk storage into unlost capsules.
//!
//! OpenCode stores data in `~/.local/share/opencode/storage/`:
//! - `session/<project-hash>/ses_*.json` - Session metadata with `directory` field
//! - `message/ses_*/msg_*.json` - Messages (mixed across all projects)
//!
//! Session files contain:
//! - `id`: Session ID (matches message's `sessionID`)
//! - `projectID`: Hash of the project
//! - `directory`: The workspace path
//!
//! Message files contain:
//! - `id`, `sessionID`, `role` (user/assistant)
//! - `time.created` (epoch ms)
//! - `summary.title` - A summary/title of the message (user text)
//! - `summary.diffs` - File diffs with before/after content
//! - `model.providerID`, `model.modelID`
//! - `tokens`, `cost` (for assistant messages)
//! - `parentID` (links assistant to user message)

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

// ============================================================================
// OpenCode storage types
// ============================================================================

#[derive(Debug, Deserialize)]
struct SessionFile {
    id: String,
    #[serde(rename = "projectID")]
    project_id: String,
    directory: String,
    #[allow(dead_code)]
    title: Option<String>,
}

#[derive(Debug, Deserialize)]
struct MessageFile {
    id: String,
    #[serde(rename = "sessionID")]
    #[allow(dead_code)]
    session_id: String,
    role: String,
    time: MessageTime,
    summary: Option<MessageSummary>,
    #[serde(rename = "parentID")]
    parent_id: Option<String>,
    #[serde(rename = "providerID")]
    provider_id: Option<String>,
    #[serde(rename = "modelID")]
    model_id: Option<String>,
    tokens: Option<MessageTokens>,
    cost: Option<f64>,
    #[allow(dead_code)]
    path: Option<MessagePath>,
}

#[derive(Debug, Deserialize)]
struct MessageTime {
    created: i64,
    #[allow(dead_code)]
    completed: Option<i64>,
}

#[derive(Debug, Deserialize)]
struct MessageSummary {
    title: Option<String>,
    diffs: Option<Vec<MessageDiff>>,
}

#[derive(Debug, Deserialize)]
struct MessageDiff {
    file: Option<String>,
    #[allow(dead_code)]
    before: Option<String>,
    #[allow(dead_code)]
    after: Option<String>,
    #[allow(dead_code)]
    status: Option<String>,
}

#[derive(Debug, Deserialize)]
struct MessageTokens {
    input: Option<i64>,
    output: Option<i64>,
    reasoning: Option<i64>,
    cache: Option<MessageCache>,
}

#[derive(Debug, Deserialize)]
struct MessageCache {
    read: Option<i64>,
    write: Option<i64>,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct MessagePath {
    cwd: Option<String>,
    root: Option<String>,
}

// ============================================================================
// Cursor state (persisted per workspace to track replayed messages)
// ============================================================================

pub(crate) fn replayed_path(workspace_id: &str) -> PathBuf {
    crate::workspace::unlost_workspace_dir(workspace_id)
        .join("opencode")
        .join("replayed.txt")
}

pub(crate) fn load_replayed(workspace_id: &str) -> HashSet<String> {
    let path = replayed_path(workspace_id);
    let data = match std::fs::read_to_string(&path) {
        Ok(s) => s,
        Err(_) => return HashSet::new(),
    };
    data.lines()
        .map(|l| l.trim())
        .filter(|l| !l.is_empty())
        .map(|l| l.to_string())
        .collect()
}

pub(crate) fn append_replayed(workspace_id: &str, ids: &[String]) {
    if ids.is_empty() {
        return;
    }
    let path = replayed_path(workspace_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 id in ids {
        if id.trim().is_empty() {
            continue;
        }
        let _ = writeln!(f, "{}", id.trim());
    }
}

// ============================================================================
// Storage discovery
// ============================================================================

fn xdg_data_home() -> PathBuf {
    if let Some(dir) = std::env::var_os("XDG_DATA_HOME") {
        return PathBuf::from(dir);
    }
    if let Some(home) = std::env::var_os("HOME") {
        return PathBuf::from(home).join(".local").join("share");
    }
    PathBuf::from(".")
}

fn opencode_storage_dir() -> PathBuf {
    xdg_data_home().join("opencode").join("storage")
}

/// Find all sessions that map to the given workspace directory.
fn find_sessions_for_workspace(workspace_dir: &Path) -> anyhow::Result<Vec<(String, String)>> {
    let storage = opencode_storage_dir();
    let session_root = storage.join("session");

    if !session_root.exists() {
        return Ok(Vec::new());
    }

    let workspace_canonical = workspace_dir
        .canonicalize()
        .unwrap_or_else(|_| workspace_dir.to_path_buf());
    let mut results: Vec<(String, String)> = Vec::new(); // (session_id, project_id)

    // Iterate over project hash directories
    for project_entry in std::fs::read_dir(&session_root)? {
        let project_entry = project_entry?;
        let project_path = project_entry.path();
        if !project_path.is_dir() {
            continue;
        }

        // Iterate over session files in this project
        for session_entry in std::fs::read_dir(&project_path)? {
            let session_entry = session_entry?;
            let session_file_path = session_entry.path();
            if session_file_path.extension().and_then(|e| e.to_str()) != Some("json") {
                continue;
            }

            // Parse session file
            let content = match std::fs::read_to_string(&session_file_path) {
                Ok(c) => c,
                Err(_) => continue,
            };
            let session: SessionFile = match serde_json::from_str(&content) {
                Ok(s) => s,
                Err(_) => continue,
            };

            // Check if this session's directory matches our workspace
            let session_dir = Path::new(&session.directory);
            let session_canonical = session_dir
                .canonicalize()
                .unwrap_or_else(|_| session_dir.to_path_buf());

            if session_canonical == workspace_canonical {
                results.push((session.id, session.project_id));
            }
        }
    }

    Ok(results)
}

/// Load all messages for the given session IDs.
fn load_messages_for_sessions(
    session_ids: &HashSet<String>,
) -> anyhow::Result<HashMap<String, Vec<MessageFile>>> {
    let storage = opencode_storage_dir();
    let message_root = storage.join("message");

    if !message_root.exists() {
        return Ok(HashMap::new());
    }

    let mut results: HashMap<String, Vec<MessageFile>> = HashMap::new();

    // Iterate over session directories in message/
    for entry in std::fs::read_dir(&message_root)? {
        let entry = entry?;
        let session_dir = entry.path();
        if !session_dir.is_dir() {
            continue;
        }

        // Check if this session directory matches one of our target sessions
        let dir_name = match session_dir.file_name().and_then(|n| n.to_str()) {
            Some(n) => n,
            None => continue,
        };

        if !session_ids.contains(dir_name) {
            continue;
        }

        // Load all messages in this session
        let mut messages: Vec<MessageFile> = Vec::new();
        for msg_entry in std::fs::read_dir(&session_dir)? {
            let msg_entry = msg_entry?;
            let msg_path = msg_entry.path();
            if msg_path.extension().and_then(|e| e.to_str()) != Some("json") {
                continue;
            }

            let content = match std::fs::read_to_string(&msg_path) {
                Ok(c) => c,
                Err(_) => continue,
            };
            let msg: MessageFile = match serde_json::from_str(&content) {
                Ok(m) => m,
                Err(_) => continue,
            };

            messages.push(msg);
        }

        // Sort by creation time
        messages.sort_by_key(|m| m.time.created);
        results.insert(dir_name.to_string(), messages);
    }

    Ok(results)
}

/// A parsed turn from OpenCode messages (user + assistant).
struct ParsedTurn {
    user_text: String,
    assistant_text: String,
    usage: Option<UsageEvent>,
    touched_paths: Vec<String>,
    turn_key: String, // user_msg_id:assistant_msg_id
    timestamp_ms: i64,
}

/// Build an `opencode+message://<session>/<assistant_msg_id>` URI from the
/// session id and the `<user_msg_id>:<assistant_msg_id>` turn key.
/// See `internal/SOURCE_POINTERS.md` for the scheme registry.
pub(crate) fn build_opencode_source_pointer(
    session_id: &str,
    turn_key: &str,
) -> Option<String> {
    let session = session_id.trim();
    if session.is_empty() {
        return None;
    }
    let assistant_id = turn_key.split(':').nth(1).map(str::trim).unwrap_or("");
    if assistant_id.is_empty() {
        return Some(format!("opencode+message://{session}"));
    }
    Some(format!("opencode+message://{session}/{assistant_id}"))
}

/// Read full message text from parts if available.
fn read_message_full_text(message_id: &str) -> Option<String> {
    let storage = opencode_storage_dir();
    let part_dir = storage.join("part").join(message_id);
    if !part_dir.exists() {
        return None;
    }

    let mut parts = Vec::new();
    if let Ok(entries) = std::fs::read_dir(part_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|e| e.to_str()) == Some("json") {
                if let Ok(content) = std::fs::read_to_string(path) {
                    if let Ok(v) = serde_json::from_str::<serde_json::Value>(&content) {
                        if v.get("type").and_then(|t| t.as_str()) == Some("text") {
                            let id = v.get("id").and_then(|i| i.as_str()).unwrap_or("");
                            let text = v.get("text").and_then(|t| t.as_str()).unwrap_or("");
                            if !text.is_empty() {
                                parts.push((id.to_string(), text.to_string()));
                            }
                        }
                    }
                }
            }
        }
    }

    if parts.is_empty() {
        return None;
    }

    // Sort by part ID to maintain order
    parts.sort_by(|a, b| a.0.cmp(&b.0));
    Some(
        parts
            .into_iter()
            .map(|p| p.1)
            .collect::<Vec<_>>()
            .join("\n")
            .trim()
            .to_string(),
    )
}

/// Extract turns from a list of messages.
fn extract_turns(messages: Vec<MessageFile>) -> Vec<ParsedTurn> {
    // Build a map of message_id -> message for quick lookup
    let msg_map: HashMap<String, &MessageFile> =
        messages.iter().map(|m| (m.id.clone(), m)).collect();

    let mut turns: Vec<ParsedTurn> = Vec::new();

    // Find all assistant messages and pair with their parent user message
    for msg in &messages {
        if msg.role != "assistant" {
            continue;
        }

        let parent_id = match &msg.parent_id {
            Some(p) => p,
            None => continue,
        };

        let user_msg = match msg_map.get(parent_id) {
            Some(m) => m,
            None => continue,
        };

        if user_msg.role != "user" {
            continue;
        }

        // Try reading full text from parts first, fallback to summary.title
        let user_text = read_message_full_text(&user_msg.id).unwrap_or_else(|| {
            user_msg
                .summary
                .as_ref()
                .and_then(|s| s.title.as_ref())
                .cloned()
                .unwrap_or_default()
        });

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

        let assistant_text = read_message_full_text(&msg.id).unwrap_or_else(|| {
            msg.summary
                .as_ref()
                .and_then(|s| s.title.as_ref())
                .cloned()
                .unwrap_or_default()
        });

        // Extract touched paths from diffs
        let mut touched_paths: Vec<String> = Vec::new();
        if let Some(summary) = &user_msg.summary {
            if let Some(diffs) = &summary.diffs {
                for diff in diffs {
                    if let Some(file) = &diff.file {
                        if !file.trim().is_empty() {
                            touched_paths.push(file.clone());
                        }
                    }
                }
            }
        }
        if let Some(summary) = &msg.summary {
            if let Some(diffs) = &summary.diffs {
                for diff in diffs {
                    if let Some(file) = &diff.file {
                        if !file.trim().is_empty() && !touched_paths.contains(file) {
                            touched_paths.push(file.clone());
                        }
                    }
                }
            }
        }

        // Build usage from assistant message
        let usage = msg.tokens.as_ref().map(|t| UsageEvent {
            provider_id: msg.provider_id.clone(),
            model_id: msg.model_id.clone(),
            cost: msg.cost,
            tokens_input: t.input,
            tokens_output: t.output,
            tokens_reasoning: t.reasoning,
            tokens_cache_read: t.cache.as_ref().and_then(|c| c.read),
            tokens_cache_write: t.cache.as_ref().and_then(|c| c.write),
        });

        let turn_key = format!("{}:{}", user_msg.id, msg.id);

        turns.push(ParsedTurn {
            user_text,
            assistant_text,
            usage,
            touched_paths,
            turn_key,
            timestamp_ms: msg.time.created,
        });
    }

    turns
}

// ============================================================================
// 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::workspace::load_workspace_config;

    let cfg = load_workspace_config();

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

    // Only warn for cloud providers
    if provider == "ollama" {
        return;
    }

    let is_expensive = is_expensive_model(model);

    if use_color {
        println!(
            "\x1b[34m?\x1b[0m Replaying \x1b[1m{}\x1b[0m turns using \x1b[32m{}/{}\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)."
                );
            }
            _ => {}
        }
    }

    if is_expensive {
        if use_color {
            println!(
                "\x1b[33m!\x1b[0m \x1b[1mWarning:\x1b[0m This model is expensive for bulk replay."
            );
            if let Some((suggested, cmd)) = suggest_cheaper_model(provider, model) {
                println!(
                    "  Consider using \x1b[32m{}\x1b[0m for faster, cheaper replays:",
                    suggested
                );
                println!("  \x1b[36m{}\x1b[0m", cmd);
            }
        } else {
            println!("! Warning: This model is expensive for bulk replay.");
            if let Some((suggested, cmd)) = suggest_cheaper_model(provider, model) {
                println!(
                    "  Consider using {} for faster, cheaper replays:",
                    suggested
                );
                println!("  {}", cmd);
            }
        }
    }

    println!();
}

// ============================================================================
// Replay entry point
// ============================================================================

pub async fn replay(
    path: String,
    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 ws = get_or_create_workspace_paths(dir_path)?;

    if clear {
        clear_replay_data(&ws, "opencode", None)?;
    }

    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();

    // Find all sessions for this workspace
    let sessions = find_sessions_for_workspace(dir_path)?;
    if sessions.is_empty() {
        if use_color {
            println!("\x1b[33m!\x1b[0m No OpenCode sessions found for this workspace");
        } else {
            println!("No OpenCode sessions found for this workspace");
        }
        println!("  Workspace: {}", dir_path.display());
        println!("  Storage: {}", opencode_storage_dir().display());
        return Ok(());
    }

    let session_ids: HashSet<String> = sessions.iter().map(|(s, _)| s.clone()).collect();

    // Load messages for all sessions first (to count turns for cost warning)
    let mut messages_by_session = load_messages_for_sessions(&session_ids)?;

    // Count total turns across all sessions for cost warning
    let total_turns: usize = messages_by_session
        .values()
        .map(|msgs| {
            // Count assistant messages with parent_id (each is a turn)
            msgs.iter()
                .filter(|m| m.role == "assistant" && m.parent_id.is_some())
                .count()
        })
        .sum();

    // Show cost warning before processing
    if total_turns > 0 && extraction_mode != crate::types::ExtractionMode::None {
        print_cost_warning(total_turns, extraction_mode, use_color);
    }

    // Spinner setup
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};

    let sessions_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_sessions = session_ids.len() as u64;
    let spinner_task = if let Some(ref pb) = spinner {
        let pb = pb.clone();
        let sessions_done = sessions_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 = sessions_done.load(Ordering::Relaxed);
                pb.set_message(format!(
                    "Replaying {} sessions ({} done)",
                    total_sessions, done_count
                ));
                pb.tick();
            }
        }))
    } else {
        None
    };

    // Process sessions in parallel
    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 (session_id, _project_id) in sessions {
        let messages = match messages_by_session.remove(&session_id) {
            Some(m) => m,
            None => continue,
        };

        let ws_id = ws.id.clone();
        let path = path.clone();
        let embed_model = embed_model.clone();
        let embed_cache_dir = embed_cache_dir.clone();
        let sessions_done = sessions_done.clone();
        let semaphore = semaphore.clone();
        let session_id_clone = session_id.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 turns = extract_turns(messages);

            // 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().map(|t| t.timestamp_ms).min().unwrap_or(0);
                    let max_ts = turns.iter().map(|t| t.timestamp_ms).max().unwrap_or(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
            };

            let mut recorded = 0usize;

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

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

                // Grounding from git logs
                let git_note = if let Some(ref available) = commits {
                    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
                };

                // 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_opencode_source_pointer(&session_id_clone, &turn.turn_key);
                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::OpenCode,
                    agent_session_id: Some(session_id_clone.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_replayed(&ws_id, &new_keys);
            }

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

            Ok::<_, anyhow::Error>((session_id_clone, 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((_session_id, recorded, llm_calls))) => {
                grand_recorded += recorded;
                total_llm_calls += llm_calls;
            }

            Ok(Err(e)) => {
                eprintln!("Error processing session: {}", 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
    let opencode_dir = crate::workspace::unlost_workspace_dir(&ws.id).join("opencode");
    if opencode_dir.exists() {
        std::fs::remove_dir_all(&opencode_dir)?;
    }

    Ok(())
}