cpa-exec 0.4.3

Part of cliproxy: upstream provider executors
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Upstream executors. One module per provider; each owns its wire format, auth headers
//! and HTTP client profile. [`Executors`] dispatches on `Credential::provider`.
//!
//! Each call gets the config snapshot current when the request started, so a hot reload
//! never changes behaviour halfway through one execution.
//!
//! Credential preparation (token refresh, device identity creation) is a two-step
//! contract so the runtime can single-flight it: [`Executors::readiness`] is a cheap
//! check, and [`Executors::prepare`] does the work and returns a [`MetadataPatch`] that
//! the runtime commits. Executors never write credential files themselves.
//!
//! [`Readiness`] separates the two reasons Go prepares a credential. Requests block only
//! on [`Readiness::PrepareNow`] (Go `RequestAuthPreparer`: Claude identity, Meta API-key
//! mint, Antigravity project ID; also an Antigravity token inside five minutes of
//! expiry, which Go's executor refreshes inline). Token refresh is
//! [`Readiness::RefreshSoon`]: the
//! background loop refreshes inside the provider's refresh lead while requests keep
//! using the current token, and an upstream 401 triggers one refresh-and-retry (Go
//! `tryRefreshAfterUnauthorized`). An already expired token is still `RefreshSoon`:
//! Go sends it and recovers on 401.

pub mod aistudio;
pub mod antigravity;
pub mod antigravity_auth;
mod antigravity_compaction;
pub mod antigravity_credits;
mod antigravity_models;
mod antigravity_replay;
mod antigravity_request;
pub mod antigravity_version;
pub mod claude;
pub mod claude_login;
pub mod codex;
mod codex_capture;
mod codex_client;
mod codex_images;
mod codex_json;
pub mod codex_live;
pub mod codex_oauth;
pub mod codex_quota;
pub mod codex_refresh;
mod codex_replay;
mod codex_request;
mod codex_response;
pub mod codex_siwc;
pub mod codex_siwc_bind;
mod codex_sse;
#[cfg(test)]
mod codex_testkit;
mod codex_tls;
mod codex_tokens;
mod codex_ws;
pub mod devin;
pub mod devin_auth;
pub mod devin_models;
mod devin_payload;
mod devin_request;
mod devin_wire;
pub mod gemini;
mod gemini_payload;
mod gemini_stream;
mod home_replay;
pub mod kimi;
pub mod kimi_auth;
#[cfg(test)]
mod kimi_fixture;
mod kimi_http;
mod kimi_replay;
pub mod meta;
pub mod meta_auth;
mod meta_codex;
mod meta_wire;
pub mod oauth;
pub mod openai_compat;
mod openai_compat_go;
mod openai_compat_http;
pub mod openai_compat_multipart;
mod openai_compat_payload;
#[cfg(test)]
mod openai_compat_usage;
mod payload_barrier;
mod payload_media;
pub mod proxy;
mod quota;
mod rawjson;
mod replay;
#[cfg(test)]
mod test_tls;
mod tls;
mod tokenizer;
mod tokens;
mod translate;
mod upstream;
pub mod vertex;
pub mod vertex_auth;
mod vertex_openapi;
mod wire;
mod ws_observation;
pub mod wsrelay;
pub mod xai;
pub mod xai_auth;
mod xai_replay;
mod xai_request;
mod xai_response;
pub mod xai_url;
mod xai_ws;

use cpa_core::config::Config;
use cpa_core::credential::{Credential, MetadataPatch};
use cpa_core::exec::{ExecError, ExecRequest, ExecResponse, ExecSession, FailureScope};

/// Go `Auth.ExpirationTime`, `authLastRefreshTimestamp` and `authPreferredInterval`, for
/// the server's plugin refresh schedule (Go `shouldRefresh` for providers without a lead).
pub use kimi_http::{expiration, last_refresh, preferred_interval};
/// Go `helps.EnsureResponsesUsageDetails`, for paths outside the executors (plugin
/// executors' Responses payloads and stream chunks).
pub use openai_compat_payload::ensure_responses_usage_details;
pub use xai_ws::websockets_enabled;

/// What a credential needs before or around use. See the module docs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Readiness {
    /// Usable as is; nothing to refresh.
    Ready,
    /// Usable now; the background refresh loop should prepare it.
    RefreshSoon,
    /// Unusable until prepared; requests wait on the single-flighted preparation.
    PrepareNow,
}

pub struct Executors {
    pub claude: claude::ClaudeExecutor,
    pub codex: codex::CodexExecutor,
    /// Device-login providers (Kimi, Meta, Devin). `Default` builds production clients.
    pub devices: DeviceExecutors,
    /// API-key upstreams speaking OpenAI wire formats (OpenAI-compatible providers, xAI).
    pub openai: OpenAIExecutors,
    /// Google-family providers (Gemini API keys, native Interactions keys).
    pub google: GoogleExecutors,
}

/// Google-family executors, grouped like [`DeviceExecutors`].
#[derive(Default)]
pub struct GoogleExecutors {
    /// `gemini` and `gemini-interactions` API keys.
    pub gemini: gemini::GeminiExecutor,
    /// `vertex` service accounts and API keys.
    pub vertex: vertex::VertexExecutor,
    /// `aistudio` sessions on the `/v1/ws` relay, which the server route shares.
    pub aistudio: aistudio::AiStudioExecutor,
    /// `antigravity` OAuth credentials.
    pub antigravity: antigravity::AntigravityExecutor,
}

/// OpenAI-wire executors, grouped like [`DeviceExecutors`].
#[derive(Default)]
pub struct OpenAIExecutors {
    pub compat: openai_compat::OpenAICompatExecutor,
    pub xai: xai::XaiExecutor,
}

/// Executors for the device-login providers, grouped so adding one does not touch every
/// [`Executors`] construction site.
#[derive(Default)]
pub struct DeviceExecutors {
    pub kimi: kimi::KimiExecutor,
    pub meta: meta::MetaExecutor,
    pub devin: devin::DevinExecutor,
}

impl Executors {
    pub async fn execute(
        &self,
        credential: &Credential,
        req: ExecRequest,
        cfg: &Config,
    ) -> Result<ExecResponse, ExecError> {
        match credential.provider.as_str() {
            "claude" => self.claude.execute(credential, req, cfg).await,
            "codex" => self.codex.execute(credential, req, cfg).await,
            p if kimi::PROVIDERS.contains(&p) => self.devices.kimi.execute(&self.claude, credential, req, cfg).await,
            meta::PROVIDER => self.devices.meta.execute(credential, req, cfg).await,
            devin::PROVIDER => self.devices.devin.execute(credential, req, cfg).await,
            p if openai_compat::handles(p) => self.openai.compat.execute(credential, req, cfg).await,
            p if gemini::handles(p) => self.google.gemini.execute(credential, req, cfg).await,
            p if vertex::handles(p) => self.google.vertex.execute(credential, req, cfg).await,
            p if aistudio::handles(p) => self.google.aistudio.execute(credential, req, cfg).await,
            antigravity::PROVIDER => self.google.antigravity.execute(credential, req, cfg).await,
            xai::PROVIDER => self.openai.xai.execute(credential, req, cfg, false).await,
            other => Err(no_executor(other)),
        }
    }

    /// The Images API (`/v1/images/generations`, `/v1/images/edits`) for providers that
    /// serve it directly. `request_path` is the inbound route; `req.stream` asks for the
    /// upstream event stream passed through raw.
    pub async fn images(
        &self,
        credential: &Credential,
        req: ExecRequest,
        request_path: &str,
        cfg: &Config,
    ) -> Result<ExecResponse, ExecError> {
        match credential.provider.as_str() {
            "codex" => self.codex.images(credential, req, request_path, cfg).await,
            p if openai_compat::handles(p) => self.openai.compat.images(credential, req, request_path, cfg).await,
            xai::PROVIDER => self.openai.xai.images(credential, req, request_path, cfg).await,
            other => Err(no_executor(other)),
        }
    }

    /// The Videos API (`/v1/videos*`, `/openai/v1/videos*`) for providers that serve it.
    /// `request_path` is the inbound route; a route other than generations, edits or
    /// extensions polls the job named by the body's `request_id`.
    pub async fn videos(
        &self,
        credential: &Credential,
        req: ExecRequest,
        request_path: &str,
        cfg: &Config,
    ) -> Result<ExecResponse, ExecError> {
        match credential.provider.as_str() {
            xai::PROVIDER => self.openai.xai.videos(credential, req, request_path, cfg).await,
            other => Err(no_executor(other)),
        }
    }

    /// The OpenAI speech routes (`/v1/audio/speech`, `/v1/tts`): Go's `openai-speech`
    /// source, which only the xAI executor serves.
    pub async fn speech(
        &self,
        credential: &Credential,
        req: ExecRequest,
        cfg: &Config,
    ) -> Result<ExecResponse, ExecError> {
        match credential.provider.as_str() {
            xai::PROVIDER => self.openai.xai.speech(credential, req, cfg).await,
            other => Err(no_executor(other)),
        }
    }

    /// One turn of a downstream Responses WebSocket session. Codex and xAI credentials
    /// with `websockets` keep a pooled upstream socket per session; every other credential
    /// runs an ordinary execution and cannot continue upstream state, so a continuation
    /// turn fails with [`ExecError::replay_required`].
    pub async fn execute_in_session(
        &self,
        credential: &Credential,
        req: ExecRequest,
        cfg: &Config,
        session: &ExecSession,
    ) -> Result<ExecResponse, ExecError> {
        match credential.provider.as_str() {
            "codex" => self.codex.execute_in_session(credential, req, cfg, session).await,
            xai::PROVIDER => self.openai.xai.execute_in_session(credential, req, cfg, session).await,
            _ if session.continuation => Err(ExecError::replay_required()),
            _ => self.execute(credential, req, cfg).await,
        }
    }

    /// Whether `credential` keeps upstream conversation state on a session socket, so
    /// the next turn may be sent as an incremental continuation.
    pub fn session_upstream(&self, credential: &Credential) -> bool {
        match credential.provider.as_str() {
            "codex" => codex::CodexExecutor::upstream_websocket(credential),
            xai::PROVIDER => xai::XaiExecutor::session_upstream(credential),
            _ => false,
        }
    }

    /// Resolves when an upstream socket held for session `id` is lost; pending forever
    /// when no executor holds one.
    pub fn session_closed(&self, id: &str) -> impl std::future::Future<Output = ExecError> + Send + 'static {
        let codex = self.codex.session_closed(id);
        let xai = self.openai.xai.session_closed(id);
        async move {
            tokio::select! {
                error = codex => error,
                error = xai => error,
            }
        }
    }

    /// Releases everything executors hold for session `id`.
    pub fn close_session(&self, id: &str) {
        self.codex.close_session(id);
        self.openai.xai.close_session(id);
    }

    /// Go `CloseCodexWebsocketSessionsForAuthID` and `CloseXAIWebsocketSessionsForAuthID`
    /// for the credential store: given a removed credential's id, ends only the upstream
    /// WebSocket sessions that credential serves.
    pub fn credential_session_closer(&self) -> impl Fn(&str) + Send + Sync + 'static {
        let codex = self.codex.ws.clone();
        let xai = self.openai.xai.ws.clone();
        move |id| {
            codex.close_credential(id);
            xai.close_credential(id);
        }
    }

    /// Whether an executor serves this provider. Credentials of other providers never
    /// enter selection (Go skips auths whose executor is not registered).
    pub fn supports(&self, provider: &str) -> bool {
        matches!(
            provider,
            "claude" | "codex" | meta::PROVIDER | xai::PROVIDER | devin::PROVIDER
        ) || kimi::PROVIDERS.contains(&provider)
            || openai_compat::handles(provider)
            || gemini::handles(provider)
            || vertex::handles(provider)
            || aistudio::handles(provider)
            || antigravity::handles(provider)
    }

    /// Go `authHasRefreshCredential`: whether an upstream 401 on `credential` should be
    /// followed by one forced [`Self::prepare`] and a retry (Go
    /// `tryRefreshAfterUnauthorized`). A refresh token qualifies for every provider;
    /// Meta re-mints its API key from its device token. Providers that recover from a
    /// 401 another way add an arm here.
    pub fn has_refresh_credential(&self, credential: &Credential) -> bool {
        let filled = |v: Option<&str>| v.is_some_and(|v| !v.trim().is_empty());
        if filled(credential.str("refresh_token")) || filled(credential.str("refreshToken")) {
            return true;
        }
        // Go `CodexSIWCNeedsRefresh`'s hasRefresh: a `siwc` refresh token and client ID.
        // ponytail: Go asks it of every provider; Codex only until the Claude replay.
        if credential.provider == "codex" && codex_siwc::refresh_due(credential, chrono::Utc::now()).is_some() {
            return true;
        }
        credential.provider.trim().eq_ignore_ascii_case("meta")
            && (filled(credential.str("dca_token"))
                || filled(credential.attributes.get("dca_token").map(String::as_str)))
    }

    /// Whether `credential` needs preparation, and whether requests must wait for it.
    /// Must be cheap and side-effect free. Providers without request-time preparation
    /// report `RefreshSoon` whenever `needs_prepare` holds.
    pub fn readiness(&self, credential: &Credential, cfg: &Config) -> Readiness {
        if !self.needs_prepare(credential, cfg) {
            return Readiness::Ready;
        }
        match credential.provider.as_str() {
            "claude" if oauth::needs_identity(credential) => Readiness::PrepareNow,
            meta::PROVIDER if self.devices.meta.must_mint(credential) => Readiness::PrepareNow,
            antigravity::PROVIDER if self.google.antigravity.must_prepare(credential) => Readiness::PrepareNow,
            _ => Readiness::RefreshSoon,
        }
    }

    /// Whether `credential` needs any preparation (`readiness` is not `Ready`). Must be
    /// cheap and side-effect free. Providers implement this; the runtime reads
    /// [`Self::readiness`].
    pub fn needs_prepare(&self, credential: &Credential, cfg: &Config) -> bool {
        self.needs_prepare_at(credential, cfg, chrono::Utc::now())
    }

    /// [`Self::needs_prepare`] as of `now`. Once true it stays true as time passes until
    /// the credential is prepared: every provider compares an expiry or a last refresh
    /// with the clock.
    pub fn needs_prepare_at(&self, credential: &Credential, cfg: &Config, now: chrono::DateTime<chrono::Utc>) -> bool {
        match credential.provider.as_str() {
            "claude" => self.claude.needs_prepare_at(credential, cfg, now),
            "codex" => self.codex.needs_prepare_at(credential, cfg, now),
            p if kimi::PROVIDERS.contains(&p) => self.devices.kimi.needs_prepare_at(credential, cfg, now),
            meta::PROVIDER => self.devices.meta.needs_prepare_at(credential, cfg, now),
            xai::PROVIDER => self.openai.xai.needs_prepare_at(credential, cfg, now),
            devin::PROVIDER => self.devices.devin.needs_prepare_at(credential, cfg, now),
            antigravity::PROVIDER => self.google.antigravity.needs_prepare_at(credential, cfg, now),
            _ => false,
        }
    }

    /// The earliest time within `horizon` of `now` at which `credential` needs
    /// preparation (`now` if it already does), to one second. Bisects
    /// [`Self::needs_prepare_at`], so every provider's own rule decides; about a dozen
    /// cheap evaluations for a credential that comes due inside the horizon, one for
    /// the rest.
    pub fn prepare_due(
        &self,
        credential: &Credential,
        cfg: &Config,
        now: chrono::DateTime<chrono::Utc>,
        horizon: chrono::Duration,
    ) -> Option<chrono::DateTime<chrono::Utc>> {
        if self.needs_prepare_at(credential, cfg, now) {
            return Some(now);
        }
        let (mut lo, mut hi) = (now, now + horizon);
        if !self.needs_prepare_at(credential, cfg, hi) {
            return None;
        }
        while hi - lo > chrono::Duration::seconds(1) {
            let mid = lo + (hi - lo) / 2;
            if self.needs_prepare_at(credential, cfg, mid) {
                hi = mid;
            } else {
                lo = mid;
            }
        }
        Some(hi)
    }

    /// The runtime registered models for `credentials` (Go `registerModelsForAuth`):
    /// providers that probe capabilities after registration start here. Must not block.
    pub fn models_registered(&self, credentials: &[std::sync::Arc<Credential>], cfg: &Config) {
        self.google.antigravity.models_registered(credentials, cfg);
    }

    /// [`Self::prepare`] for a credential whose access token an upstream rejected, or a
    /// management refresh: Codex refreshes even when not due (Go marks the token rejected
    /// or forces the refresh). Other providers prepare as usual; most of them (Claude
    /// once its identity is known, Kimi, Meta, Devin) refresh whenever asked.
    pub async fn prepare_forced(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
        match credential.provider.as_str() {
            "codex" => self.codex.refresh_now(credential, cfg).await,
            _ => self.prepare(credential, cfg).await,
        }
    }

    /// [`Self::prepare_forced`] for a management refresh (Go `IsForceRefreshContext`):
    /// Codex refreshes every token partition that can refresh.
    pub async fn prepare_management(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
        match credential.provider.as_str() {
            "codex" => self.codex.refresh_all(credential, cfg).await,
            _ => self.prepare_forced(credential, cfg).await,
        }
    }

    /// Prepares `credential` and returns the metadata change to commit.
    pub async fn prepare(&self, credential: &Credential, cfg: &Config) -> Result<MetadataPatch, ExecError> {
        match credential.provider.as_str() {
            "claude" => self.claude.prepare(credential, cfg).await,
            "codex" => self.codex.prepare(credential, cfg).await,
            p if kimi::PROVIDERS.contains(&p) => self.devices.kimi.prepare(credential, cfg).await,
            meta::PROVIDER => self.devices.meta.prepare(credential, cfg).await,
            xai::PROVIDER => self.openai.xai.prepare(credential, cfg).await,
            devin::PROVIDER => self.devices.devin.prepare(credential, cfg).await,
            antigravity::PROVIDER => self.google.antigravity.prepare(credential, cfg).await,
            other => Err(no_executor(other)),
        }
    }
}

fn no_executor(provider: &str) -> ExecError {
    ExecError::local(
        500,
        FailureScope::Credential,
        format!("no executor for provider {provider:?}"),
    )
}

#[cfg(test)]
mod readiness_tests {
    use super::{Executors, Readiness, claude};
    use cpa_core::config::Config;
    use cpa_core::credential::Credential;
    use std::path::Path;

    fn credential(metadata: serde_json::Value) -> Credential {
        Credential::from_file(
            Path::new("/auth"),
            Path::new("/auth/a.json"),
            metadata.as_object().unwrap().clone(),
        )
        .unwrap()
    }

    fn rfc3339(offset: chrono::Duration) -> String {
        (chrono::Utc::now() + offset).to_rfc3339()
    }

    /// Requests block only on Go `ShouldPrepareRequestAuth` (Claude: an OAuth token
    /// without a canonical device pool or account UUID). Expiry, even past expiry, is a
    /// background refresh.
    #[test]
    fn only_missing_identity_blocks_requests() {
        let executors = Executors {
            claude: claude::ClaudeExecutor::new("http://127.0.0.1:1").unwrap(),
            codex: Default::default(),
            devices: Default::default(),
            openai: Default::default(),
            google: Default::default(),
        };
        let cfg = Config::default();
        let pool = serde_json::json!(["a".repeat(64)]);
        let identified = |expired: String| {
            credential(serde_json::json!({
                "type": "claude",
                "access_token": "sk-ant-oat01-fake",
                "refresh_token": "fake-refresh",
                "claude_device_ids": pool,
                "account_uuid": "fake-account",
                "expired": expired,
            }))
        };
        let cases = [
            (identified(rfc3339(chrono::Duration::days(2))), Readiness::Ready),
            (identified(rfc3339(chrono::Duration::hours(1))), Readiness::RefreshSoon),
            (identified(rfc3339(-chrono::Duration::hours(1))), Readiness::RefreshSoon),
            (
                credential(serde_json::json!({
                    "type": "claude",
                    "access_token": "sk-ant-oat01-fake",
                    "refresh_token": "fake-refresh",
                    "expired": rfc3339(-chrono::Duration::hours(1)),
                })),
                Readiness::PrepareNow,
            ),
            (
                credential(serde_json::json!({"type": "claude", "access_token": "sk-ant-api03-fake"})),
                Readiness::Ready,
            ),
            (
                credential(serde_json::json!({
                    "type": "kimi",
                    "access_token": "fake-access",
                    "refresh_token": "fake-refresh",
                    "expired": rfc3339(-chrono::Duration::hours(1)),
                })),
                Readiness::RefreshSoon,
            ),
            // Antigravity: requests wait for the project ID and for a token valid five
            // more minutes (Go's inline refresh); otherwise the token refreshes 30
            // minutes ahead in the background.
            (
                credential(serde_json::json!({
                    "type": "antigravity",
                    "access_token": "fake-access",
                    "refresh_token": "fake-refresh",
                    "expired": rfc3339(chrono::Duration::hours(5)),
                })),
                Readiness::PrepareNow,
            ),
            (
                credential(serde_json::json!({
                    "type": "antigravity",
                    "project_id": "proj-1",
                    "access_token": "fake-access",
                    "refresh_token": "fake-refresh",
                    "expired": rfc3339(chrono::Duration::minutes(20)),
                })),
                Readiness::RefreshSoon,
            ),
            (
                credential(serde_json::json!({
                    "type": "antigravity",
                    "project_id": "proj-1",
                    "access_token": "fake-access",
                    "refresh_token": "fake-refresh",
                    "expired": rfc3339(chrono::Duration::minutes(6)),
                })),
                Readiness::RefreshSoon,
            ),
            (
                credential(serde_json::json!({
                    "type": "antigravity",
                    "project_id": "proj-1",
                    "access_token": "fake-access",
                    "refresh_token": "fake-refresh",
                    "expired": rfc3339(chrono::Duration::minutes(4)),
                })),
                Readiness::PrepareNow,
            ),
            (
                credential(serde_json::json!({
                    "type": "antigravity",
                    "project_id": "proj-1",
                    "refresh_token": "fake-refresh",
                    "expired": rfc3339(chrono::Duration::minutes(40)),
                })),
                Readiness::PrepareNow,
            ),
            (
                credential(serde_json::json!({
                    "type": "antigravity",
                    "project_id": "proj-1",
                    "access_token": "fake-access",
                })),
                Readiness::PrepareNow,
            ),
            (
                credential(serde_json::json!({
                    "type": "antigravity",
                    "project_id": "proj-1",
                    "access_token": "fake-access",
                    "refresh_token": "fake-refresh",
                    "expired": rfc3339(chrono::Duration::minutes(40)),
                })),
                Readiness::Ready,
            ),
        ];
        for (i, (c, expected)) in cases.iter().enumerate() {
            assert_eq!(executors.readiness(c, &cfg), *expected, "case {i}");
        }
    }

    /// The refresh loop sleeps until `prepare_due`: each provider's own lead before
    /// expiry (Claude 4 h, Kimi 5 min), to the second, `now` once due, and nothing past
    /// the horizon.
    #[test]
    fn prepare_due_finds_each_providers_deadline() {
        let executors = Executors {
            claude: claude::ClaudeExecutor::new("http://127.0.0.1:1").unwrap(),
            codex: Default::default(),
            devices: Default::default(),
            openai: Default::default(),
            google: Default::default(),
        };
        let cfg = Config::default();
        let now = chrono::Utc::now();
        let horizon = chrono::Duration::hours(1);
        let with_expiry = |provider: &str, expiry: chrono::DateTime<chrono::Utc>| {
            credential(serde_json::json!({
                "type": provider,
                "access_token": "fake-access",
                "refresh_token": "fake-refresh",
                "expired": expiry.to_rfc3339(),
            }))
        };
        let near = |got: Option<chrono::DateTime<chrono::Utc>>, want: chrono::DateTime<chrono::Utc>| {
            let got = got.expect("due inside the horizon");
            assert!((got - want).num_milliseconds().abs() <= 1000, "{got} vs {want}");
        };
        let offset = chrono::Duration::seconds(1337);
        let claude = with_expiry("claude", now + chrono::Duration::hours(4) + offset);
        near(executors.prepare_due(&claude, &cfg, now, horizon), now + offset);
        let kimi = with_expiry("kimi", now + chrono::Duration::minutes(5) + offset);
        near(executors.prepare_due(&kimi, &cfg, now, horizon), now + offset);
        let due = with_expiry("claude", now - chrono::Duration::hours(1));
        assert_eq!(executors.prepare_due(&due, &cfg, now, horizon), Some(now));
        let later = with_expiry("claude", now + chrono::Duration::hours(6));
        assert_eq!(executors.prepare_due(&later, &cfg, now, horizon), None);
        let api_key = credential(serde_json::json!({"type": "claude", "access_token": "sk-ant-api03-fake"}));
        assert_eq!(executors.prepare_due(&api_key, &cfg, now, horizon), None);
    }

    /// Go `authHasRefreshCredential`.
    #[test]
    fn refresh_credentials_follow_go() {
        let executors = Executors {
            claude: claude::ClaudeExecutor::new("http://127.0.0.1:1").unwrap(),
            codex: Default::default(),
            openai: Default::default(),
            google: Default::default(),
            devices: Default::default(),
        };
        let has = |m: serde_json::Value| executors.has_refresh_credential(&credential(m));
        assert!(has(serde_json::json!({"type": "claude", "refresh_token": "fake"})));
        assert!(has(serde_json::json!({"type": "kimi", "refreshToken": "fake"})));
        assert!(!has(serde_json::json!({"type": "claude", "refresh_token": "  "})));
        // A Sign in with ChatGPT partition needs its refresh token and client ID.
        assert!(has(
            serde_json::json!({"type": "codex", "siwc": {"refresh_token": "fake", "client_id": "c"}})
        ));
        assert!(!has(
            serde_json::json!({"type": "codex", "siwc": {"refresh_token": "fake"}})
        ));
        assert!(
            !has(serde_json::json!({"type": "claude", "siwc": {"refresh_token": "fake", "client_id": "c"}})),
            "SIWC is Codex's"
        );
        assert!(has(serde_json::json!({"type": "meta", "dca_token": "fake"})));
        assert!(
            !has(serde_json::json!({"type": "claude", "dca_token": "fake"})),
            "dca_token is Meta's"
        );
        let mut meta = credential(serde_json::json!({"type": "meta"}));
        assert!(!executors.has_refresh_credential(&meta));
        meta.attributes.insert("dca_token".into(), "fake".into());
        assert!(executors.has_refresh_credential(&meta));
    }
}