Skip to content

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

refactor(user_agents): extract surface helpers into dedicated module

Steven Enamakel59058cf64062
diff --git a/crates/openhuman-core/src/user_agents/surface.rs b/crates/openhuman-core/src/user_agents/surface.rs
new file mode 100644
index 0000000000..8fa2a939d2
--- /dev/null
+++ b/crates/openhuman-core/src/user_agents/surface.rs
@@ -0,0 +1,96 @@
+//! What a user may call in SaaS mode.
+//!
+//! A user agent's context enables a few domain families
+//! ([`host::user_domains`](super::host::user_domains)), but a family is too
+//! coarse to open whole: `threads` also holds operations that start model
+//! turns or reach the host. So in a user's scope every RPC method must also
+//! be on [`USER_METHODS`], an explicit, reviewed allowlist. The registry
+//! applies it at dispatch, in the controller list and in `/schema` alike
+//! (`core::all`), so an unlisted method is indistinguishable from an absent
+//! one.
+//!
+//! The operator plane and single-user processes are unaffected.
+
+use crate::core::runtime::{is_saas, CoreContext};
+
+/// Every RPC method a user may dispatch. Grows as each family's per-user
+/// isolation lands; a method is listed only once nothing it touches is
+/// shared between users.
+pub const USER_METHODS: &[&str] = &[
+ // Conversation threads: all state lives under the agent's workspace.
+ "openhuman.threads_list",
+ "openhuman.threads_upsert",
+ "openhuman.threads_create_new",
+ "openhuman.threads_messages_list",
+ "openhuman.threads_message_append",
+ "openhuman.threads_message_update",
+ "openhuman.threads_update_labels",
+ "openhuman.threads_update_title",
+ "openhuman.threads_delete",
+ "openhuman.threads_purge",
+ "openhuman.threads_turn_state_get",
+ "openhuman.threads_turn_state_list",
+ "openhuman.threads_turn_state_history",
+ "openhuman.threads_turn_state_get_turn",
+ "openhuman.threads_turn_state_clear",
+ "openhuman.threads_token_usage",
+ "openhuman.threads_transcript_get",
+ "openhuman.threads_goal_get",
+ "openhuman.threads_todos_get",
+];
+
+/// Whether `method` may be dispatched or listed in the current scope.
+pub fn method_visible(method: &str) -> bool {
+ visible_in(
+ is_saas(),
+ CoreContext::current()
+ .as_deref()
+ .and_then(CoreContext::session_agent)
+ .is_some(),
+ method,
+ )
+}
+
+/// [`method_visible`] as a pure function of the mode and whether the scope is
+/// a user's.
+pub fn visible_in(saas: bool, user_scope: bool, method: &str) -> bool {
+ !(saas && user_scope) || USER_METHODS.contains(&method)
+}
+
+/// Thread ids a user may choose for themselves. Ids are only unique per agent,
+/// but a few prefixes mean something to the core (channel conversations,
+/// proactive jobs, sub-agent threads) and a user must not mint them.
+pub fn validate_user_thread_id(id: &str) -> Result<(), String> {
+ const RESERVED: &[&str] = &["channel:", "proactive:", "subagent:"];
+ if id.is_empty() || id.len() > 128 {
+ return Err("thread id must be 1 to 128 characters".to_string());
+ }
+ if RESERVED.iter().any(|prefix| id.starts_with(prefix)) {
+ return Err(format!("thread id `{id}` uses a reserved prefix"));
+ }
+ if !id
+ .bytes()
+ .all(|b| b.is_ascii_alphanumeric() || b == b'-' || b == b'_')
+ {
+ return Err("thread id may contain only letters, digits, '-' and '_'".to_string());
+ }
+ Ok(())
+}
+
+/// [`validate_user_thread_id`] when the current work runs for a SaaS user;
+/// otherwise every id the single-user core accepts stays accepted.
+pub fn check_thread_id(id: &str) -> Result<(), String> {
+ let user_scope = CoreContext::current()
+ .as_deref()
+ .and_then(CoreContext::session_agent)
+ .is_some();
+ if is_saas() && user_scope {
+ validate_user_thread_id(id)
+ } else {
+ Ok(())
+ }
+}
+
+#[cfg(test)]
+#[path = "surface_tests.rs"]
+mod tests;