Skip to content

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

Rust

fix(store): keep title and body column filters on punctuated terms

Rudy Celeklia8c6d6b56f9c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fee7fa0b..cc2d08ef 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,10 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
### Fixed
+- Kept a `title:` or `body:` search filter on its column when the term carries
+ punctuation. `title:ai-memory` was quoted whole into phrases that required
+ the literal word "title" and filtered no column, so it found nothing; it now
+ becomes `title:("ai-memory" OR "ai memory")`.
- Fixed `memory_lint` reporting findings for TTL-expired pages. The lint pass
now omits expired pages before evaluating episodic staleness, rule suggestions,
duplicate titles, and vector or LLM contradiction candidates, preventing
diff --git a/crates/ai-memory-store/src/fts_query.rs b/crates/ai-memory-store/src/fts_query.rs
index ae2f5dc4..49f77372 100644
--- a/crates/ai-memory-store/src/fts_query.rs
+++ b/crates/ai-memory-store/src/fts_query.rs
@@ -219,6 +219,18 @@ fn prepare_fts5_token(token: &str) -> Vec<String> {
.collect();
}
+ // A known column filter keeps its column when its term needs quoting:
+ // `title:ai-memory` becomes `title:("ai-memory" OR "ai memory")`, not a
+ // quoted phrase that also searches for the literal word `title`.
+ if !token.contains('"')
+ && let Some((column, term)) = token.split_once(':')
+ && matches!(column, "title" | "body")
+ && !term.is_empty()
+ && should_quote_fts5_token(term)
+ {
+ return vec![format!("{column}:{}", quote_fts5_token(term))];
+ }
+
if should_quote_fts5_token(token) {
vec![quote_fts5_token(token)]
} else {
@@ -514,6 +526,27 @@ mod tests {
assert_eq!(prepare_fts5_query("title:handoff"), "title:handoff");
}
+ /// A known column filter whose term carries punctuation keeps the filter
+ /// on the quoted term, instead of quoting `title:` into the phrase and
+ /// searching every column for the word "title".
+ #[test]
+ fn known_column_filter_survives_quoting_of_its_term() {
+ assert_eq!(
+ prepare_fts5_query("title:ai-memory"),
+ r#"title:("ai-memory" OR "ai memory")"#
+ );
+ assert_eq!(
+ prepare_fts5_query("body:current.md OR title:handoff"),
+ r#"body:("current.md" OR "current md") OR title:handoff"#
+ );
+ for raw in ["title:ai-memory", "body:a.b c", "title:-", "title:a:b"] {
+ let prepared = prepare_fts5_query(raw);
+ if !prepared.is_empty() {
+ assert_parses(&prepared);
+ }
+ }
+ }
+
// --- issue #953: configurable stopword list -----------------------
/// A custom (Portuguese) list filters the words on that list, but never
diff --git a/crates/ai-memory-store/src/ops.rs b/crates/ai-memory-store/src/ops.rs
index e8bc01ce..f3909d79 100644
--- a/crates/ai-memory-store/src/ops.rs
+++ b/crates/ai-memory-store/src/ops.rs
@@ -16533,6 +16533,35 @@ pub(crate) mod tests {
);
}
+ /// `title:` must still filter on the title column when its term has
+ /// punctuation and has to be quoted.
+ #[test]
+ fn title_filter_on_punctuated_term_searches_only_titles() {
+ let (_tmp, mut conn, ws, proj) = fresh_db();
+ upsert_page(
+ &mut conn,
+ &NewPage {
+ title: "ai-memory handoff".into(),
+ ..page(ws, proj, "notes/titled.md", "unrelated body")
+ },
+ )
+ .unwrap();
+ upsert_page(
+ &mut conn,
+ &page(
+ ws,
+ proj,
+ "notes/mention.md",
+ "a body that mentions ai-memory",
+ ),
+ )
+ .unwrap();
+
+ let hits = fts_match_paths(&conn, "title:ai-memory")
+ .expect("column-filtered search must not raise an FTS5 syntax error");
+ assert_eq!(hits, vec!["notes/titled.md".to_string()]);
+ }
+
/// Regression for the live-found hyphen bug: searching `ui-refresh`
/// returned nothing in prod even though
/// `follow-ups/ui-refresh-scroll-restoration.md` exists. The first fix