Skip to content

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

feat(devices): scope tunnel frames to the owning agent

Steven Enamakele756dc6bbb30
diff --git a/crates/openhuman-core/src/security/devices/bus.rs b/crates/openhuman-core/src/security/devices/bus.rs
index 979c0747c9..2b1a829b5b 100644
--- a/crates/openhuman-core/src/security/devices/bus.rs
+++ b/crates/openhuman-core/src/security/devices/bus.rs
@@ -84,7 +84,20 @@ impl EventHandler<DomainEvent> for DeviceTunnelSubscriber {
channel_id,
payload_b64,
} => {
- handle_tunnel_frame(channel_id, payload_b64).await;
+ // Handle the frame as the agent the device belongs to, so its
+ // pairing record and the RPCs it sends land in that agent's
+ // scope (`super::owner`).
+ let pending = PENDING_SESSIONS
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .get(channel_id.as_str())
+ .cloned();
+ let owner = super::owner::owner_of(channel_id, pending.as_ref()).await;
+ crate::storage::agents::within_agent(
+ owner.as_deref(),
+ handle_tunnel_frame(channel_id, payload_b64),
+ )
+ .await;
}
_ => {}
}
@@ -361,6 +374,11 @@ async fn handle_tunnel_frame(channel_id: &str, payload_b64: &str) {
&session_token_hash,
) {
Ok(device) => {
+ super::owner::remember(
+ channel_id,
+ crate::core::runtime::CoreContext::current()
+ .and_then(|context| context.session_agent().map(str::to_string)),
+ );
log::info!(
"[devices/bus] device persisted channel_id={} label={}",
device.channel_id,
diff --git a/crates/openhuman-core/src/security/devices/mod.rs b/crates/openhuman-core/src/security/devices/mod.rs
index e9127e206a..0f0f85409b 100644
--- a/crates/openhuman-core/src/security/devices/mod.rs
+++ b/crates/openhuman-core/src/security/devices/mod.rs
@@ -5,6 +5,7 @@
pub mod bus;
pub mod crypto;
+mod owner;
pub mod rpc;
pub mod schemas;
pub mod store;
diff --git a/crates/openhuman-core/src/security/devices/owner.rs b/crates/openhuman-core/src/security/devices/owner.rs
new file mode 100644
index 0000000000..559753e9b7
--- /dev/null
+++ b/crates/openhuman-core/src/security/devices/owner.rs
@@ -0,0 +1,81 @@
+//! Which agent a paired device belongs to.
+//!
+//! A device paired from inside an agent's context is stored in that agent's
+//! storage scope (`crate::storage`), and the RPCs it sends through the tunnel
+//! must run as that agent — not as the process default, which would read and
+//! write somebody else's records. The tunnel subscriber runs outside any
+//! agent, so it asks here, per frame:
+//!
+//! 1. the pending pairing session, which recorded the agent that started it;
+//! 2. the owners this process has already resolved;
+//! 3. otherwise each storage scope, for a device paired by an earlier process
+//! (`crate::storage::agents::for_each_scope`).
+//!
+//! `None` means the `local` scope (a single-user host, or no backend).
+
+use std::collections::HashMap;
+use std::sync::{LazyLock, Mutex};
+
+use super::types::PairingSession;
+
+/// Channels whose owner this process has resolved: `Some(agent)`, or `None`
+/// for `local`.
+static OWNERS: LazyLock<Mutex<HashMap<String, Option<String>>>> =
+ LazyLock::new(Default::default);
+
+/// Records that `channel_id` belongs to `agent` (`None` = `local`).
+pub(super) fn remember(channel_id: &str, agent: Option<String>) {
+ OWNERS
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .insert(channel_id.to_string(), agent);
+}
+
+fn cached(channel_id: &str) -> Option<Option<String>> {
+ OWNERS
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .get(channel_id)
+ .cloned()
+}
+
+/// The agent `channel_id` belongs to, resolved as described above. A channel
+/// no scope knows yet (a handshake still in flight) is `local`.
+pub(super) async fn owner_of(
+ channel_id: &str,
+ pending: Option<&PairingSession>,
+) -> Option<String> {
+ if let Some(session) = pending {
+ return session.agent.clone();
+ }
+ if let Some(owner) = cached(channel_id) {
+ return owner;
+ }
+ let Ok(config) = crate::config::rpc::load_config_with_timeout().await else {
+ return None;
+ };
+ let found = crate::storage::agents::for_each_scope("device owner", || async {
+ super::store::get_device(&config, channel_id)
+ .ok()
+ .flatten()
+ .is_some()
+ })
+ .await
+ .into_iter()
+ .find_map(|(agent, has_device)| has_device.then_some(agent));
+ match found {
+ Some(owner) => {
+ log::debug!(
+ "[devices/owner] channel_id={channel_id} belongs to agent={}",
+ owner.as_deref().unwrap_or("local")
+ );
+ remember(channel_id, owner.clone());
+ owner
+ }
+ None => None,
+ }
+}
+
+#[cfg(test)]
+#[path = "owner_tests.rs"]
+mod tests;