Skip to content

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

refactor(session_host): extract prefix snapshot construction

Steven Enamakel2537e3d6dfd0
diff --git a/crates/openhuman-core/src/agent/session_host/mod.rs b/crates/openhuman-core/src/agent/session_host/mod.rs
index 8193f6f9a6..4dcd5e5308 100644
--- a/crates/openhuman-core/src/agent/session_host/mod.rs
+++ b/crates/openhuman-core/src/agent/session_host/mod.rs
@@ -37,6 +37,7 @@ mod driver;
mod factory;
mod hooks;
mod policy;
+mod prefix_snapshot;
mod runtime;
mod runtime_session;
#[cfg(test)]
diff --git a/crates/openhuman-core/src/agent/session_host/prefix_snapshot.rs b/crates/openhuman-core/src/agent/session_host/prefix_snapshot.rs
new file mode 100644
index 0000000000..f96b957935
--- /dev/null
+++ b/crates/openhuman-core/src/agent/session_host/prefix_snapshot.rs
@@ -0,0 +1,37 @@
+//! The frozen system-prompt prefix a session hands the runtime.
+//!
+//! The prompt is rendered once per session as cache tiers (see
+//! `agent::prompts::TieredPrompt::system_messages`) and sent as one leading
+//! system message per tier. These two helpers turn a rendered prompt into
+//! that prefix and recover it from a resumed transcript, so `runtime_session`
+//! never has to know how many messages a prefix is.
+
+use tinyagents_runtime::PrefixSnapshot;
+use tinyinference_llm::message::Message;
+
+use crate::agent::prompts::TieredPrompt;
+
+/// One system message per cache tier (stable+context, then volatile). The
+/// harness gives each its own cacheable segment, so a rewritten memory file or
+/// a newly connected service changes the second segment and leaves the first
+/// byte-identical for the provider's prefix cache.
+pub(super) fn tiered_prefix_snapshot(tiered: &TieredPrompt) -> PrefixSnapshot {
+ let messages = tiered.system_messages();
+ tracing::debug!(
+ segments = messages.len(),
+ bytes = ?messages.iter().map(String::len).collect::<Vec<_>>(),
+ "[session] frozen system prompt as tiered segments"
+ );
+ PrefixSnapshot::new(messages.into_iter().map(Message::system).collect())
+}
+
+/// The frozen prefix of a resumed transcript: every leading system message,
+/// not only the first, because the prompt is sent as one message per tier.
+pub(super) fn leading_system_prefix(history: &[Message]) -> Option<PrefixSnapshot> {
+ let leading: Vec<Message> = history
+ .iter()
+ .take_while(|message| matches!(message, Message::System(_)))
+ .cloned()
+ .collect();
+ (!leading.is_empty()).then(|| PrefixSnapshot::new(leading))
+}
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 d9e837fa93..750d29f3a7 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -194,21 +194,8 @@ impl OpenHumanTurnPrelude {
};
let prefix = if cold {
let learned = self.fetch_learned_context().await;
- // One system message per cache tier (stable+context, then
- // volatile): the harness gives each its own cacheable segment, so
- // a rewritten memory file or a newly connected service changes the
- // second segment and leaves the first byte-identical for the
- // provider's prefix cache.
let tiered = self.build_system_prompt_tiered(learned)?;
- let messages = tiered.system_messages();
- tracing::debug!(
- segments = messages.len(),
- bytes = ?messages.iter().map(String::len).collect::<Vec<_>>(),
- "[session] frozen system prompt as tiered segments"
- );
- Some(PrefixSnapshot::new(
- messages.into_iter().map(Message::system).collect(),
- ))
+ Some(super::prefix_snapshot::tiered_prefix_snapshot(&tiered))
} else {
None
};
@@ -1563,18 +1550,9 @@ impl OpenHumanSessionHost {
let state = state.clone();
let request_base_len = view.history.len()
+ usize::from(view.history.last() != Some(&request.input));
- // The frozen prefix is every leading system message, not
- // only the first: the prompt is sent as one message per
- // cache tier (see `prepare`).
- let resumed_prefix = view.resumed.then(|| {
- let leading: Vec<Message> = view
- .history
- .iter()
- .take_while(|message| matches!(message, Message::System(_)))
- .cloned()
- .collect();
- (!leading.is_empty()).then(|| PrefixSnapshot::new(leading))
- });
+ let resumed_prefix = view
+ .resumed
+ .then(|| super::prefix_snapshot::leading_system_prefix(&view.history));
Box::pin(async move {
let transcript_snapshot =
crate::agent::tinyagents::TranscriptSnapshotSink::default();