claudette 0.4.1

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
//! 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,
}

/// 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> {
    if !bytes.len().is_multiple_of(4) {
        return Err(format!(
            "recall: BLOB length {} is not a multiple of 4 — corrupt vector",
            bytes.len()
        ));
    }
    let mut out = Vec::with_capacity(bytes.len() / 4);
    for chunk in bytes.chunks_exact(4) {
        let arr: [u8; 4] = chunk.try_into().expect("chunks_exact yields 4-byte slices");
        out.push(f32::from_le_bytes(arr));
    }
    Ok(out)
}

// ────────────────────────────────────────────────────────────────────────────
// 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.
    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)?;

        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}"))?;

        let mut hits: Vec<RecallHit> = Vec::new();
        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
            };
            let Ok(v) = decode_vec(&blob) else {
                continue; // skip corrupt rows
            };
            let score = cosine_similarity(&qvec, &v);
            hits.push(RecallHit {
                ts,
                role,
                snippet,
                score,
            });
        }

        hits.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        hits.truncate(k);
        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)
}

// ────────────────────────────────────────────────────────────────────────────
// 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])
        }
    }

    #[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 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
        );
    }
}