Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
feat(retrieval): let the observation stream alone rank a session page it voted for
AkitaOnRails41793120680e
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 606a9132..28090c51 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -91,7 +91,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0flag on, project and `scopes` searches also run full-text search overobservations in the same project, and each matching session votes once,through its best-matching observation, for its own latest- `sessions/<id>.md` page as one more RRF stream. When page search did not+ `sessions/<id>.md` page as one more RRF stream. A page the stream voted for+ takes its lexical evidence from the stream alone: page FTS over its+ title-only body still reports a rank but adds nothing, so a session that+ only shares topic words in its titles cannot outvote the session holding+ the answer. When page search did notmatch the page itself, the matching excerpt becomes the hit's snippet, and`explain=true` reports an `observation` stream with the matched observationids. Hits are still pages and nothing is written; sessions without a pagediff --git a/crates/ai-memory-store/src/reader.rs b/crates/ai-memory-store/src/reader.rsindex 5d338d56..57258d04 100644--- a/crates/ai-memory-store/src/reader.rs+++ b/crates/ai-memory-store/src/reader.rs@@ -707,7 +707,9 @@ pub struct GraphVia {/// Per-stream RRF contributions (`1/(k+rank)`, k=60) for one hit.#[derive(Debug, Clone, Default, Serialize)]pub struct RrfContributions {- /// FTS5 stream contribution (0.0 when the hit missed that stream).+ /// FTS5 stream contribution (0.0 when the hit missed that stream, and+ /// for a session page the observation stream voted for, whose lexical+ /// evidence comes from that stream instead).pub fts: f64,/// Vector stream contribution.pub vector: f64,@@ -6301,8 +6303,21 @@ impl ReaderPool {}let mut fused: std::collections::HashMap<PageId, Fused> = std::collections::HashMap::new();+ // A session page's indexed body is only the titles of its own+ // observations, and the observation stream already ranks that+ // session by the full observation text. Counting page FTS as well+ // would let a session that merely shares topic words in its titles+ // outvote the session holding the answer, so a page the stream voted+ // for takes its lexical evidence from the stream alone. Its FTS rank+ // and snippet are kept; only the RRF contribution is dropped.+ let observation_voted: std::collections::HashSet<PageId> =+ observation_votes.iter().map(|vote| vote.page_id).collect();for (rank, h) in fts_hits.iter().enumerate() {- let contrib = 1.0 / (k + (rank + 1) as f64);+ let contrib = if observation_voted.contains(&h.id) {+ 0.0+ } else {+ 1.0 / (k + (rank + 1) as f64)+ };let entry = fused.entry(h.id).or_insert_with(|| Fused {path: h.path.clone(),title: h.title.clone(),diff --git a/crates/ai-memory-store/tests/suite/retrieval_observation_stream.rs b/crates/ai-memory-store/tests/suite/retrieval_observation_stream.rsindex 8f75b330..1b3d8c69 100644--- a/crates/ai-memory-store/tests/suite/retrieval_observation_stream.rs+++ b/crates/ai-memory-store/tests/suite/retrieval_observation_stream.rs@@ -458,3 +458,90 @@ async fn a_page_fts_matched_keeps_its_own_snippet() {assert!(hit.snippet.contains("colony"), "{}", hit.snippet);assert!(!hit.snippet.contains("assistant"), "{}", hit.snippet);}++/// A session page's title-only body shares topic words with many sessions;+/// the answer sits in one session's observation text. Page FTS must not let a+/// topic-word session outvote the session the observations rank first: a page+/// the stream voted for takes its lexical evidence from the stream alone.+#[tokio::test]+async fn a_topic_word_session_page_does_not_outvote_the_evidence_session() {+ let tmp = tempfile::tempdir().unwrap();+ let mut store = Store::open(tmp.path()).unwrap();+ store.reader.set_retrieval_tuning(stream_on());+ let (ws, proj) = scope(&store, "default", "app").await;+ let evidence = session(&store, ws, proj).await;+ let topical = session(&store, ws, proj).await;+ session_page(+ &store,+ ws,+ proj,+ evidence,+ "User asked about weekend plans.",+ )+ .await;+ session_page(&store, ws, proj, topical, "Planning the telescope trip.").await;+ observe(+ &store,+ ws,+ proj,+ evidence,+ "I finally bought the quasar telescope from the camera shop downtown",+ )+ .await;+ observe(+ &store,+ ws,+ proj,+ topical,+ "which telescope should I pack for the trip",+ )+ .await;++ let hits = search(&store, ws, proj, "quasar telescope").await;+ let evidence_path = format!("sessions/{evidence}.md");+ let topical_path = format!("sessions/{topical}.md");+ assert_eq!(paths(&hits), vec![evidence_path, topical_path.clone()]);++ let (_, topical_details) = hits+ .iter()+ .find(|(h, _)| h.path.as_str() == topical_path)+ .unwrap();+ // The page FTS match is still reported, but contributes nothing.+ assert_eq!(topical_details.fts_rank, Some(1));+ assert_eq!(topical_details.observation_rank, Some(2));+ assert!(topical_details.rrf.fts.abs() < 1e-12);+ assert!((topical_details.fused - topical_details.rrf.observation).abs() < 1e-12);+}++/// Control: a page the stream cannot vote for (anything but a session page)+/// keeps its page FTS contribution, and still outranks a session page the+/// stream ranked below it.+#[tokio::test]+async fn a_curated_page_keeps_its_page_fts_contribution() {+ let tmp = tempfile::tempdir().unwrap();+ let mut store = Store::open(tmp.path()).unwrap();+ store.reader.set_retrieval_tuning(stream_on());+ let (ws, proj) = scope(&store, "default", "app").await;+ let sid = session(&store, ws, proj).await;+ session_page(&store, ws, proj, sid, "User asked about weekend plans.").await;+ store+ .writer+ .upsert_page(page(+ ws,+ proj,+ "decisions/telescope.md",+ "We chose the quasar telescope for the observatory.",+ ))+ .await+ .unwrap();+ observe(&store, ws, proj, sid, "the quasar telescope arrived").await;++ let hits = search(&store, ws, proj, "quasar telescope").await;+ let (_, decision) = hits+ .iter()+ .find(|(h, _)| h.path.as_str() == "decisions/telescope.md")+ .unwrap();+ assert_eq!(decision.fts_rank, Some(1));+ assert!((decision.rrf.fts - 1.0 / (K + 1.0)).abs() < 1e-12);+ assert!(decision.rrf.observation.abs() < 1e-12);+}