khive-pack-memory 0.5.0

Memory verb pack — remember/recall semantics with decay-aware ranking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
//! Shared types, utilities, and pipeline helpers for the memory verb handlers.
//! See `crates/khive-pack-memory/docs/api/recall-pipeline.md`.

use std::collections::{HashMap, HashSet};
use std::sync::atomic::AtomicU64;
use std::time::Instant;

use serde::Deserialize;
use serde_json::Value;
use uuid::Uuid;

use khive_fusion::FusionStrategy;
use khive_retrieval::{fuse_search_results, HybridConfig};
use khive_runtime::{
    fts_text_leg_or_err, MemoryRecallPipeline, NamespaceToken, NoteCandidate, RuntimeError,
    SearchHit, SearchSource, VerbRegistry,
};
use khive_score::DeterministicScore;
use khive_storage::types::{
    PageRequest, TextFilter, TextQueryMode, TextSearchHit, TextSearchRequest, VectorSearchHit,
    VectorSearchRequest,
};
use khive_storage::EntityFilter;
use khive_types::SubstrateKind;

use crate::ann::{self, AnnKey};
use crate::config::{RecallConfig, ScoreBreakdown, WeightedContributions};
use crate::query_cache::QueryEmbeddingCache;
use crate::MemoryPack;

// KHIVE_RECALL_PROFILE emits per-call stage JSON to stderr.
pub(super) static RECALL_CALL_ID: AtomicU64 = AtomicU64::new(0);

thread_local! {
    pub(super) static PROF_CID: std::cell::Cell<u64> = const { std::cell::Cell::new(0) };
}

pub(super) fn recall_profile_enabled() -> bool {
    static ENABLED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
    *ENABLED.get_or_init(|| {
        let enabled = std::env::var("KHIVE_RECALL_PROFILE").is_ok();
        khive_runtime::config_ledger::record_config_locked(
            "KHIVE_RECALL_PROFILE",
            enabled.to_string(),
        );
        enabled
    })
}

pub(super) fn ann_overfetch_max_rounds() -> usize {
    static ROUNDS: std::sync::OnceLock<usize> = std::sync::OnceLock::new();
    *ROUNDS.get_or_init(|| {
        let rounds = std::env::var("ANN_OVERFETCH_MAX_ROUNDS")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(3);
        khive_runtime::config_ledger::record_config_locked(
            "ANN_OVERFETCH_MAX_ROUNDS",
            rounds.to_string(),
        );
        rounds
    })
}

/// Return the cached ANN readiness timeout, defaulting to 8 seconds.
/// See `crates/khive-pack-memory/docs/api/configuration.md`.
pub(super) fn ann_ready_timeout_ms() -> u64 {
    static TIMEOUT_MS: std::sync::OnceLock<u64> = std::sync::OnceLock::new();
    *TIMEOUT_MS.get_or_init(|| {
        const DEFAULT_ANN_READY_TIMEOUT_MS: u64 = 8_000;
        let ms = std::env::var("KHIVE_MEMORY_ANN_READY_TIMEOUT_MS")
            .ok()
            .and_then(|v| v.parse().ok())
            .unwrap_or(DEFAULT_ANN_READY_TIMEOUT_MS);
        khive_runtime::config_ledger::record_config_locked(
            "KHIVE_MEMORY_ANN_READY_TIMEOUT_MS",
            ms.to_string(),
        );
        ms
    })
}

#[inline(always)]
pub(super) fn plog(call_id: u64, stage: &str, us: u128) {
    eprintln!(r#"{{"c":{},"s":"{}","us":{}}}"#, call_id, stage, us);
}

#[inline(always)]
pub(super) fn plog_n(call_id: u64, stage: &str, us: u128, n: usize) {
    eprintln!(
        r#"{{"c":{},"s":"{}","us":{},"n":{}}}"#,
        call_id, stage, us, n
    );
}

/// Embed one model/query pair with the instruction-aware runtime path and local LRU.
pub(super) async fn embed_query_model(
    runtime: khive_runtime::KhiveRuntime,
    cache: QueryEmbeddingCache,
    model_name: String,
    query: String,
) -> Result<(String, Vec<f32>), RuntimeError> {
    if let Some(v) = cache.get(&model_name, &query) {
        return Ok((model_name, v));
    }
    let handle = tokio::runtime::Handle::current();
    let model_name_blk = model_name.clone();
    let query_blk = query.clone();
    let v = tokio::task::spawn_blocking(move || {
        handle.block_on(runtime.embed_query_with_model(&model_name_blk, &query_blk))
    })
    .await
    .map_err(|e| RuntimeError::Internal(format!("recall embed task panicked: {e}")))??;
    cache.put(&model_name, &query, v.clone());
    Ok((model_name, v))
}

pub(super) fn to_json<T: serde::Serialize>(v: &T) -> Result<Value, RuntimeError> {
    serde_json::to_value(v).map_err(|e| RuntimeError::InvalidInput(e.to_string()))
}

pub(super) fn deser<T: serde::de::DeserializeOwned>(params: Value) -> Result<T, RuntimeError> {
    serde_json::from_value(params).map_err(|e| RuntimeError::InvalidInput(e.to_string()))
}

pub(super) fn validate_memory_type(mt: &str) -> Result<(), RuntimeError> {
    match mt {
        "episodic" | "semantic" => Ok(()),
        other => Err(RuntimeError::InvalidInput(format!(
            "unknown memory_type {other:?}; valid: episodic | semantic"
        ))),
    }
}

pub(super) fn parse_fusion_strategy_str(s: &str) -> Result<FusionStrategy, RuntimeError> {
    match s {
        "rrf" => Ok(FusionStrategy::Rrf { k: 60 }),
        "weighted" => Ok(FusionStrategy::Weighted {
            weights: vec![0.7, 0.3],
        }),
        "union" => Ok(FusionStrategy::Union),
        "vector_only" => Ok(FusionStrategy::VectorOnly),
        "keyword_only" => Ok(FusionStrategy::KeywordOnly),
        other => Err(RuntimeError::InvalidInput(format!(
            "invalid fusion_strategy {other:?}: must be one of \"rrf\", \"weighted\", \"union\", \"vector_only\", \"keyword_only\""
        ))),
    }
}

/// Resolve an explicit or actor-plus-namespace recall profile; omit global fallback.
pub(super) async fn resolve_serving_profile(
    brain_profile: &Option<String>,
    token: &NamespaceToken,
    registry: &VerbRegistry,
) -> Option<String> {
    if let Some(profile_id) = brain_profile {
        return Some(profile_id.clone());
    }
    let ns = token.namespace().as_str().to_string();
    // Actor identity is required for actor-scoped, not merely namespace-scoped, bindings.
    let actor = token.actor().binding_id();
    khive_brain_core::resolve_consumer_profile(
        registry,
        actor,
        &ns,
        khive_brain_core::ConsumerKind::Recall,
    )
    .await
}

/// Maximum entity posteriors restored from a served profile snapshot.
pub(super) const PROFILE_STATE_ENTITY_CAPACITY: usize = 10_000;

/// Reconstruct profile state, returning `None` for absent or malformed snapshots.
pub(super) fn balanced_recall_state_from_profile_response(
    resp: &Value,
) -> Option<khive_brain_core::BalancedRecallState> {
    let snap_val = resp.get("state_snapshot")?;
    if snap_val.is_null() {
        return None;
    }
    let snapshot: khive_brain_core::BalancedRecallSnapshot =
        serde_json::from_value(snap_val.clone()).ok()?;
    Some(khive_brain_core::BalancedRecallState::from_snapshot(
        snapshot,
        PROFILE_STATE_ENTITY_CAPACITY,
    ))
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct RememberParams {
    pub(super) content: String,
    pub(super) memory_type: Option<String>,
    pub(super) salience: Option<f64>,
    #[serde(alias = "decay")]
    pub(super) decay_factor: Option<f64>,
    #[serde(alias = "source")]
    pub(super) source_id: Option<String>,
    pub(super) tags: Option<Vec<String>>,
    #[serde(default)]
    pub(super) embedding_model: Option<String>,
    /// Exact namespace override; otherwise episodic uses actor scope and semantic uses local.
    #[serde(default)]
    pub(super) namespace: Option<String>,
}

/// Tag filter mode: `any` = OR, `all` = AND.
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub(super) enum TagMode {
    #[default]
    Any,
    All,
}

pub(super) fn note_matches_tags(props: Option<&Value>, expected: &[String], mode: TagMode) -> bool {
    let Some(stored) = props
        .and_then(|p| p.get("tags"))
        .and_then(|tags| tags.as_array())
    else {
        return false;
    };
    let stored: HashSet<&str> = stored.iter().filter_map(Value::as_str).collect();
    match mode {
        TagMode::Any => expected.iter().any(|tag| stored.contains(tag.as_str())),
        TagMode::All => expected.iter().all(|tag| stored.contains(tag.as_str())),
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct RecallParams {
    pub(super) query: String,
    pub(super) limit: Option<u32>,
    pub(super) memory_type: Option<String>,
    pub(super) min_score: Option<f64>,
    pub(super) min_salience: Option<f64>,
    pub(super) config: Option<RecallConfig>,
    pub(super) top_k: Option<usize>,
    pub(super) fusion_strategy: Option<String>,
    pub(super) score_floor: Option<f32>,
    #[serde(default)]
    pub(super) embedding_model: Option<String>,
    #[serde(default)]
    pub(super) include_breakdown: Option<bool>,
    #[serde(default)]
    pub(super) tags: Option<Vec<String>>,
    #[serde(default)]
    pub(super) tag_mode: TagMode,
    /// Entity names to boost in scoring.
    #[serde(default)]
    pub(super) entity_names: Option<Vec<String>>,
    #[serde(default)]
    pub(super) full_content: Option<bool>,
    /// Serving-profile override; unknown IDs are per-operation errors.
    #[serde(default)]
    pub(super) profile_id: Option<String>,
    /// Exact read namespace; invalid values error and direct callers receive defense-in-depth.
    #[serde(default)]
    pub(super) namespace: Option<String>,
}

impl RecallParams {
    pub(super) fn effective_config(&self, base: RecallConfig) -> RecallConfig {
        let mut cfg = self.config.clone().unwrap_or(base);
        if let Some(ms) = self.min_score {
            cfg.min_score = ms;
        }
        if let Some(ms) = self.min_salience {
            cfg.min_salience = ms;
        }
        cfg
    }
}

/// Normalize a raw fusion score to the [0, 1] range.
pub(super) fn normalize_relevance(raw: f64, strategy: &FusionStrategy) -> f64 {
    match strategy {
        FusionStrategy::Rrf { k } => (raw * (*k as f64 + 1.0)).min(1.0),
        _ => raw,
    }
}

/// Salience amplifier exponent applied to `effective_salience` in `compute_score`.
pub(super) const SALIENCE_AMPLIFIER_ALPHA: f64 = 1.5;

/// Default salience for episodic memories (session events; decay quickly).
pub(super) const DEFAULT_SALIENCE_EPISODIC: f64 = 0.3;
/// Default salience for semantic memories (durable facts; stronger base weight).
pub(super) const DEFAULT_SALIENCE_SEMANTIC: f64 = 0.5;
/// Default decay_factor for episodic memories (~35-day half-life).
pub(super) const DEFAULT_DECAY_EPISODIC: f64 = 0.02;
/// Default decay_factor for semantic memories (~139-day half-life).
pub(super) const DEFAULT_DECAY_SEMANTIC: f64 = 0.005;

pub(super) fn compute_score(
    cfg: &RecallConfig,
    pipeline: &MemoryRecallPipeline,
    raw_relevance: f64,
    salience: f64,
    decay_factor: f64,
    age_days: f64,
) -> (f64, ScoreBreakdown) {
    let relevance = normalize_relevance(raw_relevance, &cfg.fuse_strategy);

    let effective_salience = cfg.decay_model.apply(
        salience,
        age_days,
        decay_factor,
        cfg.temporal_half_life_days,
    );
    let temporal = {
        let k = std::f64::consts::LN_2 / cfg.temporal_half_life_days;
        (-k * age_days).exp()
    };

    use uuid::Uuid;
    let candidate = NoteCandidate {
        id: Uuid::nil(),
        rrf_score: Some(relevance),
        salience,
        decay_factor,
        age_days,
        effective_salience,
        rerank_scores: std::collections::HashMap::new(),
    };

    let total = pipeline.score(&candidate);

    let weight_sum = cfg.relevance_weight + cfg.salience_weight + cfg.temporal_weight;
    let norm = if weight_sum > 0.0 { weight_sum } else { 1.0 };
    let amplified_salience = effective_salience.powf(SALIENCE_AMPLIFIER_ALPHA);
    let r_contrib = cfg.relevance_weight * relevance / norm;
    let i_contrib = cfg.salience_weight * amplified_salience / norm;
    let t_contrib = cfg.temporal_weight * temporal / norm;

    let breakdown = ScoreBreakdown {
        relevance,
        salience_raw: salience,
        salience_decayed: effective_salience,
        temporal,
        weighted: WeightedContributions {
            relevance_contribution: r_contrib,
            salience_contribution: i_contrib,
            temporal_contribution: t_contrib,
        },
        // ADR-104 §3: set to neutral/absent here; `handle_recall` overwrites
        // both when a profile served the request (component 1 ran).
        profile_component: 1.0,
        entity_posterior_mean: None,
    };
    (total, breakdown)
}

/// Build a `MemoryRecallPipeline` from a `RecallConfig`.
pub(super) fn make_pipeline(cfg: &RecallConfig) -> MemoryRecallPipeline {
    MemoryRecallPipeline::new(
        cfg.relevance_weight,
        cfg.salience_weight,
        cfg.temporal_weight,
        cfg.temporal_half_life_days,
        SALIENCE_AMPLIFIER_ALPHA,
    )
}

pub(super) struct RecallCandidateSet {
    pub(super) namespace: String,
    pub(super) text_hits: Vec<TextSearchHit>,
    /// One entry per embedding model: (model_name, hits). These have already been
    /// filtered to the caller's visible namespace set via over-fetch + post-filter.
    pub(super) vector_hits_per_model: Vec<(String, Vec<VectorSearchHit>)>,
    /// True when multilingual dense routing was requested AND a multilingual model was found.
    pub(super) multilingual_routed: bool,
    /// The caller's full visible namespace set (primary + any explicit extras).
    pub(super) visible_namespaces: Vec<String>,
    /// #836: true when at least one model's vector leg degraded to FTS-only
    /// after hitting the bounded ANN readiness wait.
    pub(super) ann_degraded: bool,
}

impl RecallCandidateSet {
    pub(super) fn all_vector_hits(&self) -> Vec<&VectorSearchHit> {
        self.vector_hits_per_model
            .iter()
            .flat_map(|(_, hits)| hits.iter())
            .collect()
    }
}

pub(super) fn recall_candidate_count(cfg: &RecallConfig, limit: u32) -> u32 {
    cfg.candidate_limit
        .unwrap_or_else(|| limit.saturating_mul(cfg.candidate_multiplier).max(40))
}

pub(super) fn search_source_label(source: SearchSource) -> &'static str {
    match source {
        SearchSource::Vector => "vector",
        SearchSource::Text => "text",
        SearchSource::Both => "both",
    }
}

/// Controls whether the FTS5 `snippet(...)` function is called during text search.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[doc(hidden)]
pub enum TextSnippetPolicy {
    Omit,
    Include { chars: usize },
}

impl TextSnippetPolicy {
    pub(crate) fn snippet_chars(self) -> usize {
        match self {
            Self::Omit => 0,
            Self::Include { chars } => chars.max(1),
        }
    }
}

pub(super) const RECALL_DIAGNOSTIC_SNIPPET_CHARS: usize = 200;

#[derive(Default)]
pub(super) struct CandidateMeta {
    pub(super) in_text: bool,
    pub(super) in_vector: bool,
    pub(super) title: Option<String>,
    pub(super) snippet: Option<String>,
}

pub(super) struct RecallCandidateParams<'a> {
    pub(super) candidate_limit: u32,
    pub(super) embedding_model: Option<&'a str>,
    /// Route the FTS path through the CJK-bypass tokenizer. Keyed on `contains_cjk`.
    pub(super) cjk_fts_bypass: bool,
    /// Route the dense/vector path to the multilingual model. Keyed on `needs_multilingual`.
    pub(super) use_multilingual: bool,
    pub(super) scoring_cfg: &'a crate::scoring::ScoringConfig,
    pub(super) snippet_policy: TextSnippetPolicy,
    pub(super) fts_gather: &'a crate::config::RecallFtsGatherConfig,
    /// Bounded ANN widening rounds, resolved before collection.
    pub(super) ann_overfetch_max_rounds: usize,
    /// Cold-ANN readiness wait before this model degrades to FTS-only.
    pub(super) ann_ready_timeout_ms: u64,
}

pub(super) struct RecallVectorCandidateParams<'a> {
    pub(super) candidate_limit: u32,
    pub(super) embedding_model: Option<&'a str>,
    /// Route the dense/vector path to the multilingual model. Keyed on `needs_multilingual`.
    pub(super) use_multilingual: bool,
    pub(super) scoring_cfg: &'a crate::scoring::ScoringConfig,
    /// Namespace set the caller is allowed to read. ANN returns global candidates;
    /// post-filter trims to this set before returning hits.
    pub(super) visible_namespaces: Vec<String>,
    /// Bounded widening rounds, already resolved from request or environment.
    pub(super) ann_overfetch_max_rounds: usize,
    /// Cold-ANN readiness wait before this model degrades to FTS-only.
    pub(super) ann_ready_timeout_ms: u64,
}

pub(super) struct RecallVectorCandidateResult {
    pub(super) vector_hits_per_model: Vec<(String, Vec<VectorSearchHit>)>,
    pub(super) multilingual_routed: bool,
    /// Whether any model timed out to FTS-only while its tracked build continued.
    pub(super) ann_degraded: bool,
}

pub(super) fn retrieval_hybrid_config(strategy: &FusionStrategy, limit: usize) -> HybridConfig {
    let mut config = HybridConfig::new(limit)
        .with_pool_size(limit)
        .with_fusion_strategy(strategy.clone());

    if let FusionStrategy::Weighted { weights } = strategy {
        config.vector_weight = weights.first().copied().unwrap_or(0.0).max(0.0);
        config.keyword_weight = weights.get(1).copied().unwrap_or(0.0).max(0.0);
    }

    config
}

pub(super) fn source_from_meta(meta: &CandidateMeta) -> SearchSource {
    match (meta.in_vector, meta.in_text) {
        (true, true) => SearchSource::Both,
        (true, false) => SearchSource::Vector,
        (false, true) => SearchSource::Text,
        (false, false) => SearchSource::Text,
    }
}

/// Combine N per-model vector source lists into one via Union (max score per ID).
pub(super) fn combine_vector_sources_union(
    sources: Vec<Vec<(Uuid, DeterministicScore)>>,
) -> Vec<(Uuid, DeterministicScore)> {
    use std::collections::hash_map::Entry;
    let capacity: usize = sources.iter().map(|s| s.len()).sum();
    let mut combined: HashMap<Uuid, DeterministicScore> = HashMap::with_capacity(capacity);
    for source in sources {
        for (id, score) in source {
            match combined.entry(id) {
                Entry::Occupied(mut e) => {
                    if score > *e.get() {
                        *e.get_mut() = score;
                    }
                }
                Entry::Vacant(e) => {
                    e.insert(score);
                }
            }
        }
    }
    let mut result: Vec<(Uuid, DeterministicScore)> = combined.into_iter().collect();
    result.sort_by(|(a, sa), (b, sb)| sb.cmp(sa).then(a.cmp(b)));
    result
}

pub(super) fn fuse_candidates(
    candidates: &RecallCandidateSet,
    memory_ids: &HashSet<Uuid>,
    cfg: &RecallConfig,
    limit: usize,
) -> Vec<SearchHit> {
    let mut meta = HashMap::<Uuid, CandidateMeta>::new();

    let text_source: Vec<_> = candidates
        .text_hits
        .iter()
        .filter(|h| memory_ids.contains(&h.subject_id))
        .map(|h| {
            let entry = meta.entry(h.subject_id).or_default();
            entry.in_text = true;
            if entry.title.is_none() {
                entry.title = h.title.clone();
            }
            if entry.snippet.is_none() {
                entry.snippet = h.snippet.clone();
            }
            (h.subject_id, h.score)
        })
        .collect();

    let vector_sources: Vec<Vec<_>> = candidates
        .vector_hits_per_model
        .iter()
        .map(|(_, hits)| {
            hits.iter()
                .filter(|h| memory_ids.contains(&h.subject_id))
                .map(|h| {
                    meta.entry(h.subject_id).or_default().in_vector = true;
                    (h.subject_id, h.score)
                })
                .collect()
        })
        .collect();

    let vector_only = matches!(&cfg.fuse_strategy, FusionStrategy::VectorOnly);
    let keyword_only = matches!(&cfg.fuse_strategy, FusionStrategy::KeywordOnly);
    let is_weighted = matches!(&cfg.fuse_strategy, FusionStrategy::Weighted { .. });

    let sources: Vec<Vec<_>> = if vector_only {
        vector_sources
    } else if keyword_only {
        vec![text_source]
    } else if is_weighted && vector_sources.len() > 1 {
        let combined_vector = combine_vector_sources_union(vector_sources);
        vec![combined_vector, text_source]
    } else {
        let mut s = if vector_sources.is_empty() {
            vec![vec![]]
        } else {
            vector_sources
        };
        s.push(text_source);
        s
    };

    if sources.is_empty() || sources.iter().all(|s| s.is_empty()) {
        return vec![];
    }

    let retrieval_cfg = retrieval_hybrid_config(&cfg.fuse_strategy, limit);
    fuse_search_results(sources, &retrieval_cfg)
        .into_iter()
        .map(|(id, score)| {
            let m = meta.remove(&id).unwrap_or_default();
            let (source, title, snippet) = if vector_only {
                (SearchSource::Vector, None, None)
            } else if keyword_only {
                (SearchSource::Text, m.title, m.snippet)
            } else {
                (source_from_meta(&m), m.title, m.snippet)
            };
            SearchHit {
                entity_id: id,
                score,
                source,
                title,
                snippet,
            }
        })
        .collect()
}

/// Maximum number of OR terms sent to the FTS5 trigram index per recall query.
pub(super) const RECALL_FTS_TERM_FANOUT_LIMIT: usize = 10;

/// Break a recall query into individual search terms for FTS fanout.
#[doc(hidden)]
pub fn recall_text_terms(query: &str) -> Vec<String> {
    recall_text_terms_with_limit(query, RECALL_FTS_TERM_FANOUT_LIMIT)
}

pub(super) fn recall_text_terms_with_limit(query: &str, limit: usize) -> Vec<String> {
    let mut seen = HashSet::new();
    let mut terms: Vec<String> = query
        .split(|c: char| {
            c.is_whitespace() || matches!(c, ',' | '.' | '?' | '!' | ';' | ':' | '(' | ')')
        })
        .map(|t| {
            t.trim_matches(|c: char| !c.is_alphanumeric())
                .to_ascii_lowercase()
        })
        .filter(|t| !t.is_empty() && seen.insert(t.clone()))
        .collect();
    terms.truncate(limit);
    terms
}

impl MemoryPack {
    /// Resolve bounded query strings against live entity names in one namespace-scoped batch.
    /// See `crates/khive-pack-memory/docs/api/recall-pipeline.md`.
    pub(super) async fn entity_anchored_candidates(
        &self,
        token: &NamespaceToken,
        query: &str,
    ) -> Result<Vec<String>, RuntimeError> {
        let store = self.runtime.entities(token)?;
        let namespace = token.namespace().as_str();

        let candidates = crate::scoring::entity_lookup_candidates(query);
        if candidates.is_empty() {
            return Ok(Vec::new());
        }
        let filter = EntityFilter {
            names_ci: candidates,
            ..EntityFilter::default()
        };
        let page = store
            .query_entities(
                namespace,
                filter,
                PageRequest {
                    limit: crate::scoring::MAX_ENTITY_LOOKUP_CANDIDATES as u32,
                    offset: 0,
                },
            )
            .await?;
        Ok(page
            .items
            .into_iter()
            .map(|e| e.name.to_lowercase())
            .collect())
    }

    #[allow(clippy::too_many_arguments)]
    pub(super) async fn collect_recall_text_hits(
        &self,
        token: &NamespaceToken,
        query: &str,
        namespaces: &[String],
        candidate_limit: u32,
        snippet_policy: TextSnippetPolicy,
        cjk_fts_bypass: bool,
        fts_gather: &crate::config::RecallFtsGatherConfig,
    ) -> Result<Vec<TextSearchHit>, RuntimeError> {
        let terms = recall_text_terms(query);
        if terms.is_empty() {
            return Ok(Vec::new());
        }
        let prof = recall_profile_enabled();
        let call_id = PROF_CID.with(|c| c.get());
        let t_fts = if prof { Some(Instant::now()) } else { None };
        let searcher = self.runtime.text_for_notes(token)?;

        // Sanitization handles known FTS5 syntax; residual parser errors propagate rather
        // than silently deleting the lexical leg. Gather-mode storage errors are already
        // flattened to RuntimeError::Internal and therefore also propagate.
        let fts_result: Result<Vec<TextSearchHit>, RuntimeError> = if fts_gather.enabled {
            crate::text_gather::collect_text_hits(
                searcher.as_ref(),
                query,
                namespaces,
                candidate_limit,
                snippet_policy,
                cjk_fts_bypass,
                fts_gather,
                &terms,
            )
            .await
        } else {
            searcher
                .search(TextSearchRequest {
                    query: terms.join(" "),
                    mode: TextQueryMode::AnyTerm,
                    filter: Some(TextFilter {
                        namespaces: namespaces.to_vec(),
                        kinds: vec![SubstrateKind::Note],
                        ..TextFilter::default()
                    }),
                    top_k: candidate_limit,
                    snippet_chars: snippet_policy.snippet_chars(),
                })
                .await
                .map_err(RuntimeError::from)
        };

        let mut hits = fts_text_leg_or_err(fts_result, "collect_recall_text_hits", query)?;
        hits.sort_by_key(|h| h.rank);
        hits.truncate(candidate_limit as usize);

        if prof {
            if let Some(t) = t_fts {
                plog_n(call_id, "fts", t.elapsed().as_micros(), hits.len());
            }
        }
        Ok(hits)
    }

    pub(super) async fn collect_recall_candidates(
        &self,
        query: &str,
        token: &NamespaceToken,
        opts: RecallCandidateParams<'_>,
    ) -> Result<RecallCandidateSet, RuntimeError> {
        let RecallCandidateParams {
            candidate_limit,
            embedding_model,
            cjk_fts_bypass,
            use_multilingual,
            scoring_cfg,
            snippet_policy,
            fts_gather,
            ann_overfetch_max_rounds,
            ann_ready_timeout_ms,
        } = opts;

        // FTS filters the shared table by visible namespaces; global ANN over-fetches and
        // post-filters to the same set.
        let visible: Vec<String> = token
            .visible_namespace_strs()
            .into_iter()
            .map(|s| s.to_string())
            .collect();
        let primary_ns = token.namespace().as_str().to_string();

        let text_fut = self.collect_recall_text_hits(
            token,
            query,
            &visible,
            candidate_limit,
            snippet_policy,
            cjk_fts_bypass,
            fts_gather,
        );
        let vector_fut = self.collect_recall_vector_hits(
            token,
            query,
            &primary_ns,
            RecallVectorCandidateParams {
                candidate_limit,
                embedding_model,
                use_multilingual,
                scoring_cfg,
                visible_namespaces: visible.clone(),
                ann_overfetch_max_rounds,
                ann_ready_timeout_ms,
            },
        );
        let (text_hits, vector_result) = tokio::try_join!(text_fut, vector_fut)?;
        Ok(RecallCandidateSet {
            namespace: primary_ns,
            text_hits,
            vector_hits_per_model: vector_result.vector_hits_per_model,
            multilingual_routed: vector_result.multilingual_routed,
            visible_namespaces: visible,
            ann_degraded: vector_result.ann_degraded,
        })
    }

    /// Collect namespace-safe model candidates through ANN or exact sqlite-vec.
    ///
    /// Global ANN starts at `max(k * 4, k + 32)` and post-filters; sqlite-vec filters in
    /// SQL. See `crates/khive-pack-memory/docs/api/recall-pipeline.md`.
    pub(super) async fn collect_recall_vector_hits(
        &self,
        token: &NamespaceToken,
        query: &str,
        ns: &str,
        opts: RecallVectorCandidateParams<'_>,
    ) -> Result<RecallVectorCandidateResult, RuntimeError> {
        let RecallVectorCandidateParams {
            candidate_limit,
            embedding_model,
            use_multilingual,
            scoring_cfg,
            visible_namespaces,
            ann_overfetch_max_rounds,
            ann_ready_timeout_ms,
        } = opts;

        // Over-fetch factor for the ANN path: F=4, M=32.
        // k' = max(k * F, k + M) so a 4× wider search compensates for foreign-namespace hits.
        // On a single-namespace store round-1 always satisfies k (zero foreign hits).
        const ANN_OVERFETCH_FACTOR: usize = 4;
        const ANN_OVERFETCH_MARGIN: usize = 32;
        let ann_fetch_limit = (candidate_limit as usize * ANN_OVERFETCH_FACTOR)
            .max(candidate_limit as usize + ANN_OVERFETCH_MARGIN);
        let prof = recall_profile_enabled();
        let call_id = PROF_CID.with(|c| c.get());

        let mut multilingual_routed = false;
        let mut ann_degraded = false;
        let model_names: Vec<String> = if let Some(m) = embedding_model {
            vec![m.to_string()]
        } else {
            let names = self.runtime.registered_embedding_model_names();
            if names.is_empty() {
                vec![]
            } else if use_multilingual {
                let multilingual_model = scoring_cfg
                    .multilingual_model
                    .as_deref()
                    .and_then(|m| names.iter().find(|n| n.as_str() == m).cloned())
                    .or_else(|| {
                        names
                            .iter()
                            .find(|n| n.contains("multilingual") || n.contains("paraphrase"))
                            .cloned()
                    });
                match multilingual_model {
                    Some(model) => {
                        multilingual_routed = true;
                        vec![model]
                    }
                    None => names,
                }
            } else {
                names
            }
        };

        let vector_hits_per_model: Vec<(String, Vec<VectorSearchHit>)> = if model_names.is_empty() {
            vec![]
        } else {
            let t_embed = if prof { Some(Instant::now()) } else { None };
            let query_vecs: Vec<(String, Vec<f32>)> = match model_names.len() {
                1 => {
                    let m = model_names.into_iter().next().unwrap();
                    vec![
                        embed_query_model(
                            self.runtime.clone(),
                            self.query_cache.clone(),
                            m,
                            query.to_string(),
                        )
                        .await?,
                    ]
                }
                2 => {
                    let mut it = model_names.into_iter();
                    let m0 = it.next().unwrap();
                    let m1 = it.next().unwrap();
                    let f0 = embed_query_model(
                        self.runtime.clone(),
                        self.query_cache.clone(),
                        m0,
                        query.to_string(),
                    );
                    let f1 = embed_query_model(
                        self.runtime.clone(),
                        self.query_cache.clone(),
                        m1,
                        query.to_string(),
                    );
                    let (r0, r1) = tokio::join!(f0, f1);
                    vec![r0?, r1?]
                }
                _ => {
                    let mut handles = Vec::with_capacity(model_names.len());
                    for model_name in model_names {
                        let rt = self.runtime.clone();
                        let cache = self.query_cache.clone();
                        let q = query.to_string();
                        handles.push(tokio::spawn(async move {
                            embed_query_model(rt, cache, model_name, q).await
                        }));
                    }
                    let mut vecs = Vec::with_capacity(handles.len());
                    for h in handles {
                        let pair = h.await.map_err(|e| {
                            RuntimeError::Internal(format!("recall embed task panicked: {e}"))
                        })??;
                        vecs.push(pair);
                    }
                    vecs
                }
            };

            if prof {
                if let Some(t) = t_embed {
                    plog_n(call_id, "embed", t.elapsed().as_micros(), query_vecs.len());
                }
            }

            // Widening rounds are resolved before this loop, enabling deterministic tests.

            let t_ann_total = if prof { Some(Instant::now()) } else { None };
            let mut ann_route = "ann";
            let mut results = Vec::with_capacity(query_vecs.len());
            for (model_name, vec) in query_vecs {
                let key = AnnKey::new(ns, &model_name);

                // Global ANN search widens only when namespace post-filtering leaves too few hits.
                // A stale installed graph remains the immediate fallback and triggers background
                // replacement; only a genuine miss waits for ensure. Durable epoch checking adds
                // cross-process reindex visibility beyond in-process generations.
                ann::maybe_check_durable_epoch(&self.runtime, &self.ann, &key).await;
                let cache_fresh = ann::is_current(&self.ann, &key).await;
                let search_result =
                    ann::search_loaded(&self.ann, &key, &vec, ann_fetch_limit).await;
                if !cache_fresh && matches!(search_result, Ok(Some(_))) {
                    ann::ensure_ann_background(&self.runtime, token, &self.ann, &model_name).await;
                }
                // Bound genuine-miss readiness, but never drop the build itself: a tracked task
                // owns ensure and its phase span while this request races only the result channel.
                // Per-model single flight prevents duplicate detached builds.
                let mut model_ann_timed_out = false;
                let initial_raw_hits: Option<Vec<(Uuid, f32)>> = match search_result {
                    Ok(Some(hits)) => Some(hits),
                    Ok(None) => {
                        let (done_tx, done_rx) = tokio::sync::oneshot::channel();
                        let rt_detached = self.runtime.clone();
                        let token_detached = token.clone();
                        let ann_detached = self.ann.clone();
                        let model_detached = model_name.clone();
                        khive_runtime::track_background_task(async move {
                            let result = ann::ensure_ann_for_model(
                                &rt_detached,
                                &token_detached,
                                &ann_detached,
                                &model_detached,
                            )
                            .await;
                            let _ = done_tx.send(result);
                        });
                        match tokio::time::timeout(
                            std::time::Duration::from_millis(ann_ready_timeout_ms),
                            done_rx,
                        )
                        .await
                        {
                            Ok(Ok(Ok(status))) => {
                                tracing::debug!(
                                    ?status,
                                    model = %model_name,
                                    namespace = %ns,
                                    "memory ANN ensured on recall miss"
                                );
                                ann::search_loaded(&self.ann, &key, &vec, ann_fetch_limit).await?
                            }
                            Ok(Ok(Err(e))) => return Err(e),
                            Ok(Err(_sender_dropped)) => {
                                // A detached-task panic degrades this model instead of failing recall.
                                tracing::warn!(
                                    model = %model_name,
                                    namespace = %ns,
                                    "memory ANN detached build task ended \
                                     without a result; degrading recall to \
                                     FTS-only for this model (#836)"
                                );
                                model_ann_timed_out = true;
                                ann_degraded = true;
                                None
                            }
                            Err(_elapsed) => {
                                tracing::warn!(
                                    model = %model_name,
                                    namespace = %ns,
                                    timeout_ms = ann_ready_timeout_ms,
                                    "memory ANN not ready within bounded wait; \
                                     degrading recall to FTS-only for this \
                                     model and detaching the build to finish \
                                     in the background (#836)"
                                );
                                model_ann_timed_out = true;
                                ann_degraded = true;
                                None
                            }
                        }
                    }
                    Err(e) => {
                        tracing::warn!(
                            error = %e,
                            namespace = %ns,
                            model = %model_name,
                            "memory ANN search failed; falling back to exact sqlite-vec"
                        );
                        ann::clear_key(&self.ann, &key).await;
                        None
                    }
                };

                if model_ann_timed_out {
                    // Do not replace a bounded timeout with an O(corpus) exact scan.
                    results.push((model_name, Vec::new()));
                    continue;
                }

                if let Some(first_raw) = initial_raw_hits {
                    // Widen until enough visible hits survive or ANN reports corpus exhaustion.
                    let note_store = self.runtime.notes(token)?;
                    let visible_set: std::collections::HashSet<&str> =
                        visible_namespaces.iter().map(String::as_str).collect();

                    // Empty namespace metadata is conservative; a visible-only set skips retry.
                    let index_has_non_visible =
                        match ann::index_namespace_set(&self.ann, &key).await {
                            Some(index_ns) if !index_ns.is_empty() => {
                                !index_ns.iter().all(|ns| visible_set.contains(ns.as_str()))
                            }
                            // Empty set (snapshot-restored without population yet) or cache miss:
                            // be conservative — assume non-visible namespaces may be present.
                            _ => true,
                        };

                    let mut best_raw = first_raw;
                    let mut current_fetch_limit = ann_fetch_limit;

                    // Visible-only indexes incur no hydration or extra ANN searches here.
                    if index_has_non_visible {
                        for _round in 1..ann_overfetch_max_rounds {
                            let corpus_exhausted = best_raw.len() < current_fetch_limit;
                            if corpus_exhausted {
                                break;
                            }
                            // Count visible-namespace survivors via a lightweight note batch fetch.
                            let candidate_ids: Vec<Uuid> =
                                best_raw.iter().map(|(id, _)| *id).collect();
                            let notes = note_store.get_notes_batch(&candidate_ids).await?;
                            let visible_count = notes
                                .iter()
                                .filter(|n| {
                                    n.deleted_at.is_none()
                                        && n.kind == "memory"
                                        && visible_set.contains(n.namespace.as_str())
                                })
                                .count();
                            if visible_count >= candidate_limit as usize {
                                break;
                            }
                            // Widen and retry.
                            current_fetch_limit *= 2;
                            tracing::debug!(
                                model = %model_name,
                                namespace = %ns,
                                visible_count,
                                candidate_limit,
                                new_fetch_limit = current_fetch_limit,
                                "memory ANN: widening over-fetch (visible survivors short)"
                            );
                            if let Ok(Some(wider)) =
                                ann::search_loaded(&self.ann, &key, &vec, current_fetch_limit).await
                            {
                                best_raw = wider;
                            } else {
                                break;
                            }
                        }
                    }

                    tracing::debug!(
                        model = %model_name,
                        namespace = %ns,
                        hits = best_raw.len(),
                        "memory recall via warm ANN"
                    );
                    let hits: Vec<VectorSearchHit> = best_raw
                        .into_iter()
                        .enumerate()
                        .map(|(idx, (uuid, score))| VectorSearchHit {
                            subject_id: uuid,
                            score: khive_score::DeterministicScore::from_f64(score as f64),
                            rank: (idx + 1) as u32,
                        })
                        .collect();
                    results.push((model_name, hits));
                    continue;
                }

                // sqlite-vec fallback: the query includes namespace IN (...) so it
                // respects the caller's visible set directly without over-fetch.
                // When visible_namespaces has multiple entries we fan out one search
                // per namespace and union the results, since VectorSearchRequest
                // accepts a single namespace string.
                tracing::debug!(model = %model_name, namespace = %ns, "memory recall via exact sqlite-vec");
                ann_route = "sqlite_vec";
                let store = self.runtime.vectors_for_model(token, &model_name)?;
                let mut all_hits: Vec<VectorSearchHit> = Vec::new();
                for search_ns in &visible_namespaces {
                    let ns_hits = store
                        .search(VectorSearchRequest {
                            query_vectors: vec![vec.clone()],
                            top_k: candidate_limit,
                            namespace: Some(search_ns.clone()),
                            kind: Some(SubstrateKind::Note),
                            embedding_model: Some(model_name.clone()),
                            filter: None,
                            backend_hints: None,
                        })
                        .await?;
                    all_hits.extend(ns_hits);
                }
                // Merge + re-rank by score descending.
                all_hits.sort_by_key(|hit| std::cmp::Reverse(hit.score));
                all_hits.truncate(candidate_limit as usize);
                for (idx, hit) in all_hits.iter_mut().enumerate() {
                    hit.rank = (idx + 1) as u32;
                }
                results.push((model_name, all_hits));
            }
            if prof {
                if let Some(t) = t_ann_total {
                    let total_hits: usize = results.iter().map(|(_, h)| h.len()).sum();
                    eprintln!(
                        r#"{{"c":{},"s":"ann","us":{},"n":{},"route":"{}"}}"#,
                        call_id,
                        t.elapsed().as_micros(),
                        total_hits,
                        ann_route,
                    );
                }
            }
            results
        };

        Ok(RecallVectorCandidateResult {
            vector_hits_per_model,
            multilingual_routed,
            ann_degraded,
        })
    }

    pub(super) async fn load_memory_candidate_notes(
        &self,
        token: &NamespaceToken,
        candidates: &RecallCandidateSet,
    ) -> Result<(HashSet<Uuid>, HashMap<Uuid, khive_storage::note::Note>), RuntimeError> {
        let all_vector_hits = candidates.all_vector_hits();
        let candidate_ids: Vec<Uuid> = {
            let mut seen = HashSet::new();
            let mut ids = Vec::new();
            for id in candidates
                .text_hits
                .iter()
                .map(|h| h.subject_id)
                .chain(all_vector_hits.iter().map(|h| h.subject_id))
            {
                if seen.insert(id) {
                    ids.push(id);
                }
            }
            ids
        };

        let note_store = self.runtime.notes(token)?;
        let batch = note_store.get_notes_batch(&candidate_ids).await?;
        let mut memory_ids = HashSet::new();
        let mut notes_by_id = HashMap::new();
        let visible_set: std::collections::HashSet<&str> = candidates
            .visible_namespaces
            .iter()
            .map(String::as_str)
            .collect();
        let now_micros = chrono::Utc::now().timestamp_micros();
        for note in batch {
            // Post-filter: ANN over-fetch may include rows from outside the caller's
            // visible namespace set. Drop them here where the note row carries its namespace.
            // Also exclude memories whose expires_at is in the past (view-layer expiry).
            let expired = note.expires_at.map(|e| e <= now_micros).unwrap_or(false);
            if note.deleted_at.is_none()
                && note.kind == "memory"
                && visible_set.contains(note.namespace.as_str())
                && !expired
            {
                memory_ids.insert(note.id);
                notes_by_id.insert(note.id, note);
            }
        }

        Ok((memory_ids, notes_by_id))
    }
}