context-forge 0.8.2

Local-first persistent memory for LLM applications - turso + Tantivy BM25 retrieval, recency decay, token-budget context assembly, secret scrubbing, and optional local-LLM distillation.
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
//! Core business logic: assembly, scoring, and snapshot management.

use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;

use crate::config::{Config, DEFAULT_RECENCY_HALF_LIFE_SECS};
use crate::entry::ContextEntry;
use crate::error::Error;
use crate::lexicon::LexiconScorer;
use crate::traits::{ContextStorage, Result, Searcher};
// Bring the Embedder trait into scope for method dispatch on Arc<FasEmbedder>.
#[cfg(feature = "semantic")]
use crate::semantic::Embedder as _;

/// Default candidate limit when fetching search results for assembly.
const DEFAULT_SEARCH_LIMIT: usize = 50;

/// Well-defined query constant that means "match all entries" in the Searcher trait.
///
/// FTS5 interprets `*` as a prefix wildcard that matches every token.
/// Searcher implementations MUST treat this value as a match-all query.
pub const MATCH_ALL_QUERY: &str = "*";

/// Options for saving a snapshot entry.
#[derive(Debug, Default, Clone)]
pub struct SaveOptions {
    /// Optional caller session identifier.
    pub session_id: Option<String>,
    /// Namespace partition for the new entry. `None` = global scope.
    pub scope: Option<String>,
    /// Arbitrary caller metadata, stored as JSON.
    ///
    /// Unlike `content`, metadata is persisted **verbatim** by
    /// [`crate::ContextForge::save`] — it is not passed through
    /// [`crate::scrub_secrets`]. Callers MUST NOT place untrusted or
    /// secret-bearing text in this field.
    pub metadata: Option<serde_json::Value>,
}

/// Estimate token count using whitespace heuristic (1 token ≈ 4 chars).
#[must_use]
pub fn estimate_tokens(text: &str) -> usize {
    text.len().div_ceil(4)
}

/// Exponential recency decay.
///
/// Returns a multiplier in `(0.0, 1.0]` where 1.0 means "just created" and
/// the value halves every `half_life` seconds.
fn recency_decay(age_seconds: f64, half_life: f64) -> f64 {
    0.5_f64.powf(age_seconds / half_life)
}

/// The core context engine that coordinates storage, search, and assembly.
pub struct ContextEngine {
    storage: Box<dyn ContextStorage>,
    searcher: Box<dyn Searcher>,
    config: Config,
    scorer: Option<Arc<dyn LexiconScorer>>,
    #[cfg(feature = "semantic")]
    embedder: Option<Arc<crate::semantic::FasEmbedder>>,
}

impl ContextEngine {
    /// Create a new engine with the given storage backend, searcher, and config.
    ///
    /// If `config.recency_half_life_secs` is not positive and finite, it is
    /// clamped to [`DEFAULT_RECENCY_HALF_LIFE_SECS`] to prevent NaN/inf in
    /// recency decay scoring.
    #[must_use]
    pub fn new(
        storage: Box<dyn ContextStorage>,
        searcher: Box<dyn Searcher>,
        mut config: Config,
    ) -> Self {
        if !config.recency_half_life_secs.is_finite() || config.recency_half_life_secs <= 0.0 {
            config.recency_half_life_secs = DEFAULT_RECENCY_HALF_LIFE_SECS;
        }

        Self {
            storage,
            searcher,
            config,
            scorer: None,
            #[cfg(feature = "semantic")]
            embedder: None,
        }
    }

    /// Attach a [`LexiconScorer`] to this engine.
    ///
    /// The scorer runs inside [`Self::assemble`] after BM25 + recency decay
    /// and before the token-budget cut. Calling this again replaces the previous
    /// scorer. Prefer constructing via [`crate::ContextForgeBuilder`], which
    /// automatically composes [`crate::DefaultEnglishScorer`] with any
    /// caller-provided persona scorer.
    #[must_use]
    pub fn with_scorer(mut self, scorer: Arc<dyn LexiconScorer>) -> Self {
        self.scorer = Some(scorer);
        self
    }

    /// Attach an [`crate::semantic::Embedder`] for semantic search.
    ///
    /// When set, [`Self::save_snapshot`] generates and stores an embedding for
    /// each new entry, and [`Self::assemble`] blends BM25 and semantic
    /// candidates via Reciprocal Rank Fusion (RRF, k=60).
    #[cfg(feature = "semantic")]
    #[must_use]
    pub(crate) fn with_embedder(mut self, embedder: Arc<crate::semantic::FasEmbedder>) -> Self {
        self.embedder = Some(embedder);
        self
    }

    /// Assemble context entries that fit within `token_budget`.
    ///
    /// 1. Searches for candidates matching `query`, restricted to `scope` if given.
    /// 2. Applies recency weighting to each candidate's score.
    /// 3. Sorts by weighted score descending.
    /// 4. Packs entries greedily until the budget is exhausted.
    ///
    /// `scope = None` searches every entry regardless of scope (global recall).
    /// `scope = Some(s)` restricts the search to entries whose `scope` equals `s`.
    ///
    /// # Errors
    ///
    /// Returns an error if the underlying search fails.
    pub async fn assemble(
        &self,
        query: &str,
        scope: Option<&str>,
        token_budget: usize,
    ) -> Result<Vec<ContextEntry>> {
        let limit = DEFAULT_SEARCH_LIMIT;

        tracing::debug!(query = %query, ?scope, token_budget, "assemble: bm25 search");
        let bm25_candidates = self.searcher.search(query, scope, limit).await?;
        tracing::debug!(count = %bm25_candidates.len(), "assemble: bm25 complete");

        // Semantic search runs when the feature is enabled and an embedder is
        // configured; otherwise returns empty (no-op default on the Searcher trait).
        let semantic_candidates = self.run_semantic_search(query, scope, limit).await;
        tracing::debug!(count = %semantic_candidates.len(), "assemble: semantic complete");

        if bm25_candidates.is_empty() && semantic_candidates.is_empty() {
            tracing::debug!("assemble: both searches empty");
            return Ok(Vec::new());
        }

        // Reciprocal Rank Fusion (RRF, k=60) over the full union of both
        // candidate sets. Entries absent from one list get worst_rank = limit+1.
        //
        // RRF score = 1/(k + bm25_rank) + 1/(k + semantic_rank)
        // Then multiply by recency decay and (1 + lexicon_boost).
        let worst_rank = limit + 1;
        let k = 60.0_f64;
        let now = current_timestamp();
        let half_life = self.config.recency_half_life_secs;

        // Build entry map: semantic first so BM25 entries win on overlap.
        let mut entry_map: std::collections::HashMap<String, ContextEntry> =
            std::collections::HashMap::new();
        for se in &semantic_candidates {
            entry_map
                .entry(se.entry.id.clone())
                .or_insert_with(|| se.entry.clone());
        }
        for se in &bm25_candidates {
            entry_map.insert(se.entry.id.clone(), se.entry.clone());
        }

        let bm25_rank: std::collections::HashMap<&str, usize> = bm25_candidates
            .iter()
            .enumerate()
            .map(|(i, se)| (se.entry.id.as_str(), i + 1))
            .collect();
        let semantic_rank: std::collections::HashMap<&str, usize> = semantic_candidates
            .iter()
            .enumerate()
            .map(|(i, se)| (se.entry.id.as_str(), i + 1))
            .collect();

        let mut weighted: Vec<(f64, ContextEntry)> = entry_map
            .into_iter()
            .map(|(id, entry)| {
                let br = *bm25_rank.get(id.as_str()).unwrap_or(&worst_rank);
                let sr = *semantic_rank.get(id.as_str()).unwrap_or(&worst_rank);
                #[allow(
                    clippy::cast_precision_loss,
                    reason = "ranks are small integers (max ~51); lossless"
                )]
                let rrf = 1.0 / (k + br as f64) + 1.0 / (k + sr as f64);

                #[allow(
                    clippy::cast_precision_loss,
                    reason = "Unix timestamps fit losslessly in f64 for millions of years"
                )]
                let age = (now - entry.timestamp).max(0) as f64;
                let decay = recency_decay(age, half_life);
                let boost = self
                    .scorer
                    .as_ref()
                    .map_or(0.0_f32, |s| s.score(&entry, query));
                let final_score = rrf * decay * (1.0 + f64::from(boost).clamp(-1.0, 2.0));
                (final_score, entry)
            })
            .collect();

        weighted.sort_by(|a, b| b.0.total_cmp(&a.0));
        tracing::debug!(fused = %weighted.len(), "assemble: rrf fusion complete");

        // Greedy bin-packing by token budget — skip oversized entries, don't abort.
        let mut result = Vec::new();
        let mut tokens_used: usize = 0;
        for (_score, entry) in weighted {
            let entry_tokens = entry
                .token_count
                .unwrap_or_else(|| estimate_tokens(&entry.content));
            if tokens_used.saturating_add(entry_tokens) > token_budget {
                continue;
            }
            tokens_used += entry_tokens;
            result.push(entry);
        }

        tracing::debug!(entries = %result.len(), tokens_used, "assemble: complete");
        Ok(result)
    }

    /// Run semantic search if an embedder is available; otherwise return empty.
    #[allow(
        clippy::unused_async,
        reason = "async only active under semantic feature"
    )]
    async fn run_semantic_search(
        &self,
        query: &str,
        scope: Option<&str>,
        limit: usize,
    ) -> Vec<crate::entry::ScoredEntry> {
        #[cfg(feature = "semantic")]
        if let Some(ref embedder) = self.embedder {
            let emb = embedder.clone();
            let query_owned = query.to_owned();
            match tokio::task::spawn_blocking(move || emb.embed(&query_owned)).await {
                Ok(Ok(embedding)) => {
                    tracing::debug!(dims = %embedding.len(), "query embedding ready");
                    match self
                        .searcher
                        .search_semantic(&embedding, scope, limit)
                        .await
                    {
                        Ok(results) => return results,
                        Err(e) => tracing::warn!(error = %e, "semantic search failed"),
                    }
                }
                Ok(Err(e)) => tracing::warn!(error = %e, "query embedding failed"),
                Err(e) => tracing::warn!(error = %e, "embed task panicked"),
            }
        }
        // Suppress unused-parameter lint when semantic feature is disabled.
        #[cfg(not(feature = "semantic"))]
        let _ = (query, scope, limit);
        Vec::new()
    }

    /// Save a new snapshot entry. Capacity enforcement (LRU eviction) is
    /// handled atomically by the storage layer inside an immediate
    /// transaction, so no in-process locking is required here.
    ///
    /// Returns the generated entry ID.
    ///
    /// # Security
    ///
    /// This is a low-level write path that does **not** scrub secrets from
    /// `content`. [`ContextForge::save`](crate::ContextForge::save) is the
    /// only entry point that applies [`scrub_secrets`](crate::scrub_secrets)
    /// before persistence; callers writing through the engine directly are
    /// responsible for scrubbing first.
    ///
    /// # Errors
    ///
    /// Returns [`Error::InvalidEntry`] if `content` is empty, or propagates
    /// any error from the underlying storage write.
    pub async fn save_snapshot(
        &self,
        content: &str,
        kind: &str,
        options: &SaveOptions,
    ) -> Result<String> {
        if content.is_empty() {
            return Err(Error::InvalidEntry("content must not be empty".into()));
        }

        let timestamp = current_timestamp();
        let id = Uuid::now_v7().to_string();
        let token_count = estimate_tokens(content);

        let entry = ContextEntry {
            id: id.clone(),
            content: content.to_owned(),
            timestamp,
            kind: kind.to_owned(),
            scope: options.scope.clone(),
            session_id: options.session_id.clone(),
            token_count: Some(token_count),
            metadata: options.metadata.clone(),
        };

        self.storage.save(&entry).await?;
        tracing::trace!(id = %id, kind = %kind, tokens = %token_count, "entry saved");

        // Non-fatal: if embedding fails, the entry is stored and BM25-searchable.
        self.embed_and_store(&id, content).await;

        Ok(id)
    }

    /// Generate and persist an embedding for an already-saved entry.
    ///
    /// Errors are logged and swallowed — the entry is always stored even when
    /// embedding fails.
    #[allow(
        clippy::unused_async,
        reason = "async only active under semantic feature"
    )]
    async fn embed_and_store(&self, id: &str, content: &str) {
        #[cfg(feature = "semantic")]
        if let Some(ref embedder) = self.embedder {
            tracing::debug!(id = %id, "generating embedding");
            let emb = embedder.clone();
            let text = content.to_owned();
            let id_owned = id.to_owned();
            match tokio::task::spawn_blocking(move || emb.embed(&text)).await {
                Ok(Ok(embedding)) => {
                    if let Err(e) = self.storage.save_embedding(&id_owned, &embedding).await {
                        tracing::warn!(
                            id = %id_owned,
                            error = %e,
                            "save_embedding failed; entry stored without semantic index"
                        );
                    } else {
                        tracing::trace!(id = %id_owned, dims = %embedding.len(), "embedding stored");
                    }
                }
                Ok(Err(e)) => tracing::warn!(
                    id = %id,
                    error = %e,
                    "embed failed; entry stored without semantic index"
                ),
                Err(e) => tracing::warn!(
                    id = %id,
                    error = %e,
                    "embed task panicked; entry stored without semantic index"
                ),
            }
        }
        #[cfg(not(feature = "semantic"))]
        let _ = (id, content);
    }

    /// Return a reference to the underlying storage backend.
    ///
    /// This is exposed for callers that need direct access to storage
    /// operations (delete, clear, count) not covered by [`Self::assemble`]
    /// or [`Self::save_snapshot`].
    #[must_use]
    pub fn storage(&self) -> &dyn ContextStorage {
        self.storage.as_ref()
    }

    /// Backfill embeddings for all entries that do not yet have one.
    ///
    /// Entries are fetched and embedded in batches of `batch_size` (capped to
    /// at least 1). The `progress` callback is called after each batch with
    /// `(done_so_far, total)`.
    ///
    /// Returns the number of entries that were successfully embedded.
    ///
    /// # Errors
    ///
    /// Returns an error if fetching unembedded entries fails or if the
    /// embedding task panics.
    #[cfg(feature = "semantic")]
    pub async fn backfill_embeddings(
        &self,
        batch_size: usize,
        progress: impl Fn(usize, usize),
    ) -> Result<usize> {
        let embedder = if let Some(e) = &self.embedder {
            e.clone()
        } else {
            tracing::debug!("backfill_embeddings: no embedder configured");
            return Ok(0);
        };

        let all = self.storage.get_unembedded(usize::MAX).await?;
        let total = all.len();
        if total == 0 {
            tracing::debug!("backfill_embeddings: all entries already embedded");
            return Ok(0);
        }

        tracing::debug!(total = %total, batch_size = %batch_size, "backfill_embeddings: starting");
        let batch = batch_size.max(1);
        let mut n_embedded = 0usize;

        for chunk in all.chunks(batch) {
            let texts: Vec<String> = chunk.iter().map(|e| e.content.clone()).collect();
            let emb = embedder.clone();
            let embeddings = tokio::task::spawn_blocking(move || {
                let refs: Vec<&str> = texts.iter().map(String::as_str).collect();
                emb.embed_batch(&refs)
            })
            .await
            .map_err(|e| Error::Migration(format!("backfill embed task panicked: {e}")))?
            .map_err(|e| Error::Migration(format!("backfill embed failed: {e}")))?;

            for (entry, embedding) in chunk.iter().zip(embeddings.iter()) {
                if let Err(e) = self.storage.save_embedding(&entry.id, embedding).await {
                    tracing::warn!(id = %entry.id, error = %e, "backfill: save_embedding failed");
                } else {
                    n_embedded += 1;
                }
            }

            progress(n_embedded, total);
            tracing::debug!(done = %n_embedded, total = %total, "backfill_embeddings: batch done");
        }

        Ok(n_embedded)
    }
}

/// Current Unix timestamp in seconds.
///
/// Returns `0` if the system clock reports a time before the Unix epoch,
/// which should not happen on any supported platform.
fn current_timestamp() -> i64 {
    let secs = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |d| d.as_secs());
    i64::try_from(secs).unwrap_or(i64::MAX)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::{EvictionPolicy, DEFAULT_RECENCY_HALF_LIFE_SECS};
    use crate::entry::ScoredEntry;
    use async_trait::async_trait;
    use std::path::PathBuf;
    use std::sync::Mutex;

    // -----------------------------------------------------------------------
    // Mock implementations
    // -----------------------------------------------------------------------

    struct MockStorage {
        entries: Mutex<Vec<ContextEntry>>,
    }

    impl MockStorage {
        fn new() -> Self {
            Self {
                entries: Mutex::new(Vec::new()),
            }
        }
    }

    #[async_trait]
    impl ContextStorage for MockStorage {
        async fn save(&self, entry: &ContextEntry) -> Result<()> {
            self.entries.lock().unwrap().push(entry.clone());
            Ok(())
        }

        async fn get_top_k(&self, k: usize) -> Result<Vec<ContextEntry>> {
            let guard = self.entries.lock().unwrap();
            let mut sorted = guard.clone();
            sorted.sort_by_key(|e| std::cmp::Reverse(e.timestamp));
            sorted.truncate(k);
            Ok(sorted)
        }

        async fn get_all(&self) -> Result<Vec<ContextEntry>> {
            Ok(self.entries.lock().unwrap().clone())
        }

        async fn delete(&self, id: &str) -> Result<bool> {
            let mut guard = self.entries.lock().unwrap();
            let before = guard.len();
            guard.retain(|e| e.id != id);
            Ok(guard.len() < before)
        }

        async fn clear(&self) -> Result<usize> {
            let mut guard = self.entries.lock().unwrap();
            let n = guard.len();
            guard.clear();
            Ok(n)
        }

        async fn clear_scope(&self, scope: &str) -> Result<usize> {
            let mut guard = self.entries.lock().unwrap();
            let before = guard.len();
            guard.retain(|e| e.scope.as_deref() != Some(scope));
            Ok(before - guard.len())
        }

        async fn count(&self) -> Result<usize> {
            Ok(self.entries.lock().unwrap().len())
        }
    }

    struct MockSearcher {
        results: Mutex<Vec<ScoredEntry>>,
    }

    impl MockSearcher {
        fn new(results: Vec<ScoredEntry>) -> Self {
            Self {
                results: Mutex::new(results),
            }
        }

        fn empty() -> Self {
            Self::new(Vec::new())
        }
    }

    #[async_trait]
    impl Searcher for MockSearcher {
        async fn search(
            &self,
            _query: &str,
            _scope: Option<&str>,
            limit: usize,
        ) -> Result<Vec<ScoredEntry>> {
            let guard = self.results.lock().unwrap();
            Ok(guard.iter().take(limit).cloned().collect())
        }
    }

    fn default_config(max_entries: usize) -> Config {
        Config {
            max_entries,
            token_budget: 8192,
            db_path: PathBuf::from(":memory:"),
            eviction_policy: EvictionPolicy::Lru,
            recency_half_life_secs: DEFAULT_RECENCY_HALF_LIFE_SECS,
            ..Config::default()
        }
    }

    fn make_entry(id: &str, content: &str, timestamp: i64) -> ContextEntry {
        ContextEntry {
            id: id.into(),
            content: content.into(),
            timestamp,
            kind: crate::entry::kind::MANUAL.to_owned(),
            scope: None,
            session_id: None,
            token_count: Some(estimate_tokens(content)),
            metadata: None,
        }
    }

    fn make_scored(id: &str, content: &str, timestamp: i64, score: f64) -> ScoredEntry {
        ScoredEntry {
            entry: make_entry(id, content, timestamp),
            score,
        }
    }

    // -----------------------------------------------------------------------
    // Tests
    // -----------------------------------------------------------------------

    #[test]
    fn test_estimate_tokens() {
        assert_eq!(estimate_tokens(""), 0);
        assert_eq!(estimate_tokens("a"), 1); // (1+3)/4 = 1
        assert_eq!(estimate_tokens("ab"), 1); // (2+3)/4 = 1
        assert_eq!(estimate_tokens("abc"), 1); // (3+3)/4 = 1 (integer)
        assert_eq!(estimate_tokens("abcd"), 1); // (4+3)/4 = 1
        assert_eq!(estimate_tokens("abcde"), 2); // (5+3)/4 = 2
        assert_eq!(estimate_tokens("abcdefgh"), 2); // (8+3)/4 = 2 (integer)
        assert_eq!(estimate_tokens("hello world, this is a test"), 7);
    }

    #[test]
    fn test_engine_send_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<ContextEngine>();
    }

    #[tokio::test]
    async fn test_assemble_fits_budget() {
        let now = current_timestamp();
        let results = vec![
            make_scored("a", "short", now, 1.0),
            make_scored("b", "medium length text here", now, 0.9),
            make_scored("c", "another entry with more content inside", now, 0.8),
        ];

        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::new(results)),
            default_config(100),
        );

        // Budget of 2 tokens: "short" = 2 tokens fits exactly.
        let assembled = engine.assemble("test", None, 2).await.unwrap();
        assert_eq!(assembled.len(), 1);
        assert_eq!(assembled[0].id, "a");

        // Budget large enough for all.
        let assembled = engine.assemble("test", None, 1000).await.unwrap();
        assert_eq!(assembled.len(), 3);
    }

    #[tokio::test]
    async fn test_assemble_skips_oversized_entries() {
        // B1 regression: oversized top-ranked entry must not abort the loop.
        let now = current_timestamp();
        let big_content = "x".repeat(4000); // 1000 tokens
        let results = vec![
            make_scored("big", &big_content, now, 1.0),
            make_scored("small", "fits", now, 0.9),
        ];

        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::new(results)),
            default_config(100),
        );

        // Budget = 5: "big" (1000 tokens) is skipped, "fits" (1 token) is returned.
        let assembled = engine.assemble("test", None, 5).await.unwrap();
        assert_eq!(assembled.len(), 1);
        assert_eq!(assembled[0].id, "small");
    }

    #[tokio::test]
    async fn test_assemble_empty_results() {
        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::empty()),
            default_config(100),
        );
        let assembled = engine.assemble("anything", None, 1000).await.unwrap();
        assert!(assembled.is_empty());
    }

    #[tokio::test]
    async fn test_recency_weighting() {
        let now = current_timestamp();
        // Two entries with equal BM25 scores but different timestamps.
        let results = vec![
            make_scored("old", "old entry", now - 86_400, 1.0), // 24h old
            make_scored("new", "new entry", now, 1.0),          // just now
        ];

        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::new(results)),
            default_config(100),
        );

        let assembled = engine.assemble("test", None, 1000).await.unwrap();
        assert_eq!(assembled.len(), 2);
        // Newer entry should rank first due to higher recency score.
        assert_eq!(assembled[0].id, "new");
        assert_eq!(assembled[1].id, "old");
    }

    #[tokio::test]
    async fn test_save_snapshot_creates_entry() {
        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::empty()),
            default_config(100),
        );

        let id = engine
            .save_snapshot(
                "hello world",
                crate::entry::kind::MANUAL,
                &SaveOptions::default(),
            )
            .await
            .unwrap();
        assert!(!id.is_empty());

        // Verify the entry exists in storage.
        let all = engine.storage.get_all().await.unwrap();
        assert_eq!(all.len(), 1);
        assert_eq!(all[0].id, id);
        assert_eq!(all[0].content, "hello world");
        assert_eq!(all[0].token_count, Some(estimate_tokens("hello world")));
        assert_eq!(all[0].kind, crate::entry::kind::MANUAL);
    }

    #[tokio::test]
    async fn test_save_snapshot_populates_scope_and_metadata() {
        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::empty()),
            default_config(100),
        );

        let metadata = serde_json::json!({"source": "test"});
        let options = SaveOptions {
            session_id: Some("sess-1".to_owned()),
            scope: Some("project:homelab-rs".to_owned()),
            metadata: Some(metadata.clone()),
        };

        let id = engine
            .save_snapshot("hello scoped", crate::entry::kind::SNAPSHOT, &options)
            .await
            .unwrap();

        let all = engine.storage.get_all().await.unwrap();
        assert_eq!(all.len(), 1);
        assert_eq!(all[0].id, id);
        assert_eq!(all[0].kind, crate::entry::kind::SNAPSHOT);
        assert_eq!(all[0].scope.as_deref(), Some("project:homelab-rs"));
        assert_eq!(all[0].session_id.as_deref(), Some("sess-1"));
        assert_eq!(all[0].metadata, Some(metadata));
    }

    #[test]
    fn test_recency_decay_values() {
        let half_life = crate::config::DEFAULT_RECENCY_HALF_LIFE_HOURS * 3600.0;

        // At age 0, decay should be 1.0.
        let decay_0 = recency_decay(0.0, half_life);
        assert!((decay_0 - 1.0).abs() < f64::EPSILON);

        // At age = half_life, decay should be 0.5.
        let decay_half = recency_decay(half_life, half_life);
        assert!((decay_half - 0.5).abs() < 1e-10);

        // At age = 2 * half_life, decay should be 0.25.
        let decay_double = recency_decay(2.0 * half_life, half_life);
        assert!((decay_double - 0.25).abs() < 1e-10);
    }

    #[tokio::test]
    async fn test_save_snapshot_ids_are_unique() {
        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::empty()),
            default_config(100),
        );

        let id1 = engine
            .save_snapshot(
                "identical content",
                crate::entry::kind::SNAPSHOT,
                &SaveOptions::default(),
            )
            .await
            .unwrap();
        let id2 = engine
            .save_snapshot(
                "identical content",
                crate::entry::kind::SNAPSHOT,
                &SaveOptions::default(),
            )
            .await
            .unwrap();
        assert_ne!(
            id1, id2,
            "Two saves of identical content must produce distinct IDs"
        );
    }

    #[tokio::test]
    async fn test_save_empty_content_rejected() {
        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::empty()),
            default_config(100),
        );

        let result = engine
            .save_snapshot("", crate::entry::kind::MANUAL, &SaveOptions::default())
            .await;
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("content must not be empty"));
    }

    #[test]
    fn test_invalid_half_life_clamped_to_default() {
        let invalid_values: Vec<f64> = vec![
            0.0,
            -1.0,
            -100.0,
            f64::NAN,
            f64::INFINITY,
            f64::NEG_INFINITY,
        ];

        for value in invalid_values {
            let mut config = default_config(100);
            config.recency_half_life_secs = value;

            let engine = ContextEngine::new(
                Box::new(MockStorage::new()),
                Box::new(MockSearcher::empty()),
                config,
            );

            assert!(
                (engine.config.recency_half_life_secs - DEFAULT_RECENCY_HALF_LIFE_SECS).abs()
                    < f64::EPSILON,
                "half_life {value} should have been clamped to default",
            );
        }
    }

    #[tokio::test]
    async fn lexicon_scorer_boosts_important_entry_over_neutral() {
        use crate::lexicon::DefaultEnglishScorer;

        let now = current_timestamp();
        // Equal BM25 scores, equal timestamps — only differentiator is lexicon signal.
        let results = vec![
            make_scored("neutral", "something unrelated here", now, 1.0),
            make_scored("important", "confirmed, that is correct", now, 1.0),
        ];

        let scorer: Arc<dyn LexiconScorer> = Arc::new(DefaultEnglishScorer::default());
        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::new(results)),
            default_config(100),
        )
        .with_scorer(scorer);

        let assembled = engine.assemble("test", None, 1000).await.unwrap();
        assert_eq!(assembled.len(), 2);
        assert_eq!(
            assembled[0].id, "important",
            "lexicon-boosted entry should rank first"
        );
    }

    #[test]
    fn test_valid_half_life_preserved() {
        let mut config = default_config(100);
        config.recency_half_life_secs = 3600.0; // 1 hour — valid

        let engine = ContextEngine::new(
            Box::new(MockStorage::new()),
            Box::new(MockSearcher::empty()),
            config,
        );

        assert!((engine.config.recency_half_life_secs - 3600.0).abs() < f64::EPSILON);
    }
}