Skip to content

Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors

Rust

feat(search): OR-join bare multi-word queries (recall fix)

Djalma Júniorc09fa077e165
diff --git a/crates/ai-memory-store/src/fts_query.rs b/crates/ai-memory-store/src/fts_query.rs
index 4d968b2a..2f3f349b 100644
--- a/crates/ai-memory-store/src/fts_query.rs
+++ b/crates/ai-memory-store/src/fts_query.rs
@@ -10,13 +10,31 @@
///
/// Returns an empty string when `raw` is empty/whitespace-only; callers
/// should skip the SQL query in that case.
+///
+/// Bare multi-word queries are joined with **`OR`**, not the FTS5 default
+/// (`AND`). A natural-language query like "cross project search strategy"
+/// otherwise requires every word to co-occur in one page — near-zero recall
+/// for anything but single keywords. With `OR` + bm25 ranking (callers
+/// `ORDER BY rank`), the best-matching pages still surface first. When the
+/// caller supplies explicit FTS5 syntax (`OR` / `AND` / `NOT` / `NEAR` /
+/// quoted phrases / parens) we preserve it verbatim instead.
#[must_use]
pub fn prepare_fts5_query(raw: &str) -> String {
+ let explicit_syntax = raw.contains('"')
+ || raw.contains('(')
+ || raw.contains(')')
+ || raw
+ .split_whitespace()
+ .any(|t| matches!(t, "OR" | "AND" | "NOT" | "NEAR"));
let tokens: Vec<String> = raw
.split_whitespace()
.flat_map(prepare_fts5_token)
.collect();
- tokens.join(" ")
+ if tokens.is_empty() {
+ return String::new();
+ }
+ let separator = if explicit_syntax { " " } else { " OR " };
+ tokens.join(separator)
}
fn prepare_fts5_token(token: &str) -> Vec<String> {
@@ -58,8 +76,33 @@ mod tests {
#[test]
fn colon_is_not_column_syntax() {
+ // Bare multi-word → OR-joined (no explicit operator present).
let q = prepare_fts5_query("pick: handoff ai-memory");
- assert_eq!(q, "\"pick\" handoff \"ai-memory\"");
+ assert_eq!(q, "\"pick\" OR handoff OR \"ai-memory\"");
+ }
+
+ #[test]
+ fn bare_multi_word_is_or_joined() {
+ // The recall fix: every word no longer has to co-occur.
+ assert_eq!(
+ prepare_fts5_query("cross project search strategy"),
+ "cross OR project OR search OR strategy"
+ );
+ }
+
+ #[test]
+ fn portuguese_accented_terms_or_join_and_keep_accents() {
+ // PT natural-language query: tokens preserved (accents intact),
+ // joined with OR so a page matching any term is found.
+ assert_eq!(
+ prepare_fts5_query("descrição testes commits"),
+ "descrição OR testes OR commits"
+ );
+ }
+
+ #[test]
+ fn single_word_has_no_or() {
+ assert_eq!(prepare_fts5_query("handoff"), "handoff");
}
#[test]