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
//! Semantic search and similarity operations.
use super::{Database, Error, Memory, embedding};
use crate::memory::store::MAX_SEARCH_LIMIT;
pub type Result<T> = std::result::Result<T, Error>;
/// Validate search limit is within acceptable bounds.
pub fn validate_limit(limit: usize) -> Result<()> {
if limit == 0 {
return Err(Error::InvalidLimit(
"Limit must be greater than 0".to_string(),
));
}
if limit > i64::MAX as usize || limit > MAX_SEARCH_LIMIT {
return Err(Error::InvalidLimit(format!(
"Limit {} exceeds maximum allowed ({})",
limit, MAX_SEARCH_LIMIT
)));
}
Ok(())
}
impl Database {
/// Search for similar memories using semantic (cosine) similarity.
///
/// Retrieves all memories for a project, computes cosine similarity with the query
/// embedding, sorts by similarity (highest first), and returns the top `limit` results.
///
/// # Arguments
///
/// * `project_id` - Project identifier
/// * `query_embedding` - The embedding vector to compare against
/// * `limit` - Maximum number of results to return
/// * `memory_types` - Optional filter for memory types (None = no filter by type)
/// * `statuses` - Optional filter for lifecycle statuses (None = default to 'active')
///
/// # Errors
///
/// Returns error if the query embedding has invalid dimensions or if the database
/// query fails.
pub fn search(
&self,
project_id: &str,
query_embedding: &[f32],
limit: usize,
memory_types: Option<&[&str]>,
statuses: Option<&[&str]>,
) -> Result<Vec<Memory>> {
validate_limit(limit)?;
let mut where_clauses = vec!["project_id = ?1".to_string()];
let mut param_index = 2usize;
// Status filter (default to active if None)
if let Some(statuses) = statuses {
if !statuses.is_empty() {
let placeholders: Vec<String> = (0..statuses.len())
.map(|i| format!("?{}", param_index + i))
.collect();
where_clauses.push(format!("status IN ({})", placeholders.join(", ")));
param_index += statuses.len();
}
} else {
where_clauses.push(format!("status = ?{}", param_index));
param_index += 1;
}
// Type filter (only if explicitly provided)
if let Some(types) = memory_types {
if !types.is_empty() {
let placeholders: Vec<String> = (0..types.len())
.map(|i| format!("?{}", param_index + i))
.collect();
where_clauses.push(format!("type IN ({})", placeholders.join(", ")));
}
}
let where_clause = where_clauses.join(" AND ");
let query = format!(
"SELECT id, project_id, content, metadata, embedding, created_at, updated_at, type, status, superseded_by, retrieval_count, last_retrieved_at
FROM memories WHERE {} ORDER BY created_at DESC",
where_clause
);
let mut stmt = self.conn.prepare(&query)?;
let mut params: Vec<&dyn rusqlite::ToSql> = vec![&project_id];
if let Some(statuses) = statuses {
if statuses.is_empty() {
// explicit empty = no status filter, but we didn't add a clause
} else {
for s in statuses {
params.push(s);
}
}
} else {
params.push(&"active");
}
if let Some(types) = memory_types {
for t in types {
params.push(t);
}
}
let mut memories: Vec<Memory> = Vec::new();
// The query vector is identical for every row in the scan, so its L2
// norm is computed exactly once here (f64 accumulation, matching
// `cosine_similarity_with_norm`) instead of once per row. This is
// safe because `query_norm` is read only at the final division, which
// is unreachable for the degenerate cases: an empty corpus leaves the
// per-row loop unrun; a NaN, empty, or dimension-mismatched query
// errors before the division; and a zero-norm operand (all-zero
// query or stored vector) hits the `Ok(0.0)` guard first. (A NaN
// query yields a NaN hoisted norm — NaN squared is NaN — but that
// path errors as well.)
let query_norm: f64 = query_embedding
.iter()
.map(|x| (*x as f64).powi(2))
.sum::<f64>()
.sqrt();
let rows = stmt.query_map(params.as_slice(), |row| {
Ok((
row.get::<_, String>(0)?,
row.get::<_, String>(1)?,
row.get::<_, String>(2)?,
row.get::<_, Option<String>>(3)?,
row.get::<_, Vec<u8>>(4)?,
row.get::<_, String>(5)?,
row.get::<_, String>(6)?,
row.get::<_, String>(7)?,
row.get::<_, String>(8)?,
row.get::<_, Option<String>>(9)?,
row.get::<_, i64>(10)?,
row.get::<_, Option<String>>(11)?,
))
})?;
for row_result in rows {
let (
id,
pid,
content,
metadata,
blob,
created_at,
updated_at,
type_val,
status_val,
superseded_by,
retrieval_count,
last_retrieved_at,
) = row_result?;
let stored_embedding = embedding::blob_to_vec(&blob).map_err(|e| {
rusqlite::Error::FromSqlConversionFailure(
6,
rusqlite::types::Type::Blob,
Box::new(e),
)
})?;
let similarity = Some(embedding::cosine_similarity_with_norm(
query_embedding,
query_norm,
&stored_embedding,
)?);
memories.push(Memory {
id,
project_id: pid,
content,
metadata,
embedding: stored_embedding,
similarity,
created_at,
updated_at,
memory_type: type_val,
status: status_val,
superseded_by,
retrieval_count,
last_retrieved_at,
});
}
memories.sort_by(|a, b| {
b.similarity
.unwrap_or(0.0)
.partial_cmp(&a.similarity.unwrap_or(0.0))
.unwrap_or(std::cmp::Ordering::Equal)
});
memories.truncate(limit);
Ok(memories)
}
/// Find memories similar to the given embedding above a threshold.
///
/// Uses semantic search to find all memories with cosine similarity >= threshold.
///
/// # Errors
///
/// Returns error if the search fails.
pub fn find_similar(
&self,
project_id: &str,
embedding: &[f32],
threshold: f64,
) -> Result<Vec<Memory>> {
let all_results = self.search(project_id, embedding, MAX_SEARCH_LIMIT, None, None)?;
Ok(all_results
.into_iter()
.filter(|m| m.similarity.unwrap_or(0.0) >= threshold)
.collect())
}
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::TempDir;
fn create_test_db() -> Database {
let dir = TempDir::new().unwrap();
let path = dir.path().join("test.db");
let db = Database::open(&path).unwrap();
std::mem::forget(dir);
db
}
#[test]
fn test_validate_limit_zero() {
assert!(validate_limit(0).is_err());
}
#[test]
fn test_validate_limit_too_large() {
assert!(validate_limit(100_000).is_err());
}
#[test]
fn test_validate_limit_valid() {
assert!(validate_limit(10).is_ok());
assert!(validate_limit(5000).is_ok());
}
#[test]
fn test_search_basic() {
let db = create_test_db();
let embedding = vec![0.1f32; 384];
db.insert(
"proj1",
"rust programming",
&embedding,
None,
"fact",
"active",
)
.unwrap();
db.insert(
"proj1",
"python data science",
&embedding,
None,
"fact",
"active",
)
.unwrap();
let results = db.search("proj1", &embedding, 10, None, None).unwrap();
assert_eq!(results.len(), 2);
assert!(results[0].similarity.unwrap() >= 0.9);
}
#[test]
fn test_search_limit() {
let db = create_test_db();
let embedding = vec![0.1f32; 384];
for i in 0..5 {
db.insert(
"proj1",
&format!("content {}", i),
&embedding,
None,
"fact",
"active",
)
.unwrap();
}
let results = db.search("proj1", &embedding, 2, None, None).unwrap();
assert_eq!(results.len(), 2);
}
#[test]
fn test_search_project_isolation() {
let db = create_test_db();
let embedding = vec![0.1f32; 384];
db.insert(
"proj1",
"project 1 memory",
&embedding,
None,
"fact",
"active",
)
.unwrap();
db.insert(
"proj2",
"project 2 memory",
&embedding,
None,
"fact",
"active",
)
.unwrap();
let results = db.search("proj1", &embedding, 10, None, None).unwrap();
assert_eq!(results.len(), 1);
assert_eq!(results[0].project_id, "proj1");
}
#[test]
fn test_search_empty_corpus_degenerate_queries() {
let db = create_test_db();
// Zero rows for "proj1": the per-row validation inside cosine_similarity
// never runs, so degenerate query vectors must return Ok(empty), not Err.
// This locks the empty-corpus contract: hoisting validation out of the
// loop (a follow-up) would break this test and force an explicit
// contract decision.
// Empty query vector
let results = db.search("proj1", &[], 10, None, None).unwrap();
assert!(results.is_empty());
// NaN-containing query vector
let nan_query: Vec<f32> = vec![f32::NAN; 384];
let results = db.search("proj1", &nan_query, 10, None, None).unwrap();
assert!(results.is_empty());
}
#[test]
fn test_find_similar_with_threshold() {
let db = create_test_db();
let embedding1 = vec![1.0f32; 384];
let mut embedding2 = vec![1.0f32; 384];
embedding2[0] = 0.0; // Slightly different
db.insert("proj1", "memory 1", &embedding1, None, "fact", "active")
.unwrap();
db.insert("proj1", "memory 2", &embedding2, None, "fact", "active")
.unwrap();
let results = db.find_similar("proj1", &embedding1, 0.99).unwrap();
assert!(!results.is_empty());
}
}