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
//! Search result explainability (RML-1242)
//!
//! Provides human-readable explanations of why a search result ranked where it
//! did, including per-signal score breakdowns and contribution percentages.
//!
//! This module is purely computational — it performs no database access.
use serde::{Deserialize, Serialize};
/// Breakdown of individual scoring signals for a search result.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ScoreBreakdown {
/// BM25 keyword relevance score (0.0–1.0)
pub bm25_score: f32,
/// Vector / semantic similarity score (0.0–1.0)
pub vector_score: f32,
/// Fuzzy match score (0.0–1.0)
pub fuzzy_score: f32,
/// Recency boost factor applied during reranking
pub recency_boost: f32,
/// Importance weight derived from `memory.importance`
pub importance_weight: f32,
/// Cross-encoder reranking score (`None` when the reranker is not active)
pub rerank_score: Option<f32>,
/// Final combined score after RRF / reranking
pub final_score: f32,
}
/// A named signal and its contribution to the final score.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SignalContribution {
/// Signal name, e.g. `"semantic similarity"`.
pub signal: String,
/// Raw score for this signal.
pub score: f32,
/// Percentage contribution to the final score (0–100).
pub contribution_pct: f32,
}
/// Human-readable explanation of why a result ranked where it did.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchExplanation {
/// Memory ID of the explained result.
pub memory_id: i64,
/// 1-based rank position.
pub rank: usize,
/// Per-signal score breakdown.
pub scores: ScoreBreakdown,
/// Human-readable explanation text.
pub explanation: String,
/// Signals sorted by contribution percentage (descending).
pub top_signals: Vec<SignalContribution>,
}
/// Generates [`SearchExplanation`] values for search results.
pub struct SearchExplainer {
/// RRF *k* parameter (should match `SearchConfig::rrf_k`).
pub rrf_k: f32,
/// Whether a cross-encoder reranker was active during this search.
pub reranking_active: bool,
}
impl SearchExplainer {
/// Create a new explainer.
pub fn new(rrf_k: f32, reranking_active: bool) -> Self {
Self {
rrf_k,
reranking_active,
}
}
/// Explain a single search result.
///
/// # Parameters
/// * `memory_id` — ID of the memory being explained.
/// * `rank` — 1-based rank position in the result set.
/// * `bm25` — BM25 keyword relevance score.
/// * `vector` — Vector/semantic similarity score.
/// * `fuzzy` — Fuzzy match score.
/// * `recency` — Recency boost applied.
/// * `importance` — Importance weight.
/// * `rerank` — Cross-encoder score (`None` if reranker inactive).
/// * `final_score` — Final combined score after fusion/reranking.
#[allow(clippy::too_many_arguments)]
pub fn explain_result(
&self,
memory_id: i64,
rank: usize,
bm25: f32,
vector: f32,
fuzzy: f32,
recency: f32,
importance: f32,
rerank: Option<f32>,
final_score: f32,
) -> SearchExplanation {
let scores = ScoreBreakdown {
bm25_score: bm25,
vector_score: vector,
fuzzy_score: fuzzy,
recency_boost: recency,
importance_weight: importance,
rerank_score: rerank,
final_score,
};
let top_signals = self.compute_signal_contributions(&scores);
let explanation = self.generate_explanation(rank, &scores, &top_signals);
SearchExplanation {
memory_id,
rank,
scores,
explanation,
top_signals,
}
}
/// Explain all results in a batch, assigning ranks 1..N in the order given.
///
/// Each tuple is `(memory_id, bm25, vector, fuzzy, recency, importance,
/// rerank, final_score)`.
pub fn explain_batch(
&self,
results: Vec<(i64, f32, f32, f32, f32, f32, Option<f32>, f32)>,
) -> Vec<SearchExplanation> {
results
.into_iter()
.enumerate()
.map(
|(
i,
(memory_id, bm25, vector, fuzzy, recency, importance, rerank, final_score),
)| {
self.explain_result(
memory_id,
i + 1,
bm25,
vector,
fuzzy,
recency,
importance,
rerank,
final_score,
)
},
)
.collect()
}
/// Build the human-readable explanation string.
pub fn generate_explanation(
&self,
rank: usize,
scores: &ScoreBreakdown,
signals: &[SignalContribution],
) -> String {
let mut parts: Vec<String> = Vec::new();
// Lead: rank + final score
parts.push(format!(
"Ranked #{rank} (score: {:.2}).",
scores.final_score
));
// Primary signal
if let Some(primary) = signals.first() {
parts.push(format!(
"Primary signal: {} ({:.0}%).",
primary.signal, primary.contribution_pct
));
}
// Secondary signals (up to 3 more)
for signal in signals.iter().skip(1).take(3) {
if signal.contribution_pct >= 1.0 {
// Only mention signals that meaningfully contributed
let verb = match signal.signal.as_str() {
"BM25 keyword match" => "BM25 keyword match contributed",
"recency boost" => "Recency boost added",
"importance weight" => "Importance weight contributed",
"fuzzy match" => "Fuzzy match contributed",
_ => "contributed",
};
parts.push(format!("{} {:.0}%.", verb, signal.contribution_pct));
}
}
// Cross-encoder note
if self.reranking_active && scores.rerank_score.is_some() {
parts.push("Cross-encoder reranking confirmed relevance.".to_string());
}
parts.join(" ")
}
// ------------------------------------------------------------------ //
// Private helpers //
// ------------------------------------------------------------------ //
/// Map raw signal scores to [`SignalContribution`] sorted by contribution.
fn compute_signal_contributions(&self, scores: &ScoreBreakdown) -> Vec<SignalContribution> {
let mut raw: Vec<(&str, f32)> = vec![
("semantic similarity", scores.vector_score),
("BM25 keyword match", scores.bm25_score),
("fuzzy match", scores.fuzzy_score),
("recency boost", scores.recency_boost),
("importance weight", scores.importance_weight),
];
// Include cross-encoder only when the reranker is active
if self.reranking_active {
if let Some(rs) = scores.rerank_score {
raw.push(("cross-encoder reranking", rs));
}
}
let total: f32 = raw.iter().map(|(_, s)| s).sum();
let mut contributions: Vec<SignalContribution> = raw
.into_iter()
.map(|(name, score)| {
let contribution_pct = if total > 0.0 {
(score / total) * 100.0
} else {
// Equal contribution when all scores are zero
0.0
};
SignalContribution {
signal: name.to_string(),
score,
contribution_pct,
}
})
.collect();
// Sort descending by contribution percentage
contributions.sort_by(|a, b| {
b.contribution_pct
.partial_cmp(&a.contribution_pct)
.unwrap_or(std::cmp::Ordering::Equal)
});
contributions
}
}
impl Default for SearchExplainer {
fn default() -> Self {
Self::new(60.0, false)
}
}
// ------------------------------------------------------------------ //
// Tests //
// ------------------------------------------------------------------ //
#[cfg(test)]
mod tests {
use super::*;
fn make_explainer() -> SearchExplainer {
SearchExplainer::new(60.0, true)
}
// Helper: produce a deterministic explanation for most tests.
fn default_explanation(explainer: &SearchExplainer) -> SearchExplanation {
explainer.explain_result(
42, // memory_id
1, // rank
0.5, // bm25
0.8, // vector
0.3, // fuzzy
0.1, // recency
0.6, // importance
Some(0.7), // rerank
0.85, // final_score
)
}
// ------------------------------------------------------------------ //
// Test 1: Single result has all required fields populated //
// ------------------------------------------------------------------ //
#[test]
fn test_single_result_has_all_fields() {
let explainer = make_explainer();
let exp = default_explanation(&explainer);
assert_eq!(exp.memory_id, 42);
assert_eq!(exp.rank, 1);
assert!((exp.scores.final_score - 0.85).abs() < f32::EPSILON);
assert!(!exp.explanation.is_empty());
assert!(!exp.top_signals.is_empty());
}
// ------------------------------------------------------------------ //
// Test 2: Top signals are sorted by contribution (descending) //
// ------------------------------------------------------------------ //
#[test]
fn test_top_signals_sorted_descending() {
let explainer = make_explainer();
let exp = default_explanation(&explainer);
for window in exp.top_signals.windows(2) {
assert!(
window[0].contribution_pct >= window[1].contribution_pct,
"signals not sorted: {} ({:.2}%) before {} ({:.2}%)",
window[0].signal,
window[0].contribution_pct,
window[1].signal,
window[1].contribution_pct
);
}
}
// ------------------------------------------------------------------ //
// Test 3: Contribution percentages sum to ~100 % //
// ------------------------------------------------------------------ //
#[test]
fn test_contribution_percentages_sum_to_100() {
let explainer = make_explainer();
let exp = default_explanation(&explainer);
let total: f32 = exp.top_signals.iter().map(|s| s.contribution_pct).sum();
assert!(
(total - 100.0).abs() < 0.1,
"percentages sum to {total:.2}, expected ~100"
);
}
// ------------------------------------------------------------------ //
// Test 4: Rerank score included when reranker is active //
// ------------------------------------------------------------------ //
#[test]
fn test_rerank_score_included_when_active() {
let explainer = SearchExplainer::new(60.0, true);
let exp = explainer.explain_result(1, 1, 0.4, 0.6, 0.2, 0.05, 0.5, Some(0.9), 0.75);
assert!(
exp.scores.rerank_score.is_some(),
"rerank_score should be Some when active"
);
// Cross-encoder signal must appear in top_signals
assert!(
exp.top_signals
.iter()
.any(|s| s.signal == "cross-encoder reranking"),
"cross-encoder signal missing from top_signals"
);
}
// ------------------------------------------------------------------ //
// Test 5: Rerank score is None when reranker is inactive //
// ------------------------------------------------------------------ //
#[test]
fn test_rerank_score_none_when_inactive() {
let explainer = SearchExplainer::new(60.0, false);
// Pass Some(0.9) as raw input but the explainer is inactive — the
// stored rerank_score comes straight from the caller's value, but
// the signal must NOT appear in top_signals.
let exp = explainer.explain_result(1, 1, 0.4, 0.6, 0.2, 0.05, 0.5, None, 0.75);
assert!(
exp.scores.rerank_score.is_none(),
"rerank_score should be None when inactive"
);
assert!(
!exp.top_signals
.iter()
.any(|s| s.signal == "cross-encoder reranking"),
"cross-encoder signal must not appear when reranker is inactive"
);
}
// ------------------------------------------------------------------ //
// Test 6: Batch explanation assigns correct sequential ranks //
// ------------------------------------------------------------------ //
#[test]
fn test_batch_assigns_correct_ranks() {
let explainer = SearchExplainer::new(60.0, false);
let results = vec![
(
1_i64, 0.9_f32, 0.8_f32, 0.1_f32, 0.05_f32, 0.7_f32, None, 0.92_f32,
),
(
2_i64, 0.7_f32, 0.6_f32, 0.0_f32, 0.02_f32, 0.5_f32, None, 0.72_f32,
),
(
3_i64, 0.5_f32, 0.4_f32, 0.2_f32, 0.01_f32, 0.3_f32, None, 0.55_f32,
),
];
let explanations = explainer.explain_batch(results);
assert_eq!(explanations.len(), 3);
for (i, exp) in explanations.iter().enumerate() {
assert_eq!(exp.rank, i + 1, "rank mismatch at index {i}");
}
assert_eq!(explanations[0].memory_id, 1);
assert_eq!(explanations[1].memory_id, 2);
assert_eq!(explanations[2].memory_id, 3);
}
// ------------------------------------------------------------------ //
// Test 7: Human-readable text contains rank and top signal name //
// ------------------------------------------------------------------ //
#[test]
fn test_explanation_text_contains_rank_and_top_signal() {
let explainer = make_explainer();
let exp = default_explanation(&explainer);
assert!(
exp.explanation.contains("#1"),
"explanation should reference rank #1: {:?}",
exp.explanation
);
let top_signal_name = &exp.top_signals[0].signal;
assert!(
exp.explanation.contains(top_signal_name.as_str()),
"explanation should mention top signal '{top_signal_name}': {:?}",
exp.explanation
);
}
// ------------------------------------------------------------------ //
// Test 8: Zero scores handled gracefully (no panic, 0% contributions) //
// ------------------------------------------------------------------ //
#[test]
fn test_zero_scores_handled_gracefully() {
let explainer = SearchExplainer::new(60.0, false);
let exp = explainer.explain_result(99, 5, 0.0, 0.0, 0.0, 0.0, 0.0, None, 0.0);
// Should not panic; all contributions are 0
for signal in &exp.top_signals {
assert!(
(signal.contribution_pct - 0.0).abs() < f32::EPSILON,
"expected 0% contribution, got {:.2}% for {}",
signal.contribution_pct,
signal.signal
);
}
// Explanation should still be generated
assert!(exp.explanation.contains("#5"));
}
// ------------------------------------------------------------------ //
// Test 9: All signals at equal score → roughly equal contributions //
// ------------------------------------------------------------------ //
#[test]
fn test_equal_signals_have_roughly_equal_contributions() {
let explainer = SearchExplainer::new(60.0, true);
// Six signals each at 1.0 (5 base + 1 rerank)
let exp = explainer.explain_result(7, 2, 1.0, 1.0, 1.0, 1.0, 1.0, Some(1.0), 1.0);
let expected_pct = 100.0 / 6.0;
for signal in &exp.top_signals {
assert!(
(signal.contribution_pct - expected_pct).abs() < 1.0,
"signal '{}' has {:.2}%, expected ~{:.2}%",
signal.signal,
signal.contribution_pct,
expected_pct
);
}
}
}