Skip to content

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

refactor(session_host): rename render_current_turn_prompt to build_system_prompt

Steven Enamakel7ba04b46e15e
diff --git a/crates/openhuman-core/src/agent/session_host/mod.rs b/crates/openhuman-core/src/agent/session_host/mod.rs
index 13a6925c88..80e7431c90 100644
--- a/crates/openhuman-core/src/agent/session_host/mod.rs
+++ b/crates/openhuman-core/src/agent/session_host/mod.rs
@@ -46,6 +46,7 @@ mod resume_bound_session_tests;
mod runtime;
mod runtime_session;
pub(crate) mod stop_summary;
+mod turn;
// `pub(crate)` since issue #6014: the tool-call-cap instruction is now appended
// inside the loop by `tinyagents::middleware::FinalCallWrapUpMiddleware`, so the
// harness-assembly site has to name it. It stays the one definition — the whole
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs b/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs
index 07aec867f5..9ed2554cab 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session_tests.rs
@@ -584,7 +584,7 @@ async fn prompt_and_declared_tools(
.clone()
.expect("prelude");
let prompt = host
- .render_current_turn_prompt()
+ .build_system_prompt()
.expect("current-turn system prompt");
let snapshot = prelude
.prepare(true)
@@ -613,7 +613,7 @@ async fn debug_prompt_matches_the_system_prompt_sent_on_a_cold_turn() {
host.ensure_runtime_session().expect("runtime session");
let dumped = host
- .render_current_turn_prompt()
+ .build_system_prompt()
.expect("debug prompt surface");
host.run_single("hello").await.expect("scripted turn");
let request = model.requests().into_iter().next().expect("model request");
diff --git a/crates/openhuman-core/src/agent/session_host/turn/context.rs b/crates/openhuman-core/src/agent/session_host/turn/context.rs
new file mode 100644
index 0000000000..6c9e0b5cc3
--- /dev/null
+++ b/crates/openhuman-core/src/agent/session_host/turn/context.rs
@@ -0,0 +1,96 @@
+//! History, context, and system prompt management.
+
+use super::super::types::OpenHumanSessionHost;
+use crate::agent::prompts::{PromptContext, PromptTool};
+use tinytools::render_tool_policy_boundary;
+
+use anyhow::Result;
+
+impl OpenHumanSessionHost {
+ /// Builds the system prompt for the current turn, including tool
+ /// instructions.
+ pub fn build_system_prompt(&self) -> Result<String> {
+ // `visible_tool_specs` holds shared `Arc<ToolSpec>` leaves. Materialise
+ // the canonical dialect input for this prompt build.
+ let visible_specs_owned: Vec<tinytools::ToolSpec> = self
+ .visible_tool_specs
+ .iter()
+ .map(|spec| spec.as_ref().clone())
+ .collect();
+ let instructions = self
+ .tool_dispatcher
+ .prompt_instructions(&visible_specs_owned);
+ // Adapt the agent's whole callable surface into the shared PromptTool
+ // shape that every prompt-building call-site uses. Temporary vec
+ // borrows from the two tool `Arc`s and lives for the duration of the
+ // prompt build. The synthesised delegates belong here: the catalogue
+ // this renders is what tells the model a `delegate_*` tool exists.
+ let all_tools = self.all_tool_refs();
+ let mut prompt_tools = PromptTool::from_tool_refs(all_tools.iter().copied());
+ prompt_tools.retain(|tool| !self.permanent_tool_names.contains(tool.name.as_ref()));
+ let mut prompt_visible_tool_names =
+ self.tool_policy_session.visible_tool_names_for_prompt();
+ crate::agent::prompts::swap_deferred_for_discovery_bridge(
+ &mut prompt_tools,
+ &mut prompt_visible_tool_names,
+ &self.deferred_tool_names,
+ );
+ // Load AGENTS.md instruction layers once per system-prompt build (never
+ // re-read per turn — the caller builds the prompt once at session start
+ // and reuses the bytes, preserving the frozen-prefix / KV-cache
+ // contract). Global layer from the workspace dir; project layer from the
+ // effective action dir. Gated by `agents_md_enabled`.
+ let agents_md = if self.config.agents_md_enabled {
+ crate::agent::prompts::load_agents_md_layers(&self.workspace_dir, &self.action_dir)
+ } else {
+ tracing::debug!(
+ "[agents_md] disabled by config; skipping AGENTS.md injection for main agent"
+ );
+ tinyagents_harness::context::AgentsMdContent::default()
+ };
+ let ctx = PromptContext {
+ workspace_dir: &self.workspace_dir,
+ model_name: &self.model_name,
+ agent_id: &self.agent_definition_name,
+ tools: &prompt_tools,
+ workflows: &self.workflows,
+ dispatcher_instructions: &instructions,
+ visible_tool_names: &prompt_visible_tool_names,
+ tool_call_format: self.tool_dispatcher.tool_call_format(),
+ connected_integrations: &self.connected_integrations,
+ connected_identities_md: crate::integrations::render_connected_identities(
+ &self.dispatch_ctx(),
+ ),
+ user_identity: crate::security::credentials::identity::peek_credential_user_identity(),
+ personality_roster: vec![], // TODO: build_personality_roster(&workspace_dir)
+ agents_md_global: agents_md.global,
+ agents_md_local: agents_md.local,
+ };
+ let prompt = self.prompt_builder.build(&ctx)?;
+ // Appended, not prepended (#5704). Every line of this block is
+ // session-scoped — agent id, channel, entry point, risk level, the
+ // allowed-tool list — so putting it first moves the prompt's first
+ // diverging byte to offset 0 and costs the inference backend's
+ // automatic prefix cache everything behind it. That is the same
+ // concern that keeps DateTimeSection out of `for_subagent` and keeps
+ // the connected-server overview sorted. The model reads the whole
+ // system message either way.
+ //
+ // It also keeps the archetype/persona as the prompt's opening line,
+ // which the prepend had replaced with a constant heading for every
+ // agent.
+ let boundary = render_tool_policy_boundary(&self.tool_policy_session, 2048);
+ Ok(append_tool_policy_boundary(prompt, boundary))
+ }
+}
+
+/// Place the tool-policy boundary block relative to the assembled prompt.
+///
+/// Separated from [`OpenHumanSessionHost`] so the ordering can be tested without standing up a
+/// session: everything that decides the placement is in these two arguments.
+fn append_tool_policy_boundary(prompt: String, boundary: Option<String>) -> String {
+ match boundary {
+ Some(boundary) => format!("{prompt}\n\n{boundary}"),
+ None => prompt,
+ }
+}