Skip to content

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

refactor(agent): extract prompt builders into submodules

Steven Enamakel93159b946651
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session.rs b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
index ffcf2c471b..64779ed20e 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -7,6 +7,8 @@
#[path = "runtime_session_attachment_input.rs"]
mod attachment_input;
+#[path = "runtime_session/prompt.rs"]
+mod prompt;
mod memory_ingest;
mod permanent;
mod post_commit;
@@ -291,91 +293,6 @@ impl OpenHumanTurnPrelude {
})
}
- fn build_system_prompt_tiered(&self) -> Result<crate::agent::prompts::TieredPrompt> {
- self.build_system_prompt_tiered_with_flow_archetype(None)
- }
-
- fn build_system_prompt_tiered_with_flow_archetype(
- &self,
- flow_archetype_text: Option<&str>,
- ) -> Result<crate::agent::prompts::TieredPrompt> {
- use crate::agent::prompts::{PromptContext, PromptTool};
- let surface = self
- .tool_surface
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let withheld = self.rule_withheld_tools(&surface);
- let specs = surface
- .visible_tool_specs
- .iter()
- .filter(|spec| !withheld.contains(&spec.name))
- .filter(|spec| {
- self.thread_id.is_some()
- || !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(&spec.name)
- })
- .map(|spec| spec.as_ref().clone())
- .collect::<Vec<_>>();
- let instructions = self.tool_dispatcher.prompt_instructions(&specs);
- let tool_refs = surface
- .tools
- .iter()
- .chain(surface.synthesized_tools.iter())
- .filter(|tool| {
- self.thread_id.is_some()
- || !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(
- tool.name(),
- )
- })
- .map(|tool| tool.as_ref())
- .collect::<Vec<_>>();
- let mut prompt_tools = PromptTool::from_tool_refs(tool_refs.iter().copied());
- prompt_tools.retain(|tool| !surface.permanent_tool_names.contains(tool.name.as_ref()));
- prompt_tools.retain(|tool| !withheld.contains(tool.name.as_ref()));
- let mut visible_tool_names = surface.tool_policy_session.visible_tool_names_for_prompt();
- visible_tool_names.retain(|name| !withheld.contains(name));
- visible_tool_names.retain(|name| !surface.permanent_tool_names.contains(name));
- if self.thread_id.is_none() {
- visible_tool_names.retain(|name| {
- !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(name)
- });
- }
- crate::agent::prompts::swap_deferred_for_discovery_bridge(
- &mut prompt_tools,
- &mut visible_tool_names,
- &surface.deferred_tool_names,
- );
- let agents_md = if self.config.agents_md_enabled {
- crate::agent::prompts::load_agents_md_layers(&self.workspace_dir, &self.action_dir)
- } else {
- Default::default()
- };
- let mutable = self
- .mutable
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner);
- let context = PromptContext {
- workspace_dir: &self.workspace_dir,
- model_name: &self.model_name,
- agent_id: &self.agent_definition_name,
- flow_archetype_text,
- tools: &prompt_tools,
- workflows: &mutable.workflows,
- dispatcher_instructions: &instructions,
- visible_tool_names: &visible_tool_names,
- tool_call_format: self.tool_dispatcher.tool_call_format(),
- connected_integrations: &mutable.connected_integrations,
- connected_identities_md: self.connected_identities_md(),
- user_identity: crate::security::credentials::identity::peek_credential_user_identity(),
- personality_roster: vec![],
- agents_md_global: agents_md.global,
- agents_md_local: agents_md.local,
- };
- self.context
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner)
- .build_system_prompt_tiered(&context)
- }
-
#[cfg(test)]
fn synthesized_tool_names_for_test(&self) -> std::collections::HashSet<String> {
self.tool_surface
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session/prompt.rs b/crates/openhuman-core/src/agent/session_host/runtime_session/prompt.rs
new file mode 100644
index 0000000000..9ce2474c37
--- /dev/null
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session/prompt.rs
@@ -0,0 +1,92 @@
+//! Cold prompt rendering for the host-owned session prelude.
+
+use crate::agent::prompts::{PromptContext, PromptTool, TieredPrompt};
+use anyhow::Result;
+
+use super::OpenHumanTurnPrelude;
+
+impl OpenHumanTurnPrelude {
+ pub(super) fn build_system_prompt_tiered(&self) -> Result<TieredPrompt> {
+ self.build_system_prompt_tiered_with_flow_archetype(None)
+ }
+
+ pub(super) fn build_system_prompt_tiered_with_flow_archetype(
+ &self,
+ flow_archetype_text: Option<&str>,
+ ) -> Result<TieredPrompt> {
+ let surface = self
+ .tool_surface
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ let withheld = self.rule_withheld_tools(&surface);
+ let specs = surface
+ .visible_tool_specs
+ .iter()
+ .filter(|spec| !withheld.contains(&spec.name))
+ .filter(|spec| {
+ self.thread_id.is_some()
+ || !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(&spec.name)
+ })
+ .map(|spec| spec.as_ref().clone())
+ .collect::<Vec<_>>();
+ let instructions = self.tool_dispatcher.prompt_instructions(&specs);
+ let tool_refs = surface
+ .tools
+ .iter()
+ .chain(surface.synthesized_tools.iter())
+ .filter(|tool| {
+ self.thread_id.is_some()
+ || !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(
+ tool.name(),
+ )
+ })
+ .map(|tool| tool.as_ref())
+ .collect::<Vec<_>>();
+ let mut prompt_tools = PromptTool::from_tool_refs(tool_refs.iter().copied());
+ prompt_tools.retain(|tool| !surface.permanent_tool_names.contains(tool.name.as_ref()));
+ prompt_tools.retain(|tool| !withheld.contains(tool.name.as_ref()));
+ let mut visible_tool_names = surface.tool_policy_session.visible_tool_names_for_prompt();
+ visible_tool_names.retain(|name| !withheld.contains(name));
+ visible_tool_names.retain(|name| !surface.permanent_tool_names.contains(name));
+ if self.thread_id.is_none() {
+ visible_tool_names.retain(|name| {
+ !crate::agent::tinyagents::tool_registration::is_thread_goal_tool(name)
+ });
+ }
+ crate::agent::prompts::swap_deferred_for_discovery_bridge(
+ &mut prompt_tools,
+ &mut visible_tool_names,
+ &surface.deferred_tool_names,
+ );
+ let agents_md = if self.config.agents_md_enabled {
+ crate::agent::prompts::load_agents_md_layers(&self.workspace_dir, &self.action_dir)
+ } else {
+ Default::default()
+ };
+ let mutable = self
+ .mutable
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ let context = PromptContext {
+ workspace_dir: &self.workspace_dir,
+ model_name: &self.model_name,
+ agent_id: &self.agent_definition_name,
+ flow_archetype_text,
+ tools: &prompt_tools,
+ workflows: &mutable.workflows,
+ dispatcher_instructions: &instructions,
+ visible_tool_names: &visible_tool_names,
+ tool_call_format: self.tool_dispatcher.tool_call_format(),
+ connected_integrations: &mutable.connected_integrations,
+ connected_identities_md: self.connected_identities_md(),
+ user_identity: crate::security::credentials::identity::peek_credential_user_identity(),
+ personality_roster: vec![],
+ agents_md_global: agents_md.global,
+ agents_md_local: agents_md.local,
+ };
+ self.context
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner)
+ .build_system_prompt_tiered(&context)
+ }
+}
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs b/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
index 1144ba5114..21ac7622d1 100644
--- a/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/runner.rs
@@ -41,6 +41,10 @@ use super::prompt::{
};
use super::provider::{resolve_subagent_source, user_is_signed_in_to_composio};
+#[path = "runner_prompt.rs"]
+mod runner_prompt;
+use runner_prompt::{load_typed_prompt_source, LoadedConfig};
+
/// Runtime spawn-hierarchy gate decision for one delegation hop.
///
/// `parent_def` is the resolved parent agent definition (looked up from the
@@ -529,44 +533,6 @@ fn workspace_descriptor_for_subagent(
)
}
-/// One spawn's snapshot of the host config, loaded once by [`run_subagent`].
-///
-/// `Config::load_or_init()` is **not cached** — it re-resolves the config dirs
-/// and re-reads `config.toml` on every call. `run_typed_mode` needed it in six
-/// places, so a single sub-agent spawn used to hit the disk six times and could
-/// observe six *different* configs if the file changed mid-spawn. One snapshot
-/// is both cheaper and more coherent.
-///
-/// The error is captured as a `String` rather than dropped to `Option` so the
-/// sites that log it can say why the config was unavailable; every site
-/// degrades without it.
-///
-/// Threading this in as a parameter (rather than loading it inside the runtime)
-/// is `docs/specs/plan-agents.md` Phase 3: the sub-agent runner is slated to
-/// move into TinyAgents, and a generic runtime has no config file to load.
-type LoadedConfig = Result<std::sync::Arc<crate::config::Config>, String>;
-
-/// Load and render a typed sub-agent prompt using the exact config snapshot
-/// supplied to this spawn. TinyFlows-backed builtins fetch their archetype
-/// over the module bus before the synchronous registry builder runs.
-async fn load_typed_prompt_source(
- definition: &AgentDefinition,
- config: &LoadedConfig,
- mut prompt_context: PromptContext<'_>,
-) -> Result<String, SubagentRunError> {
- #[cfg(feature = "flows")]
- let flow_archetype_text = crate::agent::prompts::flow_archetype::load_for_definition(
- config.as_ref().ok().map(|config| config.as_ref()),
- definition,
- )
- .await?;
- #[cfg(not(feature = "flows"))]
- let flow_archetype_text: Option<String> = None;
-
- prompt_context.flow_archetype_text = flow_archetype_text.as_deref();
- load_prompt_source(definition, &prompt_context)
-}
-
// ─────────────────────────────────────────────────────────────────────────────
// Typed mode — narrow prompt, filtered tools, cheaper model
// ─────────────────────────────────────────────────────────────────────────────
diff --git a/crates/openhuman-core/src/agent/subagent_host/ops/runner_prompt.rs b/crates/openhuman-core/src/agent/subagent_host/ops/runner_prompt.rs
new file mode 100644
index 0000000000..3129ef72cd
--- /dev/null
+++ b/crates/openhuman-core/src/agent/subagent_host/ops/runner_prompt.rs
@@ -0,0 +1,28 @@
+//! Scoped async preparation for typed sub-agent prompt rendering.
+
+use super::{AgentDefinition, PromptContext, SubagentRunError, load_prompt_source};
+
+/// One spawn's snapshot of the host config, loaded once by [`run_subagent`].
+///
+/// Threading it in as a parameter keeps all prompt, model and context decisions
+/// on the same profile snapshot without giving the generic runtime file access.
+pub(super) type LoadedConfig = Result<std::sync::Arc<crate::config::Config>, String>;
+
+/// Load and render a typed sub-agent prompt using this spawn's config snapshot.
+pub(super) async fn load_typed_prompt_source(
+ definition: &AgentDefinition,
+ config: &LoadedConfig,
+ mut prompt_context: PromptContext<'_>,
+) -> Result<String, SubagentRunError> {
+ #[cfg(feature = "flows")]
+ let flow_archetype_text = crate::agent::prompts::flow_archetype::load_for_definition(
+ config.as_ref().ok().map(|config| config.as_ref()),
+ definition,
+ )
+ .await?;
+ #[cfg(not(feature = "flows"))]
+ let flow_archetype_text: Option<String> = None;
+
+ prompt_context.flow_archetype_text = flow_archetype_text.as_deref();
+ load_prompt_source(definition, &prompt_context)
+}