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(storage): add host invocation module for storage operations
Steven Enamakela96bc491d5af
diff --git a/crates/openhuman-core/src/modules/storage_host_invocation.rs b/crates/openhuman-core/src/modules/storage_host_invocation.rsnew file mode 100644index 0000000000..eb2edddc98--- /dev/null+++ b/crates/openhuman-core/src/modules/storage_host_invocation.rs@@ -0,0 +1,200 @@+//! Invocation-scoped, Documents-only storage callbacks for module imports.+//!+//! Unlike the long-lived storage host, this endpoint is bound to the exact+//! ready module instance that will make one import call. Its `Storage` is+//! supplied by the caller and therefore retains the captured profile and+//! import-claim conditions. The endpoint has no Secrets capability and does+//! not expose data operations on the other ports.++use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};+use std::sync::Arc;++use async_trait::async_trait;+use serde_json::{json, Value};+use tinybus::module::ModuleRuntime;+use tinybus::{BusName, ObjectPath};+use tinystoragedrivers_api::BlobReader;+use tinystoragedrivers_bus::{+ BlobCapabilities, BlobKeyRequest, BlobStreamHead, CapabilitiesReply, KvCapabilitiesReply,+ PutBlobStreamRequest, QueueFeaturesReply, StorageFailure, StorageKind, WatchChanged,+};+use tinystoragedrivers_busproxy::{+ Admission, HostFailure, Object, ServedStorage, StorageAddress, StorageCall, StorageEndpoint,+ StorageServer,+};+use tokio::sync::broadcast;++use crate::storage::Storage;++static NEXT_INVOCATION: AtomicU64 = AtomicU64::new(1);++/// The callback address fields used by the transport-neutral owner request.+#[derive(Clone, Debug, PartialEq, Eq)]+pub(crate) struct CallbackAddress {+ pub host: String,+ pub object_root: String,+}++/// Owns an invocation's temporary Documents endpoint and its registrations.+///+/// Keep this value alive for the entire module RPC. Dropping it closes dispatch+/// before the TSD guard unregisters the invocation's paths.+pub(crate) struct InvocationStorageBinding {+ address: CallbackAddress,+ active: Arc<AtomicBool>,+ served: Option<ServedStorage>,+}++impl InvocationStorageBinding {+ /// Serve the supplied claim-bound storage to this exact, currently ready+ /// module instance.+ pub(crate) async fn serve(+ runtime: &ModuleRuntime,+ module_descriptor_name: &str,+ storage: Storage,+ ) -> anyhow::Result<Self> {+ let expected_peer = runtime+ .host()+ .authenticated_peer(module_descriptor_name)+ .ok_or_else(|| anyhow::anyhow!("module is not a live admitted instance"))?;+ let connection = runtime.connection();+ let host = connection+ .unique_name()+ .ok_or_else(|| anyhow::anyhow!("module runtime connection has no unique bus name"))?;+ let serial = NEXT_INVOCATION+ .fetch_update(Ordering::Relaxed, Ordering::Relaxed, |next| next.checked_add(1))+ .map_err(|_| anyhow::anyhow!("invocation storage address space is exhausted"))?;+ let object_root = ObjectPath::new(format!(+ "{}/call-{serial}",+ tinystoragedrivers_busproxy::INVOCATION_OBJECT_ROOT+ ))?;+ let address = StorageAddress::try_new(host, object_root)?;+ let active = Arc::new(AtomicBool::new(true));+ let endpoint = Arc::new(DocumentsOnlyEndpoint {+ expected_peer,+ active: Arc::clone(&active),+ server: StorageServer::new(storage),+ });+ let served =+ tinystoragedrivers_busproxy::serve_at_address(connection, address.clone(), endpoint)+ .await?;+ Ok(Self {+ address: CallbackAddress {+ host: address.host().to_string(),+ object_root: address.object_root().to_string(),+ },+ active,+ served: Some(served),+ })+ }++ /// The address for the owner request. It conveys routing only; it carries+ /// no scope, grant, or write-condition authority.+ pub(crate) fn address(&self) -> &CallbackAddress {+ &self.address+ }++ /// Synchronously revoke calls and unregister the invocation paths.+ pub(crate) async fn close(mut self) -> anyhow::Result<()> {+ self.active.store(false, Ordering::Release);+ if let Some(served) = self.served.take() {+ served.shutdown().await?;+ }+ Ok(())+ }+}++impl Drop for InvocationStorageBinding {+ fn drop(&mut self) {+ self.active.store(false, Ordering::Release);+ // `ServedStorage::drop` schedules path removal on the module runtime.+ // Keeping it in this binding ties registration lifetime to the import+ // call, including error and cancellation paths.+ self.served.take();+ }+}++struct DocumentsOnlyEndpoint {+ expected_peer: BusName,+ active: Arc<AtomicBool>,+ server: StorageServer,+}++impl DocumentsOnlyEndpoint {+ fn authorize(&self, sender: Option<&BusName>) -> Result<(), HostFailure> {+ if !self.active.load(Ordering::Acquire) {+ return Err(HostFailure::Denied("storage-endpoint-closed"));+ }+ match sender {+ Some(sender) if sender == &self.expected_peer => Ok(()),+ Some(_) => Err(HostFailure::Denied("wrong-module-instance")),+ None => Err(HostFailure::Denied("unauthenticated-caller")),+ }+ }++ fn unsupported() -> HostFailure {+ HostFailure::Storage(StorageFailure {+ kind: StorageKind::Unsupported,+ })+ }++ fn denied() -> HostFailure {+ HostFailure::Denied("documents-only-invocation")+ }+}++#[async_trait]+impl StorageEndpoint for DocumentsOnlyEndpoint {+ async fn call(&self, call: StorageCall<'_>) -> Result<Value, HostFailure> {+ // Authenticate before even answering capability negotiation. An+ // address is routing information, never proof of module identity.+ self.authorize(call.sender)?;+ match (call.object, call.selector) {+ (Object::Documents, _) => self+ .server+ .handle(+ Admission::Private,+ Object::Documents.as_str(),+ call.selector,+ call.request,+ )+ .await+ .map_err(HostFailure::Storage),+ (Object::Streams, "Capabilities") => Ok(json!(CapabilitiesReply::default())),+ (Object::Blobs, "Capabilities") => Ok(json!(BlobCapabilities::default())),+ (Object::Kv, "Capabilities") => Ok(json!(KvCapabilitiesReply::default())),+ (Object::Queues, "Features") => Ok(json!(QueueFeaturesReply::default())),+ (Object::Secrets, _) => Err(Self::unsupported()),+ (Object::Streams | Object::Blobs | Object::Kv | Object::Queues, _) => {+ Err(Self::denied())+ }+ }+ }++ async fn put_blob_stream(+ &self,+ sender: Option<&BusName>,+ _head: PutBlobStreamRequest,+ _body: BlobReader,+ ) -> Result<Value, HostFailure> {+ self.authorize(sender)?;+ Err(Self::denied())+ }++ async fn get_blob_stream(+ &self,+ sender: Option<&BusName>,+ _request: BlobKeyRequest,+ ) -> Result<Option<(BlobStreamHead, BlobReader)>, HostFailure> {+ self.authorize(sender)?;+ Err(Self::denied())+ }++ fn watch_signals(&self, _sender: &BusName) -> Option<broadcast::Receiver<WatchChanged>> {+ None+ }+}++#[cfg(test)]+#[path = "storage_host_invocation_tests.rs"]+mod tests;diff --git a/crates/openhuman-core/src/modules/storage_host_invocation_tests.rs b/crates/openhuman-core/src/modules/storage_host_invocation_tests.rsnew file mode 100644index 0000000000..075f6b4525--- /dev/null+++ b/crates/openhuman-core/src/modules/storage_host_invocation_tests.rs@@ -0,0 +1,130 @@+use super::*;+use crate::storage::StorageBackend;+use tinystoragedrivers_busproxy::{Object, StorageCall};++fn endpoint() -> (DocumentsOnlyEndpoint, BusName) {+ let backend = crate::storage::test_support::memory_backend();+ let scope = crate::storage::Scope::new("local").unwrap();+ let storage = backend.for_scope(&scope).unwrap();+ let expected_peer = BusName::new(":1.7").unwrap();+ (+ DocumentsOnlyEndpoint {+ expected_peer: expected_peer.clone(),+ active: Arc::new(AtomicBool::new(true)),+ server: StorageServer::new(storage),+ },+ expected_peer,+ )+}++async fn call(+ endpoint: &DocumentsOnlyEndpoint,+ sender: Option<&BusName>,+ object: Object,+ selector: &str,+ request: Value,+) -> Result<Value, HostFailure> {+ endpoint+ .call(StorageCall {+ object,+ selector,+ request,+ sender,+ admission: Admission::Private,+ })+ .await+}++#[tokio::test]+async fn callback_authenticates_before_metadata_and_restricts_ports() {+ let (endpoint, peer) = endpoint();+ let attacker = BusName::new(":1.8").unwrap();++ assert_eq!(+ call(+ &endpoint,+ Some(&attacker),+ Object::Streams,+ "Capabilities",+ json!(null),+ )+ .await,+ Err(HostFailure::Denied("wrong-module-instance"))+ );+ assert_eq!(+ call(&endpoint, None, Object::Blobs, "Capabilities", json!(null),).await,+ Err(HostFailure::Denied("unauthenticated-caller"))+ );++ let stream_caps = call(+ &endpoint,+ Some(&peer),+ Object::Streams,+ "Capabilities",+ json!(null),+ )+ .await+ .unwrap();+ assert_eq!(stream_caps, json!({ "capabilities": [] }));+ assert_eq!(+ call(+ &endpoint,+ Some(&peer),+ Object::Blobs,+ "Get",+ json!({"key": "secret"}),+ )+ .await,+ Err(HostFailure::Denied("documents-only-invocation"))+ );+ assert_eq!(+ call(+ &endpoint,+ Some(&peer),+ Object::Secrets,+ "Capabilities",+ json!(null),+ )+ .await,+ Err(DocumentsOnlyEndpoint::unsupported())+ );+}++#[tokio::test]+async fn callback_documents_route_uses_the_captured_storage() {+ let (endpoint, peer) = endpoint();++ let result = call(+ &endpoint,+ Some(&peer),+ Object::Documents,+ "Get",+ json!({"collection": "juice.savings", "document_id": "current"}),+ )+ .await;++ assert!(matches!(+ result,+ Err(HostFailure::Storage(StorageFailure {+ kind: StorageKind::NotFound+ }))+ ));+}++#[tokio::test]+async fn dropped_invocation_refuses_calls_already_in_flight_at_endpoint() {+ let (endpoint, peer) = endpoint();+ endpoint.active.store(false, Ordering::Release);++ assert_eq!(+ call(+ &endpoint,+ Some(&peer),+ Object::Documents,+ "Capabilities",+ json!(null),+ )+ .await,+ Err(HostFailure::Denied("storage-endpoint-closed"))+ );+}