khive-retrieval 0.7.0

Hybrid retrieval composer (HNSW + BM25 + fusion + graph + cross-encoder) with deterministic scoring
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
//! Query Intermediate Representation for the retrieval pipeline.
//!
//! Composable IR tree representing how sub-queries are structured, filtered, and fused,
//! separate from the Query struct which captures what to search.

use khive_score::DeterministicScore;
use serde::{Deserialize, Serialize};

// ---------------------------------------------------------------------------
// Core IR node
// ---------------------------------------------------------------------------

/// A node in the Query IR tree.
///
/// Each variant represents a single retrieval operation or combinator.
/// Nodes compose recursively -- a `Fuse` holds children, a `Filter` wraps
/// a single child, and leaf nodes (`Vector`, `Keyword`, `Empty`) terminate
/// the tree.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum QueryNode {
    /// Vector similarity search (e.g. HNSW nearest-neighbor).
    Vector {
        /// Pre-computed query embedding.
        embedding: Vec<f32>,
        /// Number of results to return.
        top_k: usize,
        /// Optional minimum similarity threshold.
        min_score: Option<DeterministicScore>,
    },

    /// Keyword / BM25 text search.
    Keyword {
        /// Query text.
        text: String,
        /// Number of results to return.
        top_k: usize,
        /// Optional minimum relevance threshold.
        min_score: Option<DeterministicScore>,
    },

    /// Fuse multiple sub-queries into a single ranked list.
    Fuse {
        /// Sub-queries to fuse.
        children: Vec<QueryNode>,
        /// Strategy for combining ranked lists.
        strategy: FuseStrategy,
        /// Number of results after fusion.
        top_k: usize,
    },

    /// Filter the results of a sub-query.
    Filter {
        /// The sub-query whose results are filtered.
        child: Box<QueryNode>,
        /// Predicate to apply.
        predicate: FilterPredicate,
    },

    /// Rerank the results of a sub-query.
    Rerank {
        /// The sub-query whose results are reranked.
        child: Box<QueryNode>,
        /// Reranking method.
        method: RerankMethod,
        /// Number of results after reranking.
        top_k: usize,
    },

    /// An empty query that is guaranteed to produce no results.
    ///
    /// Useful as the result of constant-folding provably-empty sub-trees.
    Empty,
}

// ---------------------------------------------------------------------------
// Supporting enums
// ---------------------------------------------------------------------------

/// Fusion strategy for combining sub-query result lists.
///
/// Mirrors [`FusionStrategy`](crate::FusionStrategy) at the IR level
/// so that the query plan is self-contained and serialisable without depending
/// on runtime fusion internals.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FuseStrategy {
    /// Reciprocal Rank Fusion with smoothing constant `k`.
    ///
    /// Standard default: k = 60 (Cormack et al., 2009).
    Rrf {
        /// Smoothing constant.
        k: usize,
    },

    /// Weighted linear combination of scores.
    ///
    /// One weight per child; weights are normalised at execution time.
    Weighted {
        /// Per-child weights (will be normalised to sum to 1.0).
        weights: Vec<f64>,
    },

    /// Union with max-score-per-document semantics.
    Union,
}

/// Predicate for post-retrieval filtering.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum FilterPredicate {
    /// Keep only results whose score meets a minimum threshold.
    MinScore(DeterministicScore),

    /// Keep at most `k` results (top-k truncation).
    TopK(usize),

    /// Keep results where a metadata field equals a given value.
    MetadataEquals {
        /// Metadata field name.
        field: String,
        /// Expected value (JSON).
        value: serde_json::Value,
    },

    /// All contained predicates must hold (conjunction).
    And(Vec<FilterPredicate>),

    /// At least one contained predicate must hold (disjunction).
    Or(Vec<FilterPredicate>),
}

/// Method for reranking search results.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RerankMethod {
    /// Cross-encoder neural reranking (placeholder for future integration).
    CrossEncoder {
        /// Model identifier.
        model: String,
    },

    /// Score-based reranking with custom per-signal weights.
    ScoreWeighted {
        /// Weights applied to each scoring signal.
        weights: Vec<f64>,
    },
}

// ---------------------------------------------------------------------------
// Construction helpers
// ---------------------------------------------------------------------------

impl QueryNode {
    /// Create a vector search leaf node.
    pub fn vector(embedding: Vec<f32>, top_k: usize) -> Self {
        QueryNode::Vector {
            embedding,
            top_k,
            min_score: None,
        }
    }

    /// Create a keyword search leaf node.
    pub fn keyword(text: impl Into<String>, top_k: usize) -> Self {
        QueryNode::Keyword {
            text: text.into(),
            top_k,
            min_score: None,
        }
    }

    /// Create a hybrid query (vector + keyword with RRF fusion, `top_k * 3` candidates each).
    pub fn hybrid(embedding: Vec<f32>, text: impl Into<String>, top_k: usize) -> Self {
        let candidate_k = top_k.saturating_mul(3);
        QueryNode::Fuse {
            children: vec![
                QueryNode::vector(embedding, candidate_k),
                QueryNode::keyword(text, candidate_k),
            ],
            strategy: FuseStrategy::Rrf { k: 60 },
            top_k,
        }
    }

    /// Wrap this node with a minimum-score filter.
    #[must_use]
    pub fn with_min_score(self, min_score: DeterministicScore) -> Self {
        QueryNode::Filter {
            child: Box::new(self),
            predicate: FilterPredicate::MinScore(min_score),
        }
    }

    /// Wrap this node with a top-k truncation filter.
    #[must_use]
    pub fn with_top_k(self, k: usize) -> Self {
        QueryNode::Filter {
            child: Box::new(self),
            predicate: FilterPredicate::TopK(k),
        }
    }

    // -----------------------------------------------------------------------
    // Analysis helpers
    // -----------------------------------------------------------------------

    /// Returns `true` if this query is provably empty (no results possible).
    ///
    /// A query is provably empty when:
    /// - It is the `Empty` variant.
    /// - A leaf has `top_k == 0`.
    /// - A keyword leaf has empty text.
    /// - A fuse node has no children.
    /// - A filter/rerank wraps a provably-empty child.
    pub fn is_empty(&self) -> bool {
        match self {
            QueryNode::Empty => true,
            QueryNode::Vector { top_k: 0, .. } => true,
            QueryNode::Keyword { top_k: 0, .. } => true,
            QueryNode::Keyword { text, .. } if text.is_empty() => true,
            QueryNode::Fuse { children, .. } if children.is_empty() => true,
            QueryNode::Filter { child, .. } => child.is_empty(),
            QueryNode::Rerank { child, .. } => child.is_empty(),
            _ => false,
        }
    }

    /// Count the total number of leaf search operations in the tree.
    ///
    /// `Vector` and `Keyword` nodes each count as 1.  `Empty` counts as 0.
    /// Combinators recurse into their children.
    pub fn leaf_count(&self) -> usize {
        match self {
            QueryNode::Vector { .. } | QueryNode::Keyword { .. } => 1,
            QueryNode::Fuse { children, .. } => children.iter().map(|c| c.leaf_count()).sum(),
            QueryNode::Filter { child, .. } | QueryNode::Rerank { child, .. } => child.leaf_count(),
            QueryNode::Empty => 0,
        }
    }

    /// Return the effective `top_k` requested by this node.
    ///
    /// For `Filter` nodes with a `TopK` predicate, the predicate's value is
    /// returned.  Otherwise the child's `top_k` propagates upward.
    pub fn top_k(&self) -> usize {
        match self {
            QueryNode::Vector { top_k, .. } => *top_k,
            QueryNode::Keyword { top_k, .. } => *top_k,
            QueryNode::Fuse { top_k, .. } => *top_k,
            QueryNode::Filter { child, predicate } => match predicate {
                FilterPredicate::TopK(k) => *k,
                _ => child.top_k(),
            },
            QueryNode::Rerank { top_k, .. } => *top_k,
            QueryNode::Empty => 0,
        }
    }

    /// Return the depth of the IR tree (longest root-to-leaf path).
    ///
    /// Leaf nodes have depth 1.  `Empty` has depth 0.
    pub fn depth(&self) -> usize {
        match self {
            QueryNode::Empty => 0,
            QueryNode::Vector { .. } | QueryNode::Keyword { .. } => 1,
            QueryNode::Fuse { children, .. } => {
                1 + children.iter().map(|c| c.depth()).max().unwrap_or(0)
            }
            QueryNode::Filter { child, .. } | QueryNode::Rerank { child, .. } => 1 + child.depth(),
        }
    }
}

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

#[cfg(test)]
#[allow(clippy::uninlined_format_args)]
mod tests {
    use super::*;

    // -- Construction -------------------------------------------------------

    #[test]
    fn test_vector_construction() {
        let emb = vec![0.1, 0.2, 0.3];
        let node = QueryNode::vector(emb.clone(), 10);
        match &node {
            QueryNode::Vector {
                embedding,
                top_k,
                min_score,
            } => {
                assert_eq!(embedding, &emb);
                assert_eq!(*top_k, 10);
                assert!(min_score.is_none());
            }
            other => panic!("expected Vector, got {:?}", other),
        }
    }

    #[test]
    fn test_keyword_construction() {
        let node = QueryNode::keyword("hello world", 5);
        match &node {
            QueryNode::Keyword {
                text,
                top_k,
                min_score,
            } => {
                assert_eq!(text, "hello world");
                assert_eq!(*top_k, 5);
                assert!(min_score.is_none());
            }
            other => panic!("expected Keyword, got {:?}", other),
        }
    }

    #[test]
    fn test_hybrid_construction() {
        let emb = vec![0.1_f32; 128];
        let node = QueryNode::hybrid(emb, "distributed consensus", 10);
        match &node {
            QueryNode::Fuse {
                children,
                strategy,
                top_k,
            } => {
                assert_eq!(children.len(), 2);
                assert_eq!(*top_k, 10);
                // Sub-queries should request 3x candidates.
                assert_eq!(children[0].top_k(), 30);
                assert_eq!(children[1].top_k(), 30);
                assert!(matches!(strategy, FuseStrategy::Rrf { k: 60 }));
            }
            other => panic!("expected Fuse, got {:?}", other),
        }
    }

    // -- is_empty -----------------------------------------------------------

    #[test]
    fn test_empty_variant() {
        assert!(QueryNode::Empty.is_empty());
        assert_eq!(QueryNode::Empty.leaf_count(), 0);
        assert_eq!(QueryNode::Empty.top_k(), 0);
        assert_eq!(QueryNode::Empty.depth(), 0);
    }

    #[test]
    fn test_vector_top_k_zero_is_empty() {
        let node = QueryNode::vector(vec![1.0], 0);
        assert!(node.is_empty());
    }

    #[test]
    fn test_keyword_top_k_zero_is_empty() {
        let node = QueryNode::keyword("hello", 0);
        assert!(node.is_empty());
    }

    #[test]
    fn test_keyword_empty_text_is_empty() {
        let node = QueryNode::keyword("", 10);
        assert!(node.is_empty());
    }

    #[test]
    fn test_fuse_no_children_is_empty() {
        let node = QueryNode::Fuse {
            children: vec![],
            strategy: FuseStrategy::Rrf { k: 60 },
            top_k: 10,
        };
        assert!(node.is_empty());
    }

    #[test]
    fn test_filter_of_empty_is_empty() {
        let node = QueryNode::Empty.with_min_score(DeterministicScore::from_f64(0.5));
        assert!(node.is_empty());
    }

    #[test]
    fn test_rerank_of_empty_is_empty() {
        let node = QueryNode::Rerank {
            child: Box::new(QueryNode::Empty),
            method: RerankMethod::ScoreWeighted { weights: vec![1.0] },
            top_k: 10,
        };
        assert!(node.is_empty());
    }

    #[test]
    fn test_non_empty_query() {
        let node = QueryNode::keyword("hello", 5);
        assert!(!node.is_empty());
    }

    // -- leaf_count ---------------------------------------------------------

    #[test]
    fn test_leaf_count_single() {
        assert_eq!(QueryNode::vector(vec![1.0], 5).leaf_count(), 1);
        assert_eq!(QueryNode::keyword("q", 5).leaf_count(), 1);
    }

    #[test]
    fn test_leaf_count_hybrid() {
        let q = QueryNode::hybrid(vec![1.0], "q", 10);
        assert_eq!(q.leaf_count(), 2);
    }

    #[test]
    fn test_leaf_count_nested() {
        // Fuse(Fuse(vec, kw), kw) = 3 leaves
        let inner = QueryNode::hybrid(vec![1.0], "inner", 10);
        let outer = QueryNode::Fuse {
            children: vec![inner, QueryNode::keyword("outer", 10)],
            strategy: FuseStrategy::Union,
            top_k: 10,
        };
        assert_eq!(outer.leaf_count(), 3);
    }

    // -- top_k --------------------------------------------------------------

    #[test]
    fn test_top_k_leaf() {
        assert_eq!(QueryNode::vector(vec![], 7).top_k(), 7);
        assert_eq!(QueryNode::keyword("q", 3).top_k(), 3);
    }

    #[test]
    fn test_top_k_fuse() {
        let q = QueryNode::hybrid(vec![1.0], "q", 15);
        assert_eq!(q.top_k(), 15);
    }

    #[test]
    fn test_top_k_filter_topk_predicate() {
        let node = QueryNode::keyword("q", 100).with_top_k(5);
        assert_eq!(node.top_k(), 5);
    }

    #[test]
    fn test_top_k_filter_non_topk_predicate() {
        let node = QueryNode::keyword("q", 20).with_min_score(DeterministicScore::from_f64(0.5));
        // min_score filter doesn't change top_k -- falls through to child.
        assert_eq!(node.top_k(), 20);
    }

    // -- depth --------------------------------------------------------------

    #[test]
    fn test_depth_leaf() {
        assert_eq!(QueryNode::vector(vec![1.0], 5).depth(), 1);
        assert_eq!(QueryNode::keyword("q", 5).depth(), 1);
    }

    #[test]
    fn test_depth_hybrid() {
        let q = QueryNode::hybrid(vec![1.0], "q", 10);
        // Fuse -> leaf = depth 2
        assert_eq!(q.depth(), 2);
    }

    #[test]
    fn test_depth_chained_filters() {
        let q = QueryNode::keyword("q", 10)
            .with_min_score(DeterministicScore::from_f64(0.5))
            .with_top_k(5);
        // TopK(Filter(MinScore(Keyword))) = 3 wrappers + 1 leaf = depth 3
        assert_eq!(q.depth(), 3);
    }

    // -- with_min_score / with_top_k chaining -------------------------------

    #[test]
    fn test_builder_chaining() {
        let node = QueryNode::keyword("rust async patterns", 20)
            .with_min_score(DeterministicScore::from_f64(0.3))
            .with_top_k(10);

        assert_eq!(node.top_k(), 10);
        assert_eq!(node.leaf_count(), 1);
        assert!(!node.is_empty());
    }

    // -- Serde round-trip ---------------------------------------------------

    #[test]
    fn test_serde_roundtrip_vector() {
        let node = QueryNode::vector(vec![0.1, 0.2, 0.3], 10);
        let json = serde_json::to_string(&node).expect("serialize");
        let back: QueryNode = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.top_k(), 10);
        assert_eq!(back.leaf_count(), 1);
    }

    #[test]
    fn test_serde_roundtrip_keyword() {
        let node = QueryNode::keyword("hello world", 5);
        let json = serde_json::to_string(&node).expect("serialize");
        let back: QueryNode = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.top_k(), 5);
    }

    #[test]
    fn test_serde_roundtrip_hybrid() {
        let node = QueryNode::hybrid(vec![1.0, 2.0], "search query", 10);
        let json = serde_json::to_string(&node).expect("serialize");
        let back: QueryNode = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.top_k(), 10);
        assert_eq!(back.leaf_count(), 2);
    }

    #[test]
    fn test_serde_roundtrip_complex() {
        let node = QueryNode::hybrid(vec![0.5; 4], "complex query", 10)
            .with_min_score(DeterministicScore::from_f64(0.2))
            .with_top_k(5);

        let json = serde_json::to_string_pretty(&node).expect("serialize");
        let back: QueryNode = serde_json::from_str(&json).expect("deserialize");

        assert_eq!(back.top_k(), 5);
        assert_eq!(back.leaf_count(), 2);
        assert!(!back.is_empty());
    }

    #[test]
    fn test_serde_roundtrip_empty() {
        let node = QueryNode::Empty;
        let json = serde_json::to_string(&node).expect("serialize");
        let back: QueryNode = serde_json::from_str(&json).expect("deserialize");
        assert!(back.is_empty());
    }

    #[test]
    fn test_serde_roundtrip_filter_metadata() {
        let node = QueryNode::Filter {
            child: Box::new(QueryNode::keyword("docs", 10)),
            predicate: FilterPredicate::MetadataEquals {
                field: "type".to_string(),
                value: serde_json::json!("memory"),
            },
        };
        let json = serde_json::to_string(&node).expect("serialize");
        let back: QueryNode = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.leaf_count(), 1);
    }

    #[test]
    fn test_serde_roundtrip_rerank() {
        let node = QueryNode::Rerank {
            child: Box::new(QueryNode::keyword("rerank me", 20)),
            method: RerankMethod::CrossEncoder {
                model: "ms-marco-MiniLM".to_string(),
            },
            top_k: 10,
        };
        let json = serde_json::to_string(&node).expect("serialize");
        let back: QueryNode = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.top_k(), 10);
    }

    #[test]
    fn test_serde_roundtrip_compound_predicate() {
        let pred = FilterPredicate::And(vec![
            FilterPredicate::MinScore(DeterministicScore::from_f64(0.3)),
            FilterPredicate::Or(vec![
                FilterPredicate::MetadataEquals {
                    field: "lang".to_string(),
                    value: serde_json::json!("en"),
                },
                FilterPredicate::MetadataEquals {
                    field: "lang".to_string(),
                    value: serde_json::json!("zh"),
                },
            ]),
        ]);
        let node = QueryNode::Filter {
            child: Box::new(QueryNode::keyword("test", 10)),
            predicate: pred,
        };
        let json = serde_json::to_string(&node).expect("serialize");
        let back: QueryNode = serde_json::from_str(&json).expect("deserialize");
        assert_eq!(back.leaf_count(), 1);
    }
}