Skip to content

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

feat(agent): preserve system prompt cache breakpoints

Steven Enamakelcaabebbf2045
diff --git a/src/openhuman/agent/message_convert.rs b/src/openhuman/agent/message_convert.rs
index f40467cd67..ff59e2682a 100644
--- a/src/openhuman/agent/message_convert.rs
+++ b/src/openhuman/agent/message_convert.rs
@@ -59,6 +59,46 @@ fn reasoning_extra_metadata(content: &[ContentBlock]) -> Option<serde_json::Valu
.map(|reasoning| serde_json::json!({ REASONING_EXT_KEY: reasoning }))
}
+
+/// Split `text` at `breakpoints`, interleaving a
+/// [`ContentBlock::CacheBreakpoint`] marker after each piece.
+///
+/// With no breakpoints this returns the single `Text` block it always did, and
+/// the OpenAI-compatible wire renders it as a bare string exactly as before —
+/// which matters, because most providers on that path cache automatically and
+/// a content array where a string used to be is a change they did not ask for.
+///
+/// Offsets are validated at construction (`ChatMessage::system_tiered`), so by
+/// the time they reach here they are ascending, in range and on character
+/// boundaries. This function still slices defensively rather than indexing,
+/// because a panic here would take down the turn.
+fn split_at_breakpoints(text: String, breakpoints: &[usize]) -> Vec<ContentBlock> {
+ if breakpoints.is_empty() {
+ return vec![ContentBlock::Text(text)];
+ }
+ let mut blocks = Vec::with_capacity(breakpoints.len() * 2 + 1);
+ let mut start = 0usize;
+ for &offset in breakpoints {
+ let Some(piece) = text.get(start..offset) else {
+ tracing::warn!(
+ start,
+ offset,
+ "[prompts] cache breakpoint is not sliceable; emitting the prompt uncut"
+ );
+ return vec![ContentBlock::Text(text)];
+ };
+ blocks.push(ContentBlock::Text(piece.to_string()));
+ blocks.push(ContentBlock::CacheBreakpoint);
+ start = offset;
+ }
+ if let Some(tail) = text.get(start..) {
+ if !tail.is_empty() {
+ blocks.push(ContentBlock::Text(tail.to_string()));
+ }
+ }
+ blocks
+}
+
/// Convert one openhuman [`ChatMessage`] into a harness [`Message`].
///
/// Role strings map onto the typed arms. A seeded **native** tool round is
@@ -75,7 +115,7 @@ pub(crate) fn chat_message_to_message(msg: &ChatMessage) -> Message {
let text = msg.content.clone();
match msg.role.as_str() {
"system" => Message::System(SystemMessage {
- content: vec![ContentBlock::Text(text)],
+ content: split_at_breakpoints(text, &msg.cache_breakpoints),
}),
"assistant" => {
// Restore any `reasoning_content` stashed on the persisted message so a
diff --git a/src/openhuman/agent/messages.rs b/src/openhuman/agent/messages.rs
index 60df43150d..b0b7c6fec6 100644
--- a/src/openhuman/agent/messages.rs
+++ b/src/openhuman/agent/messages.rs
@@ -39,6 +39,43 @@ impl ChatMessage {
}
}
+ /// A system message carrying prompt-cache breakpoints.
+ ///
+ /// `breakpoints` are ends-of-tier from
+ /// [`crate::openhuman::agent::prompts::SystemPromptBuilder::build_tiered`].
+ /// Out-of-range or non-ascending offsets are dropped rather than trusted:
+ /// a bad offset would split the prompt mid-sentence and the model would
+ /// read the damage, whereas a dropped one costs only a cache miss.
+ pub fn system_tiered(content: impl Into<String>, breakpoints: Vec<usize>) -> Self {
+ let content = content.into();
+ let mut previous = 0usize;
+ let breakpoints: Vec<usize> = breakpoints
+ .into_iter()
+ .filter(|&offset| {
+ let ok = offset > previous
+ && offset < content.len()
+ && content.is_char_boundary(offset);
+ if ok {
+ previous = offset;
+ } else {
+ tracing::warn!(
+ offset,
+ len = content.len(),
+ "[prompts] dropping an invalid cache breakpoint"
+ );
+ }
+ ok
+ })
+ .collect();
+ Self {
+ id: None,
+ role: "system".into(),
+ content,
+ extra_metadata: None,
+ cache_breakpoints: breakpoints,
+ }
+ }
+
pub fn user(content: impl Into<String>) -> Self {
Self {
id: None,