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
use crate::error::{AgentDbError, Result};
use crate::fts::FullTextStore;
use crate::memory::MemoryGraph;
use crate::memory::TraversalOptions;
use crate::vectors::collection::{Collection, SearchOptions, SearchResult};
use crate::vectors::hnsw::DistanceMetric;
use rusqlite::Connection;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::{Arc, Mutex};
/// A single result from a hybrid graph + vector query.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct HybridResult {
/// ID of the matched entity.
pub id: String,
/// Raw cosine distance from the vector ANN search (lower = more similar).
pub vector_score: f32,
/// Maximum edge weight along any graph path from the anchor to this node.
/// `0.0` if the node is not reachable from the anchor.
pub graph_weight: f64,
/// Final blended rank score: `alpha × vec_similarity + (1 − alpha) × graph_weight`.
/// Higher is better.
pub rank_score: f64,
/// Metadata stored alongside the vector, if any.
pub metadata: Option<Value>,
}
/// Parameters for a hybrid graph + vector query.
pub struct HybridQuery<'a> {
/// The memory-graph node to start traversal from.
pub anchor_node: &'a str,
/// Query embedding to rank against the vector collection.
pub embedding: &'a [f32],
/// Name of the vector collection to search.
pub collection: &'a str,
/// Maximum graph traversal depth from `anchor_node`.
pub graph_depth: usize,
/// Maximum number of results to return after blending.
pub top_k: usize,
/// Interpolation factor between vector similarity and graph proximity.
/// `0.0` = pure graph weight, `1.0` = pure vector similarity.
pub alpha: f64,
/// Optional metadata filter applied before vector scoring.
pub filter: Option<Value>,
}
/// Parameters for a tri-modal graph + vector + FTS query.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TriModalQuery {
/// The memory-graph node to start traversal from.
pub anchor_node: String,
/// Query embedding to rank against the vector collection.
pub embedding: Vec<f32>,
/// Text query for full-text search.
pub text_query: String,
/// Name of the vector collection to search.
pub collection: String,
/// Maximum graph traversal depth from `anchor_node`.
pub graph_depth: usize,
/// Maximum number of results to return after blending.
pub top_k: usize,
/// Weight for vector similarity (must satisfy alpha + beta + gamma ≈ 1.0).
pub alpha: f32,
/// Weight for graph proximity.
pub beta: f32,
/// Weight for FTS BM25 score.
pub gamma: f32,
/// Optional metadata filter applied before vector scoring.
pub filter: Option<Value>,
}
/// A single result from a tri-modal graph + vector + FTS query.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct TriModalResult {
/// ID of the matched entity.
pub id: String,
/// Final blended rank score (higher is better).
pub rank_score: f32,
/// Normalized vector similarity score in [0, 1], if the item appeared in ANN results.
pub vector_score: Option<f32>,
/// Normalized graph proximity weight in [0, 1], if the item is reachable from the anchor.
pub graph_weight: Option<f32>,
/// Normalized FTS BM25 score in [0, 1], if the item appeared in FTS results.
pub fts_rank: Option<f32>,
/// Metadata stored alongside the vector, if any.
pub metadata: Option<Value>,
}
/// Executes hybrid graph + vector queries.
pub struct HybridStore {
conn: Arc<Mutex<Connection>>,
}
impl HybridStore {
pub(crate) fn new(conn: Arc<Mutex<Connection>>) -> Self {
Self { conn }
}
/// Run a hybrid graph + vector query.
///
/// The algorithm proceeds in three stages:
///
/// 1. **Graph traversal** — walks the memory graph from `q.anchor_node` up to
/// `q.graph_depth` hops, recording the maximum edge weight seen for each
/// reachable node.
/// 2. **Vector search** — retrieves the top `q.top_k × 20` approximate nearest
/// neighbours from the named collection.
/// 3. **Score blending** — for each candidate, computes
/// `rank = q.alpha × vec_similarity + (1 − q.alpha) × graph_weight`,
/// then returns the top `q.top_k` results sorted by rank descending.
pub fn query(&self, q: HybridQuery, col: &Collection) -> Result<Vec<HybridResult>> {
// Step 1: graph traversal
let graph = MemoryGraph::new(Arc::clone(&self.conn));
let traversal = graph
.neighbors(
q.anchor_node,
TraversalOptions {
relation: None,
max_depth: q.graph_depth,
min_weight: Some(0.0),
},
)
.unwrap_or_default();
let mut graph_weights: HashMap<String, f64> = HashMap::new();
for t in &traversal {
let e = graph_weights.entry(t.node.id.clone()).or_insert(0.0);
if t.weight > *e {
*e = t.weight;
}
}
// Step 2: vector search
let fetch_k = (q.top_k * 20).max(100);
let vec_results: Vec<SearchResult> = col.search(
q.embedding,
SearchOptions {
top_k: fetch_k,
metric: DistanceMetric::Cosine,
filter: q.filter.clone(),
},
)?;
if vec_results.is_empty() {
return Ok(vec![]);
}
// Step 3: normalize vector scores (distance -> similarity)
let max_s = vec_results
.iter()
.map(|r| r.score)
.fold(f32::NEG_INFINITY, f32::max);
let min_s = vec_results
.iter()
.map(|r| r.score)
.fold(f32::INFINITY, f32::min);
let range = (max_s - min_s).max(1e-6);
// Step 4: blend and rank
let mut blended: Vec<HybridResult> = vec_results
.into_iter()
.map(|r| {
let vec_sim = 1.0 - ((r.score - min_s) / range) as f64;
let gw = graph_weights.get(&r.id).copied().unwrap_or(0.0);
let rank = q.alpha * vec_sim + (1.0 - q.alpha) * gw;
HybridResult {
id: r.id,
vector_score: r.score,
graph_weight: gw,
rank_score: rank,
metadata: r.metadata,
}
})
.collect();
blended.sort_by(|a, b| {
b.rank_score
.partial_cmp(&a.rank_score)
.unwrap_or(std::cmp::Ordering::Equal)
});
blended.truncate(q.top_k);
Ok(blended)
}
/// Run a tri-modal graph + vector + FTS query.
///
/// The algorithm runs three searches and blends results:
///
/// 1. **Vector ANN** — retrieves `top_k × 20` approximate nearest neighbours.
/// 2. **Graph traversal** — walks the memory graph from `q.anchor_node` up to
/// `q.graph_depth` hops, recording the maximum edge weight per reachable node.
/// 3. **FTS keyword search** — BM25 full-text search over the collection's FTS index.
///
/// Each component is min-max normalised to [0, 1] within its own result set, then
/// blended as `final_score = alpha × vec_score + beta × graph_weight + gamma × fts_score`.
///
/// The weights must satisfy `alpha + beta + gamma ≈ 1.0` (tolerance ±0.01).
pub fn tri_modal_query(
&self,
q: &TriModalQuery,
col: &Collection,
) -> Result<Vec<TriModalResult>> {
// Validate weights
let weight_sum = q.alpha + q.beta + q.gamma;
if (weight_sum - 1.0_f32).abs() > 0.01 {
return Err(AgentDbError::InvalidArgument(format!(
"tri_modal_query: alpha + beta + gamma must equal 1.0, got {weight_sum:.4}"
)));
}
// ── Step 1: Graph traversal ────────────────────────────────────────
let mut graph_weights: HashMap<String, f64> = HashMap::new();
if q.beta > 0.0 {
let graph = MemoryGraph::new(Arc::clone(&self.conn));
let traversal = graph
.neighbors(
&q.anchor_node,
TraversalOptions {
relation: None,
max_depth: q.graph_depth,
min_weight: Some(0.0),
},
)
.unwrap_or_default();
for t in &traversal {
let e = graph_weights.entry(t.node.id.clone()).or_insert(0.0);
if t.weight > *e {
*e = t.weight;
}
}
}
// ── Step 2: Vector ANN search ──────────────────────────────────────
let fetch_k = (q.top_k * 20).max(100);
let vec_results: Vec<SearchResult> = if q.alpha > 0.0 && !q.embedding.is_empty() {
col.search(
&q.embedding,
SearchOptions {
top_k: fetch_k,
metric: DistanceMetric::Cosine,
filter: q.filter.clone(),
},
)
.unwrap_or_default()
} else {
vec![]
};
// ── Step 3: FTS search ─────────────────────────────────────────────
let fts_results = if q.gamma > 0.0 && !q.text_query.is_empty() {
let fts = FullTextStore::new(Arc::clone(&self.conn));
fts.search(&q.collection, &q.text_query, fetch_k)
.unwrap_or_default()
} else {
vec![]
};
// ── Collect candidate IDs from all three sources ───────────────────
let mut candidate_ids: std::collections::HashSet<String> = std::collections::HashSet::new();
for r in &vec_results {
candidate_ids.insert(r.id.clone());
}
for r in &fts_results {
candidate_ids.insert(r.id.clone());
}
for id in graph_weights.keys() {
candidate_ids.insert(id.clone());
}
if candidate_ids.is_empty() {
return Ok(vec![]);
}
// ── Normalise vector scores ────────────────────────────────────────
// ANN returns cosine distance (lower = more similar). Convert to similarity.
let vec_map: HashMap<String, f32> = if !vec_results.is_empty() {
let max_s = vec_results
.iter()
.map(|r| r.score)
.fold(f32::NEG_INFINITY, f32::max);
let min_s = vec_results
.iter()
.map(|r| r.score)
.fold(f32::INFINITY, f32::min);
let range = (max_s - min_s).max(1e-6);
vec_results
.iter()
.map(|r| {
let normalised = 1.0 - (r.score - min_s) / range;
(r.id.clone(), normalised)
})
.collect()
} else {
HashMap::new()
};
// ── Normalise graph weights ────────────────────────────────────────
let graph_norm: HashMap<String, f32> = if !graph_weights.is_empty() {
let max_g = graph_weights
.values()
.copied()
.fold(f64::NEG_INFINITY, f64::max);
let min_g = graph_weights
.values()
.copied()
.fold(f64::INFINITY, f64::min);
let range_g = (max_g - min_g).max(1e-9);
graph_weights
.iter()
.map(|(id, &w)| {
let normalised = ((w - min_g) / range_g) as f32;
(id.clone(), normalised)
})
.collect()
} else {
HashMap::new()
};
// ── Normalise FTS ranks ────────────────────────────────────────────
// BM25 scores from SQLite FTS5 are negative (more negative = better).
// Invert and normalise so that higher is better.
let fts_map: HashMap<String, f32> = if !fts_results.is_empty() {
// Negate so the best result (most negative BM25) becomes the largest value.
let negated: Vec<f64> = fts_results.iter().map(|r| -r.rank).collect();
let max_f = negated.iter().copied().fold(f64::NEG_INFINITY, f64::max);
let min_f = negated.iter().copied().fold(f64::INFINITY, f64::min);
let range_f = (max_f - min_f).max(1e-9);
fts_results
.iter()
.zip(negated.iter())
.map(|(r, &neg)| {
let normalised = ((neg - min_f) / range_f) as f32;
(r.id.clone(), normalised)
})
.collect()
} else {
HashMap::new()
};
// ── Collect metadata from vector results ───────────────────────────
let meta_map: HashMap<String, Option<Value>> = vec_results
.iter()
.map(|r| (r.id.clone(), r.metadata.clone()))
.collect();
// ── Blend scores ───────────────────────────────────────────────────
let mut blended: Vec<TriModalResult> = candidate_ids
.into_iter()
.map(|id| {
let vs = vec_map.get(&id).copied();
let gw = graph_norm.get(&id).copied();
let fr = fts_map.get(&id).copied();
let rank = q.alpha * vs.unwrap_or(0.0)
+ q.beta * gw.unwrap_or(0.0)
+ q.gamma * fr.unwrap_or(0.0);
let metadata = meta_map.get(&id).cloned().flatten();
TriModalResult {
id,
rank_score: rank,
vector_score: vs,
graph_weight: gw,
fts_rank: fr,
metadata,
}
})
.collect();
blended.sort_by(|a, b| {
b.rank_score
.partial_cmp(&a.rank_score)
.unwrap_or(std::cmp::Ordering::Equal)
});
blended.truncate(q.top_k);
Ok(blended)
}
}