The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
feat(runtime): add API key management module
Steven Enamakelabe66fd67b8c
diff --git a/crates/openhuman-embed/src/runtime/api_key.rs b/crates/openhuman-embed/src/runtime/api_key.rsnew file mode 100644index 0000000000..dcc91df103--- /dev/null+++ b/crates/openhuman-embed/src/runtime/api_key.rs@@ -0,0 +1,56 @@+//! The TinyHumans API key — the one credential a library runtime holds.+//!+//! Library mode has no user login. The key is installed into the runtime's+//! own credential store (beside its `config.toml`, never in it) before the+//! core boots, and from then on every managed-backend call authenticates+//! with it: managed inference as `Authorization: Bearer <key>`, SDK REST+//! routes as `x-api-key`. See `openhuman_core::security::credentials::api_key`+//! for the core side.++/// A TinyHumans API key.+///+/// Newtype so the value cannot be confused with a provider bearer or a session+/// JWT at a call site, and so `Debug` never prints it.+#[derive(Clone, PartialEq, Eq)]+pub struct ApiKey(String);++impl ApiKey {+ /// Wrap a key. Blankness is checked at+ /// [`RuntimeBuilder::build`](super::RuntimeBuilder::build), not here, so+ /// `From` can stay infallible.+ pub fn new(key: impl Into<String>) -> Self {+ Self(key.into())+ }++ /// Whether the key is empty or whitespace.+ pub fn is_blank(&self) -> bool {+ self.0.trim().is_empty()+ }++ /// The raw key, for the one place that stores it.+ pub(crate) fn expose(&self) -> &str {+ self.0.trim()+ }+}++impl From<&str> for ApiKey {+ fn from(key: &str) -> Self {+ Self::new(key)+ }+}++impl From<String> for ApiKey {+ fn from(key: String) -> Self {+ Self::new(key)+ }+}++impl std::fmt::Debug for ApiKey {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ f.write_str("ApiKey(<redacted>)")+ }+}++#[cfg(test)]+#[path = "api_key_tests.rs"]+mod tests;diff --git a/crates/openhuman-embed/src/runtime/api_key_tests.rs b/crates/openhuman-embed/src/runtime/api_key_tests.rsnew file mode 100644index 0000000000..90e9369510--- /dev/null+++ b/crates/openhuman-embed/src/runtime/api_key_tests.rs@@ -0,0 +1,17 @@+use super::*;++#[test]+fn debug_never_prints_the_key() {+ let key = ApiKey::from("th_live_supersecret");+ let debug = format!("{key:?}");+ assert!(!debug.contains("supersecret"), "leaked: {debug}");+ assert!(debug.contains("redacted"));+}++#[test]+fn blankness_is_detected_after_trimming() {+ assert!(ApiKey::from(" ").is_blank());+ assert!(ApiKey::from("").is_blank());+ assert!(!ApiKey::from(" th_x ").is_blank());+ assert_eq!(ApiKey::from(" th_x ").expose(), "th_x");+}diff --git a/crates/openhuman-embed/src/runtime/mod.rs b/crates/openhuman-embed/src/runtime/mod.rsnew file mode 100644index 0000000000..56534313bf--- /dev/null+++ b/crates/openhuman-embed/src/runtime/mod.rs@@ -0,0 +1,338 @@+//! The two-step library API: initialise one [`Runtime`], then instantiate+//! any number of independently configured [`Agent`](crate::Agent)s on it.+//!+//! ```no_run+//! # async fn demo() -> Result<(), Box<dyn std::error::Error>> {+//! use openhuman_embed::{Access, AgentSpec, Provider, Runtime, Workspace};+//!+//! // Step 1 — the runtime: features, services, backend, the API key.+//! let runtime = Runtime::builder()+//! .workspace(Workspace::dir("/var/lib/my-product/openhuman"))+//! .api_key("th_live_…")+//! .build()+//! .await?;+//!+//! // Step 2 — agents, each fully described. Nothing about one leaks into+//! // another: their own MCP servers, skills, working directory, access tier.+//! let reviewer = runtime.agent(+//! AgentSpec::new("reviewer")+//! .system_prompt("You review pull requests.")+//! .access(Access::readonly())+//! .action_dir("/srv/checkouts/pr-42"),+//! )?;+//! let fixer = runtime.agent(+//! AgentSpec::new("fixer")+//! .provider(Provider::openai_compatible("https://api.example/v1", "sk-…").model("gpt-5"))+//! .access(Access::full())+//! .action_dir("/srv/checkouts/pr-42"),+//! )?;+//!+//! let review = reviewer.run("Summarise the risks in this change.").await?;+//! let fix = fixer.turn(format!("Address: {}", review.reply)).send().await?;+//! println!("{}", fix.reply);+//! # Ok(())+//! # }+//! ```+//!+//! # What is runtime-wide and what is per-agent+//!+//! The runtime owns everything that is process-scoped in the core: the event+//! bus, the keyring and credential store, the RPC bearer, the background+//! [`ServiceSet`], the compiled-and-registered [`DomainSet`] and the API key.+//! An agent owns everything the core reads through its ambient context:+//! its `Config` (provider route and model, MCP servers, autonomy tier,+//! `action_dir`), its [`AgentDefinition`](openhuman_core::agent::harness::definition::AgentDefinition)+//! (system prompt, tool scope, sandbox mode), its profile (allowlists,+//! dedicated memory and transcripts), its skills root, its narrowed+//! `DomainSet` and [`ToolGroups`].+//!+//! Some settings are still read from the runtime's boot config by every+//! agent; see the crate README's "still runtime-wide" list. They are+//! documented rather than hidden.+//!+//! # One runtime per process+//!+//! The process-scoped state above is seeded once, by+//! [`CoreContext::init`](openhuman_core::core::runtime::context::CoreContext::init).+//! [`RuntimeBuilder::build`] returns [`RuntimeError::AlreadyRunning`] for a+//! second runtime rather than letting two share a keyring and a bus while+//! believing they had separate workspaces. Agents are the unit of+//! multiplicity, not runtimes.+//!+//! # The tokio runtime is yours+//!+//! Build it with+//! [`AGENT_WORKER_STACK_BYTES`](openhuman_core::core::runtime::AGENT_WORKER_STACK_BYTES)+//! and [`MAX_BLOCKING_THREADS`](openhuman_core::core::runtime::MAX_BLOCKING_THREADS);+//! an agent turn is a very deep async state machine and the default 2 MiB+//! worker stack overflows.++mod api_key;+mod builder;++pub use api_key::ApiKey;+pub use builder::RuntimeBuilder;++use std::collections::HashMap;+use std::path::Path;+use std::sync::atomic::AtomicBool;+use std::sync::{Arc, Mutex, Weak};++use openhuman_core::config::Config;+use openhuman_core::core::runtime::{CoreRuntime, DomainSet, ServiceSet};+use openhuman_core::tools::toolpacks::ToolGroups;++use crate::agent::{Agent, AgentError, AgentInner, AgentSpec};+use crate::harness::workspace::ResolvedWorkspace;+use crate::harness::{Access, HarnessCore, Provider, Workspace};+use crate::{Core, CoreError};++/// Guards the process-scoped core state described in the module docs.+pub(crate) static RUNTIME_LIVE: AtomicBool = AtomicBool::new(false);++/// Error from building a [`Runtime`].+#[derive(Debug, thiserror::Error)]+pub enum RuntimeError {+ /// An RPC made during the build (storing a session) failed.+ #[error(transparent)]+ Call(#[from] CoreError),++ /// The core itself failed to initialize.+ #[error("failed to build the embedded core: {0:#}")]+ Build(#[source] anyhow::Error),++ /// A filesystem operation laying out the workspace failed.+ #[error("failed to {what}")]+ Workspace {+ /// What was being attempted, phrased to complete "failed to …".+ what: &'static str,+ /// The underlying I/O error.+ #[source]+ source: std::io::Error,+ },++ /// A second [`Runtime`] was built in this process. See the module docs.+ #[error(+ "an OpenHuman runtime is already running in this process; \+ core state (keyring, event bus, domain subscribers) is process-scoped, \+ so a second one would share it. Create agents on the existing runtime."+ )]+ AlreadyRunning,++ /// A builder input could not be honoured.+ #[error("{0}")]+ Invalid(String),++ /// [`RuntimeBuilder::api_key`] was given a blank key.+ #[error("the TinyHumans API key is blank")]+ BlankApiKey,+}++/// An initialised OpenHuman core, ready to host agents.+///+/// Build once with [`Runtime::builder`]; share as `Arc<Runtime>` when several+/// parts of the host create agents. Dropping it (after every [`Agent`]) tears+/// the core down and, for [`Workspace::Ephemeral`], removes the workspace.+pub struct Runtime {+ core: Option<Core>,+ /// Held for its `Drop`: an ephemeral workspace lives exactly as long as+ /// the runtime that owns it.+ workspace: ResolvedWorkspace,+ /// The config every agent starts from. Already carries the runtime-wide+ /// defaults (backend URL, access, provider model, supplied overrides).+ base_config: Config,+ /// Where `Workspace::Inherit` resolved to, for the per-agent layout rule.+ inherited: bool,+ domains: DomainSet,+ tool_groups: ToolGroups,+ provider: Provider,+ access: Access,+ has_api_key: bool,+ agents: Mutex<HashMap<String, Weak<AgentInner>>>,+}++impl Runtime {+ /// Start configuring a runtime.+ pub fn builder() -> RuntimeBuilder {+ RuntimeBuilder::new()+ }++ /// Instantiate an agent on this runtime.+ ///+ /// Lays out the agent's directories, copies its skills, assembles its+ /// `Config` from the runtime's base plus the spec, and derives its+ /// [`CoreContext`](openhuman_core::core::runtime::CoreContext). No turn+ /// runs and no RPC is made. Ids are unique per runtime for as long as the+ /// agent (any clone of it) is alive.+ pub fn agent(&self, spec: AgentSpec) -> Result<Agent, AgentError> {+ let id = spec.id().to_string();+ {+ let mut agents = self.agents.lock().unwrap_or_else(|e| e.into_inner());+ agents.retain(|_, weak| weak.strong_count() > 0);+ if agents.contains_key(&id) {+ return Err(AgentError::DuplicateId(id));+ }+ }+ let inner = crate::agent::build::instantiate(self, spec)?;+ let inner = Arc::new(inner);+ self.agents+ .lock()+ .unwrap_or_else(|e| e.into_inner())+ .insert(id.clone(), Arc::downgrade(&inner));+ log::debug!("[embed][runtime] agent registered id={id}");+ Ok(Agent::from_inner(inner))+ }++ /// Ids of the agents currently alive on this runtime.+ pub fn agent_ids(&self) -> Vec<String> {+ let mut agents = self.agents.lock().unwrap_or_else(|e| e.into_inner());+ agents.retain(|_, weak| weak.strong_count() > 0);+ let mut ids: Vec<String> = agents.keys().cloned().collect();+ ids.sort();+ ids+ }++ /// Typed access to non-turn core domains (config, auth, medulla).+ ///+ /// Turns belong to agents: this facade deliberately exposes neither the+ /// orchestrator agent nor the raw runtime, so no turn can bypass an+ /// agent's provider route and access tier.+ pub fn core(&self) -> HarnessCore<'_> {+ HarnessCore::new(self.core_ref())+ }++ /// The directory holding `config.toml`, the credential store and, for+ /// runtime-owned workspaces, the `workspace/` and `agents/` trees.+ pub fn root_dir(&self) -> &Path {+ self.base_config+ .config_path+ .parent()+ .unwrap_or_else(|| Path::new(""))+ }++ /// The runtime-wide workspace: session database, shared memory, and every+ /// agent's `personalities/<id>/` home and `session_raw-<id>/` transcripts.+ pub fn workspace_dir(&self) -> &Path {+ &self.base_config.workspace_dir+ }++ /// Domain families registered at build time. Agents may only narrow this.+ pub fn domains(&self) -> DomainSet {+ self.domains+ }++ /// Tool-group disclosure agents inherit unless they narrow it.+ pub fn tool_groups(&self) -> &ToolGroups {+ &self.tool_groups+ }++ /// Background services selected at build time.+ pub fn services(&self) -> ServiceSet {+ self.core_ref().raw().services()+ }++ /// Whether a TinyHumans API key was installed.+ pub fn has_api_key(&self) -> bool {+ self.has_api_key+ }++ pub(crate) fn core_ref(&self) -> &Core {+ self.core+ .as_ref()+ .expect("runtime core is present until drop")+ }++ pub(crate) fn core_runtime(&self) -> &Arc<CoreRuntime> {+ self.core_ref().raw()+ }++ pub(crate) fn base_config(&self) -> &Config {+ &self.base_config+ }++ pub(crate) fn inherited_workspace(&self) -> bool {+ self.inherited+ }++ pub(crate) fn default_provider(&self) -> &Provider {+ &self.provider+ }++ pub(crate) fn default_access(&self) -> &Access {+ &self.access+ }++ #[allow(clippy::too_many_arguments)]+ pub(crate) fn new(+ core: Core,+ workspace: ResolvedWorkspace,+ base_config: Config,+ inherited: bool,+ domains: DomainSet,+ tool_groups: ToolGroups,+ provider: Provider,+ access: Access,+ has_api_key: bool,+ ) -> Self {+ Self {+ core: Some(core),+ workspace,+ base_config,+ inherited,+ domains,+ tool_groups,+ provider,+ access,+ has_api_key,+ agents: Mutex::new(HashMap::new()),+ }+ }+}++impl Drop for Runtime {+ fn drop(&mut self) {+ // Drop the core while the process slot is still claimed. Releasing it+ // first lets another builder initialize process-scoped state while+ // this runtime's keyring, bearer, event bus and subscribers are live.+ drop(self.core.take());+ // For an ephemeral workspace, take ownership of the temp path and+ // remove it with a short retry. The core's memory/session writers keep+ // running a moment after a turn returns and can recreate workspace+ // subdirectories while `TempDir`'s own drop-time removal is racing+ // them, leaving an empty directory behind. A bounded retry lets those+ // writes settle before giving up.+ if let Some(temp) = self.workspace._temp.take() {+ let root = temp.keep();+ let mut quiet_passes = 0;+ for _ in 0..20 {+ match std::fs::remove_dir_all(&root) {+ Ok(()) => quiet_passes += 1,+ Err(error) if error.kind() == std::io::ErrorKind::NotFound => {+ quiet_passes += 1;+ }+ Err(_) => quiet_passes = 0,+ }+ if quiet_passes >= 5 {+ break;+ }+ std::thread::sleep(std::time::Duration::from_millis(50));+ }+ let _ = std::fs::remove_dir_all(&root);+ }+ RUNTIME_LIVE.store(false, std::sync::atomic::Ordering::Release);+ log::debug!("[embed][runtime] released");+ }+}++impl std::fmt::Debug for Runtime {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ // Resolved paths and a provider bearer are both in here.+ f.debug_struct("Runtime")+ .field("has_api_key", &self.has_api_key)+ .field("domains", &self.domains)+ .finish_non_exhaustive()+ }+}++/// `Workspace` re-exported next to the builder that takes it.+pub use crate::harness::Workspace as RuntimeWorkspace;