Skip to content

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

Rust

perf(store): seek the observation rowid span on the rowid-ordered index

AkitaOnRailsec9712039f16
diff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rs
index 0f5d28b2..3f97560d 100644
--- a/crates/ai-memory-store/src/reader.rs
+++ b/crates/ai-memory-store/src/reader.rs
@@ -362,6 +362,20 @@ const OBSERVATION_STREAM_IDS_PER_PAGE: usize = 3;
/// window; an observation body usually does, and a full window made each
/// excerpt-backed hit cost more context than the page hit it stands in for.
const OBSERVATION_SNIPPET_TOKENS: usize = 16;
+/// A project's first and last observation rowid, bound to `?1` = project id.
+///
+/// `idx_observations_project` is `(project_id)` with the implicit rowid
+/// suffix, so each scalar subquery is a single min/max seek. Filtering on
+/// `(workspace_id, project_id)` instead lets the planner pick the
+/// created_at-ordered `idx_observations_project_created` and walk every one
+/// of the project's entries. Project ids are unique across workspaces (V18's
+/// pairing triggers keep `observations.workspace_id` equal to the project's),
+/// so the workspace predicate adds nothing here.
+pub(crate) const OBSERVATION_ROWID_SPAN_SQL: &str = "SELECT \
+ (SELECT MIN(rowid) FROM observations INDEXED BY idx_observations_project \
+ WHERE project_id = ?1), \
+ (SELECT MAX(rowid) FROM observations INDEXED BY idx_observations_project \
+ WHERE project_id = ?1)";
#[derive(Debug, Clone, Copy)]
struct PageAuthority {
@@ -721,8 +735,8 @@ pub struct RrfContributions {
/// `[retrieval] abstract_vectors`; 0.0 when off or missed).
#[serde(rename = "abstract", skip_serializing_if = "is_zero")]
pub abstract_vector: f64,
- /// Observation-evidence stream contribution (opt-in
- /// `[retrieval] observation_stream`; 0.0 when off or missed).
+ /// Observation-evidence stream contribution (`[retrieval]
+ /// observation_stream`, on by default; 0.0 when disabled or missed).
#[serde(skip_serializing_if = "is_zero")]
pub observation: f64,
}
@@ -857,8 +871,8 @@ pub struct SearchExplain {
/// Entity names on this page that matched the query.
#[serde(skip_serializing_if = "Vec::is_empty")]
pub matched_entities: Vec<String>,
- /// 1-based rank in the observation-evidence stream (opt-in
- /// `[retrieval] observation_stream`): where this session page's
+ /// 1-based rank in the observation-evidence stream (`[retrieval]
+ /// observation_stream`, on by default): where this session page's
/// best-matching observation placed among the matching sessions.
#[serde(skip_serializing_if = "Option::is_none")]
pub observation_rank: Option<usize>,
@@ -3039,14 +3053,13 @@ impl ReaderPool {
let tx = conn.unchecked_transaction()?;
// `observations_fts` spans every project, and bm25 runs for every
// match before the scope filter can drop it. The project's rowid
- // span (one covering-index probe) bounds the index scan to the
- // stretch of the log where this project wrote; the scope
- // predicates below still decide membership inside it.
+ // span bounds the index scan to the stretch of the log where
+ // this project wrote; the scope predicates below still decide
+ // membership inside it.
let span: Option<(i64, i64)> = tx
.query_row(
- "SELECT MIN(rowid), MAX(rowid) FROM observations \
- WHERE workspace_id = ?1 AND project_id = ?2",
- params![workspace_id.as_bytes(), project_id.as_bytes()],
+ OBSERVATION_ROWID_SPAN_SQL,
+ params![project_id.as_bytes()],
|row| Ok((row.get::<_, Option<i64>>(0)?, row.get::<_, Option<i64>>(1)?)),
)
.map(|(lo, hi)| lo.zip(hi))?;
diff --git a/crates/ai-memory-store/tests/suite/retrieval_observation_stream.rs b/crates/ai-memory-store/tests/suite/retrieval_observation_stream.rs
index 1b3d8c69..8cef1abf 100644
--- a/crates/ai-memory-store/tests/suite/retrieval_observation_stream.rs
+++ b/crates/ai-memory-store/tests/suite/retrieval_observation_stream.rs
@@ -545,3 +545,91 @@ async fn a_curated_page_keeps_its_page_fts_contribution() {
assert!((decision.rrf.fts - 1.0 / (K + 1.0)).abs() < 1e-12);
assert!(decision.rrf.observation.abs() < 1e-12);
}
+
+/// The stream's rowid-span probe runs on every query, so it must stay a
+/// min/max seek on the rowid-ordered `idx_observations_project`: filtering on
+/// `(workspace_id, project_id)` let the planner choose the created_at-ordered
+/// index and walk all of the project's entries. It must also return exactly
+/// what the original two-column aggregate did, for every project in a store
+/// whose projects interleave their writes.
+#[tokio::test]
+async fn the_rowid_span_probe_seeks_the_rowid_ordered_index_and_matches_the_aggregate() {
+ let tmp = tempfile::tempdir().unwrap();
+ let store = Store::open(tmp.path()).unwrap();
+ let scopes = [
+ scope(&store, "default", "alpha").await,
+ scope(&store, "default", "beta").await,
+ scope(&store, "other", "alpha").await,
+ ];
+ let mut sessions = Vec::new();
+ for &(ws, proj) in &scopes {
+ sessions.push(session(&store, ws, proj).await);
+ }
+ for round in 0..4 {
+ for (i, &(ws, proj)) in scopes.iter().enumerate() {
+ // The third project writes only in the middle rounds, so its span
+ // sits strictly inside the others'.
+ if i == 2 && !(1..=2).contains(&round) {
+ continue;
+ }
+ observe(&store, ws, proj, sessions[i], &format!("note {round} {i}")).await;
+ }
+ }
+ let (empty_ws, empty_proj) = scope(&store, "default", "empty").await;
+
+ let conn = rusqlite::Connection::open(store.db_path()).unwrap();
+ let plan: Vec<String> = conn
+ .prepare(&format!(
+ "EXPLAIN QUERY PLAN {}",
+ crate::reader::OBSERVATION_ROWID_SPAN_SQL
+ ))
+ .unwrap()
+ .query_map([scopes[0].1.as_bytes()], |row| row.get::<_, String>(3))
+ .unwrap()
+ .collect::<Result<_, _>>()
+ .unwrap();
+ let plan = plan.join("\n");
+ assert!(
+ plan.contains("COVERING INDEX idx_observations_project (project_id=?)"),
+ "span probe must seek the rowid-ordered index:\n{plan}"
+ );
+ assert!(
+ !plan.contains("idx_observations_project_created") && !plan.contains("SCAN observations"),
+ "span probe must not walk the created_at index or the table:\n{plan}"
+ );
+
+ let mut spans = Vec::new();
+ for &(ws, proj) in scopes.iter().chain([(empty_ws, empty_proj)].iter()) {
+ let legacy: (Option<i64>, Option<i64>) = conn
+ .query_row(
+ "SELECT MIN(rowid), MAX(rowid) FROM observations \
+ WHERE workspace_id = ?1 AND project_id = ?2",
+ rusqlite::params![ws.as_bytes(), proj.as_bytes()],
+ |row| Ok((row.get(0)?, row.get(1)?)),
+ )
+ .unwrap();
+ let probe: (Option<i64>, Option<i64>) = conn
+ .query_row(
+ crate::reader::OBSERVATION_ROWID_SPAN_SQL,
+ [proj.as_bytes()],
+ |row| Ok((row.get(0)?, row.get(1)?)),
+ )
+ .unwrap();
+ assert_eq!(probe, legacy);
+ spans.push(probe);
+ }
+ assert!(
+ spans[..3]
+ .iter()
+ .all(|(lo, hi)| lo.is_some() && hi.is_some())
+ );
+ assert_ne!(
+ spans[0], spans[1],
+ "fixture projects must have distinct spans"
+ );
+ assert_eq!(
+ spans[3],
+ (None, None),
+ "a project with no observations has no span"
+ );
+}