claude-connector: with cache retention, breakpoints after the tools and the system prompt too

A cache entry is written only where a breakpoint sits. With one breakpoint on
the last message, a later turn whose messages differ found no entry for the
byte-stable tools and system prefix, so Ghostlight's DM turns resent it
uncached each turn. The last tool and the system block now carry the same
mark as the last message: three of the API's four breakpoints.

Asked by Weksa's Self for Ghostlight (ded2ede sets cache_retention per lane).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-12-03 06:26:46 +01:00
commit 898779f4dd
2 changed files with 66 additions and 13 deletions

View file

@ -231,14 +231,22 @@ fn render(request: &ProviderRequest) -> Result<Value, String> {
body.insert("model".into(), json!(request.model));
body.insert("max_tokens".into(), json!(request.max_output_tokens));
body.insert(
"system".into(),
json!([{"type": "text", "text": request.instructions}]),
);
// Breakpoints, when caching is asked: after the tools, after the system
// prompt and on the last message (three of the API's four). The render
// order is tools, system, messages, and an entry is written only where a
// breakpoint sits, so the stable prefix is reused by a later turn whose
// messages differ.
let mark = request.cache_retention.map(cache_mark);
let mut system = json!({"type": "text", "text": request.instructions});
if let Some(mark) = &mark {
system["cache_control"] = mark.clone();
}
body.insert("system".into(), json!([system]));
let mut messages = messages_of(&request.input)?;
if let Some(retention) = request.cache_retention {
mark_cache_breakpoint(&mut messages, retention);
if let Some(mark) = &mark {
mark_last_message(&mut messages, mark.clone());
}
body.insert("messages".into(), Value::Array(messages));
@ -253,6 +261,9 @@ fn render(request: &ProviderRequest) -> Result<Value, String> {
"input_schema": schema,
}));
}
if let (Some(mark), Some(last)) = (&mark, tools.last_mut()) {
last["cache_control"] = mark.clone();
}
body.insert("tools".into(), Value::Array(tools));
let kind = if request.tool_choice == ToolChoice::Required {
if rejects_forced_tool_choice(&request.model) {
@ -389,13 +400,16 @@ fn block_of(item: &InputItem) -> Result<(Role, Value), String> {
})
}
/// Puts the one breakpoint on the last block of the last message that can
/// carry it (a thinking block cannot).
fn mark_cache_breakpoint(messages: &mut [Value], retention: CacheRetention) {
let mark = match retention {
fn cache_mark(retention: CacheRetention) -> Value {
match retention {
CacheRetention::Short => json!({"type": "ephemeral"}),
CacheRetention::Long => json!({"type": "ephemeral", "ttl": "1h"}),
};
}
}
/// Puts the breakpoint on the last block of the last message that can carry
/// it (a thinking block cannot).
fn mark_last_message(messages: &mut [Value], mark: Value) {
let Some(blocks) = messages
.last_mut()
.and_then(|message| message["content"].as_array_mut())

View file

@ -420,7 +420,11 @@ fn t_c0_cache_control_long() {
asked.cache_retention = Some(CacheRetention::Long);
let body = render(&asked).unwrap();
assert_eq!(count(&body), 1);
assert_eq!(count(&body), 2, "the system prompt and the last message");
assert_eq!(
body["system"][0]["cache_control"],
json!({"type": "ephemeral", "ttl": "1h"})
);
let blocks = &body["messages"][2]["content"];
assert!(blocks[0].get("cache_control").is_none());
assert_eq!(
@ -430,13 +434,48 @@ fn t_c0_cache_control_long() {
asked.cache_retention = Some(CacheRetention::Short);
let body = render(&asked).unwrap();
assert_eq!(count(&body), 1);
assert_eq!(count(&body), 2);
assert_eq!(
body["system"][0]["cache_control"],
json!({"type": "ephemeral"})
);
assert_eq!(
body["messages"][2]["content"][1]["cache_control"],
json!({"type": "ephemeral"})
);
}
#[test]
fn caching_marks_the_last_tool_and_the_system_prompt_so_the_prefix_outlives_the_turn() {
let mut asked = request();
asked.tools = vec![
ToolDefinition {
name: "first".to_string(),
description: "one".to_string(),
parameters_json: r#"{"type":"object"}"#.to_string(),
},
ToolDefinition {
name: "second".to_string(),
description: "two".to_string(),
parameters_json: r#"{"type":"object"}"#.to_string(),
},
];
let count = |body: &Value| body.to_string().matches("cache_control").count();
assert_eq!(
count(&render(&asked).unwrap()),
0,
"no retention, no breakpoint"
);
asked.cache_retention = Some(CacheRetention::Long);
let body = render(&asked).unwrap();
let mark = json!({"type": "ephemeral", "ttl": "1h"});
assert_eq!(count(&body), 3, "at most the API's four: {body}");
assert!(body["tools"][0].get("cache_control").is_none());
assert_eq!(body["tools"][1]["cache_control"], mark);
assert_eq!(body["system"][0]["cache_control"], mark);
}
#[test]
fn a_breakpoint_skips_a_trailing_thinking_block() {
let mut asked = request();