cpa-server 0.4.2

Part of cliproxy: HTTP routes, client auth, credential selection and the management API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
//! Per-attempt model capability binding (Go sdk/cliproxy/auth/api_key_model_capabilities.go).
//!
//! Go's conductor binds the capabilities of the model an attempt actually sends upstream
//! to the execution request (`attachResolvedExecutionModelInfo`), so executors read
//! `ResolvedModelInfo(req)` instead of re-resolving config entries. In order:
//! - a configured API-key model (`lookupAPIKeyModelCapability`), routed by the client's
//!   model (alias or name, with and without a thinking suffix) and the selected upstream;
//! - any Codex API-key model the credential's entry does not list
//!   (`lookupUnlistedCodexAPIKeyModelCapability`);
//! - a Codex OAuth credential's plan catalog model (`lookupCodexOAuthModelCapability`).
//!
//! The result is [`cpa_core::exec::ExecRequest::resolved_model`].

use cpa_common::gostr::GoStr;
use cpa_common::thinking::parse_suffix;
use cpa_core::config::Config;
use cpa_core::config::credentials;
use cpa_core::credential::{Credential, Source};
use cpa_core::exec::{ResolvedModel, ResolvedSource};
use cpa_core::registry::dynamic::{self, ConfigModel};
use cpa_core::registry::{ModelInfo, ThinkingSupport, pinned};
use serde_json::{Map, Value};

/// Go `attachResolvedExecutionModelInfo`. `execution_model` is set when the attempt
/// executes the request's own model rather than the selection's (Go
/// `restoreExecutionModel`): then only Codex binds, routing by that model.
// ponytail: compiled per attempt from the config snapshot (Go precompiles a routing
// table on every config or credential change); entries are small. Cache it on
// `Config::derived` if profiling ever shows it.
pub fn resolve_attempt(
    cfg: &Config,
    c: &Credential,
    route_model: &str,
    upstream_model: &str,
    execution_model: Option<&str>,
) -> Option<ResolvedModel> {
    match execution_model {
        Some(_) if !c.provider.trim().go_eq_fold("codex") => None,
        Some(model) => resolve(cfg, c, model, model),
        None => resolve(cfg, c, route_model, upstream_model),
    }
}

/// Go `attachResolvedAPIKeyModelInfo` for a fresh attempt.
pub fn resolve(cfg: &Config, c: &Credential, route_model: &str, upstream_model: &str) -> Option<ResolvedModel> {
    let api_key = configured(cfg, c, route_model, upstream_model).or_else(|| unlisted_codex(cfg, c, upstream_model));
    if let Some(info) = api_key {
        return Some(ResolvedModel {
            info,
            source: ResolvedSource::ApiKey,
        });
    }
    codex_oauth(c, upstream_model).map(|info| ResolvedModel {
        info,
        source: ResolvedSource::CodexOAuth,
    })
}

/// Go `attachResolvedHomeModelInfo`, after the local binding of an attempt on a Home
/// credential that executes the selection's model: Home's definition of the dispatched
/// model ([`crate::remote::MODEL_INFO`]) is the attempt's model (Go's
/// `ResolvedModelInfo` reads it first). Its configuration-update support is Home's
/// explicit flag, else `local`'s when that names the same model; Go's stream path binds
/// Home's model before the local one, so callers pass no `local` there. Its is-compat
/// is the selected `credential_options` entry's, never `local`'s. `None` without a Home
/// definition: the local binding stays.
pub fn bind_home(c: &Credential, local: Option<&ResolvedModel>) -> Option<ResolvedModel> {
    let wire: Map<String, Value> = serde_json::from_str(c.attributes.get(crate::remote::MODEL_INFO)?).ok()?;
    let mut info = home_model_info(&wire)?;
    let base = |id: &str| parse_suffix(id.trim()).model_name.trim().to_owned();
    let flag = |info: &ModelInfo, key: &str| info.raw.get(key) == Some(&Value::Bool(true));
    let support = match wire.get("support_configuration_update").and_then(Value::as_bool) {
        Some(explicit) => explicit,
        None => local.is_some_and(|local| {
            base(&local.info.id).go_eq_fold(&base(&info.id)) && flag(&local.info, "support_configuration_update")
        }),
    };
    set_flag(&mut info, "support_configuration_update", support);
    if let Some(options) = c.attributes.get(credentials::HOME_MODEL_OPTIONS) {
        let options: Map<String, Value> = serde_json::from_str(options).unwrap_or_default();
        set_flag(
            &mut info,
            "is_compat",
            options.get("is-compat") == Some(&Value::Bool(true)),
        );
    }
    Some(ResolvedModel {
        info,
        source: ResolvedSource::Home,
    })
}

/// Go `homeDispatchModelInfo.registryModelInfo`: nil without an ID; the context limits,
/// thinking, native capabilities and user-defined flag carried over; never is-compat.
fn home_model_info(wire: &Map<String, Value>) -> Option<ModelInfo> {
    let text = |key: &str| {
        wire.get(key)
            .and_then(Value::as_str)
            .unwrap_or_default()
            .trim()
            .to_owned()
    };
    let id = text("id");
    if id.is_empty() {
        return None;
    }
    let mut raw = Map::new();
    raw.insert("id".into(), id.into());
    let kind = text("type");
    if !kind.is_empty() {
        raw.insert("type".into(), kind.into());
    }
    for key in [
        "inputTokenLimit",
        "outputTokenLimit",
        "context_length",
        "max_completion_tokens",
    ] {
        if let Some(n) = wire.get(key).and_then(Value::as_i64).filter(|n| *n != 0) {
            raw.insert(key.into(), n.into());
        }
    }
    for key in ["thinking", "native_capabilities"] {
        if let Some(value) = wire.get(key).filter(|v| !v.is_null()) {
            raw.insert(key.into(), go_zero_nulls(value));
        }
    }
    if wire.get("user_defined") == Some(&Value::Bool(true)) {
        raw.insert("user_defined".into(), true.into());
    }
    ModelInfo::from_raw(raw).ok()
}

/// Go decodes a JSON null as the zero value: a null field of `ThinkingSupport` or
/// `NativeCapabilities` stays unset, and a null `levels` element (their only list, of
/// strings) is "". Rust's decoder rejects nulls, so drop or zero them first.
fn go_zero_nulls(value: &Value) -> Value {
    match value {
        Value::Object(map) => Value::Object(
            map.iter()
                .filter(|(_, v)| !v.is_null())
                .map(|(k, v)| (k.clone(), go_zero_nulls(v)))
                .collect(),
        ),
        Value::Array(items) => Value::Array(
            items
                .iter()
                .map(|v| match v {
                    Value::Null => Value::String(String::new()),
                    v => go_zero_nulls(v),
                })
                .collect(),
        ),
        other => other.clone(),
    }
}

fn attr<'a>(c: &'a Credential, key: &str) -> &'a str {
    c.attributes.get(key).map_or("", |v| v.trim())
}

/// `isConfiguredModelRoutingAuth`: API keys, or config-sourced OpenAI-compatible entries.
fn configured_routing(c: &Credential) -> bool {
    dynamic::is_api_key(c) || (matches!(c.source, Source::Config { .. }) && !attr(c, "compat_name").is_empty())
}

/// One configured model as Go compiles it (`addConfiguredModelCapability` inputs).
struct Configured {
    name: String,
    alias: String,
    thinking: Option<ThinkingSupport>,
    is_compat: bool,
    /// Codex only: `SupportConfigurationUpdate` overrides the static definition.
    support_configuration_update: Option<bool>,
}

/// `compileAPIKeyModelCapabilitiesForAuth`: the credential's config entry models, with
/// Go's model type per provider.
fn entry_models(cfg: &Config, c: &Credential) -> Option<(Vec<Configured>, &'static str)> {
    let provider = c.provider.trim().go_lower();
    let (family, kind) = match provider.as_str() {
        "gemini" => ("gemini", "gemini"),
        "gemini-interactions" => ("interactions", "interactions"),
        "claude" => ("claude", "claude"),
        "codex" => ("codex", "codex"),
        "xai" => ("xai", "xai"),
        "vertex" => ("vertex", "gemini"),
        "meta" => ("meta", "meta"),
        _ => return Some((compat_models(cfg, c)?, "openai-compatibility")),
    };
    let entry = credentials::resolve_api_key_entry(cfg, family, c)?;
    let models = entry
        .models
        .iter()
        .filter_map(|m| serde_json::from_value::<ConfigModel>(m.clone()).ok())
        .map(|m| Configured {
            name: m.name,
            alias: m.alias,
            thinking: m.thinking,
            // VertexCompatModel has no is-compat (Go's GetIsCompat is absent).
            is_compat: m.is_compat && family != "vertex",
            support_configuration_update: (family == "codex").then_some(m.support_configuration_update),
        })
        .collect();
    Some((models, kind))
}

/// `resolveOpenAICompatConfigForAuth` then `compileOpenAICompatibleModelCapabilities`:
/// the entry at `config_index` among entries with a base URL when enabled, else the
/// first enabled entry named by `compat_name`, `provider_key` or the provider. Models
/// without configured thinking get low/medium/high unless they are image models.
fn compat_models(cfg: &Config, c: &Credential) -> Option<Vec<Configured>> {
    let text = |v: Option<&serde_yaml_ng::Value>| match v {
        Some(serde_yaml_ng::Value::String(s)) => s.trim().to_owned(),
        Some(serde_yaml_ng::Value::Number(n)) => n.to_string(),
        Some(serde_yaml_ng::Value::Bool(b)) => b.to_string(),
        _ => String::new(),
    };
    let entries: Vec<&serde_yaml_ng::Value> = cfg
        .document
        .get("api-keys")
        .and_then(|k| k.get("openai-compatibility"))
        .and_then(serde_yaml_ng::Value::as_sequence)
        .into_iter()
        .flatten()
        .filter(|g| !text(g.get("base-url")).is_empty())
        .collect();
    let disabled = |g: &serde_yaml_ng::Value| g.get("disabled").and_then(serde_yaml_ng::Value::as_bool) == Some(true);
    let by_index = matches!(c.source, Source::Config { .. })
        .then(|| attr(c, "config_index").parse::<usize>().ok())
        .flatten()
        .and_then(|i| entries.get(i).copied())
        .filter(|g| !disabled(g));
    let entry = by_index.or_else(|| {
        let candidates = [attr(c, "compat_name"), attr(c, "provider_key"), c.provider.trim()];
        entries.iter().copied().find(|g| {
            let name = text(g.get("name"));
            !disabled(g) && candidates.iter().any(|cand| !cand.is_empty() && cand.go_eq_fold(&name))
        })
    })?;
    let models = entry
        .get("models")
        .and_then(serde_yaml_ng::Value::as_sequence)
        .into_iter()
        .flatten()
        .filter_map(|m| serde_yaml_ng::from_value::<ConfigModel>(m.clone()).ok())
        .map(|m| Configured {
            thinking: m.thinking.or_else(|| {
                (!m.image).then(|| ThinkingSupport {
                    levels: vec!["low".into(), "medium".into(), "high".into()],
                    ..ThinkingSupport::default()
                })
            }),
            name: m.name,
            alias: m.alias,
            is_compat: m.is_compat,
            support_configuration_update: None,
        })
        .collect();
    Some(models)
}

/// `modelAliasLookupCandidates`: the model, then its suffix-free base when different.
fn candidates(model: &str) -> Vec<String> {
    let model = model.trim();
    if model.is_empty() {
        return Vec::new();
    }
    let base = parse_suffix(model).model_name;
    let base = if base.is_empty() { model.to_owned() } else { base };
    if base == model {
        vec![model.to_owned()]
    } else {
        vec![model.to_owned(), base]
    }
}

/// `configuredUpstreamFallbackMatches`: a suffix-free configured name matches the
/// selection's base.
fn fallback_matches(configured: &str, selected: &str) -> bool {
    let configured = parse_suffix(configured.trim());
    !configured.has_suffix
        && configured
            .model_name
            .trim()
            .go_eq_fold(parse_suffix(selected.trim()).model_name.trim())
}

/// `lookupAPIKeyModelCapability` over the routes `addConfiguredModelCapability` builds:
/// each model is keyed by its alias and name candidates (lowercased); a key keeps one
/// route per upstream name, first model first.
fn configured(cfg: &Config, c: &Credential, route_model: &str, upstream_model: &str) -> Option<ModelInfo> {
    if !configured_routing(c) {
        return None;
    }
    let (models, kind) = entry_models(cfg, c)?;
    // (key, upstream name, model index) in insertion order.
    let mut routes: Vec<(String, String, usize)> = Vec::new();
    for (index, m) in models.iter().enumerate() {
        let (mut name, mut alias) = (m.name.trim(), m.alias.trim());
        if name.is_empty() {
            name = alias;
        }
        if alias.is_empty() {
            alias = name;
        }
        if name.is_empty() {
            continue;
        }
        let mut seen: Vec<String> = Vec::new();
        for candidate in [alias, name].into_iter().flat_map(candidates) {
            let key = candidate.trim().go_lower();
            if key.is_empty() || seen.contains(&key) {
                continue;
            }
            seen.push(key.clone());
            if !routes.iter().any(|(k, up, _)| *k == key && up.go_eq_fold(name)) {
                routes.push((key, name.to_owned(), index));
            }
        }
    }
    let requested = dynamic::strip_prefix(route_model.trim(), c);
    let matched: Vec<(&str, usize)> = candidates(requested)
        .iter()
        .flat_map(|candidate| {
            let key = candidate.trim().go_lower();
            routes
                .iter()
                .filter(move |(k, ..)| *k == key)
                .map(|(_, up, i)| (up.as_str(), *i))
        })
        .collect();
    let selected = upstream_model.trim();
    let (name, index) = matched
        .iter()
        .find(|(up, _)| up.trim().go_eq_fold(selected))
        .or_else(|| matched.iter().find(|(up, _)| fallback_matches(up, selected)))?;
    let m = &models[*index];
    let mut info = resolve_model_info(name, kind, m.thinking.clone());
    set_flag(&mut info, "is_compat", m.is_compat);
    if let Some(support) = m.support_configuration_update {
        set_flag(&mut info, "support_configuration_update", support);
    }
    Some(info)
}

/// `lookupUnlistedCodexAPIKeyModelCapability`: every upstream model of a Codex API key
/// whose entry still matches its key and base URL binds; a configured name (exact or
/// suffix-free base) lends its thinking, configuration-update support and is-compat.
fn unlisted_codex(cfg: &Config, c: &Credential, upstream_model: &str) -> Option<ModelInfo> {
    let upstream = upstream_model.trim();
    if !dynamic::is_api_key(c) || !c.provider.trim().go_eq_fold("codex") || upstream.is_empty() {
        return None;
    }
    let entry = credentials::resolve_api_key_entry(cfg, "codex", c)?;
    let (key, base) = (attr(c, "api_key"), attr(c, "base_url"));
    let entry_base = entry.base_url.trim();
    if (key.is_empty() && base.is_empty())
        || !key.go_eq_fold(entry.api_key.trim())
        || (!entry_base.is_empty() && !base.go_eq_fold(entry_base))
    {
        return None;
    }
    let configured = entry
        .models
        .iter()
        .filter_map(|m| serde_json::from_value::<ConfigModel>(m.clone()).ok())
        .find(|m| m.name.trim().go_eq_fold(upstream) || fallback_matches(&m.name, upstream));
    let mut info = resolve_model_info(
        upstream_model,
        "codex",
        configured.as_ref().and_then(|m| m.thinking.clone()),
    );
    let (support, compat) = configured.map_or((false, false), |m| (m.support_configuration_update, m.is_compat));
    set_flag(&mut info, "support_configuration_update", support);
    set_flag(&mut info, "is_compat", compat);
    Some(info)
}

/// `lookupCodexOAuthModelCapability`: the plan catalog entry for the selection's
/// suffix-free model (`plan_type` plus, team/business/go, free; otherwise pro).
fn codex_oauth(c: &Credential, upstream_model: &str) -> Option<ModelInfo> {
    if dynamic::auth_kind(c) != Some("oauth") || !c.provider.trim().go_eq_fold("codex") {
        return None;
    }
    let channel = match attr(c, "plan_type").go_lower().as_str() {
        "plus" => "codex-plus",
        "team" | "business" | "go" => "codex-team",
        "free" => "codex-free",
        _ => "codex-pro",
    };
    let selected = parse_suffix(upstream_model.trim()).model_name;
    let selected = selected.trim();
    pinned()
        .channel(channel)
        .iter()
        .find(|m| m.id.go_eq_fold(selected))
        .cloned()
}

/// `modelconfig.ResolveModelInfo`: the static definition of the suffix-free name (Go
/// `LookupStaticModelInfo`), renamed to the configured name, typed for the provider,
/// configured thinking normalized, never user-defined.
fn resolve_model_info(name: &str, kind: &str, thinking: Option<ThinkingSupport>) -> ModelInfo {
    let name = name.trim();
    let base = parse_suffix(name).model_name;
    let mut info = pinned().lookup(base.trim()).cloned().unwrap_or_else(|| ModelInfo {
        id: String::new(),
        kind: String::new(),
        thinking: None,
        raw: Map::new(),
    });
    info.id = name.to_owned();
    info.raw.insert("id".into(), name.into());
    info.kind = kind.trim().to_owned();
    info.raw.insert("type".into(), info.kind.clone().into());
    if let Some(support) = thinking {
        let mut support = dynamic::normalize_thinking(support);
        // Configured levels replace the catalog's, but how a known model turns thinking
        // off is an upstream fact (Go ad9e456d).
        if support.disable_type.is_empty()
            && let Some(catalog) = &info.thinking
        {
            support.disable_type.clone_from(&catalog.disable_type);
        }
        info.raw.insert("thinking".into(), dynamic::thinking_value(&support));
        info.thinking = Some(support);
    }
    info.raw.remove("user_defined");
    info
}

/// Internal boolean metadata (`json:"-"` in Go) kept in `raw` only when true.
fn set_flag(info: &mut ModelInfo, key: &str, value: bool) {
    if value {
        info.raw.insert(key.into(), Value::Bool(true));
    } else {
        info.raw.remove(key);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    /// Go goldens (tests/reference/server/main.go `resolvedModels`, run through
    /// `GoldenResolvedModelInfo` in auth_export.go.overlay): Go's real
    /// `attachResolvedExecutionModelInfo` on config-synthesized auths, mutated
    /// `config_index` values and file-style auths.
    #[test]
    fn resolved_models_match_go() {
        let fixture: Value = serde_json::from_str(include_str!("../tests/fixtures/server_go.json")).unwrap();
        let cfg = Config::parse(fixture["resolved_config"].as_str().unwrap()).unwrap();
        let synthesized = credentials::from_config(&cfg);
        let cases = fixture["resolved"].as_array().unwrap();
        assert_eq!(cases.len(), 32);
        let (mut bound, mut codex_oauth) = (0, 0);
        for case in cases {
            let name = case["name"].as_str().unwrap();
            let id = case["auth_id"].as_str().unwrap();
            let attributes: std::collections::BTreeMap<String, String> = case["attributes"]
                .as_object()
                .map(|a| {
                    a.iter()
                        .map(|(k, v)| (k.clone(), v.as_str().unwrap().to_owned()))
                        .collect()
                })
                .unwrap_or_default();
            let c = if case["synthetic"].as_bool().unwrap() {
                // Go and Rust synthesize the same IDs; the case's config_index mutation
                // is carried over from Go's attributes.
                let mut c = synthesized
                    .iter()
                    .find(|c| c.id == id)
                    .unwrap_or_else(|| panic!("{name}: no synthesized credential {id}"))
                    .clone();
                c.attributes
                    .insert("config_index".into(), attributes["config_index"].clone());
                c
            } else {
                let meta = case["metadata"].as_object().cloned().unwrap_or_default();
                let path = format!("/auth/{id}");
                let mut c = Credential::from_file(Path::new("/auth"), Path::new(&path), meta).unwrap();
                c.attributes = attributes;
                c
            };
            let restore = case["restore"].as_bool().unwrap();
            let got = resolve_attempt(
                &cfg,
                &c,
                case["route"].as_str().unwrap(),
                case["upstream"].as_str().unwrap(),
                restore.then(|| case["req_model"].as_str().unwrap()),
            );
            let source = match got.as_ref().map(|r| r.source) {
                None => "",
                Some(ResolvedSource::ApiKey) => "api_key",
                Some(ResolvedSource::CodexOAuth) => "codex_oauth",
                Some(ResolvedSource::Home) => "home",
            };
            assert_eq!(source, case["source"].as_str().unwrap(), "{name}: source");
            let Some(got) = got else { continue };
            let mut want = case["info"].clone();
            if want["id"] == "claude-opus-4-7" {
                // Recorded before Go ad9e456d made Opus 4.7 adaptive-only in the v8.0.35
                // catalog (internal/registry/models/models.json).
                want["thinking"] = serde_json::json!({
                    "zero_allowed": true,
                    "dynamic_allowed": true,
                    "levels": ["low", "medium", "high", "xhigh", "max"]
                });
            }
            bound += 1;
            codex_oauth += usize::from(got.source == ResolvedSource::CodexOAuth);
            // Go's JSON drops internal fields; compare everything else, ignoring zero
            // values Go emits for an unknown static model.
            let visible = |m: &Map<String, Value>| -> Map<String, Value> {
                m.iter()
                    .filter(|(k, v)| {
                        !matches!(
                            k.as_str(),
                            "support_configuration_update" | "is_compat" | "user_defined" | "native_capabilities"
                        ) && !matches!(v, Value::Null)
                            && *v != &Value::from("")
                            && *v != &Value::from(0)
                    })
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect()
            };
            assert_eq!(
                Value::Object(visible(&got.info.raw)),
                Value::Object(visible(want.as_object().unwrap())),
                "{name}: info"
            );
            assert_eq!(got.info.id, want["id"].as_str().unwrap(), "{name}: id");
            assert_eq!(got.info.kind, want["type"].as_str().unwrap(), "{name}: type");
            assert_eq!(
                got.is_compat(),
                case["is_compat"].as_bool().unwrap(),
                "{name}: is_compat"
            );
            let caps = cpa_common::thinking::ModelCaps::from(&got.info);
            assert_eq!(
                caps.support_configuration_update,
                case["support_configuration_update"].as_bool().unwrap(),
                "{name}: support_configuration_update"
            );
            assert_eq!(
                caps.user_defined,
                case["user_defined"].as_bool().unwrap(),
                "{name}: user_defined"
            );
            assert_eq!(
                got.info.thinking.as_ref().map(dynamic::thinking_value),
                want.get("thinking").cloned(),
                "{name}: typed thinking"
            );
        }
        assert_eq!((bound, codex_oauth), (25, 4));
    }

    /// Go `TestResolveModelInfoConfiguredThinkingKeepsCatalogDisableType`: configured levels
    /// replace the catalog's, but an empty `disable-type` keeps the catalog's way of turning
    /// thinking off; an explicit one wins.
    #[test]
    fn configured_thinking_keeps_the_catalog_disable_type() {
        let support = |yaml: &str| serde_yaml_ng::from_str::<ThinkingSupport>(yaml).unwrap();
        let configured = support("levels: [low, medium, high, none]");
        let info = resolve_model_info("claude-sonnet-5-5", "claude", Some(configured.clone()));
        let thinking = info.thinking.as_ref().unwrap();
        assert_eq!(thinking.disable_type, "between_tools");
        assert_eq!(thinking.levels, ["low", "medium", "high", "none"]);
        let body = br#"{"model":"claude-sonnet-5-5","messages":[{"role":"user","content":"hi"}],"thinking":{"type":"disabled"}}"#;
        let caps = cpa_common::thinking::ModelCaps::from(&info);
        let out = cpa_common::thinking::apply_thinking_with_model_info(
            body,
            &[],
            "claude-sonnet-5-5",
            "claude",
            "claude",
            "claude",
            Some(&caps),
        )
        .unwrap();
        let out: Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(out["thinking"], serde_json::json!({"type": "between_tools"}), "{out}");

        // Configured capabilities are authoritative: a model the proxy knows refuses
        // `disabled` still gets it once its configuration allows turning thinking off.
        let opus = resolve_model_info("claude-opus-5-5", "claude", Some(support("levels: [low, none]")));
        let body = br#"{"model":"claude-opus-5-5","messages":[{"role":"user","content":"hi"}],"thinking":{"type":"disabled"}}"#;
        let caps = cpa_common::thinking::ModelCaps::from(&opus);
        let out = cpa_common::thinking::apply_thinking_with_model_info(
            body,
            &[],
            "claude-opus-5-5",
            "claude",
            "claude",
            "claude",
            Some(&caps),
        )
        .unwrap();
        let out: Value = serde_json::from_slice(&out).unwrap();
        assert_eq!(out["thinking"], serde_json::json!({"type": "disabled"}), "{out}");

        let explicit = support("levels: [low]\ndisable-type: disabled");
        let explicit = resolve_model_info("claude-sonnet-5-5", "claude", Some(explicit));
        assert_eq!(explicit.thinking.unwrap().disable_type, "disabled");
        let other = resolve_model_info("claude-opus-5", "claude", Some(configured.clone()));
        assert_eq!(other.thinking.unwrap().disable_type, "");
        let variant = resolve_model_info("claude-sonnet-5-5[1m]", "claude", Some(configured));
        assert_eq!(variant.thinking.unwrap().disable_type, "between_tools");
    }
}