use serde_json::{Map, Value};
use super::net::{
self, insert_break_even_horizon, insert_breakpoints_net_hundredths,
reorder_blocks_net_hundredths, BreakpointLayer, WriteMultiplier,
};
use super::validate;
use crate::boundary::churn::ChurnClass;
use crate::boundary::prefix_shape::PrefixShape;
use crate::generated::types::ChurnLayer;
pub const MECHANISM_INSERT_BREAKPOINTS: &str = "insert_breakpoints";
pub const MECHANISM_REORDER_BLOCKS: &str = "reorder_blocks";
const MIN_VOLATILE_CHANGES: u32 = 2;
pub struct ActContext<'a> {
pub w: WriteMultiplier,
pub model: Option<&'a str>,
pub shape: &'a PrefixShape,
pub exclude_layers: &'a [ChurnLayer],
}
#[derive(Clone, Debug)]
pub struct Measured {
pub rewritten: Value,
pub net_hundredths: i128,
}
pub fn measure_prefix_reorder(body: &Value, mechanism: &str, ctx: &ActContext) -> Option<Measured> {
if ctx.exclude_layers.contains(&ChurnLayer::System) {
return None;
}
let system = body.get("system").and_then(Value::as_array)?;
if system.is_empty() {
return None;
}
match mechanism {
MECHANISM_INSERT_BREAKPOINTS => insert_breakpoints(body, system, ctx),
MECHANISM_REORDER_BLOCKS => reorder_blocks(body, system, ctx),
_ => None,
}
}
fn insert_breakpoints(body: &Value, system: &[Value], ctx: &ActContext) -> Option<Measured> {
if !net::breakpoint_positions(body).is_empty() {
return None;
}
let seam = system.len() - 1;
let prefix_tokens = prefix_token_estimate(body);
if prefix_tokens < min_cacheable_tokens(ctx.model) {
return None;
}
let net_hundredths = insert_breakpoints_net_hundredths(prefix_tokens, ctx.shape.horizon, ctx.w);
let mut rewritten = body.clone();
let blocks = rewritten.get_mut("system")?.as_array_mut()?;
let block = blocks.get_mut(seam)?.as_object_mut()?;
block.insert("cache_control".to_string(), ephemeral_marker(ctx.w));
Some(Measured {
rewritten,
net_hundredths,
})
}
pub fn insert_horizon_floor(w: WriteMultiplier) -> u32 {
insert_break_even_horizon(w)
}
fn reorder_blocks(body: &Value, system: &[Value], ctx: &ActContext) -> Option<Measured> {
let last_bp = net::breakpoint_positions(body)
.into_iter()
.filter(|p| p.layer == BreakpointLayer::System)
.map(|p| p.index)
.next_back()?;
let victim = (0..last_bp).find(|&i| {
ctx.shape.is_volatile_now(i, MIN_VOLATILE_CHANGES) && block_is_machine_data(&system[i])
})?;
let span = victim + 1..=last_bp;
if !span.clone().all(|i| ctx.shape.is_stable(i)) {
return None;
}
let stable_tokens: u64 = span.map(|i| super::estimate_tokens(&system[i])).sum();
if stable_tokens == 0 {
return None;
}
let horizon = ctx.shape.changes.get(victim).copied().unwrap_or(0);
let net_hundredths = reorder_blocks_net_hundredths(stable_tokens, horizon, ctx.w);
let mut rewritten = body.clone();
let blocks = rewritten.get_mut("system")?.as_array_mut()?;
let moved = blocks.remove(victim);
blocks.push(moved);
Some(Measured {
rewritten,
net_hundredths,
})
}
fn block_is_machine_data(block: &Value) -> bool {
let Some(text) = block.get("text").and_then(Value::as_str) else {
return false;
};
!matches!(
crate::boundary::churn::classify_text(text),
ChurnClass::Unknown
)
}
fn ephemeral_marker(w: WriteMultiplier) -> Value {
let mut m = Map::new();
m.insert("type".to_string(), Value::String("ephemeral".to_string()));
if w == WriteMultiplier::OneHour {
m.insert("ttl".to_string(), Value::String("1h".to_string()));
}
Value::Object(m)
}
fn prefix_token_estimate(body: &Value) -> u64 {
["tools", "system"]
.into_iter()
.filter_map(|k| body.get(k))
.map(super::estimate_tokens)
.sum()
}
fn min_cacheable_tokens(model: Option<&str>) -> u64 {
const CONSERVATIVE: u64 = 4096;
let Some(m) = model.map(str::to_ascii_lowercase) else {
return CONSERVATIVE;
};
let has = |needle: &str| m.contains(needle);
if has("mythos") {
return if has("preview") { 2048 } else { 512 };
}
if has("fable") {
return 512;
}
if has("haiku") {
return if has("3-5") || has("3.5") {
2048
} else {
CONSERVATIVE
};
}
if has("opus") {
return match opus_generation(&m) {
Some(g) if g >= 500 => 512, Some(480) => 1024, Some(470) => 2048, Some(460) | Some(450) => 4096, Some(_) => 1024, None => CONSERVATIVE,
};
}
if has("sonnet") {
return 1024;
}
CONSERVATIVE
}
fn opus_generation(m: &str) -> Option<u32> {
let after = m.split("opus").nth(1)?;
let digits: Vec<u32> = after
.split(|c: char| !c.is_ascii_digit())
.filter(|s| !s.is_empty())
.take(2)
.filter_map(|s| s.parse::<u32>().ok())
.collect();
match digits.as_slice() {
[major] => Some(major * 100),
[major, minor] => Some(major * 100 + minor.min(&9) * 10),
_ => None,
}
}
pub use validate::reorder_is_structurally_valid;
#[cfg(test)]
mod tests {
use super::*;
use crate::boundary::prefix_shape::PrefixTracker;
use serde_json::json;
fn text(t: &str) -> Value {
json!({ "type": "text", "text": t })
}
fn cached(t: &str) -> Value {
json!({ "type": "text", "text": t, "cache_control": { "type": "ephemeral" } })
}
fn stable_shape(body: &Value, turns: u32) -> PrefixShape {
let t = PrefixTracker::default();
let mut shape = PrefixShape::default();
for _ in 0..turns {
shape = t.observe("i", "s", body);
}
shape
}
fn ctx<'a>(shape: &'a PrefixShape, model: &'a str) -> ActContext<'a> {
ActContext {
w: WriteMultiplier::FiveMinute,
model: Some(model),
shape,
exclude_layers: &[],
}
}
fn big() -> String {
"x".repeat(8_000)
}
#[test]
fn inserts_one_marker_on_the_last_system_block() {
let body = json!({
"model": "claude-opus-4-8",
"system": [ text(&big()), text("tail") ],
"messages": [ { "role": "user", "content": "hi" } ]
});
let shape = stable_shape(&body, 2);
let m = measure_prefix_reorder(
&body,
MECHANISM_INSERT_BREAKPOINTS,
&ctx(&shape, "claude-opus-4-8"),
)
.expect("a viable insert");
let bps = net::breakpoint_positions(&m.rewritten);
assert_eq!(bps.len(), 1, "exactly one marker added");
assert_eq!(bps[0].layer, BreakpointLayer::System);
assert_eq!(bps[0].index, 1, "on the LAST block — the stability seam");
assert!(
m.net_hundredths > 0,
"two observed turns clears 5m break-even"
);
}
#[test]
fn insert_is_content_preserving_apart_from_the_marker() {
let body = json!({
"model": "claude-opus-4-8",
"system": [ text(&big()), text("tail") ],
"messages": [ { "role": "user", "content": "hi" } ]
});
let shape = stable_shape(&body, 2);
let m = measure_prefix_reorder(
&body,
MECHANISM_INSERT_BREAKPOINTS,
&ctx(&shape, "claude-opus-4-8"),
)
.unwrap();
assert!(validate::insert_is_structurally_valid(&body, &m.rewritten));
assert_eq!(
body["messages"], m.rewritten["messages"],
"messages untouched"
);
assert_eq!(
body["system"][0], m.rewritten["system"][0],
"the non-seam block is byte-identical"
);
assert_eq!(
body["system"][1]["text"], m.rewritten["system"][1]["text"],
"the seam block's TEXT is byte-identical — only cache_control was added"
);
}
#[test]
fn insert_declines_when_a_breakpoint_already_exists() {
let body = json!({
"model": "claude-opus-4-8",
"system": [ text(&big()), cached("tail") ],
"messages": []
});
let shape = stable_shape(&body, 3);
assert!(measure_prefix_reorder(
&body,
MECHANISM_INSERT_BREAKPOINTS,
&ctx(&shape, "claude-opus-4-8")
)
.is_none());
}
#[test]
fn insert_declines_below_the_models_cacheable_floor() {
let body = json!({
"model": "claude-opus-4-8",
"system": [ text("short") ],
"messages": []
});
let shape = stable_shape(&body, 5);
assert!(
measure_prefix_reorder(&body, MECHANISM_INSERT_BREAKPOINTS, &ctx(&shape, "claude-opus-4-8")).is_none(),
"a marker below the floor is silently ignored — claiming a saving for it would be a lie"
);
}
#[test]
fn insert_declines_for_a_string_valued_system() {
let body = json!({ "model": "claude-opus-4-8", "system": big(), "messages": [] });
let shape = stable_shape(&body, 5);
assert!(measure_prefix_reorder(
&body,
MECHANISM_INSERT_BREAKPOINTS,
&ctx(&shape, "claude-opus-4-8")
)
.is_none());
}
#[test]
fn insert_declines_when_the_author_excluded_the_system_layer() {
let body = json!({
"model": "claude-opus-4-8",
"system": [ text(&big()) ],
"messages": []
});
let shape = stable_shape(&body, 3);
let c = ActContext {
exclude_layers: &[ChurnLayer::System],
..ctx(&shape, "claude-opus-4-8")
};
assert!(measure_prefix_reorder(&body, MECHANISM_INSERT_BREAKPOINTS, &c).is_none());
}
#[test]
fn insert_on_a_first_turn_is_measured_net_negative_not_applied() {
let body = json!({
"model": "claude-opus-4-8",
"system": [ text(&big()) ],
"messages": []
});
let shape = stable_shape(&body, 1);
let m = measure_prefix_reorder(
&body,
MECHANISM_INSERT_BREAKPOINTS,
&ctx(&shape, "claude-opus-4-8"),
)
.expect("viable shape");
assert!(m.net_hundredths < 0, "one turn cannot amortise a write");
}
#[test]
fn a_one_hour_request_gets_a_one_hour_marker() {
let body = json!({
"model": "claude-opus-4-8",
"system": [ text(&big()) ],
"messages": []
});
let shape = stable_shape(&body, 4);
let c = ActContext {
w: WriteMultiplier::OneHour,
..ctx(&shape, "claude-opus-4-8")
};
let m = measure_prefix_reorder(&body, MECHANISM_INSERT_BREAKPOINTS, &c).unwrap();
assert_eq!(m.rewritten["system"][0]["cache_control"]["ttl"], "1h");
}
fn churning_body(tick: u32) -> Value {
json!({
"model": "claude-opus-4-8",
"system": [
text(&format!("2026-08-18T10:00:{tick:02}Z")),
cached(&big())
],
"messages": [ { "role": "user", "content": "hi" } ]
})
}
fn churned_shape(turns: u32) -> (PrefixShape, Value) {
let t = PrefixTracker::default();
let mut shape = PrefixShape::default();
let mut last = Value::Null;
for i in 0..turns {
last = churning_body(i);
shape = t.observe("i", "s", &last);
}
(shape, last)
}
#[test]
fn moves_the_volatile_block_past_the_breakpoint() {
let (shape, body) = churned_shape(4);
let m = measure_prefix_reorder(
&body,
MECHANISM_REORDER_BLOCKS,
&ctx(&shape, "claude-opus-4-8"),
)
.expect("a viable reorder");
let sys = m.rewritten["system"].as_array().unwrap();
assert_eq!(sys.len(), 2, "no block gained or lost");
assert!(
sys[1]["text"].as_str().unwrap().starts_with("2026-08-18"),
"the timestamp moved to the end"
);
assert!(
sys[0].get("cache_control").is_some(),
"the breakpoint rides with its block and is now ahead of the volatile one"
);
assert!(m.net_hundredths > 0);
}
#[test]
fn reorder_is_a_permutation_and_leaves_everything_else_identical() {
let (shape, body) = churned_shape(4);
let m = measure_prefix_reorder(
&body,
MECHANISM_REORDER_BLOCKS,
&ctx(&shape, "claude-opus-4-8"),
)
.unwrap();
assert!(validate::reorder_is_structurally_valid(&body, &m.rewritten));
assert_eq!(body["messages"], m.rewritten["messages"]);
assert_eq!(body["model"], m.rewritten["model"]);
}
#[test]
fn reorder_declines_when_the_move_would_cross_no_breakpoint() {
let t = PrefixTracker::default();
let mut shape = PrefixShape::default();
let mut body = Value::Null;
for i in 0..4 {
body = json!({
"model": "claude-opus-4-8",
"system": [ text(&format!("2026-08-18T10:00:{i:02}Z")), text(&big()) ],
"messages": []
});
shape = t.observe("i", "s", &body);
}
assert!(measure_prefix_reorder(
&body,
MECHANISM_REORDER_BLOCKS,
&ctx(&shape, "claude-opus-4-8")
)
.is_none());
}
#[test]
fn reorder_declines_on_a_quiet_turn() {
let t = PrefixTracker::default();
for i in 0..4 {
t.observe("i", "s", &churning_body(i));
}
let quiet = churning_body(3); let shape = t.observe("i", "s", &quiet);
assert!(measure_prefix_reorder(
&quiet,
MECHANISM_REORDER_BLOCKS,
&ctx(&shape, "claude-opus-4-8")
)
.is_none());
}
#[test]
fn reorder_never_moves_prose() {
let t = PrefixTracker::default();
let mut shape = PrefixShape::default();
let mut body = Value::Null;
for i in 0..4 {
body = json!({
"model": "claude-opus-4-8",
"system": [
text(&format!("Always answer in the style of variant {i}, at length, with care.")),
cached(&big())
],
"messages": []
});
shape = t.observe("i", "s", &body);
}
assert!(
measure_prefix_reorder(
&body,
MECHANISM_REORDER_BLOCKS,
&ctx(&shape, "claude-opus-4-8")
)
.is_none(),
"prose is never reordered, however volatile"
);
}
#[test]
fn reorder_declines_after_a_single_change() {
let t = PrefixTracker::default();
t.observe("i", "s", &churning_body(0));
let shape = t.observe("i", "s", &churning_body(1));
let body = churning_body(1);
assert!(
measure_prefix_reorder(
&body,
MECHANISM_REORDER_BLOCKS,
&ctx(&shape, "claude-opus-4-8")
)
.is_none(),
"one edit is a person fixing a typo, not a per-request timestamp"
);
}
#[test]
fn reorder_declines_when_the_span_it_would_buy_back_is_itself_unstable() {
let t = PrefixTracker::default();
let mut shape = PrefixShape::default();
let mut body = Value::Null;
for i in 0..5 {
body = json!({
"model": "claude-opus-4-8",
"system": [
text(&format!("2026-08-18T10:00:{i:02}Z")),
text(&format!("counter {i}")),
cached(&big())
],
"messages": []
});
shape = t.observe("i", "s", &body);
}
assert!(measure_prefix_reorder(
&body,
MECHANISM_REORDER_BLOCKS,
&ctx(&shape, "claude-opus-4-8")
)
.is_none());
}
#[test]
fn an_unknown_mechanism_is_skipped_not_guessed() {
let body = json!({
"model": "claude-opus-4-8",
"system": [ text(&big()) ],
"messages": []
});
let shape = stable_shape(&body, 3);
assert!(measure_prefix_reorder(
&body,
"some_future_mechanism",
&ctx(&shape, "claude-opus-4-8")
)
.is_none());
}
#[test]
fn model_floors_follow_the_documented_non_monotonic_table() {
assert_eq!(min_cacheable_tokens(Some("claude-opus-5")), 512);
assert_eq!(min_cacheable_tokens(Some("claude-fable-5")), 512);
assert_eq!(min_cacheable_tokens(Some("claude-mythos-5")), 512);
assert_eq!(min_cacheable_tokens(Some("claude-mythos-preview")), 2048);
assert_eq!(min_cacheable_tokens(Some("claude-opus-4-8")), 1024);
assert_eq!(min_cacheable_tokens(Some("claude-sonnet-5")), 1024);
assert_eq!(min_cacheable_tokens(Some("claude-sonnet-4-6")), 1024);
assert_eq!(min_cacheable_tokens(Some("claude-opus-4-7")), 2048);
assert_eq!(min_cacheable_tokens(Some("claude-opus-4-6")), 4096);
assert_eq!(min_cacheable_tokens(Some("claude-haiku-4-5")), 4096);
assert_eq!(min_cacheable_tokens(Some("gpt-4o")), 4096);
assert_eq!(min_cacheable_tokens(None), 4096);
}
#[test]
fn measurement_is_deterministic() {
let (shape, body) = churned_shape(4);
let c = ctx(&shape, "claude-opus-4-8");
let first = measure_prefix_reorder(&body, MECHANISM_REORDER_BLOCKS, &c).unwrap();
for _ in 0..25 {
let again = measure_prefix_reorder(&body, MECHANISM_REORDER_BLOCKS, &c).unwrap();
assert_eq!(again.rewritten, first.rewritten);
assert_eq!(again.net_hundredths, first.net_hundredths);
}
}
#[test]
fn measurement_never_mutates_the_input_body() {
let (shape, body) = churned_shape(4);
let before = body.clone();
let _ = measure_prefix_reorder(
&body,
MECHANISM_REORDER_BLOCKS,
&ctx(&shape, "claude-opus-4-8"),
);
let _ = measure_prefix_reorder(
&body,
MECHANISM_INSERT_BREAKPOINTS,
&ctx(&shape, "claude-opus-4-8"),
);
assert_eq!(body, before);
}
}