unlost 0.15.0

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
use crate::analysis::{AnalysisMeta, AnalysisMsg};
use crate::embed::Embedder;
use crate::emotion::{
    EmotionModel, apply_context_heuristics, extract_user_and_assistant_text, map_go_emotions,
};
use crate::storage::ensure_capsules_table;
use bytes::Bytes;
use kanal::AsyncReceiver;
use lancedb::connection::Connection;
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use std::io::Write;
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::{Duration, Instant};

struct RecentJobDeduper {
    ttl: std::time::Duration,
    max_entries: usize,
    seen: HashMap<String, std::time::Instant>,
}

impl RecentJobDeduper {
    fn new(ttl: std::time::Duration, max_entries: usize) -> Self {
        Self {
            ttl,
            max_entries,
            seen: HashMap::new(),
        }
    }

    fn should_skip_and_mark(&mut self, key: String) -> bool {
        let now = std::time::Instant::now();
        self.prune(now);

        if let Some(prev) = self.seen.get(&key) {
            if now.duration_since(*prev) <= self.ttl {
                // Refresh the timestamp so rapid repeated duplicates remain suppressed.
                self.seen.insert(key, now);
                return true;
            }
        }

        self.seen.insert(key, now);
        false
    }

    fn prune(&mut self, now: std::time::Instant) {
        self.seen.retain(|_, t| now.duration_since(*t) <= self.ttl);
        if self.seen.len() <= self.max_entries {
            return;
        }

        // Keep the most recent max_entries.
        let mut items = self
            .seen
            .iter()
            .map(|(k, v)| (k.clone(), *v))
            .collect::<Vec<_>>();
        items.sort_by(|a, b| b.1.cmp(&a.1));
        items.truncate(self.max_entries);
        self.seen = items.into_iter().collect();
    }
}

fn flush_job_dedupe_key(job: &FlushJob) -> String {
    let mut h = Sha256::new();
    h.update(job.workspace_id.as_bytes());
    h.update(&[0u8]);
    h.update(job.conn_id.to_le_bytes());
    h.update(&[0u8]);
    h.update(job.meta.upstream_host.as_bytes());
    h.update(&[0u8]);
    h.update(job.meta.request_path.as_bytes());
    h.update(&[0u8]);
    h.update(
        job.meta
            .agent_session_id
            .as_deref()
            .unwrap_or("")
            .as_bytes(),
    );
    h.update(&[0u8]);
    h.update(job.input.as_bytes());
    format!(
        "{}|{}|{}|{}|{}",
        job.workspace_id,
        job.conn_id,
        job.meta.request_path,
        job.meta.agent_session_id.as_deref().unwrap_or(""),
        hex::encode(h.finalize())
    )
}

pub(crate) fn append_capsule_jsonl(
    path: &std::path::Path,
    ts_ms: i64,
    conn_id: u64,
    exchange_seq: u64,
    meta: &crate::ResponseMeta,
    capsule: &crate::IntentCapsule,
    head_sha: Option<&str>,
    commit_sha: Option<&str>,
) -> anyhow::Result<()> {
    if let Some(parent) = path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    let usage = meta.usage.as_ref().map(|u| {
        serde_json::json!({
            "provider_id": u.provider_id,
            "model_id": u.model_id,
            "cost": u.cost,
            "tokens": {
                "input": u.tokens_input,
                "output": u.tokens_output,
                "reasoning": u.tokens_reasoning,
                "cache": {
                    "read": u.tokens_cache_read,
                    "write": u.tokens_cache_write,
                }
            }
        })
    });

    let record = serde_json::json!({
        "ts_ms": ts_ms,
        "conn_id": conn_id,
        "exchange_seq": exchange_seq,
        "source": meta.source,
        "upstream_host": meta.upstream_host,
        "request_path": meta.request_path,
        "http_status": meta.http_status,
        "agent_session_id": meta.agent_session_id,
        "source_pointer": meta.source_pointer,
        "usage": usage,
        "capsule": capsule,
        "head_sha": head_sha,
        "commit_sha": commit_sha,
    });
    std::fs::OpenOptions::new()
        .create(true)
        .append(true)
        .open(path)?
        .write_all(format!("{}\n", record).as_bytes())?;
    Ok(())
}

fn contains_hex_hash(s: &str) -> bool {
    for tok in s
        .split(|c: char| !c.is_ascii_alphanumeric())
        .filter(|t| !t.is_empty())
    {
        if tok.len() < 7 || tok.len() > 40 {
            continue;
        }
        if tok.chars().all(|c: char| c.is_ascii_hexdigit()) {
            return true;
        }
    }
    false
}

/// Read the short (7-char) git HEAD SHA from the given directory.
/// Returns None if not a git repo, no commits yet, or git is unavailable.
pub(crate) fn read_git_head(workspace_root: &std::path::Path) -> Option<String> {
    if workspace_root.as_os_str().is_empty() {
        return None;
    }
    let out = std::process::Command::new("git")
        .args(["-C", workspace_root.to_string_lossy().as_ref(), "rev-parse", "--short", "HEAD"])
        .output()
        .ok()?;
    if out.status.success() {
        let sha = String::from_utf8_lossy(&out.stdout).trim().to_string();
        if sha.is_empty() { None } else { Some(sha) }
    } else {
        None
    }
}

pub(crate) fn looks_like_commit_or_pr(s: &str) -> bool {
    let s = s.to_ascii_lowercase();
    if s.contains("git commit") || s.contains("commit:") || s.contains("commit ") {
        return true;
    }
    if s.contains("pull request") || s.contains("merge request") {
        return true;
    }
    if s.contains("pr #") || s.contains("pr#") {
        return true;
    }

    // cheap hash heuristic (7+ hex) preceded by common tokens
    if s.contains("sha") || s.contains("hash") || s.contains("commit") {
        if contains_hex_hash(&s) {
            return true;
        }
    }
    false
}

#[derive(Debug, Clone)]
pub(crate) struct ChunkInput {
    pub(crate) conn_id: u64,
    pub(crate) upstream_host: String,
    pub(crate) request_path: String,
    pub(crate) http_status: u16,
    pub(crate) exchange_text: String,
    pub(crate) commit_mentioned: bool,
    /// Agent session ID (e.g., OpenCode session) for grouping conversations
    pub(crate) agent_session_id: Option<String>,
    /// Optional opaque URI pointing back to the source-of-truth for this turn
    /// (e.g. `claude+jsonl://...#L47`). Rendered by unlost; resolved by the agent.
    pub(crate) source_pointer: Option<String>,
    /// Best-effort usage metrics (tokens/cost). Not always present.
    pub(crate) usage: Option<crate::types::UsageMeta>,
    /// Optional grounding info (e.g. verified git commits)
    pub(crate) grounding_note: Option<String>,
    /// Original message timestamp (ms since epoch) from replay source.
    /// When set, overrides the wall-clock time so replayed capsules sort correctly.
    pub(crate) source_ts_ms: Option<i64>,
    /// Absolute path to the workspace root (git toplevel or directory).
    pub(crate) workspace_root: std::path::PathBuf,
    /// Governor channels snapshot captured in `check_turn` for this turn.
    /// Carries (smoothed_channels, intensity, state) into the background flush worker.
    pub(crate) turn_eval_channels: Option<(crate::types::SymptomChannels, f32, crate::types::TrajectoryState)>,
}

#[derive(Debug, Clone)]
pub(crate) struct FlushJob {
    pub(crate) workspace_id: String,
    /// Absolute path to the workspace root (git toplevel or directory).
    /// Used for git SHA capture at flush time.
    pub(crate) workspace_root: std::path::PathBuf,
    pub(crate) conn_id: u64,
    pub(crate) exchange_seq: u64,
    pub(crate) ts_ms: i64,
    pub(crate) meta: crate::ResponseMeta,
    pub(crate) input: String,
    pub(crate) grounding_note: Option<String>,
    /// Decision text from the previous capsule in this workspace's sequence.
    /// Used to encode causal continuity into the embedding.
    pub(crate) prior_decision: Option<String>,
    /// git HEAD SHA when this buffer chunk opened (short, 7-char).
    /// Always populated when inside a git repo with at least one commit.
    pub(crate) head_sha: Option<String>,
    /// git HEAD SHA after the commit that landed during this turn, if any.
    /// Sparse: only set when `commit_mentioned` triggered a flush and HEAD moved.
    pub(crate) commit_sha: Option<String>,
    /// Governor channels snapshot for TurnEval construction in the background worker.
    pub(crate) turn_eval_channels: Option<(crate::types::SymptomChannels, f32, crate::types::TrajectoryState)>,
}

struct WorkspaceBuffer {
    next_seq: u64,
    last_activity: Instant,
    last_conn_id: u64,
    last_upstream_host: String,
    last_request_path: String,
    last_http_status: u16,
    last_agent_session_id: Option<String>,
    last_usage: Option<crate::types::UsageMeta>,
    last_source_pointer: Option<String>,
    last_grounding_note: Option<String>,
    last_source_ts_ms: Option<i64>,
    total_chars: usize,
    turns: Vec<String>,
    saw_commit: bool,
    /// Decision from the most recently flushed capsule in this workspace.
    /// Threaded into the next FlushJob so embeddings encode causal continuity.
    last_decision: Option<String>,
    /// Absolute path to the workspace root, carried from ChunkInput.
    workspace_root: std::path::PathBuf,
    /// git HEAD SHA captured when this buffer was first opened (start of chunk).
    head_sha_at_open: Option<String>,
    /// Governor channels for the most recent turn ingested into this buffer.
    /// Carried into FlushJob so the background worker can build TurnEval.
    last_turn_eval_channels: Option<(crate::types::SymptomChannels, f32, crate::types::TrajectoryState)>,
}

impl WorkspaceBuffer {
    fn new(now: Instant, workspace_root: std::path::PathBuf) -> Self {
        let head_sha_at_open = read_git_head(&workspace_root);
        Self {
            next_seq: 0,
            last_activity: now,
            last_conn_id: 0,
            last_upstream_host: String::new(),
            last_request_path: String::new(),
            last_http_status: 0,
            last_agent_session_id: None,
            last_usage: None,
            last_source_pointer: None,
            last_grounding_note: None,
            last_source_ts_ms: None,
            total_chars: 0,
            turns: Vec::new(),
            saw_commit: false,
            last_decision: None,
            workspace_root,
            head_sha_at_open,
            last_turn_eval_channels: None,
        }
    }
}

#[derive(Clone)]
pub(crate) struct WorkspaceChunker {
    buffers: Arc<Mutex<HashMap<String, WorkspaceBuffer>>>,
    flush_tx: kanal::AsyncSender<FlushJob>,
}

impl WorkspaceChunker {
    const IDLE_FLUSH_AFTER: Duration = Duration::from_secs(2);
    const MAX_TOTAL_CHARS: usize = 16 * 1024;
    const MAX_TURNS: usize = 8;

    pub(crate) fn new(flush_tx: kanal::AsyncSender<FlushJob>) -> Self {
        Self {
            buffers: Arc::new(Mutex::new(HashMap::new())),
            flush_tx,
        }
    }

    /// Close the sender so the background worker exits its receive loop after
    /// draining any already-queued jobs.
    pub(crate) fn close_sender(&mut self) {
        // kanal senders are closed when all sender handles are dropped.
        // Replacing with a fresh closed channel achieves that without unsafe code.
        let (tx, _rx) = kanal::bounded_async::<FlushJob>(0);
        drop(std::mem::replace(&mut self.flush_tx, tx));
    }

    pub(crate) async fn ingest(&self, workspace_id: String, item: ChunkInput) {
        let now = Instant::now();
        let mut maybe_flush: Option<FlushJob> = None;

        {
            let mut map = self.buffers.lock().await;
            let workspace_root = item.workspace_root.clone();
            let buf = map
                .entry(workspace_id.clone())
                .or_insert_with(|| WorkspaceBuffer::new(now, workspace_root.clone()));

            // If this buffer has been idle, flush it before appending new content.
            if !buf.turns.is_empty()
                && now.duration_since(buf.last_activity) >= Self::IDLE_FLUSH_AFTER
            {
                maybe_flush = Some(build_flush_job(workspace_id.clone(), buf));
                *buf = WorkspaceBuffer::new(now, workspace_root.clone());
            }

            buf.last_activity = now;
            buf.last_conn_id = item.conn_id;
            buf.last_upstream_host = item.upstream_host;
            buf.last_request_path = item.request_path;
            buf.last_http_status = item.http_status;
            buf.last_agent_session_id = item.agent_session_id;
            buf.last_usage = item.usage;
            // Sticky: only overwrite when a non-None pointer arrives, so a single
            // chunk that spans multiple turns retains the earliest pointer.
            if item.source_pointer.is_some() {
                buf.last_source_pointer = item.source_pointer;
            }
            buf.last_grounding_note = item.grounding_note;
            if item.source_ts_ms.is_some() {
                buf.last_source_ts_ms = item.source_ts_ms;
            }
            buf.saw_commit |= item.commit_mentioned;
            if item.turn_eval_channels.is_some() {
                buf.last_turn_eval_channels = item.turn_eval_channels;
            }

            buf.total_chars = buf.total_chars.saturating_add(item.exchange_text.len());
            buf.turns.push(item.exchange_text);

            // Force flush boundaries.
            let too_big = buf.total_chars >= Self::MAX_TOTAL_CHARS;
            let too_many = buf.turns.len() >= Self::MAX_TURNS;
            let milestone = item.commit_mentioned;
            if too_big || too_many || milestone {
                if maybe_flush.is_none() {
                    maybe_flush = Some(build_flush_job(workspace_id.clone(), buf));
                    *buf = WorkspaceBuffer::new(now, workspace_root.clone());
                }
            }
        }

        if let Some(job) = maybe_flush {
            let _ = self.flush_tx.send(job).await;
        }
    }

    pub(crate) async fn flush_idle(&self) {
        let now = Instant::now();
        let mut jobs: Vec<FlushJob> = Vec::new();

        {
            let mut map = self.buffers.lock().await;
            for (ws_id, buf) in map.iter_mut() {
                if buf.turns.is_empty() {
                    continue;
                }
                if now.duration_since(buf.last_activity) < Self::IDLE_FLUSH_AFTER {
                    continue;
                }
                let root = buf.workspace_root.clone();
                jobs.push(build_flush_job(ws_id.clone(), buf));
                *buf = WorkspaceBuffer::new(now, root);
            }
        }

        for j in jobs {
            let _ = self.flush_tx.send(j).await;
        }
    }

    /// Force-flush the buffer for a single workspace, regardless of idle time.
    pub(crate) async fn flush_workspace(&self, workspace_id: &str) {
        let now = Instant::now();
        let mut job: Option<FlushJob> = None;

        {
            let mut map = self.buffers.lock().await;
            if let Some(buf) = map.get_mut(workspace_id) {
                if !buf.turns.is_empty() {
                    let root = buf.workspace_root.clone();
                    job = Some(build_flush_job(workspace_id.to_string(), buf));
                    *buf = WorkspaceBuffer::new(now, root);
                }
            }
        }

        if let Some(j) = job {
            let _ = self.flush_tx.send(j).await;
        }
    }

    /// Force-flush all buffers for all workspaces.
    pub(crate) async fn flush_all(&self) {
        let now = Instant::now();
        let mut jobs: Vec<FlushJob> = Vec::new();

        {
            let mut map = self.buffers.lock().await;
            for (ws_id, buf) in map.iter_mut() {
                if !buf.turns.is_empty() {
                    let root = buf.workspace_root.clone();
                    jobs.push(build_flush_job(ws_id.clone(), buf));
                    *buf = WorkspaceBuffer::new(now, root);
                }
            }
        }

        for j in jobs {
            let _ = self.flush_tx.send(j).await;
        }
    }
}

fn build_flush_job(workspace_id: String, buf: &mut WorkspaceBuffer) -> FlushJob {
    buf.next_seq += 1;
    let exchange_seq = buf.next_seq;
    let ts_ms = buf.last_source_ts_ms.unwrap_or_else(crate::now_ms);

    // Determine commit_sha: if a commit was mentioned this chunk, compare HEAD now
    // to HEAD at buffer-open time. If they differ, a commit landed during this turn.
    let commit_sha = if buf.saw_commit {
        let head_now = read_git_head(&buf.workspace_root);
        match (&buf.head_sha_at_open, &head_now) {
            (Some(open), Some(now)) if open != now => Some(now.clone()),
            _ => None,
        }
    } else {
        None
    };
    // head_sha is what HEAD was when this buffer chunk opened.
    let head_sha = buf.head_sha_at_open.clone();

    let mut input = String::new();
    input.push_str("Signals:\n");
    input.push_str(&format!("commit_mentioned={}\n\n", buf.saw_commit));
    input.push_str("Conversation slice (newest last):\n\n");
    for (i, t) in buf.turns.iter().enumerate() {
        input.push_str(&format!("Turn {}:\n", i + 1));
        input.push_str(t);
        input.push_str("\n\n");
    }

    let meta = crate::ResponseMeta {
        source: "record".to_string(),
        upstream_host: buf.last_upstream_host.clone(),
        request_path: buf.last_request_path.clone(),
        http_status: buf.last_http_status,
        agent_session_id: buf.last_agent_session_id.clone(),
        source_pointer: buf.last_source_pointer.clone(),
        usage: buf.last_usage.clone(),
    };

    FlushJob {
        workspace_id,
        workspace_root: buf.workspace_root.clone(),
        conn_id: buf.last_conn_id,
        exchange_seq,
        ts_ms,
        meta,
        input,
        grounding_note: buf.last_grounding_note.clone(),
        prior_decision: buf.last_decision.clone(),
        head_sha,
        commit_sha,
        turn_eval_channels: buf.last_turn_eval_channels.take(),
    }
}

#[derive(Clone)]
pub(crate) struct ServeState {
    pub(crate) embedder: Embedder,
    db_cache: Arc<Mutex<HashMap<String, Connection>>>,
    pub(crate) emotion: Arc<std::sync::Mutex<EmotionModel>>,
}

impl ServeState {
    pub(crate) fn new(embedder: Embedder, emotion: EmotionModel) -> Self {
        Self {
            embedder,
            db_cache: Arc::new(Mutex::new(HashMap::new())),
            emotion: Arc::new(std::sync::Mutex::new(emotion)),
        }
    }

    pub(crate) fn workspace_paths(&self, workspace_id: &str) -> crate::WorkspacePaths {
        let ws_dir = crate::unlost_workspace_dir(workspace_id);
        crate::WorkspacePaths {
            id: workspace_id.to_string(),
            // root is not available in serve mode without a path lookup;
            // callers that need it (SHA capture) use ws_root_for_id instead.
            root: std::path::PathBuf::new(),
            db_dir: ws_dir.join("lancedb"),
            capsules_jsonl: ws_dir.join("capsules.jsonl"),
            metrics_jsonl: ws_dir.join("metrics.jsonl"),
        }
    }

    pub(crate) async fn db_for(&self, workspace_id: &str) -> anyhow::Result<Connection> {
        {
            let cache = self.db_cache.lock().await;
            if let Some(c) = cache.get(workspace_id) {
                return Ok(c.clone());
            }
        }

        let ws = self.workspace_paths(workspace_id);
        std::fs::create_dir_all(&ws.db_dir)?;
        let db = lancedb::connect(ws.db_dir.to_string_lossy().as_ref())
            .execute()
            .await?;
        let _ = ensure_capsules_table(&db).await?;

        let mut cache = self.db_cache.lock().await;
        cache.insert(workspace_id.to_string(), db.clone());
        Ok(db)
    }

    pub(crate) async fn get_recent_capsules(
        &self,
        workspace_id: &str,
        limit: usize,
    ) -> anyhow::Result<Vec<crate::CapsuleHit>> {
        let ws = self.workspace_paths(workspace_id);
        crate::storage::scan_capsules_lancedb(&ws, limit, None, None, None, None, None).await
    }
}

pub(crate) async fn process_flush_jobs_serve(rx: AsyncReceiver<FlushJob>, state: ServeState) {
    const PREAMBLE: &str = "You are unlost. Extract a short, high-signal intent capsule from this multi-turn conversation slice.\n\
 Return JSON with fields: {category, intent, decision, rationale, next_steps (array), symbols (array), failure_mode, failure_signals, questions (array)}.\n\n\
 Rules:\n\
 - Do NOT include quotes or excerpts from the conversation. No evidence snippets.\n\
 - Keep it grounded in what happened: intent, decisions, rationale, and what's next.\n\
 - Keep each field concise; next_steps max 3.\n\
 - symbols: identifiers, file paths, endpoints, commit/PR refs if explicitly mentioned.\n\
 - questions: 2-3 natural language questions a developer would ask to find this capsule later (e.g. \"Why is the timeout 30s?\"). Specific and grounded.\n\n\
 Failure mode detection: none, drift, rediscovery, decision_conflict, retry_spiral, false_progress, unbounded_horizon.";

    let mut deduper = RecentJobDeduper::new(std::time::Duration::from_secs(45), 2048);

    loop {
        let job = match rx.recv().await {
            Ok(j) => j,
            Err(_) => break,
        };

        let dedupe_key = flush_job_dedupe_key(&job);
        if deduper.should_skip_and_mark(dedupe_key) {
            continue;
        }

        let (user_text, assistant_text) = extract_user_and_assistant_text(&job.input);

        let emotion_handle = state.emotion.clone();
        let user_text_clone = user_text.clone();
        let user_emotion = tokio::task::spawn_blocking(move || {
            let mut model = emotion_handle.lock().ok()?;
            if user_text_clone.trim().is_empty() {
                return None;
            }
            let (raw, score) = model.classify_one(&user_text_clone).ok()?;
            let meta = map_go_emotions(&raw, score);
            Some(apply_context_heuristics(&user_text_clone, meta))
        })
        .await
        .ok()
        .flatten();

        let emotion_handle = state.emotion.clone();
        let assistant_text_clone = assistant_text.clone();
        let assistant_emotion = tokio::task::spawn_blocking(move || {
            let mut model = emotion_handle.lock().ok()?;
            if assistant_text_clone.trim().is_empty() {
                return None;
            }
            let (raw, score) = model.classify_one(&assistant_text_clone).ok()?;
            Some(map_go_emotions(&raw, score))
        })
        .await
        .ok()
        .flatten();

        let symbols = crate::net::extract_symbols_from_text(&job.input);
        let user_symbols = crate::net::extract_symbols_from_text(&user_text);
        let failure_mode = crate::governor::detect_failure_keywords(&job.input)
            .unwrap_or(crate::types::FailureMode::None);

        let mut capsule =
            match crate::llm_extract::<crate::IntentCapsule>(None, PREAMBLE, &job.input).await {
                Ok(mut c) => {
                    for s in symbols {
                        if !c.symbols.contains(&s) {
                            c.symbols.push(s);
                        }
                    }
                    c.user_symbols = user_symbols;
                    c
                }
                Err(_) => crate::IntentCapsule {
                    category: "unknown".to_string(),
                    intent: user_text.lines().next().unwrap_or("").to_string(),
                    decision: assistant_text.lines().next().unwrap_or("").to_string(),
                    rationale: String::new(),
                    next_steps: vec![],
                    symbols,
                    user_symbols,
                    failure_mode,
                    failure_signals: Some("Heuristic extraction (LLM failed)".to_string()),
                    extraction_mode: crate::types::ExtractionMode::None,
                    questions: vec![],
                },
            };

        crate::util::augment_capsule_symbols_from_input(&mut capsule, &job.input);

        let ws_paths = state.workspace_paths(&job.workspace_id);
        let _ = append_capsule_jsonl(
            &ws_paths.capsules_jsonl,
            job.ts_ms,
            job.conn_id,
            job.exchange_seq,
            &job.meta,
            &capsule,
            job.head_sha.as_deref(),
            job.commit_sha.as_deref(),
        );
        let _ = crate::metrics::record_capsule_saved(
            &ws_paths,
            job.ts_ms,
            job.conn_id,
            job.exchange_seq,
            &job.meta,
            user_emotion.as_ref(),
            assistant_emotion.as_ref(),
            &capsule,
            &crate::types::TurnEval::default(),
        );

        if let Ok(db) = state.db_for(&job.workspace_id).await {
            let _ = crate::storage::insert_capsule_row(
                &db,
                &state.embedder,
                job.conn_id,
                job.exchange_seq,
                job.ts_ms,
                &job.meta,
                user_emotion.as_ref(),
                assistant_emotion.as_ref(),
                &capsule,
                &crate::types::TurnEval::default(),
                job.prior_decision.as_deref(),
                job.head_sha.as_deref(),
                job.commit_sha.as_deref(),
            )
            .await;
        }
    }
}

pub(crate) async fn process_flush_jobs_proxy(
    rx: AsyncReceiver<FlushJob>,
    ws: crate::WorkspacePaths,
    db: Connection,
    embedder: Embedder,
    emotion: Arc<std::sync::Mutex<EmotionModel>>,
) {
    const PREAMBLE: &str = "You are unlost. Extract a short, high-signal intent capsule from this multi-turn conversation slice.\n\
 Return JSON with fields: {category, intent, decision, rationale, next_steps (array), symbols (array), failure_mode, failure_signals, questions (array)}.\n\
 questions: 2-3 natural language questions a developer would ask to find this capsule later.\n\n\
 Failure mode detection: none, drift, rediscovery, decision_conflict, retry_spiral, false_progress, unbounded_horizon.";

    let mut deduper = RecentJobDeduper::new(std::time::Duration::from_secs(45), 2048);

    loop {
        let job = match rx.recv().await {
            Ok(j) => j,
            Err(_) => break,
        };

        let dedupe_key = flush_job_dedupe_key(&job);
        if deduper.should_skip_and_mark(dedupe_key) {
            continue;
        }

        let (user_text, assistant_text) = extract_user_and_assistant_text(&job.input);

        let emotion_handle = emotion.clone();
        let user_text_clone = user_text.clone();
        let user_emotion = tokio::task::spawn_blocking(move || {
            let mut model = emotion_handle.lock().ok()?;
            if user_text_clone.trim().is_empty() {
                return None;
            }
            let (raw, score) = model.classify_one(&user_text_clone).ok()?;
            let meta = map_go_emotions(&raw, score);
            Some(apply_context_heuristics(&user_text_clone, meta))
        })
        .await
        .ok()
        .flatten();

        let emotion_handle = emotion.clone();
        let assistant_text_clone = assistant_text.clone();
        let assistant_emotion = tokio::task::spawn_blocking(move || {
            let mut model = emotion_handle.lock().ok()?;
            if assistant_text_clone.trim().is_empty() {
                return None;
            }
            let (raw, score) = model.classify_one(&assistant_text_clone).ok()?;
            Some(map_go_emotions(&raw, score))
        })
        .await
        .ok()
        .flatten();

        let symbols = crate::net::extract_symbols_from_text(&job.input);
        let user_symbols = crate::net::extract_symbols_from_text(&user_text);
        let failure_mode = crate::governor::detect_failure_keywords(&job.input)
            .unwrap_or(crate::types::FailureMode::None);

        let mut capsule =
            match crate::llm_extract::<crate::IntentCapsule>(None, PREAMBLE, &job.input).await {
                Ok(mut c) => {
                    for s in symbols {
                        if !c.symbols.contains(&s) {
                            c.symbols.push(s);
                        }
                    }
                    c.user_symbols = user_symbols;
                    c
                }
                Err(_) => crate::IntentCapsule {
                    category: "unknown".to_string(),
                    intent: user_text.lines().next().unwrap_or("").to_string(),
                    decision: assistant_text.lines().next().unwrap_or("").to_string(),
                    rationale: String::new(),
                    next_steps: vec![],
                    symbols,
                    user_symbols,
                    failure_mode,
                    failure_signals: Some("Heuristic extraction (LLM failed)".to_string()),
                    extraction_mode: crate::types::ExtractionMode::None,
                    questions: vec![],
                },
            };

        crate::util::augment_capsule_symbols_from_input(&mut capsule, &job.input);

        let _ = append_capsule_jsonl(
            &ws.capsules_jsonl,
            job.ts_ms,
            job.conn_id,
            job.exchange_seq,
            &job.meta,
            &capsule,
            job.head_sha.as_deref(),
            job.commit_sha.as_deref(),
        );
        let _ = crate::metrics::record_capsule_saved(
            &ws,
            job.ts_ms,
            job.conn_id,
            job.exchange_seq,
            &job.meta,
            user_emotion.as_ref(),
            assistant_emotion.as_ref(),
            &capsule,
            &crate::types::TurnEval::default(),
        );
        let _ = crate::storage::insert_capsule_row(
            &db,
            &embedder,
            job.conn_id,
            job.exchange_seq,
            job.ts_ms,
            &job.meta,
            user_emotion.as_ref(),
            assistant_emotion.as_ref(),
            &capsule,
            &crate::types::TurnEval::default(),
            job.prior_decision.as_deref(),
            job.head_sha.as_deref(),
            job.commit_sha.as_deref(),
        )
        .await;
    }
}

pub(crate) async fn analysis_worker_multiplex(
    rx: AsyncReceiver<AnalysisMsg>,
    chunker: WorkspaceChunker,
    conn_id: u64,
) {
    let mut pending_start: Option<(AnalysisMeta, Bytes)> = None;
    loop {
        let (meta, request_body) = if let Some(p) = pending_start.take() {
            p
        } else {
            match rx.recv().await {
                Ok(AnalysisMsg::ExchangeStart { meta, request_body }) => (meta, request_body),
                _ => break,
            }
        };

        let user_text = crate::net::decode_json_lossy(&request_body)
            .and_then(|v| {
                if meta.request_path.contains("/v1/messages") {
                    crate::net::extract_anthropic_user_text(&v)
                } else {
                    crate::net::extract_openai_message_text(&v)
                }
            })
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty());

        let mut assistant_text = String::new();
        let mut sse_buf: Vec<u8> = Vec::new();
        let mut raw_buf: Vec<u8> = Vec::new();
        let sse = crate::net::is_event_stream(meta.content_type.as_deref());

        loop {
            match rx.recv().await {
                Ok(AnalysisMsg::ResponseEnd) => break,
                Ok(AnalysisMsg::ExchangeStart {
                    meta: n_meta,
                    request_body: n_req,
                }) => {
                    pending_start = Some((n_meta, n_req));
                    break;
                }
                Ok(AnalysisMsg::ResponseChunk(b)) => {
                    if sse {
                        sse_buf.extend_from_slice(&b);
                        crate::net::sse_extract_deltas(&mut sse_buf, &mut assistant_text);
                    } else {
                        raw_buf.extend_from_slice(&b);
                    }
                }
                Err(_) => break,
            }
        }

        if !sse {
            if let Some(v) = crate::net::decode_json_lossy(&raw_buf) {
                assistant_text = if meta.request_path.contains("/v1/messages") {
                    crate::net::extract_anthropic_assistant_text_from_json(&v)
                } else {
                    crate::net::extract_openai_assistant_text_from_json(&v)
                }
                .unwrap_or_default();
            }
        }

        let assistant_text = assistant_text.trim().to_string();
        if user_text.is_none() && assistant_text.is_empty() {
            continue;
        }

        let mut input = String::new();
        if let Some(u) = user_text.as_deref() {
            input.push_str("User:\n");
            input.push_str(u);
            input.push_str("\n\n");
        }
        if !assistant_text.is_empty() {
            input.push_str("Assistant:\n");
            input.push_str(&assistant_text);
        }

        let item = ChunkInput {
            conn_id,
            upstream_host: meta.upstream_host.clone(),
            request_path: meta.request_path.clone(),
            http_status: meta.http_status,
            exchange_text: input.clone(),
            commit_mentioned: looks_like_commit_or_pr(&input),
            agent_session_id: None,
            source_pointer: None,
            usage: None,
            grounding_note: None,
            source_ts_ms: None,
            // Proxy mode doesn't have the workspace root readily available.
            workspace_root: std::path::PathBuf::new(),
            // Proxy mode doesn't run the governor; no channels available.
            turn_eval_channels: None,
        };
        chunker.ingest(meta.workspace_id.clone(), item).await;
    }
}

pub(crate) async fn analysis_worker(
    rx: AsyncReceiver<AnalysisMsg>,
    chunker: WorkspaceChunker,
    conn_id: u64,
) {
    let mut pending_start: Option<(AnalysisMeta, Bytes)> = None;
    loop {
        let (meta, request_body) = if let Some(p) = pending_start.take() {
            p
        } else {
            match rx.recv().await {
                Ok(AnalysisMsg::ExchangeStart { meta, request_body }) => (meta, request_body),
                _ => break,
            }
        };

        let user_text = crate::net::decode_json_lossy(&request_body)
            .and_then(|v| {
                if meta.request_path.contains("/v1/messages") {
                    crate::net::extract_anthropic_user_text(&v)
                } else {
                    crate::net::extract_openai_message_text(&v)
                }
            })
            .map(|s| s.trim().to_string())
            .filter(|s| !s.is_empty());

        let mut assistant_text = String::new();
        let mut sse_buf: Vec<u8> = Vec::new();
        let mut raw_buf: Vec<u8> = Vec::new();
        let sse = crate::net::is_event_stream(meta.content_type.as_deref());

        loop {
            match rx.recv().await {
                Ok(AnalysisMsg::ResponseEnd) => break,
                Ok(AnalysisMsg::ExchangeStart {
                    meta: n_meta,
                    request_body: n_req,
                }) => {
                    pending_start = Some((n_meta, n_req));
                    break;
                }
                Ok(AnalysisMsg::ResponseChunk(b)) => {
                    if sse {
                        sse_buf.extend_from_slice(&b);
                        crate::net::sse_extract_deltas(&mut sse_buf, &mut assistant_text);
                    } else {
                        raw_buf.extend_from_slice(&b);
                    }
                }
                Err(_) => break,
            }
        }

        if !sse {
            if let Some(v) = crate::net::decode_json_lossy(&raw_buf) {
                assistant_text = if meta.request_path.contains("/v1/messages") {
                    crate::net::extract_anthropic_assistant_text_from_json(&v)
                } else {
                    crate::net::extract_openai_assistant_text_from_json(&v)
                }
                .unwrap_or_default();
            }
        }

        let assistant_text = assistant_text.trim().to_string();
        if user_text.is_none() && assistant_text.is_empty() {
            continue;
        }

        let mut input = String::new();
        if let Some(u) = user_text.as_deref() {
            input.push_str("User:\n");
            input.push_str(u);
            input.push_str("\n\n");
        }
        if !assistant_text.is_empty() {
            input.push_str("Assistant:\n");
            input.push_str(&assistant_text);
        }

        let item = ChunkInput {
            conn_id,
            upstream_host: meta.upstream_host.clone(),
            request_path: meta.request_path.clone(),
            http_status: meta.http_status,
            exchange_text: input.clone(),
            commit_mentioned: looks_like_commit_or_pr(&input),
            agent_session_id: None,
            source_pointer: None,
            usage: None,
            grounding_note: None,
            source_ts_ms: None,
            workspace_root: std::path::PathBuf::new(),
            turn_eval_channels: None,
        };
        chunker.ingest(meta.workspace_id.clone(), item).await;
    }
}