Skip to content

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

Rust

perf: reduce storage and query overhead

AkitaOnRails087e11521387
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 48855cae..8b363437 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
providers, and embedding providers.
### Changed
+- Same-body page upserts are now true no-ops, avoiding periodic watcher
+ reconcile writes, FTS churn, and misleading recent-page timestamps.
- Documented the vector backend policy and the measured criteria required
before adding `sqlite-vec`.
- Clarified Gemini CLI support docs: MCP registration, lifecycle hooks,
@@ -40,6 +42,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Docker images now bundle both POSIX and PowerShell hook scripts.
### Fixed
+- `backup --to` now streams the tarball to disk instead of buffering the full
+ archive in CLI memory.
- Hyphenated FTS5 queries such as `ai-memory` are normalized safely instead of
being parsed as column operators.
- Gemini 2.5 Flash requests disable default dynamic thinking so hidden thought
diff --git a/crates/ai-memory-cli/src/commands/backup.rs b/crates/ai-memory-cli/src/commands/backup.rs
index e9b187c1..51fca6f4 100644
--- a/crates/ai-memory-cli/src/commands/backup.rs
+++ b/crates/ai-memory-cli/src/commands/backup.rs
@@ -2,15 +2,14 @@
//! `/admin/backup` endpoint and save the returned gzip tarball.
//!
//! The server uses SQLite's online backup API so the live DB is never
-//! raced. This client simply receives the bytes and writes them to
-//! the caller-supplied path.
+//! raced. This client streams the response to the caller-supplied path.
use anyhow::{Context, Result};
use tracing::info;
use crate::cli::BackupArgs;
use crate::config::Config;
-use crate::http_client::{ServerEndpoint, post_bytes};
+use crate::http_client::{ServerEndpoint, post_to_file};
/// Run the `backup` subcommand.
///
@@ -19,10 +18,6 @@ use crate::http_client::{ServerEndpoint, post_bytes};
/// returns a non-2xx status, or the output file cannot be written.
pub async fn run(config: &Config, args: BackupArgs) -> Result<()> {
let endpoint = ServerEndpoint::from_config(config);
- let bytes = post_bytes(&endpoint, "/admin/backup")
- .await
- .context("requesting backup from server")?;
-
let dest = &args.to;
if let Some(parent) = dest.parent()
&& !parent.as_os_str().is_empty()
@@ -30,10 +25,10 @@ pub async fn run(config: &Config, args: BackupArgs) -> Result<()> {
std::fs::create_dir_all(parent)
.with_context(|| format!("creating parent dir for {}", dest.display()))?;
}
- std::fs::write(dest, &bytes)
- .with_context(|| format!("writing backup to {}", dest.display()))?;
+ let size = post_to_file(&endpoint, "/admin/backup", dest)
+ .await
+ .context("requesting backup from server")?;
- let size = bytes.len() as u64;
info!(path = %dest.display(), bytes = size, "backup written");
println!(
"✓ wrote backup to {} ({})",
diff --git a/crates/ai-memory-cli/src/commands/serve.rs b/crates/ai-memory-cli/src/commands/serve.rs
index 47494ce9..09c3e289 100644
--- a/crates/ai-memory-cli/src/commands/serve.rs
+++ b/crates/ai-memory-cli/src/commands/serve.rs
@@ -346,7 +346,7 @@ async fn run_embedding_backfill(
let dim = embedder.dim();
let candidates = reader.decay_candidates(workspace_id, project_id).await?;
let already: std::collections::HashSet<_> = reader
- .load_embeddings(
+ .embedded_page_ids(
workspace_id,
project_id,
provider.clone(),
@@ -355,7 +355,6 @@ async fn run_embedding_backfill(
)
.await?
.into_iter()
- .map(|s| s.id)
.collect();
let mut embedded = 0usize;
diff --git a/crates/ai-memory-cli/src/http_client.rs b/crates/ai-memory-cli/src/http_client.rs
index af82181b..c7044ac7 100644
--- a/crates/ai-memory-cli/src/http_client.rs
+++ b/crates/ai-memory-cli/src/http_client.rs
@@ -10,6 +10,9 @@
//! `AI_MEMORY_AUTH_TOKEN` exactly once; this module only consumes the
//! resolved values.
+use std::io::{BufWriter, Write as _};
+use std::path::Path;
+
use anyhow::{Context, Result, bail};
use serde::Serialize;
use serde::de::DeserializeOwned;
@@ -186,20 +189,20 @@ fn augment_connect_error(
}
}
-/// POST an empty body to `<endpoint>{path}`, return the raw response bytes.
+/// POST an empty body to `<endpoint>{path}`, streaming the response to `dest`.
///
/// Intended for routes whose response is binary (e.g. `POST /admin/backup`
/// returns an `application/gzip` tarball). On non-2xx the response body is
-/// consumed and returned as an error string.
+/// consumed and returned as an error string. Returns bytes written.
///
/// # Errors
/// Returns an error when the connection fails, the response is non-2xx,
-/// or the body cannot be read.
-pub async fn post_bytes(endpoint: &ServerEndpoint, path: &str) -> Result<Vec<u8>> {
+/// or the body cannot be read or written.
+pub async fn post_to_file(endpoint: &ServerEndpoint, path: &str, dest: &Path) -> Result<u64> {
let client = reqwest::Client::new();
let url = format!("{}{path}", endpoint.url);
let req = endpoint.authenticate(client.post(&url));
- let resp = req
+ let mut resp = req
.send()
.await
.map_err(|e| augment_connect_error(e, endpoint, &url))?;
@@ -208,10 +211,24 @@ pub async fn post_bytes(endpoint: &ServerEndpoint, path: &str) -> Result<Vec<u8>
let body = resp.text().await.unwrap_or_default();
bail!("server returned {status}: {body}");
}
- resp.bytes()
+ let file = std::fs::File::create(dest)
+ .with_context(|| format!("creating output file {}", dest.display()))?;
+ let mut writer = BufWriter::new(file);
+ let mut written = 0_u64;
+ while let Some(chunk) = resp
+ .chunk()
.await
- .map(|b| b.to_vec())
- .with_context(|| format!("reading response bytes from POST {url}"))
+ .with_context(|| format!("reading response chunk from POST {url}"))?
+ {
+ writer
+ .write_all(&chunk)
+ .with_context(|| format!("writing response chunk to {}", dest.display()))?;
+ written += chunk.len() as u64;
+ }
+ writer
+ .flush()
+ .with_context(|| format!("flushing {}", dest.display()))?;
+ Ok(written)
}
#[cfg(test)]
diff --git a/crates/ai-memory-consolidate/src/bootstrap.rs b/crates/ai-memory-consolidate/src/bootstrap.rs
index 630f8b2e..9871e9d8 100644
--- a/crates/ai-memory-consolidate/src/bootstrap.rs
+++ b/crates/ai-memory-consolidate/src/bootstrap.rs
@@ -730,13 +730,16 @@ pub fn prune_sources_to_budget(
let total_count = sources.len();
let mut total: usize = sources.iter().map(BootstrapSource::estimated_tokens).sum();
- // We iterate sources in drop order and remove until we fit.
+ // We iterate sources in drop order, then drain once. Repeated
+ // `remove(0)` shifts the whole vector each time on large repos.
+ let mut drop_count = 0;
while total > usable
- && let Some(victim) = sources.first()
+ && let Some(victim) = sources.get(drop_count)
{
total = total.saturating_sub(victim.estimated_tokens());
- sources.remove(0);
+ drop_count += 1;
}
+ sources.drain(0..drop_count);
let kept = sources;
let dropped = total_count - kept.len();
(kept, dropped, total)
diff --git a/crates/ai-memory-mcp/src/admin.rs b/crates/ai-memory-mcp/src/admin.rs
index 2cbdae77..4b590930 100644
--- a/crates/ai-memory-mcp/src/admin.rs
+++ b/crates/ai-memory-mcp/src/admin.rs
@@ -928,11 +928,10 @@ async fn handle_embed(
} else {
state
.reader
- .load_embeddings(ws, proj, provider.clone(), model.clone(), dim)
+ .embedded_page_ids(ws, proj, provider.clone(), model.clone(), dim)
.await
.map_err(|e| internal_err(e.to_string()))?
.into_iter()
- .map(|s| s.id)
.collect()
};
diff --git a/crates/ai-memory-store/migrations/V08__performance_indexes.sql b/crates/ai-memory-store/migrations/V08__performance_indexes.sql
new file mode 100644
index 00000000..11838a64
--- /dev/null
+++ b/crates/ai-memory-store/migrations/V08__performance_indexes.sql
@@ -0,0 +1,34 @@
+-- Performance indexes and narrower FTS maintenance for growing stores.
+--
+-- These are additive except for the FTS update trigger. Page title/body are
+-- the only columns mirrored into pages_fts, so metadata-only updates should
+-- not delete/reinsert FTS rows.
+
+DROP TRIGGER IF EXISTS pages_fts_au;
+
+CREATE TRIGGER pages_fts_au AFTER UPDATE OF title, body ON pages BEGIN
+ INSERT INTO pages_fts(pages_fts, rowid, title, body)
+ VALUES ('delete', old.rowid, old.title, old.body);
+ INSERT INTO pages_fts(rowid, title, body)
+ VALUES (new.rowid, new.title, new.body);
+END;
+
+-- Incoming-link refresh repoints all links matching a path, not only currently
+-- unresolved links. The partial unresolved index from V07 cannot serve that.
+CREATE INDEX IF NOT EXISTS idx_links_to_path ON links(to_path);
+
+-- Briefing/explore activity windows and latest-observation lookups are ordered
+-- by time across the whole store and per project.
+CREATE INDEX IF NOT EXISTS idx_sessions_started_at ON sessions(started_at DESC);
+CREATE INDEX IF NOT EXISTS idx_observations_created_at ON observations(created_at DESC);
+CREATE INDEX IF NOT EXISTS idx_observations_project_created
+ ON observations(workspace_id, project_id, created_at DESC);
+
+-- Recent-page queries only consider latest rows. Keep superseded history out
+-- of the hot ordered scan.
+CREATE INDEX IF NOT EXISTS idx_pages_latest_updated
+ ON pages(updated_at DESC)
+ WHERE is_latest = 1;
+CREATE INDEX IF NOT EXISTS idx_pages_project_latest_updated
+ ON pages(workspace_id, project_id, updated_at DESC)
+ WHERE is_latest = 1;
diff --git a/crates/ai-memory-store/src/lib.rs b/crates/ai-memory-store/src/lib.rs
index 889eb6dc..e2d3c2f3 100644
--- a/crates/ai-memory-store/src/lib.rs
+++ b/crates/ai-memory-store/src/lib.rs
@@ -24,8 +24,8 @@ pub use error::{StoreError, StoreResult};
pub use ops::{PurgeSummary, ReorgSummary};
pub use reader::{
ActivityWindow, BriefingPage, BriefingSnapshot, DecayCandidate, DerivedIndexStatus,
- EmbeddingTripleCount, ObservationHit, PageHit, PageMeta, PageSummary, ProjectSummary,
- ReaderPool, StatusCounts, StoredEmbedding, f32_vec_to_bytes,
+ EmbeddingTripleCount, ObservationHit, PageHit, PageHitWithMeta, PageMeta, PageSummary,
+ ProjectSummary, ReaderPool, StatusCounts, StoredEmbedding, f32_vec_to_bytes,
};
pub use writer::WriterHandle;
diff --git a/crates/ai-memory-store/src/ops.rs b/crates/ai-memory-store/src/ops.rs
index 9833eb7a..8844f502 100644
--- a/crates/ai-memory-store/src/ops.rs
+++ b/crates/ai-memory-store/src/ops.rs
@@ -173,14 +173,7 @@ fn upsert_page_in_tx(
if let Some((existing_id, existing_sha)) = existing {
if existing_sha == body_sha256 {
- tx.execute(
- "UPDATE pages SET updated_at = ?1 WHERE id = ?2",
- params![now, existing_id],
- )?;
- let existing_page_id = PageId::from_slice(&existing_id).map_err(StoreError::from)?;
- replace_links_in_tx(tx, &existing_page_id, page)?;
- refresh_incoming_links_for_path(tx, page, &existing_page_id)?;
- return Ok(existing_page_id);
+ return PageId::from_slice(&existing_id).map_err(StoreError::from);
}
let new_id = PageId::new();
tx.execute(
@@ -879,6 +872,24 @@ mod tests {
let id2 = upsert_page(&mut conn, &p).unwrap();
assert_eq!(id1, id2, "identical body should not supersede");
+ conn.execute(
+ "UPDATE pages SET updated_at = 123 WHERE id = ?1",
+ params![id1.as_bytes()],
+ )
+ .unwrap();
+ let id3 = upsert_page(&mut conn, &p).unwrap();
+ assert_eq!(id1, id3, "identical body should keep the same page id");
+ let updated_at: i64 = conn
+ .query_row(
+ "SELECT updated_at FROM pages WHERE id = ?1",
+ params![id1.as_bytes()],
+ |r| r.get(0),
+ )
+ .unwrap();
+ assert_eq!(
+ updated_at, 123,
+ "unchanged content should not dirty the row"
+ );
let total: i64 = conn
.query_row(
"SELECT COUNT(*) FROM pages WHERE path = ?1",
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rs
index 81887830..f06484db 100644
--- a/crates/ai-memory-store/src/reader.rs
+++ b/crates/ai-memory-store/src/reader.rs
@@ -37,6 +37,24 @@ pub struct PageHit {
pub rank: f64,
}
+/// Search hit with workspace/project names, used by the web UI to avoid
+/// per-hit metadata lookups after a global search.
+#[derive(Debug, Clone, Serialize)]
+pub struct PageHitWithMeta {
+ /// Name of the workspace containing the page.
+ pub workspace_name: String,
+ /// Name of the project containing the page.
+ pub project_name: String,
+ /// Relative path within the wiki tree.
+ pub path: PagePath,
+ /// Page title.
+ pub title: String,
+ /// FTS5 snippet of the body around the matched terms (HTML-marked).
+ pub snippet: String,
+ /// FTS5 rank score (lower is better — closer to query terms).
+ pub rank: f64,
+}
+
/// One raw observation fallback hit returned when compiled wiki pages miss.
#[derive(Debug, Clone, Serialize)]
pub struct ObservationHit {
@@ -290,7 +308,7 @@ impl ReaderPool {
pub async fn search_pages(&self, query: String, limit: usize) -> StoreResult<Vec<PageHit>> {
let query = normalize_fts_query(&query);
self.with_conn(move |conn| {
- let mut stmt = conn.prepare(
+ let mut stmt = conn.prepare_cached(
"SELECT pages.id, pages.path, pages.title, \
snippet(pages_fts, 1, '<mark>', '</mark>', '…', 24) AS snip, \
pages_fts.rank \
@@ -326,6 +344,59 @@ impl ReaderPool {
.await
}
+ /// Run a global full-text search and include workspace/project names in
+ /// each row. This keeps the web search route to one SQLite query instead
+ /// of one search query plus a metadata lookup per hit.
+ ///
+ /// # Errors
+ /// Propagates any SQL or pool error.
+ pub async fn search_pages_with_meta(
+ &self,
+ query: String,
+ limit: usize,
+ ) -> StoreResult<Vec<PageHitWithMeta>> {
+ let query = normalize_fts_query(&query);
+ self.with_conn(move |conn| {
+ let mut stmt = conn.prepare_cached(
+ "SELECT workspaces.name, projects.name, pages.path, pages.title, \
+ snippet(pages_fts, 1, '<mark>', '</mark>', '…', 24) AS snip, \
+ pages_fts.rank \
+ FROM pages_fts \
+ JOIN pages ON pages.rowid = pages_fts.rowid \
+ JOIN projects ON projects.id = pages.project_id \
+ JOIN workspaces ON workspaces.id = pages.workspace_id \
+ WHERE pages_fts MATCH ?1 AND pages.is_latest = 1 \
+ ORDER BY pages_fts.rank \
+ LIMIT ?2",
+ )?;
+ #[allow(clippy::cast_possible_wrap)]
+ let rows = stmt.query_map(params![query, limit as i64], |row| {
+ let workspace_name: String = row.get(0)?;
+ let project_name: String = row.get(1)?;
+ let path: String = row.get(2)?;
+ let title: String = row.get(3)?;
+ let snippet: String = row.get(4)?;
+ let rank: f64 = row.get(5)?;
+ Ok((workspace_name, project_name, path, title, snippet, rank))
+ })?;
+
+ let mut hits = Vec::new();
+ for row in rows {
+ let (workspace_name, project_name, path, title, snippet, rank) = row?;
+ hits.push(PageHitWithMeta {
+ workspace_name,
+ project_name,
+ path: PagePath::new(path)?,
+ title,
+ snippet,
+ rank,
+ });
+ }
+ Ok(hits)
+ })
+ .await
+ }
+
/// Run a full-text search scoped to one project.
///
/// # Errors
@@ -339,7 +410,7 @@ impl ReaderPool {
) -> StoreResult<Vec<PageHit>> {
let query = normalize_fts_query(&query);
self.with_conn(move |conn| {
- let mut stmt = conn.prepare(
+ let mut stmt = conn.prepare_cached(
"SELECT pages.id, pages.path, pages.title, \
snippet(pages_fts, 1, '<mark>', '</mark>', '…', 24) AS snip, \
pages_fts.rank \
@@ -467,7 +538,7 @@ impl ReaderPool {
/// Propagates any SQL or pool error.
pub async fn recent_pages(&self, limit: usize) -> StoreResult<Vec<PageHit>> {
self.with_conn(move |conn| {
- let mut stmt = conn.prepare(
+ let mut stmt = conn.prepare_cached(
"SELECT id, path, title, \
substr(body, 1, 240) AS snip, \
CAST(updated_at AS REAL) AS rank \
@@ -512,7 +583,7 @@ impl ReaderPool {
limit: usize,
) -> StoreResult<Vec<PageHit>> {
self.with_conn(move |conn| {
- let mut stmt = conn.prepare(
+ let mut stmt = conn.prepare_cached(
"SELECT id, path, title, \
substr(body, 1, 240) AS snip, \
CAST(updated_at AS REAL) AS rank \
@@ -559,7 +630,7 @@ impl ReaderPool {
session_id: SessionId,
) -> StoreResult<Vec<Observation>> {
self.with_conn(move |conn| {
- let mut stmt = conn.prepare(
+ let mut stmt = conn.prepare_cached(
"SELECT id, session_id, workspace_id, project_id, kind, title, body, \
importance, created_at \
FROM observations \
@@ -625,7 +696,7 @@ impl ReaderPool {
dim: u32,
) -> StoreResult<Vec<StoredEmbedding>> {
self.with_conn(move |conn| {
- let mut stmt = conn.prepare(
+ let mut stmt = conn.prepare_cached(
"SELECT page_embeddings.page_id, page_embeddings.vector, pages.path \
FROM page_embeddings \
JOIN pages ON pages.id = page_embeddings.page_id \
@@ -664,6 +735,116 @@ impl ReaderPool {
.await
}
+ /// Return page ids that already have a matching embedding row.
+ ///
+ /// This is cheaper than [`ReaderPool::load_embeddings`] for backfill paths
+ /// that only need to skip already-embedded pages.
+ ///
+ /// # Errors
+ /// Propagates any SQL or pool error.
+ pub async fn embedded_page_ids(
+ &self,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+ provider: String,
+ model: String,
+ dim: u32,
+ ) -> StoreResult<Vec<PageId>> {
+ self.with_conn(move |conn| {
+ let mut stmt = conn.prepare_cached(
+ "SELECT page_embeddings.page_id \
+ FROM page_embeddings \
+ JOIN pages ON pages.id = page_embeddings.page_id \
+ WHERE pages.workspace_id = ?1 \
+ AND pages.project_id = ?2 \
+ AND pages.is_latest = 1 \
+ AND page_embeddings.provider = ?3 \
+ AND page_embeddings.model = ?4 \
+ AND page_embeddings.dim = ?5",
+ )?;
+ let rows = stmt.query_map(
+ params![
+ workspace_id.as_bytes(),
+ project_id.as_bytes(),
+ provider,
+ model,
+ dim,
+ ],
+ |row| {
+ let id_bytes: Vec<u8> = row.get(0)?;
+ Ok(id_bytes)
+ },
+ )?;
+ let mut out = Vec::new();
+ for row in rows {
+ out.push(PageId::from_slice(&row?)?);
+ }
+ Ok(out)
+ })
+ .await
+ }
+
+ #[allow(clippy::too_many_arguments)]
+ async fn top_embedding_hits_for_project(
+ &self,
+ workspace_id: WorkspaceId,
+ project_id: ProjectId,
+ query_vec: Vec<f32>,
+ provider: String,
+ model: String,
+ dim: u32,
+ limit: usize,
+ ) -> StoreResult<Vec<(PageId, PagePath, f32)>> {
+ if limit == 0 {
+ return Ok(Vec::new());
+ }
+ self.with_conn(move |conn| {
+ let mut stmt = conn.prepare_cached(
+ "SELECT page_embeddings.page_id, page_embeddings.vector, pages.path \
+ FROM page_embeddings \
+ JOIN pages ON pages.id = page_embeddings.page_id \
+ WHERE pages.workspace_id = ?1 \
+ AND pages.project_id = ?2 \
+ AND pages.is_latest = 1 \
+ AND page_embeddings.provider = ?3 \
+ AND page_embeddings.model = ?4 \
+ AND page_embeddings.dim = ?5",
+ )?;
+ let rows = stmt.query_map(
+ params![
+ workspace_id.as_bytes(),
+ project_id.as_bytes(),
+ provider,
+ model,
+ dim,
+ ],
+ |row| {
+ let id_bytes: Vec<u8> = row.get(0)?;
+ let vec_bytes: Vec<u8> = row.get(1)?;
+ let path: String = row.get(2)?;
+ Ok((id_bytes, vec_bytes, path))
+ },
+ )?;
+
+ let mut out = Vec::new();
+ for row in rows {
+ let (id_bytes, vec_bytes, path) = row?;
+ out.push((
+ PageId::from_slice(&id_bytes)?,
+ PagePath::new(path)?,
+ dot_embedding_bytes(&query_vec, &vec_bytes, dim)?,
+ ));
+ }
+ if out.len() > limit {
+ out.select_nth_unstable_by(limit, score_desc);
+ out.truncate(limit);
+ }
+ out.sort_by(score_desc);
+ Ok(out)
+ })
+ .await
+ }
+
/// Return any `(provider, model, dim)` triples currently stored
/// that *don't* match the caller's expectation. An empty vec
/// means "all clean". Used at startup for the refuse-on-mismatch
@@ -882,19 +1063,17 @@ impl ReaderPool {
.await?;
let mut vec_hits: Vec<(PageId, PagePath, f32)> = Vec::new();
if let Some(qv) = query_vec {
- // Compute cosines against all stored embeddings.
- let stored = self
- .load_embeddings(workspace_id, project_id, provider, model, dim)
+ vec_hits = self
+ .top_embedding_hits_for_project(
+ workspace_id,
+ project_id,
+ qv,
+ provider,
+ model,
+ dim,
+ limit * 2,
+ )
.await?;
- vec_hits = stored
- .iter()
- .map(|s| {
- let score = dot(&qv, &s.vector);
- (s.id, s.path.clone(), score)
- })
- .collect();
- vec_hits.sort_by(|a, b| b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal));
- vec_hits.truncate(limit * 2);
}
let mut seed_seen = std::collections::HashSet::new();
@@ -1099,13 +1278,13 @@ impl ReaderPool {
// Rules: any `is_latest = 1` page under `_rules/`.
// Routed there automatically by the consolidator when
// `kind = "rule"` — see consolidator.rs::slugify_for_rule.
- let mut rules_stmt = conn.prepare(
+ let mut rules_stmt = conn.prepare_cached(
"SELECT path, title, \
COALESCE(json_extract(frontmatter_json, '$.kind'), 'fact') AS kind, \
updated_at \
FROM pages \
- WHERE is_latest = 1 AND path LIKE '_rules/%' \
- ORDER BY updated_at DESC",
+ WHERE is_latest = 1 AND path GLOB '_rules/*' \
+ ORDER BY updated_at DESC",
)?;
let rules: Vec<BriefingPage> = rules_stmt
.query_map([], briefing_page_from_row)?
@@ -1113,13 +1292,13 @@ impl ReaderPool {
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
- let mut slots_stmt = conn.prepare(
+ let mut slots_stmt = conn.prepare_cached(
"SELECT path, title, \
COALESCE(json_extract(frontmatter_json, '$.kind'), 'slot') AS kind, \
updated_at \
FROM pages \
- WHERE is_latest = 1 AND path LIKE '_slots/%' \
- ORDER BY path ASC",
+ WHERE is_latest = 1 AND path GLOB '_slots/*' \
+ ORDER BY path ASC",
)?;
let slots: Vec<BriefingPage> = slots_stmt
.query_map([], briefing_page_from_row)?
@@ -1127,7 +1306,7 @@ impl ReaderPool {
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
- let mut recent_stmt = conn.prepare(
+ let mut recent_stmt = conn.prepare_cached(
"SELECT path, title, \
COALESCE(json_extract(frontmatter_json, '$.kind'), 'fact') AS kind, \
updated_at \
@@ -1223,13 +1402,13 @@ impl ReaderPool {
project_id,
)?;
- let mut rules_stmt = conn.prepare(
+ let mut rules_stmt = conn.prepare_cached(
"SELECT path, title, \
COALESCE(json_extract(frontmatter_json, '$.kind'), 'fact') AS kind, \
updated_at \
FROM pages \
- WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1 AND path LIKE '_rules/%' \
- ORDER BY updated_at DESC",
+ WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1 AND path GLOB '_rules/*' \
+ ORDER BY updated_at DESC",
)?;
let rules: Vec<BriefingPage> = rules_stmt
.query_map(params![workspace_id.as_bytes(), project_id.as_bytes()], briefing_page_from_row)?
@@ -1237,13 +1416,13 @@ impl ReaderPool {
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
- let mut slots_stmt = conn.prepare(
+ let mut slots_stmt = conn.prepare_cached(
"SELECT path, title, \
COALESCE(json_extract(frontmatter_json, '$.kind'), 'slot') AS kind, \
updated_at \
FROM pages \
- WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1 AND path LIKE '_slots/%' \
- ORDER BY path ASC",
+ WHERE workspace_id = ?1 AND project_id = ?2 AND is_latest = 1 AND path GLOB '_slots/*' \
+ ORDER BY path ASC",
)?;
let slots: Vec<BriefingPage> = slots_stmt
.query_map(params![workspace_id.as_bytes(), project_id.as_bytes()], briefing_page_from_row)?
@@ -1251,7 +1430,7 @@ impl ReaderPool {
.into_iter()
.collect::<Result<Vec<_>, _>>()?;
- let mut recent_stmt = conn.prepare(
+ let mut recent_stmt = conn.prepare_cached(
"SELECT path, title, \
COALESCE(json_extract(frontmatter_json, '$.kind'), 'fact') AS kind, \
updated_at \
@@ -1789,8 +1968,36 @@ pub struct StoredEmbedding {
pub vector: Vec<f32>,
}
-fn dot(a: &[f32], b: &[f32]) -> f32 {
- a.iter().zip(b).map(|(x, y)| x * y).sum()
+fn score_desc(a: &(PageId, PagePath, f32), b: &(PageId, PagePath, f32)) -> std::cmp::Ordering {
+ b.2.partial_cmp(&a.2).unwrap_or(std::cmp::Ordering::Equal)
+}
+
+fn dot_embedding_bytes(query: &[f32], bytes: &[u8], dim: u32) -> StoreResult<f32> {
+ let dim = dim as usize;
+ if query.len() != dim {
+ return Err(StoreError::Memory(
+ ai_memory_core::MemoryError::MalformedRecord(format!(
+ "query vector dim {} != expected {}",
+ query.len(),
+ dim
+ )),
+ ));
+ }
+ let expected = dim * 4;
+ if bytes.len() != expected {
+ return Err(StoreError::Memory(
+ ai_memory_core::MemoryError::MalformedRecord(format!(
+ "embedding bytes {} != expected {}",
+ bytes.len(),
+ expected
+ )),
+ ));
+ }
+ Ok(query
+ .iter()
+ .zip(bytes.chunks_exact(4))
+ .map(|(q, chunk)| f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]) * q)
+ .sum())
}
fn bytes_to_f32_vec(bytes: &[u8], dim: u32) -> StoreResult<Vec<f32>> {
diff --git a/crates/ai-memory-web/src/routes/search.rs b/crates/ai-memory-web/src/routes/search.rs
index 5eb5fd25..dc3a63f9 100644
--- a/crates/ai-memory-web/src/routes/search.rs
+++ b/crates/ai-memory-web/src/routes/search.rs
@@ -32,29 +32,22 @@ pub(crate) async fn handler(
} else {
let raw = state
.reader
- .search_pages(query.clone(), 50)
+ .search_pages_with_meta(query.clone(), 50)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
- // Search hits carry only the page id/path/title/snippet. Resolve
- // workspace + project by page id so duplicate paths in sibling
- // projects still link to the right page.
let mut results = Vec::with_capacity(raw.len());
for h in raw {
- if let Ok(Some(m)) = state.reader.page_meta_by_id(h.id).await {
- let path = h.path.as_str().to_owned();
- let href = page_href(&m.workspace_name, &m.project_name, &path);
- results.push(SearchHit {
- workspace: m.workspace_name,
- project: m.project_name,
- path,
- href,
- title: h.title,
- snippet: markdown::escape_snippet(&h.snippet),
- });
- } else {
- // Fallback: no workspace/project known; skip for now.
- }
+ let path = h.path.as_str().to_owned();
+ let href = page_href(&h.workspace_name, &h.project_name, &path);
+ results.push(SearchHit {
+ workspace: h.workspace_name,
+ project: h.project_name,
+ path,
+ href,
+ title: h.title,
+ snippet: markdown::escape_snippet(&h.snippet),
+ });
}
results
};