Skip to main content

cpa_exec/
claude.rs

1//! Claude executor: Anthropic Messages and count_tokens with OAuth tokens or API keys.
2//!
3//! The request pipeline follows claude_executor_execute.go / _stream.go / _tokens.go
4//! in order: client detection, session identity, translation, cloaking, diagnostics,
5//! cache policy, betas, MCP aliases, credential identity, CCH signing, headers, and
6//! the native transport. Responses restore aliases and record billing continuity.
7//!
8//! First-party Anthropic gets the native Claude Code transport (tls.rs); every other
9//! origin gets Go's standard transport (proxy.rs), exactly as Go's fallback round
10//! tripper does. Delegating executors (Kimi) pass a [`Delegation`].
11//!
12//! Thinking (`cpa_common::thinking`), signature sanitizing (`cpa_common::signature`)
13//! and request translation (`cpa_translate::translate_request`) are the shared modules.
14//!
15//! Payload rules (`cpa_common::payload`) are the last change to the body, as in Go
16//! v8.0.35 (868e137): only beta extraction, CCH signing and read-only checks follow.
17//!
18//! Usage reports go through `ExecRequest::usage` with Go's reporter calls: the body
19//! sent, the upstream body and lines, the TTFT marks, `Publish` on native Execute,
20//! required usage on translated Execute and ExecuteStream (no `EnsurePublished`), and
21//! tokenless failures where Go's `TrackFailure` or apply_patch hooks publish them. Go 6fecc6e has no Vertex delegation to
22//! this executor: `claudeCCHUpstreamVertex` has no caller, so CCH signing stays
23//! Anthropic-only.
24//!
25//! In Home mode, device profiles come from Home KV (`profile::resolve_required`) and
26//! so do credential identities (`crate::oauth`); request logs belong to the server.
27
28mod alias;
29mod betas;
30mod cloak;
31mod compaction;
32mod detect;
33mod headers;
34mod identity;
35#[cfg(test)]
36pub(crate) mod kv_test;
37mod profile;
38mod replay;
39mod session;
40mod settings;
41mod signals;
42mod signing;
43mod stream;
44mod telemetry;
45mod thread;
46mod tool_search;
47
48use cpa_core::gostr::GoStr;
49use std::sync::Arc;
50
51use bytes::{Bytes, BytesMut};
52use cpa_core::config::Config;
53use cpa_core::credential::{Credential, MetadataPatch};
54use cpa_core::exec::{
55    CaptureEvent, CaptureSink, ExecError, ExecRequest, ExecResponse, FailureScope, Operation, ResponseBody,
56    UpstreamRequest,
57};
58use cpa_core::format::Format;
59use futures_util::StreamExt;
60
61use crate::oauth::{self, OAuth};
62use crate::proxy::{GoClients, GoHeaders, Proxy, Route};
63use crate::rawjson;
64use crate::tls::Transport;
65use crate::upstream::{Decoded, decode_upstream};
66use crate::{quota, tokens, translate};
67use settings::Settings;
68
69pub const DEFAULT_BASE_URL: &str = "https://api.anthropic.com";
70/// The message of the executor's own 404 for a thread continuation whose state is gone.
71pub use thread::NOT_FOUND as THREAD_NOT_FOUND;
72pub const ANTHROPIC_VERSION: &str = "2023-06-01";
73/// Advertised in `CLIProxyAPI/<version>` when a caller-owned request has no User-Agent.
74pub(crate) const PROXY_VERSION: &str = env!("CARGO_PKG_VERSION");
75
76pub use crate::proxy::Hooks;
77
78/// The fields of a `"type": "claude"` credential this executor needs.
79pub struct ClaudeView<'a> {
80    pub access_token: &'a str,
81    pub email: &'a str,
82}
83
84impl<'a> ClaudeView<'a> {
85    pub fn new(credential: &'a Credential) -> Result<Self, ExecError> {
86        let access_token = credential
87            .attributes
88            .get("api_key")
89            .map(String::as_str)
90            .filter(|t| !t.is_empty())
91            .or_else(|| credential.str("access_token"))
92            .filter(|t| !t.is_empty())
93            .ok_or_else(|| ExecError::local(401, FailureScope::Credential, "claude credential has no access_token"))?;
94        Ok(Self {
95            access_token,
96            email: credential.str("email").unwrap_or_default(),
97        })
98    }
99}
100
101/// The first-party Anthropic transport.
102enum Native {
103    /// Production or harness: native and OAuth profiles per effective proxy.
104    Transport(Arc<Transport>),
105    /// Unit tests against plain HTTP mocks.
106    Fixed(wreq::Client),
107}
108
109/// What a delegating executor changes in the Claude pipeline. Go embeds a
110/// `ClaudeExecutor` configured this way (`NewKimiExecutor`).
111#[derive(Clone, Copy, Default)]
112pub struct Delegation {
113    /// `upstreamModelNormalizer`: the model sent upstream, given the base model.
114    pub upstream_model: Option<fn(&str) -> String>,
115    /// `countTokensUpstream`: count on the upstream for every origin and credential.
116    pub count_upstream: bool,
117    /// `requestLogProvider`: the provider request logs name (Kimi logs `kimi`); `None`
118    /// is the executor's identifier, `claude`.
119    pub request_log_provider: Option<&'static str>,
120}
121
122pub struct ClaudeExecutor {
123    native: Native,
124    /// Go standard-transport clients for every other origin.
125    go: Arc<GoClients>,
126    base_url: String,
127    oauth: OAuth,
128    replay: Arc<replay::ReplayCache>,
129    /// Passive quota snapshots per credential, for the management credential entry.
130    quota: Arc<crate::quota::Observations>,
131    /// Tool aliases thread continuations restore (thread.rs): this executor's, and a
132    /// delegating executor's (Kimi), which Go keeps in an executor of its own.
133    thread_aliases: Arc<thread::Aliases>,
134    delegated_thread_aliases: Arc<thread::Aliases>,
135}
136
137impl ClaudeExecutor {
138    /// Production executor: native wire profile with the process-global transport.
139    pub fn new(base_url: impl Into<String>) -> wreq::Result<Self> {
140        Ok(Self::with_transport(
141            Arc::new(Transport::new(Hooks::default())),
142            Hooks::default(),
143            base_url,
144        ))
145    }
146
147    /// The production profile with test trust roots and dial overrides (harness).
148    /// OAuth refresh uses the same transport, so it cannot escape the hooks.
149    pub fn with_hooks(hooks: Hooks, base_url: impl Into<String>) -> Self {
150        Self::with_transport(Arc::new(Transport::new(hooks.clone())), hooks, base_url)
151    }
152
153    fn with_transport(transport: Arc<Transport>, hooks: Hooks, base_url: impl Into<String>) -> Self {
154        Self {
155            oauth: OAuth::with_transport(transport.clone()),
156            replay: replay::shared(),
157            quota: Arc::default(),
158            thread_aliases: Arc::default(),
159            delegated_thread_aliases: Arc::default(),
160            native: Native::Transport(transport),
161            go: Arc::new(GoClients::new(hooks).timed()),
162            base_url: base_url.into().trim_end_matches('/').to_owned(),
163        }
164    }
165
166    /// A caller-built client for every unproxied request (plain mocks). First-party
167    /// requests keep the native header order on it, but not the native TLS profile.
168    pub fn with_client(client: wreq::Client, base_url: impl Into<String>) -> Self {
169        Self {
170            oauth: OAuth::new(client.clone()),
171            replay: replay::shared(),
172            quota: Arc::default(),
173            thread_aliases: Arc::default(),
174            delegated_thread_aliases: Arc::default(),
175            go: Arc::new(GoClients::with_default(client.clone())),
176            native: Native::Fixed(client),
177            base_url: base_url.into().trim_end_matches('/').to_owned(),
178        }
179    }
180
181    /// The latest quota signals each Claude credential's responses carried (Go
182    /// `QuotaState.ObserveResponseHeadersForProvider`).
183    pub fn quota(&self) -> &crate::quota::Observations {
184        &self.quota
185    }
186
187    /// Overrides the OAuth service (local token/profile mocks).
188    pub fn with_oauth(mut self, oauth: OAuth) -> Self {
189        self.oauth = oauth;
190        self
191    }
192
193    pub fn needs_prepare(&self, credential: &Credential, cfg: &Config) -> bool {
194        self.needs_prepare_at(credential, cfg, chrono::Utc::now())
195    }
196
197    pub fn needs_prepare_at(&self, credential: &Credential, _cfg: &Config, now: chrono::DateTime<chrono::Utc>) -> bool {
198        oauth::needs_prepare(credential, now)
199    }
200
201    pub async fn prepare(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
202        self.oauth.prepare(credential, &Proxy::effective(credential, cfg)).await
203    }
204
205    fn native_client(&self, proxy: &Proxy, handshake: Option<std::time::Duration>) -> Result<wreq::Client, ExecError> {
206        match &self.native {
207            Native::Fixed(client) => Ok(client.clone()),
208            Native::Transport(t) => t
209                .clients(proxy, handshake)
210                .map(|clients| clients.native.clone())
211                .map_err(|_| ExecError::local(502, FailureScope::Transport, "upstream request failed")),
212        }
213    }
214
215    pub async fn execute(
216        &self,
217        credential: &Credential,
218        req: ExecRequest,
219        cfg: &Config,
220    ) -> Result<ExecResponse, ExecError> {
221        self.execute_delegated(credential, req, cfg, Delegation::default())
222            .await
223    }
224
225    /// [`Self::execute`] on behalf of a delegating executor.
226    pub async fn execute_delegated(
227        &self,
228        credential: &Credential,
229        req: ExecRequest,
230        cfg: &Config,
231        delegation: Delegation,
232    ) -> Result<ExecResponse, ExecError> {
233        tracing::trace!(target: "cpa_latency", stage = "executor");
234        // Go executorForAuth: an API-key credential runs with cfg.ForAPIKey(), which
235        // zeroes the v8 OAuth-only provider settings.
236        let scoped = if cpa_core::registry::dynamic::auth_kind(credential) == Some("apikey") {
237            cfg.for_api_key()
238        } else {
239            std::borrow::Cow::Borrowed(cfg)
240        };
241        let cfg = &*scoped;
242        // Go never checks for a token here: an empty key counts locally, or reaches the
243        // upstream unauthenticated and gets its answer.
244        let mut req = req;
245        if req.operation == Operation::Generate {
246            if let Err(e) = crate::antigravity_compaction::expand_request(&mut req, "claude") {
247                // Go rejects the capsule before it creates the usage reporter.
248                req.usage.discard();
249                return Err(crate::gemini::status_err(400, e));
250            }
251            if req.alt.as_deref() == Some("responses/compact")
252                || crate::antigravity_compaction::has_trigger(&req.body)
253                || crate::antigravity_compaction::has_trigger(&req.original_body)
254            {
255                return Box::pin(self.compact(credential, req, cfg, delegation)).await;
256            }
257        }
258        // Both paths are boxed: a request future of about 10 KB inline in every caller's
259        // state (the dispatch loop, Home, the WebSocket session) overflowed a 2 MiB
260        // worker stack in debug builds once the summary turn added a second one.
261        Box::pin(self.run(credential, req, cfg, delegation, false)).await
262    }
263
264    /// `executeClaudeCompaction` / `executeClaudeCompactionStream`: a non-stream Claude
265    /// summary of the conversation, sealed into a compaction item.
266    async fn compact(
267        &self,
268        credential: &Credential,
269        mut req: ExecRequest,
270        cfg: &Config,
271        delegation: Delegation,
272    ) -> Result<ExecResponse, ExecError> {
273        let model = base_model(&req.model);
274        let stream = req.stream;
275        req.body = compaction::summary_payload(crate::antigravity_compaction::source_payload(
276            &req.body,
277            &req.original_body,
278        ))
279        .into();
280        req.original_body = Bytes::new();
281        req.alt = None;
282        req.stream = false;
283        req.source_format = Format::OpenAIResponse;
284        req.response_format = Format::Claude;
285        let answer = self.run(credential, req, cfg, delegation, true).await?;
286        let ResponseBody::Buffered(payload) = answer.body else {
287            return Err(crate::antigravity_compaction::statusless(
288                "extract summary: no summary text found in upstream response",
289            ));
290        };
291        let text = crate::antigravity_compaction::summary_text(&payload)
292            .map_err(|e| crate::antigravity_compaction::statusless(format!("extract summary: {e}")))?;
293        let sealed = crate::antigravity_compaction::seal(&text, &model)
294            .map_err(|e| crate::antigravity_compaction::statusless(format!("seal compaction capsule: {e}")))?;
295        let usage = compaction::Usage::of(&payload);
296        let mut headers = answer.headers;
297        if !stream {
298            return Ok(ExecResponse {
299                status: 200,
300                headers,
301                body: ResponseBody::Buffered(compaction::response(&model, &sealed, usage).into()),
302            });
303        }
304        headers.insert(
305            http::header::CONTENT_TYPE,
306            http::HeaderValue::from_static("text/event-stream"),
307        );
308        let chunks: Vec<Result<Bytes, ExecError>> = compaction::stream_chunks(&model, &sealed, usage)
309            .into_iter()
310            .map(|chunk| Ok(Bytes::from(chunk)))
311            .collect();
312        Ok(ExecResponse {
313            status: 200,
314            headers,
315            body: ResponseBody::Stream(futures_util::stream::iter(chunks).boxed()),
316        })
317    }
318
319    /// One Execute / ExecuteStream / CountTokens after compaction; `compaction_summary`
320    /// marks the summary turn of [`Self::compact`].
321    async fn run(
322        &self,
323        credential: &Credential,
324        req: ExecRequest,
325        cfg: &Config,
326        delegation: Delegation,
327        compaction_summary: bool,
328    ) -> Result<ExecResponse, ExecError> {
329        if req.response_format != Format::Claude && cpa_translate::pair(req.response_format, Format::Claude).is_none() {
330            return Err(ExecError::local(
331                501,
332                FailureScope::Request,
333                "Claude response translation pair is not registered",
334            ));
335        }
336        let mut ctx = Ctx::new(self, credential, &req, cfg, delegation);
337        ctx.compaction_summary = compaction_summary;
338        match req.operation {
339            Operation::Generate => self.generate(ctx, req).await,
340            Operation::CountTokens => self.count_tokens(ctx, req, delegation.count_upstream).await,
341        }
342    }
343
344    /// Execute / ExecuteStream around the compat thinking replay: restore before
345    /// translation; `generate_with` clears applied replay the upstream rejected.
346    async fn generate(&self, ctx: Ctx<'_>, mut req: ExecRequest) -> Result<ExecResponse, ExecError> {
347        ctx.tool_search_fallback(&mut req);
348        let attr = |k: &str| ctx.credential.attributes.get(k).map(String::as_str).unwrap_or_default();
349        let gate = replay::Gate {
350            credential: ctx.credential,
351            api_key: &ctx.api_key,
352            base_url: attr("base_url"),
353            base_model: &ctx.base_model,
354            is_compat: ctx.is_compat,
355            oauth_token: ctx.oauth_token,
356        };
357        let scope = replay::prepare(&self.replay, &gate, &mut req).await;
358        let result = self.generate_with(ctx, req, scope.as_ref()).await;
359        match (result, scope) {
360            // wrapClaudeThinkingReplayStream wraps the stream ExecuteStream returns,
361            // after translation; the response waits for the cache writes it caused.
362            (Ok(mut response), Some(scope)) => {
363                let writes = scope.writes.clone();
364                match response.body {
365                    ResponseBody::Stream(events) => {
366                        response.body = ResponseBody::Stream(writes.gate(scope.wrap(events)));
367                    }
368                    body => {
369                        response.body = body;
370                        writes.settle().await;
371                    }
372                }
373                Ok(response)
374            }
375            (Err(error), Some(scope)) => {
376                scope.writes.settle().await;
377                Err(error)
378            }
379            (result, None) => result,
380        }
381    }
382
383    async fn generate_with(
384        &self,
385        ctx: Ctx<'_>,
386        req: ExecRequest,
387        replay: Option<&replay::Scope>,
388    ) -> Result<ExecResponse, ExecError> {
389        let upstream_stream = req.stream || req.response_format != Format::Claude;
390        // reporter.SetUpstreamModel when the delegation renames the model.
391        if ctx.upstream_model != ctx.base_model {
392            req.usage.upstream_model(&ctx.upstream_model);
393        }
394        let tool_search = ctx.tool_search();
395        let translated = translate::request(&req, ctx.codex, &ctx.base_model, ctx.is_compat, tool_search)?;
396        let original_translated = translate::original(
397            &req,
398            &translated,
399            ctx.codex,
400            &ctx.base_model,
401            ctx.is_compat,
402            ctx.tool_search(),
403        )?;
404        tracing::trace!(target: "cpa_latency", stage = "translated");
405        let mut prepared = ctx
406            .prepare_messages(&req, &translated, &original_translated, upstream_stream)
407            .await?;
408        drop(original_translated);
409        // Only a translated client needs the translation again, for its response; a
410        // Claude client's copy would otherwise wait out the upload and the upstream's
411        // time to first byte.
412        let translated = if req.response_format == Format::Claude {
413            drop(translated);
414            Bytes::new()
415        } else {
416            translated
417        };
418        // reporter.SetTranslatedReasoningEffort on the body sent upstream.
419        if req.usage.enabled() {
420            req.usage.request(Format::Claude, prepared.body.as_bytes());
421        }
422        if req.usage.wants_claude_cache()
423            && let Some(telemetry) = telemetry::collect(&ctx, &req, &prepared)
424        {
425            req.usage.claude_cache(telemetry);
426        }
427        let response = self
428            .send(&ctx, &mut prepared, "/v1/messages", Some(&req.usage), req.capture())
429            .await;
430        let response = response.inspect_err(|error| {
431            // shouldClearKimiThinkingReplayAfterError: an upstream rejection of applied replay.
432            if let Some(scope) = replay.filter(|s| s.applied)
433                && replay::upstream_rejects(error)
434            {
435                scope.clear();
436            }
437        })?;
438        let reverse = prepared.reverse.clone();
439        let completion = ctx.completion(&mut prepared);
440        let request_id = header_value(&response.headers, "request-id");
441        let fast = ctx.first_party && prepared.fast;
442        let wrap = |e| fast_request_error(fast, e);
443        let body = match response.body {
444            ResponseBody::Stream(raw) if upstream_stream && req.stream && req.operation == Operation::Generate => {
445                let done: stream::OnComplete = Box::new(move |message_id| {
446                    completion.complete(&message_id, &request_id);
447                });
448                // ExecuteStream publishes through its stream buffer only (no
449                // EnsurePublished): a stream that never carried usage records nothing.
450                req.usage.usage_required();
451                let (usage, capture) = (req.usage.clone(), req.capture().clone());
452                let relayed = if req.response_format == Format::Claude {
453                    stream::relay(raw, reverse, done, usage, capture)
454                } else {
455                    stream::relay_translated(raw, reverse, done, usage, capture)
456                };
457                ResponseBody::Stream(relayed.map(move |r| r.map_err(|e| fast_request_error(fast, e))).boxed())
458            }
459            ResponseBody::Stream(raw) => {
460                let capture = req.capture();
461                let data = collect(raw).await.map_err(|e| {
462                    capture_error(capture, &e);
463                    wrap(e)
464                })?;
465                capture.record(CaptureEvent::ResponseChunk(&data));
466                if upstream_stream {
467                    // Translated Execute publishes with streamUsage.Publish only: no usage
468                    // seen, no record. A restore failure below keeps the usage seen so far
469                    // (streamUsage.PublishFailure), which the server does for Claude.
470                    req.usage.usage_required();
471                    stream::validate_buffered(&data).map_err(|e| {
472                        capture_error(capture, &e);
473                        wrap(e)
474                    })?;
475                    let id = stream::buffered_message_id(&data);
476                    if !id.is_empty() {
477                        completion.complete(&id, &request_id);
478                    }
479                    let mut out = Vec::with_capacity(data.len());
480                    for (i, line) in data.split(|b| *b == b'\n').enumerate() {
481                        if i > 0 {
482                            out.push(b'\n');
483                        }
484                        // ObserveResponseModel and ObserveClaudeStream, before restore.
485                        req.usage.response_line(Format::Claude, line);
486                        out.extend(stream::restore_line(line, &reverse).map_err(|m| {
487                            let error =
488                                plain_error(format!("restore Claude OAuth tool name from streaming response: {m}"));
489                            capture_error(capture, &error);
490                            wrap(error)
491                        })?);
492                    }
493                    if let Some(scope) = replay {
494                        scope.store_response(&out);
495                    }
496                    ResponseBody::Buffered(Bytes::from(out))
497                } else {
498                    // ObserveResponseModel and ParseClaudeUsage of the upstream body.
499                    req.usage.response_body(Format::Claude, &data);
500                    let text = String::from_utf8_lossy(&data).into_owned();
501                    let id = rawjson::string(&text, "id").trim().to_owned();
502                    completion.complete(&id, &request_id);
503                    // A failed restore returns before ParseClaudeUsage: Go's deferred
504                    // TrackFailure publishes no tokens. The error is plain (wrapped for a
505                    // fast request with the 2xx status), so Go records no status.
506                    let restored = alias::restore_response(&text, &reverse).map_err(|m| {
507                        let message = format!("restore Claude OAuth tool name from response: {m}");
508                        req.usage.publish_failure(0, &message);
509                        capture.record(CaptureEvent::ResponseError(&message));
510                        wrap(plain_error(message))
511                    })?;
512                    if let Some(scope) = replay {
513                        scope.store_response(restored.as_bytes());
514                    }
515                    // reporter.Publish(ParseClaudeUsage(data)): native Execute always
516                    // publishes, with whatever usage the body carried.
517                    req.usage.publish();
518                    ResponseBody::Buffered(Bytes::from(restored))
519                }
520            }
521            ResponseBody::Buffered(_) => unreachable!("send returns raw streams"),
522        };
523        let response = ExecResponse {
524            status: response.status,
525            headers: response.headers,
526            body,
527        };
528        if req.response_format == Format::Claude {
529            return Ok(response);
530        }
531        // Translated clients: framed events through the registered stream translator.
532        let response = match response.body {
533            ResponseBody::Buffered(_) => response,
534            ResponseBody::Stream(events) => ExecResponse {
535                body: ResponseBody::Stream(events),
536                ..response
537            },
538        };
539        translate::response(req, translated, response, tool_search).await
540    }
541
542    async fn count_tokens(
543        &self,
544        ctx: Ctx<'_>,
545        mut req: ExecRequest,
546        upstream: bool,
547    ) -> Result<ExecResponse, ExecError> {
548        ctx.tool_search_fallback(&mut req);
549        let translated = translate::request(&req, ctx.codex, &ctx.base_model, ctx.is_compat, ctx.tool_search())?;
550        // sdktranslator.TranslateTokenCount(to=claude, responseFormat, count, raw).
551        let render = |raw: &[u8]| {
552            let count = rawjson::get(&String::from_utf8_lossy(raw), "input_tokens").i64();
553            Bytes::from(cpa_translate::translate_token_count(
554                req.response_format,
555                Format::Claude,
556                count,
557                raw,
558            ))
559        };
560        let original = translate::count_original(
561            &req,
562            &translated,
563            ctx.codex,
564            &ctx.base_model,
565            ctx.is_compat,
566            ctx.tool_search(),
567        )?;
568        if !upstream && (ctx.api_key.trim().is_empty() || !ctx.first_party) {
569            let mut body = ctx.apply_thinking(&req, String::from_utf8_lossy(&translated).into_owned())?;
570            if ctx.rebuild_mid_system() {
571                body = rebuild_mid_system(&body);
572            }
573            let body = sanitize_for_upstream(body, &ctx.base_model, ctx.is_compat);
574            let body = cloak::strip_prompt_cache_options(ctx.count_rules(&req, body, &original));
575            return Ok(ExecResponse {
576                status: 200,
577                headers: Default::default(),
578                body: ResponseBody::Buffered(render(&tokens::count(body.as_bytes())?)),
579            });
580        }
581        let mut prepared = ctx.prepare_count(&req, &translated, &original).await?;
582        let capture = req.capture();
583        let response = self
584            .send(&ctx, &mut prepared, "/v1/messages/count_tokens", None, capture)
585            .await?;
586        let body = match response.body {
587            ResponseBody::Stream(raw) => collect(raw).await.inspect_err(|e| capture_error(capture, e))?,
588            ResponseBody::Buffered(b) => b,
589        };
590        capture.record(CaptureEvent::ResponseChunk(&body));
591        Ok(ExecResponse {
592            status: response.status,
593            headers: response.headers,
594            body: ResponseBody::Buffered(render(&body)),
595        })
596    }
597
598    /// Sends one Messages or count_tokens request. `usage` receives Go's TTFT marks
599    /// (`TrackHTTPClient`): the round trip starts, then the first raw body byte arrives,
600    /// before decoding and for any status.
601    // ponytail: Go's http.Client drains up to 2 KiB of a redirect response's body
602    // through the tracked transport, so a redirect with a body marks the first byte at
603    // that hop; this marks it on the final response only.
604    async fn send(
605        &self,
606        ctx: &Ctx<'_>,
607        prepared: &mut Prepared,
608        path: &str,
609        usage: Option<&cpa_core::exec::UsageSink>,
610        capture: &CaptureSink,
611    ) -> Result<RawResponse, ExecError> {
612        let usage = usage.filter(|u| u.enabled());
613        let url = format!("{}{path}?beta=true", ctx.base_url);
614        let fast = ctx.first_party && prepared.fast;
615        // The body moves into the request; nothing reads it after the send.
616        let body = Bytes::from(std::mem::take(&mut prepared.body));
617        let prepared = &*prepared;
618        let mut headers = GoHeaders::new();
619        for (name, value) in &prepared.headers {
620            headers.add_raw(name, value.as_str());
621        }
622        // Go's fallbackRoundTripper, per hop: the native Claude Code transport and
623        // ordered writer for Anthropic, http.DefaultTransport (or the proxy transport)
624        // for every other origin, behind one redirect-following http.Client.
625        let route = |hop: &url::Url| {
626            Ok(if tokens::first_party(hop.as_str()) {
627                Route {
628                    client: self.native_client(&ctx.proxy, ctx.handshake)?,
629                    order: Some(prepared.order.clone()),
630                }
631            } else {
632                Route {
633                    client: self.go.get(&ctx.proxy),
634                    order: None,
635                }
636            })
637        };
638        // helps.RecordAPIRequest: the header map the executor built (Go spelling, the
639        // transport's own headers excluded), unmasked; every physical send. The log may
640        // keep the body for the whole exchange, so it gets the body being sent rather
641        // than a copy.
642        if capture.enabled() {
643            let (auth_type, auth_value) = cpa_core::registry::dynamic::account_info(ctx.credential);
644            capture.record_request(
645                UpstreamRequest {
646                    url: &url,
647                    method: "POST",
648                    headers: &prepared.headers,
649                    body: &body,
650                    provider: ctx.log_provider,
651                    auth_id: &ctx.credential.id,
652                    auth_label: &ctx.credential.label,
653                    auth_type,
654                    auth_value: &auth_value,
655                },
656                &body,
657            );
658        }
659        if let Some(usage) = usage {
660            usage.round_trip_started();
661        }
662        tracing::trace!(target: "cpa_latency", stage = "prepared");
663        let mut upstream = crate::proxy::send_routed(&route, &url, headers, body, None, usage)
664            .await
665            .map_err(|e| {
666                capture_error(capture, &e);
667                fast_request_error(fast, e)
668            })?;
669        if capture.enabled() {
670            capture.record(CaptureEvent::ResponseMetadata(
671                upstream.status,
672                &go_headers(&upstream.headers),
673            ));
674        }
675        if let Some(usage) = usage.cloned() {
676            let mut marked = false;
677            upstream.body = upstream
678                .body
679                .inspect(move |chunk| {
680                    if !marked && chunk.as_ref().is_ok_and(|c| !c.is_empty()) {
681                        marked = true;
682                        usage.first_byte();
683                    }
684                })
685                .boxed();
686        }
687        // MarkResult observes the headers of every upstream answer to a Messages request
688        // from a Claude credential, whatever its status or body (Go records them before
689        // reading the body); count_tokens results skip observation in Go's conductor.
690        // ponytail: Go keys the model state by the conductor's state model; the executor
691        // sees the upstream base model, as Codex's observation does.
692        if path == "/v1/messages" && ctx.credential.provider.trim().eq_ignore_ascii_case("claude") {
693            self.quota
694                .observe_model(&ctx.credential.id, &ctx.base_model, &upstream.headers);
695        }
696        finish(
697            decode_upstream(upstream).await,
698            fast,
699            ctx.settings.model_level_cooling,
700            capture,
701        )
702        .await
703    }
704}
705
706/// `httpResp.Header.Clone()` for request logs: Go's canonical names, every value.
707fn go_headers(headers: &http::HeaderMap) -> Vec<(String, String)> {
708    headers
709        .iter()
710        .map(|(name, value)| {
711            (
712                crate::proxy::canonical_header(name.as_str()),
713                String::from_utf8_lossy(value.as_bytes()).into_owned(),
714            )
715        })
716        .collect()
717}
718
719/// `helps.RecordAPIResponseError` with the error's text.
720fn capture_error(capture: &CaptureSink, error: &ExecError) {
721    if capture.enabled() {
722        capture.record(CaptureEvent::ResponseError(&String::from_utf8_lossy(&error.body)));
723    }
724}
725
726/// The response half of Go's Execute, ExecuteStream and countTokensUpstream: the
727/// decoded 2xx body, or the error Go builds from a non-2xx answer. An undecodable or
728/// unreadable error body keeps the upstream status and says why.
729/// Request logs record a decode failure, an unreadable error body, and the error body
730/// itself, at Go's sites.
731async fn finish(
732    decoded: Decoded,
733    fast: bool,
734    model_level_cooling: bool,
735    capture: &CaptureSink,
736) -> Result<RawResponse, ExecError> {
737    let Decoded { status, headers, body } = decoded;
738    if (200..300).contains(&status) {
739        let body = body.map_err(|m| {
740            capture.record(CaptureEvent::ResponseError(&m));
741            fast_request_error(fast, plain_error(m))
742        })?;
743        return Ok(RawResponse {
744            status,
745            headers,
746            body: ResponseBody::Stream(body),
747        });
748    }
749    let data = match body {
750        Err(m) => {
751            capture.record(CaptureEvent::ResponseError(&m));
752            let message = format!("failed to decode error response body: {m}");
753            let error = upstream_error(status, headers, Bytes::from(message), false, model_level_cooling);
754            return Err(fast_request_error(fast, error));
755        }
756        Ok(mut body) => {
757            // ponytail: Go reads error bodies without a bound; 16 MiB covers any real
758            // provider error without letting a hostile upstream exhaust a small VPS.
759            let mut data = BytesMut::new();
760            loop {
761                match body.next().await {
762                    Some(Ok(chunk)) if data.len() < crate::proxy::MAX_ERROR_BODY => data.extend_from_slice(&chunk),
763                    Some(Ok(_)) | None => break data.freeze(),
764                    Some(Err(e)) => {
765                        let reason = if e.scope == FailureScope::Transport {
766                            "unexpected EOF".into()
767                        } else {
768                            String::from_utf8_lossy(&e.body).into_owned()
769                        };
770                        capture.record(CaptureEvent::ResponseError(&reason));
771                        break Bytes::from(format!("failed to read error response body: {reason}"));
772                    }
773                }
774            }
775        }
776    };
777    capture.record(CaptureEvent::ResponseChunk(&data));
778    Err(upstream_error(status, headers, data, fast, model_level_cooling))
779}
780
781/// What a completed Messages response records: billing continuity
782/// (`commitClaudeContinuity`) and, for a thread turn, the tool aliases a continuation
783/// of it will need (`rememberClaudeOAuthToolAliases`).
784struct Completion {
785    continuity: session::Continuity,
786    thread: Option<(thread::Thread, Arc<alias::Reverse>, Arc<thread::Aliases>)>,
787}
788
789impl Completion {
790    fn complete(&self, message_id: &str, request_id: &str) {
791        let c = &self.continuity;
792        session::commit(&c.key, c.sequence, message_id, request_id, &c.prompt_id);
793        if let Some((thread, aliases, store)) = &self.thread {
794            store.remember(thread, aliases, message_id);
795        }
796    }
797}
798
799struct RawResponse {
800    status: u16,
801    headers: http::HeaderMap,
802    body: ResponseBody,
803}
804
805fn header_value(headers: &http::HeaderMap, name: &str) -> String {
806    detect::header(headers, name).to_owned()
807}
808
809async fn collect(mut raw: cpa_core::exec::ExecStream) -> Result<Bytes, ExecError> {
810    let mut out = BytesMut::new();
811    while let Some(chunk) = raw.next().await {
812        out.extend_from_slice(&chunk?);
813    }
814    Ok(out.freeze())
815}
816
817/// A non-2xx upstream answer. `headers` are already decoded (no Content-Encoding or
818/// Content-Length). Fast requests to Anthropic answer the client as sent
819/// (`newClaudeFastDirectResponseError`); everything else is classified
820/// (`classifyClaudeUpstreamErrorWithCooling`).
821fn upstream_error(
822    status: u16,
823    headers: http::HeaderMap,
824    body: Bytes,
825    fast: bool,
826    model_level_cooling: bool,
827) -> ExecError {
828    if fast {
829        // Only a genuine shared-window 429 leaves the request's scope.
830        let rate_limited = status == 429;
831        return ExecError {
832            status,
833            scope: if rate_limited && quota::shared_rejection(&headers) {
834                FailureScope::Credential
835            } else {
836                FailureScope::Request
837            },
838            retry_after: rate_limited
839                .then(|| quota::rate_limit_reset_for_error(&headers, &body))
840                .flatten(),
841            headers: Box::new(headers),
842            body,
843            direct: true,
844            addon: None,
845        };
846    }
847    let error = ExecError {
848        status,
849        scope: crate::upstream::scope_for(status),
850        retry_after: None,
851        headers: Box::new(headers),
852        body: if body.is_empty() {
853            Bytes::from(format!("status {status}"))
854        } else {
855            body
856        },
857        direct: false,
858        addon: None,
859    };
860    quota::classify(error, model_level_cooling)
861}
862
863/// A Go error with no status or scope (`fmt.Errorf`): the handler answers 500, and the
864/// conductor neither cools the credential nor treats the request as unservable.
865pub(crate) fn plain_error(message: impl Into<String>) -> ExecError {
866    ExecError::local(500, FailureScope::Transport, message)
867}
868
869/// `wrapClaudeFastRequestError`: any other failure of a fast request stops at the
870/// caller instead of failing over. Only a cause that is explicitly credential-scoped
871/// (a classified shared-window 429) keeps that scope; status-derived scope does not.
872fn fast_request_error(fast: bool, mut error: ExecError) -> ExecError {
873    if fast {
874        let shared_window =
875            error.scope == FailureScope::Credential && error.status == 429 && quota::shared_rejection(&error.headers);
876        if !shared_window {
877            error.scope = FailureScope::Request;
878        }
879    }
880    error
881}
882
883/// Request facts shared by every stage.
884struct Ctx<'a> {
885    credential: &'a Credential,
886    settings: Settings,
887    api_key: String,
888    base_url: String,
889    first_party: bool,
890    proxy: Proxy,
891    /// `requests.tls-handshake-timeout-seconds` for the native transport.
892    handshake: Option<std::time::Duration>,
893    base_model: String,
894    /// The model sent upstream (`upstreamModel`): the base model unless a delegating
895    /// executor normalizes it.
896    upstream_model: String,
897    /// `isKimiMessagesUpstream`.
898    kimi: bool,
899    /// `upstreamRequestLogProvider`: the provider request logs name.
900    log_provider: &'static str,
901    /// The summary turn of a Responses compaction (`claudeCompactionSummaryFromContext`).
902    compaction_summary: bool,
903    /// Normalized execution-session ID (websocket executions), or empty.
904    execution: String,
905    /// `cliproxyauth.ResolvedModelInfo`: the capabilities dispatch bound to this attempt.
906    resolved: Option<cpa_common::thinking::ModelCaps>,
907    /// `helps.APIKeyModelIsCompat`.
908    is_compat: bool,
909    /// The Codex client rewrite settings of this config snapshot (Go `e.cfg`).
910    codex: cpa_common::codex_client::Settings,
911    /// `requests.payload` rules of this config snapshot (parsed once per snapshot).
912    payload_rules: std::sync::Arc<cpa_common::payload::Rules>,
913    /// Real Claude OAuth token (`sk-ant-oat`).
914    oauth_token: bool,
915    /// `fp.ProfileClaudeCodeCLI`: OAuth token or `fingerprint-profile: claude-code-cli`.
916    cli_profile: bool,
917    /// Bearer vs x-api-key on first-party (`claudeCredentialUsesOAuth`).
918    bearer: bool,
919    /// Local date for the currentDate reminder, unless the session pinned one.
920    // ponytail: only UTC and the process zone are resolvable without a tz database;
921    // other IANA names fall back to local time, as Go does for unknown zones.
922    today: String,
923    /// The thread continuation aliases of the executor serving the request.
924    thread_aliases: Arc<thread::Aliases>,
925}
926
927struct Prepared {
928    body: String,
929    headers: Vec<(String, String)>,
930    order: Vec<String>,
931    /// The tool aliases of the request, shared with the response relay and, for a
932    /// thread turn, with the thread alias store.
933    reverse: Arc<alias::Reverse>,
934    continuity: session::Continuity,
935    /// Set for a thread turn whose aliases its continuation may need.
936    thread: Option<thread::Thread>,
937    fast: bool,
938}
939
940/// `config.NormalizeClaudeFingerprintProfile`: unknown values are the default ("").
941fn normalize_fingerprint_profile(raw: &str) -> &'static str {
942    match raw.trim().go_lower().as_str() {
943        "claude-code-cli" | "oauth-cli" => "claude-code-cli",
944        _ => "",
945    }
946}
947
948/// The executor keeps bodies as text; shared byte APIs never split UTF-8 they were given.
949/// `String::from_utf8_lossy` with the fast validator first: request bodies are
950/// almost always valid UTF-8, and the lossy decoder's own check is several times slower.
951fn utf8(bytes: &[u8]) -> std::borrow::Cow<'_, str> {
952    std::str::from_utf8(bytes).map_or_else(|_| String::from_utf8_lossy(bytes), std::borrow::Cow::Borrowed)
953}
954
955fn text(bytes: Vec<u8>) -> String {
956    String::from_utf8(bytes).unwrap_or_else(|e| String::from_utf8_lossy(e.as_bytes()).into_owned())
957}
958
959/// `helps.SetStringIfDifferent`.
960fn set_string_if_different(body: String, path: &str, value: &str) -> String {
961    let current = rawjson::get(&body, path);
962    if current.kind() == gjson::Kind::String && current.str() == value {
963        return body;
964    }
965    rawjson::set_str(&body, path, value)
966}
967
968/// `isKimiMessagesUpstream`: a Kimi credential, or Kimi's API host.
969fn kimi_upstream(provider: &str, base_url: &str) -> bool {
970    let provider = provider.trim().go_lower();
971    if matches!(provider.as_str(), "kimi" | "kimi-ai" | "kimi.ai" | "kimi.com") {
972        return true;
973    }
974    url::Url::parse(base_url.trim()).is_ok_and(|u| {
975        u.host_str()
976            .is_some_and(|h| h.eq_ignore_ascii_case("api.kimi.com") || h.eq_ignore_ascii_case("api.kimi.ai"))
977    })
978}
979
980/// `helps.ClaudeCLIAuthIdentitySeed`.
981// ponytail: Go falls back to the auth index and file name when the ID is empty; every
982// loaded or synthesized credential here has an ID.
983fn identity_seed(credential: &Credential) -> String {
984    let id = credential.id.trim();
985    if id.is_empty() {
986        String::new()
987    } else {
988        format!("auth-id|{id}")
989    }
990}
991
992/// `upstream.claude.tool-search` (Go `ClientToolSearchEnabled`), for translations outside
993/// this executor that Go reads it in.
994pub fn tool_search_enabled(cfg: &Config) -> bool {
995    Settings::from_config(cfg).tool_search
996}
997
998/// `ClaudeExecutor.SupportsNativeToolSearch` under `cfg` (the Codex catalog's
999/// per-credential check): whether a credential takes Codex tool_search natively for an
1000/// upstream model.
1001pub fn native_tool_search(cfg: &Config) -> impl Fn(&Credential, &str) -> bool + use<> {
1002    let settings = Settings::from_config(cfg);
1003    move |credential, upstream_model| supports_native_tool_search(&settings, credential, upstream_model)
1004}
1005
1006/// `supportsNativeToolSearch`: Anthropic's origin (or no base URL), else a key or
1007/// credential trusted with `native-tool-search`, for a model that has tool search, while
1008/// `upstream.claude.tool-search` is on.
1009fn supports_native_tool_search(settings: &Settings, credential: &Credential, upstream_model: &str) -> bool {
1010    if !settings.tool_search || tool_search::model_lacks(upstream_model) {
1011        return false;
1012    }
1013    let attr = |k: &str| credential.attributes.get(k).map(String::as_str).unwrap_or_default();
1014    let base = attr("base_url").trim();
1015    if base.is_empty() || tokens::first_party(base) || attr("native_tool_search").trim().go_eq_fold("true") {
1016        return true;
1017    }
1018    // claudeCreds: the configured key, else the OAuth access token.
1019    let api_key = match attr("api_key") {
1020        "" => credential.str("access_token").unwrap_or_default(),
1021        key => key,
1022    };
1023    settings
1024        .key_for(api_key, attr("base_url"))
1025        .is_some_and(|k| k.native_tool_search)
1026}
1027
1028/// `thinking.ParseSuffix`: `model(level)` → `model`.
1029fn base_model(model: &str) -> String {
1030    cpa_common::thinking::parse_suffix(model).model_name
1031}
1032
1033/// `sanitizeClaudeMessagesForClaudeUpstreamWithDebug`: Claude-family targets (and
1034/// compat models, which keep empty thinking blocks) go through the shared Messages
1035/// signature sanitizer; then empty web_search domain lists are removed.
1036fn sanitize_for_upstream(mut body: String, base_model: &str, preserve_empty_thinking: bool) -> String {
1037    use cpa_common::signature::{Provider, provider_from_model_name, sanitize_claude_messages_for_claude_upstream};
1038    if (provider_from_model_name(base_model) == Provider::Claude || preserve_empty_thinking)
1039        && let Some(sanitized) =
1040            sanitize_claude_messages_for_claude_upstream(body.as_bytes(), base_model, preserve_empty_thinking).0
1041    {
1042        body = text(sanitized);
1043    }
1044    // sanitizeClaudeWebSearchDomains: one pass over the tools, then the deletes.
1045    let mut empty = Vec::new();
1046    for (t, tool) in rawjson::get(&body, "tools").items().iter().enumerate() {
1047        if !tool.get("type").str().starts_with("web_search_") {
1048            continue;
1049        }
1050        for field in ["allowed_domains", "blocked_domains"] {
1051            let v = tool.get(field);
1052            if v.kind() == gjson::Kind::Array && v.array().is_empty() {
1053                empty.push(format!("tools.{t}.{field}"));
1054            }
1055        }
1056    }
1057    for path in empty {
1058        body = rawjson::delete(&body, &path);
1059    }
1060    body
1061}
1062
1063/// `extractAndRemoveBetas`.
1064fn extract_betas(body: String) -> (Vec<String>, String) {
1065    let v = rawjson::get(&body, "betas");
1066    if !v.exists() {
1067        return (Vec::new(), body);
1068    }
1069    let betas = if v.kind() == gjson::Kind::Array {
1070        v.array()
1071            .iter()
1072            .map(|b| b.str().trim().to_owned())
1073            .filter(|b| !b.is_empty())
1074            .collect()
1075    } else {
1076        Some(v.str().trim().to_owned())
1077            .filter(|b| !b.is_empty())
1078            .into_iter()
1079            .collect()
1080    };
1081    (betas, rawjson::delete(&body, "betas"))
1082}
1083
1084impl<'a> Ctx<'a> {
1085    fn new(
1086        exec: &ClaudeExecutor,
1087        credential: &'a Credential,
1088        req: &ExecRequest,
1089        cfg: &Config,
1090        delegation: Delegation,
1091    ) -> Self {
1092        let settings = Settings::from_config(cfg);
1093        let attr = |k: &str| credential.attributes.get(k).map(String::as_str).unwrap_or_default();
1094        let api_key = if attr("api_key").is_empty() {
1095            credential.str("access_token").unwrap_or_default().to_owned()
1096        } else {
1097            attr("api_key").to_owned()
1098        };
1099        let base_url = if attr("base_url").is_empty() {
1100            exec.base_url.clone()
1101        } else {
1102            attr("base_url").trim_end_matches('/').to_owned()
1103        };
1104        let oauth_token = api_key.contains("sk-ant-oat");
1105        // claudeFingerprintProfileFromConfig: the credential's own value, else the key's.
1106        let own = Some(attr("fingerprint_profile"))
1107            .filter(|s| !s.trim().is_empty())
1108            .or_else(|| {
1109                ["fingerprint_profile", "fingerprint-profile"]
1110                    .iter()
1111                    .filter_map(|k| credential.str(k))
1112                    .find(|s| !s.trim().is_empty())
1113            })
1114            .map(normalize_fingerprint_profile)
1115            .unwrap_or_default();
1116        let profile = if own.is_empty() {
1117            settings
1118                .key_for(&api_key, attr("base_url"))
1119                .map(|k| normalize_fingerprint_profile(&k.fingerprint_profile))
1120                .unwrap_or_default()
1121        } else {
1122            own
1123        };
1124        let api_key_kind = attr("auth_kind") == "apikey";
1125        let bearer = oauth_token || (!api_key_kind && attr("api_key").trim().is_empty());
1126        let base = base_model(&req.model);
1127        // Go `cliproxyauth.ResolvedModelInfo(req)`, bound by dispatch per attempt.
1128        let resolved = req
1129            .resolved_model
1130            .as_ref()
1131            .map(|r| cpa_common::thinking::ModelCaps::from(&r.info));
1132        Self {
1133            credential,
1134            first_party: tokens::first_party(&base_url),
1135            proxy: Proxy::effective(credential, cfg),
1136            handshake: crate::tls::handshake_timeout(cfg),
1137            upstream_model: delegation.upstream_model.map_or_else(|| base.clone(), |f| f(&base)),
1138            kimi: kimi_upstream(&credential.provider, &base_url),
1139            log_provider: delegation.request_log_provider.unwrap_or("claude"),
1140            compaction_summary: false,
1141            execution: session::normalize(req.execution_session.as_deref().unwrap_or_default()),
1142            is_compat: req.resolved_model.as_ref().is_some_and(|r| r.is_compat()),
1143            codex: cpa_common::codex_client::Settings::from_config(cfg),
1144            payload_rules: cpa_common::payload::Rules::of(cfg),
1145            resolved,
1146            base_model: base,
1147            cli_profile: oauth_token || profile == "claude-code-cli",
1148            oauth_token,
1149            bearer,
1150            settings,
1151            api_key,
1152            base_url,
1153            today: String::new(),
1154            thread_aliases: if delegation.request_log_provider.is_some() {
1155                exec.delegated_thread_aliases.clone()
1156            } else {
1157                exec.thread_aliases.clone()
1158            },
1159        }
1160        .with_today(credential, chrono::Utc::now())
1161    }
1162
1163    /// What a completed response of `prepared` records.
1164    fn completion(&self, prepared: &mut Prepared) -> Completion {
1165        Completion {
1166            continuity: prepared.continuity.clone(),
1167            thread: prepared
1168                .thread
1169                .take()
1170                .map(|thread| (thread, prepared.reverse.clone(), self.thread_aliases.clone())),
1171        }
1172    }
1173
1174    /// `claudeCodeTimezone`: credential `timezone`, then header defaults, else local.
1175    fn with_today(mut self, credential: &Credential, now: chrono::DateTime<chrono::Utc>) -> Self {
1176        // Go's tests swap the clock (`claudeCodeCurrentTimeFunc`); so do these.
1177        #[cfg(test)]
1178        let now = tests::clock().unwrap_or(now);
1179        let own = credential
1180            .attributes
1181            .get("timezone")
1182            .map(|s| s.trim().to_owned())
1183            .filter(|s| !s.is_empty());
1184        let zone = own
1185            .or_else(|| {
1186                credential
1187                    .str("timezone")
1188                    .map(|s| s.trim().to_owned())
1189                    .filter(|s| !s.is_empty())
1190            })
1191            .unwrap_or_else(|| self.settings.header_defaults.timezone.trim().to_owned());
1192        self.today = if matches!(zone.as_str(), "UTC" | "Etc/UTC") {
1193            now.format("%Y-%m-%d").to_string()
1194        } else {
1195            now.with_timezone(&chrono::Local).format("%Y-%m-%d").to_string()
1196        };
1197        #[cfg(test)]
1198        if let Some(today) = tests::pinned_today() {
1199            self.today = today;
1200        }
1201        self
1202    }
1203
1204    /// `helps.ApplyRequestThinking` with provider `claude`.
1205    fn apply_thinking(&self, req: &ExecRequest, body: String) -> Result<String, ExecError> {
1206        use cpa_common::thinking::{RequestThinking, apply_request_thinking};
1207        apply_request_thinking(&RequestThinking {
1208            body: body.as_bytes(),
1209            payload: &req.body,
1210            original: &req.original_body,
1211            model: &req.model,
1212            from: req.source_format.as_str(),
1213            to: Format::Claude.as_str(),
1214            provider: "claude",
1215            resolved: self.resolved.as_ref().map(Some),
1216            has_request_transformer: cpa_translate::pair(req.source_format, Format::Claude).is_some(),
1217            updates_changed: false,
1218        })
1219        .map(text)
1220        .map_err(|e| {
1221            if e.code.is_some() {
1222                ExecError::local(e.status(), FailureScope::Request, e.message)
1223            } else {
1224                plain_error(e.message)
1225            }
1226        })
1227    }
1228
1229    /// `resolveClaudeWirePolicy`: (cloak, strict, sensitive words, cache user id).
1230    fn wire_policy(&self, confirmed: bool) -> (bool, bool, Vec<String>, bool) {
1231        let attr = |k: &str| {
1232            let a = self.credential.attributes.get(k).map(|s| s.trim()).unwrap_or_default();
1233            if a.is_empty() {
1234                self.credential.str(k).unwrap_or_default().trim().to_owned()
1235            } else {
1236                a.to_owned()
1237            }
1238        };
1239        let attr_mode = attr("cloak_mode");
1240        let attr_strict = attr("cloak_strict_mode").eq_ignore_ascii_case("true");
1241        let attr_words: Vec<String> = Some(attr("cloak_sensitive_words"))
1242            .filter(|s| !s.is_empty())
1243            .map(|s| s.split(',').map(|w| w.trim().to_owned()).collect())
1244            .unwrap_or_default();
1245        let attr_cache = attr("cloak_cache_user_id").eq_ignore_ascii_case("true");
1246        let mut mode = if self.settings.disable_cloak_mode {
1247            "never".to_owned()
1248        } else {
1249            "auto".to_owned()
1250        };
1251        let (mut strict, mut words, mut cache) = (attr_strict, attr_words.clone(), attr_cache);
1252        if !attr_mode.is_empty() {
1253            mode = attr_mode.clone();
1254        }
1255        let base = self
1256            .credential
1257            .attributes
1258            .get("base_url")
1259            .map(String::as_str)
1260            .unwrap_or_default();
1261        let key_cloak = self.settings.key_for(&self.api_key, base).and_then(|k| k.cloak.clone());
1262        if let Some(c) = &key_cloak {
1263            if !c.mode.trim().is_empty() {
1264                mode = c.mode.trim().to_owned();
1265            }
1266            strict |= c.strict_mode;
1267            if !c.sensitive_words.is_empty() {
1268                words = c.sensitive_words.clone();
1269            }
1270            if let Some(v) = c.cache_user_id {
1271                cache = v;
1272            }
1273        }
1274        let configured =
1275            key_cloak.is_some() || !attr_mode.is_empty() || attr_strict || !attr_words.is_empty() || attr_cache;
1276        let mut cloak = (self.cli_profile || configured) && !confirmed;
1277        if !confirmed {
1278            match mode.trim().go_lower().as_str() {
1279                "always" => cloak = true,
1280                "never" => cloak = false,
1281                _ => {}
1282            }
1283        }
1284        (cloak, strict, words, cache)
1285    }
1286
1287    /// Go `derived_session_id` metadata, as the server's `session.Enrich` set it.
1288    fn derived_session(&self, req: &ExecRequest) -> String {
1289        req.derived_session.clone().unwrap_or_default()
1290    }
1291
1292    // ponytail: every rewrite that changes the body still returns a new copy of it, so a
1293    // cloaked 1.9 MB request allocates 32 buffers of 256 KiB or more and peaks at 5.1x its
1294    // body (alloc.cloak.* in bench/budgets.txt). Recording the edits from one walk and
1295    // splicing them into one output, as signals.rs does for cache_control, is the way to
1296    // the 3x target.
1297    async fn prepare_messages(
1298        &self,
1299        req: &ExecRequest,
1300        translated: &[u8],
1301        original_translated: &[u8],
1302        upstream_stream: bool,
1303    ) -> Result<Prepared, ExecError> {
1304        let original = utf8(&req.original_body);
1305        let detection = detect::detect(&req.headers, &original, false, &self.settings);
1306        let confirmed = detection.confirmed;
1307        let derived = self.derived_session(req);
1308        let translated = utf8(translated).into_owned();
1309        let session_id = if self.cli_profile {
1310            session::agent_session_uuid(
1311                &session::Inputs {
1312                    headers: &req.headers,
1313                    original: &original,
1314                    translated: &utf8(&req.body),
1315                    derived: &derived,
1316                    execution: &self.execution,
1317                },
1318                confirmed,
1319            )
1320        } else {
1321            String::new()
1322        };
1323        let mut body = translated;
1324        body = set_string_if_different(body, "model", &self.upstream_model);
1325        body = self.apply_thinking(req, body)?;
1326        if self.rebuild_mid_system() {
1327            body = rebuild_mid_system(&body);
1328        }
1329        let (cloak, strict, words, cache_user_id) = self.wire_policy(confirmed);
1330        let cch = signing::enabled(&self.api_key, self.cli_profile, self.first_party);
1331        // v8.0.27 (eb6a768): the caller owns every breakpoint in explicit cache mode.
1332        let explicit = cloak::explicit_cache_mode(&[&original, &utf8(&req.body), &body]);
1333        let probe_before = signals::probe_or_helper(&body);
1334        let mut continuity = session::Continuity::default();
1335        let mut cloaked = false;
1336        if cloak {
1337            if !strict && let Some(message) = cloak::invalid_system_block(&body) {
1338                return Err(ExecError::local(400, FailureScope::Request, message));
1339            }
1340            let mut subagent = false;
1341            let (mut prev, mut prompt) = (String::new(), String::new());
1342            if !probe_before {
1343                subagent = signals::subagent(&req.headers, &body);
1344                let (existing_prev, existing_prompt) = signals::billing_tags(&body);
1345                (prev, prompt, continuity) =
1346                    self.continuity_tags(&req.headers, &body, &session_id, &existing_prev, &existing_prompt);
1347            }
1348            let version = profile::default_version(&self.settings);
1349            let workload = detect::header(&req.headers, "x-cpa-claude-workload").to_owned();
1350            // 6d57ac9: a session keeps the date of its first request. Without
1351            // continuity (probes, helpers, no session) the date is today's.
1352            let date = session::pin_date(&continuity.key, &self.today);
1353            body = cloak::install_system(
1354                body,
1355                &cloak::SystemPlan {
1356                    strict,
1357                    billing: cloak::Billing {
1358                        signed: cch,
1359                        version: &version,
1360                        message: "",
1361                        entrypoint: "cli",
1362                        workload: &workload,
1363                        subagent,
1364                        prev_req: &prev,
1365                        prompt_id: &prompt,
1366                        human_turn: !probe_before && !subagent,
1367                    },
1368                    date: &date,
1369                    explicit,
1370                },
1371            );
1372            let model = rawjson::string(&body, "model").trim().go_lower();
1373            if cloak::is_opus55(&model) && !probe_before && !rawjson::get(&body, "fallbacks").exists() {
1374                body = rawjson::set_raw(&body, "fallbacks", r#"[{"model":"claude-opus-4-8"}]"#);
1375            }
1376            if cloak::is_fable51(&model) && !probe_before && !rawjson::get(&body, "fallbacks").exists() {
1377                body = rawjson::set_raw(&body, "fallbacks", r#"[{"model":"claude-opus-5"}]"#);
1378            }
1379            if !probe_before {
1380                body = cloak_thinking_display(body);
1381            }
1382            if !explicit && (probe_before || (subagent && !signals::subagent_requests_1h(&req.headers, &body))) {
1383                body = cloak::strip_ttl(body);
1384            }
1385            if !self.cli_profile && needs_fake_user_id(&body) {
1386                // Go `injectFakeUserID`: Home KV in Home mode, and its errors fail the
1387                // request.
1388                let user_id = if cache_user_id {
1389                    session::cached_user_id_required(&self.api_key).await
1390                } else {
1391                    session::cached_session_id_required(&self.api_key)
1392                        .await
1393                        .map(|session| cloak::fake_user_id(&session))
1394                };
1395                let user_id = user_id.map_err(|e| ExecError::local(500, FailureScope::Transport, e))?;
1396                body = rawjson::set_str(&body, "metadata.user_id", &user_id);
1397            }
1398            cloaked = true;
1399        }
1400        let probe = probe_before || signals::probe_or_helper(&body);
1401        let eligible = cloaked && self.first_party;
1402        let caller_owned_cm = rawjson::get(&body, "context_management").exists();
1403        let mut injected_cm = false;
1404        let mut diagnostics = session::Continuity::default();
1405        if continuity.initialized {
1406            diagnostics = continuity.clone();
1407        }
1408        if eligible {
1409            if let Some(updated) = cloak::inject_context_management(&body) {
1410                body = updated;
1411                injected_cm = true;
1412            }
1413            if self.cli_profile && !probe {
1414                if !continuity.initialized {
1415                    let begun = session::begin(&self.continuity_identity(), &session_id, false, "");
1416                    diagnostics = begun;
1417                    diagnostics.initialized = !diagnostics.key.is_empty();
1418                }
1419                if diagnostics.initialized || !diagnostics.key.is_empty() {
1420                    body = inject_diagnostics(&body, &diagnostics.previous_message_id);
1421                }
1422            }
1423        }
1424        body = self.ensure_max_tokens(body);
1425        body = disable_thinking_if_tool_choice_forced(body);
1426        body = cloak::reconcile_context_management(body, eligible, caller_owned_cm, injected_cm);
1427        body = normalize_sampling(body, confirmed);
1428        // `shouldEnsureCacheControl`.
1429        let cpa_owns_cache = !explicit && !confirmed && (cloaked || cloak::count_cache_controls(&body) == 0);
1430        if cpa_owns_cache {
1431            body = cloak::ensure_cache_control(body);
1432        }
1433        body = cloak::enforce_cache_limit(body, 4);
1434        body = cloak::strip_prompt_cache_options(body);
1435        let subagent = signals::subagent(&req.headers, &body);
1436        let subagent_1h = subagent && signals::subagent_requests_1h(&req.headers, &body);
1437        if cpa_owns_cache && self.cli_profile && (!subagent || subagent_1h) && !probe {
1438            body = cloak::upgrade_ttl(body, "1h");
1439        } else if (probe || (subagent && !subagent_1h)) && !explicit {
1440            body = cloak::strip_ttl(body);
1441        }
1442        if !explicit {
1443            body = cloak::normalize_ttl(body);
1444        }
1445        // Execute only: ExecuteStream forwards the body's own `stream` field.
1446        let stream_field = rawjson::get(&body, "stream");
1447        if !req.stream && (!detection.helper_profile || stream_field.exists() || upstream_stream) {
1448            let want = if upstream_stream { "true" } else { "false" };
1449            if stream_field.json() != want {
1450                body = rawjson::set_raw(&body, "stream", want);
1451            }
1452        }
1453        let thread = thread::Thread::of(&body);
1454        let mut reverse = Arc::default();
1455        if self.cli_profile && cloaked {
1456            // `prepareClaudeOAuthToolNamesForRequest`: a continuation without tools
1457            // reuses its thread's aliases and is sent as is.
1458            if let Some(restored) = thread
1459                .as_ref()
1460                .and_then(|thread| self.thread_aliases.for_continuation(thread, &body))
1461            {
1462                reverse = restored?;
1463            } else {
1464                let secret = Some(req.caller.principal.trim())
1465                    .filter(|s| !s.is_empty())
1466                    .unwrap_or(alias::DEFAULT_SECRET);
1467                let aliases;
1468                (body, aliases) = alias::remap(&body, secret);
1469                reverse = Arc::new(aliases);
1470            }
1471        }
1472        body = sanitize_for_upstream(body, &self.base_model, self.is_compat);
1473        if self.cli_profile {
1474            body = self.apply_identity(&body, &session_id)?;
1475        }
1476        if cloaked
1477            && !words.is_empty()
1478            && let Some(m) = cloak::SensitiveWords::new(&words)
1479        {
1480            body = m.obfuscate(&body);
1481        }
1482        if cch {
1483            let fallback = if !detection.helper_profile || rawjson::get(&body, "system").exists() {
1484                self.fallback_billing(&req.headers, &body, &detection.entrypoint, &continuity, confirmed)
1485            } else {
1486                String::new()
1487            };
1488            body = signing::placeholder(body, &fallback);
1489        }
1490        if self.kimi && !self.cli_profile {
1491            // stripDefaultKimiClaudeCodeAttribution: Kimi reads the block as prompt text.
1492            body = strip_attribution_system(&body);
1493        }
1494        if self.compaction_summary {
1495            body = compaction::finalize(body);
1496        }
1497        // 868e137: the payload rules match the fully prepared body and apply once. Only
1498        // header extraction, signing and read-only checks follow.
1499        let (edited, touched) = cpa_common::payload::apply_tracked(
1500            &self.payload_rules,
1501            &cpa_common::payload::Request {
1502                target_executor: "claude",
1503                model: &self.base_model,
1504                requested_model: &req.requested_model,
1505                protocol: Format::Claude.as_str(),
1506                from_protocol: req.source_format.as_str(),
1507                root: "",
1508                original: original_translated,
1509                request_path: &req.request_path,
1510                headers: Some(&req.headers),
1511            },
1512            body.into_bytes(),
1513            &["diagnostics"],
1514        );
1515        body = text(edited);
1516        if touched.contains("diagnostics") {
1517            diagnostics = session::Continuity::default();
1518        }
1519        let (extra_betas, stripped) = extract_betas(body);
1520        body = cloak::strip_prompt_cache_options(stripped);
1521        if cch {
1522            body = signing::sign(body)
1523                .map_err(|e| ExecError::local(500, FailureScope::Request, format!("sign Claude CCH: {e}")))?;
1524        }
1525        validate_mid_system(&body, confirmed, self.first_party)?;
1526        let fast = betas::uses_fast_mode(&body, &betas::requested("", &[]));
1527        let (headers, order) = self
1528            .headers(
1529                req,
1530                &body,
1531                &extra_betas,
1532                upstream_stream,
1533                false,
1534                confirmed && !cloaked,
1535                detection.helper_profile,
1536                &session_id,
1537                cloaked,
1538            )
1539            .await?;
1540        let fast = fast
1541            || headers.iter().any(|(k, v)| {
1542                k.eq_ignore_ascii_case("anthropic-beta") && v.split(',').any(|b| b.trim() == betas::FAST_MODE)
1543            });
1544        Ok(Prepared {
1545            body,
1546            headers,
1547            order,
1548            reverse,
1549            continuity: diagnostics,
1550            thread,
1551            fast,
1552        })
1553    }
1554
1555    /// The payload rules on a finished count_tokens body (868e137), against the client's
1556    /// original request translated as the working one ([`translate::count_original`]).
1557    fn count_rules(&self, req: &ExecRequest, body: String, original: &[u8]) -> String {
1558        let edited = cpa_common::payload::apply(
1559            &self.payload_rules,
1560            &cpa_common::payload::Request {
1561                target_executor: "claude",
1562                model: &self.base_model,
1563                requested_model: &req.requested_model,
1564                protocol: Format::Claude.as_str(),
1565                from_protocol: req.source_format.as_str(),
1566                root: "",
1567                original,
1568                request_path: &req.request_path,
1569                headers: Some(&req.headers),
1570            },
1571            body.into_bytes(),
1572        );
1573        text(edited)
1574    }
1575
1576    async fn prepare_count(
1577        &self,
1578        req: &ExecRequest,
1579        translated: &[u8],
1580        baseline: &[u8],
1581    ) -> Result<Prepared, ExecError> {
1582        let original = utf8(&req.original_body);
1583        let detection = detect::detect(&req.headers, &original, true, &self.settings);
1584        let confirmed = detection.confirmed;
1585        let session_id = if self.cli_profile {
1586            let derived = self.derived_session(req);
1587            session::agent_session_uuid(
1588                &session::Inputs {
1589                    headers: &req.headers,
1590                    original: &original,
1591                    translated: &utf8(&req.body),
1592                    derived: &derived,
1593                    execution: &self.execution,
1594                },
1595                confirmed,
1596            )
1597        } else {
1598            String::new()
1599        };
1600        let mut body = utf8(translated).into_owned();
1601        body = set_string_if_different(body, "model", &self.upstream_model);
1602        body = self.apply_thinking(req, body)?;
1603        if self.rebuild_mid_system() {
1604            body = rebuild_mid_system(&body);
1605        }
1606        let (cloak, strict, words, _) = self.wire_policy(confirmed);
1607        let explicit = cloak::explicit_cache_mode(&[&original, &utf8(&req.body), &body]);
1608        if cloak {
1609            if !strict && let Some(message) = cloak::invalid_system_block(&body) {
1610                return Err(ExecError::local(400, FailureScope::Request, message));
1611            }
1612            body = cloak::relocate_system_for_count(&body, strict, explicit);
1613            if !words.is_empty()
1614                && let Some(m) = cloak::SensitiveWords::new(&words)
1615            {
1616                body = m.obfuscate(&body);
1617            }
1618        }
1619        body = cloak::enforce_cache_limit(body, 4);
1620        if !explicit {
1621            body = cloak::normalize_ttl(body);
1622        }
1623        let (mut extra_betas, stripped) = extract_betas(body);
1624        body = stripped;
1625        extra_betas.push(betas::TOKEN_COUNTING.into());
1626        if self.cli_profile && cloak {
1627            let secret = Some(req.caller.principal.trim())
1628                .filter(|s| !s.is_empty())
1629                .unwrap_or(alias::DEFAULT_SECRET);
1630            body = alias::remap(&body, secret).0;
1631        }
1632        body = sanitize_for_upstream(body, &self.base_model, self.is_compat);
1633        body = cloak::strip_prompt_cache_options(body);
1634        if self.first_party || self.cli_profile {
1635            for field in ["metadata", "context_management", "diagnostics"] {
1636                body = rawjson::delete(&body, field);
1637            }
1638        }
1639        if self.cli_profile {
1640            body = strip_attribution_system(&body);
1641        }
1642        validate_mid_system(&body, confirmed, self.first_party)?;
1643        body = cloak::strip_prompt_cache_options(self.count_rules(req, body, baseline));
1644        let (headers, order) = self
1645            .headers(
1646                req,
1647                &body,
1648                &extra_betas,
1649                false,
1650                true,
1651                confirmed && !cloak,
1652                false,
1653                &session_id,
1654                cloak,
1655            )
1656            .await?;
1657        Ok(Prepared {
1658            body,
1659            headers,
1660            order,
1661            reverse: Default::default(),
1662            continuity: Default::default(),
1663            thread: None,
1664            fast: false,
1665        })
1666    }
1667
1668    #[allow(clippy::too_many_arguments)]
1669    async fn headers(
1670        &self,
1671        req: &ExecRequest,
1672        body: &str,
1673        extra_betas: &[String],
1674        stream: bool,
1675        count_tokens: bool,
1676        confirmed: bool,
1677        helper: bool,
1678        session_id: &str,
1679        cloak: bool,
1680    ) -> Result<(Vec<(String, String)>, Vec<String>), ExecError> {
1681        let cpa_session = cpa_common::session::cpa_session_id(req.session.as_deref()).unwrap_or_default();
1682        // Only a fingerprinted request replaces the caller's identity; an API key without
1683        // cloaking forwards the caller's own User-Agent, refused or not.
1684        if self.cli_profile || cloak {
1685            profile::note_refused(self.credential, &req.headers, &self.settings);
1686        }
1687        // Go: stabilizeDeviceProfile && confirmedClaudeCode, and the error (Home KV
1688        // unreachable in Home mode) fails the request before it is sent.
1689        let device_profile = if self.settings.header_defaults.stabilize_device_profile && confirmed {
1690            let profile = profile::resolve_required(&self.credential.id, &self.api_key, &req.headers, &self.settings)
1691                .await
1692                .map_err(|e| ExecError::local(500, FailureScope::Transport, e))?;
1693            Some(profile)
1694        } else {
1695            None
1696        };
1697        // Go `applyClaudeHeaders`, after the device profile and the passthrough
1698        // return: without a session, the key's cached session ID.
1699        let cached_session_id = if headers::needs_cached_session(self.cli_profile || cloak, confirmed, session_id) {
1700            session::cached_session_id_required(&self.api_key)
1701                .await
1702                .map_err(|e| ExecError::local(500, FailureScope::Transport, e))?
1703        } else {
1704            String::new()
1705        };
1706        let h = headers::build(&headers::Plan {
1707            api_key: &self.api_key,
1708            bearer: self.bearer,
1709            first_party: self.first_party,
1710            count_tokens,
1711            stream,
1712            extra_betas,
1713            body,
1714            incoming: &req.headers,
1715            confirmed,
1716            helper,
1717            cli_fingerprint: self.cli_profile || cloak,
1718            use_oauth_betas: self.cli_profile,
1719            session_id,
1720            settings: &self.settings,
1721            attributes: &self.credential.attributes,
1722            cpa_session: &cpa_session,
1723            device_profile,
1724            cached_session_id: &cached_session_id,
1725        });
1726        Ok(headers::wire(h, self.first_party, count_tokens))
1727    }
1728
1729    fn rebuild_mid_system(&self) -> bool {
1730        self.credential
1731            .attributes
1732            .get("rebuild_mid_system_message")
1733            .is_some_and(|v| v.trim().eq_ignore_ascii_case("true"))
1734            || self
1735                .settings
1736                .key_for(
1737                    &self.api_key,
1738                    self.credential
1739                        .attributes
1740                        .get("base_url")
1741                        .map(String::as_str)
1742                        .unwrap_or_default(),
1743                )
1744                .is_some_and(|k| k.rebuild_mid_system_message)
1745    }
1746
1747    /// `upstream.claude.tool-search` of this run's config snapshot: Go pins it in the
1748    /// request context, so the fallback and both translations agree across a reload.
1749    fn tool_search(&self) -> bool {
1750        self.settings.tool_search
1751    }
1752
1753    fn supports_native_tool_search(&self) -> bool {
1754        supports_native_tool_search(&self.settings, self.credential, &self.upstream_model)
1755    }
1756
1757    /// `applyNativeToolSearchFallback`: a Responses request with tool_search whose
1758    /// credential cannot use native tool search loses it before translation, so it
1759    /// translates as it did before tool_search support.
1760    fn tool_search_fallback(&self, req: &mut ExecRequest) {
1761        if req.source_format != Format::OpenAIResponse
1762            || !self.tool_search()
1763            || !(tool_search::has(&req.body) || tool_search::has(&req.original_body))
1764            || self.supports_native_tool_search()
1765        {
1766            return;
1767        }
1768        if let Some(body) = tool_search::stripped(&req.body) {
1769            req.body = body;
1770        }
1771        if let Some(body) = tool_search::stripped(&req.original_body) {
1772            req.original_body = body;
1773        }
1774        tracing::debug!(
1775            "claude executor: native tool search unavailable, removed tool_search from request (model={})",
1776            self.upstream_model
1777        );
1778    }
1779
1780    /// `claudeDiagnosticsCredentialIdentity`.
1781    fn continuity_identity(&self) -> String {
1782        if !self.credential.id.trim().is_empty() {
1783            return format!("id:{}", self.credential.id.trim());
1784        }
1785        let account = identity::account_uuid(self.credential);
1786        if !account.is_empty() {
1787            return format!("account:{account}");
1788        }
1789        String::new()
1790    }
1791
1792    /// `resolveClaudeContinuityTags`.
1793    fn continuity_tags(
1794        &self,
1795        headers: &http::HeaderMap,
1796        body: &str,
1797        session_id: &str,
1798        existing_prev: &str,
1799        existing_prompt: &str,
1800    ) -> (String, String, session::Continuity) {
1801        let session = if session_id.is_empty() {
1802            session::agent_session_uuid(
1803                &session::Inputs {
1804                    headers,
1805                    original: body,
1806                    translated: body,
1807                    derived: "",
1808                    execution: &self.execution,
1809                },
1810                false,
1811            )
1812        } else {
1813            session_id.to_owned()
1814        };
1815        // ClaudeRequestHasExecutionMetadata.
1816        let has_execution_metadata = !self.execution.is_empty();
1817        let new_turn = signals::new_prompt_turn(body);
1818        let begun = session::begin(&self.continuity_identity(), &session, new_turn, existing_prompt);
1819        if begun.key.is_empty() {
1820            return Default::default();
1821        }
1822        let prompt = if !existing_prompt.is_empty() {
1823            existing_prompt.to_owned()
1824        } else if !begun.previous_message_id.is_empty()
1825            && !begun.prompt_id.is_empty()
1826            && (has_execution_metadata || !new_turn)
1827        {
1828            begun.prompt_id.clone()
1829        } else if !has_execution_metadata {
1830            session::deterministic_prompt_id(&format!("cpa:prompt:{}", cloak::fingerprint_message(body)))
1831        } else {
1832            begun.prompt_id.clone()
1833        };
1834        let use_stored = has_execution_metadata || !existing_prev.is_empty();
1835        let prev = if use_stored && !begun.previous_request_id.is_empty() {
1836            begun.previous_request_id.clone()
1837        } else {
1838            existing_prev.to_owned()
1839        };
1840        let continuity = session::Continuity {
1841            key: begun.key,
1842            sequence: begun.sequence,
1843            previous_message_id: if use_stored {
1844                begun.previous_message_id
1845            } else {
1846                String::new()
1847            },
1848            previous_request_id: if use_stored {
1849                begun.previous_request_id
1850            } else {
1851                String::new()
1852            },
1853            prompt_id: prompt.clone(),
1854            initialized: true,
1855        };
1856        (prev, prompt, continuity)
1857    }
1858
1859    /// `claudeCCHFallbackBillingHeader`.
1860    fn fallback_billing(
1861        &self,
1862        headers: &http::HeaderMap,
1863        body: &str,
1864        entrypoint: &str,
1865        continuity: &session::Continuity,
1866        confirmed: bool,
1867    ) -> String {
1868        let probe = signals::probe_or_helper(body);
1869        let (mut prev, mut prompt) = signals::billing_tags(body);
1870        if !probe {
1871            if prev.is_empty() {
1872                prev = continuity.previous_request_id.clone();
1873            }
1874            if prompt.is_empty() {
1875                prompt = continuity.prompt_id.clone();
1876            }
1877        }
1878        let message = cloak::fingerprint_message(body);
1879        cloak::billing_header(&cloak::Billing {
1880            signed: true,
1881            // A forwarded newer release signs with its own version, as its User-Agent
1882            // says (docs/DIFFERENCES-FROM-GO.md).
1883            version: &profile::billing_version(detect::header(headers, "user-agent"), confirmed, &self.settings),
1884            message: &message,
1885            entrypoint,
1886            workload: detect::header(headers, "x-cpa-claude-workload"),
1887            subagent: signals::subagent(headers, body),
1888            prev_req: &prev,
1889            prompt_id: &prompt,
1890            human_turn: false,
1891        })
1892    }
1893
1894    fn apply_identity(&self, body: &str, session_id: &str) -> Result<String, ExecError> {
1895        let synthesize = self.cli_profile && !self.oauth_token;
1896        // Delegated providers seed from the stable auth identity, so a token rotation
1897        // does not rotate the device fingerprint.
1898        let seed = if self.kimi {
1899            identity_seed(self.credential)
1900        } else {
1901            self.api_key.clone()
1902        };
1903        let (device, account) = identity::wire_identity(self.credential, &seed, synthesize);
1904        // applyClaudeCLIIdentity wraps every ApplyClaudeCredentialMetadata error once more.
1905        identity::apply(body, &device, &account, session_id).map_err(|e| match e {
1906            identity::ApplyError::Plain(e) => plain_error(format!("apply Claude credential metadata: {e}")),
1907            identity::ApplyError::Request(e) => ExecError::local(
1908                400,
1909                FailureScope::Request,
1910                format!("apply Claude credential metadata: {e}"),
1911            ),
1912        })
1913    }
1914
1915    /// `ensureModelMaxTokens`: only a model some Claude credential registered gets a
1916    /// default `max_tokens`, its registered completion limit or 1024.
1917    fn ensure_max_tokens(&self, body: String) -> String {
1918        let model = self.base_model.trim();
1919        if !rawjson::valid(&body)
1920            || rawjson::get(&body, "max_tokens").exists()
1921            || !cpa_core::registry::model_providers(model)
1922                .iter()
1923                .any(|p| p.eq_ignore_ascii_case("claude"))
1924        {
1925            return body;
1926        }
1927        let max = cpa_core::registry::registered_model(model, Some("claude"))
1928            .and_then(|info| {
1929                info.raw
1930                    .get("max_completion_tokens")
1931                    .and_then(serde_json::Value::as_i64)
1932            })
1933            .filter(|n| *n > 0)
1934            .unwrap_or(DEFAULT_MAX_TOKENS);
1935        rawjson::set_raw(&body, "max_tokens", &max.to_string())
1936    }
1937}
1938
1939/// `defaultModelMaxTokens`.
1940const DEFAULT_MAX_TOKENS: i64 = 1024;
1941
1942/// Fill `thinking.display: updates` for progress-display models with active thinking.
1943fn cloak_thinking_display(body: String) -> String {
1944    if rawjson::get(&body, "thinking.display").exists() || !betas::progress_display(&rawjson::string(&body, "model")) {
1945        return body;
1946    }
1947    match rawjson::string(&body, "thinking.type").trim().go_lower().as_str() {
1948        "adaptive" | "enabled" => rawjson::set_str(&body, "thinking.display", "updates"),
1949        _ => body,
1950    }
1951}
1952
1953/// `injectFakeUserID`: a caller's valid Claude Code user ID is kept.
1954/// `injectFakeUserID`: a missing or invalid `metadata.user_id` is replaced.
1955fn needs_fake_user_id(body: &str) -> bool {
1956    let existing = rawjson::string(body, "metadata.user_id");
1957    !rawjson::get(body, "metadata").exists() || existing.is_empty() || !detect::valid_user_id(&existing)
1958}
1959
1960/// `injectClaudeDiagnosticsWithState`: after context_management when present.
1961fn inject_diagnostics(body: &str, previous_message_id: &str) -> String {
1962    let value = if previous_message_id.is_empty() {
1963        r#"{"previous_message_id":null}"#.to_owned()
1964    } else {
1965        format!(
1966            r#"{{"previous_message_id":{}}}"#,
1967            rawjson::js_string(previous_message_id)
1968        )
1969    };
1970    if rawjson::get(body, "diagnostics").exists() {
1971        return rawjson::set_raw(body, "diagnostics", &value);
1972    }
1973    let cm = rawjson::get(body, "context_management");
1974    if let Some(start) = rawjson::offset(body, &cm) {
1975        let end = start + cm.json().len();
1976        return format!("{},\"diagnostics\":{value}{}", &body[..end], &body[end..]);
1977    }
1978    rawjson::set_raw(body, "diagnostics", &value)
1979}
1980
1981/// `disableThinkingIfToolChoiceForced`.
1982fn disable_thinking_if_tool_choice_forced(body: String) -> String {
1983    if !matches!(rawjson::get(&body, "tool_choice.type").str(), "any" | "tool") {
1984        return body;
1985    }
1986    let mut body = rawjson::delete(&body, "thinking");
1987    body = rawjson::delete(&body, "output_config.effort");
1988    let oc = rawjson::get(&body, "output_config");
1989    let mut members = 0;
1990    oc.each(|_, _| {
1991        members += 1;
1992        true
1993    });
1994    if oc.kind() == gjson::Kind::Object && members == 0 {
1995        body = rawjson::delete(&body, "output_config");
1996    }
1997    body
1998}
1999
2000/// `normalizeClaudeSamplingForUpstream`.
2001fn normalize_sampling(mut body: String, native: bool) -> String {
2002    let thinking = matches!(
2003        rawjson::string(&body, "thinking.type").trim().go_lower().as_str(),
2004        "enabled" | "adaptive" | "auto"
2005    );
2006    if !native {
2007        body = rawjson::delete_owned(body, "temperature");
2008        body = rawjson::delete_owned(body, "top_p");
2009        if thinking {
2010            body = rawjson::delete_owned(body, "top_k");
2011        }
2012        return body;
2013    }
2014    if thinking {
2015        let t = rawjson::get(&body, "temperature");
2016        if t.exists() && t.f64() != 1.0 {
2017            body = rawjson::delete(&body, "temperature");
2018        }
2019        let p = rawjson::get(&body, "top_p");
2020        if p.exists() && p.f64() < 0.95 {
2021            body = rawjson::delete(&body, "top_p");
2022        }
2023        return rawjson::delete_owned(body, "top_k");
2024    }
2025    if rawjson::get(&body, "temperature").exists() && rawjson::get(&body, "top_p").exists() {
2026        body = rawjson::delete(&body, "top_p");
2027    }
2028    body
2029}
2030
2031fn has_mid_system(body: &str) -> bool {
2032    rawjson::get(body, "messages")
2033        .items()
2034        .iter()
2035        .any(|m| m.get("role").str().trim().go_eq_fold("system"))
2036}
2037
2038/// `validateClaudeMidSystemMessageModel`.
2039fn validate_mid_system(body: &str, confirmed: bool, first_party: bool) -> Result<(), ExecError> {
2040    if confirmed || !first_party || !cloak::legacy_system_reminder(body) || !has_mid_system(body) {
2041        return Ok(());
2042    }
2043    let model = rawjson::string(body, "model");
2044    let model = if model.is_empty() { "unknown".to_owned() } else { model };
2045    Err(ExecError::local(
2046        400,
2047        FailureScope::Request,
2048        format!(
2049            "invalid_request_error: role 'system' is not supported on this model. Model {model:?} predates mid-conversation system turns, so system instructions must stay in the top-level system field for it."
2050        ),
2051    ))
2052}
2053
2054/// `rebuildMidSystemMessagesToTopLevel`: fold role=system turns into `system`.
2055fn rebuild_mid_system(body: &str) -> String {
2056    let messages = rawjson::get(body, "messages");
2057    if messages.kind() != gjson::Kind::Array {
2058        return body.to_owned();
2059    }
2060    let mut moved = Vec::new();
2061    let mut kept = Vec::new();
2062    for m in messages.array() {
2063        if m.get("role").str().trim().go_eq_fold("system") {
2064            moved.extend(system_text_parts(&m.get("content")));
2065        } else {
2066            kept.push(m.json().to_owned());
2067        }
2068    }
2069    if moved.is_empty() {
2070        return body.to_owned();
2071    }
2072    let mut parts = system_text_parts(&rawjson::get(body, "system"));
2073    parts.extend(moved);
2074    let body = rawjson::set_raw(body, "system", &format!("[{}]", parts.join(",")));
2075    rawjson::set_raw(&body, "messages", &format!("[{}]", kept.join(",")))
2076}
2077
2078/// `claudeSystemTextParts`: strings become text blocks, text objects stay raw.
2079fn system_text_parts(content: &rawjson::Value<'_>) -> Vec<String> {
2080    let block = |text: &str| rawjson::set_str(r#"{"type":"text","text":""}"#, "text", text);
2081    match content.kind() {
2082        gjson::Kind::String if !content.str().trim().is_empty() => vec![block(content.str())],
2083        gjson::Kind::Array => content
2084            .array()
2085            .iter()
2086            .filter_map(|item| match item.kind() {
2087                gjson::Kind::String if !item.str().trim().is_empty() => Some(block(item.str())),
2088                gjson::Kind::Object
2089                    if item.get("type").str() == "text" && !item.get("text").str().trim().is_empty() =>
2090                {
2091                    Some(item.json().to_owned())
2092                }
2093                _ => None,
2094            })
2095            .collect(),
2096        _ => Vec::new(),
2097    }
2098}
2099
2100/// `util.StripClaudeCodeAttributionSystem`.
2101fn strip_attribution_system(body: &str) -> String {
2102    let system = rawjson::get(body, "system");
2103    let attribution = |t: &str| t.trim_start().starts_with(cloak::BILLING_PREFIX);
2104    match system.kind() {
2105        gjson::Kind::String if attribution(system.str()) => rawjson::delete(body, "system"),
2106        gjson::Kind::Array => {
2107            let blocks = system.array();
2108            let kept: Vec<String> = blocks
2109                .iter()
2110                .filter(|b| !(b.get("type").str() == "text" && attribution(b.get("text").str())))
2111                .map(|b| b.json().to_owned())
2112                .collect();
2113            if kept.len() == blocks.len() {
2114                body.to_owned()
2115            } else if kept.is_empty() {
2116                rawjson::delete(body, "system")
2117            } else {
2118                rawjson::set_raw(body, "system", &format!("[{}]", kept.join(",")))
2119            }
2120        }
2121        _ => body.to_owned(),
2122    }
2123}
2124
2125#[cfg(test)]
2126mod go_exec;
2127#[cfg(test)]
2128mod go_unit;
2129#[cfg(test)]
2130mod tests;