Skip to content

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

feat(agent): add layout module for agent embedding

Steven Enamakel1a68ee161997
diff --git a/crates/openhuman-embed/src/agent/layout.rs b/crates/openhuman-embed/src/agent/layout.rs
new file mode 100644
index 0000000000..63bad756bb
--- /dev/null
+++ b/crates/openhuman-embed/src/agent/layout.rs
@@ -0,0 +1,84 @@
+//! Where one agent's files live.
+//!
+//! Everything an agent owns sits under the runtime's workspace, keyed by the
+//! agent id, using the core's own per-profile conventions so the desktop's
+//! profile tooling and the agent harness read the same paths:
+//!
+//! ```text
+//! <root>/
+//! config.toml, auth-profiles.json, core.token runtime-wide
+//! workspace/
+//! session_db/sessions.db runtime-wide run ledger
+//! personalities/<id>/{SOUL.md, MEMORY.md, skills/} the agent's home
+//! session_raw-<id>/ its transcripts
+//! memory-<id>/, memory_tree-<id>/ its memory (dedicated)
+//! agents/<id>/action/ its default action_dir
+//! ```
+//!
+//! Under [`Workspace::Inherit`](crate::Workspace) the same per-profile paths
+//! are used inside the operator's workspace, and the default `action_dir`
+//! becomes `<action_dir>/profiles/<id>` — the desktop's own dedicated-
+//! workspace convention — so a library agent never invents a directory the
+//! operator's install does not already know about.
+
+use std::path::{Path, PathBuf};
+
+use openhuman_core::agent::profiles::{
+ effective_memory_suffix, profile_action_workspace, profile_home, profile_skills_dir,
+ session_raw_subdir_for_suffix, AgentProfile,
+};
+
+/// Resolved per-agent paths.
+#[derive(Debug, Clone, PartialEq, Eq)]
+pub struct AgentLayout {
+ /// `<workspace>/personalities/<id>/`.
+ pub home: PathBuf,
+ /// `<workspace>/personalities/<id>/skills/`.
+ pub skills: PathBuf,
+ /// The transcript directory the harness writes for this agent.
+ pub transcripts: PathBuf,
+ /// The agent's read/write root.
+ pub action_dir: PathBuf,
+}
+
+impl AgentLayout {
+ /// Lay out `profile` under `workspace_dir`, with `action_dir` either the
+ /// spec's explicit choice or the default for this runtime's workspace
+ /// kind.
+ pub(crate) fn resolve(
+ workspace_dir: &Path,
+ profile: &AgentProfile,
+ action_dir: PathBuf,
+ ) -> Self {
+ let suffix = effective_memory_suffix(profile);
+ Self {
+ home: profile_home(workspace_dir, &profile.id),
+ skills: profile_skills_dir(workspace_dir, &profile.id),
+ transcripts: workspace_dir.join(session_raw_subdir_for_suffix(&suffix)),
+ action_dir,
+ }
+ }
+
+ /// The default `action_dir` for agent `id`.
+ ///
+ /// Runtime-owned roots get `<root>/agents/<id>/action` — a sibling of the
+ /// workspace, never inside it, because `is_workspace_internal_path`
+ /// blocks agent writes beneath the workspace fail-closed. An inherited
+ /// workspace gets the desktop's `<action_dir>/profiles/<id>`.
+ pub(crate) fn default_action_dir(
+ root_dir: &Path,
+ runtime_action_dir: &Path,
+ inherited: bool,
+ id: &str,
+ ) -> PathBuf {
+ if inherited {
+ profile_action_workspace(runtime_action_dir, id)
+ } else {
+ root_dir.join("agents").join(id).join("action")
+ }
+ }
+}
+
+#[cfg(test)]
+#[path = "layout_tests.rs"]
+mod tests;
diff --git a/crates/openhuman-embed/src/agent/layout_tests.rs b/crates/openhuman-embed/src/agent/layout_tests.rs
new file mode 100644
index 0000000000..41965c0c97
--- /dev/null
+++ b/crates/openhuman-embed/src/agent/layout_tests.rs
@@ -0,0 +1,39 @@
+use super::*;
+
+fn profile(id: &str, dedicated: bool) -> AgentProfile {
+ let mut p = crate::agent::spec::blank_profile(id);
+ p.dedicated_memory = dedicated;
+ p
+}
+
+#[test]
+fn dedicated_memory_keys_transcripts_by_agent_id() {
+ let ws = Path::new("/r/workspace");
+ let layout = AgentLayout::resolve(ws, &profile("alpha", true), PathBuf::from("/r/a"));
+ assert_eq!(layout.home, Path::new("/r/workspace/personalities/alpha"));
+ assert_eq!(
+ layout.skills,
+ Path::new("/r/workspace/personalities/alpha/skills")
+ );
+ assert_eq!(layout.transcripts, Path::new("/r/workspace/session_raw-alpha"));
+ assert_eq!(layout.action_dir, Path::new("/r/a"));
+}
+
+#[test]
+fn shared_memory_uses_the_workspace_transcript_root() {
+ let ws = Path::new("/r/workspace");
+ let layout = AgentLayout::resolve(ws, &profile("alpha", false), PathBuf::from("/r/a"));
+ assert_eq!(layout.transcripts, Path::new("/r/workspace/session_raw"));
+}
+
+#[test]
+fn default_action_dir_is_a_workspace_sibling_or_a_profile_dir() {
+ assert_eq!(
+ AgentLayout::default_action_dir(Path::new("/r"), Path::new("/r/action"), false, "a"),
+ Path::new("/r/agents/a/action")
+ );
+ assert_eq!(
+ AgentLayout::default_action_dir(Path::new("/home/u/.openhuman"), Path::new("/home/u/OpenHuman/projects"), true, "a"),
+ Path::new("/home/u/OpenHuman/projects/profiles/a")
+ );
+}
diff --git a/crates/openhuman-embed/src/agent/mod.rs b/crates/openhuman-embed/src/agent/mod.rs
new file mode 100644
index 0000000000..b96cc3071e
--- /dev/null
+++ b/crates/openhuman-embed/src/agent/mod.rs
@@ -0,0 +1,193 @@
+//! A runtime-owned agent: one fully described, independently configured
+//! participant on a [`Runtime`](crate::Runtime).
+//!
+//! An [`Agent`] is a cheap clone handle over the state
+//! [`Runtime::agent`](crate::Runtime::agent) assembled from an
+//! [`AgentSpec`]: its own `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
+//! [`AgentProfile`](openhuman_core::agent::profiles::AgentProfile) (allowlists,
+//! dedicated memory and transcripts, prompt suffix), and the derived
+//! [`CoreContext`](openhuman_core::core::runtime::CoreContext) every turn
+//! dispatches under. Two agents on one runtime never read each other's
+//! settings: each turn is scoped to its own context, and the core's config
+//! loader, DomainSet gate, tool-group filter and skill discovery all read
+//! the ambient context.
+
+mod build;
+mod definition;
+mod layout;
+mod spec;
+
+pub use definition::{AgentDefinitionSpec, SandboxModeSpec, ToolScopeSpec};
+pub use layout::AgentLayout;
+pub use spec::AgentSpec;
+
+use std::path::Path;
+use std::sync::Arc;
+
+use openhuman_core::agent::harness::definition::AgentDefinition;
+use openhuman_core::agent::profiles::AgentProfile;
+use openhuman_core::config::Config;
+use openhuman_core::core::runtime::{CoreContext, CoreRuntime};
+
+use crate::harness::{Access, Provider};
+use crate::turn::{Turn, TurnOutcome, TurnTarget};
+use crate::CoreError;
+
+/// Error from instantiating or driving an [`Agent`].
+#[derive(Debug, thiserror::Error)]
+pub enum AgentError {
+ /// A turn failed. See [`CoreError`] for the distinctions.
+ #[error(transparent)]
+ Call(#[from] CoreError),
+
+ /// An agent with this id is already alive on the runtime.
+ #[error("agent id {0:?} is already registered on this runtime")]
+ DuplicateId(String),
+
+ /// The id does not match `^[a-z0-9][a-z0-9_-]{0,63}$`.
+ #[error("invalid agent id {id:?}: {reason}")]
+ InvalidId {
+ /// The offending id.
+ id: String,
+ /// Why it was refused.
+ reason: String,
+ },
+
+ /// The spec asked for a domain family or tool group the runtime did not
+ /// register. Agents can only narrow the runtime's surface.
+ #[error("agent widens the runtime's surface: {0}")]
+ WidensRuntime(String),
+
+ /// A filesystem operation laying out the agent 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 spec input could not be honoured.
+ #[error("{0}")]
+ Invalid(String),
+}
+
+/// The assembled state behind an [`Agent`], shared by every clone of it and
+/// by every [`Turn`] it issues.
+pub(crate) struct AgentInner {
+ pub(crate) id: String,
+ pub(crate) runtime: Arc<CoreRuntime>,
+ pub(crate) ctx: Arc<CoreContext>,
+ pub(crate) config: Config,
+ pub(crate) definition: AgentDefinition,
+ pub(crate) profile: AgentProfile,
+ pub(crate) provider: Provider,
+ pub(crate) access: Access,
+ pub(crate) layout: AgentLayout,
+}
+
+/// A handle to one agent on a runtime.
+///
+/// Cheap to clone; every clone drives the same agent. The id is released for
+/// reuse once the last clone is dropped. Dropping an agent does **not** remove
+/// its directories — its transcripts and memory persist with the runtime's
+/// workspace.
+#[derive(Clone)]
+pub struct Agent {
+ inner: Arc<AgentInner>,
+}
+
+impl Agent {
+ pub(crate) fn from_inner(inner: Arc<AgentInner>) -> Self {
+ Self { inner }
+ }
+
+ /// The id this agent was created with.
+ pub fn id(&self) -> &str {
+ &self.inner.id
+ }
+
+ /// Run one turn and get the reply.
+ ///
+ /// Each call starts a **new** conversation. Pass the returned
+ /// [`TurnOutcome::session_id`] to [`Agent::turn`] + [`Turn::session`] to
+ /// continue one.
+ pub async fn run(&self, message: impl Into<String>) -> Result<TurnOutcome, AgentError> {
+ self.turn(message).send().await.map_err(Into::into)
+ }
+
+ /// Begin a turn, to configure before sending.
+ ///
+ /// The agent's provider route, model and access origin are pre-applied;
+ /// anything set on the returned [`Turn`] overrides them for that turn
+ /// alone.
+ pub fn turn(&self, message: impl Into<String>) -> Turn {
+ let mut turn =
+ Turn::new(TurnTarget::Agent(Arc::clone(&self.inner)), message).with_agent_id(&self.inner.id);
+ if let Some(route) = self.inner.provider.route() {
+ turn = turn.route(route.clone());
+ }
+ if let Some(model) = self.inner.provider.model_id() {
+ turn = turn.model(model);
+ }
+ if let Some(origin) = self.inner.access.turn_origin() {
+ turn = turn.origin(origin.clone());
+ }
+ turn
+ }
+
+ /// The agent's read/write root for acting tools.
+ pub fn action_dir(&self) -> &Path {
+ &self.inner.config.action_dir
+ }
+
+ /// The runtime-wide workspace this agent's state lives under.
+ pub fn workspace_dir(&self) -> &Path {
+ &self.inner.config.workspace_dir
+ }
+
+ /// `<workspace>/personalities/<id>/` — the agent's home (SOUL.md, MEMORY.md).
+ pub fn home_dir(&self) -> &Path {
+ &self.inner.layout.home
+ }
+
+ /// `<workspace>/personalities/<id>/skills/` — where its skill bundles were
+ /// copied and the only skills root it sees besides the workspace's own.
+ pub fn skills_dir(&self) -> &Path {
+ &self.inner.layout.skills
+ }
+
+ /// Where this agent's transcripts are written
+ /// (`<workspace>/session_raw-<id>/`, or the shared `session_raw/` when
+ /// [`AgentSpec::shared_memory`] was set).
+ pub fn transcripts_dir(&self) -> &Path {
+ &self.inner.layout.transcripts
+ }
+
+ /// The provider this agent's turns run on.
+ pub fn provider(&self) -> &Provider {
+ &self.inner.provider
+ }
+
+ /// The access tier this agent's turns run with.
+ pub fn access(&self) -> &Access {
+ &self.inner.access
+ }
+
+ /// The config this agent was instantiated with, for inspection.
+ pub fn config(&self) -> &Config {
+ &self.inner.config
+ }
+}
+
+impl std::fmt::Debug for Agent {
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ f.debug_struct("Agent")
+ .field("id", &self.inner.id)
+ .finish_non_exhaustive()
+ }
+}