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
// SPDX-License-Identifier: Apache-2.0 OR MIT
//! Reranker plugin trait — interface stub for slice 1.
//!
//! Slice 1 ships **no implementation**. The trait exists so a future plan
//! adding hosted rerankers (Cohere, Voyage), cross-encoders, or LLM-judges
//! can land without breaking SemVer on `kiromi-ai-memory`. RRF stays
//! hard-coded inside `Memory::search` (see spec § 10).
//!
//! See spec § 12.9.
use async_trait::async_trait;
use crate::error::Result;
/// Reranks search candidates against a query.
#[async_trait]
pub trait Reranker: Send + Sync + std::fmt::Debug + 'static {
/// Stable identifier — analogous to `Embedder::id`.
/// Convention: `"<family>:<model>:<version>"`.
fn id(&self) -> &str;
/// Score each candidate against the query. Returns one `f32` per
/// candidate, in input order. Higher is better. Implementations should
/// L2- or sigmoid-bound the scores so callers can fuse with other
/// rankers without scale shock.
async fn rerank(&self, query: &str, candidates: &[&str]) -> Result<Vec<f32>>;
}
#[cfg(test)]
mod tests {
use super::*;
/// In-tree test impl proving the trait is object-safe and that a future
/// plan can wire one through `Box<dyn Reranker>` without further trait
/// surgery.
#[derive(Debug)]
struct ConstantReranker;
#[async_trait]
impl Reranker for ConstantReranker {
fn id(&self) -> &str {
"test:constant:v1"
}
async fn rerank(&self, _q: &str, candidates: &[&str]) -> Result<Vec<f32>> {
Ok(candidates.iter().map(|_| 0.5_f32).collect())
}
}
#[tokio::test]
async fn object_safe_and_callable() {
let boxed: Box<dyn Reranker> = Box::new(ConstantReranker);
let s = boxed.rerank("q", &["a", "b", "c"]).await.unwrap();
assert_eq!(s, vec![0.5, 0.5, 0.5]);
assert_eq!(boxed.id(), "test:constant:v1");
}
}