Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(query): preserve corrections in answer admission
AkitaOnRailsfce03b1cd8ba
diff --git a/crates/ai-memory-mcp/src/server.rs b/crates/ai-memory-mcp/src/server.rsindex 10708e50..b41a86e1 100644--- a/crates/ai-memory-mcp/src/server.rs+++ b/crates/ai-memory-mcp/src/server.rs@@ -7848,11 +7848,26 @@ fn answer_context_notice(admitted: usize, omitted: usize, clipped: usize) -> Str)}-fn answer_evidence_newer(a: &QueryHit, b: &QueryHit) -> bool {+struct AnswerEvidenceOrder<'a> {+ hits: Vec<&'a QueryHit>,+ explicit_newer: HashSet<(PageId, PageId)>,+}++impl AnswerEvidenceOrder<'_> {+ fn is_newer(&self, a: &QueryHit, b: &QueryHit) -> bool {+ answer_evidence_newer(a, b, &self.explicit_newer)+ }+}++fn answer_evidence_newer(+ a: &QueryHit,+ b: &QueryHit,+ explicit_newer: &HashSet<(PageId, PageId)>,+) -> bool {match (&a.evidence, &b.evidence) {(Some(x), Some(y)) if x.workspace_id == y.workspace_id && x.project_id == y.project_id => {- x.supersedes == Some(b.hit.id)- || (y.supersedes != Some(a.hit.id)+ explicit_newer.contains(&(a.hit.id, b.hit.id))+ || (!explicit_newer.contains(&(b.hit.id, a.hit.id))&& !x.title.trim().is_empty()&& x.title.trim().to_lowercase() == y.title.trim().to_lowercase()&& (!a.hit.superseded || b.hit.superseded)@@ -7862,16 +7877,52 @@ fn answer_evidence_newer(a: &QueryHit, b: &QueryHit) -> bool {}}-fn answer_evidence_order<'a>(hits: &'a [QueryHit], global: &'a [QueryHit]) -> Vec<&'a QueryHit> {+fn answer_evidence_order<'a>(+ hits: &'a [QueryHit],+ global: &'a [QueryHit],+) -> AnswerEvidenceOrder<'a> {let pending: Vec<_> = hits.iter().chain(global).collect();-let count = pending.len();+ let mut explicit_edges = vec![vec![false; count]; count];+ for (new_index, new) in pending.iter().enumerate() {+ let Some(new_meta) = &new.evidence else {+ continue;+ };+ for (old_index, old) in pending.iter().enumerate() {+ let Some(old_meta) = &old.evidence else {+ continue;+ };+ explicit_edges[new_index][old_index] = new_meta.workspace_id == old_meta.workspace_id+ && new_meta.project_id == old_meta.project_id+ && new_meta.supersedes == Some(old.hit.id);+ }+ }+ // Close explicit lineage before adding timestamp heuristics. Otherwise an+ // imported timestamp on an ancestor can introduce a cycle across a chain.+ for via in 0..count {+ let ancestors = explicit_edges[via].clone();+ for row in &mut explicit_edges {+ if row[via] {+ for (is_newer, &is_ancestor) in row.iter_mut().zip(&ancestors) {+ *is_newer |= is_ancestor;+ }+ }+ }+ }+ let mut explicit_newer = HashSet::new();+ for (new, row) in explicit_edges.iter().enumerate() {+ for (old, &is_newer) in row.iter().enumerate() {+ if is_newer {+ explicit_newer.insert((pending[new].hit.id, pending[old].hit.id));+ }+ }+ }let edges: Vec<Vec<bool>> = pending.iter().map(|new| {pending.iter()- .map(|old| answer_evidence_newer(new, old))+ .map(|old| answer_evidence_newer(new, old, &explicit_newer)).collect()}).collect();@@ -7894,7 +7945,10 @@ fn answer_evidence_order<'a>(hits: &'a [QueryHit], global: &'a [QueryHit]) -> Ve}ordered.push(pending[index]);}- ordered+ AnswerEvidenceOrder {+ hits: ordered,+ explicit_newer,+ }}fn answer_snippet_prefix(hit: &QueryHit, number: usize) -> String {@@ -7907,36 +7961,63 @@ fn answer_snippet_prefix(hit: &QueryHit, number: usize) -> String {)}+fn answer_period_is_abbreviation(snippet: &str, period: usize) -> bool {+ let token = snippet[..period]+ .rsplit_once(char::is_whitespace)+ .map_or(&snippet[..period], |(_, token)| token)+ .trim_matches(|c: char| !c.is_alphanumeric() && c != '.');+ let normalized = token.to_ascii_lowercase();+ token.contains('.')+ || matches!(+ normalized.as_str(),+ "mr" | "mrs" | "ms" | "dr" | "prof" | "sr" | "jr" | "vs" | "etc"+ )+}+// A complete trailing sentence can preserve a correction lost by prefix clipping.-// This bounded syntactic rule does not infer meaning from stored prose.+// The scanner is deliberately conservative around quotations and abbreviations.fn answer_sentence_suffix(snippet: &str, available: usize) -> Option<String> {let code = ["//", "fn ", "const ", "```"].iter().any(|prefix| snippet.trim_start().starts_with(prefix));- let terminator = |i: usize, c: char| {- matches!(c, '。' | '!' | '?')- || ((matches!(c, '.' | '!' | '?') || (code && matches!(c, ';' | '}')))- && snippet[i + c.len_utf8()..]- .chars()- .next()- .is_none_or(char::is_whitespace))- };- let end = snippet- .char_indices()- .rev()- .find(|(i, c)| terminator(*i, *c))- .map(|(i, c)| i + c.len_utf8())?;+ let mut boundaries = Vec::new();+ let mut double_quoted = false;+ let mut single_quoted = false;+ let mut escaped = false;+ for (i, c) in snippet.char_indices() {+ let next = snippet[i + c.len_utf8()..].chars().next();+ let previous = snippet[..i].chars().next_back();+ let apostrophe_within_word =+ previous.is_some_and(char::is_alphanumeric) && next.is_some_and(char::is_alphanumeric);+ if !escaped && c == '"' && !single_quoted {+ double_quoted = !double_quoted;+ } else if c == '\'' && !escaped && !double_quoted && !apostrophe_within_word {+ single_quoted = !single_quoted;+ } else if !double_quoted && !single_quoted {+ let separated = next.is_none_or(char::is_whitespace);+ let sentence =+ matches!(c, '。' | '!' | '?') || (matches!(c, '.' | '!' | '?') && separated);+ let statement = code && matches!(c, ';' | '}') && separated;+ let next_word = snippet[i + c.len_utf8()..].trim_start().chars().next();+ let unsafe_period = c == '.'+ && (answer_period_is_abbreviation(snippet, i)+ || next_word.is_some_and(char::is_lowercase));+ if (sentence || statement) && !unsafe_period {+ boundaries.push(i + c.len_utf8());+ }+ }+ escaped = c == '\\' && !escaped;+ if c != '\\' {+ escaped = false;+ }+ }+ let &end = boundaries.last()?;// A partial trailing qualification may reverse an earlier sentence.// Omit rather than silently discard that qualification.if !snippet[end..].trim().is_empty() {return None;}- let starts = std::iter::once(0).chain(- snippet[..end]- .char_indices()- .filter(|(i, c)| terminator(*i, *c))- .map(|(i, c)| i + c.len_utf8()),- );+ let starts = std::iter::once(0).chain(boundaries.into_iter().filter(|&i| i < end));for start in starts {let text = snippet[start..end].trim();let head_marker = if start == 0 { "" } else { "… " };@@ -7999,17 +8080,18 @@ fn prepare_answer_request<'a>(let mut sources = AnswerSources::new();let mut admitted = 0;let mut clipped = 0;- let ordered = answer_evidence_order(hits, global_scope_hits);+ let order = answer_evidence_order(hits, global_scope_hits);let mut omitted: Vec<&QueryHit> = Vec::new();let mut unadmitted: Vec<&QueryHit> = Vec::new();- for hit in ordered {+ for hit in order.hits.iter().copied() {// An unadmitted correction must not be replaced by its obsolete predecessor.- if unadmitted.iter().any(|new| answer_evidence_newer(new, hit)) {+ if unadmitted.iter().any(|new| order.is_newer(new, hit)) {unadmitted.push(hit);continue;}let snippet = ai_memory_store::plain_snippet(&hit.hit.snippet);if !snippet.chars().any(char::is_alphanumeric) {+ unadmitted.push(hit);continue;}let prefix = answer_snippet_prefix(hit, admitted + 1);@@ -9031,7 +9113,7 @@ mod tests {let before = serde_json::to_value(&hits).unwrap();let order = answer_evidence_order(&hits, &[]);assert_eq!(- order.iter().map(|hit| hit.hit.id).collect::<Vec<_>>(),+ order.hits.iter().map(|hit| hit.hit.id).collect::<Vec<_>>(),vec![hits[1].hit.id, hits[0].hit.id]);assert_eq!(serde_json::to_value(&hits).unwrap(), before);@@ -9051,7 +9133,10 @@ mod tests {// determines the ordering even if an imported timestamp is older.replacement.updated_at = 0;replacement.supersedes = Some(previous);- assert_eq!(answer_evidence_order(&hits, &[])[0].hit.id, hits[1].hit.id);+ assert_eq!(+ answer_evidence_order(&hits, &[]).hits[0].hit.id,+ hits[1].hit.id+ );}#[test]@@ -9065,11 +9150,11 @@ mod tests {let replacement = hits[1].evidence.as_mut().unwrap();replacement.updated_at = 0;replacement.supersedes = Some(previous);- assert!(answer_evidence_newer(&hits[1], &hits[0]));- assert!(!answer_evidence_newer(&hits[0], &hits[1]));let order = answer_evidence_order(&hits, &[]);- assert_eq!(order[0].hit.id, hits[1].hit.id);- assert_eq!(order[1].hit.id, hits[0].hit.id);+ assert!(order.is_newer(&hits[1], &hits[0]));+ assert!(!order.is_newer(&hits[0], &hits[1]));+ assert_eq!(order.hits[0].hit.id, hits[1].hit.id);+ assert_eq!(order.hits[1].hit.id, hits[0].hit.id);}#[test]@@ -9089,7 +9174,7 @@ mod tests {}let order = answer_evidence_order(&hits[..1], &hits[1..]);assert_eq!(- order.iter().map(|hit| hit.hit.id).collect::<Vec<_>>(),+ order.hits.iter().map(|hit| hit.hit.id).collect::<Vec<_>>(),vec![hits[0].hit.id, hits[1].hit.id],"{variation}");@@ -9154,7 +9239,7 @@ mod tests {hits[2].evidence.as_mut().unwrap().supersedes = Some(hits[1].hit.id);let order = answer_evidence_order(&hits, &[]);assert_eq!(- order.iter().map(|h| h.hit.id).collect::<Vec<_>>(),+ order.hits.iter().map(|h| h.hit.id).collect::<Vec<_>>(),vec![hits[2].hit.id, hits[1].hit.id, hits[0].hit.id]);let full = prepare_answer_request(@@ -9172,6 +9257,84 @@ mod tests {assert!(plan.sources.is_empty());}+ #[test]+ fn answer_budget_transitive_supersession_overrides_ancestor_timestamp() {+ let mut hits = answer_budget_hits(&[+ "Obsolete choice A.",+ "Obsolete choice B.",+ "Obsolete choice C.",+ &"Current choice D that cannot fit whole. ".repeat(100),+ ]);+ answer_test_metadata(&mut hits);+ for hit in &mut hits[..3] {+ hit.hit.superseded = true;+ }+ hits[0].evidence.as_mut().unwrap().updated_at = 1_000;+ for index in 1..hits.len() {+ hits[index].evidence.as_mut().unwrap().supersedes = Some(hits[index - 1].hit.id);+ }+ let order = answer_evidence_order(&hits, &[]);+ assert_eq!(+ order.hits.iter().map(|hit| hit.hit.id).collect::<Vec<_>>(),+ vec![+ hits[3].hit.id,+ hits[2].hit.id,+ hits[1].hit.id,+ hits[0].hit.id+ ]+ );+ assert!(order.is_newer(&hits[3], &hits[0]));++ let old_only = prepare_answer_request(+ "question",+ &hits[..1],+ &[],+ ReasoningTier::Minimal,+ Some(100_000),+ );+ let context = old_only.context.unwrap();+ let limit = context.estimated_input_tokens + context.reserved_output_tokens as usize;+ let plan =+ prepare_answer_request("question", &hits, &[], ReasoningTier::Minimal, Some(limit));+ assert!(plan.request.is_none());+ assert!(plan.sources.is_empty());+ }++ #[test]+ fn answer_budget_unusable_newer_hit_suppresses_older_decision() {+ let mut hits = answer_budget_hits(&["Use the obsolete database.", "..."]);+ answer_test_metadata(&mut hits);+ let plan = prepare_answer_request(+ "question",+ &hits,+ &[],+ ReasoningTier::Minimal,+ Some(100_000),+ );+ assert!(plan.request.is_none());+ assert!(plan.sources.is_empty());+ let context = plan.context.unwrap();+ assert_eq!(+ (context.admitted, context.omitted, context.clipped),+ (0, 2, 0)+ );+ }++ #[test]+ fn answer_budget_refuses_ambiguous_abbreviation_and_quoted_code_clips() {+ assert_eq!(+ answer_sentence_suffix("Do not deploy to e.g. production.", 15),+ None+ );+ assert_eq!(+ answer_sentence_suffix(+ "const POLICY = \"Never disable TLS; even for local testing\";",+ 32+ ),+ None+ );+ }+#[test]fn answer_budget_refuses_unsplittable_or_unterminated_fragments() {for snippet in [diff --git a/docs/benchmarks/query-answer-context-draft.md b/docs/benchmarks/query-answer-context-draft.mdindex c60d7254..12004b2c 100644--- a/docs/benchmarks/query-answer-context-draft.md+++ b/docs/benchmarks/query-answer-context-draft.md@@ -1,8 +1,9 @@# Query-answer context admission: draft local measurements-This receipt accompanies the default-disabled proposal in #1684. Its configuration,-response contract was accepted by the maintainer in the review on #1689. It measures-builder size and local execution time, not answer correctness or provider usage.+This receipt records measurements from the default-disabled proposal in #1684.+The maintainer accepted its configuration and response contract in the review on+#1689. It measures builder size and local execution time, not answer correctness+or provider usage.## Provenance@@ -52,11 +53,10 @@ The one-hit ASCII case added local work: p50 rose from 5.317 µs to 49.540 µs,including schema sizing. Larger cases did less rendering because they omittedsources. No overhead acceptance threshold has been agreed.-The CJK case admits only 15 of 100 sources at this conservative budget. Clipping-can remove a correction or qualification. Deterministic tests check that supplied-corrections remain intact when they fit and that superseded status is labelled;-answer accuracy under pruning remains unmeasured and blocks a ready-for-review-claim. Retrieval hits and ranking remain unchanged.+The CJK case admits only 15 of 100 sources at this conservative budget. These+measurements predate the reviewed admission policy below, which added stricter+correction ordering and clipping guards. Answer accuracy under pruning remains+unmeasured. Retrieval hits and ranking remain unchanged.Full measurements: [CSV](query-answer-context-draft.csv).@@ -89,12 +89,11 @@ fixtures mark the admitted historical source `superseded: true`; that labeldoes not recover an omitted correction. Lines prefixed `C2RETENTION,` reportthe asserted outcomes when run with `--nocapture`.-This is a literal-retention regression, not model answer-quality evaluation.-The two loss cases deliberately pass only when they expose the known loss;-they do not establish acceptable pruning quality. Clipping and ranked-prefix-omission can remove the current fact while leaving historical evidence available-for synthesis. That unresolved policy risk and maintainer contract agreement-still block readiness. No prompt-only abstention guarantee is claimed.+This was a literal-retention regression, not a model answer-quality evaluation.+The two loss cases passed only when they exposed the known loss; they did not+establish acceptable pruning quality. The reviewed policy below replaced those+expectations with current-fact retention checks. No prompt-only abstention+guarantee is claimed.```shcargo test -p ai-memory-mcp --lib answer_budget_labeled_evidence_retention_boundaries -- --nocapture@@ -124,14 +123,18 @@ allowance instead. After whole-snippet selection, unused space can hold onecomplete trailing sentence or statement from an omitted snippet. Partialsentences and unsplittable text are omitted. Unicode punctuation and codestatement boundaries are supported; this is a syntactic boundary rule, not a-language parser. Wrapped prose newlines and semicolons do not permit clipping. Ellipses and the receipt make omissions visible.+language parser. Wrapped prose newlines and semicolons do not permit clipping.+Ambiguous boundaries inside abbreviations or quoted code are omitted. Ellipses+and the receipt make omissions visible.The original sixteen literal-retention probes now require all corrections to-remain present. Additional tests cover same-topic session chronology, lineage-over imported timestamps, independent topics/scopes, whole-snippet priority-and safe refusal when no complete sentence fits. These probes do not measure-model answer accuracy or prove that arbitrary omitted evidence is irrelevant.-The earlier timings and 8/16 result above remain historical measurements.+remain present. Additional tests cover same-topic session chronology, transitive+lineage over imported timestamps, unusable newer evidence, independent topics+and scopes, whole-snippet priority, ambiguous abbreviation and quoted-code+boundaries, and safe refusal when no complete sentence fits. These probes do not+measure model answer accuracy or prove that arbitrary omitted evidence is+irrelevant. The earlier timings and 8/16 result above remain historical+measurements.A snippet with an incomplete trailing qualification is omitted rather thanclipped back to an earlier complete sentence; that tail might reverse the rule.diff --git a/docs/security-boundaries.md b/docs/security-boundaries.mdindex 5ebe363e..46be9fa6 100644--- a/docs/security-boundaries.md+++ b/docs/security-boundaries.md@@ -235,5 +235,7 @@ without loading bodies or expanding supersession links. The store tests`answer_evidence_metadata_returns_only_requested_ids_without_expanding_links`and the session chronology test cover requested-only and same-scope joins.`answer_budget_uses_store_recency_without_reordering_retrieval_hits` verifies-the production metadata-to-prompt path; existing foreign-project citation and-shared-path ambiguity tests continue to enforce the authorization boundary.+the production metadata-to-prompt path. The transitive-lineage and unusable-newer-hit+unit tests keep obsolete evidence out when the current evidence cannot be admitted;+existing foreign-project citation and shared-path ambiguity tests continue to+enforce the authorization boundary.