rustbrain-core 0.2.0

Core engine for rustbrain: SQLite knowledge graph, ranked FTS, CSR mmap cache, and graph-aware AI context
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
//! Ranked hybrid query: FTS5 BM25 + tag / alias / title boosts.
//!
//! This is not neural retrieval. Scores are useful for ordering within a single
//! brain (and for cross-workspace merge) but are not calibrated probabilities.

use crate::error::Result;
use crate::fts::escape_fts5_query;
use crate::storage::Database;
use crate::types::{Node, NodeType};
use rusqlite::params;
use serde::{Deserialize, Serialize};

/// A single ranked search hit.
///
/// `reasons` is a debug-oriented list of score components (e.g. `fts:…`, `tag:raft`)
/// intended for CLI `--scores` and UI tooltips — not a stable public schema.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RankedHit {
    /// Matching node.
    pub node: Node,
    /// Higher is better. Combines inverted BM25 with additive boosts.
    pub score: f32,
    /// Why this hit was selected (for debugging / UI).
    pub reasons: Vec<String>,
}

/// Options for [`Database::search_ranked`] / [`crate::Brain::query_ranked`].
#[derive(Debug, Clone)]
pub struct QueryOptions {
    /// Maximum hits to return after ranking and deduplication.
    pub limit: usize,
    /// Multiplicative boosts applied when `node.node_type` matches.
    ///
    /// Defaults slightly prefer goals/ADRs/edge cases over raw symbols.
    pub type_boosts: Vec<(NodeType, f32)>,
    /// If non-empty, only include these node types.
    pub include_types: Vec<NodeType>,
    /// Exclude these node types after ranking (applied after include filter).
    pub exclude_types: Vec<NodeType>,
    /// Convenience: exclude [`NodeType::Symbol`] (typical for human CLI search).
    pub no_symbols: bool,
}

impl Default for QueryOptions {
    fn default() -> Self {
        Self {
            limit: 50,
            type_boosts: vec![
                (NodeType::Goal, 1.15),
                (NodeType::Adr, 1.1),
                (NodeType::EdgeCase, 1.1),
                (NodeType::Concept, 1.05),
                (NodeType::Symbol, 0.95),
            ],
            include_types: Vec::new(),
            exclude_types: Vec::new(),
            no_symbols: false,
        }
    }
}

impl QueryOptions {
    /// Human-oriented defaults: exclude pure code symbols unless asked otherwise.
    pub fn human() -> Self {
        Self {
            no_symbols: true,
            ..Self::default()
        }
    }

    fn allows(&self, ty: &NodeType) -> bool {
        if self.no_symbols && *ty == NodeType::Symbol {
            return false;
        }
        if !self.include_types.is_empty() && !self.include_types.contains(ty) {
            return false;
        }
        if self.exclude_types.contains(ty) {
            return false;
        }
        true
    }
}

impl Database {
    /// Ranked search combining FTS5 BM25 with tag, alias, title, and id boosts.
    ///
    /// Steps:
    /// 1. Escape the user query for FTS5 `MATCH`
    /// 2. Collect BM25-ordered candidates (oversample by `2 × limit`)
    /// 3. Apply additive boosts for title/id/tag/alias token hits
    /// 4. Apply multiplicative type priors from [`QueryOptions::type_boosts`]
    /// 5. Augment with pure tag/alias exact matches FTS may have missed
    /// 6. Sort by score, dedupe by id, truncate to `limit`
    ///
    /// # Errors
    ///
    /// Returns [`crate::BrainError::FtsQuery`] for empty input after tokenization.
    pub fn search_ranked(&self, query: &str, opts: &QueryOptions) -> Result<Vec<RankedHit>> {
        let tokens: Vec<String> = query
            .split_whitespace()
            .filter(|t| !t.is_empty())
            .map(|t| t.to_lowercase())
            .collect();
        if tokens.is_empty() {
            return Err(crate::error::BrainError::FtsQuery(
                "query must contain at least one non-whitespace token".into(),
            ));
        }

        let escaped = escape_fts5_query(query)?;
        let mut stmt = self.conn.prepare(
            "SELECT n.id, n.node_type, n.title, n.file_path, n.symbol_hash, n.summary,
                    n.content_hash, n.created_at, n.updated_at,
                    bm25(node_fts) AS bm25_score
             FROM node_fts f
             JOIN nodes n ON n.id = f.node_id
             WHERE node_fts MATCH ?1
             ORDER BY bm25_score
             LIMIT ?2",
        )?;

        let rows = stmt.query_map(params![escaped, opts.limit as i64 * 2], |row| {
            let type_str: String = row.get(1)?;
            let node_type = NodeType::parse(&type_str).unwrap_or(NodeType::Concept);
            let symbol_hash_raw: Option<i64> = row.get(4)?;
            let bm25: f64 = row.get(9)?;
            Ok((
                Node {
                    id: row.get(0)?,
                    node_type,
                    title: row.get(2)?,
                    file_path: row.get(3)?,
                    symbol_hash: symbol_hash_raw.map(|h| h as u64),
                    summary: row.get(5)?,
                    content_hash: row.get(6)?,
                    created_at: row.get(7)?,
                    updated_at: row.get(8)?,
                },
                bm25 as f32,
            ))
        })?;

        let mut hits: Vec<RankedHit> = Vec::new();
        for r in rows {
            let (node, bm25) = r?;
            if !opts.allows(&node.node_type) {
                continue;
            }
            // bm25() in SQLite FTS5: more relevant → more negative. Invert.
            let fts_score = (-bm25).max(0.01);
            let mut score = fts_score;
            let mut reasons = vec![format!("fts:{fts_score:.3}")];
            // Hub boost for root README node.
            if node.id == "readme" || node.file_path.as_deref() == Some("README.md") {
                score *= 1.2;
                reasons.push("hub:readme".into());
            }

            // Title token hits
            let title_l = node.title.to_lowercase();
            let id_l = node.id.to_lowercase();
            for t in &tokens {
                if title_l.split_whitespace().any(|w| w == t) || title_l.contains(t) {
                    score += 2.0;
                    reasons.push(format!("title:{t}"));
                }
                if id_l.rsplit('/').next() == Some(t.as_str()) || id_l.ends_with(t) {
                    score += 1.5;
                    reasons.push(format!("id:{t}"));
                }
            }

            // Tag boosts
            let tags = self.get_tags_for(&node.id)?;
            for tag in &tags {
                let tag_l = tag.to_lowercase();
                for t in &tokens {
                    if tag_l == *t || tag_l.contains(t) {
                        score += 3.0;
                        reasons.push(format!("tag:{tag}"));
                    }
                }
            }

            // Alias boosts
            let aliases = self.get_aliases_for(&node.id)?;
            for alias in &aliases {
                let a = alias.to_lowercase();
                for t in &tokens {
                    if a == *t || a.contains(t) {
                        score += 2.5;
                        reasons.push(format!("alias:{alias}"));
                    }
                }
            }

            // Type boost
            for (ty, mult) in &opts.type_boosts {
                if node.node_type == *ty {
                    score *= mult;
                    reasons.push(format!("type:{}×{mult}", ty.as_str()));
                }
            }

            hits.push(RankedHit {
                node,
                score,
                reasons,
            });
        }

        // Also pull pure tag/alias matches that FTS may have missed (short queries).
        self.augment_with_tag_alias_hits(&tokens, &mut hits, opts)?;

        hits.retain(|h| opts.allows(&h.node.node_type));

        hits.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        // Dedup by id keeping best score
        let mut seen = std::collections::HashSet::new();
        hits.retain(|h| seen.insert(h.node.id.clone()));
        hits.truncate(opts.limit);
        Ok(hits)
    }

    fn get_tags_for(&self, node_id: &str) -> Result<Vec<String>> {
        let mut stmt = self
            .conn
            .prepare("SELECT tag FROM node_tags WHERE node_id = ?1")?;
        let rows = stmt.query_map([node_id], |row| row.get(0))?;
        let mut v = Vec::new();
        for r in rows {
            v.push(r?);
        }
        Ok(v)
    }

    fn get_aliases_for(&self, node_id: &str) -> Result<Vec<String>> {
        let mut stmt = self
            .conn
            .prepare("SELECT alias FROM node_aliases WHERE node_id = ?1")?;
        let rows = stmt.query_map([node_id], |row| row.get(0))?;
        let mut v = Vec::new();
        for r in rows {
            v.push(r?);
        }
        Ok(v)
    }

    fn augment_with_tag_alias_hits(
        &self,
        tokens: &[String],
        hits: &mut Vec<RankedHit>,
        opts: &QueryOptions,
    ) -> Result<()> {
        let existing: std::collections::HashSet<String> =
            hits.iter().map(|h| h.node.id.clone()).collect();

        for t in tokens {
            // Tag exact
            let mut stmt = self.conn.prepare(
                "SELECT n.id, n.node_type, n.title, n.file_path, n.symbol_hash, n.summary,
                        n.content_hash, n.created_at, n.updated_at
                 FROM node_tags t
                 JOIN nodes n ON n.id = t.node_id
                 WHERE lower(t.tag) = ?1
                 LIMIT 20",
            )?;
            let rows = stmt.query_map([t.as_str()], |row| {
                let type_str: String = row.get(1)?;
                let node_type = NodeType::parse(&type_str).unwrap_or(NodeType::Concept);
                let symbol_hash_raw: Option<i64> = row.get(4)?;
                Ok(Node {
                    id: row.get(0)?,
                    node_type,
                    title: row.get(2)?,
                    file_path: row.get(3)?,
                    symbol_hash: symbol_hash_raw.map(|h| h as u64),
                    summary: row.get(5)?,
                    content_hash: row.get(6)?,
                    created_at: row.get(7)?,
                    updated_at: row.get(8)?,
                })
            })?;
            for r in rows {
                let node = r?;
                if existing.contains(&node.id) || !opts.allows(&node.node_type) {
                    continue;
                }
                hits.push(RankedHit {
                    node,
                    score: 3.5,
                    reasons: vec![format!("tag-exact:{t}")],
                });
            }

            // Alias exact
            let mut stmt = self.conn.prepare(
                "SELECT n.id, n.node_type, n.title, n.file_path, n.symbol_hash, n.summary,
                        n.content_hash, n.created_at, n.updated_at
                 FROM node_aliases a
                 JOIN nodes n ON n.id = a.node_id
                 WHERE a.alias = ?1
                 LIMIT 20",
            )?;
            let rows = stmt.query_map([t.as_str()], |row| {
                let type_str: String = row.get(1)?;
                let node_type = NodeType::parse(&type_str).unwrap_or(NodeType::Concept);
                let symbol_hash_raw: Option<i64> = row.get(4)?;
                Ok(Node {
                    id: row.get(0)?,
                    node_type,
                    title: row.get(2)?,
                    file_path: row.get(3)?,
                    symbol_hash: symbol_hash_raw.map(|h| h as u64),
                    summary: row.get(5)?,
                    content_hash: row.get(6)?,
                    created_at: row.get(7)?,
                    updated_at: row.get(8)?,
                })
            })?;
            for r in rows {
                let node = r?;
                if hits.iter().any(|h| h.node.id == node.id) || !opts.allows(&node.node_type)
                {
                    continue;
                }
                hits.push(RankedHit {
                    node,
                    score: 3.0,
                    reasons: vec![format!("alias-exact:{t}")],
                });
            }
        }
        Ok(())
    }

    /// List unresolved WikiLink / symbol targets.
    pub fn list_pending_links(&self) -> Result<Vec<PendingLink>> {
        let mut stmt = self.conn.prepare(
            "SELECT source_id, raw_target, relation_type, created_at FROM pending_links ORDER BY source_id, raw_target",
        )?;
        let rows = stmt.query_map([], |row| {
            Ok(PendingLink {
                source_id: row.get(0)?,
                raw_target: row.get(1)?,
                relation_type: row.get(2)?,
                created_at: row.get(3)?,
            })
        })?;
        let mut out = Vec::new();
        for r in rows {
            out.push(r?);
        }
        Ok(out)
    }

    /// Count nodes grouped by `node_type`.
    pub fn count_nodes_by_type(&self) -> Result<Vec<(String, usize)>> {
        let mut stmt = self
            .conn
            .prepare("SELECT node_type, COUNT(*) FROM nodes GROUP BY node_type ORDER BY COUNT(*) DESC")?;
        let rows = stmt.query_map([], |row| {
            Ok((row.get::<_, String>(0)?, row.get::<_, usize>(1)?))
        })?;
        let mut out = Vec::new();
        for r in rows {
            out.push(r?);
        }
        Ok(out)
    }
}

/// Unresolved link kept for later resolution.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PendingLink {
    /// Source node id that referenced the target.
    pub source_id: String,
    /// Raw WikiLink / `symbol:…` target text.
    pub raw_target: String,
    /// Intended relation type (`relates_to`, `anchors`, …).
    pub relation_type: String,
    /// When the pending link was recorded (unix seconds).
    pub created_at: i64,
}

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

    #[test]
    fn tag_boost_ranks_higher() {
        let db = Database::open_in_memory().unwrap();
        let n1 = Node {
            id: "docs/a".into(),
            node_type: NodeType::Concept,
            title: "Something Else".into(),
            file_path: None,
            symbol_hash: None,
            summary: Some("mentions raft in body".into()),
            content_hash: None,
            created_at: 1,
            updated_at: 1,
        };
        let n2 = Node {
            id: "docs/b".into(),
            node_type: NodeType::Concept,
            title: "Protocol".into(),
            file_path: None,
            symbol_hash: None,
            summary: Some("tagged note".into()),
            content_hash: None,
            created_at: 1,
            updated_at: 1,
        };
        db.insert_node(&n1).unwrap();
        db.insert_node(&n2).unwrap();
        db.index_fts("docs/a", "Something Else", "the raft algorithm", "").unwrap();
        db.index_fts("docs/b", "Protocol", "tagged note about consensus", "raft").unwrap();
        db.replace_node_tags("docs/b", &["raft".into()]).unwrap();

        let hits = db
            .search_ranked("raft", &QueryOptions::default())
            .unwrap();
        assert!(hits.len() >= 2);
        // Tag match should beat pure body mention when scores are close, or at least appear
        assert!(hits.iter().any(|h| h.node.id == "docs/b"));
        let b = hits.iter().find(|h| h.node.id == "docs/b").unwrap();
        assert!(b.reasons.iter().any(|r| r.starts_with("tag")));
    }
}