Skip to content

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

refactor(security): expose embedding scope module

Steven Enamakel6d257175a79c
diff --git a/crates/openhuman-core/src/inference/embedding_host/factory.rs b/crates/openhuman-core/src/inference/embedding_host/factory.rs
index 8fe9d1b98a..b125473646 100644
--- a/crates/openhuman-core/src/inference/embedding_host/factory.rs
+++ b/crates/openhuman-core/src/inference/embedding_host/factory.rs
@@ -9,10 +9,11 @@ use tinyinference_embeddings::factory::{
};
use tinyinference_embeddings::EmbeddingModel;
-use super::cloud::{credential_scope, UnavailableEmbeddingModel};
+use super::cloud::UnavailableEmbeddingModel;
use crate::inference::provider::factory::PROVIDER_OPENHUMAN;
use crate::inference::registry::EmbeddingRequest;
use crate::security::approval::ApprovalChatContext;
+use crate::security::credentials::embedding_scope::credential_scope;
fn settings(
provider: &str,
diff --git a/crates/openhuman-core/src/security/credentials/embedding_scope.rs b/crates/openhuman-core/src/security/credentials/embedding_scope.rs
new file mode 100644
index 0000000000..ee5fe71491
--- /dev/null
+++ b/crates/openhuman-core/src/security/credentials/embedding_scope.rs
@@ -0,0 +1,66 @@
+use std::path::{Path, PathBuf};
+
+use crate::config::Config;
+
+/// Resolve the managed embedder's credential directory and encryption policy.
+/// Callers with a config use its own state directory and encryption setting;
+/// callers without one use the process's active user scope and default to
+/// encrypted credentials.
+pub(crate) fn credential_scope(config: Option<&Config>) -> (PathBuf, bool) {
+ match config {
+ Some(config) => (super::state_dir_from_config(config), config.secrets.encrypt),
+ None => (default_state_dir(), true),
+ }
+}
+
+/// Resolve the process credential scope for call sites without a `Config`.
+/// Workspace overrides follow the same config-dir mapping as config loading;
+/// otherwise credentials live below the active user (or the pre-login user).
+fn default_state_dir() -> PathBuf {
+ log::debug!("[credentials] default managed credential scope: resolving");
+ if let Some(workspace) = std::env::var_os("OPENHUMAN_WORKSPACE")
+ .filter(|value| !value.is_empty())
+ .map(PathBuf::from)
+ {
+ log::debug!(
+ "[credentials] default managed credential scope uses OPENHUMAN_WORKSPACE-derived config dir"
+ );
+ return env_workspace_state_dir(&workspace);
+ }
+
+ let root = crate::config::default_root_openhuman_dir().unwrap_or_else(|error| {
+ log::warn!(
+ "[credentials] could not resolve the openhuman root dir ({error}); \
+ falling back to a relative .openhuman path"
+ );
+ PathBuf::from(".openhuman")
+ });
+
+ let user_id = crate::config::read_active_user_id(&root);
+ log::debug!(
+ "[credentials] default managed credential scope resolved to user-scoped dir (active_user_present={})",
+ user_id.is_some()
+ );
+ user_scoped_state_dir(&root, user_id.as_deref())
+}
+
+/// Resolve a workspace override through the same workspace→config-dir mapping
+/// as config loading. In the legacy layout this is the sibling `.openhuman`
+/// directory containing `auth-profiles.json`, rather than the workspace itself.
+fn env_workspace_state_dir(workspace: &Path) -> PathBuf {
+ let (config_dir, _workspace_dir) = crate::config::resolve_config_dir_for_workspace(workspace);
+ config_dir
+}
+
+/// Resolve the active user's credential directory, using the pre-login user
+/// when no account is active.
+fn user_scoped_state_dir(root: &Path, active_user_id: Option<&str>) -> PathBuf {
+ crate::config::user_openhuman_dir(
+ root,
+ active_user_id.unwrap_or(crate::config::PRE_LOGIN_USER_ID),
+ )
+}
+
+#[cfg(test)]
+#[path = "embedding_scope_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-core/src/security/credentials/mod.rs b/crates/openhuman-core/src/security/credentials/mod.rs
index 30fe16ae91..151bfeca2c 100644
--- a/crates/openhuman-core/src/security/credentials/mod.rs
+++ b/crates/openhuman-core/src/security/credentials/mod.rs
@@ -4,6 +4,7 @@ pub mod api_key;
pub mod bus;
mod core;
pub mod credential_ref;
+pub(crate) mod embedding_scope;
pub mod http_creds;
pub mod identity;
pub mod jwt;