Skip to content

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

test(storage): wire cdylib host tests into the module tree

Steven Enamakel2b5e85e8e732
diff --git a/crates/openhuman-core/src/modules/storage_host_cdylib_tests.rs b/crates/openhuman-core/src/modules/storage_host_cdylib_tests.rs
new file mode 100644
index 0000000000..3829ca0039
--- /dev/null
+++ b/crates/openhuman-core/src/modules/storage_host_cdylib_tests.rs
@@ -0,0 +1,578 @@
+//! The storage host against a REAL loaded module (#7772).
+//!
+//! `tests/fixtures/storage_probe_module` is a cdylib that relays one raw call
+//! to `ai.tinyhumans.tinystorage.Storage` and reports the answer. Here it is
+//! built, hash-pinned in a `modules.toml` beside it and loaded through
+//! `ModuleHost::load_file`, so every call below crosses the real ABI, the real
+//! broker and the real `authenticated_sender` rule of a loaded module. The
+//! host double only decides which registry id the probe's peer stands for
+//! (`tinymcp` or `tinyjuice`, so the compiled grants apply).
+//!
+//! What this proves, per the issue:
+//! * a module cannot reach another module's collections, another scope, or a
+//! collection its grant never declared;
+//! * a blob over the 16 MiB frame cannot go inline;
+//! * `PutStream`, `GetStream` and `WatchOpen` are refused as unsupported.
+//!
+//! What still waits for tinybus 0.1.5 (#7781): the positive halves of the last
+//! two. Peer-bound streams (`Connection::accept_stream_from`) and unicast watch
+//! signals (`Connection::emit_to`) do not exist in the vendored tinybus 0.1.3
+//! (`vendor/tinybus`), so the host cannot bind `PutStream`/`GetStream`/
+//! `WatchOpen` and answers them `unsupported-selector`. Once #7781 moves the
+//! pin, replace `streams_and_watches_are_refused_until_tinybus_binds_them` with
+//! the >16 MiB `PutStream`/`GetStream` round trip and a watch signal.
+
+use std::path::{Path, PathBuf};
+use std::sync::{Mutex, OnceLock};
+
+use serde_json::{json, Value};
+use tinybus::broker::Broker;
+use tinybus::module::ModuleHost;
+use tinybus::transport::memory::MemoryBus;
+use tinybus::{Connection, Proxy};
+use tinystoragedrivers::secrets::{DerivedKeys, KeyProvider};
+use tinystoragedrivers::MemoryStorage;
+use zeroize::Zeroizing;
+
+use super::super::storage_grants::{declared, StorageGrant};
+use super::super::storage_host::{
+ module_scope, AuditRecord, AuditSink, CallerDirectory, Environment, StorageHost,
+};
+use super::super::storage_host_linked::attach;
+use super::{async_trait, Arc, BusName, Scope, StorageBackend, StorageError, STORAGE_INTERFACE};
+
+const PROBE: &str = "ai.tinyhumans.test.StorageProbe";
+
+/// The linked storage module's pending slot is process-wide, so every test
+/// that attaches takes a turn.
+static TURN: tokio::sync::Mutex<()> = tokio::sync::Mutex::const_new(());
+
+fn repo_root() -> PathBuf {
+ Path::new(env!("CARGO_MANIFEST_DIR"))
+ .join("../..")
+ .canonicalize()
+ .expect("repository root")
+}
+
+/// The built probe library: `OPENHUMAN_STORAGE_PROBE_MODULE` when set, else a
+/// build of the fixture under the checkout's own `target/storage-probe`.
+fn probe_artifact() -> &'static Path {
+ static BUILT: OnceLock<PathBuf> = OnceLock::new();
+ BUILT.get_or_init(|| {
+ if let Some(path) = std::env::var_os("OPENHUMAN_STORAGE_PROBE_MODULE") {
+ return PathBuf::from(path);
+ }
+ let root = repo_root();
+ let target = root.join("target/storage-probe");
+ let status = std::process::Command::new(
+ std::env::var_os("CARGO").unwrap_or_else(|| "cargo".into()),
+ )
+ .args(["build", "--quiet", "--manifest-path"])
+ .arg(root.join("tests/fixtures/storage_probe_module/Cargo.toml"))
+ .arg("--target-dir")
+ .arg(&target)
+ // The test run's own flags (coverage instrumentation, a linker choice)
+ // must not leak into a fixture that is a different build.
+ .env_remove("RUSTFLAGS")
+ .env_remove("CARGO_ENCODED_RUSTFLAGS")
+ .env_remove("RUSTC_WRAPPER")
+ .env_remove("CARGO_TARGET_DIR")
+ .status()
+ .expect("run cargo to build the storage probe module");
+ assert!(status.success(), "building the storage probe module failed");
+ target.join("debug").join(format!(
+ "{}storage_probe_module{}",
+ std::env::consts::DLL_PREFIX,
+ std::env::consts::DLL_SUFFIX
+ ))
+ })
+}
+
+/// Stages the probe beside a `modules.toml` pinning its digest, which is what
+/// makes a loaded module an attested recipient.
+fn stage(directory: &Path) -> PathBuf {
+ let artifact = probe_artifact();
+ let name = artifact.file_name().expect("artifact name");
+ let staged = directory.join(name);
+ std::fs::copy(artifact, &staged).expect("stage the probe");
+ let hash = tinybus::module::sha256_file(&staged).expect("digest");
+ std::fs::write(
+ directory.join("modules.toml"),
+ format!("{:?} = {hash:?}\n", name.to_str().expect("utf-8 name")),
+ )
+ .expect("write modules.toml");
+ staged
+}
+
+/// Names the probe's peer after `module_id` and nobody else.
+struct ProbeDirectory {
+ connection: Connection,
+ module_id: &'static str,
+}
+
+#[async_trait]
+impl CallerDirectory for ProbeDirectory {
+ async fn module_for(&self, sender: &BusName) -> Option<String> {
+ match self.connection.name_owner(PROBE).await {
+ Ok(Some(owner)) if &owner == sender => Some(self.module_id.to_string()),
+ _ => None,
+ }
+ }
+}
+
+struct Env(Arc<dyn StorageBackend>);
+
+#[async_trait]
+impl Environment for Env {
+ fn backend(&self) -> Option<Arc<dyn StorageBackend>> {
+ Some(Arc::clone(&self.0))
+ }
+ fn module_scope(&self, module_id: &str) -> Result<Scope, StorageError> {
+ module_scope(None, &Scope::local(), module_id)
+ }
+ fn grant(&self, module_id: &str) -> Option<StorageGrant> {
+ declared(module_id)
+ }
+ fn keys(&self) -> Result<Arc<dyn KeyProvider>, StorageError> {
+ Ok(Arc::new(DerivedKeys::new(Zeroizing::new([9u8; 32]))))
+ }
+ async fn admit_write(&self, _scope: &Scope) -> Result<(), StorageError> {
+ Ok(())
+ }
+}
+
+#[derive(Default)]
+struct Trail(Mutex<Vec<AuditRecord>>);
+
+impl AuditSink for Trail {
+ fn record(&self, record: &AuditRecord) {
+ self.0.lock().unwrap().push(record.clone());
+ }
+}
+
+struct Rig {
+ backend: Arc<dyn StorageBackend>,
+ trail: Arc<Trail>,
+ /// A peer that is not the probe: the host must not know it.
+ stranger: Connection,
+ probe: Proxy,
+ _modules: ModuleHost,
+ _staged: tempfile::TempDir,
+}
+
+/// The real probe, loaded, standing for registry module `module_id`.
+async fn rig(module_id: &'static str) -> Rig {
+ let transport = MemoryBus::new();
+ let broker = Broker::new();
+ broker.spawn(transport.clone());
+ let modules = ModuleHost::new(broker);
+ let host_connection = Connection::connect(transport.connect().await.unwrap())
+ .await
+ .unwrap();
+ let stranger = Connection::connect(transport.connect().await.unwrap())
+ .await
+ .unwrap();
+ let backend: Arc<dyn StorageBackend> = Arc::new(MemoryStorage::new());
+ let trail = Arc::new(Trail::default());
+ let host = Arc::new(StorageHost::new(
+ Arc::new(ProbeDirectory {
+ connection: host_connection.clone(),
+ module_id,
+ }),
+ Arc::new(Env(Arc::clone(&backend))),
+ trail.clone(),
+ ));
+ attach(&modules, &host_connection, host).await.unwrap();
+ let staged = tempfile::tempdir().unwrap();
+ let artifact = stage(staged.path());
+ modules.load_file(&artifact).expect("load the probe cdylib");
+ let probe = stranger.proxy(PROBE, "/probe", PROBE).unwrap();
+ Rig {
+ backend,
+ trail,
+ stranger,
+ probe,
+ _modules: modules,
+ _staged: staged,
+ }
+}
+
+impl Rig {
+ /// One raw storage call made by the loaded module.
+ async fn call(&self, object: &str, selector: &str, request: Value) -> Value {
+ self.probe
+ .call("Call", (object, selector, request, false))
+ .await
+ .expect("the probe answers")
+ }
+
+ fn scope(&self, module_id: &str) -> Scope {
+ module_scope(None, &Scope::local(), module_id).unwrap()
+ }
+}
+
+fn denied(answer: &Value, label: &str) -> bool {
+ answer["err"]["name"] == "ai.tinyhumans.tinystorage.Error.Denied"
+ && answer["err"]["message"] == label
+}
+
+fn put(collection: &str, id: &str, document: &Value) -> Value {
+ json!({
+ "collection": collection,
+ "document_id": id,
+ "document_json": document.to_string(),
+ "precondition": {"kind": "none"},
+ })
+}
+
+fn get(collection: &str, id: &str) -> Value {
+ json!({"collection": collection, "document_id": id})
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn a_loaded_module_reads_and_writes_its_own_granted_collection() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinymcp").await;
+ let stored = rig
+ .call(
+ "Documents",
+ "Put",
+ put("mcp_servers", "files", &json!({"qualified_name": "files"})),
+ )
+ .await;
+ assert!(stored.get("ok").is_some(), "{stored}");
+ let read = rig
+ .call("Documents", "Get", get("mcp_servers", "files"))
+ .await;
+ assert!(read.get("ok").is_some(), "{read}");
+ assert!(read.to_string().contains("qualified_name"), "{read}");
+ // It landed in the module's own scope, in the backend the host opened.
+ let own = rig.backend.for_scope(&rig.scope("tinymcp")).unwrap();
+ assert!(own
+ .documents()
+ .get("mcp_servers", "files")
+ .await
+ .unwrap()
+ .is_some());
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn a_loaded_module_cannot_reach_another_modules_collections() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinymcp").await;
+ // Another module's collection, seeded in that module's own scope.
+ let theirs = super::super::storage_grants::declared("tinychannels")
+ .expect("tinychannels declares storage")
+ .collections[0]
+ .name
+ .clone();
+ let victim = rig.backend.for_scope(&rig.scope("tinychannels")).unwrap();
+ victim
+ .documents()
+ .ensure_collection(
+ &declared("tinychannels")
+ .unwrap()
+ .collections
+ .into_iter()
+ .next()
+ .unwrap(),
+ )
+ .await
+ .unwrap();
+ victim
+ .documents()
+ .put(
+ &theirs,
+ "session",
+ "{\"marker\":\"OTHER-MODULES-SECRET\"}",
+ Default::default(),
+ )
+ .await
+ .unwrap();
+
+ for (selector, request) in [
+ ("Get", get(&theirs, "session")),
+ ("Put", put(&theirs, "session", &json!({"marker": "overwritten"}))),
+ (
+ "Query",
+ json!({"collection": theirs, "filter": null, "limit": 10}),
+ ),
+ (
+ "AtomicBatch",
+ json!({"ops": [{"kind": "delete", "collection": theirs, "document_id": "session"}]}),
+ ),
+ ] {
+ let answer = rig.call("Documents", selector, request).await;
+ assert!(denied(&answer, "target-not-granted"), "{selector}: {answer}");
+ assert!(!answer.to_string().contains("OTHER-MODULES-SECRET"));
+ }
+ // Untouched: the refusal came before the backend.
+ let after = victim.documents().get(&theirs, "session").await.unwrap();
+ assert!(after
+ .expect("the other module's document survives")
+ .document_json()
+ .contains("OTHER-MODULES-SECRET"));
+ // And its own scope never got the collection.
+ let own = rig.backend.for_scope(&rig.scope("tinymcp")).unwrap();
+ assert!(!own.documents().collections().await.unwrap().contains(&theirs));
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn a_loaded_module_cannot_name_or_reach_another_scope() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinymcp").await;
+ // The same collection and id in the host's default scope and in another
+ // tenant's: neither may be visible to the module.
+ for scope in [Scope::local(), Scope::new("someone-else").unwrap()] {
+ let handle = rig.backend.for_scope(&scope).unwrap();
+ let spec = declared("tinymcp")
+ .unwrap()
+ .collections
+ .into_iter()
+ .next()
+ .unwrap();
+ handle.documents().ensure_collection(&spec).await.unwrap();
+ handle
+ .documents()
+ .put(
+ "mcp_servers",
+ "victim",
+ "{\"marker\":\"OTHER-SCOPE-SECRET\"}",
+ Default::default(),
+ )
+ .await
+ .unwrap();
+ }
+ // Same id, own scope: not found, not the other scope's document.
+ let read = rig
+ .call("Documents", "Get", get("mcp_servers", "victim"))
+ .await;
+ assert!(!read.to_string().contains("OTHER-SCOPE-SECRET"), "{read}");
+ // A scope smuggled into the request is not a selector the wire knows.
+ for field in ["scope", "agent", "module", "profile"] {
+ let mut request = get("mcp_servers", "victim");
+ request[field] = json!("local");
+ let answer = rig.call("Documents", "Get", request).await;
+ assert!(answer.get("err").is_some(), "{field}: {answer}");
+ assert!(!answer.to_string().contains("OTHER-SCOPE-SECRET"));
+ }
+ // A write lands only in the module's own scope.
+ let wrote = rig
+ .call(
+ "Documents",
+ "Put",
+ put("mcp_servers", "mine", &json!({"qualified_name": "mine"})),
+ )
+ .await;
+ assert!(wrote.get("ok").is_some(), "{wrote}");
+ for scope in [Scope::local(), Scope::new("someone-else").unwrap()] {
+ let handle = rig.backend.for_scope(&scope).unwrap();
+ assert!(handle
+ .documents()
+ .get("mcp_servers", "mine")
+ .await
+ .unwrap()
+ .is_none());
+ }
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn a_loaded_module_cannot_use_a_collection_it_never_declared() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinymcp").await;
+ for selector in ["Get", "Query", "Count"] {
+ let request = match selector {
+ "Get" => get("never_declared", "x"),
+ _ => json!({"collection": "never_declared"}),
+ };
+ let answer = rig.call("Documents", selector, request).await;
+ assert!(denied(&answer, "target-not-granted"), "{selector}: {answer}");
+ }
+ let wrote = rig
+ .call(
+ "Documents",
+ "Put",
+ put("never_declared", "x", &json!({"a": 1})),
+ )
+ .await;
+ assert!(denied(&wrote, "target-not-granted"), "{wrote}");
+ // Schema is the host's alone: a module cannot create the collection, nor
+ // change a declared one, nor drop one.
+ let created = rig
+ .call(
+ "Documents",
+ "EnsureCollection",
+ json!({"spec": {"name": "never_declared"}}),
+ )
+ .await;
+ assert!(denied(&created, "schema-is-host-only"), "{created}");
+ let dropped = rig
+ .call(
+ "Documents",
+ "DropCollection",
+ json!({"collection": "mcp_servers"}),
+ )
+ .await;
+ assert!(denied(&dropped, "schema-is-host-only"), "{dropped}");
+ let own = rig.backend.for_scope(&rig.scope("tinymcp")).unwrap();
+ let listed = own.documents().collections().await.unwrap();
+ assert!(!listed.contains(&"never_declared".to_string()), "{listed:?}");
+ // The module's listing shows its own names only.
+ let seen = rig.call("Documents", "Collections", json!({})).await;
+ assert!(!seen.to_string().contains("never_declared"), "{seen}");
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn an_undeclared_object_and_prefix_are_refused() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinymcp").await;
+ // tinymcp declares no blobs at all.
+ let blob = rig
+ .call("Blobs", "Get", json!({"key": "anything/at/all"}))
+ .await;
+ assert!(denied(&blob, "object-not-granted"), "{blob}");
+ // A stream outside its audit prefix.
+ let stream = rig
+ .call("Streams", "Len", json!({"stream": "someone_elses_stream"}))
+ .await;
+ assert!(denied(&stream, "target-not-granted"), "{stream}");
+ // A secret outside its prefixes, on the confidential route.
+ let secret: Value = rig
+ .probe
+ .call(
+ "Call",
+ ("Secrets", "Get", json!({"name": "someone_elses/key"}), true),
+ )
+ .await
+ .unwrap();
+ assert!(denied(&secret, "target-not-granted"), "{secret}");
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn a_peer_that_is_not_a_registry_module_is_refused() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinymcp").await;
+ let proxy = rig
+ .stranger
+ .proxy(
+ STORAGE_INTERFACE,
+ "/ai/tinyhumans/tinystorage/Documents",
+ STORAGE_INTERFACE,
+ )
+ .unwrap();
+ let refused: tinybus::Result<Value> = proxy
+ .call("Get", (get("mcp_servers", "files"),))
+ .await;
+ match refused {
+ Err(tinybus::Error::MethodFailed { name, message }) => {
+ assert_eq!(name, "ai.tinyhumans.tinystorage.Error.Denied");
+ assert_eq!(message, "unknown-caller");
+ }
+ other => panic!("expected a refusal, got {other:?}"),
+ }
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn a_blob_over_the_frame_cannot_go_inline() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinyjuice").await;
+ let key = "tinyjuice_ccr/oversized";
+ let small: Value = rig
+ .probe
+ .call("PutBlob", ("tinyjuice_ccr/small", 1024usize))
+ .await
+ .unwrap();
+ assert!(small.get("ok").is_some(), "a small inline blob works: {small}");
+ // 17 MiB of body cannot be one frame, whatever the grant says. Streams are
+ // the way (#7781); until then it must fail closed and store nothing.
+ let big: Value = rig
+ .probe
+ .call("PutBlob", (key, 17usize * 1024 * 1024))
+ .await
+ .unwrap();
+ assert!(big.get("err").is_some(), "an oversized inline blob must fail");
+ let handle = rig.backend.for_scope(&rig.scope("tinyjuice")).unwrap();
+ assert!(handle.blobs().head(key).await.unwrap().is_none());
+ // A blob key outside the CCR prefix is not the module's.
+ let outside = rig
+ .call("Blobs", "Get", json!({"key": "tinyjuice_other/x"}))
+ .await;
+ assert!(denied(&outside, "target-not-granted"), "{outside}");
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn streams_and_watches_are_refused_until_tinybus_binds_them() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinyjuice").await;
+ let put_stream = rig
+ .call(
+ "Blobs",
+ "PutStream",
+ json!({"key": "tinyjuice_ccr/streamed", "len": 17 * 1024 * 1024}),
+ )
+ .await;
+ assert!(denied(&put_stream, "unsupported-selector"), "{put_stream}");
+ let get_stream = rig
+ .call("Blobs", "GetStream", json!({"key": "tinyjuice_ccr/streamed"}))
+ .await;
+ assert!(denied(&get_stream, "unsupported-selector"), "{get_stream}");
+ let mcp = super::super::storage_grants::declared("tinymcp").unwrap();
+ assert!(!mcp.collections.is_empty());
+ // Watches belong to a document grant; tinyjuice has none, so the object is
+ // refused first. The unsupported label is pinned for a documented grant by
+ // `storage_grants_tests` and below for tinymcp.
+ let watch = rig
+ .call("Documents", "WatchOpen", json!({"collection": "mcp_servers"}))
+ .await;
+ assert!(
+ denied(&watch, "object-not-granted") || denied(&watch, "unsupported-selector"),
+ "{watch}"
+ );
+ // Nothing was written by the refused stream.
+ let handle = rig.backend.for_scope(&rig.scope("tinyjuice")).unwrap();
+ assert!(handle
+ .blobs()
+ .head("tinyjuice_ccr/streamed")
+ .await
+ .unwrap()
+ .is_none());
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn a_watch_is_unsupported_for_a_module_that_may_use_documents() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinymcp").await;
+ let watch = rig
+ .call("Documents", "WatchOpen", json!({"collection": "mcp_servers"}))
+ .await;
+ assert!(denied(&watch, "unsupported-selector"), "{watch}");
+ let close = rig
+ .call("Documents", "WatchClose", json!({"watch": "w1"}))
+ .await;
+ assert!(denied(&close, "unsupported-selector"), "{close}");
+}
+
+#[tokio::test(flavor = "multi_thread")]
+async fn every_call_is_audited_under_the_module_id_without_a_payload() {
+ let _turn = TURN.lock().await;
+ let rig = rig("tinymcp").await;
+ let _ = rig
+ .call(
+ "Documents",
+ "Put",
+ put("mcp_servers", "audited", &json!({"marker": "PAYLOAD-MARKER"})),
+ )
+ .await;
+ let _ = rig
+ .call("Documents", "Get", get("never_declared", "x"))
+ .await;
+ let trail = rig.trail.0.lock().unwrap().clone();
+ assert!(trail.iter().all(|record| record.module == "tinymcp"));
+ assert!(trail
+ .iter()
+ .any(|record| record.op == "Put" && record.outcome == "ok"));
+ assert!(trail
+ .iter()
+ .any(|record| record.outcome == "target-not-granted"));
+ assert!(!format!("{trail:?}").contains("PAYLOAD-MARKER"));
+}
diff --git a/crates/openhuman-core/src/modules/storage_host_tests.rs b/crates/openhuman-core/src/modules/storage_host_tests.rs
index 9268ba3738..5e0d4c42db 100644
--- a/crates/openhuman-core/src/modules/storage_host_tests.rs
+++ b/crates/openhuman-core/src/modules/storage_host_tests.rs
@@ -698,6 +698,9 @@ async fn every_call_is_audited_without_its_payload() {
#[path = "storage_host_bus_tests.rs"]
mod bus;
+#[path = "storage_host_cdylib_tests.rs"]
+mod cdylib;
+
async fn set_secret(rig: &Rig) -> Result<Value, HostError> {
let who = alpha();
rig.host