Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

test(prefetch): split recall-each-turn tests into their own module

Steven Enamakeld5b5309c29a6
diff --git a/crates/openhuman-core/src/memory/lifecycle/prefetch.rs b/crates/openhuman-core/src/memory/lifecycle/prefetch.rs
index fa370aeeb4..f000b7fda6 100644
--- a/crates/openhuman-core/src/memory/lifecycle/prefetch.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/prefetch.rs
@@ -377,3 +377,7 @@ pub(crate) async fn settle_for_test(
#[cfg(test)]
#[path = "prefetch_tests.rs"]
mod tests;
+
+#[cfg(test)]
+#[path = "prefetch_recall_each_turn_tests.rs"]
+mod recall_each_turn_tests;
diff --git a/crates/openhuman-core/src/memory/lifecycle/prefetch_recall_each_turn_tests.rs b/crates/openhuman-core/src/memory/lifecycle/prefetch_recall_each_turn_tests.rs
new file mode 100644
index 0000000000..c9bc4a5a81
--- /dev/null
+++ b/crates/openhuman-core/src/memory/lifecycle/prefetch_recall_each_turn_tests.rs
@@ -0,0 +1,182 @@
+//! Recall for the current message (`RecallEachTurn`): a turn that waits,
+//! up to its deadline, for a pack answering its own message.
+
+use super::tests::{drain, HeldEngine, CACHED_NOTICE, TEST_LOCK};
+use super::*;
+
+use crate::memory::test_fixtures::{bind_reference, config_in};
+
+async fn remember_teal(engine: &tinymemory_api::conformance::ReferenceEngine) {
+ use tinymemory_api::MemoryEngine;
+ engine
+ .store(tinymemory_api::StoreItem::learning(
+ "The user's favourite colour is teal",
+ tinymemory_api::LearningKind::Preference,
+ 0.9,
+ tinymemory_api::MemoryMeta::default(),
+ ))
+ .await
+ .unwrap();
+}
+
+async fn turn_on(
+ config: &Arc<Config>,
+ identity: &ResolvedIdentity,
+ thread: &str,
+ turn_index: u32,
+ each_turn: Option<RecallEachTurn>,
+) -> Option<TurnPack> {
+ pre_turn(
+ &crate::core::runtime::DispatchCtx::ambient(),
+ config.clone(),
+ identity.clone(),
+ thread.into(),
+ turn_index,
+ "what colour do I like?".into(),
+ 0,
+ chrono::Utc::now(),
+ false,
+ None,
+ None,
+ each_turn,
+ )
+ .await
+}
+
+#[tokio::test]
+async fn recall_each_turn_answers_the_current_message_within_its_deadline() {
+ let _guard = TEST_LOCK.lock().await;
+ let tmp = tempfile::tempdir().unwrap();
+ let config = Arc::new(config_in(&tmp));
+ remember_teal(&bind_reference(&config)).await;
+ let identity = tinymemory_tools::layout::identity::Identity::agent("each-turn")
+ .resolve(&crate::memory::scope::identity_pins(&config));
+
+ let pack = turn_on(
+ &config,
+ &identity,
+ "each-turn",
+ 0,
+ Some(RecallEachTurn::new(Duration::from_secs(5))),
+ )
+ .await
+ .expect("the first turn already carries its own pack");
+ assert!(pack.markdown.contains("teal"), "{}", pack.markdown);
+ assert!(
+ !pack.markdown.contains(CACHED_NOTICE.trim()),
+ "a pack for this message, not a cached one from an earlier turn"
+ );
+ drain(
+ &cache(&crate::core::runtime::DispatchCtx::ambient()),
+ &Key::new(&config, &identity, "each-turn", false),
+ )
+ .await;
+}
+
+#[tokio::test(start_paused = true)]
+async fn recall_each_turn_gives_up_at_its_deadline_and_adds_no_latency_beyond_it() {
+ let _guard = TEST_LOCK.lock().await;
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ let release = Arc::new(tokio::sync::Semaphore::new(0));
+ let engine = Arc::new(HeldEngine {
+ inner: tinymemory_api::conformance::ReferenceEngine::new(),
+ release: release.clone(),
+ });
+ remember_teal(&engine.inner).await;
+ crate::memory::engine::install_test_engine(&config.workspace_dir, engine.clone());
+ let config = Arc::new(config);
+ let identity = tinymemory_tools::layout::identity::Identity::agent("each-turn-late")
+ .resolve(&crate::memory::scope::identity_pins(&config));
+ let deadline = Duration::from_millis(800);
+
+ let started = tokio::time::Instant::now();
+ let pack = turn_on(
+ &config,
+ &identity,
+ "each-turn-late",
+ 0,
+ Some(RecallEachTurn::new(deadline)),
+ )
+ .await;
+ assert!(pack.is_none(), "a late recall adds no note");
+ assert_eq!(
+ started.elapsed(),
+ deadline,
+ "the turn waits the deadline, no longer"
+ );
+
+ release.add_permits(100);
+ drain(
+ &cache(&crate::core::runtime::DispatchCtx::ambient()),
+ &Key::new(&config, &identity, "each-turn-late", false),
+ )
+ .await;
+}
+
+#[tokio::test]
+async fn without_recall_each_turn_a_turn_still_reads_only_completed_packs() {
+ let _guard = TEST_LOCK.lock().await;
+ let tmp = tempfile::tempdir().unwrap();
+ let config = Arc::new(config_in(&tmp));
+ remember_teal(&bind_reference(&config)).await;
+ let identity = tinymemory_tools::layout::identity::Identity::agent("each-turn-off")
+ .resolve(&crate::memory::scope::identity_pins(&config));
+ let cache = cache(&crate::core::runtime::DispatchCtx::ambient());
+ let key = Key::new(&config, &identity, "each-turn-off", false);
+
+ assert!(
+ turn_on(&config, &identity, "each-turn-off", 0, None)
+ .await
+ .is_none(),
+ "a cold cache gives the first turn nothing"
+ );
+ drain(&cache, &key).await;
+ 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
+ );
+ drain(&cache, &key).await;
+}
+
+#[tokio::test]
+async fn an_excluded_recall_leaves_the_turn_without_a_pack() {
+ let _guard = TEST_LOCK.lock().await;
+ let tmp = tempfile::tempdir().unwrap();
+ let config = Arc::new(config_in(&tmp));
+ remember_teal(&bind_reference(&config)).await;
+ let identity = tinymemory_tools::layout::identity::Identity::agent("each-turn-excluded")
+ .resolve(&crate::memory::scope::identity_pins(&config));
+ 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 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"),
+ ));
+ assert!(
+ 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;
+}
diff --git a/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs b/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs
index 4ef87b5a97..572f308365 100644
--- a/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs
+++ b/crates/openhuman-core/src/memory/lifecycle/prefetch_tests.rs
@@ -5,9 +5,9 @@ use tinymemory_tools::lifecycle::turn::TurnPack as NativeTurnPack;
const MAX_PENDING: usize = 16;
const MAX_PACK_BYTES: usize = 64 * 1024;
-const CACHED_NOTICE: &str = "This is cached memory from a completed background lookup for an earlier turn. It may be incomplete or unrelated to the current question; use explicit memory recall or fetch when you need a current answer.\n\n";
+pub(super) const CACHED_NOTICE: &str = "This is cached memory from a completed background lookup for an earlier turn. It may be incomplete or unrelated to the current question; use explicit memory recall or fetch when you need a current answer.\n\n";
-static TEST_LOCK: LazyLock<tokio::sync::Mutex<()>> = LazyLock::new(|| tokio::sync::Mutex::new(()));
+pub(super) static TEST_LOCK: LazyLock<tokio::sync::Mutex<()>> = LazyLock::new(|| tokio::sync::Mutex::new(()));
fn pack(text: &str) -> TurnPack {
TurnPack::from_native(NativeTurnPack {
@@ -20,7 +20,7 @@ fn pack(text: &str) -> TurnPack {
})
}
-async fn drain(cache: &Cache, key: &Key) {
+pub(super) async fn drain(cache: &Cache, key: &Key) {
tokio::time::timeout(Duration::from_secs(1), async {
while !cache.0.is_idle(key) {
tokio::task::yield_now().await;
@@ -308,9 +308,9 @@ async fn credential_invalidation_removes_ready_and_rejects_late_inflight_results
);
}
-struct HeldEngine {
- inner: tinymemory_api::conformance::ReferenceEngine,
- release: Arc<tokio::sync::Semaphore>,
+pub(super) struct HeldEngine {
+ pub(super) inner: tinymemory_api::conformance::ReferenceEngine,
+ pub(super) release: Arc<tokio::sync::Semaphore>,
}
#[async_trait::async_trait]
impl tinymemory_api::MemoryEngine for HeldEngine {
@@ -648,180 +648,3 @@ async fn erase_waits_for_current_automatic_work_before_returning_its_barrier() {
drop(fence);
drain(&cache, &key).await;
}
-
-// ── recall for the current message (`RecallEachTurn`) ──────────────────────
-
-async fn remember_teal(engine: &tinymemory_api::conformance::ReferenceEngine) {
- use tinymemory_api::MemoryEngine;
- engine
- .store(tinymemory_api::StoreItem::learning(
- "The user's favourite colour is teal",
- tinymemory_api::LearningKind::Preference,
- 0.9,
- tinymemory_api::MemoryMeta::default(),
- ))
- .await
- .unwrap();
-}
-
-async fn turn_on(
- config: &Arc<Config>,
- identity: &ResolvedIdentity,
- thread: &str,
- turn_index: u32,
- each_turn: Option<RecallEachTurn>,
-) -> Option<TurnPack> {
- pre_turn(
- &crate::core::runtime::DispatchCtx::ambient(),
- config.clone(),
- identity.clone(),
- thread.into(),
- turn_index,
- "what colour do I like?".into(),
- 0,
- chrono::Utc::now(),
- false,
- None,
- None,
- each_turn,
- )
- .await
-}
-
-#[tokio::test]
-async fn recall_each_turn_answers_the_current_message_within_its_deadline() {
- let _guard = TEST_LOCK.lock().await;
- let tmp = tempfile::tempdir().unwrap();
- let config = Arc::new(config_in(&tmp));
- remember_teal(&bind_reference(&config)).await;
- let identity = tinymemory_tools::layout::identity::Identity::agent("each-turn")
- .resolve(&crate::memory::scope::identity_pins(&config));
-
- let pack = turn_on(
- &config,
- &identity,
- "each-turn",
- 0,
- Some(RecallEachTurn::new(Duration::from_secs(5))),
- )
- .await
- .expect("the first turn already carries its own pack");
- assert!(pack.markdown.contains("teal"), "{}", pack.markdown);
- assert!(
- !pack.markdown.contains(CACHED_NOTICE.trim()),
- "a pack for this message, not a cached one from an earlier turn"
- );
- drain(
- &cache(&crate::core::runtime::DispatchCtx::ambient()),
- &Key::new(&config, &identity, "each-turn", false),
- )
- .await;
-}
-
-#[tokio::test(start_paused = true)]
-async fn recall_each_turn_gives_up_at_its_deadline_and_adds_no_latency_beyond_it() {
- let _guard = TEST_LOCK.lock().await;
- let tmp = tempfile::tempdir().unwrap();
- let config = config_in(&tmp);
- let release = Arc::new(tokio::sync::Semaphore::new(0));
- let engine = Arc::new(HeldEngine {
- inner: tinymemory_api::conformance::ReferenceEngine::new(),
- release: release.clone(),
- });
- remember_teal(&engine.inner).await;
- crate::memory::engine::install_test_engine(&config.workspace_dir, engine.clone());
- let config = Arc::new(config);
- let identity = tinymemory_tools::layout::identity::Identity::agent("each-turn-late")
- .resolve(&crate::memory::scope::identity_pins(&config));
- let deadline = Duration::from_millis(800);
-
- let started = tokio::time::Instant::now();
- let pack = turn_on(
- &config,
- &identity,
- "each-turn-late",
- 0,
- Some(RecallEachTurn::new(deadline)),
- )
- .await;
- assert!(pack.is_none(), "a late recall adds no note");
- assert_eq!(
- started.elapsed(),
- deadline,
- "the turn waits the deadline, no longer"
- );
-
- release.add_permits(100);
- drain(
- &cache(&crate::core::runtime::DispatchCtx::ambient()),
- &Key::new(&config, &identity, "each-turn-late", false),
- )
- .await;
-}
-
-#[tokio::test]
-async fn without_recall_each_turn_a_turn_still_reads_only_completed_packs() {
- let _guard = TEST_LOCK.lock().await;
- let tmp = tempfile::tempdir().unwrap();
- let config = Arc::new(config_in(&tmp));
- remember_teal(&bind_reference(&config)).await;
- let identity = tinymemory_tools::layout::identity::Identity::agent("each-turn-off")
- .resolve(&crate::memory::scope::identity_pins(&config));
- let cache = cache(&crate::core::runtime::DispatchCtx::ambient());
- let key = Key::new(&config, &identity, "each-turn-off", false);
-
- assert!(
- turn_on(&config, &identity, "each-turn-off", 0, None)
- .await
- .is_none(),
- "a cold cache gives the first turn nothing"
- );
- drain(&cache, &key).await;
- 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
- );
- drain(&cache, &key).await;
-}
-
-#[tokio::test]
-async fn an_excluded_recall_leaves_the_turn_without_a_pack() {
- let _guard = TEST_LOCK.lock().await;
- let tmp = tempfile::tempdir().unwrap();
- let config = Arc::new(config_in(&tmp));
- remember_teal(&bind_reference(&config)).await;
- let identity = tinymemory_tools::layout::identity::Identity::agent("each-turn-excluded")
- .resolve(&crate::memory::scope::identity_pins(&config));
- 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 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"),
- ));
- assert!(
- 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;
-}