claudette 0.7.0

Local-first AI personal secretary for Ollama. Telegram bot, voice, persistent scheduler, Gmail and Calendar. Single-binary Rust.
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
//! Cross-session semantic recall — long-term memory the agent can query
//! across sessions.
//!
//! Every text message (user + assistant) is embedded with `nomic-embed-text`
//! via Ollama and stored in `~/.claudette/recall.sqlite`. At query time the
//! query text is embedded the same way, and the top-k rows by cosine
//! similarity are returned. Tool calls and tool results are intentionally
//! NOT indexed (too noisy, would balloon the index size).
//!
//! Lazy install: the first call discovers whether Ollama already has the
//! embed model and pulls it (`POST /api/pull`) on miss with a status line
//! to stderr. Subsequent calls skip the probe. ~270MB / 768 dims / MIT.
//!
//! Storage caps at 50_000 rows with FIFO eviction (oldest id first).
//! ~3KB/row → ~150MB ceiling.
//!
//! The embed call is wrapped behind [`Embedder`] so the live tests can
//! inject a deterministic mock without standing up Ollama. Production code
//! goes through [`global_index`] / [`global_query`], which lazy-init a
//! process-wide [`RecallStore`] backed by either [`OllamaEmbedder`] (the
//! default) or [`OpenAICompatEmbedder`] (when `CLAUDETTE_OPENAI_COMPAT=1`,
//! e.g. against LM Studio's `/v1/embeddings`).

use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard, OnceLock};

use rusqlite::{params, Connection};
use serde_json::{json, Value};

use crate::api::{resolve_ollama_url, resolve_openai_compat};

// ────────────────────────────────────────────────────────────────────────────
// Public types
// ────────────────────────────────────────────────────────────────────────────

/// Which side of the conversation a snippet came from. The DB stores this
/// as TEXT (`"user"` / `"assistant"`); kept as an enum at the API edge so
/// callers can't write nonsense values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Role {
    User,
    Assistant,
}

impl Role {
    fn as_str(self) -> &'static str {
        match self {
            Self::User => "user",
            Self::Assistant => "assistant",
        }
    }

    fn parse(s: &str) -> Option<Self> {
        match s {
            "user" => Some(Self::User),
            "assistant" => Some(Self::Assistant),
            _ => None,
        }
    }
}

/// One scored hit returned by [`RecallStore::query`].
#[derive(Debug, Clone, PartialEq)]
pub struct RecallHit {
    pub ts: String,
    pub role: Role,
    pub snippet: String,
    /// Cosine similarity in [-1, 1]. Higher is more similar.
    pub score: f32,
}

/// Internal heap entry. Wraps `RecallHit` with an `Ord` impl over the
/// score, since `f32` is only `PartialOrd`. NaN sinks to the smallest so
/// it gets evicted from the top-k heap first.
#[derive(Debug, Clone)]
struct ScoredHit {
    score: f32,
    hit: RecallHit,
}

impl PartialEq for ScoredHit {
    fn eq(&self, other: &Self) -> bool {
        self.score == other.score
    }
}

impl Eq for ScoredHit {}

impl PartialOrd for ScoredHit {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for ScoredHit {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // NaN-tolerant: treat NaN as smaller than everything so it
        // evicts first from the heap.
        match self.score.partial_cmp(&other.score) {
            Some(o) => o,
            None => {
                if self.score.is_nan() && other.score.is_nan() {
                    std::cmp::Ordering::Equal
                } else if self.score.is_nan() {
                    std::cmp::Ordering::Less
                } else {
                    std::cmp::Ordering::Greater
                }
            }
        }
    }
}

/// Embedding-model abstraction. Production = [`OllamaEmbedder`]; tests
/// inject a deterministic mock so they don't need a live Ollama.
pub trait Embedder: Send {
    /// Embed a single text and return the vector. Dimensionality is
    /// fixed per implementation — the store records it on first insert
    /// and rejects mismatched future inserts.
    fn embed(&mut self, text: &str) -> Result<Vec<f32>, String>;
}

// ────────────────────────────────────────────────────────────────────────────
// Constants
// ────────────────────────────────────────────────────────────────────────────

/// Hard cap on stored rows. FIFO-evicted on insert overflow. ~3KB/row at
/// 768 dims → ~150MB ceiling. Decision rationale lives in
/// `project_claudette_recall_feature_2026-05-08.md`.
pub const RECALL_ROW_CAP: usize = 50_000;

/// Default embed model. Ollama accepts either `nomic-embed-text` or
/// `nomic-embed-text:latest`; the bare form is what `ollama pull` prints
/// in its progress bar so we use that for consistency.
pub const DEFAULT_EMBED_MODEL: &str = "nomic-embed-text";

// ────────────────────────────────────────────────────────────────────────────
// Path resolution
// ────────────────────────────────────────────────────────────────────────────

/// Resolve the recall DB path. `CLAUDETTE_RECALL_DB` overrides for tests
/// and bring-your-own-path setups; otherwise `~/.claudette/recall.sqlite`.
#[must_use]
pub fn default_recall_db_path() -> PathBuf {
    if let Ok(p) = std::env::var("CLAUDETTE_RECALL_DB") {
        if !p.is_empty() {
            return PathBuf::from(p);
        }
    }
    let home = std::env::var("USERPROFILE")
        .or_else(|_| std::env::var("HOME"))
        .unwrap_or_else(|_| ".".to_string());
    PathBuf::from(home).join(".claudette").join("recall.sqlite")
}

// ────────────────────────────────────────────────────────────────────────────
// BLOB encode/decode for f32 vectors (little-endian)
// ────────────────────────────────────────────────────────────────────────────

#[must_use]
pub fn encode_vec(v: &[f32]) -> Vec<u8> {
    let mut out = Vec::with_capacity(v.len() * 4);
    for x in v {
        out.extend_from_slice(&x.to_le_bytes());
    }
    out
}

pub fn decode_vec(bytes: &[u8]) -> Result<Vec<f32>, String> {
    let mut out = Vec::with_capacity(bytes.len() / 4);
    decode_vec_into(bytes, &mut out)?;
    Ok(out)
}

/// Decode a vector into a pre-allocated buffer. `dst` is cleared first;
/// the caller picks the capacity so this can be reused across rows in a
/// scan without re-allocating 50K times per query.
pub fn decode_vec_into(bytes: &[u8], dst: &mut Vec<f32>) -> Result<(), String> {
    if !bytes.len().is_multiple_of(4) {
        return Err(format!(
            "recall: BLOB length {} is not a multiple of 4 — corrupt vector",
            bytes.len()
        ));
    }
    dst.clear();
    dst.reserve(bytes.len() / 4);
    for chunk in bytes.chunks_exact(4) {
        let arr: [u8; 4] = chunk.try_into().expect("chunks_exact yields 4-byte slices");
        dst.push(f32::from_le_bytes(arr));
    }
    Ok(())
}

// ────────────────────────────────────────────────────────────────────────────
// Cosine similarity
// ────────────────────────────────────────────────────────────────────────────

/// Cosine similarity. Returns `0.0` when either vector is the zero vector
/// (instead of NaN); callers treat zero as "no match" which is what we
/// want.
#[must_use]
pub fn cosine_similarity(a: &[f32], b: &[f32]) -> f32 {
    if a.len() != b.len() || a.is_empty() {
        return 0.0;
    }
    let mut dot = 0.0_f32;
    let mut na = 0.0_f32;
    let mut nb = 0.0_f32;
    for i in 0..a.len() {
        dot += a[i] * b[i];
        na += a[i] * a[i];
        nb += b[i] * b[i];
    }
    let denom = na.sqrt() * nb.sqrt();
    if denom == 0.0 {
        0.0
    } else {
        dot / denom
    }
}

// ────────────────────────────────────────────────────────────────────────────
// RecallStore — sqlite + embedder
// ────────────────────────────────────────────────────────────────────────────

pub struct RecallStore {
    conn: Connection,
    embedder: Box<dyn Embedder>,
    cap: usize,
}

impl RecallStore {
    /// Open a store at `path`, creating the schema if missing.
    pub fn open(path: impl AsRef<Path>, embedder: Box<dyn Embedder>) -> Result<Self, String> {
        let path = path.as_ref();
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent)
                .map_err(|e| format!("recall: create_dir_all {}: {e}", parent.display()))?;
        }
        let conn =
            Connection::open(path).map_err(|e| format!("recall: open {}: {e}", path.display()))?;
        Self::init_schema(&conn)?;
        Ok(Self {
            conn,
            embedder,
            cap: RECALL_ROW_CAP,
        })
    }

    /// Open an in-memory store. Used by the unit tests.
    pub fn open_in_memory(embedder: Box<dyn Embedder>) -> Result<Self, String> {
        let conn =
            Connection::open_in_memory().map_err(|e| format!("recall: open in-memory: {e}"))?;
        Self::init_schema(&conn)?;
        Ok(Self {
            conn,
            embedder,
            cap: RECALL_ROW_CAP,
        })
    }

    /// Override the row cap. Tests use this to exercise FIFO eviction
    /// without inserting 50k rows.
    pub fn with_cap(mut self, cap: usize) -> Self {
        self.cap = cap;
        self
    }

    fn init_schema(conn: &Connection) -> Result<(), String> {
        conn.execute_batch(
            "CREATE TABLE IF NOT EXISTS recall (
                id      INTEGER PRIMARY KEY,
                ts      TEXT    NOT NULL,
                role    TEXT    NOT NULL,
                snippet TEXT    NOT NULL,
                vec     BLOB    NOT NULL
            );
            CREATE INDEX IF NOT EXISTS idx_recall_ts ON recall(ts);",
        )
        .map_err(|e| format!("recall: init schema: {e}"))
    }

    /// Embed `snippet` and insert it. No-op for empty/whitespace snippets.
    /// Trims to 8KB to keep the DB bounded against runaway long messages
    /// (image OCR transcripts, paste storms, etc).
    pub fn index(&mut self, role: Role, snippet: &str) -> Result<(), String> {
        let trimmed = snippet.trim();
        if trimmed.is_empty() {
            return Ok(());
        }
        // Cap stored snippet at 8KB. The embedder still sees the trimmed
        // version, but extreme inputs (think: the assistant pasting a
        // 100KB log) shouldn't bloat the DB.
        let stored: &str = if trimmed.len() > 8 * 1024 {
            &trimmed[..8 * 1024]
        } else {
            trimmed
        };
        let vec = self.embedder.embed(trimmed)?;
        let ts = chrono::Utc::now().to_rfc3339();
        let blob = encode_vec(&vec);
        self.conn
            .execute(
                "INSERT INTO recall (ts, role, snippet, vec) VALUES (?1, ?2, ?3, ?4)",
                params![ts, role.as_str(), stored, blob],
            )
            .map_err(|e| format!("recall: insert: {e}"))?;
        self.evict_to_cap()?;
        Ok(())
    }

    /// Embed `query` and return the top `k` rows by cosine similarity,
    /// sorted descending by score.
    ///
    /// **Hot-loop optimizations (2026-05-15):** the previous implementation
    /// called [`cosine_similarity`] per row, which recomputed the query's
    /// norm on every row, and decoded each stored vector into a fresh
    /// `Vec<f32>` allocation. At the 50 K row cap that's 50 K wasted
    /// `qvec` norm passes and 50 K allocations per query. This rewrite:
    /// 1. computes `||q||` once outside the loop,
    /// 2. reuses one decode buffer across all rows,
    /// 3. fuses the dot-product and stored-vector norm into a single pass
    ///    over the row,
    /// 4. keeps only the running top-k via a min-heap so we don't sort the
    ///    full scored list.
    ///
    /// Sub-linear search (HNSW / sqlite-vec) is still the right answer
    /// past ~100K rows but it adds a heavy dependency. The audit flagged
    /// the brute-force scan as "borderline acceptable" at 50 K — these
    /// constant-factor wins move it back into "comfortable" without
    /// changing the storage model.
    pub fn query(&mut self, query: &str, k: usize) -> Result<Vec<RecallHit>, String> {
        let trimmed = query.trim();
        if trimmed.is_empty() || k == 0 {
            return Ok(Vec::new());
        }
        let qvec = self.embedder.embed(trimmed)?;

        // Norm of q is constant across the scan — hoist it out.
        let mut qnorm_sq = 0.0_f32;
        for &x in &qvec {
            qnorm_sq = x.mul_add(x, qnorm_sq);
        }
        if qnorm_sq <= 0.0 {
            return Ok(Vec::new());
        }
        let qnorm = qnorm_sq.sqrt();

        let mut stmt = self
            .conn
            .prepare("SELECT ts, role, snippet, vec FROM recall")
            .map_err(|e| format!("recall: prepare select: {e}"))?;
        let rows = stmt
            .query_map([], |row| {
                let ts: String = row.get(0)?;
                let role: String = row.get(1)?;
                let snippet: String = row.get(2)?;
                let vec_blob: Vec<u8> = row.get(3)?;
                Ok((ts, role, snippet, vec_blob))
            })
            .map_err(|e| format!("recall: query_map: {e}"))?;

        // Single buffer reused across rows to avoid 50K allocations on a
        // full-table scan.
        let mut vbuf: Vec<f32> = Vec::with_capacity(qvec.len());
        // Min-heap of (-score, hit) so the smallest score is at the top
        // and gets bumped when a better one arrives. `Reverse` keeps the
        // ordering inverted on `f32` (NaN/partial_cmp tolerance built in).
        use std::cmp::Reverse;
        use std::collections::BinaryHeap;
        let mut heap: BinaryHeap<Reverse<ScoredHit>> = BinaryHeap::with_capacity(k + 1);
        for row in rows {
            let (ts, role_str, snippet, blob) =
                row.map_err(|e| format!("recall: row error: {e}"))?;
            let Some(role) = Role::parse(&role_str) else {
                continue; // skip rows with unknown roles
            };
            if decode_vec_into(&blob, &mut vbuf).is_err() {
                continue; // skip corrupt rows
            }
            if vbuf.len() != qvec.len() {
                continue; // dim mismatch — old rows from a different model
            }
            // Fused dot + ||b||² — one pass, exploits FMA on x86.
            let mut dot = 0.0_f32;
            let mut bnorm_sq = 0.0_f32;
            for (qx, &bx) in qvec.iter().zip(&vbuf) {
                dot = qx.mul_add(bx, dot);
                bnorm_sq = bx.mul_add(bx, bnorm_sq);
            }
            let denom = qnorm * bnorm_sq.sqrt();
            let score = if denom == 0.0 { 0.0 } else { dot / denom };

            heap.push(Reverse(ScoredHit {
                score,
                hit: RecallHit {
                    ts,
                    role,
                    snippet,
                    score,
                },
            }));
            if heap.len() > k {
                heap.pop();
            }
        }

        // Heap is sorted ascending by score; drain and reverse so the
        // caller sees descending order.
        let mut hits: Vec<RecallHit> = heap.into_iter().map(|Reverse(s)| s.hit).collect();
        hits.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        Ok(hits)
    }

    /// Total row count. Useful for `/status`-style reporting.
    pub fn count(&self) -> Result<usize, String> {
        self.conn
            .query_row("SELECT COUNT(*) FROM recall", [], |r| r.get::<_, i64>(0))
            .map(|n| n.max(0) as usize)
            .map_err(|e| format!("recall: count: {e}"))
    }

    fn evict_to_cap(&mut self) -> Result<(), String> {
        let n = self.count()?;
        if n <= self.cap {
            return Ok(());
        }
        let to_remove = n - self.cap;
        // Oldest first by id (which is monotonic — INTEGER PRIMARY KEY
        // without AUTOINCREMENT still increments, and our inserts are
        // single-threaded behind the global Mutex).
        // SQLite expects a signed integer; clamp at i64::MAX (we will
        // never realistically have more than 9e18 rows to evict, but the
        // cast lint fires on the unchecked direction).
        let to_remove_i64 = i64::try_from(to_remove).unwrap_or(i64::MAX);
        self.conn
            .execute(
                "DELETE FROM recall WHERE id IN (SELECT id FROM recall ORDER BY id ASC LIMIT ?1)",
                params![to_remove_i64],
            )
            .map_err(|e| format!("recall: evict: {e}"))?;
        Ok(())
    }
}

// ────────────────────────────────────────────────────────────────────────────
// OllamaEmbedder — production embedder backed by /api/embeddings
// ────────────────────────────────────────────────────────────────────────────

pub struct OllamaEmbedder {
    client: reqwest::blocking::Client,
    base_url: String,
    model: String,
    /// Latch: once we've confirmed the model is pullable on this process,
    /// don't probe again.
    ready: bool,
}

impl OllamaEmbedder {
    pub fn new() -> Result<Self, String> {
        let client = reqwest::blocking::Client::builder()
            // Embed calls themselves are fast (<100ms) but the first call
            // on a cold model triggers a load that can take ~2s on CPU.
            // Pulls are bounded by the separate ensure_model branch.
            .timeout(std::time::Duration::from_secs(60))
            .build()
            .map_err(|e| format!("recall: build http client: {e}"))?;
        let model = std::env::var("CLAUDETTE_RECALL_MODEL")
            .ok()
            .filter(|s| !s.is_empty())
            .unwrap_or_else(|| DEFAULT_EMBED_MODEL.to_string());
        Ok(Self {
            client,
            base_url: resolve_ollama_url(),
            model,
            ready: false,
        })
    }

    /// Make sure Ollama has the model loaded. POSTs to `/api/show`; if
    /// the model is missing, runs `/api/pull` (stream:false) with a
    /// status line on stderr so the user knows the ~270MB fetch is happening.
    fn ensure_model(&mut self) -> Result<(), String> {
        if self.ready {
            return Ok(());
        }
        // /api/show — cheapest probe; returns 404 when the model isn't
        // present locally.
        let show_url = format!("{}/api/show", self.base_url);
        let resp = self
            .client
            .post(&show_url)
            .json(&json!({ "name": self.model }))
            .send()
            .map_err(|e| {
                format!(
                    "recall: cannot reach Ollama at {} ({e}). Start it with `ollama serve`.",
                    self.base_url
                )
            })?;

        if resp.status().is_success() {
            self.ready = true;
            return Ok(());
        }

        // Anything other than 404 is a hard error — probably a config issue
        // we shouldn't paper over by trying to pull.
        if resp.status() != reqwest::StatusCode::NOT_FOUND {
            return Err(format!(
                "recall: /api/show returned {} for {}",
                resp.status(),
                self.model
            ));
        }

        // Lazy install path — print a status line so the user knows why
        // the next call is taking a while.
        eprintln!(
            "{} pulling embed model {} (~270MB, one-time) ...",
            crate::theme::SAVE,
            crate::theme::accent(&self.model)
        );

        let pull_url = format!("{}/api/pull", self.base_url);
        let pull_resp = self
            .client
            // Pulls take real time — extend timeout for this call only.
            .post(&pull_url)
            .timeout(std::time::Duration::from_secs(600))
            .json(&json!({ "name": self.model, "stream": false }))
            .send()
            .map_err(|e| format!("recall: /api/pull request failed: {e}"))?;

        if !pull_resp.status().is_success() {
            return Err(format!(
                "recall: /api/pull returned {} for {} — try `ollama pull {}` manually",
                pull_resp.status(),
                self.model,
                self.model
            ));
        }

        eprintln!(
            "{} {} ready",
            crate::theme::ok(crate::theme::OK_GLYPH),
            crate::theme::ok(&self.model)
        );
        self.ready = true;
        Ok(())
    }
}

impl Embedder for OllamaEmbedder {
    fn embed(&mut self, text: &str) -> Result<Vec<f32>, String> {
        self.ensure_model()?;
        let url = format!("{}/api/embeddings", self.base_url);
        let resp = self
            .client
            .post(&url)
            .json(&json!({ "model": self.model, "prompt": text }))
            .send()
            .map_err(|e| format!("recall: /api/embeddings request: {e}"))?;
        if !resp.status().is_success() {
            return Err(format!("recall: /api/embeddings HTTP {}", resp.status()));
        }
        let body: Value = resp
            .json()
            .map_err(|e| format!("recall: /api/embeddings parse: {e}"))?;
        parse_ollama_embedding(&body)
    }
}

/// Parse the Ollama-native `/api/embeddings` response shape:
/// `{ "embedding": [f32, …] }`.
fn parse_ollama_embedding(body: &Value) -> Result<Vec<f32>, String> {
    let arr = body
        .get("embedding")
        .and_then(Value::as_array)
        .ok_or_else(|| format!("recall: response missing 'embedding': {body}"))?;
    json_array_to_f32s(arr)
}

/// Parse the OpenAI-compat `/v1/embeddings` response shape:
/// `{ "data": [ { "embedding": [f32, …] }, … ] }`. Only the first element
/// is used since we only ever embed one input per request.
fn parse_openai_compat_embedding(body: &Value) -> Result<Vec<f32>, String> {
    let arr = body
        .get("data")
        .and_then(Value::as_array)
        .and_then(|d| d.first())
        .and_then(|d| d.get("embedding"))
        .and_then(Value::as_array)
        .ok_or_else(|| format!("recall: response missing 'data[0].embedding': {body}"))?;
    json_array_to_f32s(arr)
}

/// Convert a JSON array of numbers into `Vec<f32>`. Shared by both the
/// Ollama-native and OpenAI-compat response parsers.
fn json_array_to_f32s(arr: &[Value]) -> Result<Vec<f32>, String> {
    let mut out = Vec::with_capacity(arr.len());
    for v in arr {
        let f = v
            .as_f64()
            .ok_or_else(|| "recall: non-numeric value in 'embedding'".to_string())?;
        out.push(f as f32);
    }
    if out.is_empty() {
        return Err("recall: empty embedding returned".to_string());
    }
    Ok(out)
}

// ────────────────────────────────────────────────────────────────────────────
// OpenAICompatEmbedder — production embedder backed by /v1/embeddings
// ────────────────────────────────────────────────────────────────────────────
//
// Used when CLAUDETTE_OPENAI_COMPAT=1, e.g. against LM Studio. Unlike
// OllamaEmbedder, no /api/show probe and no /api/pull auto-install — LM
// Studio expects the user to load the embed model ahead of time from its
// "Local Server" tab. A failed embed surfaces a clear hint to do so.

pub struct OpenAICompatEmbedder {
    client: reqwest::blocking::Client,
    base_url: String,
    model: String,
}

impl OpenAICompatEmbedder {
    pub fn new() -> Result<Self, String> {
        let client = reqwest::blocking::Client::builder()
            .timeout(std::time::Duration::from_secs(60))
            .build()
            .map_err(|e| format!("recall: build http client: {e}"))?;
        let model = std::env::var("CLAUDETTE_RECALL_MODEL")
            .ok()
            .filter(|s| !s.is_empty())
            .unwrap_or_else(|| DEFAULT_EMBED_MODEL.to_string());
        Ok(Self {
            client,
            base_url: resolve_ollama_url(),
            model,
        })
    }
}

impl Embedder for OpenAICompatEmbedder {
    fn embed(&mut self, text: &str) -> Result<Vec<f32>, String> {
        let url = format!("{}/v1/embeddings", self.base_url);
        let resp = self
            .client
            .post(&url)
            .json(&json!({ "model": self.model, "input": text }))
            .send()
            .map_err(|e| {
                format!(
                    "recall: cannot reach OpenAI-compat server at {} ({e}). \
                     Is LM Studio running on this port?",
                    self.base_url
                )
            })?;
        if !resp.status().is_success() {
            let status = resp.status();
            let body = resp.text().unwrap_or_default();
            return Err(format!(
                "recall: /v1/embeddings HTTP {status} — load `{}` in LM Studio's \
                 Local Server tab (or set CLAUDETTE_RECALL_MODEL to a model id you have loaded). \
                 Body: {body}",
                self.model
            ));
        }
        let body: Value = resp
            .json()
            .map_err(|e| format!("recall: /v1/embeddings parse: {e}"))?;
        parse_openai_compat_embedding(&body)
    }
}

// ────────────────────────────────────────────────────────────────────────────
// Process-wide singleton
// ────────────────────────────────────────────────────────────────────────────

fn store_cell() -> &'static Mutex<Option<RecallStore>> {
    static CELL: OnceLock<Mutex<Option<RecallStore>>> = OnceLock::new();
    CELL.get_or_init(|| Mutex::new(None))
}

fn lock_store() -> Result<MutexGuard<'static, Option<RecallStore>>, String> {
    store_cell()
        .lock()
        .map_err(|e| format!("recall: store lock poisoned: {e}"))
}

fn ensure_store(guard: &mut MutexGuard<'static, Option<RecallStore>>) -> Result<(), String> {
    if guard.is_some() {
        return Ok(());
    }
    let embedder: Box<dyn Embedder> = if resolve_openai_compat() {
        Box::new(OpenAICompatEmbedder::new()?)
    } else {
        Box::new(OllamaEmbedder::new()?)
    };
    let store = RecallStore::open(default_recall_db_path(), embedder)?;
    **guard = Some(store);
    Ok(())
}

/// Reset the global store. ONLY for tests — production code should never
/// call this. Drops the existing store (if any), so the next `global_*`
/// call lazy-inits afresh.
#[cfg(test)]
pub fn reset_global() {
    if let Ok(mut guard) = lock_store() {
        *guard = None;
    }
}

/// Install a custom store as the global. ONLY for tests — production code
/// should never call this. Used to inject the in-memory mock store.
#[cfg(test)]
pub fn install_global_for_test(store: RecallStore) {
    if let Ok(mut guard) = lock_store() {
        *guard = Some(store);
    }
}

/// Index `snippet` under `role`. Lazy-inits the global store on first call.
/// No-op for empty/whitespace snippets. Errors are returned to the caller —
/// the post-turn indexing hook turns them into a single warn line so a
/// transient Ollama outage doesn't break the REPL.
pub fn global_index(role: Role, snippet: &str) -> Result<(), String> {
    let mut guard = lock_store()?;
    ensure_store(&mut guard)?;
    guard
        .as_mut()
        .expect("ensure_store left store None")
        .index(role, snippet)
}

/// Run a top-`k` recall query against the global store. Lazy-inits on
/// first call.
pub fn global_query(query: &str, k: usize) -> Result<Vec<RecallHit>, String> {
    let mut guard = lock_store()?;
    ensure_store(&mut guard)?;
    guard
        .as_mut()
        .expect("ensure_store left store None")
        .query(query, k)
}

/// Pre-flight the recall embedder: do a tiny embed call and discard the
/// result. Lazy-inits the store on first use. Returns the upstream embed
/// error string on failure (e.g. LM Studio's "No models loaded" 400),
/// otherwise `Ok(())`.
///
/// The REPL and TUI call this once at startup so the user discovers a
/// missing embed model BEFORE their first turn, instead of after, with
/// noise on every subsequent turn until the sticky-disable layer kicks in.
pub fn probe() -> Result<(), String> {
    let mut guard = lock_store()?;
    ensure_store(&mut guard)?;
    guard
        .as_mut()
        .expect("ensure_store left store None")
        .embedder
        .embed("probe")
        .map(|_| ())
}

// ────────────────────────────────────────────────────────────────────────────
// Tests
// ────────────────────────────────────────────────────────────────────────────

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

    /// Deterministic test embedder: hashes the input into a small fixed-dim
    /// vector. Two calls with identical text produce identical vectors;
    /// different text produces different vectors. Good enough for ranking
    /// roundtrip tests without a live Ollama.
    struct HashEmbedder {
        dim: usize,
    }

    impl HashEmbedder {
        fn new() -> Self {
            Self { dim: 8 }
        }
    }

    impl Embedder for HashEmbedder {
        fn embed(&mut self, text: &str) -> Result<Vec<f32>, String> {
            // Bag-of-words style: each char contributes to a bucket.
            // Sufficient signal for "matches my prior message" tests.
            let mut v = vec![0.0_f32; self.dim];
            for ch in text.chars() {
                let bucket = (ch as usize) % self.dim;
                v[bucket] += 1.0;
            }
            // Normalize so cosine equals dot product.
            let norm: f32 = v.iter().map(|x| x * x).sum::<f32>().sqrt();
            if norm > 0.0 {
                for x in &mut v {
                    *x /= norm;
                }
            }
            Ok(v)
        }
    }

    /// Embedder that always returns a fixed vector — for cap/eviction
    /// tests where we don't care about ranking quality.
    struct ConstEmbedder;
    impl Embedder for ConstEmbedder {
        fn embed(&mut self, _text: &str) -> Result<Vec<f32>, String> {
            Ok(vec![1.0, 0.0, 0.0, 0.0])
        }
    }

    /// Embedder that always fails — for probe-failure tests. Mimics the
    /// shape of LM Studio's "No models loaded" 400 response.
    struct FailingEmbedder;
    impl Embedder for FailingEmbedder {
        fn embed(&mut self, _text: &str) -> Result<Vec<f32>, String> {
            Err(
                "recall: /v1/embeddings HTTP 400 — load `nomic-embed-text` in LM Studio's \
                 Local Server tab"
                    .to_string(),
            )
        }
    }

    #[test]
    fn embedder_failure_propagates_as_error_string() {
        // This is what `recall::probe` relies on: a failing embed must
        // surface the upstream error message intact, not swallow it. Pins
        // the contract since startup-probe diagnostics depend on the
        // user being able to read the LM Studio hint.
        let mut e = FailingEmbedder;
        let err = e.embed("probe").expect_err("FailingEmbedder must fail");
        assert!(err.contains("Local Server tab"), "got: {err}");
    }

    #[test]
    fn probe_through_store_returns_err_on_embedder_failure() {
        // Exercise the same code path `recall::probe` takes through the
        // store layer: ensure_store → embedder.embed → discard the vector.
        // Uses an in-memory store so we don't touch the global singleton.
        let mut store = RecallStore::open_in_memory(Box::new(FailingEmbedder)).expect("open");
        let err = store
            .embedder
            .embed("probe")
            .expect_err("FailingEmbedder must fail");
        assert!(err.contains("HTTP 400"), "got: {err}");
    }

    #[test]
    fn encode_decode_roundtrip() {
        let v = vec![0.0, 1.0, -1.5, std::f32::consts::PI, f32::EPSILON];
        let bytes = encode_vec(&v);
        assert_eq!(bytes.len(), v.len() * 4);
        let back = decode_vec(&bytes).expect("decode");
        assert_eq!(back, v);
    }

    #[test]
    fn decode_rejects_misaligned_bytes() {
        let err = decode_vec(&[1, 2, 3]).expect_err("should reject 3-byte input");
        assert!(err.contains("multiple of 4"), "got: {err}");
    }

    #[test]
    fn cosine_handles_zero_vectors() {
        // Don't NaN out on zero norm — return exactly 0.
        assert!(cosine_similarity(&[0.0, 0.0], &[0.0, 0.0]).abs() < 1e-9);
        assert!(cosine_similarity(&[1.0, 0.0], &[0.0, 0.0]).abs() < 1e-9);
    }

    #[test]
    fn cosine_is_one_for_identical() {
        let a = vec![0.5, 0.5, 0.5];
        assert!((cosine_similarity(&a, &a) - 1.0).abs() < 1e-6);
    }

    #[test]
    fn cosine_is_zero_for_orthogonal() {
        let a = vec![1.0, 0.0];
        let b = vec![0.0, 1.0];
        assert!(cosine_similarity(&a, &b).abs() < 1e-6);
    }

    #[test]
    fn cosine_returns_zero_for_mismatched_length() {
        assert!(cosine_similarity(&[1.0, 0.0], &[1.0, 0.0, 0.0]).abs() < 1e-9);
    }

    #[test]
    fn store_roundtrip_indexes_and_queries() {
        let mut store = RecallStore::open_in_memory(Box::new(HashEmbedder::new())).expect("open");
        store
            .index(Role::User, "the meeting with brian is on tuesday")
            .unwrap();
        store
            .index(Role::Assistant, "got it, brian + tuesday noted")
            .unwrap();
        store
            .index(Role::User, "completely unrelated content about weather")
            .unwrap();

        let hits = store.query("when is brian's meeting", 2).unwrap();
        assert_eq!(hits.len(), 2);
        // The brian-related hits should outrank the weather one — but the
        // hash embedder is too crude for stronger ordering claims, so just
        // check that the weather snippet isn't in the top-2.
        for hit in &hits {
            assert!(
                !hit.snippet.contains("weather"),
                "weather should not be in top-2: {hits:?}"
            );
        }
    }

    #[test]
    fn store_skips_empty_snippets() {
        let mut store = RecallStore::open_in_memory(Box::new(HashEmbedder::new())).expect("open");
        store.index(Role::User, "").unwrap();
        store.index(Role::User, "   \t\n  ").unwrap();
        assert_eq!(store.count().unwrap(), 0);
    }

    #[test]
    fn empty_query_returns_empty_results() {
        let mut store = RecallStore::open_in_memory(Box::new(HashEmbedder::new())).expect("open");
        store.index(Role::User, "hello").unwrap();
        assert!(store.query("", 5).unwrap().is_empty());
        assert!(store.query("   ", 5).unwrap().is_empty());
        assert!(store.query("hello", 0).unwrap().is_empty());
    }

    #[test]
    fn fifo_eviction_at_cap() {
        let mut store = RecallStore::open_in_memory(Box::new(ConstEmbedder))
            .expect("open")
            .with_cap(3);
        store.index(Role::User, "first").unwrap();
        store.index(Role::User, "second").unwrap();
        store.index(Role::User, "third").unwrap();
        assert_eq!(store.count().unwrap(), 3);
        store.index(Role::User, "fourth").unwrap();
        assert_eq!(store.count().unwrap(), 3, "cap should hold");

        let hits = store.query("any", 10).unwrap();
        let snippets: Vec<&str> = hits.iter().map(|h| h.snippet.as_str()).collect();
        assert!(!snippets.contains(&"first"), "oldest evicted: {snippets:?}");
        assert!(snippets.contains(&"second"));
        assert!(snippets.contains(&"third"));
        assert!(snippets.contains(&"fourth"));
    }

    #[test]
    fn long_snippet_is_truncated() {
        let mut store = RecallStore::open_in_memory(Box::new(HashEmbedder::new())).expect("open");
        let huge = "x".repeat(20_000);
        store.index(Role::User, &huge).unwrap();
        let hits = store.query("xxxx", 1).unwrap();
        assert_eq!(hits.len(), 1);
        assert!(
            hits[0].snippet.len() <= 8 * 1024,
            "snippet should be capped at 8KB, got {}",
            hits[0].snippet.len()
        );
    }

    #[test]
    fn results_are_sorted_descending_by_score() {
        let mut store = RecallStore::open_in_memory(Box::new(HashEmbedder::new())).expect("open");
        for snippet in [
            "the cat sat on the mat",
            "weather forecast for next tuesday",
            "the cat stretched across the rug",
            "currency exchange rates today",
        ] {
            store.index(Role::User, snippet).unwrap();
        }
        let hits = store.query("cat on mat", 4).unwrap();
        // Top hit must be one of the cat lines.
        assert!(
            hits[0].snippet.contains("cat"),
            "top hit should be cat-related: {:?}",
            hits[0]
        );
        // Sorted descending.
        for w in hits.windows(2) {
            assert!(
                w[0].score >= w[1].score,
                "results must be descending by score"
            );
        }
    }

    #[test]
    fn query_returns_only_top_k_via_heap() {
        // The rewrite swapped a "score everything, sort, truncate" pass
        // for a running top-k heap. Pin that the heap-based path still
        // returns exactly `k` results in descending order even when the
        // store has many more rows than `k`.
        let mut store = RecallStore::open_in_memory(Box::new(HashEmbedder::new())).expect("open");
        for i in 0..20 {
            store
                .index(
                    Role::User,
                    &format!("snippet number {i} talking about cats"),
                )
                .unwrap();
        }
        let hits = store.query("cats", 3).unwrap();
        assert_eq!(hits.len(), 3, "exactly k results");
        for w in hits.windows(2) {
            assert!(
                w[0].score >= w[1].score,
                "results must be descending: {:?}",
                hits
            );
        }
    }

    #[test]
    fn query_skips_rows_with_mismatched_dim() {
        // The new query path filters out rows whose vector dimension
        // doesn't match the embedder's current dim (covers the
        // "stored under an older model" case without crashing).
        let mut store = RecallStore::open_in_memory(Box::new(HashEmbedder::new())).expect("open");
        store
            .conn
            .execute(
                "INSERT INTO recall (ts, role, snippet, vec) VALUES ('2026-01-01T00:00:00Z', 'user', 'old-model row', ?1)",
                params![encode_vec(&[1.0_f32, 0.0, 0.0])],
            )
            .expect("seed insert");
        store
            .index(Role::User, "modern cat content matching the embedder dim")
            .unwrap();
        let hits = store.query("cat", 5).unwrap();
        // The old-dim row should be silently skipped, leaving exactly one
        // modern hit.
        assert_eq!(hits.len(), 1);
        assert!(hits[0].snippet.contains("modern cat"));
    }

    #[test]
    fn role_parse_roundtrip() {
        for r in [Role::User, Role::Assistant] {
            assert_eq!(Role::parse(r.as_str()), Some(r));
        }
        assert_eq!(Role::parse("system"), None);
    }

    #[test]
    fn parse_ollama_embedding_happy_path() {
        let body = json!({ "embedding": [0.1, 0.2, -0.3, 0.0, 1.5] });
        let v = parse_ollama_embedding(&body).expect("parse");
        assert_eq!(v.len(), 5);
        assert!((v[0] - 0.1).abs() < 1e-6);
        assert!((v[2] - -0.3).abs() < 1e-6);
    }

    #[test]
    fn parse_ollama_embedding_rejects_missing_field() {
        let body = json!({ "data": [] });
        let err = parse_ollama_embedding(&body).expect_err("should fail");
        assert!(err.contains("missing 'embedding'"), "got: {err}");
    }

    #[test]
    fn parse_openai_compat_embedding_happy_path() {
        // Real LM Studio shape — extra fields we don't care about included
        // to make sure we tolerate them.
        let body = json!({
            "object": "list",
            "data": [
                {
                    "object": "embedding",
                    "index": 0,
                    "embedding": [0.42, -0.17, 0.99]
                }
            ],
            "model": "nomic-embed-text-v1.5",
            "usage": { "prompt_tokens": 4, "total_tokens": 4 }
        });
        let v = parse_openai_compat_embedding(&body).expect("parse");
        assert_eq!(v.len(), 3);
        assert!((v[0] - 0.42).abs() < 1e-6);
    }

    #[test]
    fn parse_openai_compat_embedding_rejects_missing_data() {
        let body = json!({ "embedding": [0.1, 0.2] });
        let err = parse_openai_compat_embedding(&body).expect_err("should fail");
        assert!(err.contains("'data[0].embedding'"), "got: {err}");
    }

    #[test]
    fn parse_openai_compat_embedding_rejects_empty_data_array() {
        let body = json!({ "object": "list", "data": [] });
        let err = parse_openai_compat_embedding(&body).expect_err("should fail");
        assert!(err.contains("'data[0].embedding'"), "got: {err}");
    }

    #[test]
    fn json_array_to_f32s_rejects_empty() {
        let err = json_array_to_f32s(&[]).expect_err("should fail");
        assert!(err.contains("empty embedding"), "got: {err}");
    }

    #[test]
    fn json_array_to_f32s_rejects_non_numeric() {
        let arr = vec![json!(0.5), json!("not a number")];
        let err = json_array_to_f32s(&arr).expect_err("should fail");
        assert!(err.contains("non-numeric"), "got: {err}");
    }

    // ────────────────────────────────────────────────────────────────────
    // Live smoke tests — #[ignore]'d so normal `cargo test` skips them.
    // Run on-demand against a real LM Studio with the embed model loaded:
    //
    //   $env:OLLAMA_HOST = "http://localhost:1234"
    //   $env:CLAUDETTE_RECALL_MODEL = "text-embedding-nomic-embed-text-v1.5"
    //   cargo test --lib recall_live -- --ignored --test-threads=1
    //
    // These DO mutate process-wide env vars, so they must run serially —
    // hence `--test-threads=1`. Both tests set the same values, but other
    // tests in the binary may read OLLAMA_HOST and race.
    // ────────────────────────────────────────────────────────────────────

    #[test]
    #[ignore = "requires live LM Studio with embed model loaded"]
    fn recall_live_openai_compat_embed_is_deterministic() {
        let mut e = OpenAICompatEmbedder::new().expect("construct embedder");
        let v1 = e
            .embed("hello from claudette recall smoke")
            .expect("embed 1");
        let v2 = e
            .embed("hello from claudette recall smoke")
            .expect("embed 2");
        assert!(!v1.is_empty(), "got empty vector");
        assert!(
            v1.len() >= 256,
            "expected an embedding ≥256 dims, got {}",
            v1.len()
        );
        assert_eq!(v1.len(), v2.len(), "dim should be stable across calls");
        let cos = cosine_similarity(&v1, &v2);
        assert!(
            (cos - 1.0).abs() < 1e-3,
            "same input should produce ~identical vectors, cos={cos}"
        );
    }

    #[test]
    #[ignore = "requires live LM Studio with embed model loaded"]
    fn recall_live_full_index_query_roundtrip() {
        let embedder: Box<dyn Embedder> =
            Box::new(OpenAICompatEmbedder::new().expect("construct embedder"));
        let mut store = RecallStore::open_in_memory(embedder).expect("open store");

        store
            .index(Role::User, "the meeting with brian is on tuesday at 3pm")
            .unwrap();
        store
            .index(Role::Assistant, "got it — brian, tuesday 3pm noted")
            .unwrap();
        store
            .index(
                Role::User,
                "completely unrelated content about the weather forecast for next week",
            )
            .unwrap();
        store
            .index(
                Role::User,
                "another tangent about currency exchange rates today",
            )
            .unwrap();

        let hits = store.query("when is brian's meeting", 2).expect("query");
        assert_eq!(hits.len(), 2, "asked for top-2: {hits:?}");
        for h in &hits {
            assert!(
                !h.snippet.contains("weather") && !h.snippet.contains("currency"),
                "off-topic snippet leaked into top-2: {h:?}"
            );
        }
        // Top hit must be one of the brian/tuesday lines.
        assert!(
            hits[0].snippet.contains("brian") || hits[0].snippet.contains("tuesday"),
            "top hit should be brian-related, got: {:?}",
            hits[0]
        );
        // Real embedding scores should be meaningfully positive on a
        // semantic match — not just barely-above-zero noise.
        assert!(
            hits[0].score > 0.5,
            "top hit score too low ({}); embedder may be returning noise",
            hits[0].score
        );
    }
}