Skip to content

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

fix(cache): keep compacted summaries outside frozen prefix

Steven Enamakel27f9e0d64376
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 d0d387b2bc..b301f73f02 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -1307,7 +1307,7 @@ impl OpenHumanSessionHost {
context.workspace = self.workspace_descriptor.clone();
let cancellation = context.cancellation.clone();
let root_config = context.root_run_config("openhuman-session");
- let options = TurnOptions {
+ let mut options = TurnOptions {
request_id: crate::agent::turn_origin::current_request_id(),
thread_id: self.thread_id.clone(),
stream: self.on_progress.is_some(),
@@ -1360,6 +1360,19 @@ impl OpenHumanSessionHost {
}
}
}
+ // Resume restores the exact leading prompt messages from the durable
+ // transcript. Carry their count to the cache stamper: a later System
+ // compaction summary may be adjacent, but is not a frozen prompt tier.
+ let frozen_prefix_len = self
+ .runtime_session
+ .as_ref()
+ .expect("runtime session initialized")
+ .prefix_snapshot()
+ .messages()
+ .len();
+ if frozen_prefix_len > 0 {
+ options.run_context.data.cacheable_system_prefix_len = Some(frozen_prefix_len);
+ }
let outcome = self
.runtime_session
.as_mut()
diff --git a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
index 0a935431d6..7c7f7148c8 100644
--- a/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/host/run_context.rs
@@ -301,6 +301,9 @@ pub struct OpenHumanRunContext {
/// the registry that recovers positional / code-style calls; `Auto`
/// (the default) leaves the harness to choose from the model profile.
pub(crate) tool_dialect: tinyagents_harness::config::ToolDispatcher,
+ /// Number of frozen system tiers restored from the session prefix. A
+ /// later System compaction summary remains outside this cacheable prefix.
+ pub(crate) cacheable_system_prefix_len: Option<usize>,
}
impl Default for OpenHumanRunContext {
@@ -341,6 +344,7 @@ impl OpenHumanRunContext {
session_sidecar: Arc::new(Mutex::new(SessionTurnSidecar::default())),
required_output: None,
tool_dialect: tinyagents_harness::config::ToolDispatcher::Auto,
+ cacheable_system_prefix_len: None,
}
}
@@ -431,6 +435,7 @@ impl OpenHumanRunContext {
child.parent_subagent_usage = Some(self.subagent_usage.clone());
child.subagent_usage = Arc::new(Mutex::new(Vec::new()));
child.resolved_route = Arc::new(Mutex::new(None));
+ child.cacheable_system_prefix_len = None;
child
}
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/prompt_cache.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/prompt_cache.rs
index 6a612c1880..9125508bf8 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/prompt_cache.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/prompt_cache.rs
@@ -85,11 +85,22 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
// `runtime_session::prepare`), so a rewritten volatile tier shows up
// as a change to `system.1` while `system` keeps its id and the
// layout guard can say which tier moved.
- let leading_system = request
+ let observed_leading_system = request
.messages
.iter()
.take_while(|m| matches!(m, TaMessage::System(_)))
.count();
+ let leading_system = match ctx.data.cacheable_system_prefix_len {
+ Some(frozen) => frozen.min(observed_leading_system),
+ None => {
+ // A new session renders its prefix during the first turn's
+ // prepare hook, after the run context was constructed. Learn
+ // that initial tier count once; later System summaries do not
+ // become new cacheable tiers within the run.
+ ctx.data.cacheable_system_prefix_len = Some(observed_leading_system);
+ observed_leading_system
+ }
+ };
for index in 0..leading_system {
segments.push(PromptSegment {
id: tinyagents_harness::prompt::system_segment_id(index),
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs
index 17d1f7cc8f..261e6b9042 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware_tool_output_tests.rs
@@ -291,6 +291,30 @@ async fn prompt_cache_segments_ignore_system_nudges_after_the_user_turn() {
assert_eq!(before.prompt_fingerprint, after.prompt_fingerprint);
}
+#[tokio::test]
+async fn prompt_cache_segments_do_not_promote_a_compaction_summary() {
+ let mw = PromptCacheSegmentMiddleware;
+ let mut run = ctx();
+ run.data.cacheable_system_prefix_len = Some(2);
+ let mut before = ModelRequest::new(vec![
+ TaMessage::system("stable"),
+ TaMessage::system("context"),
+ TaMessage::user("first"),
+ ]);
+ let mut after = ModelRequest::new(vec![
+ TaMessage::system("stable"),
+ TaMessage::system("context"),
+ TaMessage::system("changing history summary"),
+ TaMessage::user("later"),
+ ]);
+
+ mw.before_model(&mut run, &(), &mut before).await.unwrap();
+ mw.before_model(&mut run, &(), &mut after).await.unwrap();
+
+ assert_eq!(before.cache_segments, after.cache_segments);
+ assert_eq!(before.prompt_fingerprint, after.prompt_fingerprint);
+}
+
#[tokio::test]
async fn prompt_cache_segments_name_each_system_tier_and_skip_tools_under_a_text_dialect() {
// Two leading system messages (stable+context, then volatile) are two
diff --git a/vendor/tinyagents b/vendor/tinyagents
index 132a5daa62..ef79919182 160000
--- a/vendor/tinyagents
+++ b/vendor/tinyagents
@@ -1 +1 @@
-Subproject commit 132a5daa629d771690c80a75c424c5a2b17b6627
+Subproject commit ef79919182b2a598aa6ba041e15b7bcac067f25f