Skip to main content

lean_ctx/proxy/
anthropic.rs

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::compress::compress_tool_result;
11use super::forward;
12use super::tool_kind::{self, ToolResultKind, should_protect};
13use super::{cache_safety, prose};
14use crate::core::config::{HistoryMode, ProseRole};
15
16pub async fn handler(
17    State(state): State<ProxyState>,
18    req: Request<Body>,
19) -> Result<Response, StatusCode> {
20    let upstream = state.anthropic_upstream();
21    forward::forward_request(
22        State(state),
23        req,
24        &upstream,
25        "/v1/messages",
26        compress_request_body,
27        "Anthropic",
28        &[],
29    )
30    .await
31}
32
33fn compress_request_body(parsed: Value, original_size: usize) -> (Vec<u8>, usize, usize) {
34    let mut doc = parsed;
35    let mut modified = false;
36
37    // Opt-in per-role prose aggressiveness (#710). Both default to `None`, in
38    // which case nothing below fires and the body is byte-for-byte unchanged.
39    let cfg = crate::core::config::Config::load();
40    let system_aggr = cfg.proxy.resolved_role_aggressiveness(ProseRole::System);
41    let user_aggr = cfg.proxy.resolved_role_aggressiveness(ProseRole::User);
42    let live_compress = cfg.proxy.live_compresses();
43    let mode = cfg.proxy.resolved_history_mode();
44    // #493: in-band CCR expansion (opt-in). Splice any <lc_expand:HASH> the model
45    // echoed back into the verbatim original from the local tee store. A strict
46    // no-op when no marker is present (byte-identical body → cache-safe). Runs
47    // before the meter-only short-circuit so an explicit expand request is
48    // honored even when the proxy is otherwise byte-passthrough.
49    if cfg.proxy.ccr_inband_enabled() {
50        modified |= super::ccr::splice_inband_in_place(&mut doc);
51    }
52    // #834: cache-safe cross-provider effort control. Default off → no-op. The
53    // value is a constant, so it never perturbs the prompt-cache prefix; it only
54    // dials an *existing* adaptive thinking request (never enables thinking the
55    // client didn't ask for).
56    if let Some(effort) = cfg.proxy.resolved_effort() {
57        modified |= super::effort::apply_anthropic(&mut doc, effort);
58    }
59    // Meter-only (#481): live compression off, no history pruning, no prose
60    // rewriting → forward + usage metering still run, but the body is left
61    // unchanged so the provider prompt-cache prefix stays byte-stable. A pending
62    // in-band splice (`modified`) opts out: the body did change this turn.
63    if !live_compress
64        && mode == HistoryMode::Off
65        && system_aggr.is_none()
66        && user_aggr.is_none()
67        && !modified
68    {
69        let out = serde_json::to_vec(&doc).unwrap_or_default();
70        return (out, original_size, original_size);
71    }
72    let mut prose_segments: u64 = 0;
73
74    // Length of the client's provider-cached message prefix. Needed both for
75    // cache-safe pruning below and to gate top-level system prose: if any
76    // message is client-cached, `system` (which precedes every message) is part
77    // of that cached prefix and must not be rewritten.
78    let cached = doc
79        .get("messages")
80        .and_then(|m| m.as_array())
81        .map_or(0, |m| super::history_prune::cached_prefix_len(m));
82
83    // #480: opt-in big-gap cold-prefix repack. When enabled AND the proxy can
84    // confidently predict (from idle time vs the provider cache TTL) that the
85    // client-cached prefix is already cold, override the normal "never touch the
86    // cached prefix" rule for THIS request and prune/compress the prefix too,
87    // re-seeding a leaner cache. Default-off; never fires without a measured idle
88    // gap past TTL × margin, so warm caches stay byte-stable (#448).
89    let repack = cfg.proxy.repacks_cold_prefix()
90        && doc
91            .get("messages")
92            .and_then(|m| m.as_array())
93            .is_some_and(|m| super::cold_prefix::repack_decision(m, cached));
94    // The prefix length the rewrites below must protect: the full cached prefix
95    // normally, or 0 when we are intentionally repacking the cold prefix.
96    let protect = if repack { 0 } else { cached };
97
98    // System prose: only when nothing is client-cached and the `system` field
99    // carries no `cache_control` of its own — otherwise it anchors the cache.
100    // A cold-prefix repack (`protect == 0` with `repack`) deliberately rewrites
101    // it to re-seed a leaner cache.
102    if let Some(a) = system_aggr
103        && protect == 0
104        && let Some(system) = doc.get_mut("system")
105        && (repack || !prose::value_has_cache_control(system))
106    {
107        let n = prose::compress_system_value(system, a);
108        if n > 0 {
109            prose_segments += u64::from(n);
110            modified = true;
111        }
112    }
113
114    if let Some(messages) = doc.get_mut("messages").and_then(|m| m.as_array_mut()) {
115        // Resolve tool-call id → tool name so file/source reads can be protected
116        // from lossy compression that would force the model to re-read mid-task.
117        let tool_names = tool_kind::anthropic_tool_names(messages);
118
119        // Prune at a frozen, cache-aware boundary by default: Anthropic's
120        // prompt cache matches exact prefixes, so the boundary must not move
121        // every turn (see `history_prune::prune_boundary`). `mode` resolved above.
122        let boundary = super::history_prune::prune_boundary(mode, messages.len());
123        // Never rewrite content the client has marked with `cache_control`:
124        // pruning inside the already-cached prefix invalidates Anthropic's
125        // prompt cache from the first changed message (#448). Pruning therefore
126        // starts after the last breakpoint; with no breakpoint this is 0, i.e.
127        // the previous behaviour.
128        modified |=
129            super::history_prune::prune_history_range(messages, protect, boundary, &tool_names);
130
131        for msg in messages.iter_mut() {
132            let role = msg.get("role").and_then(|r| r.as_str()).unwrap_or("");
133            if role != "user" {
134                continue;
135            }
136
137            if let Some(content) = msg.get_mut("content").and_then(|c| c.as_array_mut()) {
138                for block in content.iter_mut() {
139                    if block.get("type").and_then(|t| t.as_str()) != Some("tool_result") {
140                        continue;
141                    }
142
143                    let name = block
144                        .get("tool_use_id")
145                        .and_then(|v| v.as_str())
146                        .and_then(|id| tool_names.get(id))
147                        .map(String::as_str);
148                    let kind = name.map_or(ToolResultKind::Other, tool_kind::classify_tool_name);
149
150                    // #481: skip live compression when globally off or when the
151                    // originating tool is on the exclusion list (Serena default).
152                    let excluded =
153                        name.is_some_and(|n| cfg.proxy.is_tool_live_compress_excluded(n));
154                    if live_compress
155                        && !excluded
156                        && let Some(inner_content) = block.get_mut("content")
157                    {
158                        modified |= compress_content_field(inner_content, name, kind);
159                    }
160                }
161            }
162        }
163
164        // Frozen-region user prose: free-text `text` blocks of user turns in
165        // `[cached, boundary)`. Cache-safe by construction — the cached prefix
166        // and the live tail (`>= boundary`) are both left intact, and the
167        // rewrite is content-deterministic so the prefix stays byte-stable.
168        if let Some(a) = user_aggr {
169            let end = boundary.min(messages.len());
170            let start = protect.min(end);
171            for msg in &mut messages[start..end] {
172                if msg.get("role").and_then(|r| r.as_str()) == Some("user")
173                    && let Some(content) = msg.get_mut("content").and_then(|c| c.as_array_mut())
174                {
175                    prose_segments += u64::from(prose::compress_text_blocks(content, a));
176                }
177            }
178        }
179    }
180
181    if prose_segments > 0 {
182        modified = true;
183    }
184    // A deliberate cold-prefix repack (#480) is the one sanctioned exception to
185    // the frozen-window rule; count it on its own gauge so it never dilutes the
186    // cache-safe ratio (which exists to catch *accidental* #448 regressions).
187    // Every other rewrite lands strictly inside the cache-safe frozen window.
188    if repack {
189        cache_safety::record_cold_repack();
190    }
191    cache_safety::record(prose_segments, true);
192
193    let out = serde_json::to_vec(&doc).unwrap_or_default();
194    let compressed_size = if modified { out.len() } else { original_size };
195    (out, original_size, compressed_size)
196}
197
198/// Compresses a tool_result `content` field unless it is a protected file/source
199/// read, which must reach the model intact (it is what gets edited).
200fn compress_content_field(
201    content: &mut Value,
202    tool_name: Option<&str>,
203    kind: ToolResultKind,
204) -> bool {
205    match content {
206        Value::String(s) => {
207            if should_protect(kind, s) {
208                return false;
209            }
210            let compressed = compress_tool_result(s, tool_name);
211            if compressed.len() < s.len() {
212                *s = compressed;
213                return true;
214            }
215            false
216        }
217        Value::Array(arr) => {
218            let mut modified = false;
219            for item in arr.iter_mut() {
220                if item.get("type").and_then(|t| t.as_str()) == Some("text")
221                    && let Some(text) = item
222                        .get_mut("text")
223                        .and_then(|t| t.as_str().map(String::from))
224                {
225                    if should_protect(kind, &text) {
226                        continue;
227                    }
228                    let compressed = compress_tool_result(&text, tool_name);
229                    if compressed.len() < text.len() {
230                        item["text"] = Value::String(compressed);
231                        modified = true;
232                    }
233                }
234            }
235            modified
236        }
237        _ => false,
238    }
239}
240
241#[cfg(test)]
242mod tests {
243    use super::*;
244
245    fn source_file_body() -> Vec<u8> {
246        let code = (0..60)
247            .map(|i| format!("    let binding_{i} = compute_value_{i}(context, options);"))
248            .collect::<Vec<_>>()
249            .join("\n");
250        let body = serde_json::json!({
251            "model": "claude-opus-4-8",
252            "messages": [
253                {
254                    "role": "assistant",
255                    "content": [{"type": "tool_use", "id": "toolu_1", "name": "Read", "input": {"file_path": "src/app.rs"}}]
256                },
257                {
258                    "role": "user",
259                    "content": [{"type": "tool_result", "tool_use_id": "toolu_1", "content": code}]
260                }
261            ]
262        });
263        serde_json::to_vec(&body).unwrap()
264    }
265
266    #[test]
267    fn read_tool_result_is_never_truncated() {
268        let bytes = source_file_body();
269        let body: Value = serde_json::from_slice(&bytes).unwrap();
270        let (out, _orig, _comp) = compress_request_body(body, bytes.len());
271        let parsed: Value = serde_json::from_slice(&out).unwrap();
272        let content = parsed["messages"][1]["content"][0]["content"]
273            .as_str()
274            .unwrap();
275        assert!(
276            content.contains("binding_59"),
277            "the full source body must survive — refactors need it intact"
278        );
279        assert!(!content.contains("lines omitted"));
280    }
281
282    fn forge_log_body(tool_name: &str) -> Value {
283        // Generic, highly-repetitive log with no `$ cmd` hint, so routing falls
284        // back to the tool name (exercising the foreign-tool classification)
285        // and the generic compressor (not a command-specific pattern).
286        let mut log = String::new();
287        for i in 0..90 {
288            log.push_str(&format!(
289                "INFO  processing item {i}: ok, latency={i}ms, queue depth normal, retries 0\n"
290            ));
291        }
292        serde_json::json!({
293            "messages": [
294                {"role": "assistant", "content": [{"type": "tool_use", "id": "f1", "name": tool_name, "input": {}}]},
295                {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "f1", "content": log}]}
296            ]
297        })
298    }
299
300    #[test]
301    fn forge_shell_tool_result_compresses() {
302        // A vendor-prefixed foreign shell tool reaches the proxy; its log output
303        // must still be compressed (rtk/ctx_* never see another server's tools).
304        let body = forge_log_body("forge_shell");
305        let bytes = serde_json::to_vec(&body).unwrap();
306        let (_out, orig, comp) = compress_request_body(body, bytes.len());
307        assert!(comp < orig, "foreign shell output must be compressed");
308    }
309
310    #[test]
311    fn foreign_read_tool_protects_source() {
312        // `forge_read` is classified FileRead via the segment fallback, so the
313        // source body must reach the model intact (it is what gets edited).
314        let code = (0..60)
315            .map(|i| format!("    let binding_{i} = compute_value_{i}(context, options);"))
316            .collect::<Vec<_>>()
317            .join("\n");
318        let body = serde_json::json!({
319            "messages": [
320                {"role": "assistant", "content": [{"type": "tool_use", "id": "r1", "name": "forge_read", "input": {"path": "src/app.rs"}}]},
321                {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "r1", "content": code}]}
322            ]
323        });
324        let bytes = serde_json::to_vec(&body).unwrap();
325        let (out, _orig, _comp) = compress_request_body(body, bytes.len());
326        let parsed: Value = serde_json::from_slice(&out).unwrap();
327        let content = parsed["messages"][1]["content"][0]["content"]
328            .as_str()
329            .unwrap();
330        assert!(
331            content.contains("binding_59"),
332            "source body must survive intact"
333        );
334    }
335
336    #[test]
337    fn compress_request_body_is_deterministic() {
338        // tee path depends on the data dir; serialize env access so a parallel
339        // test never swaps LEAN_CTX_DATA_DIR between the two compressions.
340        let _lock = crate::core::data_dir::test_env_lock();
341        // #498: the proxy rewrite must be a pure function of the body so the
342        // provider prompt-cache prefix stays byte-identical across turns.
343        let bytes = serde_json::to_vec(&forge_log_body("Bash")).unwrap();
344        let a = compress_request_body(serde_json::from_slice(&bytes).unwrap(), bytes.len()).0;
345        let b = compress_request_body(serde_json::from_slice(&bytes).unwrap(), bytes.len()).0;
346        assert_eq!(a, b, "identical input must yield byte-identical output");
347    }
348
349    /// A large, highly-compressible foreign log so the live path tees + stubs it.
350    fn big_log() -> String {
351        (0..200)
352            .map(|i| format!("[info] processed item {i:04} ok, latency {i}ms, queue normal"))
353            .collect::<Vec<_>>()
354            .join("\n")
355    }
356
357    #[test]
358    fn inband_ccr_emit_echo_splice_round_trip() {
359        // Full #493 cycle through the real Anthropic request path: a lossy stub
360        // emits an <lc_expand:HASH> marker, the model echoes it, and the proxy
361        // splices the verbatim original back inline on the next request.
362        let _iso = crate::core::data_dir::isolated_data_dir();
363        crate::test_env::remove_var("LEAN_CTX_PROXY_CCR_INBAND");
364        crate::core::config::Config::update_global(|c| {
365            c.proxy.ccr_inband = Some(true);
366        })
367        .unwrap();
368
369        // EMIT: live-compress a foreign tool_result → recovery stub with a marker.
370        let log = big_log();
371        let emit = serde_json::json!({
372            "messages": [
373                {"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "bash", "input": {}}]},
374                {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": log}]}
375            ]
376        });
377        let bytes = serde_json::to_vec(&emit).unwrap();
378        let (out, _o, _c) = compress_request_body(emit, bytes.len());
379        let emitted: Value = serde_json::from_slice(&out).unwrap();
380        let stub = emitted["messages"][1]["content"][0]["content"]
381            .as_str()
382            .unwrap();
383        assert!(
384            stub.contains("<lc_expand:"),
385            "in-band stub must advertise an echo-able marker: {stub}"
386        );
387        assert!(
388            !stub.contains("/tee/proxy_"),
389            "in-band stub must not leak the unreachable local tee path: {stub}"
390        );
391
392        // The marker the model would copy into its next turn.
393        let start = stub.find("<lc_expand:").unwrap();
394        let end = stub[start..].find('>').unwrap() + start + 1;
395        let marker = &stub[start..end];
396
397        // ECHO + SPLICE: the model echoes the marker; the proxy splices the
398        // verbatim original (recovered from the local tee store) back inline.
399        let echo = serde_json::json!({
400            "messages": [
401                {"role": "user", "content": [{"type": "text", "text": "look again"}]},
402                {"role": "assistant", "content": format!("revisiting that output: {marker}")}
403            ]
404        });
405        let bytes = serde_json::to_vec(&echo).unwrap();
406        let (out, _o, _c) = compress_request_body(echo, bytes.len());
407        let spliced: Value = serde_json::from_slice(&out).unwrap();
408        let assistant = spliced["messages"][1]["content"].as_str().unwrap();
409        assert!(
410            assistant.contains("processed item 0007 ok")
411                && assistant.contains("processed item 0199 ok"),
412            "the verbatim original must be spliced back in full: {assistant}"
413        );
414        assert!(
415            !assistant.contains("<lc_expand:"),
416            "the marker must be consumed by the splice"
417        );
418    }
419
420    #[test]
421    fn inband_marker_less_turn_is_byte_identical_on_or_off() {
422        // Cache-safety (#493): enabling in-band must be a strict no-op on a turn
423        // with no marker — same bytes on the wire, so the provider cache prefix is
424        // never perturbed unless the model actually asked to expand. Uses a body
425        // with nothing to prune/compress, isolating the splice from stub emission.
426        let _iso = crate::core::data_dir::isolated_data_dir();
427        crate::test_env::remove_var("LEAN_CTX_PROXY_CCR_INBAND");
428        let body = serde_json::json!({
429            "messages": [
430                {"role": "user", "content": [{"type": "text", "text": "hello there"}]},
431                {"role": "assistant", "content": "hi — how can I help?"}
432            ]
433        });
434        let bytes = serde_json::to_vec(&body).unwrap();
435
436        crate::core::config::Config::update_global(|c| c.proxy.ccr_inband = Some(false)).unwrap();
437        let off = compress_request_body(body.clone(), bytes.len()).0;
438        crate::core::config::Config::update_global(|c| c.proxy.ccr_inband = Some(true)).unwrap();
439        let on = compress_request_body(body, bytes.len()).0;
440
441        assert_eq!(
442            off, on,
443            "a marker-less request must be byte-identical whether in-band is on or off"
444        );
445    }
446
447    /// Long, duplicate-rich natural-language prose that compresses cleanly.
448    fn big_prose() -> String {
449        let p = "You are a careful, senior software engineer. You always explain your \
450                 reasoning before making changes, you prefer small reviewable diffs, and \
451                 you never introduce mock data or placeholders into production code. ";
452        [p; 6].join("\n")
453    }
454
455    #[test]
456    fn system_prose_compressed_and_assistant_untouched() {
457        let _iso = crate::core::data_dir::isolated_data_dir();
458        crate::core::config::Config::update_global(|c| {
459            c.proxy.role_aggressiveness.system = Some(0.6);
460            c.proxy.role_aggressiveness.user = Some(0.6);
461        })
462        .unwrap();
463
464        let prose = big_prose();
465        let assistant_text = big_prose();
466        let body = serde_json::json!({
467            "model": "claude-opus-4-8",
468            "system": prose,
469            "messages": [
470                {"role": "user", "content": [{"type": "text", "text": prose}]},
471                {"role": "assistant", "content": assistant_text},
472            ]
473        });
474        let bytes = serde_json::to_vec(&body).unwrap();
475        let (out, _orig, _comp) = compress_request_body(body, bytes.len());
476        let parsed: Value = serde_json::from_slice(&out).unwrap();
477
478        assert!(
479            parsed["system"].as_str().unwrap().len() < prose.len(),
480            "system prose must be compressed when enabled"
481        );
482        assert_eq!(
483            parsed["messages"][1]["content"].as_str().unwrap(),
484            assistant_text,
485            "assistant turns must pass through verbatim (#710)"
486        );
487    }
488
489    #[test]
490    fn user_prose_compressed_only_in_frozen_region() {
491        let _iso = crate::core::data_dir::isolated_data_dir();
492        crate::core::config::Config::update_global(|c| {
493            c.proxy.role_aggressiveness.user = Some(0.7);
494        })
495        .unwrap();
496
497        let prose = big_prose();
498        // 30 messages → cache-aware boundary = ((30-8)/16)*16 = 16.
499        let mut messages = Vec::new();
500        for i in 0..30 {
501            let role = if i % 2 == 0 { "user" } else { "assistant" };
502            messages.push(serde_json::json!({
503                "role": role,
504                "content": [{"type": "text", "text": prose}]
505            }));
506        }
507        let body = serde_json::json!({ "messages": messages });
508        let bytes = serde_json::to_vec(&body).unwrap();
509        let (out, _o, _c) = compress_request_body(body, bytes.len());
510        let parsed: Value = serde_json::from_slice(&out).unwrap();
511
512        let frozen_user = parsed["messages"][0]["content"][0]["text"]
513            .as_str()
514            .unwrap();
515        assert!(
516            frozen_user.len() < prose.len(),
517            "user prose in the frozen region must be compressed"
518        );
519        assert_eq!(
520            parsed["messages"][1]["content"][0]["text"]
521                .as_str()
522                .unwrap(),
523            prose,
524            "assistant prose is never compressed"
525        );
526        let live_tail_user = parsed["messages"][28]["content"][0]["text"]
527            .as_str()
528            .unwrap();
529        assert_eq!(
530            live_tail_user, prose,
531            "user prose in the live tail (>= boundary) must be preserved for quality"
532        );
533    }
534
535    #[test]
536    fn client_cached_prefix_disables_system_prose() {
537        let _iso = crate::core::data_dir::isolated_data_dir();
538        crate::core::config::Config::update_global(|c| {
539            c.proxy.role_aggressiveness.system = Some(0.9);
540        })
541        .unwrap();
542
543        let prose = big_prose();
544        let body = serde_json::json!({
545            "system": prose,
546            "messages": [
547                {"role": "user", "content": [
548                    {"type": "text", "text": "hi", "cache_control": {"type": "ephemeral"}}
549                ]},
550                {"role": "assistant", "content": "ok"}
551            ]
552        });
553        let bytes = serde_json::to_vec(&body).unwrap();
554        let (out, _o, _c) = compress_request_body(body, bytes.len());
555        let parsed: Value = serde_json::from_slice(&out).unwrap();
556        assert_eq!(
557            parsed["system"].as_str().unwrap(),
558            prose,
559            "system must stay verbatim when the client caches a message prefix (#448)"
560        );
561    }
562
563    #[test]
564    fn prose_compression_is_deterministic() {
565        let _iso = crate::core::data_dir::isolated_data_dir();
566        crate::core::config::Config::update_global(|c| {
567            c.proxy.role_aggressiveness.system = Some(0.6);
568        })
569        .unwrap();
570        let prose = big_prose();
571        let mk = || serde_json::json!({"system": prose, "messages": [{"role": "user", "content": "hi"}]});
572        let (a, b) = (mk(), mk());
573        let la = serde_json::to_vec(&a).unwrap().len();
574        let lb = serde_json::to_vec(&b).unwrap().len();
575        assert_eq!(
576            compress_request_body(a, la).0,
577            compress_request_body(b, lb).0,
578            "prose compression must be byte-identical for identical input (#498)"
579        );
580    }
581
582    #[test]
583    fn bash_tool_result_still_compresses() {
584        let log = {
585            let mut s = String::from(
586                "$ 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",
587            );
588            for i in 0..90 {
589                s.push_str(&format!("\tmodified:   src/module_{i}/file_{i}.rs\n"));
590            }
591            s.push_str("\nno changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
592            s
593        };
594        let body = serde_json::json!({
595            "messages": [
596                {"role": "assistant", "content": [{"type": "tool_use", "id": "t1", "name": "Bash", "input": {}}]},
597                {"role": "user", "content": [{"type": "tool_result", "tool_use_id": "t1", "content": log}]}
598            ]
599        });
600        let bytes = serde_json::to_vec(&body).unwrap();
601        let (_out, orig, comp) = compress_request_body(body, bytes.len());
602        assert!(comp < orig, "shell output must still be compressed");
603    }
604
605    /// A client-cached message anchors the prefix; `system` precedes it, so the
606    /// cached prefix is `cached > 0` and system prose is normally protected.
607    /// System-prose verbatim-vs-rewritten is therefore a clean binary signal for
608    /// whether the #480 cold-prefix repack fired.
609    ///
610    /// `first_text` must be UNIQUE per test: it is `messages[0]`, which the
611    /// cold-prefix tracker hashes into the conversation key. A shared global
612    /// last-touch store has no test-clear hook (that would race with the unit
613    /// tests), so distinct keys are how parallel tests stay isolated.
614    fn cached_prefix_body(first_text: &str, prose: &str) -> (Vec<Value>, Value) {
615        let messages = vec![
616            serde_json::json!({"role": "user", "content": [
617                {"type": "text", "text": first_text, "cache_control": {"type": "ephemeral"}}
618            ]}),
619            serde_json::json!({"role": "assistant", "content": "ok"}),
620        ];
621        let body = serde_json::json!({ "system": prose, "messages": messages.clone() });
622        (messages, body)
623    }
624
625    #[test]
626    fn cold_prefix_repack_rewrites_protected_system_prose_when_enabled() {
627        let _iso = crate::core::data_dir::isolated_data_dir();
628        crate::test_env::remove_var("LEAN_CTX_PROXY_COLD_PREFIX_REPACK");
629        crate::core::config::Config::update_global(|c| {
630            c.proxy.role_aggressiveness.system = Some(0.9);
631            c.proxy.cold_prefix_repack = Some(true);
632        })
633        .unwrap();
634
635        let prose = big_prose();
636        let (messages, body) = cached_prefix_body("cold-repack-enabled-session", &prose);
637        // Predict cold: last touched 3h ago, well past the 5m default TTL × margin.
638        super::super::cold_prefix::test_seed_last_touch(&messages, 3 * 60 * 60);
639
640        let bytes = serde_json::to_vec(&body).unwrap();
641        let (out, _o, _c) = compress_request_body(body, bytes.len());
642        let parsed: Value = serde_json::from_slice(&out).unwrap();
643        assert!(
644            parsed["system"].as_str().unwrap().len() < prose.len(),
645            "a predicted-cold prefix must let the proxy repack the otherwise-protected system prose"
646        );
647    }
648
649    #[test]
650    fn cold_prefix_repack_off_by_default_keeps_prefix_protected() {
651        let _iso = crate::core::data_dir::isolated_data_dir();
652        crate::test_env::remove_var("LEAN_CTX_PROXY_COLD_PREFIX_REPACK");
653        crate::core::config::Config::update_global(|c| {
654            c.proxy.role_aggressiveness.system = Some(0.9);
655            c.proxy.cold_prefix_repack = Some(false);
656        })
657        .unwrap();
658
659        let prose = big_prose();
660        let (messages, body) = cached_prefix_body("cold-repack-disabled-session", &prose);
661        // Even with a huge idle gap, default-off must never touch the prefix.
662        super::super::cold_prefix::test_seed_last_touch(&messages, 24 * 60 * 60);
663
664        let bytes = serde_json::to_vec(&body).unwrap();
665        let (out, _o, _c) = compress_request_body(body, bytes.len());
666        let parsed: Value = serde_json::from_slice(&out).unwrap();
667        assert_eq!(
668            parsed["system"].as_str().unwrap(),
669            prose,
670            "with repack off the cached prefix stays byte-stable regardless of idle time (#448)"
671        );
672    }
673
674    #[test]
675    fn cold_prefix_repack_protects_warm_prefix_even_when_enabled() {
676        let _iso = crate::core::data_dir::isolated_data_dir();
677        crate::test_env::remove_var("LEAN_CTX_PROXY_COLD_PREFIX_REPACK");
678        crate::core::config::Config::update_global(|c| {
679            c.proxy.role_aggressiveness.system = Some(0.9);
680            c.proxy.cold_prefix_repack = Some(true);
681        })
682        .unwrap();
683
684        let prose = big_prose();
685        let (messages, body) = cached_prefix_body("cold-repack-warm-session", &prose);
686        // Warm: touched 1 minute ago → the prediction must keep protecting.
687        super::super::cold_prefix::test_seed_last_touch(&messages, 60);
688
689        let bytes = serde_json::to_vec(&body).unwrap();
690        let (out, _o, _c) = compress_request_body(body, bytes.len());
691        let parsed: Value = serde_json::from_slice(&out).unwrap();
692        assert_eq!(
693            parsed["system"].as_str().unwrap(),
694            prose,
695            "a warm prefix must stay protected even with repack enabled — only LARGE gaps trigger"
696        );
697    }
698
699    #[test]
700    fn effort_control_dials_adaptive_thinking_only() {
701        // #834 end-to-end: fill output_config.effort when the client already
702        // asked for adaptive thinking, but never enable thinking otherwise.
703        let _iso = crate::core::data_dir::isolated_data_dir();
704        crate::test_env::remove_var("LEAN_CTX_PROXY_EFFORT");
705        crate::core::config::Config::update_global(|c| {
706            c.proxy.effort = Some("medium".into());
707        })
708        .unwrap();
709
710        let adaptive = serde_json::json!({
711            "model": "claude-opus-4-8",
712            "thinking": {"type": "adaptive"},
713            "messages": [{"role": "user", "content": "hi"}]
714        });
715        let bytes = serde_json::to_vec(&adaptive).unwrap();
716        let (out, _o, _c) = compress_request_body(adaptive, bytes.len());
717        assert_eq!(
718            serde_json::from_slice::<Value>(&out).unwrap()["output_config"]["effort"],
719            "medium"
720        );
721
722        // No thinking field → the proxy must not add output_config (no surprise
723        // reasoning cost, no 400 risk).
724        let plain = serde_json::json!({
725            "model": "claude-opus-4-8",
726            "messages": [{"role": "user", "content": "hi"}]
727        });
728        let bytes = serde_json::to_vec(&plain).unwrap();
729        let (out, _o, _c) = compress_request_body(plain, bytes.len());
730        assert!(
731            serde_json::from_slice::<Value>(&out)
732                .unwrap()
733                .get("output_config")
734                .is_none()
735        );
736    }
737}