remem-ai 0.5.96

Persistent memory for Claude Code and OpenAI Codex coding agents
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
use anyhow::{Context, Result};
use rusqlite::{params, Connection};

use super::{search_with_branch_explain, search_with_branch_weights, SearchWeights};

fn setup_explain_conn() -> Result<Connection> {
    let conn = Connection::open_in_memory()?;
    crate::memory::tests_helper::setup_memory_schema(&conn);
    Ok(conn)
}

struct ExplainMemory<'a> {
    id: i64,
    project: &'a str,
    title: &'a str,
    content: &'a str,
    scope: &'a str,
    updated_at_epoch: i64,
}

fn insert_explain_memory(conn: &Connection, memory: &ExplainMemory<'_>) -> Result<()> {
    conn.execute(
        "INSERT INTO memories
         (id, session_id, project, topic_key, title, content, memory_type, files,
          created_at_epoch, updated_at_epoch, status, branch, scope)
         VALUES (?1, ?2, ?3, NULL, ?4, ?5, 'decision', NULL, ?6, ?6, 'active', NULL, ?7)",
        params![
            memory.id,
            format!("session-{}", memory.id),
            memory.project,
            memory.title,
            memory.content,
            memory.updated_at_epoch,
            memory.scope,
        ],
    )?;
    Ok(())
}

#[test]
fn search_explain_reports_channels_scores_and_visibility() -> Result<()> {
    let conn = setup_explain_conn()?;
    let now = chrono::Utc::now().timestamp();
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 1,
            project: "/repo",
            title: "Recently SQLite project fix",
            content: "recently SQLite project migration fix",
            scope: "project",
            updated_at_epoch: now - 100,
        },
    )?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 2,
            project: "/elsewhere",
            title: "Recently SQLite global preference",
            content: "recently SQLite global preference",
            scope: "global",
            updated_at_epoch: now - 90,
        },
    )?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 3,
            project: "/repo",
            title: "Recently unrelated note",
            content: "recently unrelated note",
            scope: "project",
            updated_at_epoch: now - 80,
        },
    )?;
    crate::retrieval::entity::link_entities(&conn, 1, &["SQLite".to_string()])?;
    crate::retrieval::entity::link_entities(&conn, 2, &["SQLite".to_string()])?;

    let (memories, explain) = search_with_branch_explain(
        &conn,
        Some("recently SQLite"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
    )?;
    let explain = explain.context("query explain should be present")?;

    assert!(!memories.is_empty());
    for expected in ["fts", "entity", "temporal", "vector", "like_fallback"] {
        assert!(
            explain
                .channels
                .iter()
                .any(|channel| channel.name == expected),
            "{expected} channel missing from {:#?}",
            explain.channels
        );
    }
    assert_eq!(explain.rrf_k, 60.0);
    assert!(explain
        .fts_query
        .as_deref()
        .unwrap_or("")
        .contains("SQLite"));
    assert!(explain.temporal_range.is_some());
    assert!(explain
        .results
        .iter()
        .any(|result| result.visibility == "global-overlay"));
    assert!(explain.results.iter().all(|result| {
        result.staleness.status == "active"
            && result.staleness.age == "fresh"
            && result.staleness.source_anchor == "untracked"
            && result.staleness.label.contains("source_anchor=untracked")
    }));
    let like = explain
        .channels
        .iter()
        .find(|channel| channel.name == "like_fallback")
        .context("like_fallback channel should be reported")?;
    assert!(!like.enabled);
    assert!(like
        .disabled_reason
        .as_deref()
        .unwrap_or("")
        .contains("stronger retrieval channels returned hits"));
    assert!(explain.results.iter().all(|result| {
        result
            .contributions
            .iter()
            .all(|contribution| contribution.channel != "like_fallback")
    }));
    assert!(explain.results.iter().all(|result| {
        !result.contributions.is_empty()
            && result
                .contributions
                .iter()
                .all(|contribution| contribution.score > 0.0)
    }));
    Ok(())
}

#[test]
fn like_fallback_only_participates_when_stronger_channels_are_empty() -> Result<()> {
    let conn = setup_explain_conn()?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 1,
            project: "/repo",
            title: "DB schema migration",
            content: "Updated AI model",
            scope: "project",
            updated_at_epoch: 100,
        },
    )?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 2,
            project: "/repo",
            title: "Other topic entirely",
            content: "Nothing relevant",
            scope: "project",
            updated_at_epoch: 90,
        },
    )?;

    let (memories, explain) =
        search_with_branch_explain(&conn, Some("DB"), Some("/repo"), None, 5, 0, false, None)?;
    let explain = explain.context("query explain should be present")?;

    assert_eq!(memories.first().map(|memory| memory.id), Some(1));
    let like = explain
        .channels
        .iter()
        .find(|channel| channel.name == "like_fallback")
        .context("like_fallback channel should be reported")?;
    assert!(like.enabled, "{like:#?}");
    assert_eq!(like.hits.first().map(|hit| hit.memory_id), Some(1));
    let result = explain
        .results
        .iter()
        .find(|result| result.memory_id == 1)
        .context("LIKE fallback result should be explained")?;
    assert!(result
        .contributions
        .iter()
        .any(|contribution| contribution.channel == "like_fallback" && contribution.score > 0.0));
    Ok(())
}

#[test]
fn semantic_vector_channel_recalls_paraphrase_without_lexical_overlap() -> Result<()> {
    let conn = setup_explain_conn()?;
    let id = crate::memory::insert_memory(
        &conn,
        Some("s1"),
        "/repo",
        Some("credential-storage"),
        "Credential store",
        "SQLCipher encrypts secrets at rest.",
        "architecture",
        None,
    )?;

    let (memories, explain) = search_with_branch_explain(
        &conn,
        Some("How do we protect private persisted data?"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
    )?;
    let explain = explain.context("query explain should be present")?;

    assert_eq!(memories.first().map(|memory| memory.id), Some(id));
    let result = explain
        .results
        .iter()
        .find(|result| result.memory_id == id)
        .context("expected vector-recalled memory in explain results")?;
    assert!(
        result
            .contributions
            .iter()
            .any(|contribution| contribution.channel == "vector"),
        "{result:#?}"
    );
    Ok(())
}

#[test]
fn usage_weight_preserves_vector_only_confidence_gate() -> Result<()> {
    let conn = setup_explain_conn()?;
    let id = crate::memory::insert_memory(
        &conn,
        Some("s1"),
        "/repo",
        Some("credential-storage"),
        "Credential store",
        "SQLCipher encrypts secrets at rest.",
        "architecture",
        None,
    )?;
    conn.execute(
        "UPDATE memories
         SET access_count = 8,
             last_accessed_epoch = ?1
         WHERE id = ?2",
        params![chrono::Utc::now().timestamp(), id],
    )?;

    let memories = search_with_branch_weights(
        &conn,
        Some("How do we protect private persisted data?"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
        SearchWeights {
            usage: 1.0,
            ..SearchWeights::default()
        },
    )?;

    assert!(
        memories.iter().any(|memory| memory.id == id),
        "usage must not make vector-only evidence fail the confidence gate: {memories:#?}"
    );
    Ok(())
}

#[test]
fn search_abstains_when_entity_match_lacks_claim_evidence() -> Result<()> {
    let conn = setup_explain_conn()?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 1,
            project: "synthetic/kestrelnook",
            title: "Kestrelnook Nebulalatch Owner",
            content: "NebulaLatch is owned by Team Mica.",
            scope: "project",
            updated_at_epoch: 100,
        },
    )?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 2,
            project: "synthetic/kestrelnook",
            title: "Kestrelnook Nebulalatch Quorum Current",
            content: "current NebulaLatch quorum is 7.",
            scope: "project",
            updated_at_epoch: 90,
        },
    )?;
    for id in [1, 2] {
        crate::retrieval::entity::link_entities(
            &conn,
            id,
            &["KestrelNook".to_string(), "NebulaLatch".to_string()],
        )?;
    }

    let (memories, explain) = search_with_branch_explain(
        &conn,
        Some("Has Project KestrelNook migrated NebulaLatch to Oracle Cloud?"),
        Some("synthetic/kestrelnook"),
        None,
        5,
        0,
        false,
        None,
    )?;
    let explain = explain.context("query explain should be present")?;

    assert!(memories.is_empty(), "{memories:#?}");
    assert!(
        explain.filtered_result_count > 0,
        "entity/FTS candidates should be filtered by evidence gate: {explain:#?}"
    );
    assert!(explain.claim_terms.iter().any(|term| term == "migrated"));
    Ok(())
}

#[test]
fn evidence_gate_preserves_entity_match_with_supported_claim() -> Result<()> {
    let conn = setup_explain_conn()?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 1,
            project: "synthetic/kestrelnook",
            title: "Kestrelnook Nebulalatch Quorum Current",
            content: "current NebulaLatch quorum is 7.",
            scope: "project",
            updated_at_epoch: 100,
        },
    )?;
    crate::retrieval::entity::link_entities(
        &conn,
        1,
        &["KestrelNook".to_string(), "NebulaLatch".to_string()],
    )?;

    let (memories, explain) = search_with_branch_explain(
        &conn,
        Some("Current NebulaLatch quorum for Project kestrelnook?"),
        Some("synthetic/kestrelnook"),
        None,
        5,
        0,
        false,
        None,
    )?;
    let explain = explain.context("query explain should be present")?;

    assert_eq!(memories.first().map(|memory| memory.id), Some(1));
    assert_eq!(explain.filtered_result_count, 0);
    let result = explain
        .results
        .iter()
        .find(|result| result.memory_id == 1)
        .context("expected retained result in explain")?;
    assert!(result.evidence_confidence >= explain.min_evidence_confidence);
    assert!(explain.claim_terms.iter().any(|term| term == "quorum"));
    Ok(())
}

#[test]
fn evidence_gate_preserves_family_relation_aliases() -> Result<()> {
    let conn = setup_explain_conn()?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 1,
            project: "personal",
            title: "Family update from Melanie",
            content: "Melanie mentioned her son Tom and her daughter Sarah.",
            scope: "project",
            updated_at_epoch: 100,
        },
    )?;
    crate::retrieval::entity::link_entities(
        &conn,
        1,
        &[
            "Melanie".to_string(),
            "Tom".to_string(),
            "Sarah".to_string(),
        ],
    )?;

    let (memories, explain) = search_with_branch_explain(
        &conn,
        Some("Melanie kids"),
        Some("personal"),
        None,
        5,
        0,
        false,
        None,
    )?;
    let explain = explain.context("query explain should be present")?;

    assert_eq!(memories.first().map(|memory| memory.id), Some(1));
    assert!(explain.claim_terms.iter().any(|term| term == "kids"));
    let result = explain
        .results
        .iter()
        .find(|result| result.memory_id == 1)
        .context("expected retained family relation result")?;
    assert!(result.evidence_confidence >= explain.min_evidence_confidence);
    Ok(())
}

#[test]
fn fact_channel_recalls_source_memory_without_lexical_overlap() -> Result<()> {
    let conn = Connection::open_in_memory()?;
    crate::migrate::run_migrations(&conn)?;
    let now = chrono::Utc::now().timestamp();
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 1,
            project: "/repo",
            title: "Signer fact source",
            content: "Signer details live in the temporal fact layer.",
            scope: "project",
            updated_at_epoch: now - 100,
        },
    )?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 2,
            project: "/repo",
            title: "Stale signer fact source",
            content: "Old signer details live outside the searchable text.",
            scope: "project",
            updated_at_epoch: now - 90,
        },
    )?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 3,
            project: "/repo",
            title: "Partial fact source",
            content: "Another active fact source for a different topic.",
            scope: "project",
            updated_at_epoch: now - 80,
        },
    )?;
    conn.execute(
        "INSERT INTO memory_facts
         (project, subject, predicate, object, valid_from_epoch, valid_to_epoch,
          learned_at_epoch, source_memory_id, source_observation_id, source_event_ids,
          confidence, supersedes_fact_id, status, invalidated_at_epoch,
          created_at_epoch, updated_at_epoch)
         VALUES ('/repo', 'HarborMint', 'verified_by', 'Toma Reed', ?1, ?2, ?3, 1,
                 NULL, '[]', 0.95, NULL, 'active', NULL, ?3, ?3)",
        params![now - 1_000, now + 1_000, now - 900],
    )?;
    conn.execute(
        "INSERT INTO memory_facts
         (project, subject, predicate, object, valid_from_epoch, valid_to_epoch,
          learned_at_epoch, source_memory_id, source_observation_id, source_event_ids,
          confidence, supersedes_fact_id, status, invalidated_at_epoch,
          created_at_epoch, updated_at_epoch)
         VALUES ('/repo', 'HarborMint', 'verified_by', 'Toma Reed', ?1, ?2, ?3, 2,
                 NULL, '[]', 0.95, NULL, 'stale', ?4, ?3, ?3)",
        params![now - 1_000, now + 1_000, now - 800, now - 10],
    )?;
    conn.execute(
        "INSERT INTO memory_facts
         (project, subject, predicate, object, valid_from_epoch, valid_to_epoch,
          learned_at_epoch, source_memory_id, source_observation_id, source_event_ids,
          confidence, supersedes_fact_id, status, invalidated_at_epoch,
          created_at_epoch, updated_at_epoch)
         VALUES ('/repo', 'HarborMint', 'verified_by', 'Mira Lane', ?1, ?2, ?3, 3,
                 NULL, '[]', 0.95, NULL, 'active', NULL, ?3, ?3)",
        params![now - 1_000, now + 1_000, now - 700],
    )?;

    let (memories, explain) = search_with_branch_explain(
        &conn,
        Some("Who signs HarborMint with Toma Reed?"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
    )?;
    let explain = explain.context("query explain should be present")?;

    assert_eq!(memories.first().map(|memory| memory.id), Some(1));
    assert!(
        memories[0].text.contains("Temporal facts:"),
        "{memories:#?}"
    );
    assert!(memories[0]
        .text
        .contains("HarborMint verified_by Toma Reed"));
    let fact = explain
        .channels
        .iter()
        .find(|channel| channel.name == "fact")
        .context("fact channel should be reported")?;
    assert!(fact.enabled, "{fact:#?}");
    assert_eq!(fact.hits.first().map(|hit| hit.memory_id), Some(1));
    assert!(!fact.hits.iter().any(|hit| hit.memory_id == 2));
    assert!(!fact.hits.iter().any(|hit| hit.memory_id == 3));
    let result = explain
        .results
        .iter()
        .find(|result| result.memory_id == 1)
        .context("expected fact-recalled result")?;
    assert!(result
        .contributions
        .iter()
        .any(|contribution| contribution.channel == "fact" && contribution.score > 0.0));
    assert_eq!(explain.filtered_result_count, 0);
    Ok(())
}

#[test]
fn fact_evidence_survives_when_text_channels_also_match() -> Result<()> {
    let conn = Connection::open_in_memory()?;
    crate::migrate::run_migrations(&conn)?;
    let now = chrono::Utc::now().timestamp();
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 1,
            project: "/repo",
            title: "HarborMint signer source",
            content: "Structured fact source without the verifier name.",
            scope: "project",
            updated_at_epoch: now - 100,
        },
    )?;
    crate::retrieval::entity::link_entities(&conn, 1, &["HarborMint".to_string()])?;
    conn.execute(
        "INSERT INTO memory_facts
         (project, subject, predicate, object, valid_from_epoch, valid_to_epoch,
          learned_at_epoch, source_memory_id, source_observation_id, source_event_ids,
          confidence, supersedes_fact_id, status, invalidated_at_epoch,
          created_at_epoch, updated_at_epoch)
         VALUES ('/repo', 'HarborMint', 'verified_by', 'Toma Reed', ?1, ?2, ?3, 1,
                 NULL, '[]', 0.95, NULL, 'active', NULL, ?3, ?3)",
        params![now - 1_000, now + 1_000, now - 900],
    )?;

    let (memories, explain) = search_with_branch_explain(
        &conn,
        Some("Who verified HarborMint with Toma Reed?"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
    )?;
    let explain = explain.context("query explain should be present")?;

    assert_eq!(memories.first().map(|memory| memory.id), Some(1));
    let result = explain
        .results
        .iter()
        .find(|result| result.memory_id == 1)
        .context("fact and text channel result should survive gate")?;
    assert!(result
        .contributions
        .iter()
        .any(|contribution| contribution.channel == "fact"));
    assert_eq!(explain.filtered_result_count, 0);
    Ok(())
}

#[test]
fn zero_fact_weight_disables_fact_only_results() -> Result<()> {
    let conn = Connection::open_in_memory()?;
    crate::migrate::run_migrations(&conn)?;
    let now = chrono::Utc::now().timestamp();
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 1,
            project: "/repo",
            title: "Opaque source",
            content: "Details live only in structured facts.",
            scope: "project",
            updated_at_epoch: now - 100,
        },
    )?;
    conn.execute(
        "INSERT INTO memory_facts
         (project, subject, predicate, object, valid_from_epoch, valid_to_epoch,
          learned_at_epoch, source_memory_id, source_observation_id, source_event_ids,
          confidence, supersedes_fact_id, status, invalidated_at_epoch,
          created_at_epoch, updated_at_epoch)
         VALUES ('/repo', 'HarborMint', 'verified_by', 'Toma Reed', ?1, ?2, ?3, 1,
                 NULL, '[]', 0.95, NULL, 'active', NULL, ?3, ?3)",
        params![now - 1_000, now + 1_000, now - 900],
    )?;

    let disabled = search_with_branch_weights(
        &conn,
        Some("Who signs HarborMint with Toma Reed?"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
        SearchWeights {
            fact: 0.0,
            max_vector_distance: 0.0,
            min_evidence_confidence: 0.0,
            ..SearchWeights::default()
        },
    )?;
    let enabled = search_with_branch_weights(
        &conn,
        Some("Who signs HarborMint with Toma Reed?"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
        SearchWeights {
            max_vector_distance: 0.0,
            min_evidence_confidence: 0.0,
            ..SearchWeights::default()
        },
    )?;

    assert!(disabled.is_empty());
    assert_eq!(enabled.first().map(|memory| memory.id), Some(1));
    Ok(())
}

#[test]
fn usage_weight_reranks_only_retrieved_candidates() -> Result<()> {
    let conn = setup_explain_conn()?;
    let now = chrono::Utc::now().timestamp();
    for memory in [
        ExplainMemory {
            id: 1,
            project: "/repo",
            title: "SQLite timeout old path",
            content: "SQLite timeout fix should update busy_timeout.",
            scope: "project",
            updated_at_epoch: now - 100,
        },
        ExplainMemory {
            id: 2,
            project: "/repo",
            title: "SQLite timeout proven path",
            content: "SQLite timeout fix should update busy_timeout.",
            scope: "project",
            updated_at_epoch: now - 90,
        },
        ExplainMemory {
            id: 3,
            project: "/repo",
            title: "Popular unrelated note",
            content: "Unrelated launch checklist for release paperwork.",
            scope: "project",
            updated_at_epoch: now - 80,
        },
    ] {
        insert_explain_memory(&conn, &memory)?;
    }
    conn.execute(
        "UPDATE memories
         SET access_count = CASE id WHEN 1 THEN 1 WHEN 2 THEN 25 WHEN 3 THEN 100 END,
             last_accessed_epoch = CASE id WHEN 1 THEN ?1 WHEN 2 THEN ?2 WHEN 3 THEN ?2 END
         WHERE id IN (1, 2, 3)",
        params![now - 90 * 86_400, now],
    )?;

    let ranked = search_with_branch_weights(
        &conn,
        Some("SQLite timeout busy_timeout"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
        SearchWeights {
            usage: 10.0,
            max_vector_distance: 0.0,
            min_evidence_confidence: 0.0,
            ..SearchWeights::default()
        },
    )?;

    assert_eq!(ranked.first().map(|memory| memory.id), Some(2));
    assert!(
        !ranked.iter().any(|memory| memory.id == 3),
        "usage must not retrieve memories absent from text/vector/fact/entity candidates: {ranked:#?}"
    );
    Ok(())
}

#[test]
fn zero_fact_weight_does_not_block_like_fallback() -> Result<()> {
    let conn = Connection::open_in_memory()?;
    crate::migrate::run_migrations(&conn)?;
    let now = chrono::Utc::now().timestamp();
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 1,
            project: "/repo",
            title: "Opaque ticket fact",
            content: "Structured ticket detail only.",
            scope: "project",
            updated_at_epoch: now - 100,
        },
    )?;
    insert_explain_memory(
        &conn,
        &ExplainMemory {
            id: 2,
            project: "/repo",
            title: "PR 12 text note",
            content: "PR 12 is documented in searchable text.",
            scope: "project",
            updated_at_epoch: now - 200,
        },
    )?;
    conn.execute(
        "INSERT INTO memory_facts
         (project, subject, predicate, object, valid_from_epoch, valid_to_epoch,
          learned_at_epoch, source_memory_id, source_observation_id, source_event_ids,
          confidence, supersedes_fact_id, status, invalidated_at_epoch,
          created_at_epoch, updated_at_epoch)
         VALUES ('/repo', 'PR', 'affects_project', '12', ?1, NULL, ?2, 1,
                 NULL, '[]', 0.95, NULL, 'active', NULL, ?2, ?2)",
        params![now - 1_000, now - 900],
    )?;

    let memories = search_with_branch_weights(
        &conn,
        Some("PR 12"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
        SearchWeights {
            fact: 0.0,
            max_vector_distance: 0.0,
            min_evidence_confidence: 0.0,
            ..SearchWeights::default()
        },
    )?;

    assert_eq!(memories.first().map(|memory| memory.id), Some(2));
    Ok(())
}

#[test]
fn search_explain_reports_disabled_vector_channel_when_table_is_missing() -> Result<()> {
    let conn = setup_explain_conn()?;
    conn.execute("DROP TABLE memory_embeddings", [])?;

    let (_memories, explain) = search_with_branch_explain(
        &conn,
        Some("semantic recall"),
        Some("/repo"),
        None,
        5,
        0,
        false,
        None,
    )?;
    let explain = explain.context("query explain should be present")?;
    let vector = explain
        .channels
        .iter()
        .find(|channel| channel.name == "vector")
        .context("vector channel should be reported")?;

    assert!(!vector.enabled);
    assert!(vector
        .disabled_reason
        .as_deref()
        .unwrap_or("")
        .contains("memory_embeddings table is missing"));
    Ok(())
}