Solution for long term memory for agent coding CLIs and to facilitate handoff between different agent vendors
Rust
fix(profile): a reversed ruling wins over a merge that calls it unchanged
AkitaOnRailse518a0f02e61
diff --git a/crates/ai-memory-consolidate/src/profile.rs b/crates/ai-memory-consolidate/src/profile.rsindex bf2b8192..0c317497 100644--- a/crates/ai-memory-consolidate/src/profile.rs+++ b/crates/ai-memory-consolidate/src/profile.rs@@ -705,6 +705,22 @@ fn same_ruling(a: &str, b: &str) -> bool {sequence.iter().any(|w| w != "not") && sequence == ruling_sequence(b)}+/// Whether `b` reverses `a`: the same topic words with the negation flipped+/// ("Always use pnpm" / "Never use pnpm"). The LLM merge may not keep the old+/// text over such a reversal: the latest ruling wins.+fn negation_flipped(a: &str, b: &str) -> bool {+ let (a, b) = (ruling_sequence(a), ruling_sequence(b));+ let topic = |s: &[String]| {+ let mut words: Vec<String> = s.iter().filter(|w| *w != "not").cloned().collect();+ words.sort();+ words.dedup();+ words+ };+ let negated = |s: &[String]| s.iter().filter(|w| *w == "not").count() % 2 == 1;+ let topic_a = topic(&a);+ !topic_a.is_empty() && topic_a == topic(&b) && negated(&a) != negated(&b)+}+/// The statement, reasoning and scope a managed entry page currently shows.struct StoredText {statement: String,@@ -2087,7 +2103,12 @@ async fn converge_target(*merge_budget -= 1;let current = stored.as_ref().map(|stored| stored.statement.as_str());match merge_entry(llm.as_ref(), wiki.sanitizer(), current, &entry).await {- Ok(merged) if !merged.changed && stored.is_some() => {+ Ok(merged)+ if !merged.changed+ && stored.as_ref().is_some_and(|stored| {+ !negation_flipped(&stored.statement, &entry.statement)+ }) =>+ {// The evidence corroborates the entry: it stands as written.report.llm_calls += 1;if let Some(stored) = stored {@@ -2096,6 +2117,11 @@ async fn converge_target(entry.applies_to = stored.applies_to;}}+ Ok(merged) if !merged.changed => {+ // A reversal the model called unchanged: keep the newest+ // ruling as harvested rather than the stored text.+ report.llm_calls += 1;+ }Ok(merged) => {report.llm_calls += 1;let statement = clean_line(&merged.statement, STATEMENT_MAX_BYTES);@@ -2651,6 +2677,15 @@ mod tests {assert!(MERGE_SYSTEM_PROMPT.contains("user's own words"));}+ #[test]+ fn a_reversal_is_a_negation_flip_and_a_rewording_is_not() {+ assert!(negation_flipped("Always use pnpm", "Never use pnpm"));+ assert!(negation_flipped("Don't use npm", "Use npm"));+ assert!(!negation_flipped("Always use pnpm", "Use pnpm always"));+ assert!(!negation_flipped("Never use npm", "Never use yarn"));+ assert!(!negation_flipped("never", "always"));+ }+#[test]fn a_ruling_keeps_its_order_and_its_negations() {assert!(same_ruling("Use pnpm.", "I always use pnpm!"));diff --git a/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs b/crates/ai-memory-consolidate/tests/suite/profile_harvest.rsindex 797493bf..a7565ac3 100644--- a/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs+++ b/crates/ai-memory-consolidate/tests/suite/profile_harvest.rs@@ -508,6 +508,48 @@ async fn an_unchanged_merge_keeps_the_entry_as_written() {assert!(!body.contains("yarn"), "{body}");}+/// `changed: false` cannot freeze a reversed ruling: when the newest+/// statement flips the stored one's negation, the latest ruling wins even if+/// the model calls it unchanged.+#[tokio::test]+async fn an_unchanged_merge_cannot_freeze_a_reversal() {+ let fx = fixture().await;+ let alpha = project(&fx, "alpha").await;+ prompt(&fx, alpha, "Always use pnpm in every project.", 1).await;+ let first = FakeProfileLlm {+ merge_statement: "Always use pnpm in every project.".into(),+ classify_statement: "Always use pnpm in every project.".into(),+ ..FakeProfileLlm::default()+ };+ pass_with(&fx, &single_user(), Arc::new(first)).await;+ assert_eq!(+ profile_pages(&fx, "_global")[0].1,+ "Always use pnpm in every project."+ );++ let beta = project(&fx, "beta").await;+ prompt(&fx, beta, "Never use pnpm in every project.", 2).await;+ let stale = FakeProfileLlm {+ merge_statement: "Always use pnpm in every project.".into(),+ classify_statement: "Never use pnpm in every project.".into(),+ merge_unchanged: true,+ ..FakeProfileLlm::default()+ };+ pass_with(&fx, &single_user(), Arc::new(stale)).await;+ let statements: Vec<String> = profile_pages(&fx, "_global")+ .into_iter()+ .map(|(_, statement, _)| statement)+ .collect();+ assert!(+ statements.iter().any(|s| s.starts_with("Never use pnpm")),+ "the reversal must win: {statements:?}"+ );+ assert!(+ !statements.iter().any(|s| s.starts_with("Always use pnpm")),+ "the old ruling must not stay current: {statements:?}"+ );+}+/// A page the user edited by hand is never rewritten by the harvester, even/// when new evidence arrives for its topic.#[tokio::test]@@ -792,6 +834,8 @@ async fn a_personal_profile_admits_one_operators_habit() {#[derive(Clone, Default)]struct FakeProfileLlm {merge_statement: String,+ /// The classifier's normalized statement; a fixed pnpm rule when empty.+ classify_statement: String,/// Report `changed: false` from the merge: the evidence only corroborates.merge_unchanged: bool,fail: bool,@@ -835,7 +879,11 @@ impl LlmProvider for FakeProfileLlm {"keep": true,"generality": "general","category": "tools",- "statement": "Use pnpm for JavaScript dependencies.",+ "statement": if self.classify_statement.is_empty() {+ "Use pnpm for JavaScript dependencies."+ } else {+ self.classify_statement.as_str()+ },"applies_to": ["javascript"],"confidence": 0.9,})diff --git a/docs/design-cross-project-profile.md b/docs/design-cross-project-profile.mdindex d8647ade..2af1c056 100644--- a/docs/design-cross-project-profile.md+++ b/docs/design-cross-project-profile.md@@ -338,4 +338,3 @@ left to 2.7.Lower priority, considered and not scheduled: a byte ruler with retry insteadof truncating long statements (truncation only affects the rare entry over 200bytes).-