Skip to content

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

refactor(session-host): keep runtime_session.rs under its size pin

M3gA-Mindcc7db37063cb
diff --git a/crates/openhuman-core/src/agent/prompts/mod.rs b/crates/openhuman-core/src/agent/prompts/mod.rs
index 9ec694941e..93c2fc2999 100644
--- a/crates/openhuman-core/src/agent/prompts/mod.rs
+++ b/crates/openhuman-core/src/agent/prompts/mod.rs
@@ -22,7 +22,7 @@ pub use render_helpers::{
inject_workspace_file, inject_workspace_file_capped, render_ambient_environment,
render_datetime, render_identity, render_runtime, render_safety, render_subagent_system_prompt,
render_subagent_system_prompt_with_format, render_tools, render_user_identity,
- render_workspace, sync_workspace_file,
+ render_workspace, sync_workspace_file, turn_preamble,
};
#[cfg(test)]
diff --git a/crates/openhuman-core/src/agent/prompts/render_helpers.rs b/crates/openhuman-core/src/agent/prompts/render_helpers.rs
index abc4946745..5f0b76779a 100644
--- a/crates/openhuman-core/src/agent/prompts/render_helpers.rs
+++ b/crates/openhuman-core/src/agent/prompts/render_helpers.rs
@@ -19,6 +19,7 @@ mod workspace_files;
pub use section_renderers::{
current_datetime_line, render_ambient_environment, render_datetime, render_identity,
render_runtime, render_safety, render_tools, render_user_identity, render_workspace,
+ turn_preamble,
};
pub(crate) use subagent::harness_json_tool_prompt;
pub use subagent::{render_subagent_system_prompt, render_subagent_system_prompt_with_format};
diff --git a/crates/openhuman-core/src/agent/prompts/render_helpers/section_renderers.rs b/crates/openhuman-core/src/agent/prompts/render_helpers/section_renderers.rs
index bc0e371abe..8bb7d5eba0 100644
--- a/crates/openhuman-core/src/agent/prompts/render_helpers/section_renderers.rs
+++ b/crates/openhuman-core/src/agent/prompts/render_helpers/section_renderers.rs
@@ -108,6 +108,18 @@ pub fn current_datetime_line() -> String {
}
}
+/// What leads every user message: [`current_datetime_line`], then the
+/// reply-language instruction for the user's interface locale, when there is
+/// one. Per message, never in the cached prompt prefix, so a locale change
+/// applies from the next turn and the prefix stays byte-stable.
+pub fn turn_preamble(reply_language_directive: Option<&str>) -> String {
+ let now = current_datetime_line();
+ match reply_language_directive {
+ Some(directive) => format!("{now}\n{directive}"),
+ None => now,
+ }
+}
+
/// Render the `## User` identity block. Empty when
/// [`PromptContext::user_identity`] is unset or has no populated
/// fields. See issue #926.
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 4c8575c3fe..ecccccffdf 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -54,9 +54,6 @@ pub(super) struct OpenHumanSessionState {
required_output: Option<tinyagents_harness::config::RequiredOutput>,
pub(crate) pending_turn_overrides: super::types::TurnOverrides,
pub(super) active_turn_overrides: super::types::TurnOverrides,
- /// The reply-language instruction for the user's interface locale, sent
- /// on every user message until the host changes it. Not part of the
- /// cached system prompt, so a locale change takes effect on the next turn.
pub(super) reply_language_directive: Option<String>,
prelude: Option<OpenHumanTurnPrelude>,
}
@@ -533,13 +530,12 @@ impl OpenHumanTurnPrelude {
async fn enrich_request(
&self,
original_user_message: &str,
- overrides: &super::types::TurnOverrides,
- reply_language_directive: Option<&str>,
+ turn: &super::types::TurnInputs,
run_context: &mut OpenHumanRunContext,
) -> String {
let mut context = String::new();
- let active_goal = if overrides.suppress_active_goal {
+ let active_goal = if turn.overrides.suppress_active_goal {
None
} else {
let loaded = crate::agent::goals::runtime::load_for_thread(
@@ -638,11 +634,10 @@ impl OpenHumanTurnPrelude {
self.tool_dispatcher.tool_call_format(),
)
.harness_dispatcher();
- let now = crate::agent::prompts::current_datetime_line();
- match reply_language_directive {
- Some(directive) => format!("{now}\n{directive}\n\n{enriched}"),
- None => format!("{now}\n\n{enriched}"),
- }
+ format!(
+ "{}\n\n{enriched}",
+ crate::agent::prompts::turn_preamble(turn.reply_language_directive.as_deref())
+ )
}
fn parent_context(&self) -> crate::agent::harness::ParentExecutionContext {
@@ -1136,22 +1131,16 @@ impl OpenHumanSessionHost {
.context_window = context_window;
let original_user_message = user_text_with_markers(&request.input);
prelude.begin_user_effects(request);
- let (overrides, reply_language_directive) = {
- let mut state = state
- .lock()
- .unwrap_or_else(|poisoned| poisoned.into_inner());
- (
- std::mem::take(&mut state.active_turn_overrides),
- state.reply_language_directive.clone(),
- )
- };
+ let turn = state
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner())
+ .take_turn_inputs();
let current_input =
view.history.last().filter(|last| **last == request.input);
let (enriched, memory_turn) = futures::join!(
prelude.enrich_request(
&original_user_message,
- &overrides,
- reply_language_directive.as_deref(),
+ &turn,
&mut options.run_context.data,
),
// Boxed: the hook's future (config load, engine
@@ -1170,7 +1159,7 @@ impl OpenHumanSessionHost {
tinyagents_runtime::RuntimeError::Driver(error.to_string())
})?;
prelude.refresh_permanent_prefix(&mut preparation, view.prefix);
- if overrides.suppress_tools {
+ if turn.overrides.suppress_tools {
// One-off tool-less turn: must not become the
// thread's recorded tool list.
preparation.tools = Some(ToolSnapshot::default().exact());
@@ -1182,7 +1171,7 @@ impl OpenHumanSessionHost {
policy_session_id,
policy_channel,
) = prelude.current_tool_source();
- if overrides.suppress_tools {
+ if turn.overrides.suppress_tools {
current_tools = Arc::new(Vec::new());
current_synthesized_tools = Arc::new(Vec::new());
}
@@ -1199,7 +1188,7 @@ impl OpenHumanSessionHost {
middleware.transcript_snapshot = Some(transcript_snapshot);
options.run_context.data.context_middleware = Some(middleware);
options.run_context.data.current_tools = Some(current_tools);
- if !overrides.suppress_tools {
+ if !turn.overrides.suppress_tools {
options.run_context.data.deferred_tool_names =
Arc::new(prelude.current_deferred_tool_names());
}
diff --git a/crates/openhuman-core/src/agent/session_host/types.rs b/crates/openhuman-core/src/agent/session_host/types.rs
index 489c2b95aa..2adb467916 100644
--- a/crates/openhuman-core/src/agent/session_host/types.rs
+++ b/crates/openhuman-core/src/agent/session_host/types.rs
@@ -55,6 +55,26 @@ pub struct TurnOverrides {
pub suppress_transcript_autoload: bool,
}
+/// What the runtime hook hands one user turn's request enrichment: this
+/// turn's [`TurnOverrides`] (consumed) and the reply-language instruction the
+/// host armed with `set_reply_language_directive` (kept for later turns).
+#[derive(Debug, Default)]
+pub(crate) struct TurnInputs {
+ pub(crate) overrides: TurnOverrides,
+ pub(crate) reply_language_directive: Option<String>,
+}
+
+impl super::runtime_session::OpenHumanSessionState {
+ /// Takes this turn's overrides (they apply once) and copies the
+ /// reply-language instruction (it applies until the host changes it).
+ pub(super) fn take_turn_inputs(&mut self) -> TurnInputs {
+ TurnInputs {
+ overrides: std::mem::take(&mut self.active_turn_overrides),
+ reply_language_directive: self.reply_language_directive.clone(),
+ }
+ }
+}
+
impl OpenHumanSessionHost {
/// How this turn resumes its history.
///
diff --git a/scripts/ci/check-openhuman-rust-layout.mjs b/scripts/ci/check-openhuman-rust-layout.mjs
index 1d36f620fb..77f9e69513 100644
--- a/scripts/ci/check-openhuman-rust-layout.mjs
+++ b/scripts/ci/check-openhuman-rust-layout.mjs
@@ -38,7 +38,7 @@ const LEGACY_LIMIT_ENTRIES = [
// under the general 750 limit, so it needs no exception at all.
// The session-todo integration added transcript metadata construction to
// this already-exempt composition seam. Keep its allowance exact.
- ["crates/openhuman-core/src/agent/session_host/runtime_session.rs", 1477],
+ ["crates/openhuman-core/src/agent/session_host/runtime_session.rs", 1476],
// Session-host factory still assembles the product's deliberately coupled
// provider, security, memory, tool and prompt policy. Generic session
// state moved to tinyagents-runtime; this remaining composition is split in