Skip to content

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

refactor(plan_review): encapsulate socket tenant as review scope

Steven Enamakel58a146c5dab9
diff --git a/crates/openhuman-core/src/plan_review/gate.rs b/crates/openhuman-core/src/plan_review/gate.rs
index 59aedd2078..9300d2a52d 100644
--- a/crates/openhuman-core/src/plan_review/gate.rs
+++ b/crates/openhuman-core/src/plan_review/gate.rs
@@ -68,6 +68,11 @@ pub struct PlanReviewGate {
parked: Mutex<HashMap<(Tenant, String), ParkedReview>>,
}
+/// A tenant captured from trusted server context for a live socket. Its
+/// private field prevents transport payloads from manufacturing replay scope.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct SocketReviewScope(Tenant);
+
impl PlanReviewGate {
fn new(ttl: Duration) -> Self {
Self {
@@ -200,10 +205,20 @@ impl PlanReviewGate {
/// context when the socket connects; client event payloads are not identity.
pub fn parked_review_for_socket(
&self,
- socket_tenant: &Tenant,
+ socket_tenant: &SocketReviewScope,
thread_id: &str,
) -> Option<ParkedReview> {
- self.parked_review_for_thread(socket_tenant, thread_id)
+ self.parked_review_for_thread(&socket_tenant.0, thread_id)
+ }
+
+ /// Capture this socket's replay scope from the active server context.
+ /// SaaS requests with no scoped tenant fail closed; local hosts keep their
+ /// current profile/default context for the lifetime of the socket.
+ pub fn socket_scope_for_current_context(&self) -> Option<SocketReviewScope> {
+ socket_scope_from_context(
+ crate::core::runtime::DispatchCtx::ambient().tenant(),
+ crate::core::runtime::is_saas(),
+ )
}
/// Resolve a parked review by `request_id`. Returns `true` when a waiter was
@@ -251,6 +266,17 @@ impl PlanReviewGate {
}
}
+fn socket_scope_from_context(
+ tenant: Result<Tenant, crate::core::runtime::tenant::NoTenant>,
+ saas: bool,
+) -> Option<SocketReviewScope> {
+ match tenant {
+ Ok(tenant) => Some(SocketReviewScope(tenant)),
+ Err(_) if saas => None,
+ Err(_) => Some(SocketReviewScope(Tenant::default())),
+ }
+}
+
/// Removes a parked review's registry entries on drop — covers both the normal
/// return and cancellation of the parked future.
struct ParkGuard<'a> {
diff --git a/crates/openhuman-rpc/src/server/socketio.rs b/crates/openhuman-rpc/src/server/socketio.rs
index 85f6bd047c..a1adfe79a0 100644
--- a/crates/openhuman-rpc/src/server/socketio.rs
+++ b/crates/openhuman-rpc/src/server/socketio.rs
@@ -69,7 +69,7 @@ struct AuthedConnection;
/// The per-process bearer authenticates the connection but does not encode a
/// SaaS profile, so never accept tenant selection from a later event payload.
#[derive(Clone, Debug)]
-struct SocketTenant(crate::core_host::core::runtime::Tenant);
+struct SocketTenant(crate::core_host::agent::plan_review::gate::SocketReviewScope);
/// Connection-time payload the client passes via Socket.IO's `auth` field.
///
@@ -286,10 +286,9 @@ pub fn attach_socketio() -> (socketioxide::layer::SocketIoLayer, SocketIo) {
return;
}
socket.extensions.insert(AuthedConnection);
- let Some(tenant) = tenant_for_socket_connection(
- crate::core_host::core::runtime::DispatchCtx::ambient().tenant(),
- crate::core_host::core::runtime::is_saas(),
- ) else {
+ let Some(tenant) = crate::core_host::agent::plan_review::gate::global()
+ .socket_scope_for_current_context()
+ else {
log::warn!("[socketio] rejecting socket without a tenant scope in SaaS mode");
let _ = socket.clone().disconnect();
return;
@@ -1230,19 +1229,6 @@ fn replay_parked_plan_review(socket: &SocketRef, thread_id: &str) {
emit_with_aliases(socket, "plan_review_request", &payload);
}
-fn tenant_for_socket_connection(
- tenant: Result<
- crate::core_host::core::runtime::Tenant,
- crate::core_host::core::runtime::NoTenant,
- >,
- saas: bool,
-) -> Option<crate::core_host::core::runtime::Tenant> {
- match tenant {
- Ok(tenant) => Some(tenant),
- Err(_) if saas => None,
- Err(_) => Some(Default::default()),
- }
-}
fn emit_with_aliases(socket: &SocketRef, name: &str, payload: &serde_json::Value) {
let _ = socket.emit(name, payload);