Skip to content

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

Rust

fix(store): keep the FTS5 prefix operator on punctuated search terms

Rudy Celeklid058a73c24f3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index fee7fa0b..c32d8433 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,6 +12,10 @@ version in [`docs/changelog/`](docs/changelog/):
## [Unreleased]
### Fixed
+- Kept the FTS5 prefix operator on search terms that carry punctuation. A
+ search such as `ai-mem*` or `ui-ref*` was quoted with the `*` inside the
+ phrase, where FTS5 reads it as a separator, so it became an exact search and
+ matched nothing; the `*` now follows each quoted phrase.
- 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..4690b48f 100644
--- a/crates/ai-memory-store/src/fts_query.rs
+++ b/crates/ai-memory-store/src/fts_query.rs
@@ -269,15 +269,24 @@ fn quote_fts5_token(token: &str) -> String {
// punctuation that would otherwise be FTS5 query grammar — the original
// `current.md` → `syntax error` bug.) With no punctuation the two coincide
// and we emit a single phrase.
+ //
+ // A trailing `*` is the FTS5 prefix operator. Inside a quoted string it is
+ // only a separator, so it moves outside each closing quote instead:
+ // `ai-mem*` becomes `("ai-mem"* OR "ai mem"*)`, which FTS5 reads as a
+ // prefix match on the phrase's last token.
+ let (token, prefix) = match token.strip_suffix('*') {
+ Some(core) if core.chars().any(char::is_alphanumeric) => (core, "*"),
+ _ => (token, ""),
+ };
let split = token
.chars()
.map(|c| if c.is_ascii_punctuation() { ' ' } else { c })
.collect::<String>();
let split = split.split_whitespace().collect::<Vec<_>>().join(" ");
if split.is_empty() || split == token {
- format!("\"{token}\"")
+ format!("\"{token}\"{prefix}")
} else {
- format!("(\"{token}\" OR \"{split}\")")
+ format!("(\"{token}\"{prefix} OR \"{split}\"{prefix})")
}
}
@@ -437,6 +446,28 @@ mod tests {
assert_eq!(prepare_fts5_query("curr*"), "curr*");
}
+ /// A punctuated token is quoted, and inside an FTS5 string `*` is only a
+ /// separator. The prefix operator has to follow each closing quote, or
+ /// `ai-mem*` silently becomes an exact search for `ai-mem`.
+ #[test]
+ fn prefix_star_survives_quoting_of_punctuated_tokens() {
+ assert_eq!(prepare_fts5_query("ai-mem*"), r#"("ai-mem"* OR "ai mem"*)"#);
+ assert_eq!(
+ prepare_fts5_query("current.m*"),
+ r#"("current.m"* OR "current m"*)"#
+ );
+ // Unknown column syntax is split, and the prefix stays on its term.
+ assert_eq!(prepare_fts5_query("pick:hand*"), r#""pick" OR "hand"*"#);
+ // A token of punctuation alone has no term to prefix-match.
+ assert_eq!(quote_fts5_token("-*"), r#""-*""#);
+ for raw in ["ai-mem*", "current.m*", "pick:hand*", "a-* b", "-*", "x.*"] {
+ let prepared = prepare_fts5_query(raw);
+ if !prepared.is_empty() {
+ assert_parses(&prepared);
+ }
+ }
+ }
+
#[test]
fn empty_yields_empty() {
assert_eq!(prepare_fts5_query(" "), "");
diff --git a/crates/ai-memory-store/src/ops.rs b/crates/ai-memory-store/src/ops.rs
index e8bc01ce..6bf52ca2 100644
--- a/crates/ai-memory-store/src/ops.rs
+++ b/crates/ai-memory-store/src/ops.rs
@@ -16582,6 +16582,32 @@ pub(crate) mod tests {
assert_eq!(count("\"ui refresh\""), 1, "sub-token phrase must match");
}
+ /// A prefix search on a hyphenated term (`ai-mem*`) must still be a
+ /// prefix search once the token is quoted for its punctuation, and match
+ /// both the content token `ai-memory` and the split path index.
+ #[test]
+ fn prefix_search_on_punctuated_term_matches_content_and_path() {
+ let (_tmp, mut conn, ws, proj) = fresh_db();
+ upsert_page(
+ &mut conn,
+ &page(ws, proj, "notes/content.md", "we ship ai-memory today"),
+ )
+ .unwrap();
+ upsert_page(
+ &mut conn,
+ &page(ws, proj, "follow-ups/ui-refresh-scroll.md", "body text"),
+ )
+ .unwrap();
+
+ let hits = fts_match_paths(&conn, "ai-mem*")
+ .expect("prefix search must not raise an FTS5 syntax error");
+ assert_eq!(hits, vec!["notes/content.md".to_string()]);
+
+ let hits = fts_match_paths(&conn, "ui-ref*")
+ .expect("prefix search must not raise an FTS5 syntax error");
+ assert_eq!(hits, vec!["follow-ups/ui-refresh-scroll.md".to_string()]);
+ }
+
/// Issue #103 legacy heal: the two broad sentinels ($HOME and `/`) are
/// always NULLed. Here the inputs are non-existent fake paths, so the
/// nested row is preserved by the multi-user/unmounted safety rule (a