rsclaw 2026.5.20

AI Agent Engine Compatible with OpenClaw
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
//! kb_search pipeline: dense + sparse → filter → fuse → boost → mmr → fetch.

use std::{collections::HashMap, sync::Arc};

use anyhow::Result;
use serde::Serialize;

use crate::kb::{
    content_store::read::read_doc_range,
    embedder::KbEmbedder,
    index::KbIndex,
    model::{CallerScope, KbChunk, KbDoc, KbLocator, KbSource},
    paths::KbPaths,
    search::{
        filter::{SearchFilter, is_latest_version, keep_doc},
        mmr::{MmrCandidate, mmr_select},
        rrf::rrf_fuse,
    },
    store::{KbStore, chunks, docs, entities},
};

#[derive(Debug, Clone)]
pub struct SearchRequest {
    pub query: String,
    pub k: usize,
    pub filter: SearchFilter,
    pub mode: SearchMode,
    pub diversity: Diversity,
    pub mmr_lambda: f32,
    pub boost_entities: Vec<String>,
    /// Asymmetric-embedding query instruction (Qwen3), applied to the dense
    /// query only. `None` = symmetric (the query is embedded as-is).
    pub query_instruction: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchMode {
    Auto,
    Dense,
    Bm25,
    Hybrid,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Diversity {
    Off,
    Mmr,
}

#[derive(Debug, Clone, Serialize)]
pub struct RetrievalHit {
    pub chunk_id: String,
    pub doc_id: String,
    pub doc_title: String,
    pub text: String,
    pub heading_path: Vec<String>,
    pub score: f32,
    pub citation: Citation,
    pub entities: Vec<String>,
}

#[derive(Debug, Clone, Serialize)]
pub struct Citation {
    pub source: String,
    pub locator_human: String,
    pub locator_machine: KbLocator,
}

pub struct SearchCtx {
    pub store: Arc<KbStore>,
    pub index: Arc<KbIndex>,
    pub paths: Arc<KbPaths>,
    pub embedder: Arc<dyn KbEmbedder>,
}

impl SearchCtx {
    pub fn search(&self, req: &SearchRequest, scope: &CallerScope) -> Result<Vec<RetrievalHit>> {
        let recall_k = (req.k * 3).max(10);

        // 1. Dense recall.
        let dense = match req.mode {
            SearchMode::Bm25 => Vec::new(),
            _ => {
                // Dense side applies the asymmetric query instruction if set;
                // BM25 (below) always uses the raw query.
                let dense_query =
                    crate::embed::format_query(req.query_instruction.as_deref(), &req.query);
                let qv = self.embedder.embed_batch(&[dense_query])?;
                match qv.first() {
                    Some(qvec) => self.index.hnsw.search(qvec, recall_k),
                    // Embedder returned no vector — skip dense recall rather
                    // than panic; sparse recall still runs in Auto/Hybrid.
                    None => Vec::new(),
                }
            }
        };

        // 2. Sparse recall.
        let sparse = match req.mode {
            SearchMode::Dense => Vec::new(),
            _ => self.index.tantivy.search(&req.query, recall_k)?,
        };

        // 3. Filter (visibility + status + version + tags + source_kind + doc_ids).
        let rtx = self.store.begin_read()?;
        let mut materialised: HashMap<String, (KbChunk, KbDoc)> = HashMap::new();

        let keep =
            |cid: &str, materialised: &mut HashMap<String, (KbChunk, KbDoc)>| -> Result<bool> {
                if materialised.contains_key(cid) {
                    return Ok(true);
                }
                let c = match chunks::get(&rtx, cid)? {
                    Some(c) => c,
                    None => return Ok(false),
                };
                let d = match docs::get(&rtx, &c.doc_id)? {
                    Some(d) => d,
                    None => return Ok(false),
                };
                if !keep_doc(&d, scope, &req.filter) {
                    return Ok(false);
                }
                if !is_latest_version(&rtx, &d)? {
                    return Ok(false);
                }
                materialised.insert(cid.to_string(), (c, d));
                Ok(true)
            };

        let mut kept_dense: Vec<(String, f32)> = Vec::new();
        for (cid, score) in &dense {
            if keep(cid, &mut materialised)? {
                kept_dense.push((cid.clone(), *score));
            }
        }
        let mut kept_sparse: Vec<(String, f32)> = Vec::new();
        for (cid, score) in &sparse {
            if keep(cid, &mut materialised)? {
                kept_sparse.push((cid.clone(), *score));
            }
        }

        // 4. Fuse.
        let mut fused = match req.mode {
            SearchMode::Dense => kept_dense,
            SearchMode::Bm25 => kept_sparse,
            _ => rrf_fuse(&[&kept_dense, &kept_sparse]),
        };

        // 5a. require_entities: drop fused hits whose chunk_id is
        //     not in EVERY required entity's chunk set.
        //     (Entity edges are populated by the chunk_embed handler's
        //     regex extractor — `KbEntityIndex` rows keyed
        //     `entity_id\0chunk_id`.)
        if !req.filter.require_entities.is_empty() {
            let mut required_sets: Vec<std::collections::HashSet<String>> = Vec::new();
            for eid in &req.filter.require_entities {
                let set: std::collections::HashSet<String> =
                    entities::chunks_for_entity(&rtx, eid)?
                        .into_iter()
                        .map(|e| e.chunk_id)
                        .collect();
                required_sets.push(set);
            }
            fused.retain(|(cid, _)| required_sets.iter().all(|s| s.contains(cid)));
        }

        // 5b. boost_entities: multiply each fused hit's score by
        //     `BOOST_FACTOR` for every boost entity it mentions.
        //     Bounded so adding more boost entities has diminishing
        //     impact (1.0 + Σ min(boost_factor, ...)). Re-sort.
        const BOOST_FACTOR: f32 = 0.2;
        if !req.boost_entities.is_empty() {
            let mut boost_sets: Vec<std::collections::HashSet<String>> = Vec::new();
            for eid in &req.boost_entities {
                let set: std::collections::HashSet<String> =
                    entities::chunks_for_entity(&rtx, eid)?
                        .into_iter()
                        .map(|e| e.chunk_id)
                        .collect();
                boost_sets.push(set);
            }
            for (cid, score) in fused.iter_mut() {
                let bonus: f32 = boost_sets
                    .iter()
                    .map(|s| if s.contains(cid) { BOOST_FACTOR } else { 0.0 })
                    .sum();
                *score *= 1.0 + bonus;
            }
            fused.sort_by(|a, b| {
                b.1.partial_cmp(&a.1)
                    .unwrap_or(std::cmp::Ordering::Equal)
                    .then(a.0.cmp(&b.0))
            });
        }

        // 6. MMR.
        let mut final_ids: Vec<(String, f32)> = match req.diversity {
            Diversity::Off => fused.into_iter().take(req.k).collect(),
            Diversity::Mmr => {
                let candidates: Vec<MmrCandidate> = fused
                    .iter()
                    .filter_map(|(id, sc)| {
                        materialised.get(id).map(|(c, _)| MmrCandidate {
                            chunk_id: id.clone(),
                            relevance: *sc,
                            vector: c.vector.as_slice(),
                        })
                    })
                    .collect();
                mmr_select(candidates, req.k, req.mmr_lambda)
            }
        };
        // Spec §3 KV-cache friendliness: stable order across calls.
        // RRF already breaks ties deterministically; MMR's greedy
        // picker can produce equal-score ties — apply (score desc,
        // chunk_id asc) one more time so the same inputs always
        // produce the same on-the-wire byte sequence.
        final_ids.sort_by(|a, b| {
            b.1.partial_cmp(&a.1)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then(a.0.cmp(&b.0))
        });

        // 7. Lazy text fetch + build hits.
        let mut hits = Vec::with_capacity(final_ids.len());
        for (chunk_id, score) in final_ids {
            let (c, d) = match materialised.get(&chunk_id) {
                Some(p) => p,
                None => continue,
            };
            let abs = self.paths.root.join(&d.markdown_path);
            // Body-read failures degrade to empty text rather than
            // erroring the whole search, but log them — usually means
            // the file was reaped by the compactor in a race, which
            // should be rare and worth knowing about.
            let text = match read_doc_range(&abs, c.byte_offset.0, c.byte_offset.1) {
                Ok(t) => t,
                Err(e) => {
                    tracing::warn!(
                        chunk = %crate::kb::redact(&chunk_id),
                        path = %abs.display(),
                        "kb search: chunk body read failed: {e}"
                    );
                    String::new()
                }
            };
            hits.push(RetrievalHit {
                chunk_id,
                doc_id: d.id.clone(),
                doc_title: d.title.clone(),
                text,
                heading_path: c.heading_path.clone(),
                score,
                citation: Citation {
                    source: render_source(d),
                    locator_human: c.locator.human(),
                    locator_machine: c.locator.clone(),
                },
                entities: Vec::new(),
            });
        }
        Ok(hits)
    }
}

fn render_source(d: &KbDoc) -> String {
    match &d.source {
        KbSource::Doc { path } => format!("file://{}", path.display()),
        KbSource::Url { url, .. } => url.clone(),
        KbSource::Img { path } => format!("file://{}", path.display()),
        _ => d.title.clone(),
    }
}

#[cfg(test)]
mod tests {
    use tempfile::TempDir;

    use super::*;
    use crate::kb::{
        canonicalize::{CanonicalizeInput, canonicalize_by_mime},
        embedder::{KbEmbedder, StubEmbedder},
        model::KbVisibility,
        pipeline::{IngestInput, ingest_canonicalized},
        worker::{DefaultDispatcher, WorkerConfig, WorkerPool, handlers::HandlerCtx},
    };

    fn ctx_with_ingested(body: &str) -> (TempDir, SearchCtx) {
        let tmp = TempDir::new().unwrap();
        let store = Arc::new(KbStore::open(&tmp.path().join("kb.redb")).unwrap());
        let paths = Arc::new(KbPaths::new(tmp.path().join("kb")));
        paths.ensure_layout().unwrap();
        let embedder: Arc<dyn KbEmbedder> = Arc::new(StubEmbedder::default());
        let index = Arc::new(KbIndex::open(&paths).unwrap());

        let canon = canonicalize_by_mime(CanonicalizeInput {
            bytes: body.as_bytes(),
            mime: "text/markdown",
            hint_title: Some("t"),
            logical_source_id_seed: None,
        })
        .unwrap()
        .unwrap();
        ingest_canonicalized(
            &store,
            IngestInput {
                canon: &canon,
                raw_bytes: body.as_bytes(),
                raw_ext: "md",
                visibility: None,
                owner_user_id: None,
                seen_key: None,
                source: None,
                paths: &paths,
            },
        )
        .unwrap();
        let hctx = HandlerCtx {
            store: store.clone(),
            paths: paths.clone(),
            embedder: embedder.clone(),
            index: index.clone(),
        };
        let cfg = WorkerConfig {
            worker_id: "w".into(),
            ..WorkerConfig::default()
        };
        WorkerPool::run_one_blocking(&hctx, &cfg, &DefaultDispatcher).unwrap();
        (
            tmp,
            SearchCtx {
                store,
                index,
                paths,
                embedder,
            },
        )
    }

    #[test]
    fn search_returns_hits_for_indexed_body() {
        let (_tmp, ctx) = ctx_with_ingested("# Greeting\n\nThe quick brown fox jumps over.");
        let req = SearchRequest {
            query: "brown fox".into(),
            k: 5,
            filter: SearchFilter::default(),
            mode: SearchMode::Hybrid,
            diversity: Diversity::Mmr,
            mmr_lambda: 0.5,
            boost_entities: vec![],
            query_instruction: None,
        };
        let hits = ctx.search(&req, &CallerScope::default()).unwrap();
        assert!(!hits.is_empty(), "expected at least one hit");
    }

    #[test]
    fn search_output_is_deterministic_across_calls() {
        // Spec §3 KV-cache friendliness: same inputs MUST produce
        // the same chunk_id sequence on the wire. MMR's greedy
        // picker can tie on score; the post-MMR `(score desc,
        // chunk_id asc)` sort makes the tiebreak stable.
        let (_tmp, ctx) = ctx_with_ingested(
            "# A\n\npara one.\n\npara two.\n\npara three.\n\npara four.\n\npara five.",
        );
        let req = SearchRequest {
            query: "para".into(),
            k: 3,
            filter: SearchFilter::default(),
            mode: SearchMode::Hybrid,
            diversity: Diversity::Mmr,
            mmr_lambda: 0.5,
            boost_entities: vec![],
            query_instruction: None,
        };
        let first: Vec<String> = ctx
            .search(&req, &CallerScope::default())
            .unwrap()
            .into_iter()
            .map(|h| h.chunk_id)
            .collect();
        for _ in 0..3 {
            let again: Vec<String> = ctx
                .search(&req, &CallerScope::default())
                .unwrap()
                .into_iter()
                .map(|h| h.chunk_id)
                .collect();
            assert_eq!(first, again, "search order not stable across calls");
        }
    }

    #[test]
    fn search_filter_by_visibility_hides_private() {
        let (_tmp, ctx) = ctx_with_ingested("# Secret\n\nclassified info goes here.");
        // Re-tag the existing doc as Private + owner u1.
        let rtx = ctx.store.begin_read().unwrap();
        let all: Vec<KbDoc> = {
            use redb::ReadableTable;

            use crate::kb::store::{codec::decode, schema::KB_DOCS};
            let tbl = rtx.open_table(KB_DOCS).unwrap();
            let mut out = Vec::new();
            for e in tbl.iter().unwrap() {
                let (_, v) = e.unwrap();
                out.push(decode(v.value()).unwrap());
            }
            out
        };
        drop(rtx);
        let mut d = all.into_iter().next().unwrap();
        d.visibility = KbVisibility::Private;
        d.owner_user_id = Some("u1".into());
        {
            let wtx = ctx.store.begin_write().unwrap();
            crate::kb::store::docs::put(&wtx, &d).unwrap();
            wtx.commit().unwrap();
        }
        let req = SearchRequest {
            query: "classified".into(),
            k: 5,
            filter: SearchFilter::default(),
            mode: SearchMode::Hybrid,
            diversity: Diversity::Off,
            mmr_lambda: 0.5,
            boost_entities: vec![],
            query_instruction: None,
        };
        let scope_other = CallerScope {
            user_id: Some("u2".into()),
            ..Default::default()
        };
        let hits = ctx.search(&req, &scope_other).unwrap();
        assert!(hits.is_empty(), "Private doc must not leak to other user");
        let scope_owner = CallerScope {
            user_id: Some("u1".into()),
            ..Default::default()
        };
        let hits = ctx.search(&req, &scope_owner).unwrap();
        assert!(!hits.is_empty(), "owner must see their own Private doc");
    }
}