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
//! Codex Alpha Search (`Server.codexAlphaSearch`, internal/api/server_routes.go):
//! `POST /v1/alpha/search` and `POST /backend-api/codex/alpha/search`.
//!
//! The body is already in Codex search format and is never translated. Only OAuth Codex
//! credentials and API keys with `alpha-search: true` may serve it (credential policy
//! `codex_alpha_search_v1`). The upstream status and body reach the client unchanged.

use std::sync::Arc;

use axum::Extension;
use axum::Router;
use axum::body::Bytes;
use axum::extract::State;
use axum::extract::rejection::BytesRejection;
use axum::http::{HeaderMap, HeaderValue, StatusCode, header};
use axum::response::{IntoResponse, Response};
use axum::routing::post;
use cpa_core::credential::Credential;

use crate::runtime::{AcquireError, Runtime, Selection};

pub fn routes() -> Router<Arc<Runtime>> {
    Router::new()
        .route("/v1/alpha/search", post(alpha_search))
        .route("/backend-api/codex/alpha/search", post(alpha_search))
}

/// Go reads at most 16 MiB of the request body (`io.LimitReader`).
const MAX_BODY: usize = 16 << 20;

fn error(status: u16, message: &str) -> Response {
    let status = StatusCode::from_u16(status).unwrap_or(StatusCode::SERVICE_UNAVAILABLE);
    (status, axum::Json(serde_json::json!({ "error": message }))).into_response()
}

/// `json.Unmarshal(body, &struct{ ID, Model string })`, trimmed: keys match
/// case-insensitively and the last duplicate wins; a null or non-string value leaves the
/// field as it was; invalid JSON or a non-object sets nothing.
fn routing(body: &[u8]) -> (String, String) {
    let (mut id, mut model) = (String::new(), String::new());
    let text = String::from_utf8_lossy(body);
    if !gjson::valid(&text) {
        return (id, model);
    }
    let root = gjson::parse(&text);
    if root.kind() == gjson::Kind::Object {
        root.each(|key, value| {
            let slot = match key.str() {
                k if k.eq_ignore_ascii_case("id") => &mut id,
                k if k.eq_ignore_ascii_case("model") => &mut model,
                _ => return true,
            };
            if value.kind() == gjson::Kind::String {
                *slot = value.str().to_owned();
            }
            true
        });
    }
    (id.trim().to_owned(), model.trim().to_owned())
}

fn with_retry_after(mut response: Response, seconds: Option<u64>) -> Response {
    if let Some(seconds) = seconds {
        response
            .headers_mut()
            .insert(header::RETRY_AFTER, HeaderValue::from(seconds));
    }
    response
}

/// `credentialPolicyAllows(codex_alpha_search_v1, auth)`.
fn allowed(c: &Credential) -> bool {
    c.provider.eq_ignore_ascii_case("codex")
        && match cpa_core::registry::dynamic::auth_kind(c) {
            Some("oauth") => true,
            Some("apikey") => c
                .attributes
                .get("codex_alpha_search")
                .is_some_and(|v| v.trim().eq_ignore_ascii_case("true")),
            _ => false,
        }
}

async fn alpha_search(
    State(rt): State<Arc<Runtime>>,
    Extension(caller): Extension<cpa_core::exec::Caller>,
    headers: HeaderMap,
    body: Result<Bytes, BytesRejection>,
) -> Response {
    let body = match body {
        Ok(body) => body.slice(..body.len().min(MAX_BODY)),
        Err(_) => return error(400, "Failed to read search request"),
    };
    let (id, model) = routing(&body);
    let capture = crate::request_logging::current()
        .map(|log| log.capture_sink())
        .unwrap_or_default();
    let (cfg, policy) = rt.request_snapshot();
    // The credential policy narrows selection; disallowed Codex credentials are excluded.
    let exclude = rt
        .store()
        .snapshot()
        .iter()
        .filter(|c| c.provider == "codex" && !allowed(c))
        .map(|c| c.id.clone())
        .collect();
    // Go selects with the route model (`pickNextLegacy`: registry admission and per-model
    // cooldown) and keys session affinity on the explicit session, with `X-Session-ID`
    // set from the body's `id` and no derived identity.
    let mut selection_headers = headers.clone();
    if let Ok(value) = HeaderValue::from_str(&id)
        && !id.is_empty()
    {
        selection_headers.insert("x-session-id", value);
    }
    let meta = cpa_common::session::Meta {
        execution_session: None,
        derived: None,
    };
    let (mut primary, mut parent, fork) = cpa_common::session::explicit_session_ids(&selection_headers, &body, &meta);
    if primary.is_empty() {
        (primary, parent) = cpa_common::session::session_ids(&selection_headers, &body, &meta);
    }
    // Go's Alpha Search context holds the request ID, this session hierarchy (it never
    // passes `GetContextWithCancel`; Home's canonical session replaces it) and the
    // authenticated caller.
    let client = crate::usage_record::Client {
        session_id: primary.clone(),
        parent_session_id: if parent == primary {
            String::new()
        } else {
            parent.clone()
        },
        request_id: crate::observability::current_request_id().unwrap_or_default(),
        api_key: caller.principal,
        access_provider: caller.provider,
        ..Default::default()
    };
    let bound = |s: String| (!s.is_empty()).then(|| cpa_common::session::bound_session_identity(&s));
    let selection = Selection {
        provider: "codex".into(),
        model: model.clone(),
        session: bound(primary),
        session_parent: bound(parent),
        session_fork: fork,
        exclude,
        scheduler: crate::plugins::provider_scheduler_context(
            &rt,
            "codex",
            &selection_headers,
            if policy.session_affinity { &model } else { "" },
            &[],
        ),
        ..Selection::default()
    };
    // ponytail: one selection, no failover or outcome recording, as in Go; plugin model
    // routing is not ported.
    if let Some(remote) = rt.remote_dispatch() {
        let (lease, home_key) = match home_lease(&rt, remote.as_ref(), selection, &headers).await {
            Ok(picked) => picked,
            Err(response) => return *response,
        };
        let facts = crate::usage_record::Facts {
            client: crate::usage_record::Client {
                api_key: crate::usage_record::home_unauthorized_key(&client.api_key, &home_key),
                ..client
            },
            format: cpa_core::format::Format::OpenAIResponse,
            alias: String::new(),
            reasoning_effort: String::new(),
            service_tier: "default".into(),
            generate: false,
            stream: false,
            clock: Default::default(),
            prompt_prefix_hash: String::new(),
            attempts: Default::default(),
        };
        return search(&rt, lease, &body, &headers, &model, &cfg, &capture, Some(&facts)).await;
    }
    let lease = match rt.acquire(selection, &cfg, policy, &rt.registry()).await {
        Ok(lease) => lease,
        // Go answers `gin.H{"error": err.Error()}` with the selector's error text.
        Err(AcquireError::Unavailable { retry_after: None, .. }) => {
            return error(503, "auth_not_found: no auth available");
        }
        Err(AcquireError::Unavailable {
            retry_after: Some(wait),
            cause,
        }) => {
            // `authUnavailableError`: the earliest retry and the last upstream error.
            let mut text = "auth_unavailable: no auth available".to_owned();
            if let Some(summary) = cause
                .as_deref()
                .map(crate::dispatch::upstream_summary)
                .filter(|s| !s.is_empty() && !text.contains(s.as_str()))
            {
                text.push_str(&format!(" (last upstream error: {summary})"));
            }
            let failure = crate::dispatch::Failure::Unavailable {
                code: "auth_unavailable",
                providers: vec!["codex".into()],
                model: model.clone(),
                cause: None,
                retry_after: Some(wait),
            };
            return with_retry_after(error(503, &text), failure.retry_after());
        }
        Err(AcquireError::Cooldown { wait, cause }) => {
            // `modelCooldownError`: the same JSON the inference routes return, as a string.
            let failure = crate::dispatch::Failure::Cooldown {
                model: model.clone(),
                provider: "codex".into(),
                wait,
                cause,
            };
            return with_retry_after(error(429, &failure.text()), failure.retry_after());
        }
        Err(AcquireError::Prepare { error: e, .. }) => {
            return error(e.status, &String::from_utf8_lossy(&e.body));
        }
        // A plugin scheduler's answer: Go returns the error as is, and a statusless one
        // answers 503 (`HTTPStatusFromErrorOr`).
        Err(AcquireError::Rejected { code, message }) => {
            return error(503, &format!("{code}: {message}"));
        }
        Err(AcquireError::Scheduler(e)) => {
            let status = if e.status > 0 { e.status } else { 503 };
            return error(status, &crate::classify::error_text(&e));
        }
    };
    search(&rt, lease, &body, &headers, &model, &cfg, &capture, None).await
}

/// Go `SelectHomeAuthWithCredentialPolicy`: Home picks under `codex_alpha_search_v1`;
/// a pick the policy does not allow ends and is excluded from the next one.
async fn home_lease(
    rt: &Arc<Runtime>,
    remote: &dyn crate::remote::RemoteDispatch,
    selection: Selection,
    headers: &HeaderMap,
) -> Result<(crate::runtime::Lease, String), Box<Response>> {
    let request = crate::remote::RemoteRequest {
        model: selection.model.clone(),
        session_id: selection.session.clone().unwrap_or_default(),
        parent_session_id: selection.session_parent.clone().unwrap_or_default(),
        headers: crate::remote::home_headers(headers, None),
        count: 1,
        retry_round: 0,
        excluded: Vec::new(),
        pinned: String::new(),
        request_id: crate::observability::current_request_id().unwrap_or_default(),
        kind: "http",
        credential_policy: "codex_alpha_search_v1".into(),
    };
    let accept = |c: &Credential| crate::remote::selection_provider(c) == "codex" && allowed(c);
    crate::remote::select(rt, remote, selection, request, accept)
        .await
        .map_err(|refused| {
            Box::new(with_retry_after(
                error(refused.status, &refused.text),
                refused.retry_after,
            ))
        })
}

/// One Alpha Search on `lease`'s credential. A Home pick reports an upstream 401 as a
/// Home result with `unauthorized` (Go `ReportHomeUnauthorized`).
#[allow(clippy::too_many_arguments)]
async fn search(
    rt: &Arc<Runtime>,
    lease: crate::runtime::Lease,
    body: &Bytes,
    headers: &HeaderMap,
    model: &str,
    cfg: &cpa_core::config::Config,
    capture: &cpa_core::exec::CaptureSink,
    unauthorized: Option<&crate::usage_record::Facts>,
) -> Response {
    let model = model.to_owned();
    // `ResolveExecutionModel`: Home's upstream model for a dispatched credential, else the
    // first credential-resolved candidate, else the route model.
    let aliases = cpa_core::registry::dynamic::global_aliases(cfg);
    let execution_model = lease
        .credential
        .attributes
        .get(crate::remote::UPSTREAM_MODEL)
        .map(|m| m.trim().to_owned())
        .filter(|m| !m.is_empty())
        .unwrap_or_else(|| {
            cpa_core::registry::dynamic::execution_models(&aliases, &lease.credential, &model)
                .0
                .into_iter()
                .next()
                .unwrap_or_else(|| model.clone())
        });
    // Go runs a Home pick on the selection's attempt context: a draining dispatcher
    // cancels it before or during the upstream call (`context.Canceled`, status 499).
    let search = rt
        .executors
        .codex
        .alpha_search(&lease.credential, body, headers, &execution_model, cfg, capture);
    let result = match lease.remote_cancelled() {
        None => search.await,
        Some(mut cancelled) => {
            if futures_util::FutureExt::now_or_never(&mut cancelled).is_some() {
                return error(crate::remote::CLIENT_CLOSED, "context canceled");
            }
            tokio::select! {
                biased;
                _ = cancelled => {
                    let text = match rt.executors.codex.alpha_search_url(&lease.credential, cfg) {
                        Some(url) => crate::remote::cancelled_request("POST", &url),
                        None => "context canceled".to_owned(),
                    };
                    return error(crate::remote::CLIENT_CLOSED, &text);
                }
                result = search => result,
            }
        }
    };
    if let (Ok(response), Some(facts)) = (&result, unauthorized)
        && response.status == 401
    {
        let body = match &response.body {
            cpa_core::exec::ResponseBody::Buffered(bytes) => String::from_utf8_lossy(bytes).into_owned(),
            _ => String::new(),
        };
        crate::usage_record::publish_home_unauthorized(rt, facts, &lease.credential, "codex", &model, &body);
    }
    // Dropping the lease reports `Cancelled`: Go's Alpha Search never marks a result.
    drop(lease);
    match result {
        Ok(response) => {
            let status = StatusCode::from_u16(response.status).unwrap_or(StatusCode::BAD_GATEWAY);
            let cpa_core::exec::ResponseBody::Buffered(bytes) = response.body else {
                return error(502, "Failed to read Codex search response");
            };
            let mut out = (status, bytes).into_response();
            if let Some(content_type) = response.headers.get(header::CONTENT_TYPE) {
                out.headers_mut().insert(header::CONTENT_TYPE, content_type.clone());
            }
            out
        }
        Err(e) => error(e.status, &String::from_utf8_lossy(&e.body)),
    }
}

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

    fn cred(meta: serde_json::Value, attrs: &[(&str, &str)]) -> Credential {
        let mut c = Credential::from_file(
            Path::new("/a"),
            Path::new("/a/c.json"),
            meta.as_object().unwrap().clone(),
        )
        .unwrap();
        for (k, v) in attrs {
            c.attributes.insert((*k).into(), (*v).into());
        }
        c
    }

    /// Go `encoding/json` into `struct{ ID, Model string }`.
    #[test]
    fn routing_fields_decode_like_go() {
        let r = |s: &str| routing(s.as_bytes());
        let pair = |a: &str, b: &str| (a.to_owned(), b.to_owned());
        assert_eq!(
            r(r#"{"id":"s","id":"t","model":"unregistered","query":"q"}"#),
            pair("t", "unregistered"),
            "duplicates: last wins, other fields kept"
        );
        assert_eq!(r(r#"{"ID":" a ","Model":"m"}"#), pair("a", "m"), "keys fold case");
        assert_eq!(
            r(r#"{"id":1,"model":"m","id":null}"#),
            pair("", "m"),
            "type errors skip only that field"
        );
        assert_eq!(r(r#"{"id":"a","model":"m""#), pair("", ""), "invalid JSON sets nothing");
        assert_eq!(r("[1]"), pair("", ""));
    }

    #[test]
    fn credential_policy_matches_go() {
        let oauth = cred(serde_json::json!({"type":"codex","access_token":"t"}), &[]);
        assert!(allowed(&oauth));
        let key = cred(serde_json::json!({"type":"codex"}), &[("api_key", "k")]);
        assert!(!allowed(&key), "API keys need alpha-search");
        let key = cred(
            serde_json::json!({"type":"codex"}),
            &[("api_key", "k"), ("codex_alpha_search", "true")],
        );
        assert!(allowed(&key));
        let unknown = cred(serde_json::json!({"type":"codex"}), &[]);
        assert!(!allowed(&unknown), "no kind, no access");
        let other = cred(serde_json::json!({"type":"claude","access_token":"t"}), &[]);
        assert!(!allowed(&other));
        let explicit = cred(
            serde_json::json!({"type":"codex","access_token":"t"}),
            &[("auth_kind", "api-key")],
        );
        assert!(!allowed(&explicit), "explicit kind wins over token metadata");
    }
}