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): wait for per-turn recall when each_turn is set
Steven Enamakel4d30c50a9480
diff --git a/crates/openhuman-core/src/inference/turn_models_summarizer_tests.rs b/crates/openhuman-core/src/inference/turn_models_summarizer_tests.rsindex b3ed499914..c965a50eb0 100644--- a/crates/openhuman-core/src/inference/turn_models_summarizer_tests.rs+++ b/crates/openhuman-core/src/inference/turn_models_summarizer_tests.rs@@ -105,7 +105,11 @@ async fn wire_models(summarizer_model: Option<&str>) -> WireModels {body["model"].as_str().unwrap_or_default().to_owned()}).collect();- assert_eq!(sent.len(), 2, "one summarizer and one primary call: {sent:?}");+ assert_eq!(+ sent.len(),+ 2,+ "one summarizer and one primary call: {sent:?}"+ );WireModels {summarizer: sent[0].clone(),primary: sent[1].clone(),diff --git a/crates/openhuman-core/src/memory/lifecycle/prefetch.rs b/crates/openhuman-core/src/memory/lifecycle/prefetch.rsindex eec0fb019a..2417ae2b47 100644--- a/crates/openhuman-core/src/memory/lifecycle/prefetch.rs+++ b/crates/openhuman-core/src/memory/lifecycle/prefetch.rs@@ -163,16 +163,35 @@ impl RecallEachTurn {}/// Leaves out a pack whose every cited hit `exclude` matches.+ ///+ /// This is the seam for a host that already shows some memory in the+ /// system prompt. A pack with any hit `exclude` keeps is shown whole:+ /// TinyMemory renders the pack before the host sees it, so single lines+ /// cannot be dropped here.#[must_use]pub(crate) fn excluding(mut self, exclude: RecallExclude) -> Self {self.exclude = Some(exclude);self}++ /// Whether `exclude` matches every hit `pack` cites. A pack citing+ /// nothing (a refusal notice) is never excluded.+ fn excludes(&self, pack: &TurnPack) -> bool {+ self.exclude.as_ref().is_some_and(|exclude| {+ !pack.native.citations.is_empty()+ && pack.native.citations.iter().all(|hit| exclude(hit))+ })+ }}-/// Returns the last completed pack immediately, then logs this user turn and-/// refreshes its context in the background. A cold cache returns `None`.-/// Omitted recall still logs, but cannot consume another identity's pack.+/// Logs this user turn and refreshes its context in the background.+///+/// Without `each_turn`, returns the last completed pack immediately (a cold+/// cache returns `None`). With it, waits at most its deadline for the pack+/// recalled for *this* message, and returns `None` when that pack is late,+/// missing or excluded; the background work still completes and is cached.+/// Omitted recall still logs, but cannot consume another identity's pack, and+/// ignores `each_turn`.pub(crate) async fn pre_turn(ctx: &DispatchCtx,config: Arc<Config>,@@ -185,11 +204,12 @@ pub(crate) async fn pre_turn(resumed_after_compaction: bool,observed_actor: Option<tinymemory_api::ObservedActor>,channel: Option<String>,- _each_turn: Option<RecallEachTurn>,+ each_turn: Option<RecallEachTurn>,) -> Option<TurnPack> {if !config.memory.conversations.enabled && !identity.recall {return None;}+ let each_turn = each_turn.filter(|_| identity.recall);let cache = cache(ctx);let key = Key::with_fingerprint(&config,@@ -198,38 +218,97 @@ pub(crate) async fn pre_turn(&thread_id,false,);- let pack = identity.recall.then(|| completed(&cache, &key)).flatten();- tracing::debug!(thread_id = %key.thread, agent_id = %key.identity.agent_id, cache_hit = pack.is_some(), "[memory:prefetch] pre_turn cache");- if let Some(pack) = &pack {- hooks::deliver_citations(ctx, &config, &identity, &thread_id, pack);+ let cached = (identity.recall && each_turn.is_none())+ .then(|| completed(&cache, &key))+ .flatten();+ if each_turn.is_none() {+ tracing::debug!(thread_id = %key.thread, agent_id = %key.identity.agent_id, cache_hit = cached.is_some(), "[memory:prefetch] pre_turn cache");}+ let (answer, answered) = match each_turn {+ Some(_) => {+ let (answer, answered) = tokio::sync::oneshot::channel();+ (Some(answer), Some(answered))+ }+ None => (None, None),+ };let dispatch = ctx.clone();- enqueue(- cache,- key,- guard_turn_work(- &config.clone(),- Box::pin(dispatch.clone().enter(async move {- hooks::pre_turn_work(- &dispatch,- &config,- &identity,- thread_id,- turn_index,- user_text,- in_prompt_from,- at,- resumed_after_compaction,- observed_actor,- channel,- )- .await- })),- ),+ let shown = (config.clone(), identity.clone(), thread_id.clone());+ let work = guard_turn_work(+ &config.clone(),+ Box::pin(dispatch.clone().enter(async move {+ hooks::pre_turn_work(+ &dispatch,+ &config,+ &identity,+ thread_id,+ turn_index,+ user_text,+ in_prompt_from,+ at,+ resumed_after_compaction,+ observed_actor,+ channel,+ )+ .await+ })),);+ let work: Work = match answer {+ Some(answer) => Box::pin(async move {+ let pack = work.await;+ // The turn may have stopped waiting; the cache still keeps the pack.+ let _ = answer.send(pack.clone());+ pack+ }),+ None => work,+ };+ let (thread, agent) = (key.thread.clone(), key.identity.agent_id.clone());+ enqueue(cache, key, work);+ let pack = match (each_turn, answered) {+ (Some(each_turn), Some(answered)) => {+ current_pack(&each_turn, answered, &thread, &agent).await+ }+ _ => cached,+ };+ if let Some(pack) = &pack {+ let (config, identity, thread_id) = shown;+ hooks::deliver_citations(ctx, &config, &identity, &thread_id, pack);+ }pack}+/// The pack recalled for the current message, if it answers within the+/// deadline and is not excluded. Logs ids and timings only, never content.+async fn current_pack(+ each_turn: &RecallEachTurn,+ answered: tokio::sync::oneshot::Receiver<Option<TurnPack>>,+ thread: &str,+ agent: &str,+) -> Option<TurnPack> {+ let timeout_ms = u64::try_from(each_turn.timeout.as_millis()).unwrap_or(u64::MAX);+ match tokio::time::timeout(each_turn.timeout, answered).await {+ Ok(Ok(Some(pack))) if each_turn.excludes(&pack) => {+ tracing::debug!(thread_id = %thread, agent_id = %agent, "[memory:recall] every cited hit is excluded; no note");+ None+ }+ Ok(Ok(Some(pack))) => {+ tracing::debug!(thread_id = %thread, agent_id = %agent, refs = pack.refs.len(), "[memory:recall] current message recalled in time");+ Some(pack)+ }+ Ok(Ok(None)) => {+ tracing::debug!(thread_id = %thread, agent_id = %agent, "[memory:recall] nothing recalled for the current message");+ None+ }+ Ok(Err(_)) => {+ tracing::warn!(thread_id = %thread, agent_id = %agent, timeout_ms, "[memory:recall] deadline: recall work ended without an answer (skipped or cancelled); no note");+ None+ }+ Err(_) => {+ tracing::debug!(thread_id = %thread, agent_id = %agent, timeout_ms, "[memory:recall] deadline: recall not ready in time; no note");+ None+ }+ }+}+fn guard_turn_work(config: &Config, work: Work) -> Work {let mutation = crate::memory::tool_writes::automatic_mutation(config);Box::pin(async move { mutation.run(work).await.flatten() })diff --git a/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs b/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rsindex 3d3396af31..4ef87b5a97 100644--- a/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs+++ b/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs@@ -745,7 +745,11 @@ async fn recall_each_turn_gives_up_at_its_deadline_and_adds_no_latency_beyond_it).await;assert!(pack.is_none(), "a late recall adds no note");- assert_eq!(started.elapsed(), deadline, "the turn waits the deadline, no longer");+ assert_eq!(+ started.elapsed(),+ deadline,+ "the turn waits the deadline, no longer"+ );release.add_permits(100);drain(@@ -776,7 +780,11 @@ async fn without_recall_each_turn_a_turn_still_reads_only_completed_packs() {let pack = turn_on(&config, &identity, "each-turn-off", 2, None).await.expect("the next turn reads the completed pack");- assert!(pack.markdown.starts_with(CACHED_NOTICE), "{}", pack.markdown);+ assert!(+ pack.markdown.starts_with(CACHED_NOTICE),+ "{}",+ pack.markdown+ );drain(&cache, &key).await;}@@ -791,20 +799,28 @@ async fn an_excluded_recall_leaves_the_turn_without_a_pack() {let cache = cache(&crate::core::runtime::DispatchCtx::ambient());let key = Key::new(&config, &identity, "each-turn-excluded", false);- let unrelated = RecallEachTurn::new(Duration::from_secs(5))- .excluding(Arc::new(|hit: &tinymemory_api::Hit| hit.text.contains("violet")));+ let unrelated = RecallEachTurn::new(Duration::from_secs(5)).excluding(Arc::new(+ |hit: &tinymemory_api::Hit| hit.text.contains("violet"),+ ));let kept = turn_on(&config, &identity, "each-turn-excluded", 0, Some(unrelated)).await.expect("a pack with nothing excluded is shown");assert!(kept.markdown.contains("teal"));drain(&cache, &key).await;- let everything = RecallEachTurn::new(Duration::from_secs(5))- .excluding(Arc::new(|hit: &tinymemory_api::Hit| hit.text.contains("teal")));+ let everything = RecallEachTurn::new(Duration::from_secs(5)).excluding(Arc::new(+ |hit: &tinymemory_api::Hit| hit.text.contains("teal"),+ ));assert!(- turn_on(&config, &identity, "each-turn-excluded", 2, Some(everything))- .await- .is_none(),+ turn_on(+ &config,+ &identity,+ "each-turn-excluded",+ 2,+ Some(everything)+ )+ .await+ .is_none(),"a pack whose every cited hit is excluded is left out");drain(&cache, &key).await;