Skip to main content

cpa_exec/
codex.rs

1//! Codex executor: OpenAI Responses on the ChatGPT Codex backend, with an OAuth token
2//! (bearer + `Chatgpt-Account-Id`) or a configured API key (`codex-api-key`).
3//!
4//! HTTP paths port codex_executor_execute.go and codex_executor_stream.go. The WebSocket
5//! upstream lives in `codex_ws`.
6
7use std::sync::Arc;
8use std::time::Instant;
9
10use bytes::Bytes;
11use chrono::Utc;
12use cpa_core::config::Config;
13use cpa_core::credential::{Credential, MetadataPatch};
14use cpa_core::exec::{
15    ExecError, ExecRequest, ExecResponse, ExecSession, ExecStream, FailureScope, Operation, ResponseBody,
16};
17use cpa_core::format::Format;
18use futures_util::StreamExt;
19use http::HeaderMap;
20
21use crate::codex_capture::Wire;
22use crate::codex_oauth::{self, CodexOAuth};
23use crate::codex_quota::QuotaSignals;
24use crate::codex_request::{self as request, Call, Settings, View};
25use crate::codex_response::{self as response, Bootstrap, Processor};
26
27pub use crate::codex_request::DEFAULT_BASE_URL;
28pub use crate::codex_ws::{ClientFrames, InterruptError, NO_ACTIVE_UPSTREAM, SteeringInput};
29
30pub struct CodexExecutor {
31    /// Chrome profile for chatgpt.com, Go's standard transport elsewhere, per proxy.
32    pub(crate) transport: crate::codex_tls::Transport,
33    oauth: CodexOAuth,
34    quota: Arc<QuotaSignals>,
35    /// Upstream Responses WebSocket sockets per downstream session.
36    pub(crate) ws: Arc<crate::codex_ws::Pool>,
37    /// Claude clients' reasoning replay (process-wide in Go).
38    pub(crate) replay: Arc<crate::codex_replay::Cache>,
39    /// Base for OAuth Alpha Search, which Go never derives from credential attributes.
40    alpha_base_url: String,
41    /// Live call, sideband and hangup URLs (`codex_live`).
42    pub(crate) live: crate::codex_live::Endpoints,
43}
44
45/// The production executor. Construction only fails if the TLS backend cannot initialise.
46impl Default for CodexExecutor {
47    fn default() -> Self {
48        Self::new().expect("codex HTTP client")
49    }
50}
51
52impl CodexExecutor {
53    /// Production transports: uTLS Chrome for chatgpt.com, Go's standard transport
54    /// elsewhere, both per effective proxy (crate::proxy). Token refreshes use the
55    /// standard transport for the credential's effective proxy.
56    pub fn new() -> wreq::Result<Self> {
57        Ok(Self::with_transport(
58            crate::codex_tls::Transport::new(crate::proxy::Hooks::default()),
59            CodexOAuth::new(crate::proxy::default_client()),
60        ))
61    }
62
63    /// Caller-built transports. Tests pass a plain client and an OAuth service pointed at
64    /// a local mock so nothing reaches OpenAI. Unproxied non-chatgpt.com requests use
65    /// `client`.
66    pub fn with_client(client: wreq::Client, oauth: CodexOAuth) -> Self {
67        Self::with_transport(crate::codex_tls::Transport::with_default(client), oauth)
68    }
69
70    fn with_transport(transport: crate::codex_tls::Transport, oauth: CodexOAuth) -> Self {
71        Self {
72            transport,
73            oauth,
74            quota: Arc::default(),
75            ws: Default::default(),
76            replay: Arc::default(),
77            alpha_base_url: DEFAULT_BASE_URL.into(),
78            live: Default::default(),
79        }
80    }
81
82    /// Test hook: where OAuth Alpha Search requests go.
83    pub fn with_alpha_base_url(mut self, base: impl Into<String>) -> Self {
84        self.alpha_base_url = base.into().trim_end_matches('/').to_owned();
85        self
86    }
87
88    /// Passive quota snapshots per credential (M4-0016), for the management API.
89    pub fn quota(&self) -> &QuotaSignals {
90        &self.quota
91    }
92
93    pub(crate) fn quota_handle(&self) -> Arc<QuotaSignals> {
94        self.quota.clone()
95    }
96
97    /// One turn of a downstream Responses WebSocket session (`CodexAutoExecutor`):
98    /// credentials with `websockets` enabled keep a pooled upstream socket; others run
99    /// the HTTP stream with the session as prompt-cache identity and cannot continue
100    /// upstream state.
101    pub async fn execute_in_session(
102        &self,
103        credential: &Credential,
104        req: ExecRequest,
105        cfg: &Config,
106        session: &ExecSession,
107    ) -> Result<ExecResponse, ExecError> {
108        check_response_format(&req)?;
109        crate::codex_siwc::require_token(credential)?;
110        let view = View::for_request(credential, cfg).with_session(explicit_session(&req));
111        let settings = Settings::scoped(cfg, &view);
112        if view.websockets() {
113            return self.stream_ws(&view, &settings, req, session, cfg).await;
114        }
115        if session.continuation {
116            return Err(ExecError::replay_required());
117        }
118        self.stream_with_session(&view, &settings, req, Some(&session.id)).await
119    }
120
121    /// Whether `credential` keeps upstream state on a WebSocket (Go:
122    /// `websocketUpstreamSupportsIncrementalInput`).
123    pub fn upstream_websocket(credential: &Credential) -> bool {
124        View::new(credential).websockets()
125    }
126
127    /// `upstream.codex.response-steering` as the Responses WebSocket handler reads it
128    /// (Go `SDKConfig.CodexResponseSteering`), shared by every credential kind.
129    pub fn response_steering_configured(cfg: &Config) -> bool {
130        request::response_steering(cfg)
131    }
132
133    /// Binds a downstream connection's later client frames to its session (Go
134    /// `WithWebsocketInput` and `WithWebsocketAuthCheck`). A steering turn on that session
135    /// reads them after its first `response.created`; [`Self::close_session`] releases them.
136    pub fn attach_steering(&self, session: &str, input: SteeringInput) {
137        self.ws.attach_steering(session, input);
138    }
139
140    /// Go `InterruptExecutionSession`: forwards a client's `response.interrupt` to the
141    /// session's upstream socket while a turn reads on it (see `codex_ws::Pool::interrupt`).
142    pub async fn interrupt_session(
143        &self,
144        session: &str,
145        payload: &[u8],
146        enabled: impl FnOnce(&str) -> bool,
147    ) -> Result<(), InterruptError> {
148        self.ws.interrupt(session, payload, enabled).await
149    }
150
151    /// The error that lost the session's upstream socket, once it is lost (what Go's
152    /// disconnect notifier hands the downstream handler).
153    pub fn session_loss(&self, id: &str) -> Option<ExecError> {
154        self.ws.loss(id)
155    }
156
157    /// Resolves when the session's upstream socket is lost.
158    pub fn session_closed(&self, id: &str) -> impl std::future::Future<Output = ExecError> + Send + 'static {
159        self.ws.closed(id)
160    }
161
162    /// Releases the session's upstream socket (downstream connection ended).
163    pub fn close_session(&self, id: &str) {
164        self.ws.close(id);
165    }
166
167    /// Refresh due: the outer tokens under Go's 96h Codex lead, or the Sign in with
168    /// ChatGPT partition under its 5-minute lead (Go `shouldRefresh`). Cheap and side-effect free.
169    pub fn needs_prepare(&self, credential: &Credential, cfg: &Config) -> bool {
170        self.needs_prepare_at(credential, cfg, Utc::now())
171    }
172
173    pub fn needs_prepare_at(&self, credential: &Credential, _cfg: &Config, now: chrono::DateTime<Utc>) -> bool {
174        codex_oauth::refresh_due(credential, now) || crate::codex_siwc::refresh_due(credential, now) == Some(true)
175    }
176
177    /// The refresh grants for `credential`, on the client its proxy selects
178    /// (`NewCodexAuthWithProxyURL(cfg, auth.ProxyURL)`: the credential's proxy, then the
179    /// global one, on Go's standard transport).
180    pub fn refresher(&self, credential: &Credential, cfg: &Config) -> crate::codex_refresh::Refresher {
181        let proxy = crate::proxy::Proxy::effective(credential, cfg);
182        crate::codex_refresh::Refresher::new(self.oauth.with_client(self.transport.standard(&proxy)))
183    }
184
185    /// [`crate::codex_refresh::Refresher::prepare`].
186    pub async fn prepare(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
187        self.refresher(credential, cfg).prepare(credential).await
188    }
189
190    /// [`crate::codex_refresh::Refresher::refresh_now`].
191    pub async fn refresh_now(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
192        self.refresher(credential, cfg).refresh_now(credential).await
193    }
194
195    /// [`crate::codex_refresh::Refresher::refresh_all`].
196    pub async fn refresh_all(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
197        self.refresher(credential, cfg).refresh_all(credential).await
198    }
199
200    pub async fn execute(
201        &self,
202        credential: &Credential,
203        req: ExecRequest,
204        cfg: &Config,
205    ) -> Result<ExecResponse, ExecError> {
206        if req.operation == Operation::CountTokens {
207            return count_tokens(&req, cfg);
208        }
209        check_response_format(&req)?;
210        crate::codex_siwc::require_token(credential)?;
211        let view = View::for_request(credential, cfg).with_session(explicit_session(&req));
212        let settings = Settings::scoped(cfg, &view);
213        match (req.alt.as_deref(), req.stream) {
214            (Some("responses/compact"), true) => Err(ExecError::local(
215                400,
216                FailureScope::Request,
217                "streaming not supported for /responses/compact",
218            )),
219            (Some("responses/compact"), false) => self.compact(&view, &settings, req).await,
220            (_, true) => self.stream_with_session(&view, &settings, req, None).await,
221            (_, false) => self.buffered(&view, &settings, req).await,
222        }
223    }
224
225    /// Go's `http.Client.Do` through the Codex transport: the client is picked per hop
226    /// (Chrome for chatgpt.com, Go's standard transport elsewhere), redirects are
227    /// followed like Go's, and gzip is undone only when the transport asked for it.
228    pub(crate) async fn post(
229        &self,
230        view: &View<'_>,
231        url: &str,
232        headers: &HeaderMap,
233        body: Bytes,
234    ) -> Result<crate::proxy::Upstream, ExecError> {
235        let transport = &self.transport;
236        let (proxy, keep_alive, handshake) = (&view.proxy, view.chatgpt_keep_alive, view.handshake);
237        let route = |hop: &url::Url| {
238            Ok(crate::proxy::Route {
239                client: transport.for_url(hop.as_str(), proxy, keep_alive, handshake),
240                order: None,
241            })
242        };
243        crate::proxy::send_routed(&route, url, go_headers(headers), body, None, None).await
244    }
245
246    /// One POST to the Codex backend, captured as Go's call sites record it: the
247    /// request, a transport error, the response metadata and an error body. A non-2xx
248    /// answer is the returned error; success bodies are read (and recorded) by the
249    /// caller as its Go call site does.
250    #[allow(clippy::too_many_arguments)]
251    pub(crate) async fn open(
252        &self,
253        view: &View<'_>,
254        settings: &Settings,
255        url: String,
256        headers: HeaderMap,
257        body: String,
258        replay: &crate::codex_replay::Scope,
259        wire: &Wire,
260        error_body: ErrorBody,
261    ) -> Result<crate::proxy::Upstream, ExecError> {
262        // The shaped body names the base model, the key of per-model quota snapshots.
263        let model = gjson::get(&body, "model").str().to_owned();
264        wire.request(&url, &headers, body.as_bytes());
265        let mut upstream = match self.post(view, &url, &headers, Bytes::from(body)).await {
266            Ok(upstream) => upstream,
267            Err(error) => {
268                // ponytail: the text is this executor's transport message, not net/http's
269                // `Post "<url>": ...`.
270                wire.exec_error(&error);
271                return Err(error);
272            }
273        };
274        wire.metadata(upstream.status, &upstream.headers);
275        self.quota.observe(&view.credential.id, &model, &upstream.headers);
276        let status = upstream.status;
277        upstream.headers.remove(http::header::CONTENT_ENCODING);
278        upstream.headers.remove(http::header::CONTENT_LENGTH);
279        if !(200..300).contains(&status) {
280            let lossy = error_body == ErrorBody::Lossy;
281            let body = match crate::proxy::read_all(upstream.body, crate::proxy::MAX_ERROR_BODY, lossy).await {
282                Ok(body) => body,
283                Err(error) => {
284                    wire.exec_error(&error);
285                    return Err(error);
286                }
287            };
288            wire.chunk(&body);
289            crate::codex_replay::clear_on_invalid_signature(&self.replay, replay, status, &body);
290            return Err(response::status_error(
291                status,
292                &body,
293                upstream.headers,
294                settings.model_level_cooling,
295            ));
296        }
297        Ok(upstream)
298    }
299
300    /// [`Self::open`], then the body: SSE framed into events, anything else read whole.
301    #[allow(clippy::too_many_arguments)]
302    pub(crate) async fn send(
303        &self,
304        view: &View<'_>,
305        settings: &Settings,
306        url: String,
307        headers: HeaderMap,
308        body: String,
309        replay: &crate::codex_replay::Scope,
310        wire: &Wire,
311        error_body: ErrorBody,
312    ) -> Result<ExecResponse, ExecError> {
313        let upstream = self
314            .open(view, settings, url, headers, body, replay, wire, error_body)
315            .await?;
316        let (status, headers) = (upstream.status, upstream.headers);
317        let sse = headers
318            .get(http::header::CONTENT_TYPE)
319            .and_then(|v| v.to_str().ok())
320            .is_some_and(|v| v.starts_with("text/event-stream"));
321        let body = if sse {
322            ResponseBody::Stream(crate::upstream::framed(upstream.body))
323        } else {
324            match crate::proxy::read_all(upstream.body, usize::MAX, false).await {
325                Ok(body) => ResponseBody::Buffered(body),
326                Err(error) => {
327                    wire.exec_error(&error);
328                    return Err(error);
329                }
330            }
331        };
332        Ok(ExecResponse { status, headers, body })
333    }
334
335    /// HTTP streaming. `ws_session` is the downstream WebSocket connection when an HTTP
336    /// credential serves a WebSocket client (Go's execution session id).
337    pub(crate) async fn stream_with_session(
338        &self,
339        view: &View<'_>,
340        settings: &Settings,
341        req: ExecRequest,
342        ws_session: Option<&str>,
343    ) -> Result<ExecResponse, ExecError> {
344        let model = request::base_model(&req.model).to_owned();
345        let request::Shaped {
346            body,
347            optimized: restore,
348            finalize,
349            ..
350        } = request::shape(&req, view, settings, Call::Stream)?;
351        let (body, scope) = crate::codex_replay::apply(&self.replay, &req, body).await;
352        let (body, cache) = request::prompt_cache(&req, body, ws_session, false);
353        let body = finalize.apply(&req, settings, request::sanitize_input_ids(body));
354        report_request(&req, Format::Codex, &body);
355        let headers = request::http_headers(view, settings, &req.headers, &body, &model, cache.as_deref(), true);
356        let url = format!("{}/responses", view.base_url);
357        let started = Instant::now();
358        let wire = Wire::new(req.capture(), view.credential);
359        // The response waits for the Home replay writes it caused (Go writes inline).
360        let writes = scope.writes.clone();
361        let sent = self
362            .send(
363                view,
364                settings,
365                url,
366                headers,
367                body.clone(),
368                &scope,
369                &wire,
370                ErrorBody::Strict,
371            )
372            .await;
373        let res = match sent {
374            Ok(res) => res,
375            Err(error) => {
376                writes.settle().await;
377                return Err(error);
378            }
379        };
380        let upstream = events(res.body);
381        let processor = Processor::new(request::is_native(&req), settings.model_level_cooling)
382            .grok_keepalive(&req.headers)
383            .restoring(restore)
384            .replaying(self.replay.clone(), scope)
385            .reporting(req.usage.clone())
386            .capturing(wire);
387        let stream = if settings.bootstrap_buffering {
388            match response::bootstrap(upstream, processor, settings.bootstrap_timeout, started).await {
389                Bootstrap::Reject(error) => {
390                    writes.settle().await;
391                    return Err(error);
392                }
393                Bootstrap::Stream(stream) => stream,
394            }
395        } else {
396            response::processed(upstream, processor, 0)
397        };
398        Ok(ExecResponse {
399            status: res.status,
400            headers: res.headers,
401            body: ResponseBody::Stream(writes.gate(client_stream(&req, &body, stream))),
402        })
403    }
404
405    async fn buffered(
406        &self,
407        view: &View<'_>,
408        settings: &Settings,
409        req: ExecRequest,
410    ) -> Result<ExecResponse, ExecError> {
411        let model = request::base_model(&req.model).to_owned();
412        let request::Shaped {
413            body,
414            optimized: restore,
415            finalize,
416            ..
417        } = request::shape(&req, view, settings, Call::NonStream)?;
418        let (body, scope) = crate::codex_replay::apply(&self.replay, &req, body).await;
419        let (body, cache) = request::prompt_cache(&req, body, None, false);
420        let body = finalize.apply(&req, settings, request::sanitize_input_ids(body));
421        report_request(&req, Format::Codex, &body);
422        let headers = request::http_headers(view, settings, &req.headers, &body, &model, cache.as_deref(), true);
423        let url = format!("{}/responses", view.base_url);
424        let wire = Wire::new(req.capture(), view.credential);
425        // The response waits for the Home replay writes it caused (Go writes inline).
426        let writes = scope.writes.clone();
427        let result = async {
428            let res = self
429                .open(
430                    view,
431                    settings,
432                    url,
433                    headers,
434                    body.clone(),
435                    &scope,
436                    &wire,
437                    ErrorBody::Lossy,
438                )
439                .await?;
440            // Go reads the whole body (`io.ReadAll`, whatever its type), records every
441            // byte that arrived, then parses it, a read error's partial tail included.
442            let mut upstream = res.body;
443            let (mut data, mut failed) = (Vec::new(), None);
444            while let Some(chunk) = upstream.next().await {
445                match chunk {
446                    Ok(chunk) => data.extend_from_slice(&chunk),
447                    Err(error) => {
448                        failed = Some(error);
449                        break;
450                    }
451                }
452            }
453            wire.chunk(&data);
454            let mut processor = Processor::new(false, settings.model_level_cooling)
455                .restoring(restore)
456                .replaying(self.replay.clone(), scope)
457                .reporting(req.usage.clone());
458            // `bytes.Split(data, "\n")`: every line in order, an unterminated tail included.
459            if let Some(completed) = processor.buffered(&data)? {
460                return Ok(ExecResponse {
461                    status: res.status,
462                    headers: res.headers,
463                    body: ResponseBody::Buffered(non_stream_output(&req, &body, &completed, Format::Codex)?),
464                });
465            }
466            if let Some(error) = failed {
467                wire.exec_error(&error);
468            }
469            Err(response::request_scoped(408, response::INCOMPLETE_MESSAGE))
470        }
471        .await;
472        writes.settle().await;
473        result
474    }
475
476    async fn compact(&self, view: &View<'_>, settings: &Settings, req: ExecRequest) -> Result<ExecResponse, ExecError> {
477        let model = request::base_model(&req.model).to_owned();
478        let request::Shaped {
479            body,
480            optimized: restore,
481            finalize,
482            ..
483        } = request::shape(&req, view, settings, Call::Compact)?;
484        let (body, cache) = request::prompt_cache(&req, body, None, false);
485        let body = finalize.apply(&req, settings, request::sanitize_input_ids(body));
486        report_request(&req, Format::OpenAIResponse, &body);
487        let headers = request::http_headers(view, settings, &req.headers, &body, &model, cache.as_deref(), false);
488        let url = format!("{}/responses/compact", view.base_url);
489        let wire = Wire::new(req.capture(), view.credential);
490        let res = self
491            .send(
492                view,
493                settings,
494                url,
495                headers,
496                body.clone(),
497                &Default::default(),
498                &wire,
499                ErrorBody::Lossy,
500            )
501            .await
502            .map_err(compact_error)?;
503        let data = match res.body {
504            ResponseBody::Buffered(bytes) => bytes,
505            ResponseBody::Stream(mut stream) => {
506                let mut out = Vec::new();
507                while let Some(event) = stream.next().await {
508                    match event {
509                        Ok(event) => out.extend_from_slice(&event),
510                        Err(error) => {
511                            wire.exec_error(&error);
512                            return Err(compact_error(error));
513                        }
514                    }
515                }
516                Bytes::from(out)
517            }
518        };
519        wire.chunk(&data);
520        let text = String::from_utf8_lossy(&data);
521        let text = response::restore(&text, restore);
522        if req.usage.enabled() {
523            // Go publishes `ParseOpenAIUsage` of the restored compaction body.
524            req.usage.response_body(Format::OpenAIResponse, text.as_bytes());
525        }
526        Ok(ExecResponse {
527            status: res.status,
528            headers: res.headers,
529            body: ResponseBody::Buffered(non_stream_output(&req, &body, &text, Format::OpenAIResponse)?),
530        })
531    }
532
533    /// The URL an Alpha Search on `credential` posts to; `None` for an API key without
534    /// a base URL.
535    pub fn alpha_search_url(&self, credential: &Credential, cfg: &Config) -> Option<String> {
536        self.alpha_search_url_for(&View::for_request(credential, cfg))
537    }
538
539    fn alpha_search_url_for(&self, view: &View<'_>) -> Option<String> {
540        if !view.api_key {
541            return Some(format!("{}/alpha/search", self.alpha_base_url));
542        }
543        let base = view.attr("base_url").trim();
544        (!base.is_empty()).then(|| format!("{}/alpha/search", base.trim_end_matches('/')))
545    }
546
547    /// The standalone Codex Alpha Search call (`Server.codexAlphaSearch` minus selection):
548    /// already in Codex search format, never translated. The upstream status and body are
549    /// returned as is, with only its Content-Type.
550    ///
551    /// `upstream_model` is the credential-resolved model; it replaces `model` for API keys.
552    /// `capture` records the attempt as Go's route does (`server_routes.go`).
553    pub async fn alpha_search(
554        &self,
555        credential: &Credential,
556        body: &[u8],
557        client: &HeaderMap,
558        upstream_model: &str,
559        cfg: &Config,
560        capture: &cpa_core::exec::CaptureSink,
561    ) -> Result<ExecResponse, ExecError> {
562        // Go selects with `X-Session-ID` set from the body's `id`; the request context then
563        // carries that explicit session for `$CPA-SESSION-ID`.
564        let mut selection_headers = client.clone();
565        let id = gjson::get(&String::from_utf8_lossy(body), "id").str().trim().to_owned();
566        if !id.is_empty()
567            && let Ok(value) = http::HeaderValue::from_str(&id)
568        {
569            selection_headers.insert("x-session-id", value);
570        }
571        let session = cpa_common::session::cpa_session_id(Some(&cpa_common::session::extract_session_id(
572            &selection_headers,
573            body,
574            &Default::default(),
575        )));
576        let view = View::for_request(credential, cfg).with_session(session);
577        let mut body = sanitize_alpha_search(body);
578        let Some(url) = self.alpha_search_url_for(&view) else {
579            return Err(ExecError::local(
580                503,
581                FailureScope::Credential,
582                "Codex Alpha Search API key base URL unavailable",
583            ));
584        };
585        if view.api_key && !upstream_model.trim().is_empty() {
586            body = rewrite_alpha_search_model(body, upstream_model.trim());
587        }
588        // `PrepareRequest` (inside `NewHttpRequest`): Sign in with ChatGPT needs its token.
589        crate::codex_siwc::require_token(credential)?;
590        let mut headers = HeaderMap::new();
591        let mut set = |name: &'static str, value: &str| {
592            if let Ok(value) = http::HeaderValue::from_str(value) {
593                headers.insert(name, value);
594            }
595        };
596        set("content-type", "application/json");
597        set("accept", "application/json");
598        set("originator", "codex_cli_rs");
599        for name in ["version", "user-agent", "session_id", "x-client-request-id"] {
600            let value = request::header(client, name).trim();
601            if !value.is_empty() {
602                set(name, value);
603            }
604        }
605        if let Some(account) = credential.str("account_id").map(str::trim).filter(|s| !s.is_empty()) {
606            set("chatgpt-account-id", account);
607        }
608        // PrepareRequest: bearer from the API key or access token, then operator headers.
609        if !view.token.trim().is_empty() {
610            set("authorization", &format!("Bearer {}", view.token));
611        }
612        for (name, value) in request::custom_headers(&view, client) {
613            if let (Ok(name), Ok(value)) = (
614                http::HeaderName::try_from(name.as_str()),
615                http::HeaderValue::from_str(&value),
616            ) {
617                headers.insert(name, value);
618            }
619        }
620        let wire = Wire::new(capture, credential);
621        wire.request(&url, &headers, &body);
622        let upstream = match self.post(&view, &url, &headers, Bytes::from(body)).await {
623            Ok(upstream) => upstream,
624            Err(error) => {
625                wire.exec_error(&error);
626                return Err(error);
627            }
628        };
629        wire.metadata(upstream.status, &upstream.headers);
630        let status = upstream.status;
631        let content_type = upstream.headers.get(http::header::CONTENT_TYPE).cloned();
632        // `io.ReadAll(io.LimitReader(resp.Body, 32 MiB))`: bytes past the limit are never
633        // read off the socket. On a read error Go records what arrived, then the error.
634        let mut body = upstream.body;
635        let mut data = Vec::new();
636        let failed = loop {
637            if data.len() >= ALPHA_SEARCH_MAX_RESPONSE {
638                break None;
639            }
640            match body.next().await {
641                Some(Ok(chunk)) => {
642                    let take = chunk.len().min(ALPHA_SEARCH_MAX_RESPONSE - data.len());
643                    data.extend_from_slice(&chunk[..take]);
644                }
645                Some(Err(error)) => break Some(error),
646                None => break None,
647            }
648        };
649        wire.chunk(&data);
650        if let Some(error) = failed {
651            wire.exec_error(&error);
652            return Err(ExecError::local(
653                502,
654                FailureScope::Transport,
655                "Failed to read Codex search response",
656            ));
657        }
658        let data = Bytes::from(data);
659        let mut headers = HeaderMap::new();
660        if let Some(value) = content_type {
661            headers.insert(http::header::CONTENT_TYPE, value);
662        }
663        Ok(ExecResponse {
664            status,
665            headers,
666            body: ResponseBody::Buffered(data),
667        })
668    }
669}
670
671/// `CountTokens` (codex_executor_tokens.go): the request shaped as for Codex, counted
672/// locally with the model's tiktoken encoding; nothing is sent upstream.
673fn count_tokens(req: &ExecRequest, cfg: &Config) -> Result<ExecResponse, ExecError> {
674    use crate::codex_json::{delete, set_bool_if_different, set_str, set_str_if_different};
675    let model = request::base_model(&req.model);
676    // Go reads is-compat only from the attempt's binding here (APIKeyModelIsCompat).
677    let is_compat = req.resolved_model.as_ref().is_some_and(|r| r.is_compat());
678    let client = crate::codex_client::Client::new(&req.headers, cfg, "codex", is_compat);
679    let caps = req
680        .resolved_model
681        .as_ref()
682        .map(|r| cpa_common::thinking::ModelCaps::from(&r.info));
683    let ctx = cpa_translate::RequestCtx {
684        model: &model,
685        stream: false,
686        tool_search: false,
687    };
688    let translated = crate::codex_client::translate_request(req.source_format, Format::Codex, &ctx, &req.body, &client)
689        .map_err(|e| ExecError::local(400, FailureScope::Request, e.to_string()))?;
690    // The rule baseline: the translated original, else the translated body before
691    // thinking. Go counts with a failed translation's partial body; this refuses.
692    let original = if req.original_body.is_empty() {
693        translated.body.clone()
694    } else {
695        crate::codex_client::translate_request(req.source_format, Format::Codex, &ctx, &req.original_body, &client)
696            .map_err(|e| ExecError::local(400, FailureScope::Request, e.to_string()))?
697            .body
698    };
699    let body = cpa_common::thinking::apply_request_thinking(&cpa_common::thinking::RequestThinking {
700        body: &translated.body,
701        payload: &req.body,
702        original: &req.original_body,
703        model: &req.model,
704        from: req.source_format.as_str(),
705        to: Format::Codex.as_str(),
706        provider: "codex",
707        resolved: caps.as_ref().map(Some),
708        has_request_transformer: cpa_translate::pair(req.source_format, Format::Codex).is_some(),
709        updates_changed: translated.configuration_updates_changed,
710    })
711    .map_err(|e| ExecError::local(e.status(), FailureScope::Request, e.message))?;
712    let mut body = String::from_utf8_lossy(&body).into_owned();
713    body = set_str_if_different(body, "model", &model);
714    for key in [
715        "previous_response_id",
716        "generate",
717        "prompt_cache_retention",
718        "safety_identifier",
719        "stream_options",
720    ] {
721        body = delete(&body, key);
722    }
723    body = set_bool_if_different(body, "stream", false);
724    if !request::is_native(req) {
725        let instructions = gjson::get(&body, "instructions");
726        if !instructions.exists() || instructions.kind() == gjson::Kind::Null {
727            body = set_str(&body, "instructions", "");
728        }
729    }
730    let body = crate::payload_barrier::Barrier {
731        executor: "codex",
732        model: &model,
733        protocol: Format::Codex.as_str(),
734        root: "",
735        original: &original,
736    }
737    .apply(cfg, req, body.into_bytes());
738    let encoding = crate::codex_tokens::encoding_for_model(&model);
739    let count = crate::codex_tokens::count_input_tokens(encoding, &body).map_err(|e| {
740        ExecError::local(
741            500,
742            FailureScope::Request,
743            format!("codex executor: tokenizer init failed: {e}"),
744        )
745    })?;
746    let usage =
747        format!(r#"{{"response":{{"usage":{{"input_tokens":{count},"output_tokens":0,"total_tokens":{count}}}}}}}"#);
748    let payload = cpa_translate::translate_token_count(req.response_format, Format::Codex, count, usage.as_bytes());
749    Ok(ExecResponse {
750        status: 200,
751        headers: HeaderMap::new(),
752        body: ResponseBody::Buffered(Bytes::from(payload)),
753    })
754}
755
756/// Go reads at most 32 MiB of an Alpha Search response, whatever its status.
757const ALPHA_SEARCH_MAX_RESPONSE: usize = 32 << 20;
758
759/// Go's compact policy (`isResponsesCompactRequestFaultError`,
760/// `isResponsesCompactAvailabilityNeutralError`): an upstream without compact support must
761/// not cool ordinary Responses traffic. Request faults stop the request; other failures
762/// except auth/payment/quota fail over without cooldown.
763// ponytail: Go also exempts Cloudflare challenges and invalid_grant bodies from both rules;
764// those classifiers are not ported (M4-0024).
765fn compact_error(mut error: ExecError) -> ExecError {
766    let credential_quota = error.status == 429 && error.scope == FailureScope::Credential;
767    if credential_quota || matches!(error.status, 401 | 402 | 403 | 429) || error.scope == FailureScope::Transport {
768        return error;
769    }
770    let body = String::from_utf8_lossy(&error.body);
771    error.scope = if matches!(error.status, 400 | 404 | 405 | 409 | 413 | 422 | 501)
772        || response::request_fault(error.status, &body)
773    {
774        FailureScope::Request
775    } else {
776        FailureScope::Transport
777    };
778    error
779}
780
781/// `sanitizeCodexAlphaSearchBody`: drop prompt-cache fields, re-marshalling only when one
782/// was present (Go's map round trip sorts keys and compacts).
783pub fn sanitize_alpha_search(body: &[u8]) -> Vec<u8> {
784    let Ok(text) = std::str::from_utf8(body) else {
785        return body.to_vec();
786    };
787    crate::codex_json::go_remarshal_object(text, |members| {
788        let a = members.remove("prompt_cache_key").is_some();
789        let b = members.remove("prompt_cache_retention").is_some();
790        a || b
791    })
792    .map(String::into_bytes)
793    .unwrap_or_else(|| body.to_vec())
794}
795
796/// `rewriteCodexAlphaSearchModel`.
797fn rewrite_alpha_search_model(body: Vec<u8>, model: &str) -> Vec<u8> {
798    let Ok(text) = std::str::from_utf8(&body) else {
799        return body;
800    };
801    let quoted = crate::codex_json::go_quote(model, true);
802    crate::codex_json::go_remarshal_object(text, |members| match members.get_mut("model") {
803        Some(current) if *current != quoted => {
804            *current = quoted.clone();
805            true
806        }
807        _ => false,
808    })
809    .map(String::into_bytes)
810    .unwrap_or(body)
811}
812
813/// The shaped headers as Go's `http.Header`: canonical keys, values in order. Go's
814/// transport adds `Accept-Encoding: gzip` itself unless a header already set it.
815/// How a call site reads a non-2xx body: Go's Execute and compact ignore a read error
816/// (`b, _ := io.ReadAll`) and report what arrived; its stream and Images paths record the
817/// read error and return it instead.
818#[derive(Clone, Copy, PartialEq, Eq)]
819pub(crate) enum ErrorBody {
820    Lossy,
821    Strict,
822}
823
824fn go_headers(headers: &HeaderMap) -> crate::proxy::GoHeaders {
825    let mut out = crate::proxy::GoHeaders::new();
826    for (name, value) in headers {
827        out.add_raw(
828            &crate::proxy::canonical_header(name.as_str()),
829            String::from_utf8_lossy(value.as_bytes()),
830        );
831    }
832    out
833}
834
835/// Go `SetTranslatedReasoningEffort`: the finalized upstream payload (prompt-cache key
836/// set, input IDs sanitized), for the usage record's reasoning effort (c7b4d573).
837pub(crate) fn report_request(req: &ExecRequest, upstream: Format, body: &str) {
838    if req.usage.enabled() {
839        req.usage.request(upstream, body.as_bytes());
840    }
841}
842
843/// Go's request-context session for `$CPA-SESSION-ID` headers.
844pub(crate) fn explicit_session(req: &ExecRequest) -> Option<String> {
845    cpa_common::session::cpa_session_id(req.session.as_deref())
846}
847
848fn check_response_format(req: &ExecRequest) -> Result<(), ExecError> {
849    let identity = matches!(req.response_format, Format::Codex | Format::OpenAIResponse);
850    if identity || cpa_translate::pair(req.response_format, Format::Codex).is_some() {
851        return Ok(());
852    }
853    Err(ExecError::local(
854        501,
855        FailureScope::Request,
856        format!(
857            "codex -> {} response translation is not registered",
858            req.response_format.as_str()
859        ),
860    ))
861}
862
863/// Upstream body as framed SSE events, whatever its declared content type (Go scans lines).
864pub(crate) fn events(body: ResponseBody) -> ExecStream {
865    match body {
866        ResponseBody::Stream(stream) => stream,
867        ResponseBody::Buffered(bytes) => {
868            let mut framer = cpa_translate::sse::Framer::default();
869            let mut events = framer.push(&bytes).unwrap_or_default();
870            events.extend(framer.finish());
871            futures_util::stream::iter(events.into_iter().map(Ok)).boxed()
872        }
873    }
874}
875
876fn translate_error(error: cpa_translate::Error) -> ExecError {
877    ExecError::local(502, FailureScope::Request, error.to_string())
878}
879
880/// The client body for a buffered response (`TranslateNonStream`): the registered pair,
881/// else Go's identity. A translator failure (Go's apply_patch tool-input error) or empty
882/// output is Go's 502 `ApplyPatchUpstreamErrorMessage`.
883fn non_stream_output(
884    req: &ExecRequest,
885    translated: &str,
886    completed: &str,
887    upstream: Format,
888) -> Result<Bytes, ExecError> {
889    let out = match cpa_translate::pair(req.response_format, upstream) {
890        Some(pair) => {
891            let ctx = cpa_translate::ResponseCtx {
892                model: &req.model,
893                original_request: &req.original_body,
894                translated_request: translated.as_bytes(),
895                tool_search: false,
896            };
897            (pair.non_stream)(&ctx, completed.as_bytes()).unwrap_or_default()
898        }
899        None => completed.as_bytes().to_vec(),
900    };
901    if out.is_empty() {
902        return Err(ExecError::local(
903            502,
904            FailureScope::Request,
905            cpa_translate::APPLY_PATCH_UPSTREAM_ERROR,
906        ));
907    }
908    let out = String::from_utf8_lossy(&out).into_owned();
909    let out = if req.response_format == Format::OpenAIResponse {
910        response::ensure_usage_details(out)
911    } else {
912        out
913    };
914    Ok(Bytes::from(out))
915}
916
917/// The client stream (`TranslateStreamWithClaudeInputTokens`): translated, with Go's
918/// usage details for OpenAI Responses clients and the input-token estimate for Claude
919/// clients.
920fn client_stream(req: &ExecRequest, translated: &str, upstream: ExecStream) -> ExecStream {
921    translate_stream(req, translated, upstream)
922}
923
924/// Streaming translation for non-Codex clients; identity for Codex clients. Go keeps
925/// streaming after an apply_patch tool-input failure (Codex supports the tool natively)
926/// but skips the usage and input-token touch-ups for those chunks.
927fn translate_stream(req: &ExecRequest, translated: &str, upstream: ExecStream) -> ExecStream {
928    let responses_client = req.response_format == Format::OpenAIResponse;
929    let Some(pair) = cpa_translate::pair(req.response_format, Format::Codex) else {
930        return if responses_client {
931            upstream
932                .map(|chunk| chunk.map(response::ensure_usage_details_chunk))
933                .boxed()
934        } else {
935            upstream
936        };
937    };
938    let ctx = cpa_translate::ResponseCtx {
939        model: &req.model,
940        original_request: &req.original_body,
941        translated_request: translated.as_bytes(),
942        tool_search: false,
943    };
944    let translator = crate::translate::stream(pair, &ctx, req);
945    let original = if req.original_body.is_empty() {
946        req.body.clone()
947    } else {
948        req.original_body.clone()
949    };
950    let claude =
951        crate::gemini_stream::ClaudeInputTokens::new(req.source_format, Format::Codex, req.response_format, original);
952    struct State {
953        upstream: ExecStream,
954        translator: Box<dyn cpa_translate::StreamTranslator>,
955        /// Claude clients' `message_start` input-token estimate.
956        claude: crate::gemini_stream::ClaudeInputTokens,
957        responses_client: bool,
958        ready: std::collections::VecDeque<Bytes>,
959        /// The terminal error, written after the frames it flushed.
960        failed: Option<ExecError>,
961        done: bool,
962    }
963    futures_util::stream::unfold(
964        State {
965            upstream,
966            translator,
967            claude,
968            responses_client,
969            ready: Default::default(),
970            failed: None,
971            done: false,
972        },
973        |mut st| async move {
974            loop {
975                if let Some(event) = st.ready.pop_front() {
976                    return Some((Ok(event), st));
977                }
978                if let Some(error) = st.failed.take() {
979                    return Some((Err(error), st));
980                }
981                if st.done {
982                    return None;
983                }
984                let result = match st.upstream.next().await {
985                    Some(Ok(event)) => st.translator.event(&event).map_err(translate_error),
986                    Some(Err(error)) => Err(error),
987                    None => {
988                        st.done = true;
989                        st.translator.finish().map_err(translate_error)
990                    }
991                };
992                match result {
993                    Ok(mut events) => {
994                        if !st.translator.tool_input_failed() {
995                            st.claude.apply(&mut events);
996                            if st.responses_client {
997                                events = events.into_iter().map(response::ensure_usage_details_chunk).collect();
998                            }
999                        }
1000                        st.ready.extend(events);
1001                    }
1002                    Err(error) => {
1003                        // Go's responsesSSEFramer flushes the pending client frame before a
1004                        // terminal error is written.
1005                        st.ready.extend(st.translator.flush_frames());
1006                        st.failed = Some(error);
1007                        st.done = true;
1008                    }
1009                }
1010            }
1011        },
1012    )
1013    .boxed()
1014}
1015
1016#[cfg(test)]
1017#[path = "codex_tests.rs"]
1018mod tests;