Skip to main content

lean_ctx/proxy/
google.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::forward;
11use super::tool_kind::{self, ToolResultKind};
12use super::{cache_safety, prose};
13use crate::core::config::{HistoryMode, ProseRole};
14
15pub async fn handler(
16    State(state): State<ProxyState>,
17    req: Request<Body>,
18) -> Result<Response, StatusCode> {
19    let upstream = state.gemini_upstream();
20    // Gemini carries the model in the URL path, not the body — capture it here so
21    // the effort applier can pick the right thinking control (#840).
22    let model = super::usage::gemini_model_from_path(req.uri().path());
23    forward::forward_request(
24        State(state),
25        req,
26        &upstream,
27        "/",
28        move |body, size| compress_request_body(body, size, model.as_deref()),
29        "Gemini",
30        &["application/x-ndjson"],
31    )
32    .await
33}
34
35pub(super) fn compress_request_body(
36    parsed: Value,
37    original_size: usize,
38    model: Option<&str>,
39) -> (Vec<u8>, usize, usize) {
40    let mut doc = parsed;
41    let mut modified = false;
42
43    // Opt-in per-role prose aggressiveness (#710); both default `None` → no-op.
44    let cfg = crate::core::config::Config::load();
45    let system_aggr = cfg.proxy.resolved_role_aggressiveness(ProseRole::System);
46    let user_aggr = cfg.proxy.resolved_role_aggressiveness(ProseRole::User);
47    let live_compress = cfg.proxy.live_compresses();
48    let mode = cfg.proxy.resolved_history_mode();
49    // #895 Track B: output-savings holdout arm, from the pristine body (before any
50    // mutation below) so it matches the arm the response meter records. Control
51    // conversations skip output-shaping but are still metered. Default 0 → Treatment.
52    let arm = super::holdout::assign(
53        &super::holdout::google_key(&doc),
54        cfg.proxy.output_holdout_fraction(),
55    );
56    // #493: in-band CCR expansion (opt-in). Splice any <lc_expand:HASH> the model
57    // echoed back into the verbatim original from the local tee store. A strict
58    // no-op when no marker is present (byte-identical body → cache-safe). Runs
59    // before the meter-only short-circuit so an explicit expand request is
60    // honored even when the proxy is otherwise byte-passthrough.
61    if cfg.proxy.ccr_inband_enabled() {
62        modified |= super::ccr::splice_inband_in_place(&mut doc);
63    }
64    // #834/#840: cache-safe cross-provider effort control. Default off → no-op.
65    // The level is a constant, so it never perturbs the prompt-cache prefix; it
66    // sets generationConfig.thinkingConfig (thinkingLevel on 3.x, thinkingBudget
67    // on 2.5 pro/flash) only for models that accept it and only when the client
68    // didn't pin its own thinking field. `model` is read from the URL path.
69    if arm == super::holdout::Arm::Treatment {
70        if let Some(effort) = cfg.proxy.resolved_effort() {
71            modified |= super::effort::apply_google(&mut doc, effort, model);
72        }
73        // #895: cache-safe wire verbosity steer; control arm skips it (measured).
74        if cfg.proxy.verbosity_steer_enabled() {
75            modified |= super::verbosity::apply_google(&mut doc);
76        }
77    }
78    // Meter-only (#481): no live compression, no history pruning, no prose → the
79    // body is forwarded unchanged while usage metering still runs. A pending
80    // in-band splice (`modified`) opts out: the body did change this turn.
81    if !live_compress
82        && mode == HistoryMode::Off
83        && system_aggr.is_none()
84        && user_aggr.is_none()
85        && !modified
86    {
87        let out = serde_json::to_vec(&doc).unwrap_or_default();
88        return (out, original_size, original_size);
89    }
90    let mut prose_segments: u64 = 0;
91
92    // System prose: the top-level `systemInstruction` anchor. Gemini has no
93    // client `cache_control`, and the rewrite is deterministic, so the implicit
94    // prefix cache stays byte-stable across turns — cache-safe by construction.
95    if let Some(a) = system_aggr {
96        for key in ["systemInstruction", "system_instruction"] {
97            if let Some(parts) = doc
98                .get_mut(key)
99                .and_then(|si| si.get_mut("parts"))
100                .and_then(|p| p.as_array_mut())
101            {
102                prose_segments += u64::from(prose::compress_gemini_text_parts(parts, a));
103            }
104        }
105    }
106
107    if let Some(contents) = doc.get_mut("contents").and_then(|c| c.as_array_mut()) {
108        // Gemini's implicit prompt cache is prefix-based, so the frozen OLD
109        // region is pruned at the same monotone staircase boundary as every
110        // other rail. We never remove a `contents` entry — only rewrite the
111        // `functionResponse` text in place — so the conversation structure
112        // (and any `functionCall` ↔ `functionResponse` correspondence) is intact.
113        // `mode` resolved above.
114        let boundary = super::history_prune::prune_boundary(mode, contents.len());
115
116        for (idx, content) in contents.iter_mut().enumerate() {
117            let in_old_region = idx < boundary;
118            // Own the role before the mutable `parts` borrow below.
119            let role = content
120                .get("role")
121                .and_then(|r| r.as_str())
122                .map(String::from);
123            let Some(parts) = content.get_mut("parts").and_then(|p| p.as_array_mut()) else {
124                continue;
125            };
126            for part in parts.iter_mut() {
127                let Some(func_resp) = part.get_mut("functionResponse") else {
128                    continue;
129                };
130                // Gemini carries the originating function name inline — route it
131                // to the compressor (not `None`) so tool-specific patterns apply.
132                let name = func_resp
133                    .get("name")
134                    .and_then(|v| v.as_str())
135                    .map(String::from);
136                let kind = name
137                    .as_deref()
138                    .map_or(ToolResultKind::Other, tool_kind::classify_tool_name);
139                // #481: recent-region live compression respects the global toggle
140                // and the per-tool exclusion list (Serena default). Old-region
141                // pruning stays governed by `history_mode`.
142                let live = live_compress
143                    && !name
144                        .as_deref()
145                        .is_some_and(|n| cfg.proxy.is_tool_live_compress_excluded(n));
146                let Some(response) = func_resp.get_mut("response") else {
147                    continue;
148                };
149                for field in ["result", "content"] {
150                    modified |= if in_old_region {
151                        prune_string_field(response, field, kind)
152                    } else if live {
153                        compress_string_field(response, field, name.as_deref(), kind)
154                    } else {
155                        false
156                    };
157                }
158            }
159
160            // Frozen-region user prose: free-text `text` parts of user turns in
161            // the old region `[0, boundary)`. Model turns (assistant) and tool
162            // I/O parts are never touched.
163            if in_old_region
164                && role.as_deref() == Some("user")
165                && let Some(a) = user_aggr
166            {
167                prose_segments += u64::from(prose::compress_gemini_text_parts(parts, a));
168            }
169        }
170    }
171
172    if prose_segments > 0 {
173        modified = true;
174    }
175    cache_safety::record(prose_segments, true);
176
177    let out = serde_json::to_vec(&doc).unwrap_or_default();
178    let compressed_size = if modified { out.len() } else { original_size };
179    (out, original_size, compressed_size)
180}
181
182/// Compress a recent `functionResponse.response.<field>` string. `tool_name` is
183/// routed to the compressor so tool-specific patterns (git status, ls, …) apply;
184/// protected file/source reads in the recent region are left intact.
185fn compress_string_field(
186    obj: &mut Value,
187    field: &str,
188    tool_name: Option<&str>,
189    kind: ToolResultKind,
190) -> bool {
191    if let Some(val) = obj
192        .get_mut(field)
193        .and_then(|v| v.as_str().map(String::from))
194    {
195        let mut val = val;
196        if super::tool_output::compress_text(&mut val, tool_name, kind) {
197            obj[field] = Value::String(val);
198            return true;
199        }
200    }
201    false
202}
203
204/// Cache-aware prune of an OLD `functionResponse.response.<field>`: file/source
205/// reads collapse to a re-read stub, everything else head/tail summarizes.
206/// Content-deterministic, so the cached prefix stays byte-stable across turns.
207fn prune_string_field(obj: &mut Value, field: &str, kind: ToolResultKind) -> bool {
208    if let Some(val) = obj
209        .get_mut(field)
210        .and_then(|v| v.as_str().map(String::from))
211    {
212        let mut val = val;
213        if super::tool_output::prune_text(&mut val, kind) {
214            obj[field] = Value::String(val);
215            return true;
216        }
217    }
218    false
219}
220
221#[cfg(test)]
222mod tests {
223    use super::super::compress::compress_tool_result;
224    use super::*;
225
226    /// `pairs` Gemini turns: a `model` `functionCall` then the `user`
227    /// `functionResponse` carrying a long file read.
228    fn gemini_read_turns(pairs: usize) -> Vec<Value> {
229        let code = (0..40)
230            .map(|i| format!("    let v{i} = compute_{i}(ctx, opts);"))
231            .collect::<Vec<_>>()
232            .join("\n");
233        let mut contents = Vec::new();
234        for t in 0..pairs {
235            contents.push(serde_json::json!({
236                "role": "model",
237                "parts": [{"functionCall": {"name": "read_file", "args": {}}}]
238            }));
239            contents.push(serde_json::json!({
240                "role": "user",
241                "parts": [{"functionResponse": {
242                    "name": "read_file",
243                    "response": {"result": format!("{code}\n// turn {t}")}
244                }}]
245            }));
246        }
247        contents
248    }
249
250    #[test]
251    fn recent_response_routes_tool_name_to_compressor() {
252        // Default (isolated) config; single content → boundary 0 → recent path.
253        let _iso = crate::core::data_dir::isolated_data_dir();
254        // A compressible search result. The proxy must route the inline tool name
255        // to the shared engine, so its output matches the name-routed engine
256        // byte-for-byte (the contract that distinguishes this from `None`).
257        // `infer_command`'s use of the name is unit-tested in `compress.rs`.
258        let raw = (0..60)
259            .map(|i| format!("src/matches.rs:{i}:    let matched = find(foo, bar, baz);"))
260            .collect::<Vec<_>>()
261            .join("\n");
262        let routed = compress_tool_result(&raw, Some("search_files"));
263        assert!(routed.len() < raw.len(), "fixture must be compressible");
264
265        let body = serde_json::json!({
266            "contents": [
267                {"role": "user", "parts": [{"functionResponse": {
268                    "name": "search_files", "response": {"result": raw}
269                }}]}
270            ]
271        });
272        let bytes = serde_json::to_vec(&body).unwrap();
273        let (out, orig, comp) = compress_request_body(body, bytes.len(), None);
274        assert!(comp < orig, "recent response must be compressed");
275        let parsed: Value = serde_json::from_slice(&out).unwrap();
276        assert_eq!(
277            parsed["contents"][0]["parts"][0]["functionResponse"]["response"]["result"]
278                .as_str()
279                .unwrap(),
280            routed,
281            "Gemini path must route the inline tool name to the shared compressor"
282        );
283    }
284
285    #[test]
286    fn json_envelope_function_response_is_compressed() {
287        let _iso = crate::core::data_dir::isolated_data_dir();
288        let raw = long_git_status();
289        let expected = compress_tool_result(&raw, Some("Bash"));
290        let envelope = serde_json::to_string(&serde_json::json!({
291            "content": [{"type": "text", "text": raw}],
292            "isError": false,
293        }))
294        .unwrap();
295        let body = serde_json::json!({
296            "contents": [
297                {"role": "user", "parts": [{"functionResponse": {
298                    "name": "Bash",
299                    "response": {"result": envelope}
300                }}]}
301            ]
302        });
303        let bytes = serde_json::to_vec(&body).unwrap();
304        let (out, orig, comp) = compress_request_body(body, bytes.len(), None);
305
306        assert!(comp < orig, "JSON envelope function response should shrink");
307        let parsed: Value = serde_json::from_slice(&out).unwrap();
308        let result = parsed["contents"][0]["parts"][0]["functionResponse"]["response"]["result"]
309            .as_str()
310            .unwrap();
311        let envelope: Value = serde_json::from_str(result).unwrap();
312        assert_eq!(envelope["content"][0]["text"].as_str().unwrap(), expected);
313    }
314
315    #[test]
316    fn cache_aware_prune_stubs_old_reads_keeps_recent() {
317        let _iso = crate::core::data_dir::isolated_data_dir();
318        // 13 pairs = 26 contents → staircase boundary 16.
319        let contents = gemini_read_turns(13);
320        let n = contents.len();
321        let body = serde_json::json!({ "contents": contents });
322        let bytes = serde_json::to_vec(&body).unwrap();
323        let (out, orig, comp) = compress_request_body(body, bytes.len(), None);
324        assert!(comp < orig, "old reads must be pruned for savings");
325
326        let parsed: Value = serde_json::from_slice(&out).unwrap();
327        let got = parsed["contents"].as_array().unwrap();
328        assert_eq!(got.len(), n, "no contents may be removed");
329        // OLD file read (content index 1, before boundary 16) is stubbed.
330        let old = got[1]["parts"][0]["functionResponse"]["response"]["result"]
331            .as_str()
332            .unwrap();
333        assert!(
334            old.contains("Re-read the file"),
335            "old read should be stubbed, got: {old}"
336        );
337        // RECENT file read (content index 25, after the boundary) keeps its body.
338        let recent = got[25]["parts"][0]["functionResponse"]["response"]["result"]
339            .as_str()
340            .unwrap();
341        assert!(
342            recent.contains("v39"),
343            "recent read must be protected, got: {recent}"
344        );
345    }
346
347    fn long_git_status() -> String {
348        let mut s = String::from(
349            "$ 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",
350        );
351        for i in 0..80 {
352            s.push_str(&format!("\tmodified:   src/module_{i}/file_{i}.rs\n"));
353        }
354        s.push_str("\nno changes added to commit (use \"git add\" and/or \"git commit -a\")\n");
355        s
356    }
357
358    #[test]
359    fn short_history_is_passthrough() {
360        let _iso = crate::core::data_dir::isolated_data_dir();
361        crate::core::config::Config::update_global(|c| c.proxy.proxy_mode = Some("cache".into()))
362            .unwrap();
363        let body = serde_json::json!({
364            "contents": [
365                {"role": "user", "parts": [{"functionResponse": {
366                    "name": "read_file", "response": {"result": "ok"}
367                }}]}
368            ]
369        });
370        let bytes = serde_json::to_vec(&body).unwrap();
371        let (out, orig, comp) = compress_request_body(body.clone(), bytes.len(), None);
372        assert_eq!(comp, orig);
373        let reparsed: Value = serde_json::from_slice(&out).unwrap();
374        assert_eq!(reparsed, body);
375    }
376
377    #[test]
378    fn gemini_compression_is_deterministic() {
379        // #498: identical request → identical bytes.
380        let _iso = crate::core::data_dir::isolated_data_dir();
381        let mk = || serde_json::json!({ "contents": gemini_read_turns(13) });
382        let (a, b) = (mk(), mk());
383        let (la, lb) = (
384            serde_json::to_vec(&a).unwrap().len(),
385            serde_json::to_vec(&b).unwrap().len(),
386        );
387        let (out_a, _, _) = compress_request_body(a, la, None);
388        let (out_b, _, _) = compress_request_body(b, lb, None);
389        assert_eq!(out_a, out_b, "identical input must yield identical bytes");
390    }
391
392    fn big_prose() -> String {
393        let p = "You are a careful, senior software engineer. You always explain your \
394                 reasoning before making changes, you prefer small reviewable diffs, and \
395                 you never introduce mock data or placeholders into production code. ";
396        [p; 6].join("\n")
397    }
398
399    #[test]
400    fn system_instruction_compressed_and_model_untouched() {
401        let _iso = crate::core::data_dir::isolated_data_dir();
402        crate::core::config::Config::update_global(|c| {
403            c.proxy.role_aggressiveness.system = Some(0.6);
404        })
405        .unwrap();
406
407        let prose = big_prose();
408        let body = serde_json::json!({
409            "systemInstruction": {"parts": [{"text": prose}]},
410            "contents": [
411                {"role": "user", "parts": [{"text": "hi"}]},
412                {"role": "model", "parts": [{"text": prose}]},
413            ]
414        });
415        let bytes = serde_json::to_vec(&body).unwrap();
416        let (out, _o, _c) = compress_request_body(body, bytes.len(), None);
417        let parsed: Value = serde_json::from_slice(&out).unwrap();
418
419        assert!(
420            parsed["systemInstruction"]["parts"][0]["text"]
421                .as_str()
422                .unwrap()
423                .len()
424                < prose.len(),
425            "systemInstruction prose must be compressed when enabled"
426        );
427        assert_eq!(
428            parsed["contents"][1]["parts"][0]["text"].as_str().unwrap(),
429            prose,
430            "model (assistant) turns must pass through verbatim (#710)"
431        );
432    }
433
434    #[test]
435    fn gemini_prose_compression_is_deterministic() {
436        let _iso = crate::core::data_dir::isolated_data_dir();
437        crate::core::config::Config::update_global(|c| {
438            c.proxy.role_aggressiveness.system = Some(0.6);
439        })
440        .unwrap();
441        let prose = big_prose();
442        let mk = || {
443            serde_json::json!({
444                "systemInstruction": {"parts": [{"text": prose}]},
445                "contents": [{"role": "user", "parts": [{"text": "hi"}]}]
446            })
447        };
448        let (a, b) = (mk(), mk());
449        let la = serde_json::to_vec(&a).unwrap().len();
450        let lb = serde_json::to_vec(&b).unwrap().len();
451        assert_eq!(
452            compress_request_body(a, la, None).0,
453            compress_request_body(b, lb, None).0,
454            "identical input must yield byte-identical output (#498)"
455        );
456    }
457
458    #[test]
459    fn cache_aware_gemini_prefix_is_byte_stable_across_turns() {
460        // THE cache invariant for the Gemini rail: every `contents` entry before
461        // an already-passed boundary stays byte-identical as the chat grows.
462        let _iso = crate::core::data_dir::isolated_data_dir();
463        let mut prev: Vec<String> = Vec::new();
464        let mut prev_boundary = 0;
465        for pairs in 1..=20 {
466            let contents = gemini_read_turns(pairs);
467            let len = contents.len();
468            let body = serde_json::json!({ "contents": contents });
469            let bytes = serde_json::to_vec(&body).unwrap();
470            let (out, _, _) = compress_request_body(body, bytes.len(), None);
471            let parsed: Value = serde_json::from_slice(&out).unwrap();
472            let items: Vec<String> = parsed["contents"]
473                .as_array()
474                .unwrap()
475                .iter()
476                .map(Value::to_string)
477                .collect();
478            for i in 0..prev_boundary {
479                assert_eq!(
480                    prev[i], items[i],
481                    "Gemini content {i} changed at turn {pairs} — prompt cache prefix broken"
482                );
483            }
484            prev = items;
485            prev_boundary = crate::proxy::history_prune::prune_boundary(
486                crate::core::config::HistoryMode::CacheAware,
487                len,
488            );
489        }
490    }
491
492    #[test]
493    fn effort_control_sets_thinking_config_by_generation() {
494        // #840 end-to-end: the model is taken from the URL path, so the handler
495        // threads it in. 3.x → thinkingLevel; off / unknown model → byte no-op.
496        let _iso = crate::core::data_dir::isolated_data_dir();
497        crate::core::config::Config::update_global(|c| c.proxy.proxy_mode = Some("cache".into()))
498            .unwrap();
499        crate::test_env::remove_var("LEAN_CTX_PROXY_EFFORT");
500        crate::core::config::Config::update_global(|c| {
501            c.proxy.effort = Some("low".into());
502        })
503        .unwrap();
504
505        let body = serde_json::json!({
506            "contents": [{"role": "user", "parts": [{"text": "hi"}]}]
507        });
508        let bytes = serde_json::to_vec(&body).unwrap();
509        let (out, _o, _c) = compress_request_body(body.clone(), bytes.len(), Some("gemini-3-pro"));
510        assert_eq!(
511            serde_json::from_slice::<Value>(&out).unwrap()["generationConfig"]["thinkingConfig"]["thinkingLevel"],
512            "low"
513        );
514
515        // No model (path didn't resolve) → strict no-op, body byte-unchanged.
516        let bytes = serde_json::to_vec(&body).unwrap();
517        let (out, _o, _c) = compress_request_body(body.clone(), bytes.len(), None);
518        assert_eq!(serde_json::from_slice::<Value>(&out).unwrap(), body);
519    }
520}