Skip to content

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

feat(devices): track pairing agent and add agent-scoped context helpers

Steven Enamakelf475d87bc16a
diff --git a/crates/openhuman-core/src/security/devices/rpc.rs b/crates/openhuman-core/src/security/devices/rpc.rs
index 88546d9526..f4558da194 100644
--- a/crates/openhuman-core/src/security/devices/rpc.rs
+++ b/crates/openhuman-core/src/security/devices/rpc.rs
@@ -137,6 +137,8 @@ pub async fn devices_create_pairing(
core_pubkey: core_pubkey.clone(),
rpc_url: rpc_url.clone(),
expires_at: expires_at.clone(),
+ agent: crate::core::runtime::CoreContext::current()
+ .and_then(|context| context.session_agent().map(str::to_string)),
},
);
diff --git a/crates/openhuman-core/src/security/devices/types.rs b/crates/openhuman-core/src/security/devices/types.rs
index 1f2642f248..135a1bdac3 100644
--- a/crates/openhuman-core/src/security/devices/types.rs
+++ b/crates/openhuman-core/src/security/devices/types.rs
@@ -39,6 +39,11 @@ pub struct PairingSession {
pub rpc_url: Option<String>,
/// ISO 8601 timestamp when the pairing token expires.
pub expires_at: String,
+ /// The agent that started the pairing (`CoreContext::session_agent`),
+ /// when it was acting for one: the paired device is stored in, and its
+ /// tunnel frames are handled as, that agent (`devices::owner`).
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub agent: Option<String>,
}
/// Response payload for `devices_create_pairing`.
diff --git a/crates/openhuman-core/src/storage/agents.rs b/crates/openhuman-core/src/storage/agents.rs
index aa669310f4..1fbc6eaf45 100644
--- a/crates/openhuman-core/src/storage/agents.rs
+++ b/crates/openhuman-core/src/storage/agents.rs
@@ -150,6 +150,27 @@ fn agent_contexts(fallback: Option<&Arc<CoreContext>>) -> Vec<(String, Arc<CoreC
contexts.into_iter().collect()
}
+/// The context to act for `agent` under: its live context when one exists,
+/// else the current context acting for it (`CoreContext::for_agent`).
+pub fn context_for(agent: &str) -> Option<Arc<CoreContext>> {
+ let live = LIVE
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .get(agent)
+ .and_then(Weak::upgrade);
+ live.or_else(|| CoreContext::current().map(|current| current.for_agent(agent)))
+}
+
+/// Runs `fut` acting for `agent` when there is one — background work that
+/// learned whose record it is handling (a device's pairing agent, an event's
+/// publisher) re-enters that agent's scope — and as-is otherwise.
+pub async fn within_agent<F: Future>(agent: Option<&str>, fut: F) -> F::Output {
+ match agent.and_then(context_for) {
+ Some(context) => CoreContext::scope(context, fut).await,
+ None => fut.await,
+ }
+}
+
/// Runs `step` for every storage scope background work must cover: once
/// under the current context (the `local` scope, outside SaaS mode), then
/// once per known agent ([`for_each_agent`]). Each result is returned with