khive-runtime 0.19.0

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

use super::*;

impl KhiveRuntime {
    /// Search with an explicit top-level note status and strict vector errors.
    ///
    /// The count records distinct candidates whose status mismatches the selector after
    /// namespace, mailbox, kind, tag and property checks. It precedes superseded
    /// filtering and ranking, so it is not a count of missing eligible matches.
    /// The existing bounded four-times-per-arm window is unchanged. A supplied
    /// mailbox view adds a gate; it does not replace the default actor gate.
    #[allow(clippy::too_many_arguments)]
    pub async fn search_notes_with_text_mode_and_status(
        &self,
        token: &NamespaceToken,
        query_text: &str,
        query_vector: Option<Vec<f32>>,
        limit: u32,
        note_kind: Option<&str>,
        include_superseded: bool,
        tags_any: &[String],
        properties_filter: Option<&serde_json::Value>,
        text_mode: TextQueryMode,
        note_status: khive_storage::note::NoteStatusSelector,
        additional_mailbox_view: Option<&crate::MailboxView>,
    ) -> RuntimeResult<(Vec<NoteSearchHit>, u64)> {
        let (hits, _vector_error, note_status_dropped) = self
            .search_notes_inner(
                token,
                query_text,
                query_vector,
                limit,
                note_kind,
                include_superseded,
                tags_any,
                properties_filter,
                text_mode,
                false,
                note_status,
                additional_mailbox_view,
            )
            .await?;
        Ok((hits, note_status_dropped))
    }

    /// Note-search outcome with explicit status and bounded status-drop evidence.
    ///
    /// A successful text leg survives a vector error, which remains in the
    /// existing outcome. Count semantics match the strict selector entrypoint.
    #[allow(clippy::too_many_arguments)]
    pub async fn search_notes_outcome_with_text_mode_and_status(
        &self,
        token: &NamespaceToken,
        query_text: &str,
        limit: u32,
        note_kind: Option<&str>,
        include_superseded: bool,
        tags_any: &[String],
        properties_filter: Option<&serde_json::Value>,
        text_mode: TextQueryMode,
        note_status: khive_storage::note::NoteStatusSelector,
        additional_mailbox_view: Option<&crate::MailboxView>,
    ) -> RuntimeResult<(NoteSearchOutcome, u64)> {
        let (hits, vector_error, note_status_dropped) = self
            .search_notes_inner(
                token,
                query_text,
                None,
                limit,
                note_kind,
                include_superseded,
                tags_any,
                properties_filter,
                text_mode,
                true,
                note_status,
                additional_mailbox_view,
            )
            .await?;
        Ok((
            NoteSearchOutcome { hits, vector_error },
            note_status_dropped,
        ))
    }

    #[allow(clippy::too_many_arguments)]
    pub(super) async fn search_notes_inner(
        &self,
        token: &NamespaceToken,
        query_text: &str,
        query_vector: Option<Vec<f32>>,
        limit: u32,
        note_kind: Option<&str>,
        include_superseded: bool,
        tags_any: &[String],
        properties_filter: Option<&serde_json::Value>,
        text_mode: TextQueryMode,
        tolerate_vector_error: bool,
        note_status: khive_storage::note::NoteStatusSelector,
        additional_mailbox_view: Option<&crate::MailboxView>,
    ) -> RuntimeResult<(Vec<NoteSearchHit>, Option<String>, u64)> {
        const RRF_K: usize = 60;
        let candidates = limit.saturating_mul(4).max(limit);
        let visible_ns: Vec<String> = token
            .visible_namespaces()
            .iter()
            .map(|ns| ns.as_str().to_owned())
            .collect();

        // FTS5 over the notes index — search all visible namespaces.
        //
        // `sanitize_fts5_query` strips known-unsafe FTS5 metacharacters, but
        // residual punctuation the sanitizer does not strip can still reach
        // the FTS5 parser and error. This fails loud instead of degrading to
        // vector-only fusion, so callers see the bad query instead of
        // silently losing the lexical leg. Errors from any other leg (vector
        // search, note hydration) still propagate normally.
        //
        // Injection: check FTS_SEARCH_FAIL_NS (armed by `arm_fts_search_fail(ns)`),
        // exercising the propagate branch above. Fires only when the armed
        // namespace is among this call's visible namespaces, then clears (one-shot).
        #[cfg(any(test, feature = "fault-injection"))]
        let fts_search_inject = {
            let mut g = FTS_SEARCH_FAIL_NS.lock().unwrap();
            match g.as_deref() {
                Some(armed) if visible_ns.iter().any(|ns| ns == armed) => {
                    *g = None;
                    true
                }
                _ => false,
            }
        };
        #[cfg(not(any(test, feature = "fault-injection")))]
        let fts_search_inject = false;

        let text_store = self.text_for_notes(token)?;
        let text_fut = async {
            let _phase = khive_storage::request_context::enter_search_phase(
                khive_storage::request_context::SearchPhase::TextSearch,
            );
            if fts_search_inject {
                return Err(khive_storage::StorageError::Timeout {
                    operation: "fts_search".into(),
                });
            }
            text_store
                .search(TextSearchRequest {
                    query: query_text.to_string(),
                    mode: text_mode,
                    filter: Some(TextFilter {
                        namespaces: visible_ns.clone(),
                        // Push the note-kind filter into the FTS query. Without it the
                        // text arm returns the top `candidates` rows across EVERY note
                        // kind in the namespace and the kind is applied post-fetch, so a
                        // store where short message/session rows outrank task
                        // descriptions under BM25 hands the caller one or two task hits
                        // while the store holds many more carrying the literal.
                        record_kinds: note_kind
                            .map(|kind| vec![kind.to_string()])
                            .unwrap_or_default(),
                        ..TextFilter::default()
                    }),
                    top_k: candidates,
                    snippet_chars: 200,
                })
                .await
        };
        let text_fut = crate::stage_seam::text_stage(text_fut);

        // Vector search filtered to notes; it runs with the text stage, and a text error wins.
        let vector_fut = async {
            let _phase = khive_storage::request_context::enter_search_phase(
                khive_storage::request_context::SearchPhase::VectorSearch,
            );
            if query_vector.is_some() || !self.default_embedder_name().is_empty() {
                crate::search_audit::record(crate::search_audit::SearchArm::Vector);
                self.note_search_vector_search(token, query_vector, query_text, candidates)
                    .await
            } else {
                Ok(vec![])
            }
        };
        let (text_search_result, vector_result) = tokio::join!(text_fut, vector_fut);

        // FtsPasses is counted inside the store's `search()` (khive-db
        // stores/text.rs), only once a real FTS5 statement is prepared —
        // an empty/fully-sanitized query short-circuits there before any
        // statement exists and must not count (nor does the injected-failure
        // branch above, which never reaches the store at all).
        let text_hits = crate::error::fts_text_leg_or_err(
            text_search_result.map_err(RuntimeError::from),
            "search_notes",
            query_text,
        )?;

        let mut vector_error: Option<String> = None;
        let vector_hits = match vector_result {
            Ok(hits) => hits,
            Err(e) if tolerate_vector_error => {
                vector_error = Some(e.to_string());
                Vec::new()
            }
            Err(e) => return Err(e),
        };

        // Keep the full text∪vector union through RRF — salience weighting and
        // soft-delete/kind filtering happen *after* this, and the final
        // `hits.truncate(limit)` is the only result-limiting cut. Truncating to
        // `candidates` here would drop a high-salience note ranked just outside
        // the raw RRF cutoff before salience ever applied.
        let fuse_k = text_hits.len() + vector_hits.len();
        let fused = {
            let _phase = khive_storage::request_context::enter_search_phase(
                khive_storage::request_context::SearchPhase::Fusion,
            );
            self.fuse_with_strategy(
                text_hits,
                vector_hits,
                &crate::fusion::FusionStrategy::Rrf { k: RRF_K },
                fuse_k,
            )
            .await?
        };

        let candidate_ids: Vec<Uuid> = fused.iter().map(|hit| hit.entity_id).collect();
        if candidate_ids.is_empty() {
            return Ok((vec![], vector_error, 0));
        }

        // Hydrate every candidate note with one batched read to get salience and
        // apply soft-delete + (optional) kind filtering. The store chunks the read
        // below its bound-parameter ceiling, so the read costs `ceil(candidates / 900)`
        // statements instead of one per candidate. Notes whose `kind` doesn't
        // match `note_kind` are dropped post-fetch — they're a small set
        // bounded by the text∪vector union (≤ 2×candidates), so the read is cheap.
        let note_store = self.notes(token)?;
        let search_pool = self.backend().pool_arc();
        let mailbox_view = crate::MailboxView {
            actor_id: token.actor().id.clone(),
            delegated: false,
        };
        let mut alive_notes: HashMap<Uuid, Note> = HashMap::new();
        // Fused candidate IDs and the batched identity read each contain one row per note.
        let mut note_status_dropped = 0_u64;
        let fetched_notes = {
            let _phase = khive_storage::request_context::enter_search_phase(
                khive_storage::request_context::SearchPhase::NoteHydration,
            );
            note_store.get_notes_batch(&candidate_ids).await?
        };
        let _authorization_phase = khive_storage::request_context::enter_search_phase(
            khive_storage::request_context::SearchPhase::MailboxAuthorization,
        );
        for note in fetched_notes {
            search_pool.record_note_candidate_hydration_row();
            // Identity batch reads are neutral. Revalidate the full row even
            // when an index candidate claimed a visible namespace.
            if !visible_ns
                .iter()
                .any(|namespace| namespace == &note.namespace)
            {
                continue;
            }
            if note.deleted_at.is_some() {
                continue;
            }
            if !mailbox_view.permits_message_note(token, &note) {
                continue;
            }
            if additional_mailbox_view.is_some_and(|view| !view.permits_message_note(token, &note))
            {
                continue;
            }
            if let Some(want_kind) = note_kind {
                if note.kind != want_kind {
                    continue;
                }
            }
            // Apply tag predicate before adding to alive set: tags on notes live
            // inside `properties["tags"]` (a JSON array). This pushes the filter
            // before truncation so matching notes ranked beyond `limit` in the raw
            // fusion are not silently dropped.
            if !tags_any.is_empty() {
                let note_tags: Vec<String> = note
                    .properties
                    .as_ref()
                    .and_then(|p| p.get("tags"))
                    .and_then(serde_json::Value::as_array)
                    .map(|arr| {
                        arr.iter()
                            .filter_map(serde_json::Value::as_str)
                            .map(str::to_owned)
                            .collect()
                    })
                    .unwrap_or_default();
                if !note_tags
                    .iter()
                    .any(|t| tags_any.iter().any(|w| t.eq_ignore_ascii_case(w)))
                {
                    continue;
                }
            }
            // Apply properties predicate before truncation, same reasoning as tags above.
            if let Some(pf) = properties_filter {
                if !crate::retrieval::properties_match(note.properties.as_ref(), pf) {
                    continue;
                }
            }
            if !note_status.matches(&note.status) {
                note_status_dropped = note_status_dropped.checked_add(1).ok_or_else(|| {
                    RuntimeError::Internal("note status drop count exceeds u64".into())
                })?;
                continue;
            }
            alive_notes.insert(note.id, note);
        }
        drop(_authorization_phase);

        // Drop superseded notes unless include_superseded is true: any note targeted
        // by a `supersedes` edge is obsolete and excluded from default search.
        if !include_superseded && !alive_notes.is_empty() {
            let graph = self.graph(token)?;
            let note_ids: Vec<Uuid> = alive_notes.keys().copied().collect();
            let superseded: std::collections::HashSet<Uuid> = {
                let _phase = khive_storage::request_context::enter_search_phase(
                    khive_storage::request_context::SearchPhase::SupersededFilter,
                );
                graph
                    .batch_neighbors(
                        &note_ids,
                        NeighborQuery {
                            direction: Direction::In,
                            relations: Some(vec![EdgeRelation::Supersedes]),
                            limit: Some(1),
                            min_weight: None,
                        },
                    )
                    .await?
                    .into_iter()
                    .map(|(note_id, _)| note_id)
                    .collect()
            };
            alive_notes.retain(|id, _| !superseded.contains(id));
        }

        // Apply salience weighting and collect final hits.
        let mut hits: Vec<NoteSearchHit> = fused
            .into_iter()
            .filter_map(|hit| {
                let note = alive_notes.get(&hit.entity_id)?;
                let weighted = salience_weighted_rank(hit.score, note.salience);
                Some(NoteSearchHit {
                    note_id: hit.entity_id,
                    score: weighted,
                    rank_score_kind: hit.rank_score_kind,
                    signals: hit.signals,
                    source: hit.source,
                    title: hit.title.or_else(|| note_title(note)),
                    snippet: hit.snippet.or_else(|| note_snippet(note)),
                })
            })
            .collect();

        hits.sort_by(|a, b| b.score.cmp(&a.score).then(a.note_id.cmp(&b.note_id)));
        hits.truncate(limit as usize);
        Ok((hits, vector_error, note_status_dropped))
    }
}

#[cfg(test)]
mod status_tests {
    use super::*;
    use crate::embedder_registry::EmbedderProvider;
    use async_trait::async_trait;
    use khive_storage::note::{NoteStatusSelector, NoteStore};
    use khive_storage::types::TextDocument;
    use lattice_embed::{EmbedError, EmbeddingModel, EmbeddingService};
    use std::collections::HashSet;
    use std::future::Future;
    use std::sync::Arc;
    use std::time::Duration;

    const QUERY: &str = "archivestatusfixture";

    // Every async setup/search/assertion in these tests is inside this budget.
    // Tokio timeouts bound cooperative async waits, not synchronous native code.
    async fn bounded(future: impl Future<Output = ()>) {
        tokio::time::timeout(Duration::from_secs(20), future)
            .await
            .expect("status fixture exceeded its async budget");
    }

    fn note(namespace: &str, kind: &str, status: &str) -> Note {
        let mut note = Note::new(namespace, kind, QUERY);
        note.status = status.into();
        note
    }

    async fn seed(
        runtime: &KhiveRuntime,
        token: &NamespaceToken,
        note: Note,
        indexed_namespace: &str,
        indexed_kind: &str,
    ) -> Uuid {
        let id = note.id;
        // Isolated fixture only: actual backend NoteStore bypasses the live
        // message-forgery guard so private/stale records can be represented.
        runtime.backend().upsert_note(note).await.unwrap();
        runtime
            .text_for_notes(token)
            .unwrap()
            .upsert_document(TextDocument {
                subject_id: id,
                kind: khive_types::SubstrateKind::Note,
                record_kind: Some(indexed_kind.into()),
                namespace: indexed_namespace.into(),
                title: None,
                body: QUERY.into(),
                tags: vec![],
                metadata: None,
                updated_at: chrono::Utc::now(),
            })
            .await
            .unwrap();
        id
    }

    async fn raw_candidates(
        runtime: &KhiveRuntime,
        token: &NamespaceToken,
        top_k: u32,
    ) -> HashSet<Uuid> {
        runtime
            .text_for_notes(token)
            .unwrap()
            .search(TextSearchRequest {
                query: QUERY.into(),
                mode: TextQueryMode::Plain,
                filter: Some(TextFilter {
                    namespaces: vec![token.namespace().as_str().into()],
                    ..Default::default()
                }),
                top_k,
                snippet_chars: 100,
            })
            .await
            .unwrap()
            .into_iter()
            .map(|hit| hit.subject_id)
            .collect()
    }

    async fn select(
        runtime: &KhiveRuntime,
        token: &NamespaceToken,
        status: NoteStatusSelector,
        limit: u32,
    ) -> (Vec<NoteSearchHit>, u64) {
        runtime
            .search_notes_with_text_mode_and_status(
                token,
                QUERY,
                None,
                limit,
                None,
                true,
                &[],
                None,
                TextQueryMode::Plain,
                status,
                None,
            )
            .await
            .unwrap()
    }

    fn ids(hits: &[NoteSearchHit]) -> HashSet<Uuid> {
        hits.iter().map(|hit| hit.note_id).collect()
    }

    #[tokio::test]
    async fn legacy_wrappers_default_active_and_explicit_selectors_differ() {
        bounded(async {
            let runtime = KhiveRuntime::memory().unwrap();
            let token = NamespaceToken::local();
            let ns = token.namespace().as_str();
            let mut active = note(ns, "observation", "active");
            active.properties = Some(serde_json::json!({"status": "archived"}));
            let active_id = seed(&runtime, &token, active, ns, "observation").await;
            let active_second = seed(
                &runtime,
                &token,
                note(ns, "observation", "active"),
                ns,
                "observation",
            )
            .await;
            let mut archived = note(ns, "observation", "archived");
            archived.properties = Some(serde_json::json!({"status": "active"}));
            let archived_id = seed(&runtime, &token, archived, ns, "observation").await;
            let archived_second = seed(
                &runtime,
                &token,
                note(ns, "observation", "archived"),
                ns,
                "observation",
            )
            .await;
            let custom_id = seed(
                &runtime,
                &token,
                note(ns, "observation", "custom"),
                ns,
                "observation",
            )
            .await;
            let active_ids = HashSet::from([active_id, active_second]);
            let archived_ids = HashSet::from([archived_id, archived_second]);
            let all_ids = HashSet::from([
                active_id,
                active_second,
                archived_id,
                archived_second,
                custom_id,
            ]);
            assert_eq!(raw_candidates(&runtime, &token, 40).await, all_ids);
            let plain = runtime
                .search_notes(&token, QUERY, None, 10, None, true, &[], None)
                .await
                .unwrap();
            assert_eq!(ids(&plain), active_ids);
            let mode = runtime
                .search_notes_with_text_mode(
                    &token,
                    QUERY,
                    None,
                    10,
                    None,
                    true,
                    &[],
                    None,
                    TextQueryMode::Plain,
                )
                .await
                .unwrap();
            assert_eq!(ids(&mode), active_ids);
            let outcome = runtime
                .search_notes_outcome(&token, QUERY, 10, None, true, &[], None)
                .await
                .unwrap();
            assert_eq!(ids(&outcome.hits), active_ids);
            assert!(outcome.vector_error.is_none());
            let mode_outcome = runtime
                .search_notes_outcome_with_text_mode(
                    &token,
                    QUERY,
                    10,
                    None,
                    true,
                    &[],
                    None,
                    TextQueryMode::Plain,
                )
                .await
                .unwrap();
            assert_eq!(ids(&mode_outcome.hits), active_ids);
            assert!(mode_outcome.vector_error.is_none());
            for (selector, expected_ids, expected_count) in [
                (NoteStatusSelector::Active, active_ids, 3),
                (NoteStatusSelector::Archived, archived_ids, 3),
                (NoteStatusSelector::Any, all_ids, 0),
            ] {
                let (hits, count) = select(&runtime, &token, selector, 10).await;
                assert_eq!(ids(&hits), expected_ids);
                assert_eq!(
                    count, expected_count,
                    "all lifecycle mismatches count, including custom"
                );
                let (outcome, count) = runtime
                    .search_notes_outcome_with_text_mode_and_status(
                        &token,
                        QUERY,
                        10,
                        None,
                        true,
                        &[],
                        None,
                        TextQueryMode::Plain,
                        selector,
                        None,
                    )
                    .await
                    .unwrap();
                assert_eq!(ids(&outcome.hits), expected_ids);
                assert_eq!(count, expected_count);
                assert!(outcome.vector_error.is_none());
            }
            let raw = runtime
                .notes(&token)
                .unwrap()
                .get_notes_batch(&[archived_id, custom_id])
                .await
                .unwrap();
            assert_eq!(raw.len(), 2, "identity hydration remains status neutral");
        })
        .await;
    }

    #[tokio::test]
    async fn invisible_hydrated_candidate_never_becomes_hit_or_status_count() {
        bounded(async {
            let runtime = KhiveRuntime::memory().unwrap();
            let token = NamespaceToken::local();
            let ns = token.namespace().as_str();
            let foreign_id = seed(
                &runtime,
                &token,
                note("foreign-status-fixture", "observation", "archived"),
                ns,
                "observation",
            )
            .await;
            // Prove the seeded mismatch reaches the genuine candidate and raw
            // hydration seams; a retrieval-only refusal cannot pass this test.
            assert!(raw_candidates(&runtime, &token, 40)
                .await
                .contains(&foreign_id));
            let raw = runtime
                .notes(&token)
                .unwrap()
                .get_notes_batch(&[foreign_id])
                .await
                .unwrap();
            assert_eq!(raw.len(), 1);
            assert_eq!(raw[0].namespace, "foreign-status-fixture");
            assert!(!token
                .visible_namespaces()
                .iter()
                .any(|ns| ns.as_str() == raw[0].namespace.as_str()));
            for status in [
                NoteStatusSelector::Active,
                NoteStatusSelector::Archived,
                NoteStatusSelector::Any,
            ] {
                let (hits, dropped) = select(&runtime, &token, status, 10).await;
                assert!(
                    hits.is_empty(),
                    "invisible hydrated candidate must not be a hit"
                );
                assert_eq!(
                    dropped, 0,
                    "invisible archived candidate must not be counted"
                );
            }
            let visible_id = seed(
                &runtime,
                &token,
                note(ns, "observation", "archived"),
                ns,
                "observation",
            )
            .await;
            let (hits, dropped) = select(&runtime, &token, NoteStatusSelector::Active, 10).await;
            assert!(hits.is_empty());
            assert_eq!(
                dropped, 1,
                "positive control: only visible archive contributes"
            );
            let (hits, dropped) = select(&runtime, &token, NoteStatusSelector::Any, 10).await;
            assert_eq!(ids(&hits), HashSet::from([visible_id]));
            assert_eq!(dropped, 0);
        })
        .await;
    }

    #[tokio::test]
    async fn hydration_predicates_run_before_status_accounting() {
        bounded(async {
            let runtime = KhiveRuntime::memory().unwrap();
            let token = NamespaceToken::local();
            let ns = token.namespace().as_str();
            let matched = serde_json::json!({"tags": ["wanted"], "project": "keep"});
            let mut kept_archive = note(ns, "observation", "archived");
            kept_archive.properties = Some(matched.clone());
            let archived_id = seed(&runtime, &token, kept_archive, ns, "observation").await;
            let mut kept_active = note(ns, "observation", "active");
            kept_active.properties = Some(matched.clone());
            let active_id = seed(&runtime, &token, kept_active, ns, "observation").await;
            let mut wrong_kind = note(ns, "question", "archived");
            wrong_kind.properties = Some(matched.clone());
            // Stale indexed kind ensures the full-note kind check is exercised.
            let wrong_kind_id = seed(&runtime, &token, wrong_kind, ns, "observation").await;
            let mut wrong_tag = note(ns, "observation", "archived");
            wrong_tag.properties = Some(serde_json::json!({"tags": ["other"], "project": "keep"}));
            let wrong_tag_id = seed(&runtime, &token, wrong_tag, ns, "observation").await;
            let mut wrong_property = note(ns, "observation", "archived");
            wrong_property.properties =
                Some(serde_json::json!({"tags": ["wanted"], "project": "other"}));
            let wrong_property_id = seed(&runtime, &token, wrong_property, ns, "observation").await;
            let mut deleted = note(ns, "observation", "archived");
            deleted.properties = Some(matched);
            deleted.deleted_at = Some(deleted.updated_at);
            let deleted_id = seed(&runtime, &token, deleted, ns, "observation").await;
            assert_eq!(
                raw_candidates(&runtime, &token, 40).await,
                HashSet::from([
                    archived_id,
                    active_id,
                    wrong_kind_id,
                    wrong_tag_id,
                    wrong_property_id,
                    deleted_id
                ])
            );
            let tags = vec!["wanted".into()];
            let properties = serde_json::json!({"project": "keep"});
            let (hits, dropped) = runtime
                .search_notes_with_text_mode_and_status(
                    &token,
                    QUERY,
                    None,
                    10,
                    Some("observation"),
                    true,
                    &tags,
                    Some(&properties),
                    TextQueryMode::Plain,
                    NoteStatusSelector::Active,
                    None,
                )
                .await
                .unwrap();
            assert_eq!(ids(&hits), HashSet::from([active_id]));
            assert_eq!(
                dropped, 1,
                "only the otherwise matching live archive counts"
            );
        })
        .await;
    }

    #[tokio::test]
    async fn default_mailbox_rejection_precedes_status_count() {
        bounded(async {
            let runtime = KhiveRuntime::memory().unwrap();
            let token = NamespaceToken::local();
            let ns = token.namespace().as_str();
            let mut private = note(ns, "message", "archived");
            private.properties = Some(serde_json::json!({
                "direction": "inbound", "from_actor": "status-fixture-sender",
                "to_actor": "status-fixture-other"
            }));
            let private_id = seed(&runtime, &token, private, ns, "message").await;
            assert_eq!(
                raw_candidates(&runtime, &token, 40).await,
                HashSet::from([private_id])
            );
            for selector in [
                NoteStatusSelector::Active,
                NoteStatusSelector::Archived,
                NoteStatusSelector::Any,
            ] {
                // The real search constructs its own caller view; no test MailboxView literal.
                let (hits, dropped) = select(&runtime, &token, selector, 10).await;
                assert!(hits.is_empty());
                assert_eq!(
                    dropped, 0,
                    "another actor's inbound message contributes no count"
                );
            }
            let archive_id = seed(
                &runtime,
                &token,
                note(ns, "observation", "archived"),
                ns,
                "observation",
            )
            .await;
            let (hits, count) = select(&runtime, &token, NoteStatusSelector::Active, 10).await;
            assert!(hits.is_empty());
            assert_eq!(
                count, 1,
                "positive control: visible non-message archive is counted"
            );
            let (hits, count) = select(&runtime, &token, NoteStatusSelector::Any, 10).await;
            assert_eq!(ids(&hits), HashSet::from([archive_id]));
            assert_eq!(count, 0);
        })
        .await;
    }

    #[tokio::test]
    async fn actual_authorized_additional_view_intersects_default_mailbox() {
        bounded(async {
            // Use the same real policy/authorization constructors as mailbox_view/tests.rs.
            let gate = crate::MailboxReadGate::new(
                Arc::new(crate::AllowAllGate),
                crate::ActorRef::new("actor", "lambda:owner"),
                vec![crate::ActorRef::new("actor", "lambda:reader")],
            )
            .unwrap();
            let runtime = KhiveRuntime::new(crate::RuntimeConfig {
                db_path: None,
                gate: Arc::new(gate),
                actor_id: Some("lambda:owner".into()),
                ..crate::RuntimeConfig::no_embeddings()
            })
            .unwrap();
            let token = NamespaceToken::mint_authorized(
                crate::Namespace::local(),
                crate::ActorRef::new("actor", "lambda:reader"),
            );
            let ns = token.namespace().as_str();
            let own_view = runtime
                .authorize_mailbox_view(
                    &token,
                    "search",
                    None,
                    &serde_json::json!({"query": QUERY, "kind": "note"}),
                )
                .unwrap();
            // This is a genuine separately authorized mailbox view, used only to
            // exercise the API's intersection invariant. Generic search itself
            // does not grant cross-actor mailbox selection.
            let owner_view = runtime
                .authorize_mailbox_view(
                    &token,
                    "comm.inbox",
                    Some("lambda:owner"),
                    &serde_json::json!({"mailbox_actor": "lambda:owner"}),
                )
                .unwrap();
            let mut own = note(ns, "message", "archived");
            own.properties =
                Some(serde_json::json!({"direction": "inbound", "to_actor": "lambda:reader"}));
            let mut other = note(ns, "message", "archived");
            other.properties =
                Some(serde_json::json!({"direction": "inbound", "to_actor": "lambda:owner"}));
            assert!(own_view.permits_message_note(&token, &own));
            assert!(!own_view.permits_message_note(&token, &other));
            assert!(!owner_view.permits_message_note(&token, &own));
            assert!(owner_view.permits_message_note(&token, &other));
            let own_id = seed(&runtime, &token, own, ns, "message").await;
            let other_id = seed(&runtime, &token, other, ns, "message").await;
            assert_eq!(
                raw_candidates(&runtime, &token, 40).await,
                HashSet::from([own_id, other_id])
            );
            let (hits, count) = runtime
                .search_notes_with_text_mode_and_status(
                    &token,
                    QUERY,
                    None,
                    10,
                    None,
                    true,
                    &[],
                    None,
                    TextQueryMode::Plain,
                    NoteStatusSelector::Active,
                    Some(&own_view),
                )
                .await
                .unwrap();
            assert!(hits.is_empty());
            assert_eq!(
                count, 1,
                "own authorized view preserves one otherwise-eligible archive"
            );
            for selector in [
                NoteStatusSelector::Active,
                NoteStatusSelector::Archived,
                NoteStatusSelector::Any,
            ] {
                let (hits, count) = runtime
                    .search_notes_with_text_mode_and_status(
                        &token,
                        QUERY,
                        None,
                        10,
                        None,
                        true,
                        &[],
                        None,
                        TextQueryMode::Plain,
                        selector,
                        Some(&owner_view),
                    )
                    .await
                    .unwrap();
                assert!(hits.is_empty(), "both independent mailbox gates apply");
                assert_eq!(
                    count, 0,
                    "neither mailbox intersection row may be disclosed by count"
                );
                let (outcome, count) = runtime
                    .search_notes_outcome_with_text_mode_and_status(
                        &token,
                        QUERY,
                        10,
                        None,
                        true,
                        &[],
                        None,
                        TextQueryMode::Plain,
                        selector,
                        Some(&owner_view),
                    )
                    .await
                    .unwrap();
                assert!(outcome.hits.is_empty());
                assert!(outcome.vector_error.is_none());
                assert_eq!(count, 0, "tolerant API preserves the additional view too");
            }
        })
        .await;
    }

    #[tokio::test]
    async fn runtime_count_survives_both_underfilled_and_full_pages() {
        bounded(async {
            let runtime = KhiveRuntime::memory().unwrap();
            let token = NamespaceToken::local();
            let ns = token.namespace().as_str();
            let active = seed(
                &runtime,
                &token,
                note(ns, "observation", "active"),
                ns,
                "observation",
            )
            .await;
            seed(
                &runtime,
                &token,
                note(ns, "observation", "archived"),
                ns,
                "observation",
            )
            .await;
            for limit in [1, 4] {
                let (hits, dropped) =
                    select(&runtime, &token, NoteStatusSelector::Active, limit).await;
                assert_eq!(ids(&hits), HashSet::from([active]));
                assert_eq!(
                    dropped, 1,
                    "count is independent of full versus underfilled presentation"
                );
                assert_eq!(hits.len() < limit as usize, limit == 4);
            }
        })
        .await;
    }

    #[tokio::test]
    async fn archive_heavy_search_keeps_existing_four_times_window() {
        bounded(async {
            let runtime = KhiveRuntime::memory().unwrap();
            let token = NamespaceToken::local();
            let ns = token.namespace().as_str();
            let mut all = HashSet::new();
            for _ in 0..5 {
                all.insert(
                    seed(
                        &runtime,
                        &token,
                        note(ns, "observation", "active"),
                        ns,
                        "observation",
                    )
                    .await,
                );
            }
            // Choose the archive set from actual backend ordering rather than
            // assuming UUID/rowid tie-breaking. Status updates do not reindex.
            let gathered = raw_candidates(&runtime, &token, 4).await;
            assert_eq!(gathered.len(), 4);
            let outside: HashSet<_> = all.difference(&gathered).copied().collect();
            assert_eq!(outside.len(), 1);
            for id in &gathered {
                let mut row = runtime
                    .notes(&token)
                    .unwrap()
                    .get_note(*id)
                    .await
                    .unwrap()
                    .unwrap();
                row.status = "archived".into();
                runtime.backend().upsert_note(row).await.unwrap();
            }
            assert_eq!(raw_candidates(&runtime, &token, 4).await, gathered);
            let (hits, dropped) = select(&runtime, &token, NoteStatusSelector::Active, 1).await;
            assert!(
                hits.is_empty(),
                "bounded search must not silently refill outside its four candidates"
            );
            assert_eq!(dropped, 4);
            let outside_id = *outside.iter().next().unwrap();
            let outside_note = runtime
                .notes(&token)
                .unwrap()
                .get_note(outside_id)
                .await
                .unwrap()
                .unwrap();
            assert_eq!(
                outside_note.status, "active",
                "the bounded result is not eligible-top-k complete"
            );
        })
        .await;
    }

    #[tokio::test]
    async fn healthy_empty_and_all_matching_searches_return_zero_count() {
        bounded(async {
            let runtime = KhiveRuntime::memory().unwrap();
            let token = NamespaceToken::local();
            for selector in [
                NoteStatusSelector::Active,
                NoteStatusSelector::Archived,
                NoteStatusSelector::Any,
            ] {
                let (hits, count) = select(&runtime, &token, selector, 10).await;
                assert!(hits.is_empty());
                assert_eq!(count, 0);
                let (outcome, count) = runtime
                    .search_notes_outcome_with_text_mode_and_status(
                        &token,
                        QUERY,
                        10,
                        None,
                        true,
                        &[],
                        None,
                        TextQueryMode::Plain,
                        selector,
                        None,
                    )
                    .await
                    .unwrap();
                assert!(outcome.hits.is_empty());
                assert!(outcome.vector_error.is_none());
                assert_eq!(count, 0);
            }
            let ns = token.namespace().as_str();
            let id = seed(
                &runtime,
                &token,
                note(ns, "observation", "active"),
                ns,
                "observation",
            )
            .await;
            let (hits, count) = select(&runtime, &token, NoteStatusSelector::Active, 10).await;
            assert_eq!(ids(&hits), HashSet::from([id]));
            assert_eq!(count, 0);
        })
        .await;
    }

    #[tokio::test]
    async fn later_superseded_filter_does_not_revise_status_count() {
        bounded(async {
            let runtime = KhiveRuntime::memory().unwrap();
            let token = NamespaceToken::local();
            let mut old = runtime
                .create_note(&token, "observation", None, QUERY, None, None, vec![])
                .await
                .unwrap();
            let new = runtime
                .create_note(&token, "observation", None, QUERY, None, None, vec![])
                .await
                .unwrap();
            runtime
                .link(&token, new.id, old.id, EdgeRelation::Supersedes, 1.0, None)
                .await
                .unwrap();
            old.status = "archived".into();
            runtime.backend().upsert_note(old.clone()).await.unwrap();
            assert_eq!(
                raw_candidates(&runtime, &token, 40).await,
                HashSet::from([old.id, new.id])
            );
            let (all, count) = select(&runtime, &token, NoteStatusSelector::Any, 10).await;
            assert_eq!(ids(&all), HashSet::from([old.id, new.id]));
            assert_eq!(count, 0);
            for (selector, expected, count) in [
                (NoteStatusSelector::Active, HashSet::from([new.id]), 1),
                (NoteStatusSelector::Archived, HashSet::new(), 1),
                (NoteStatusSelector::Any, HashSet::from([new.id]), 0),
            ] {
                let (hits, dropped) = runtime
                    .search_notes_with_text_mode_and_status(
                        &token,
                        QUERY,
                        None,
                        10,
                        None,
                        false,
                        &[],
                        None,
                        TextQueryMode::Plain,
                        selector,
                        None,
                    )
                    .await
                    .unwrap();
                assert_eq!(ids(&hits), expected);
                assert_eq!(
                    dropped, count,
                    "superseded filtering is later than status accounting"
                );
            }
        })
        .await;
    }

    struct FixtureEmbedder {
        name: String,
        dimensions: usize,
        fail: bool,
    }
    struct FixtureService {
        dimensions: usize,
        fail: bool,
    }

    #[async_trait]
    impl EmbedderProvider for FixtureEmbedder {
        fn name(&self) -> &str {
            &self.name
        }
        fn dimensions(&self) -> usize {
            self.dimensions
        }
        async fn build(&self) -> crate::error::RuntimeResult<Arc<dyn EmbeddingService>> {
            Ok(Arc::new(FixtureService {
                dimensions: self.dimensions,
                fail: self.fail,
            }))
        }
    }

    #[async_trait]
    impl EmbeddingService for FixtureService {
        async fn embed(
            &self,
            texts: &[String],
            _model: EmbeddingModel,
        ) -> Result<Vec<Vec<f32>>, EmbedError> {
            if self.fail {
                Err(EmbedError::InferenceFailed(
                    "search status fixture vector failure".into(),
                ))
            } else {
                Ok(texts
                    .iter()
                    .map(|_| vec![1.0_f32; self.dimensions])
                    .collect())
            }
        }
        fn supports_model(&self, _model: EmbeddingModel) -> bool {
            true
        }
        fn name(&self) -> &'static str {
            "search-status-fixture"
        }
    }

    #[tokio::test]
    async fn same_archived_note_from_both_arms_counts_once() {
        bounded(async {
            let model = EmbeddingModel::AllMiniLmL6V2;
            let runtime = KhiveRuntime::new(crate::RuntimeConfig {
                db_path: None,
                embedding_model: Some(model),
                ..crate::RuntimeConfig::no_embeddings()
            })
            .unwrap();
            let token = NamespaceToken::local();
            runtime.register_embedder(FixtureEmbedder {
                name: model.to_string(),
                dimensions: model.dimensions(),
                fail: false,
            });
            let mut row = runtime
                .create_note(&token, "observation", None, QUERY, None, None, vec![])
                .await
                .unwrap();
            let id = row.id;
            row.status = "archived".into();
            runtime.backend().upsert_note(row).await.unwrap();
            let (any, count) = select(&runtime, &token, NoteStatusSelector::Any, 10).await;
            assert_eq!(ids(&any), HashSet::from([id]));
            assert_eq!(any.len(), 1);
            assert_eq!(
                any[0].source,
                crate::SearchSource::Both,
                "fixture must actually exercise both arms"
            );
            assert_eq!(count, 0);
            let (explicit_vector, count) = runtime
                .search_notes_with_text_mode_and_status(
                    &token,
                    QUERY,
                    Some(vec![1.0_f32; model.dimensions()]),
                    10,
                    None,
                    true,
                    &[],
                    None,
                    TextQueryMode::Plain,
                    NoteStatusSelector::Any,
                    None,
                )
                .await
                .unwrap();
            assert_eq!(ids(&explicit_vector), HashSet::from([id]));
            assert_eq!(explicit_vector[0].source, crate::SearchSource::Both);
            assert_eq!(count, 0);
            let (active, count) = select(&runtime, &token, NoteStatusSelector::Active, 10).await;
            assert!(active.is_empty());
            assert_eq!(count, 1);
        })
        .await;
    }

    #[tokio::test]
    async fn tolerant_selector_keeps_text_and_count_when_strict_vector_fails() {
        bounded(async {
            let model = EmbeddingModel::AllMiniLmL6V2;
            let runtime = KhiveRuntime::new(crate::RuntimeConfig {
                db_path: None,
                embedding_model: Some(model),
                ..crate::RuntimeConfig::no_embeddings()
            })
            .unwrap();
            let token = NamespaceToken::local();
            let ns = token.namespace().as_str();
            let active = seed(
                &runtime,
                &token,
                note(ns, "observation", "active"),
                ns,
                "observation",
            )
            .await;
            seed(
                &runtime,
                &token,
                note(ns, "observation", "archived"),
                ns,
                "observation",
            )
            .await;
            runtime.register_embedder(FixtureEmbedder {
                name: model.to_string(),
                dimensions: model.dimensions(),
                fail: true,
            });
            let error = runtime
                .search_notes_with_text_mode_and_status(
                    &token,
                    QUERY,
                    None,
                    10,
                    None,
                    true,
                    &[],
                    None,
                    TextQueryMode::Plain,
                    NoteStatusSelector::Active,
                    None,
                )
                .await
                .unwrap_err();
            assert!(error
                .to_string()
                .contains("search status fixture vector failure"));
            let (outcome, count) = runtime
                .search_notes_outcome_with_text_mode_and_status(
                    &token,
                    QUERY,
                    10,
                    None,
                    true,
                    &[],
                    None,
                    TextQueryMode::Plain,
                    NoteStatusSelector::Active,
                    None,
                )
                .await
                .unwrap();
            assert_eq!(ids(&outcome.hits), HashSet::from([active]));
            assert_eq!(count, 1);
            assert!(outcome
                .vector_error
                .as_deref()
                .unwrap()
                .contains("search status fixture vector failure"));
        })
        .await;
    }
}