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(archivist): fold hosted segment recaps with the host's model
Shanu509b7b9e7185
diff --git a/crates/openhuman-core/src/agent/harness/archivist/recap.rs b/crates/openhuman-core/src/agent/harness/archivist/recap.rsindex 5f6b245b7c..6c82991fe7 100644--- a/crates/openhuman-core/src/agent/harness/archivist/recap.rs+++ b/crates/openhuman-core/src/agent/harness/archivist/recap.rs@@ -92,6 +92,78 @@ async fn fold_through_driver(tree.summarise(inputs, context).await}+/// The role a host-side fold builds its model for — the one+/// `lifecycle::with_config` probes when it decides `summariser_available`.+const HOST_RECAP_ROLE: &str = "summarization";++/// What a host-side fold asks of its model.+const HOST_RECAP_PROMPT: &str = "\+You write the recap of one stretch of a conversation between a user and their assistant. \+Write two to four sentences, in the language of the conversation, saying what it was about, \+what was decided or done, and what was left open. Plain prose: no headings, no lists. \+Use only what the transcript says.";++/// The transcript a host-side fold reads: one `role: text` paragraph per turn,+/// oldest first, keeping the newest turns that fit in `max_chars`.+pub(super) fn transcript(entries: &[&EpisodicTurn], max_chars: usize) -> String {+ let mut kept = Vec::new();+ let mut used = 0usize;+ for entry in entries.iter().rev() {+ let text = entry.content.trim();+ if text.is_empty() {+ continue;+ }+ let paragraph = format!("{}: {text}", entry.role);+ used = used.saturating_add(paragraph.len() + 2);+ if used > max_chars && !kept.is_empty() {+ break;+ }+ kept.push(paragraph);+ }+ kept.reverse();+ kept.join("\n\n")+}++/// Folds a segment's turns with the host's own summarisation model.+///+/// For a driver with no model to fold with: hosted memory answers `summarise`+/// with `Unsupported`, and a recap that never comes leaves every hosted segment+/// closed and unsummarised, so nothing that reads segment summaries ever sees+/// one. The host already holds a model for this role — the probe that set+/// `summariser_available` built it — so it folds here instead, over the same+/// turns and within the same input budget. The prose goes where the+/// conversation itself already went: the host's chat provider.+pub(super) async fn fold_with_host_model(+ config: &crate::config::Config,+ entries: &[&EpisodicTurn],+) -> anyhow::Result<String> {+ use tinyinference_llm::message::Message;+ use tinyinference_llm::model::ModelRequest;++ let max_chars = (INPUT_TOKEN_BUDGET - SUMMARY_OVERHEAD_RESERVE_TOKENS) as usize * 4;+ let transcript = transcript(entries, max_chars);+ if transcript.is_empty() {+ return Ok(String::new());+ }+ let (model, model_id) =+ crate::inference::provider::create_chat_model_with_model_id(HOST_RECAP_ROLE, config, 0.3)?;+ tracing::debug!(+ "[archivist] host recap fold model={model_id} chars={}",+ transcript.len()+ );+ let recap = model+ .invoke(+ &(),+ ModelRequest::new(vec![+ Message::system(HOST_RECAP_PROMPT),+ Message::user(transcript),+ ]),+ )+ .await?+ .text();+ Ok(recap.trim().to_string())+}+/// An episodic entry paired with the stable identity exposed by its backing/// store. The md archivist uses a per-session sequence while the legacy FTS5/// store uses a row id.@@ -287,9 +359,8 @@ impl ArchivistHook {if let Some(ref config) = self.config {// The `Some` gate stays because it is the one this function// has always had: no config, no LLM recap, heuristic bookend- // instead. Nothing reads the config now that every build folds- // through the driver.- let _ = config;+ // instead. The config is what a host-side fold builds its+ // model from, when the driver has none (see below).tracing::debug!("[archivist] summarize_entries: LLM recap segment={segment_id} entries={}",entries.len()@@ -350,12 +421,42 @@ impl ArchivistHook {);}// A driver with a tree but no model to fold with (hosted- // memory) says so: the heuristic recap is its answer.+ // memory) says so; the host folds with its own model.Err(e @ crate::memory::api::error::MemoryError::Unsupported { .. }) => {tracing::debug!("[archivist] summarize_entries: driver has no summariser — \- heuristic fallback segment={segment_id}: {e}"+ folding with the host model segment={segment_id}: {e}");+ let started = std::time::Instant::now();+ match tokio::time::timeout(+ RECAP_DEADLINE,+ fold_with_host_model(config, entries),+ )+ .await+ {+ Ok(Ok(recap)) if !recap.is_empty() => {+ tracing::debug!(+ "[archivist] summarize_entries: host recap ok \+ segment={segment_id} chars={} elapsed_ms={}",+ recap.len(),+ started.elapsed().as_millis()+ );+ return (recap, true);+ }+ Ok(Ok(_)) => tracing::debug!(+ "[archivist] summarize_entries: host recap empty — \+ heuristic fallback segment={segment_id}"+ ),+ Ok(Err(error)) => tracing::warn!(+ "[archivist] summarize_entries: host recap failed \+ (non-fatal) segment={segment_id}: {error} — heuristic fallback"+ ),+ Err(_) => tracing::warn!(+ "[archivist] summarize_entries: host recap exceeded {:?} — \+ segment={segment_id} left unsummarised",+ RECAP_DEADLINE+ ),+ }}Err(e) => {tracing::warn!(diff --git a/crates/openhuman-core/src/agent/harness/archivist/recap_tests.rs b/crates/openhuman-core/src/agent/harness/archivist/recap_tests.rsindex 15658088dd..c3d88ec1d1 100644--- a/crates/openhuman-core/src/agent/harness/archivist/recap_tests.rs+++ b/crates/openhuman-core/src/agent/harness/archivist/recap_tests.rs@@ -84,3 +84,78 @@ fn the_recap_deadline_leaves_room_for_the_drivers_retry() {default, which is the bound it exists to replace");}++// ── Host-side fold, for a driver with no model ──────────────────────────────++fn turn(role: &str, content: &str) -> EpisodicTurn {+ EpisodicTurn {+ id: None,+ session_id: "s1".into(),+ timestamp: 1.0,+ role: role.into(),+ content: content.into(),+ lesson: None,+ tool_calls_json: None,+ cost_microdollars: 0,+ }+}++/// The transcript keeps the newest turns that fit, in order, and skips blank+/// ones.+#[test]+fn a_transcript_keeps_the_newest_turns_that_fit() {+ let turns = [+ turn("user", "first question"),+ turn("assistant", " "),+ turn("user", "second question"),+ turn("assistant", "the answer"),+ ];+ let refs: Vec<&EpisodicTurn> = turns.iter().collect();+ assert_eq!(+ transcript(&refs, 10_000),+ "user: first question\n\nuser: second question\n\nassistant: the answer"+ );+ // Two paragraphs of 21 characters, each with its separator, need 46.+ let tight = transcript(&refs, 46);+ assert_eq!(tight, "user: second question\n\nassistant: the answer");+ assert_eq!(+ transcript(&refs[..1], 5),+ "user: first question",+ "one turn always fits, whatever the budget"+ );+}++struct ScriptedRecap;++#[async_trait::async_trait]+impl tinyinference_llm::model::ChatModel<()> for ScriptedRecap {+ async fn invoke(+ &self,+ _state: &(),+ request: tinyinference_llm::model::ModelRequest,+ ) -> tinyinference_llm::Result<tinyinference_llm::model::ModelResponse> {+ let seen = format!("{request:?}");+ assert!(seen.contains("user: book the flights"), "{seen}");+ Ok(tinyinference_llm::model::ModelResponse::assistant(+ " They booked the flights. ",+ ))+ }+}++/// When the driver has no model, the host's own folds the segment, and the+/// recap comes back trimmed.+#[tokio::test]+async fn the_host_model_folds_a_segment_the_driver_cannot() {+ let _model = crate::inference::provider::factory::test_provider_override::install_model(+ std::sync::Arc::new(ScriptedRecap),+ );+ let config = crate::config::Config::default();+ let turns = [turn("user", "book the flights"), turn("assistant", "done")];+ let refs: Vec<&EpisodicTurn> = turns.iter().collect();+ let recap = fold_with_host_model(&config, &refs)+ .await+ .expect("the host folds");+ assert_eq!(recap, "They booked the flights.");+ let none = fold_with_host_model(&config, &[]).await.expect("no turns");+ assert!(none.is_empty(), "no turns, no model call, no recap");+}