Skip to content

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

fix(tinymemes): restore callback handling in tests

Steven Enamakel666838fddfeb
diff --git a/crates/openhuman-core/src/tinymemes/callbacks.rs b/crates/openhuman-core/src/tinymemes/callbacks.rs
index 95cf8ec22f..7ea62b624a 100644
--- a/crates/openhuman-core/src/tinymemes/callbacks.rs
+++ b/crates/openhuman-core/src/tinymemes/callbacks.rs
@@ -145,6 +145,16 @@ pub(super) fn end(correlation: &str) {
}
}
+/// How many registered remixes belong to `workspace`; tests use a unique
+/// workspace so parallel tests cannot disturb the count.
+#[cfg(test)]
+pub(super) fn live_for(workspace: &std::path::Path) -> usize {
+ registry()
+ .values()
+ .filter(|entry| entry.session.config.workspace_dir == workspace)
+ .count()
+}
+
/// The correlation id a callback payload carries (`[ { "correlation": .. } ]`).
pub(super) fn correlation_of(payload: &Value) -> Option<&str> {
payload
diff --git a/crates/openhuman-core/src/tinymemes/callbacks_tests.rs b/crates/openhuman-core/src/tinymemes/callbacks_tests.rs
index ebe04e985c..15c6f79554 100644
--- a/crates/openhuman-core/src/tinymemes/callbacks_tests.rs
+++ b/crates/openhuman-core/src/tinymemes/callbacks_tests.rs
@@ -158,3 +158,30 @@ fn the_correlation_id_is_read_from_the_first_argument_only() {
assert_eq!(correlation_of(&bad), None, "{bad}");
}
}
+
+#[tokio::test]
+async fn callbacks_run_as_the_identity_that_registered_the_remix() {
+ let dir = tempfile::tempdir().unwrap();
+ let config = config_in(dir.path());
+ let identity = MemoryIdentity {
+ agent_id: Some("tenant-agent".into()),
+ team: Some("team-a".into()),
+ };
+ // Registered inside the caller's task...
+ let session = crate::memory::scope::within(identity.clone(), async { Session::new(&config) }).await;
+ // ...and answered later, on a task that has no scope of its own.
+ assert_eq!(crate::memory::scope::current(), None);
+ let seen = session.enter(async { crate::memory::scope::current() }).await;
+ assert_eq!(seen, Some(identity));
+ assert_eq!(crate::memory::scope::current(), None, "the scope does not leak out");
+}
+
+#[tokio::test]
+async fn a_session_registered_without_a_scope_adds_none() {
+ let dir = tempfile::tempdir().unwrap();
+ let session = Session::new(&config_in(dir.path()));
+ assert_eq!(
+ session.enter(async { crate::memory::scope::current() }).await,
+ None
+ );
+}
diff --git a/crates/openhuman-core/src/tinymemes/e2e_tests.rs b/crates/openhuman-core/src/tinymemes/e2e_tests.rs
index 41005e0b59..22ec357742 100644
--- a/crates/openhuman-core/src/tinymemes/e2e_tests.rs
+++ b/crates/openhuman-core/src/tinymemes/e2e_tests.rs
@@ -72,6 +72,8 @@ async fn the_real_module_fails_open_and_reads_its_documents_through_the_host() {
.expect("the module answered");
assert!(!response.remixed, "{response:?}");
assert_eq!(response.reply, "That's done.");
+ // The module reported the request finished, so nothing is kept for it.
+ assert_eq!(callbacks::live_for(&config.workspace_dir), 0);
let legacy = config.workspace_dir.join("tinymemes/remixed.json");
assert!(
@@ -95,6 +97,11 @@ async fn a_disabled_request_passes_through_without_a_callback() {
let response = service::remix(&config, disabled).await.unwrap();
assert_eq!(response.reply, "hello");
assert_eq!(response.skipped, Some(SkipReason::Disabled));
+ assert_eq!(
+ callbacks::live_for(&config.workspace_dir),
+ 0,
+ "a disabled request keeps no registration"
+ );
assert!(
config.workspace_dir.join("tinymemes/remixed.json").exists(),
"a disabled request reads nothing"
diff --git a/crates/openhuman-core/src/tinymemes/service_tests.rs b/crates/openhuman-core/src/tinymemes/service_tests.rs
index c944632aea..7b4b0a2bb0 100644
--- a/crates/openhuman-core/src/tinymemes/service_tests.rs
+++ b/crates/openhuman-core/src/tinymemes/service_tests.rs
@@ -215,3 +215,21 @@ async fn component_off_refuses_before_any_module_load() {
let error = remix(&config, request).await.unwrap_err();
assert!(error.contains("component is off"), "{error}");
}
+
+#[test]
+fn foreground_remixes_share_a_lock_per_workspace_and_never_across_workspaces() {
+ let (dir_a, dir_b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
+ let (a, b) = (config_in(dir_a.path()), config_in(dir_b.path()));
+ assert!(std::sync::Arc::ptr_eq(&scope_lock(&a), &scope_lock(&a)));
+ assert!(!std::sync::Arc::ptr_eq(&scope_lock(&a), &scope_lock(&b)));
+}
+
+#[tokio::test]
+async fn a_second_remix_for_one_workspace_waits_for_the_first_but_another_workspace_does_not() {
+ let (dir_a, dir_b) = (tempfile::tempdir().unwrap(), tempfile::tempdir().unwrap());
+ let (a, b) = (config_in(dir_a.path()), config_in(dir_b.path()));
+ let held = scope_lock(&a);
+ let _first = held.lock().await;
+ assert!(scope_lock(&a).try_lock().is_err(), "same workspace waits");
+ assert!(scope_lock(&b).try_lock().is_ok(), "another workspace does not");
+}