The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(memory_summarizer): strip stale recall sections before compaction
Steven Enamakel2067129552d8
diff --git a/crates/openhuman-core/src/agent/tinyagents/memory_summarizer.rs b/crates/openhuman-core/src/agent/tinyagents/memory_summarizer.rsindex 828a7e9fdc..33608ed2bb 100644--- a/crates/openhuman-core/src/agent/tinyagents/memory_summarizer.rs+++ b/crates/openhuman-core/src/agent/tinyagents/memory_summarizer.rs@@ -58,6 +58,9 @@ impl MemoryRecallSummarizer {);let (record, pack) = futures::join!(summary, recall);let mut record = record?;+ // A later compaction folds the previous checkpoint, recall included;+ // keep one recall section, the current one.+ strip_recalled(&mut record.summary);if let Some(pack) = pack {append(&mut record.summary,@@ -107,6 +110,34 @@ pub(crate) fn dropped_turns(messages: &[Message]) -> Vec<Turn> {.collect()}+/// Drops a "Recalled from memory" section a summary carried over from an+/// earlier checkpoint: the heading and everything after it.+fn strip_recalled(summary: &mut Message) {+ let blocks = match summary {+ Message::User(message) => &mut message.content,+ Message::System(message) => &mut message.content,+ Message::Assistant(message) => &mut message.content,+ Message::Tool(message) => &mut message.content,+ Message::Custom(_) => return,+ };+ let Some(at) = blocks.iter().position(+ |block| matches!(block, ContentBlock::Text(text) if text.contains(RECALLED_HEADING)),+ ) else {+ return;+ };+ blocks.truncate(at + 1);+ if let Some(ContentBlock::Text(text)) = blocks.last_mut() {+ if let Some(cut) = text.find(RECALLED_HEADING) {+ text.truncate(cut);+ let kept = text.trim_end().len();+ text.truncate(kept);+ }+ }+ if matches!(blocks.last(), Some(ContentBlock::Text(text)) if text.is_empty()) {+ blocks.pop();+ }+}+/// Appends `text` to the summary message, whatever its role.fn append(summary: &mut Message, text: &str) {let block = ContentBlock::Text(text.to_string());diff --git a/crates/openhuman-core/src/agent/tinyagents/memory_summarizer_tests.rs b/crates/openhuman-core/src/agent/tinyagents/memory_summarizer_tests.rsindex 62a8c5a397..c30b6a56fb 100644--- a/crates/openhuman-core/src/agent/tinyagents/memory_summarizer_tests.rs+++ b/crates/openhuman-core/src/agent/tinyagents/memory_summarizer_tests.rs@@ -110,3 +110,28 @@ async fn without_recall_the_summarizer_is_unchanged_and_its_failure_is_its_own()let failing = MemoryRecallSummarizer::wrap(Box::new(Failing), Some(turn));assert!(failing.summarize(&dropped()).await.is_err());}++#[test]+fn an_earlier_recall_section_is_dropped_before_the_new_one() {+ let mut summary = Message::user(format!(+ "the summary so far\n\n{RECALLED_HEADING}\n\n- an old recall"+ ));+ strip_recalled(&mut summary);+ assert_eq!(summary.text(), "the summary so far");++ let mut untouched = Message::user("nothing recalled here");+ strip_recalled(&mut untouched);+ assert_eq!(untouched.text(), "nothing recalled here");+}++#[tokio::test]+async fn a_second_compaction_keeps_one_recall_section() {+ let tmp = tempfile::tempdir().unwrap();+ let turn = bound_turn(&tmp).await;+ let summarizer = MemoryRecallSummarizer::wrap(Box::new(ConcatSummarizer), Some(turn));+ let first = summarizer.summarize(&dropped()).await.unwrap();+ let mut again = dropped();+ again.insert(0, first.summary.clone());+ let second = summarizer.summarize(&again).await.unwrap();+ assert_eq!(second.summary.text().matches(RECALLED_HEADING).count(), 1);+}