1use axum::{
2 body::Body,
3 extract::State,
4 http::{Request, StatusCode},
5 response::Response,
6};
7use serde_json::Value;
8
9use super::ProxyState;
10use super::forward;
11use super::tool_kind::{self, ToolResultKind};
12use super::{cache_safety, prefix_cache_stats, prefix_replay, prose, sticky_tools};
13
14std::thread_local! {
15 static HEADROOM_REQUEST: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
17}
18
19pub(super) fn set_headroom_request(val: bool) {
21 HEADROOM_REQUEST.set(val);
22}
23use crate::core::config::{HistoryMode, ProseRole};
24
25pub async fn handler(
26 State(state): State<ProxyState>,
27 req: Request<Body>,
28) -> Result<Response, StatusCode> {
29 let upstream = state.anthropic_upstream();
30 forward::forward_request(
31 State(state),
32 req,
33 &upstream,
34 "/v1/messages",
35 compress_request_body,
36 "Anthropic",
37 &[],
38 )
39 .await
40}
41
42pub(super) fn compress_request_body(
43 parsed: Value,
44 original_size: usize,
45) -> (Vec<u8>, usize, usize) {
46 let mut doc = parsed;
47 let mut modified = false;
48
49 let cfg = crate::core::config::Config::load();
52 let config_headroom = cfg.proxy.is_headroom_compat();
53 if config_headroom {
54 prefix_cache_stats::record_headroom_compat();
55 }
56 let _headroom_compat = config_headroom || HEADROOM_REQUEST.get();
61 let system_aggr = cfg.proxy.resolved_role_aggressiveness(ProseRole::System);
62 let user_aggr = cfg.proxy.resolved_role_aggressiveness(ProseRole::User);
63 let live_compress = cfg.proxy.live_compresses();
64 let mode = cfg.proxy.resolved_history_mode();
65 let inject_breakpoint = cfg.proxy.cache_breakpoint_enabled();
70 let align_volatile = cfg.proxy.cache_aligner_enabled();
75 let relocate_volatile = cfg.proxy.cache_align_relocate_enabled();
80 let cache_economics = cfg.proxy.cache_policy_enabled();
87 let arm = super::holdout::assign(
92 &super::holdout::anthropic_key(&doc),
93 cfg.proxy.output_holdout_fraction(),
94 );
95 if cfg.proxy.ccr_inband_enabled() {
101 modified |= super::ccr::splice_inband_in_place(&mut doc);
102 }
103 if arm == super::holdout::Arm::Treatment {
108 if let Some(effort) = cfg.proxy.resolved_effort() {
109 modified |= super::effort::apply_anthropic(&mut doc, effort);
110 }
111 if cfg.proxy.verbosity_steer_enabled() {
115 modified |= super::verbosity::apply_anthropic(&mut doc);
116 }
117 }
118 if !live_compress
123 && mode == HistoryMode::Off
124 && system_aggr.is_none()
125 && user_aggr.is_none()
126 && !modified
127 && !inject_breakpoint
128 && !align_volatile
129 && !relocate_volatile
130 && !cache_economics
131 {
132 let out = serde_json::to_vec(&doc).unwrap_or_default();
133 return (out, original_size, original_size);
134 }
135 let mut prose_segments: u64 = 0;
136
137 let cached = doc
142 .get("messages")
143 .and_then(|m| m.as_array())
144 .map_or(0, |m| super::history_prune::cached_prefix_len(m));
145
146 if cache_economics
157 && let Some(m) = doc.get("messages").and_then(|m| m.as_array())
158 && let Some(outcome) = super::cache_attribution::record_request(m, cached)
159 {
160 match outcome {
161 super::cache_attribution::CacheOutcome::WarmReuse => {
162 prefix_cache_stats::record_hit();
163 }
164 super::cache_attribution::CacheOutcome::ColdStart => {}
165 _ => {
166 prefix_cache_stats::record_miss();
167 }
168 }
169 }
170 let repack = cfg.proxy.repacks_cold_prefix()
175 && doc
176 .get("messages")
177 .and_then(|m| m.as_array())
178 .is_some_and(|m| {
179 super::cold_prefix::repack_decision(m, cached)
180 && (!cache_economics
181 || super::cache_policy::worth_repacking(doc.get("system"), m, cached))
182 });
183 let protect = if repack { 0 } else { cached };
186
187 let model_name = doc
192 .get("model")
193 .and_then(Value::as_str)
194 .unwrap_or("default")
195 .to_owned();
196 if let Some(a) = system_aggr
197 && protect == 0
198 && let Some(system) = doc.get_mut("system")
199 && (repack || !prose::value_has_cache_control(system))
200 {
201 let should_compress = if repack || cached == 0 {
202 true
203 } else {
204 let sys_tokens = prose::estimate_tokens(system) as u64;
205 let estimated_after = (sys_tokens as f64 * (1.0 - a)).max(0.0) as u64;
206 let reuse_rate = super::cache_attribution::estimated_reuse_rate();
207 let model_cost = super::cache_policy::model_cost_for(&model_name);
208 let gate = super::cache_policy::should_mutate_frozen(
209 sys_tokens,
210 estimated_after,
211 reuse_rate,
212 &model_cost,
213 );
214 matches!(gate, super::cache_policy::MutationDecision::Mutate { .. })
215 };
216 if should_compress {
217 let n = prose::compress_system_value(system, a);
218 if n > 0 {
219 prose_segments += u64::from(n);
220 modified = true;
221 }
222 }
223 }
224
225 if let Some(messages) = doc.get_mut("messages").and_then(|m| m.as_array_mut()) {
226 let tool_names = tool_kind::anthropic_tool_names(messages);
229
230 let boundary = super::history_prune::prune_boundary(mode, messages.len());
234 modified |=
240 super::history_prune::prune_history_range(messages, protect, boundary, &tool_names);
241
242 for msg in messages.iter_mut() {
243 let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
244 if role != "user" {
245 continue;
246 }
247
248 if let Some(content) = msg.get_mut("content").and_then(|c| c.as_array_mut()) {
249 for block in content.iter_mut() {
250 if block.get("type").and_then(|t| t.as_str()) != Some("tool_result") {
251 continue;
252 }
253
254 let name = block
255 .get("tool_use_id")
256 .and_then(|v| v.as_str())
257 .and_then(|id| tool_names.get(id))
258 .map(String::as_str);
259 let kind = name.map_or(ToolResultKind::Other, tool_kind::classify_tool_name);
260
261 let excluded =
264 name.is_some_and(|n| cfg.proxy.is_tool_live_compress_excluded(n));
265 if live_compress
266 && !excluded
267 && let Some(inner_content) = block.get_mut("content")
268 {
269 modified |= compress_content_field(inner_content, name, kind);
270 }
271 }
272 }
273 }
274
275 if let Some(a) = user_aggr {
280 let end = boundary.min(messages.len());
281 let start = protect.min(end);
282 for msg in &mut messages[start..end] {
283 if msg.get("role").and_then(|r| r.as_str()) == Some("user")
284 && let Some(content) = msg.get_mut("content").and_then(|c| c.as_array_mut())
285 {
286 prose_segments += u64::from(prose::compress_text_blocks(content, a));
287 }
288 }
289 }
290 }
291
292 if prose_segments > 0 {
293 modified = true;
294 }
295 if align_volatile
301 && cached == 0
302 && let Some(system) = doc.get("system")
303 && !prose::value_has_cache_control(system)
304 && let Some(text) = super::cache_aligner::system_text(system)
305 {
306 let scan = super::cache_aligner::scan_volatile(&text);
307 cache_safety::record_volatile_system(scan.fields as u64);
308 }
309 if relocate_volatile
318 && arm == super::holdout::Arm::Treatment
319 && cached == 0
320 && doc
321 .get("system")
322 .is_some_and(|s| !prose::value_has_cache_control(s))
323 {
324 let relocated = super::cache_aligner::apply_anthropic_relocate(&mut doc);
325 if relocated > 0 {
326 modified = true;
327 cache_safety::record_volatile_relocated(relocated as u64);
328 }
329 }
330 if inject_breakpoint
339 && cached == 0
340 && doc
341 .get("system")
342 .is_some_and(|s| !prose::value_has_cache_control(s))
343 && super::cache_breakpoint::inject_anthropic_system(&mut doc)
344 {
345 modified = true;
346 cache_safety::record_breakpoint_injected();
347 }
348 if repack {
353 cache_safety::record_cold_repack();
354 }
355 cache_safety::record(prose_segments, true);
356
357 let system_val = doc.get("system");
360 let messages_for_id = doc.get("messages").and_then(Value::as_array);
361 if let Some(msgs) = messages_for_id {
362 let conv_id = prefix_replay::conversation_id(system_val, msgs);
363 if sticky_tools::ensure_tool_present(conv_id, &mut doc) {
364 modified = true;
365 prefix_cache_stats::record_sticky_injection();
366 }
367 }
368
369 prefix_cache_stats::record_frozen_count(cached as u64);
370
371 let system_val_replay = doc.get("system");
374 let msgs_replay = doc.get("messages").and_then(Value::as_array);
375 let out = if let Some(msgs) = msgs_replay {
376 let conv_id = prefix_replay::conversation_id(system_val_replay, msgs);
377 if let Some(delta) = prefix_replay::detect_append_only(conv_id, msgs) {
378 let delta_msgs = &msgs[delta.delta_start..];
379 if let Some(replayed) = prefix_replay::overlay_prefix(&delta.prefix_bytes, delta_msgs) {
380 prefix_cache_stats::record_replay_hit();
381 let original_bytes = serde_json::to_vec(&doc).unwrap_or_default();
382 prefix_cache_stats::record_delta(
383 original_bytes.len() as u64,
384 replayed.len() as u64,
385 );
386 replayed
387 } else {
388 prefix_cache_stats::record_replay_miss();
389 serde_json::to_vec(&doc).unwrap_or_default()
390 }
391 } else {
392 prefix_cache_stats::record_replay_miss();
393 serde_json::to_vec(&doc).unwrap_or_default()
394 }
395 } else {
396 serde_json::to_vec(&doc).unwrap_or_default()
397 };
398 let compressed_size = if modified { out.len() } else { original_size };
399 (out, original_size, compressed_size)
400}
401
402fn compress_content_field(
405 content: &mut Value,
406 tool_name: Option<&str>,
407 kind: ToolResultKind,
408) -> bool {
409 match content {
410 Value::String(s) => super::tool_output::compress_text(s, tool_name, kind),
411 Value::Array(arr) => {
412 let mut modified = false;
413 for item in arr.iter_mut() {
414 if item.get("type").and_then(|t| t.as_str()) == Some("text")
415 && let Some(Value::String(text)) = item.get_mut("text")
416 {
417 modified |= super::tool_output::compress_text(text, tool_name, kind);
418 }
419 }
420 modified
421 }
422 _ => false,
423 }
424}
425
426#[cfg(test)]
427mod tests {
428 use super::super::compress::compress_tool_result;
429 use super::*;
430
431 fn source_file_body() -> Vec<u8> {
432 let code = (0..60)
433 .map(|i| format!(" let binding_{i} = compute_value_{i}(context, options);"))
434 .collect::<Vec<_>>()
435 .join("\n");
436 let body = serde_json::json!({
437 "model": "claude-opus-4-8",
438 "messages": [
439 {
440 "role": "assistant",
441 "content": [{"type": "tool_use", "id": "toolu_1", "name": "Read", "input": {"file_path": "src/app.rs"}}]
442 },
443 {
444 "role": "user",
445 "content": [{"type": "tool_result", "tool_use_id": "toolu_1", "content": code}]
446 }
447 ]
448 });
449 serde_json::to_vec(&body).unwrap()
450 }
451
452 #[test]
453 fn read_tool_result_is_never_truncated() {
454 let bytes = source_file_body();
455 let body: Value = serde_json::from_slice(&bytes).unwrap();
456 let (out, _orig, _comp) = compress_request_body(body, bytes.len());
457 let parsed: Value = serde_json::from_slice(&out).unwrap();
458 let content = parsed["messages"][1]["content"][0]["content"]
459 .as_str()
460 .unwrap();
461 assert!(
462 content.contains("binding_59"),
463 "the full source body must survive — refactors need it intact"
464 );
465 assert!(!content.contains("lines omitted"));
466 }
467
468 fn forge_log_body(tool_name: &str) -> Value {
469 let mut log = String::new();
473 for i in 0..90 {
474 log.push_str(&format!(
475 "INFO processing item {i}: ok, latency={i}ms, queue depth normal, retries 0\n"
476 ));
477 }
478 serde_json::json!({
479 "messages": [
480 {"role": "assistant", "content": [{"type": "tool_use", "id": "f1", "name": tool_name, "input": {}}]},
481 {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "f1", "content": log}]}
482 ]
483 })
484 }
485
486 #[test]
487 fn forge_shell_tool_result_compresses() {
488 let body = forge_log_body("forge_shell");
491 let bytes = serde_json::to_vec(&body).unwrap();
492 let (_out, orig, comp) = compress_request_body(body, bytes.len());
493 assert!(comp < orig, "foreign shell output must be compressed");
494 }
495
496 #[test]
497 fn foreign_read_tool_protects_source() {
498 let code = (0..60)
501 .map(|i| format!(" let binding_{i} = compute_value_{i}(context, options);"))
502 .collect::<Vec<_>>()
503 .join("\n");
504 let body = serde_json::json!({
505 "messages": [
506 {"role": "assistant", "content": [{"type": "tool_use", "id": "r1", "name": "forge_read", "input": {"path": "src/app.rs"}}]},
507 {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "r1", "content": code}]}
508 ]
509 });
510 let bytes = serde_json::to_vec(&body).unwrap();
511 let (out, _orig, _comp) = compress_request_body(body, bytes.len());
512 let parsed: Value = serde_json::from_slice(&out).unwrap();
513 let content = parsed["messages"][1]["content"][0]["content"]
514 .as_str()
515 .unwrap();
516 assert!(
517 content.contains("binding_59"),
518 "source body must survive intact"
519 );
520 }
521
522 #[test]
523 fn compress_request_body_is_deterministic() {
524 let _lock = crate::core::data_dir::test_env_lock();
527 let bytes = serde_json::to_vec(&forge_log_body("Bash")).unwrap();
530 let a = compress_request_body(serde_json::from_slice(&bytes).unwrap(), bytes.len()).0;
531 let b = compress_request_body(serde_json::from_slice(&bytes).unwrap(), bytes.len()).0;
532 assert_eq!(a, b, "identical input must yield byte-identical output");
533 }
534
535 fn big_log() -> String {
537 (0..200)
538 .map(|i| format!("[info] processed item {i:04} ok, latency {i}ms, queue normal"))
539 .collect::<Vec<_>>()
540 .join("\n")
541 }
542
543 #[test]
544 fn inband_ccr_emit_echo_splice_round_trip() {
545 let _iso = crate::core::data_dir::isolated_data_dir();
549 crate::test_env::remove_var("LEAN_CTX_PROXY_CCR_INBAND");
550 crate::core::config::Config::update_global(|c| {
551 c.proxy.ccr_inband = Some(true);
552 })
553 .unwrap();
554
555 let log = big_log();
557 let emit = serde_json::json!({
558 "messages": [
559 {"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "bash", "input": {}}]},
560 {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": log}]}
561 ]
562 });
563 let bytes = serde_json::to_vec(&emit).unwrap();
564 let (out, _o, _c) = compress_request_body(emit, bytes.len());
565 let emitted: Value = serde_json::from_slice(&out).unwrap();
566 let stub = emitted["messages"][1]["content"][0]["content"]
567 .as_str()
568 .unwrap();
569 assert!(
570 stub.contains("<lc_expand:"),
571 "in-band stub must advertise an echo-able marker: {stub}"
572 );
573 assert!(
574 !stub.contains("/tee/proxy_"),
575 "in-band stub must not leak the unreachable local tee path: {stub}"
576 );
577
578 let start = stub.find("<lc_expand:").unwrap();
580 let end = stub[start..].find('>').unwrap() + start + 1;
581 let marker = &stub[start..end];
582
583 let echo = serde_json::json!({
586 "messages": [
587 {"role": "user", "content": [{"type": "text", "text": "look again"}]},
588 {"role": "assistant", "content": format!("revisiting that output: {marker}")}
589 ]
590 });
591 let bytes = serde_json::to_vec(&echo).unwrap();
592 let (out, _o, _c) = compress_request_body(echo, bytes.len());
593 let spliced: Value = serde_json::from_slice(&out).unwrap();
594 let assistant = spliced["messages"][1]["content"].as_str().unwrap();
595 assert!(
596 assistant.contains("processed item 0007 ok")
597 && assistant.contains("processed item 0199 ok"),
598 "the verbatim original must be spliced back in full: {assistant}"
599 );
600 assert!(
601 !assistant.contains("<lc_expand:"),
602 "the marker must be consumed by the splice"
603 );
604 }
605
606 #[test]
607 fn inband_marker_less_turn_is_byte_identical_on_or_off() {
608 let _iso = crate::core::data_dir::isolated_data_dir();
613 crate::test_env::remove_var("LEAN_CTX_PROXY_CCR_INBAND");
614 let body = serde_json::json!({
615 "messages": [
616 {"role": "user", "content": [{"type": "text", "text": "hello there"}]},
617 {"role": "assistant", "content": "hi — how can I help?"}
618 ]
619 });
620 let bytes = serde_json::to_vec(&body).unwrap();
621
622 crate::core::config::Config::update_global(|c| c.proxy.ccr_inband = Some(false)).unwrap();
623 let off = compress_request_body(body.clone(), bytes.len()).0;
624 crate::core::config::Config::update_global(|c| c.proxy.ccr_inband = Some(true)).unwrap();
625 let on = compress_request_body(body, bytes.len()).0;
626
627 assert_eq!(
628 off, on,
629 "a marker-less request must be byte-identical whether in-band is on or off"
630 );
631 }
632
633 fn big_prose() -> String {
635 let p = "You are a careful, senior software engineer. You always explain your \
636 reasoning before making changes, you prefer small reviewable diffs, and \
637 you never introduce mock data or placeholders into production code. ";
638 [p; 6].join("\n")
639 }
640
641 #[test]
642 fn system_prose_compressed_and_assistant_untouched() {
643 let _iso = crate::core::data_dir::isolated_data_dir();
644 crate::core::config::Config::update_global(|c| {
645 c.proxy.role_aggressiveness.system = Some(0.6);
646 c.proxy.role_aggressiveness.user = Some(0.6);
647 })
648 .unwrap();
649
650 let prose = big_prose();
651 let assistant_text = big_prose();
652 let body = serde_json::json!({
653 "model": "claude-opus-4-8",
654 "system": prose,
655 "messages": [
656 {"role": "user", "content": [{"type": "text", "text": prose}]},
657 {"role": "assistant", "content": assistant_text},
658 ]
659 });
660 let bytes = serde_json::to_vec(&body).unwrap();
661 let (out, _orig, _comp) = compress_request_body(body, bytes.len());
662 let parsed: Value = serde_json::from_slice(&out).unwrap();
663
664 assert!(
665 parsed["system"].as_str().unwrap().len() < prose.len(),
666 "system prose must be compressed when enabled"
667 );
668 assert_eq!(
669 parsed["messages"][1]["content"].as_str().unwrap(),
670 assistant_text,
671 "assistant turns must pass through verbatim (#710)"
672 );
673 }
674
675 #[test]
676 fn user_prose_compressed_only_in_frozen_region() {
677 let _iso = crate::core::data_dir::isolated_data_dir();
678 crate::core::config::Config::update_global(|c| {
679 c.proxy.role_aggressiveness.user = Some(0.7);
680 })
681 .unwrap();
682
683 let prose = big_prose();
684 let mut messages = Vec::new();
686 for i in 0..30 {
687 let role = if i % 2 == 0 { "user" } else { "assistant" };
688 messages.push(serde_json::json!({
689 "role": role,
690 "content": [{"type": "text", "text": prose}]
691 }));
692 }
693 let body = serde_json::json!({ "messages": messages });
694 let bytes = serde_json::to_vec(&body).unwrap();
695 let (out, _o, _c) = compress_request_body(body, bytes.len());
696 let parsed: Value = serde_json::from_slice(&out).unwrap();
697
698 let frozen_user = parsed["messages"][0]["content"][0]["text"]
699 .as_str()
700 .unwrap();
701 assert!(
702 frozen_user.len() < prose.len(),
703 "user prose in the frozen region must be compressed"
704 );
705 assert_eq!(
706 parsed["messages"][1]["content"][0]["text"]
707 .as_str()
708 .unwrap(),
709 prose,
710 "assistant prose is never compressed"
711 );
712 let live_tail_user = parsed["messages"][28]["content"][0]["text"]
713 .as_str()
714 .unwrap();
715 assert_eq!(
716 live_tail_user, prose,
717 "user prose in the live tail (>= boundary) must be preserved for quality"
718 );
719 }
720
721 #[test]
722 fn client_cached_prefix_disables_system_prose() {
723 let _iso = crate::core::data_dir::isolated_data_dir();
724 crate::core::config::Config::update_global(|c| {
725 c.proxy.role_aggressiveness.system = Some(0.9);
726 })
727 .unwrap();
728
729 let prose = big_prose();
730 let body = serde_json::json!({
731 "system": prose,
732 "messages": [
733 {"role": "user", "content": [
734 {"type": "text", "text": "hi", "cache_control": {"type": "ephemeral"}}
735 ]},
736 {"role": "assistant", "content": "ok"}
737 ]
738 });
739 let bytes = serde_json::to_vec(&body).unwrap();
740 let (out, _o, _c) = compress_request_body(body, bytes.len());
741 let parsed: Value = serde_json::from_slice(&out).unwrap();
742 assert_eq!(
743 parsed["system"].as_str().unwrap(),
744 prose,
745 "system must stay verbatim when the client caches a message prefix (#448)"
746 );
747 }
748
749 #[test]
750 fn prose_compression_is_deterministic() {
751 let _iso = crate::core::data_dir::isolated_data_dir();
752 crate::core::config::Config::update_global(|c| {
753 c.proxy.role_aggressiveness.system = Some(0.6);
754 })
755 .unwrap();
756 let prose = big_prose();
757 let mk = || serde_json::json!({"system": prose, "messages": [{"role": "user", "content": "hi"}]});
758 let (a, b) = (mk(), mk());
759 let la = serde_json::to_vec(&a).unwrap().len();
760 let lb = serde_json::to_vec(&b).unwrap().len();
761 assert_eq!(
762 compress_request_body(a, la).0,
763 compress_request_body(b, lb).0,
764 "prose compression must be byte-identical for identical input (#498)"
765 );
766 }
767
768 #[test]
769 fn bash_tool_result_still_compresses() {
770 let log = {
771 let mut s = String::from(
772 "$ git status\nOn branch main\nYour branch is up to date with 'origin/main'.\n\nChanges not staged for commit:\n (use \"git add <file>...\" to update what will be committed)\n",
773 );
774 for i in 0..90 {
775 s.push_str(&format!("\tmodified: src/module_{i}/file_{i}.rs\n"));
776 }
777 s.push_str("\nno changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
778 s
779 };
780 let body = serde_json::json!({
781 "messages": [
782 {"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "Bash", "input": {}}]},
783 {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": log}]}
784 ]
785 });
786 let bytes = serde_json::to_vec(&body).unwrap();
787 let (_out, orig, comp) = compress_request_body(body, bytes.len());
788 assert!(comp < orig, "shell output must still be compressed");
789 }
790
791 #[test]
792 fn json_envelope_tool_result_is_compressed() {
793 let _iso = crate::core::data_dir::isolated_data_dir();
794 let log = long_git_status();
795 let expected = compress_tool_result(&log, Some("Bash"));
796 let envelope = serde_json::to_string(&serde_json::json!({
797 "content": [{"type": "text", "text": log}],
798 "isError": false,
799 }))
800 .unwrap();
801 let body = serde_json::json!({
802 "messages": [
803 {"role": "assistant", "content": [{
804 "type": "tool_use",
805 "id": "t1",
806 "name": "Bash",
807 "input": {}
808 }]},
809 {"role": "user", "content": [{
810 "type": "tool_result",
811 "tool_use_id": "t1",
812 "content": envelope
813 }]}
814 ]
815 });
816 let bytes = serde_json::to_vec(&body).unwrap();
817 let (out, orig, comp) = compress_request_body(body, bytes.len());
818
819 assert!(comp < orig, "JSON envelope tool result should shrink");
820 let parsed: Value = serde_json::from_slice(&out).unwrap();
821 let content = parsed["messages"][1]["content"][0]["content"]
822 .as_str()
823 .unwrap();
824 let envelope: Value = serde_json::from_str(content).unwrap();
825 assert_eq!(envelope["content"][0]["text"].as_str().unwrap(), expected);
826 }
827
828 fn long_git_status() -> String {
829 let mut s = String::from(
830 "$ git status\nOn branch main\nYour branch is up to date with 'origin/main'.\n\nChanges not staged for commit:\n (use \"git add <file>...\" to update what will be committed)\n",
831 );
832 for i in 0..80 {
833 s.push_str(&format!("\tmodified: src/module_{i}/file_{i}.rs\n"));
834 }
835 s.push_str("\nno changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
836 s
837 }
838
839 fn cached_prefix_body(first_text: &str, prose: &str) -> (Vec<Value>, Value) {
849 let messages = vec![
850 serde_json::json!({"role": "user", "content": [
851 {"type": "text", "text": first_text, "cache_control": {"type": "ephemeral"}}
852 ]}),
853 serde_json::json!({"role": "assistant", "content": "ok"}),
854 ];
855 let body = serde_json::json!({ "system": prose, "messages": messages.clone() });
856 (messages, body)
857 }
858
859 #[test]
860 fn cold_prefix_repack_rewrites_protected_system_prose_when_enabled() {
861 let _iso = crate::core::data_dir::isolated_data_dir();
862 crate::test_env::remove_var("LEAN_CTX_PROXY_COLD_PREFIX_REPACK");
863 crate::core::config::Config::update_global(|c| {
864 c.proxy.role_aggressiveness.system = Some(0.9);
865 c.proxy.cold_prefix_repack = Some(true);
866 })
867 .unwrap();
868
869 let prose = big_prose().repeat(6);
874 let (messages, body) = cached_prefix_body("cold-repack-enabled-session", &prose);
875 super::super::cold_prefix::test_seed_last_touch(&messages, 3 * 60 * 60);
877
878 let bytes = serde_json::to_vec(&body).unwrap();
879 let (out, _o, _c) = compress_request_body(body, bytes.len());
880 let parsed: Value = serde_json::from_slice(&out).unwrap();
881 assert!(
882 parsed["system"].as_str().unwrap().len() < prose.len(),
883 "a predicted-cold prefix must let the proxy repack the otherwise-protected system prose"
884 );
885 }
886
887 #[test]
888 fn cold_prefix_repack_skipped_for_subcacheable_prefix_by_default() {
889 let _iso = crate::core::data_dir::isolated_data_dir();
895 crate::test_env::remove_var("LEAN_CTX_PROXY_COLD_PREFIX_REPACK");
896 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_POLICY");
897 crate::core::config::Config::update_global(|c| {
898 c.proxy.role_aggressiveness.system = Some(0.9);
899 c.proxy.cold_prefix_repack = Some(true);
900 })
901 .unwrap();
902
903 let prose = big_prose();
904 let (messages, body) = cached_prefix_body("cold-repack-subcacheable-session", &prose);
905 super::super::cold_prefix::test_seed_last_touch(&messages, 3 * 60 * 60);
906
907 let bytes = serde_json::to_vec(&body).unwrap();
908 let (out, _o, _c) = compress_request_body(body, bytes.len());
909 let parsed: Value = serde_json::from_slice(&out).unwrap();
910 assert_eq!(
911 parsed["system"].as_str().unwrap(),
912 prose,
913 "the net-cost gate must skip repacking a sub-cacheable prefix (premium default)"
914 );
915 }
916
917 #[test]
918 fn cold_prefix_repack_off_by_default_keeps_prefix_protected() {
919 let _iso = crate::core::data_dir::isolated_data_dir();
920 crate::test_env::remove_var("LEAN_CTX_PROXY_COLD_PREFIX_REPACK");
921 crate::core::config::Config::update_global(|c| {
922 c.proxy.role_aggressiveness.system = Some(0.9);
923 c.proxy.cold_prefix_repack = Some(false);
924 })
925 .unwrap();
926
927 let prose = big_prose();
928 let (messages, body) = cached_prefix_body("cold-repack-disabled-session", &prose);
929 super::super::cold_prefix::test_seed_last_touch(&messages, 24 * 60 * 60);
931
932 let bytes = serde_json::to_vec(&body).unwrap();
933 let (out, _o, _c) = compress_request_body(body, bytes.len());
934 let parsed: Value = serde_json::from_slice(&out).unwrap();
935 assert_eq!(
936 parsed["system"].as_str().unwrap(),
937 prose,
938 "with repack off the cached prefix stays byte-stable regardless of idle time (#448)"
939 );
940 }
941
942 #[test]
943 fn cold_prefix_repack_protects_warm_prefix_even_when_enabled() {
944 let _iso = crate::core::data_dir::isolated_data_dir();
945 crate::test_env::remove_var("LEAN_CTX_PROXY_COLD_PREFIX_REPACK");
946 crate::core::config::Config::update_global(|c| {
947 c.proxy.role_aggressiveness.system = Some(0.9);
948 c.proxy.cold_prefix_repack = Some(true);
949 })
950 .unwrap();
951
952 let prose = big_prose();
953 let (messages, body) = cached_prefix_body("cold-repack-warm-session", &prose);
954 super::super::cold_prefix::test_seed_last_touch(&messages, 60);
956
957 let bytes = serde_json::to_vec(&body).unwrap();
958 let (out, _o, _c) = compress_request_body(body, bytes.len());
959 let parsed: Value = serde_json::from_slice(&out).unwrap();
960 assert_eq!(
961 parsed["system"].as_str().unwrap(),
962 prose,
963 "a warm prefix must stay protected even with repack enabled — only LARGE gaps trigger"
964 );
965 }
966
967 #[test]
968 fn cache_policy_attribution_is_measurement_only() {
969 let _iso = crate::core::data_dir::isolated_data_dir();
973 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_POLICY");
974 crate::test_env::remove_var("LEAN_CTX_PROXY_COLD_PREFIX_REPACK");
975
976 let prose = big_prose();
977 let (_messages, body) = cached_prefix_body("cache-policy-measurement-session", &prose);
978 let bytes = serde_json::to_vec(&body).unwrap();
979
980 crate::core::config::Config::update_global(|c| {
981 c.proxy.cache_policy = Some(false);
982 })
983 .unwrap();
984 let (off, _o, _c) = compress_request_body(body.clone(), bytes.len());
985
986 crate::core::config::Config::update_global(|c| {
987 c.proxy.cache_policy = Some(true);
988 })
989 .unwrap();
990 let (on, _o, _c) = compress_request_body(body, bytes.len());
991
992 assert_eq!(
993 off, on,
994 "miss attribution is measurement-only: the wire bytes must not change"
995 );
996 }
997
998 #[test]
999 fn effort_control_dials_adaptive_thinking_only() {
1000 let _iso = crate::core::data_dir::isolated_data_dir();
1003 crate::test_env::remove_var("LEAN_CTX_PROXY_EFFORT");
1004 crate::core::config::Config::update_global(|c| {
1005 c.proxy.effort = Some("medium".into());
1006 })
1007 .unwrap();
1008
1009 let adaptive = serde_json::json!({
1010 "model": "claude-opus-4-8",
1011 "thinking": {"type": "adaptive"},
1012 "messages": [{"role": "user", "content": "hi"}]
1013 });
1014 let bytes = serde_json::to_vec(&adaptive).unwrap();
1015 let (out, _o, _c) = compress_request_body(adaptive, bytes.len());
1016 assert_eq!(
1017 serde_json::from_slice::<Value>(&out).unwrap()["output_config"]["effort"],
1018 "medium"
1019 );
1020
1021 let plain = serde_json::json!({
1024 "model": "claude-opus-4-8",
1025 "messages": [{"role": "user", "content": "hi"}]
1026 });
1027 let bytes = serde_json::to_vec(&plain).unwrap();
1028 let (out, _o, _c) = compress_request_body(plain, bytes.len());
1029 assert!(
1030 serde_json::from_slice::<Value>(&out)
1031 .unwrap()
1032 .get("output_config")
1033 .is_none()
1034 );
1035 }
1036
1037 #[test]
1038 fn verbosity_steer_applies_to_treatment_skips_control() {
1039 let _iso = crate::core::data_dir::isolated_data_dir();
1042 crate::test_env::remove_var("LEAN_CTX_PROXY_VERBOSITY_STEER");
1043 crate::test_env::remove_var("LEAN_CTX_PROXY_OUTPUT_HOLDOUT");
1044 crate::test_env::remove_var("LEAN_CTX_PROXY_EFFORT");
1045
1046 let req = serde_json::json!({
1047 "model": "claude-opus-4-8",
1048 "messages": [{"role": "user", "content": "Summarize the design."}]
1049 });
1050 let bytes = serde_json::to_vec(&req).unwrap();
1051
1052 crate::core::config::Config::update_global(|c| {
1054 c.proxy.verbosity_steer = Some(true);
1055 c.proxy.output_holdout = Some(0.0);
1056 })
1057 .unwrap();
1058 let (out, _o, _c) = compress_request_body(req.clone(), bytes.len());
1059 let v: Value = serde_json::from_slice(&out).unwrap();
1060 assert!(
1061 v["messages"][0]["content"]
1062 .as_str()
1063 .unwrap()
1064 .contains(crate::proxy::verbosity::STEER),
1065 "treatment arm must receive the verbosity steer"
1066 );
1067
1068 crate::core::config::Config::update_global(|c| {
1070 c.proxy.output_holdout = Some(1.0);
1071 })
1072 .unwrap();
1073 let (out2, _o, _c) = compress_request_body(req, bytes.len());
1074 let v2: Value = serde_json::from_slice(&out2).unwrap();
1075 assert!(
1076 !v2["messages"][0]["content"]
1077 .as_str()
1078 .unwrap()
1079 .contains(crate::proxy::verbosity::STEER),
1080 "control arm must NOT be steered (measurement baseline)"
1081 );
1082 }
1083
1084 fn cacheable_system() -> String {
1087 "You are a careful, senior software engineer who writes maintainable code. ".repeat(400)
1088 }
1089
1090 #[test]
1091 fn cache_breakpoint_injected_on_unanchored_system_when_opt_in() {
1092 let _iso = crate::core::data_dir::isolated_data_dir();
1095 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_BREAKPOINT");
1096 let body = serde_json::json!({
1097 "model": "claude-opus-4-8",
1098 "system": cacheable_system(),
1099 "messages": [{"role": "user", "content": "Refactor the parser."}]
1100 });
1101 let bytes = serde_json::to_vec(&body).unwrap();
1102
1103 crate::core::config::Config::update_global(|c| c.proxy.cache_breakpoint = Some(true))
1104 .unwrap();
1105 let (out, _o, _c) = compress_request_body(body, bytes.len());
1106 let v: Value = serde_json::from_slice(&out).unwrap();
1107
1108 assert_eq!(
1109 v["system"][0]["cache_control"]["type"], "ephemeral",
1110 "an unanchored system prompt must receive one ephemeral breakpoint"
1111 );
1112 assert!(
1113 v["system"][0]["text"]
1114 .as_str()
1115 .unwrap()
1116 .contains("senior software engineer"),
1117 "the system text must be preserved verbatim under the marker"
1118 );
1119 }
1120
1121 #[test]
1122 fn cache_breakpoint_off_by_default_is_byte_unchanged() {
1123 let _iso = crate::core::data_dir::isolated_data_dir();
1126 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_BREAKPOINT");
1127 let body = serde_json::json!({
1128 "model": "claude-opus-4-8",
1129 "system": cacheable_system(),
1130 "messages": [{"role": "user", "content": "Refactor the parser."}]
1131 });
1132 let bytes = serde_json::to_vec(&body).unwrap();
1133 let (out, _o, _c) = compress_request_body(body, bytes.len());
1134 assert_eq!(
1135 out, bytes,
1136 "default-off must leave the request byte-identical"
1137 );
1138 }
1139
1140 #[test]
1141 fn cache_breakpoint_respects_client_anchor() {
1142 let _iso = crate::core::data_dir::isolated_data_dir();
1145 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_BREAKPOINT");
1146 let body = serde_json::json!({
1147 "model": "claude-opus-4-8",
1148 "system": cacheable_system(),
1149 "messages": [{
1150 "role": "user",
1151 "content": [{
1152 "type": "text",
1153 "text": "hello",
1154 "cache_control": {"type": "ephemeral"}
1155 }]
1156 }]
1157 });
1158 let bytes = serde_json::to_vec(&body).unwrap();
1159 crate::core::config::Config::update_global(|c| c.proxy.cache_breakpoint = Some(true))
1160 .unwrap();
1161 let (out, _o, _c) = compress_request_body(body, bytes.len());
1162 let v: Value = serde_json::from_slice(&out).unwrap();
1163 assert!(
1164 v["system"].is_string(),
1165 "with a client anchor present, system must be left untouched (no second breakpoint)"
1166 );
1167 }
1168
1169 #[test]
1170 fn cache_aligner_measures_without_mutating_body() {
1171 let _iso = crate::core::data_dir::isolated_data_dir();
1175 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_ALIGNER");
1176 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_BREAKPOINT");
1177 let body = serde_json::json!({
1178 "model": "claude-opus-4-8",
1179 "system": "Today is 2026-06-22. Session 550e8400-e29b-41d4-a716-446655440000.",
1180 "messages": [{"role": "user", "content": "Hello."}]
1181 });
1182 let bytes = serde_json::to_vec(&body).unwrap();
1183 crate::core::config::Config::update_global(|c| c.proxy.cache_aligner = Some(true)).unwrap();
1184 let (out, _o, _c) = compress_request_body(body, bytes.len());
1185 assert_eq!(
1186 out, bytes,
1187 "cache-aligner telemetry must never mutate the request body"
1188 );
1189 }
1190
1191 fn cacheable_system_with_date() -> String {
1194 format!("Today is 2026-06-27. {}", cacheable_system())
1195 }
1196
1197 fn clear_relocate_env() {
1198 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_ALIGN_RELOCATE");
1199 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_BREAKPOINT");
1200 crate::test_env::remove_var("LEAN_CTX_PROXY_CACHE_ALIGNER");
1201 crate::test_env::remove_var("LEAN_CTX_PROXY_OUTPUT_HOLDOUT");
1202 }
1203
1204 #[test]
1205 fn cache_align_relocate_moves_volatiles_to_tail_when_opt_in() {
1206 let _iso = crate::core::data_dir::isolated_data_dir();
1210 clear_relocate_env();
1211 let body = serde_json::json!({
1212 "model": "claude-opus-4-8",
1213 "system": cacheable_system_with_date(),
1214 "messages": [{"role": "user", "content": "Refactor the parser."}]
1215 });
1216 let bytes = serde_json::to_vec(&body).unwrap();
1217 crate::core::config::Config::update_global(|c| {
1218 c.proxy.cache_align_relocate = Some(true);
1219 c.proxy.output_holdout = Some(0.0);
1220 })
1221 .unwrap();
1222 let (out, _o, _c) = compress_request_body(body, bytes.len());
1223 let v: Value = serde_json::from_slice(&out).unwrap();
1224
1225 assert!(v["system"].is_array(), "system reshaped into a block array");
1226 assert_eq!(v["system"][0]["cache_control"]["type"], "ephemeral");
1227 assert!(
1228 !v["system"][0]["text"]
1229 .as_str()
1230 .unwrap()
1231 .contains("2026-06-27"),
1232 "the volatile date must leave the cacheable prefix"
1233 );
1234 assert!(
1235 v["system"][1].get("cache_control").is_none(),
1236 "the relocated tail block stays uncached"
1237 );
1238 assert!(
1239 v["system"][1]["text"]
1240 .as_str()
1241 .unwrap()
1242 .contains("2026-06-27"),
1243 "the date must be re-stated in the tail"
1244 );
1245 }
1246
1247 #[test]
1248 fn cache_align_relocate_off_by_default_is_byte_unchanged() {
1249 let _iso = crate::core::data_dir::isolated_data_dir();
1252 clear_relocate_env();
1253 let body = serde_json::json!({
1254 "model": "claude-opus-4-8",
1255 "system": cacheable_system_with_date(),
1256 "messages": [{"role": "user", "content": "Refactor the parser."}]
1257 });
1258 let bytes = serde_json::to_vec(&body).unwrap();
1259 let (out, _o, _c) = compress_request_body(body, bytes.len());
1260 assert_eq!(
1261 out, bytes,
1262 "default-off must leave the request byte-identical"
1263 );
1264 }
1265
1266 #[test]
1267 fn cache_align_relocate_skips_control_arm() {
1268 let _iso = crate::core::data_dir::isolated_data_dir();
1271 clear_relocate_env();
1272 let body = serde_json::json!({
1273 "model": "claude-opus-4-8",
1274 "system": cacheable_system_with_date(),
1275 "messages": [{"role": "user", "content": "Refactor the parser."}]
1276 });
1277 let bytes = serde_json::to_vec(&body).unwrap();
1278 crate::core::config::Config::update_global(|c| {
1279 c.proxy.cache_align_relocate = Some(true);
1280 c.proxy.output_holdout = Some(1.0);
1281 })
1282 .unwrap();
1283 let (out, _o, _c) = compress_request_body(body, bytes.len());
1284 let v: Value = serde_json::from_slice(&out).unwrap();
1285 assert!(
1286 v["system"].is_string(),
1287 "control arm must not be relocated (measurement baseline)"
1288 );
1289 }
1290
1291 #[test]
1292 fn cache_align_relocate_respects_client_anchor() {
1293 let _iso = crate::core::data_dir::isolated_data_dir();
1296 clear_relocate_env();
1297 let body = serde_json::json!({
1298 "model": "claude-opus-4-8",
1299 "system": cacheable_system_with_date(),
1300 "messages": [{
1301 "role": "user",
1302 "content": [{
1303 "type": "text",
1304 "text": "hello",
1305 "cache_control": {"type": "ephemeral"}
1306 }]
1307 }]
1308 });
1309 let bytes = serde_json::to_vec(&body).unwrap();
1310 crate::core::config::Config::update_global(|c| {
1311 c.proxy.cache_align_relocate = Some(true);
1312 c.proxy.output_holdout = Some(0.0);
1313 })
1314 .unwrap();
1315 let (out, _o, _c) = compress_request_body(body, bytes.len());
1316 let v: Value = serde_json::from_slice(&out).unwrap();
1317 assert!(
1318 v["system"].is_string(),
1319 "with a client anchor present, system must be left untouched"
1320 );
1321 }
1322
1323 #[test]
1324 fn cache_align_relocate_composes_with_breakpoint_to_one_anchor() {
1325 let _iso = crate::core::data_dir::isolated_data_dir();
1329 clear_relocate_env();
1330 let body = serde_json::json!({
1331 "model": "claude-opus-4-8",
1332 "system": cacheable_system_with_date(),
1333 "messages": [{"role": "user", "content": "Refactor the parser."}]
1334 });
1335 let bytes = serde_json::to_vec(&body).unwrap();
1336 crate::core::config::Config::update_global(|c| {
1337 c.proxy.cache_align_relocate = Some(true);
1338 c.proxy.cache_breakpoint = Some(true);
1339 c.proxy.output_holdout = Some(0.0);
1340 })
1341 .unwrap();
1342 let (out, _o, _c) = compress_request_body(body, bytes.len());
1343 let v: Value = serde_json::from_slice(&out).unwrap();
1344 let blocks = v["system"].as_array().expect("system is a block array");
1345 assert_eq!(blocks.len(), 2, "stable block + volatile tail");
1346 assert_eq!(blocks[0]["cache_control"]["type"], "ephemeral");
1347 assert!(
1348 blocks[1].get("cache_control").is_none(),
1349 "no second breakpoint — the tail stays uncached"
1350 );
1351 }
1352}