Skip to content

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

fix(cache): keep missing frozen prefixes uncached

Steven Enamakel5fd5e5fe9278
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 b301f73f02..7c24d343e6 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_session.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_session.rs
@@ -1363,14 +1363,12 @@ 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
+ let runtime = self
.runtime_session
.as_ref()
- .expect("runtime session initialized")
- .prefix_snapshot()
- .messages()
- .len();
- if frozen_prefix_len > 0 {
+ .expect("runtime session initialized");
+ let frozen_prefix_len = runtime.prefix_snapshot().messages().len();
+ if frozen_prefix_len > 0 || !runtime.history().is_empty() {
options.run_context.data.cacheable_system_prefix_len = Some(frozen_prefix_len);
}
let outcome = self
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 9125508bf8..adf9231254 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/prompt_cache.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/prompt_cache.rs
@@ -36,8 +36,10 @@ fn stable_prefix_fingerprint(value: &serde_json::Value) -> String {
/// the crate prompt builder, so `cache_segments` would otherwise stay empty and
/// the crate `PromptCacheGuardMiddleware` (installed immediately after this)
/// would have no prefix to protect. The segments use the harness-layout ids
-/// `system` (`system.1`, … per leading system message) and `tools` — exactly
-/// those, and only those. The crate's
+/// `system` (`system.1`, … per frozen system tier) and `tools` — exactly
+/// those for an ordinary session. If a resumed session has no recoverable
+/// frozen prefix, a noncacheable marker prevents the dispatch layer from
+/// promoting its leading System history summary into one. The crate's
/// `refresh_prompt_cache_fingerprint` (agent_loop/run_loop.rs) recognises that
/// layout at dispatch and rebuilds `prompt_fingerprint` from the bytes actually
/// sent (system messages + tool schemas), so an unchanged system prompt +
@@ -62,6 +64,9 @@ pub(crate) struct PromptCacheSegmentMiddleware;
/// `tinyagents_harness::prompt::system_segment_id`). Any other id opts the
/// request into whole-request fingerprinting (see the middleware docs).
const HARNESS_TOOLS_SEGMENT_ID: &str = "tools";
+/// Explicitly opt out of auto-promoting a leading System history row when a
+/// resumed session has no recoverable frozen prompt prefix.
+const VOLATILE_SYSTEM_HISTORY_SEGMENT_ID: &str = "volatile-system-history";
#[async_trait]
impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
@@ -129,6 +134,16 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext>
cacheable: true,
});
}
+ if segments.is_empty()
+ && matches!(ctx.data.cacheable_system_prefix_len, Some(0))
+ && observed_leading_system > 0
+ {
+ segments.push(PromptSegment {
+ id: VOLATILE_SYSTEM_HISTORY_SEGMENT_ID.to_string(),
+ role: SegmentRole::Volatile,
+ cacheable: false,
+ });
+ }
if !segments.is_empty() {
// Content-derived, so a guard reading it before dispatch sees a
// system-prompt or tool-schema edit; the crate recomputes it from
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 261e6b9042..f5a2ccedab 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
@@ -315,6 +315,23 @@ async fn prompt_cache_segments_do_not_promote_a_compaction_summary() {
assert_eq!(before.prompt_fingerprint, after.prompt_fingerprint);
}
+#[tokio::test]
+async fn prompt_cache_segments_keep_an_unrecoverable_prefix_uncacheable() {
+ let mw = PromptCacheSegmentMiddleware;
+ let mut run = ctx();
+ run.data.cacheable_system_prefix_len = Some(0);
+ let mut request = ModelRequest::new(vec![
+ TaMessage::system("history summary"),
+ TaMessage::user("continue"),
+ ]);
+
+ mw.before_model(&mut run, &(), &mut request).await.unwrap();
+
+ assert_eq!(request.cache_segments.len(), 1);
+ assert_eq!(request.cache_segments[0].role, SegmentRole::Volatile);
+ assert!(!request.cache_segments[0].cacheable);
+}
+
#[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 05798673d6..a9d8a07c6e 160000
--- a/vendor/tinyagents
+++ b/vendor/tinyagents
@@ -1 +1 @@
-Subproject commit 05798673d66cd4d7bc06d1f5c561c9ce28b36def
+Subproject commit a9d8a07c6e2297d22e130c87aed9c592cea79afb