zeph-skills 0.20.2

SKILL.md parser, registry, embedding matcher, and hot-reload for Zeph
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
// SPDX-FileCopyrightText: 2026 Andrei G <bug-ops>
// SPDX-License-Identifier: MIT OR Apache-2.0

//! In-memory BM25 inverted index over skill descriptions with Reciprocal Rank Fusion.
//!
//! BM25 (Okapi BM25) is a bag-of-words ranking function that outperforms TF-IDF for
//! short descriptions by normalizing term frequency against document length.
//!
//! This module provides two components:
//!
//! 1. **[`Bm25Index`]** — an inverted index built from skill descriptions that can be
//!    queried for lexical matches.
//! 2. **[`rrf_fuse`]** — Reciprocal Rank Fusion to combine BM25 and embedding results
//!    into a single ranked list.
//!
//! # Tokenization
//!
//! Text is lower-cased and split on all non-alphanumeric characters. Tokens shorter than
//! 3 characters are discarded to reduce noise from articles, prepositions, and abbreviations.
//!
//! # Examples
//!
//! ```rust
//! use zeph_skills::bm25::Bm25Index;
//!
//! let index = Bm25Index::build(&[
//!     "run git commands and manage repositories",
//!     "manage docker containers and compose files",
//! ]);
//!
//! let results = index.search("git commit", 5);
//! assert!(!results.is_empty());
//! assert_eq!(results[0].0, 0); // git doc ranks first
//! ```

use std::collections::{HashMap, HashSet};

use crate::matcher::ScoredMatch;

/// In-memory BM25 index over skill descriptions.
///
/// Built once at skill-load time with `k1 = 1.2` and `b = 0.75` (standard BM25 defaults).
/// The index is read-only after construction; rebuild via [`Bm25Index::build`] after a reload.
#[derive(Debug)]
pub struct Bm25Index {
    inverted: HashMap<String, Vec<(usize, f32)>>,
    doc_lengths: Vec<f32>,
    avg_doc_length: f32,
    doc_count: usize,
    k1: f32,
    b: f32,
}

impl Bm25Index {
    /// Build a BM25 index from a slice of document strings.
    ///
    /// Documents are indexed positionally — `descriptions[i]` corresponds to skill index `i`
    /// in the caller's skill slice, which must remain stable across the index lifetime.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_skills::bm25::Bm25Index;
    ///
    /// let index = Bm25Index::build(&["fetch weather data", "run docker containers"]);
    /// assert!(!index.search("weather", 3).is_empty());
    /// ```
    #[must_use]
    pub fn build(descriptions: &[&str]) -> Self {
        let k1 = 1.2_f32;
        let b = 0.75_f32;
        let doc_count = descriptions.len();
        let mut inverted: HashMap<String, Vec<(usize, f32)>> = HashMap::new();
        let mut doc_lengths = Vec::with_capacity(doc_count);

        for (i, desc) in descriptions.iter().enumerate() {
            let tokens = tokenize(desc);
            #[allow(clippy::cast_precision_loss)]
            let len = tokens.len() as f32;
            doc_lengths.push(len);

            let mut term_counts: HashMap<&str, u32> = HashMap::new();
            for token in &tokens {
                *term_counts.entry(token.as_str()).or_default() += 1;
            }
            for (term, count) in term_counts {
                #[allow(clippy::cast_precision_loss)]
                inverted
                    .entry(term.to_owned())
                    .or_default()
                    .push((i, count as f32));
            }
        }

        #[allow(clippy::cast_precision_loss)]
        let avg_doc_length = if doc_count == 0 {
            0.0
        } else {
            doc_lengths.iter().sum::<f32>() / doc_count as f32
        };

        Self {
            inverted,
            doc_lengths,
            avg_doc_length,
            doc_count,
            k1,
            b,
        }
    }

    /// Score all documents against the query, returning up to `limit` `(index, score)` pairs.
    ///
    /// Documents with a score of zero (no query terms match) are excluded. Results are
    /// sorted by score descending.
    ///
    /// Returns an empty `Vec` when the index is empty or the query contains no known terms.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use zeph_skills::bm25::Bm25Index;
    ///
    /// let index = Bm25Index::build(&["git version control", "docker container orchestration"]);
    /// let results = index.search("git", 5);
    /// assert_eq!(results.len(), 1);
    /// assert_eq!(results[0].0, 0);
    /// ```
    #[cfg_attr(
        feature = "profiling",
        tracing::instrument(name = "skill.bm25_search", skip_all, fields(query_len = %query.len(), limit = %limit))
    )]
    #[must_use]
    pub fn search(&self, query: &str, limit: usize) -> Vec<(usize, f32)> {
        if self.doc_count == 0 || self.avg_doc_length == 0.0 {
            return vec![];
        }
        let query_tokens = tokenize(query);
        let unique_terms: HashSet<&str> = query_tokens.iter().map(String::as_str).collect();
        let mut scores = vec![0.0_f32; self.doc_count];

        #[allow(clippy::cast_precision_loss)]
        for term in unique_terms {
            let Some(postings) = self.inverted.get(term) else {
                continue;
            };
            let df = postings.len() as f32;
            let idf = ((self.doc_count as f32 - df + 0.5) / (df + 0.5) + 1.0).ln();

            for &(doc_idx, tf) in postings {
                let dl = self.doc_lengths[doc_idx];
                let norm_tf = (tf * (self.k1 + 1.0))
                    / (tf + self.k1 * (1.0 - self.b + self.b * dl / self.avg_doc_length));
                scores[doc_idx] += idf * norm_tf;
            }
        }

        let mut results: Vec<(usize, f32)> = scores
            .into_iter()
            .enumerate()
            .filter(|(_, s)| *s > 0.0)
            .collect();
        results.sort_unstable_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        results.truncate(limit);
        results
    }
}

fn tokenize(text: &str) -> Vec<String> {
    text.to_lowercase()
        .split(|c: char| !c.is_alphanumeric())
        .filter(|s| s.len() >= 3)
        .map(String::from)
        .collect()
}

/// Reciprocal Rank Fusion: combine two ranked result lists into one.
///
/// Uses constant `k=60` from the original RRF paper (Cormack et al., 2009).
/// Results are sorted by fused score, highest first, and truncated to `limit`.
///
/// Scores in the returned `Vec` are normalized to `[0, 1]`: a rank-0 hit in both lists
/// scores `1.0`; a rank-0 hit in one list scores `0.5`. This makes fused scores
/// comparable to raw cosine similarity scores used by downstream consumers such as
/// `min_injection_score`.
#[must_use]
pub fn rrf_fuse(
    embedding_results: &[ScoredMatch],
    bm25_results: &[(usize, f32)],
    limit: usize,
) -> Vec<ScoredMatch> {
    const K: f32 = 60.0;
    let mut scores: HashMap<usize, f32> = HashMap::new();

    #[allow(clippy::cast_precision_loss)]
    for (rank, m) in embedding_results.iter().enumerate() {
        *scores.entry(m.index).or_default() += 1.0 / (K + rank as f32 + 1.0);
    }
    #[allow(clippy::cast_precision_loss)]
    for (rank, (idx, _)) in bm25_results.iter().enumerate() {
        *scores.entry(*idx).or_default() += 1.0 / (K + rank as f32 + 1.0);
    }

    // Normalize RRF scores to [0, 1] so they are comparable to cosine similarity scores.
    // Theoretical maximum for two lists: 2 / (K + 1). Dividing by this makes the best
    // possible score (rank 0 in both lists) equal to 1.0 and a single-list rank-0 hit
    // equal to 0.5, preserving rank order while matching the scale expected by downstream
    // consumers such as `min_injection_score`.
    let max_rrf_score = 2.0 / (K + 1.0);
    let mut fused: Vec<ScoredMatch> = scores
        .into_iter()
        .map(|(index, score)| ScoredMatch {
            index,
            score: score / max_rrf_score,
        })
        .collect();
    fused.sort_unstable_by(|a, b| {
        b.score
            .partial_cmp(&a.score)
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    fused.truncate(limit);
    fused
}

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

    #[test]
    fn build_empty() {
        let idx = Bm25Index::build(&[]);
        assert!(idx.search("anything", 5).is_empty());
    }

    #[test]
    fn search_no_match() {
        let idx = Bm25Index::build(&["run git commands", "manage docker containers"]);
        let results = idx.search("zzzyyyxxx", 5);
        assert!(results.is_empty());
    }

    #[test]
    fn search_exact_term_ranks_highest() {
        let idx = Bm25Index::build(&[
            "manage docker containers",
            "git commit push pull",
            "web search browsing",
        ]);
        let results = idx.search("git", 3);
        assert!(!results.is_empty());
        assert_eq!(results[0].0, 1, "git doc should rank first");
    }

    #[test]
    fn search_multi_word_query() {
        let idx = Bm25Index::build(&["run shell commands bash", "git version control commit"]);
        let results = idx.search("git commit", 5);
        assert!(!results.is_empty());
        assert_eq!(results[0].0, 1);
    }

    #[test]
    fn tokenize_filters_short_tokens() {
        let tokens = super::tokenize("a ab abc");
        assert!(!tokens.contains(&"a".to_string()));
        assert!(!tokens.contains(&"ab".to_string()));
        assert!(tokens.contains(&"abc".to_string()));
    }

    #[test]
    fn tokenize_splits_on_non_alphanumeric() {
        let tokens = super::tokenize("foo-bar baz_qux");
        assert!(tokens.contains(&"foo".to_string()));
        assert!(tokens.contains(&"bar".to_string()));
        assert!(tokens.contains(&"baz".to_string()));
        assert!(tokens.contains(&"qux".to_string()));
    }

    #[test]
    fn rrf_fuse_merges_lists() {
        let emb = vec![
            ScoredMatch {
                index: 0,
                score: 0.9,
            },
            ScoredMatch {
                index: 1,
                score: 0.5,
            },
        ];
        let bm25 = vec![(1, 2.0_f32), (2, 1.5_f32)];
        let fused = rrf_fuse(&emb, &bm25, 5);
        // index 1 appears in both lists — should score highest
        assert_eq!(fused[0].index, 1);
    }

    #[test]
    fn rrf_fuse_disjoint_lists() {
        let emb = vec![ScoredMatch {
            index: 0,
            score: 0.9,
        }];
        let bm25 = vec![(1, 1.0_f32)];
        let fused = rrf_fuse(&emb, &bm25, 5);
        assert_eq!(fused.len(), 2);
    }

    #[test]
    fn rrf_fuse_empty_embedding() {
        let bm25 = vec![(0, 1.0_f32)];
        let fused = rrf_fuse(&[], &bm25, 5);
        assert_eq!(fused.len(), 1);
        assert_eq!(fused[0].index, 0);
    }

    #[test]
    fn rrf_fuse_empty_bm25() {
        let emb = vec![ScoredMatch {
            index: 0,
            score: 0.9,
        }];
        let fused = rrf_fuse(&emb, &[], 5);
        assert_eq!(fused.len(), 1);
    }

    #[test]
    fn rrf_fuse_respects_limit() {
        let emb = vec![
            ScoredMatch {
                index: 0,
                score: 0.9,
            },
            ScoredMatch {
                index: 1,
                score: 0.8,
            },
            ScoredMatch {
                index: 2,
                score: 0.7,
            },
        ];
        let fused = rrf_fuse(&emb, &[], 2);
        assert_eq!(fused.len(), 2);
    }

    #[test]
    fn search_limit_zero_returns_empty() {
        let idx = Bm25Index::build(&["run git commands"]);
        let results = idx.search("git", 0);
        assert!(results.is_empty());
    }

    /// Rank-0 in one list must normalize to 0.5, which is well above
    /// the default `min_injection_score = 0.20` threshold. This is the
    /// regression test for issue #3435: before normalization, the score
    /// was ~0.016 and every skill candidate was silently dropped.
    #[test]
    fn rrf_fuse_single_list_rank0_passes_min_injection_threshold() {
        let emb = vec![ScoredMatch {
            index: 0,
            score: 0.85,
        }];
        let fused = rrf_fuse(&emb, &[], 5);
        assert_eq!(fused.len(), 1);
        // Normalized: 1/(K+1) / (2/(K+1)) = 0.5 exactly.
        assert!(
            (fused[0].score - 0.5).abs() < 1e-4,
            "expected normalized RRF score 0.5 for rank-0 single-list hit, got {:.6}",
            fused[0].score,
        );
    }

    /// Rank-0 in both lists must normalize to exactly 1.0.
    #[test]
    fn rrf_fuse_both_lists_rank0_normalizes_to_one() {
        let emb = vec![ScoredMatch {
            index: 0,
            score: 0.9,
        }];
        let bm25 = vec![(0, 2.0_f32)];
        let fused = rrf_fuse(&emb, &bm25, 5);
        assert_eq!(fused.len(), 1);
        assert!(
            (fused[0].score - 1.0).abs() < 1e-4,
            "expected ~1.0 for rank-0 in both lists, got {:.4}",
            fused[0].score,
        );
    }

    /// All normalized scores must stay in [0, 1].
    #[allow(clippy::cast_precision_loss)]
    #[test]
    fn rrf_fuse_scores_bounded_zero_to_one() {
        let emb: Vec<ScoredMatch> = (0..10)
            .map(|i| ScoredMatch {
                index: i,
                score: 1.0 - i as f32 * 0.05,
            })
            .collect();
        let bm25: Vec<(usize, f32)> = (5..15).map(|i| (i, 1.0)).collect();
        let fused = rrf_fuse(&emb, &bm25, 20);
        for m in &fused {
            assert!(
                m.score >= 0.0 && m.score <= 1.0,
                "score {:.4} is outside [0, 1]",
                m.score,
            );
        }
    }

    #[test]
    fn search_single_doc_hit() {
        let idx = Bm25Index::build(&["run git commands"]);
        let results = idx.search("git", 5);
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].0, 0);
        assert!(results[0].1 > 0.0);
    }

    #[test]
    fn rrf_fuse_both_empty_returns_empty() {
        let fused = rrf_fuse(&[], &[], 5);
        assert!(fused.is_empty());
    }

    #[test]
    fn tokenize_all_short_returns_empty() {
        let tokens = super::tokenize("a bb cc");
        assert!(
            tokens.is_empty(),
            "all tokens shorter than 3 chars should be filtered"
        );
    }

    #[test]
    fn rrf_fuse_rank_matters_first_beats_second() {
        // Both lists have same two items; item at rank 0 scores higher than rank 1
        let emb = vec![
            ScoredMatch {
                index: 0,
                score: 0.9,
            },
            ScoredMatch {
                index: 1,
                score: 0.8,
            },
        ];
        let bm25 = vec![(0, 2.0_f32), (1, 1.0_f32)];
        let fused = rrf_fuse(&emb, &bm25, 5);
        // index 0 is rank-1 in both lists → higher RRF score than index 1
        assert_eq!(fused[0].index, 0);
        assert!(fused[0].score > fused[1].score);
    }

    #[test]
    fn search_scores_positive_for_matching_term() {
        let idx = Bm25Index::build(&["database query optimization", "network protocol handling"]);
        let results = idx.search("database", 5);
        assert_eq!(results.len(), 1);
        assert!(results[0].1 > 0.0, "BM25 score should be positive");
    }
}