synaps-core 0.9.0

Foundation types, config, session, auth, protocol — leaf crate for agent-runtime
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
//! Kimi Code OAuth (RFC 8628 device authorization; direct bearer, no mint).
//!
//! Experimental. Flow verified against the official Kimi Code CLI (v0.31.1):
//! its embedded `packages/oauth` sources pin the same endpoints, client id,
//! grant type, error codes, and refresh semantics implemented here. The
//! endpoints are not published as a stable third-party API; do not describe
//! them as officially supported.
//!
//! Flow shape (differences from GitHub Copilot):
//! - No session mint: the device-flow `access_token` is used directly as the
//!   API bearer for `https://api.kimi.com/coding/v1`.
//! - Access tokens are short-lived (~15 min) and refresh tokens **rotate on
//!   every refresh** — the rotated pair must always be persisted.
//! - A refresh rejected with HTTP 401/403 or `invalid_grant` means the stored
//!   refresh token is dead; the user must re-login.
//! - Upstream sends `X-Msh-*` device-identity headers on every OAuth and API
//!   request; we mirror that conventional surface with an honest User-Agent.
//!
//! Credential mapping:
//! - `OAuthCredentials.refresh` = rotating Kimi refresh token (broker-owned)
//! - `OAuthCredentials.access`  = short-lived Kimi access token
//! - `OAuthCredentials.expires` = access expiry (ms, with refresh margin)

use std::{
    fmt,
    path::PathBuf,
    sync::{
        atomic::{AtomicBool, Ordering},
        Arc,
    },
    time::Duration,
};

use async_trait::async_trait;
use reqwest::Client;
use serde::Deserialize;
use url::Url;

use super::{auth_file_path, now_millis, open_browser, save_provider_auth, OAuthCredentials};

// ── Public constants (evidence: Kimi Code CLI v0.31.1 embedded oauth pkg) ────

/// Canonical storage / broker / model-prefix id.
pub const PROVIDER: &str = "kimi-code";

/// Public native-client id shipped inside the official Kimi Code CLI
/// (`KIMI_CODE_FLOW_CONFIG.clientId`). Not a client secret.
pub const CLIENT_ID: &str = "17e5f671-d194-4dfb-9706-5516cb48c098";

/// OAuth host pinned by the official CLI (`https://auth.kimi.com`).
pub const OAUTH_HOST: &str = "https://auth.kimi.com";

/// Device-authorization endpoint (RFC 8628 §3.1, Kimi path shape).
pub const DEVICE_AUTH_URL: &str = "https://auth.kimi.com/api/oauth/device_authorization";

/// Token endpoint — device-code polling AND refresh grants.
pub const TOKEN_URL: &str = "https://auth.kimi.com/api/oauth/token";

/// Device-code grant type (RFC 8628).
pub const DEVICE_GRANT_TYPE: &str = "urn:ietf:params:oauth:grant-type:device_code";

/// Managed Kimi Code API base (bearer = OAuth access token).
pub const API_BASE_URL: &str = "https://api.kimi.com/coding/v1";

/// Conventional platform tag sent by the official CLI (`X-Msh-Platform`).
pub const MSH_PLATFORM: &str = "kimi_code_cli";
/// Kimi Code surface version this flow was verified against (`X-Msh-Version`).
pub const MSH_SURFACE_VERSION: &str = "0.31.1";
/// Honest User-Agent (matches the Copilot mint precedent).
pub const USER_AGENT: &str = "SynapsCLI/0.6.0";

const DEFAULT_POLL_INTERVAL_SECS: u64 = 5;
/// Hard ceiling so `slow_down` cannot grow unbounded.
const MAX_POLL_INTERVAL_SECS: u64 = 30;
/// Seconds added on each `slow_down` (upstream: +5s).
const SLOW_DOWN_STEP_SECS: u64 = 5;
/// Local wall-clock budget when the server omits `expires_in`
/// (upstream uses a 15-minute device-code timeout).
const DEFAULT_DEVICE_EXPIRES_IN_SECS: u64 = 900;

/// Refresh-margin floor/ratio (upstream: `max(300, expires_in * 0.5)` secs).
const MIN_REFRESH_MARGIN_SECS: u64 = 300;
/// Never bake a margin that would mark a fresh token as already expired.
const MARGIN_HEADROOM_SECS: u64 = 60;

/// Refresh retry policy (upstream parity: 3 attempts, 2^n seconds backoff).
const REFRESH_MAX_ATTEMPTS: u32 = 3;
const REFRESH_BACKOFF_BASE_MS: u64 = 1_000;

/// Connect timeout for production OAuth HTTP.
pub const CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
/// Full request timeout for production OAuth HTTP.
pub const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
/// Maximum response body bytes accepted from OAuth endpoints.
pub const MAX_RESPONSE_BODY_BYTES: usize = 64 * 1024;

/// Logical sleep quantum for cancel checks (production wall-clock chunks).
const CANCEL_POLL_QUANTUM: Duration = Duration::from_millis(200);

// ── Secret-safe errors ───────────────────────────────────────────────────────

/// Failures from the Kimi auth chain. Variants intentionally store **no**
/// device codes, user codes, or tokens so Display/Debug cannot leak them.
#[derive(Clone, PartialEq, Eq)]
pub enum KimiAuthError {
    UntrustedEndpoint,
    InvalidDeviceResponse,
    InvalidTokenResponse,
    AccessDenied,
    Expired,
    Cancelled,
    /// Refresh rejected (401/403/invalid_grant) — re-login required.
    Unauthorized,
    HttpStatus(u16),
    Transport,
    ResponseTooLarge,
    Persist,
    Other(&'static str),
}

impl KimiAuthError {
    fn label(&self) -> &'static str {
        match self {
            Self::UntrustedEndpoint => "untrusted endpoint",
            Self::InvalidDeviceResponse => "invalid device authorization response",
            Self::InvalidTokenResponse => "invalid token response",
            Self::AccessDenied => "access denied",
            Self::Expired => "device code expired",
            Self::Cancelled => "login cancelled",
            Self::Unauthorized => "kimi-code refresh token rejected; run `synaps login kimi-code`",
            Self::HttpStatus(_) => "HTTP error",
            Self::Transport => "transport error",
            Self::ResponseTooLarge => "response body too large",
            Self::Persist => "credential persistence failed",
            Self::Other(msg) => msg,
        }
    }
}

impl fmt::Display for KimiAuthError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::HttpStatus(code) => write!(f, "HTTP error {code}"),
            other => f.write_str(other.label()),
        }
    }
}

impl fmt::Debug for KimiAuthError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "KimiAuthError({self})")
    }
}

impl From<KimiAuthError> for String {
    fn from(value: KimiAuthError) -> Self {
        value.to_string()
    }
}

// ── Injectable boundaries ────────────────────────────────────────────────────

/// Minimal HTTP response used by the device-flow / refresh state machine.
#[derive(Debug, Clone)]
pub struct InjectedHttpResponse {
    pub status: u16,
    pub body: String,
}

/// Injectable HTTP surface so tests never touch the network.
#[async_trait]
pub trait KimiHttp: Send + Sync {
    async fn post_form(
        &self,
        url: &str,
        form: &[(&str, &str)],
    ) -> Result<InjectedHttpResponse, KimiAuthError>;
}

/// Injectable clock (now + cancellable sleep) for deterministic poll tests.
#[async_trait]
pub trait KimiClock: Send + Sync {
    fn now_millis(&self) -> u64;

    /// Sleep `duration`, returning early with [`KimiAuthError::Cancelled`]
    /// when `cancel` flips.
    async fn sleep_cancellable<X: KimiCancel + ?Sized>(
        &self,
        duration: Duration,
        cancel: &X,
    ) -> Result<(), KimiAuthError>;
}

/// Injectable cancellation signal for device-flow polling.
pub trait KimiCancel: Send + Sync {
    fn is_cancelled(&self) -> bool;
}

impl KimiCancel for AtomicBool {
    fn is_cancelled(&self) -> bool {
        self.load(Ordering::SeqCst)
    }
}

impl KimiCancel for Arc<AtomicBool> {
    fn is_cancelled(&self) -> bool {
        self.load(Ordering::SeqCst)
    }
}

/// Injectable browser opener (production uses `open_browser`).
pub trait KimiBrowser: Send + Sync {
    fn open(&self, url: &str) -> Result<(), KimiAuthError>;
}

// ── Device-flow types ────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
pub struct DeviceAuthorization {
    /// Opaque device code — never logged via public error types.
    pub(crate) device_code: String,
    pub user_code: String,
    /// Plain verification page (may be empty; server contract keeps it
    /// optional as long as the complete URI is present).
    pub verification_uri: String,
    /// Verification URI with the user code embedded — preferred display/open
    /// target (required by the upstream contract).
    pub verification_uri_complete: String,
    pub expires_in_secs: u64,
    pub interval_secs: u64,
    pub issued_at_ms: u64,
}

/// Successful token grant (poll or refresh). Secret-bearing — never logged.
#[derive(Clone, PartialEq, Eq)]
pub struct KimiTokenSet {
    pub access_token: String,
    pub refresh_token: String,
    pub expires_in_secs: u64,
}

impl fmt::Debug for KimiTokenSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("KimiTokenSet")
            .field("access_token", &"[REDACTED]")
            .field("refresh_token", &"[REDACTED]")
            .field("expires_in_secs", &self.expires_in_secs)
            .finish()
    }
}

/// Outcome of one device-token poll (secret-safe; tokens only on Authorized).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DevicePollOutcome {
    Pending,
    SlowDown,
    Authorized { token: KimiTokenSet },
    Denied,
    Expired,
    OtherError,
}

// ── Endpoint pinning ─────────────────────────────────────────────────────────

/// Accept only the exact pinned HTTPS auth.kimi.com OAuth endpoints.
pub fn validate_oauth_endpoint(url: &str) -> Result<(), KimiAuthError> {
    if url != DEVICE_AUTH_URL && url != TOKEN_URL {
        return Err(KimiAuthError::UntrustedEndpoint);
    }
    let parsed = Url::parse(url).map_err(|_| KimiAuthError::UntrustedEndpoint)?;
    if parsed.scheme() != "https" || parsed.host_str() != Some("auth.kimi.com") {
        return Err(KimiAuthError::UntrustedEndpoint);
    }
    Ok(())
}

/// Accept only an HTTPS kimi.com / *.kimi.com verification destination with
/// no userinfo, no fragment, and no explicit port. The exact path is
/// server-assigned (not a documented constant), so the host is the pin.
pub fn validate_verification_uri(uri: &str) -> Result<(), KimiAuthError> {
    let parsed = Url::parse(uri).map_err(|_| KimiAuthError::UntrustedEndpoint)?;
    if parsed.scheme() != "https" {
        return Err(KimiAuthError::UntrustedEndpoint);
    }
    if !parsed.username().is_empty() || parsed.password().is_some() {
        return Err(KimiAuthError::UntrustedEndpoint);
    }
    if parsed.port().is_some() || parsed.fragment().is_some() {
        return Err(KimiAuthError::UntrustedEndpoint);
    }
    match parsed.host_str() {
        Some(host) if host == "kimi.com" || host.ends_with(".kimi.com") => Ok(()),
        _ => Err(KimiAuthError::UntrustedEndpoint),
    }
}

// ── Device identity headers ──────────────────────────────────────────────────

/// Strip to printable ASCII, mirroring the upstream `asciiHeader` helper.
fn ascii_header(value: &str, fallback: &str) -> String {
    let cleaned: String = value
        .chars()
        .filter(|c| (' '..='~').contains(c))
        .collect::<String>()
        .trim()
        .to_string();
    if cleaned.is_empty() {
        fallback.to_string()
    } else {
        cleaned
    }
}

/// Path of the persistent Kimi device id (kept beside auth.json, mode 0600).
pub fn device_id_path() -> PathBuf {
    auth_file_path().with_file_name("kimi_device_id")
}

/// Load the stable device id, creating (and best-effort persisting) a fresh
/// UUIDv4 on first use. Upstream parity: `createKimiDeviceId`.
pub fn load_or_create_device_id() -> String {
    let path = device_id_path();
    if let Ok(text) = std::fs::read_to_string(&path) {
        let trimmed = text.trim();
        if !trimmed.is_empty() {
            return trimmed.to_string();
        }
    }
    let id = uuid::Uuid::new_v4().to_string();
    if let Some(parent) = path.parent() {
        let _ = std::fs::create_dir_all(parent);
    }
    let _ = std::fs::write(&path, &id);
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let _ = std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o600));
    }
    id
}

/// Device-identity headers sent on every OAuth and managed-API request.
///
/// Upstream (`createKimiDeviceHeaders`) sends this exact `X-Msh-*` surface;
/// the User-Agent identifies Synaps honestly (Copilot mint precedent).
/// No header value is secret.
pub fn identity_request_headers(device_id: &str) -> Vec<(&'static str, String)> {
    let host_name = std::env::var("HOSTNAME")
        .ok()
        .filter(|v| !v.trim().is_empty())
        .unwrap_or_else(|| "synaps".to_string());
    vec![
        ("User-Agent", USER_AGENT.to_string()),
        ("Accept", "application/json".to_string()),
        ("X-Msh-Platform", MSH_PLATFORM.to_string()),
        ("X-Msh-Version", MSH_SURFACE_VERSION.to_string()),
        ("X-Msh-Device-Id", ascii_header(device_id, "unknown")),
        ("X-Msh-Device-Name", ascii_header(&host_name, "synaps")),
        (
            "X-Msh-Device-Model",
            ascii_header(
                &format!("{} {}", std::env::consts::OS, std::env::consts::ARCH),
                "unknown",
            ),
        ),
        ("X-Msh-Os-Version", std::env::consts::OS.to_string()),
    ]
}

// ── Parsing (pure) ───────────────────────────────────────────────────────────

#[derive(Debug, Deserialize)]
struct DeviceCodeJson {
    device_code: String,
    user_code: String,
    #[serde(default)]
    verification_uri: Option<String>,
    verification_uri_complete: String,
    #[serde(default)]
    expires_in: Option<u64>,
    #[serde(default)]
    interval: Option<u64>,
}

#[derive(Debug, Deserialize)]
struct TokenJson {
    access_token: Option<String>,
    refresh_token: Option<String>,
    expires_in: Option<u64>,
    error: Option<String>,
}

pub fn parse_device_code_response(
    body: &str,
    now_ms: u64,
) -> Result<DeviceAuthorization, KimiAuthError> {
    let raw: DeviceCodeJson =
        serde_json::from_str(body).map_err(|_| KimiAuthError::InvalidDeviceResponse)?;
    if raw.device_code.trim().is_empty()
        || raw.user_code.trim().is_empty()
        || raw.verification_uri_complete.trim().is_empty()
    {
        return Err(KimiAuthError::InvalidDeviceResponse);
    }
    validate_verification_uri(&raw.verification_uri_complete)?;
    let verification_uri = raw.verification_uri.unwrap_or_default();
    if !verification_uri.is_empty() {
        validate_verification_uri(&verification_uri)?;
    }
    let expires_in = match raw.expires_in {
        Some(0) => return Err(KimiAuthError::InvalidDeviceResponse),
        Some(v) => v,
        // Upstream tolerates a missing expires_in and relies on its local
        // 15-minute wall-clock budget; mirror that.
        None => DEFAULT_DEVICE_EXPIRES_IN_SECS,
    };
    let interval = raw
        .interval
        .unwrap_or(DEFAULT_POLL_INTERVAL_SECS)
        .clamp(1, MAX_POLL_INTERVAL_SECS);
    Ok(DeviceAuthorization {
        device_code: raw.device_code,
        user_code: raw.user_code,
        verification_uri,
        verification_uri_complete: raw.verification_uri_complete,
        expires_in_secs: expires_in,
        interval_secs: interval,
        issued_at_ms: now_ms,
    })
}

/// Parse a successful token grant. The upstream contract requires all three
/// of access_token / refresh_token / expires_in — enforce the same here so a
/// non-rotating or non-expiring credential can never be persisted silently.
fn parse_token_success(raw: TokenJson) -> Result<KimiTokenSet, KimiAuthError> {
    let access = raw
        .access_token
        .filter(|t| !t.trim().is_empty())
        .ok_or(KimiAuthError::InvalidTokenResponse)?;
    let refresh = raw
        .refresh_token
        .filter(|t| !t.trim().is_empty())
        .ok_or(KimiAuthError::InvalidTokenResponse)?;
    let expires_in = match raw.expires_in {
        Some(v) if v > 0 => v,
        _ => return Err(KimiAuthError::InvalidTokenResponse),
    };
    Ok(KimiTokenSet {
        access_token: access,
        refresh_token: refresh,
        expires_in_secs: expires_in,
    })
}

pub fn parse_device_poll_response(body: &str) -> Result<DevicePollOutcome, KimiAuthError> {
    let raw: TokenJson =
        serde_json::from_str(body).map_err(|_| KimiAuthError::InvalidTokenResponse)?;
    if raw.access_token.is_some() {
        return Ok(DevicePollOutcome::Authorized {
            token: parse_token_success(raw)?,
        });
    }
    Ok(match raw.error.as_deref().unwrap_or("") {
        "authorization_pending" => DevicePollOutcome::Pending,
        "slow_down" => DevicePollOutcome::SlowDown,
        "access_denied" => DevicePollOutcome::Denied,
        "expired_token" => DevicePollOutcome::Expired,
        _ => DevicePollOutcome::OtherError,
    })
}

/// Map a granted token set onto broker credentials.
///
/// The stored `expires` bakes in the upstream refresh margin
/// (`max(300, expires_in / 2)` seconds, capped so a fresh token is never
/// instantly "expired"), so the repo-wide bare `now >= expires` check
/// refreshes on the same schedule as the official CLI.
pub fn credentials_from_token(token: KimiTokenSet, now_ms: u64) -> OAuthCredentials {
    let margin_secs = MIN_REFRESH_MARGIN_SECS
        .max(token.expires_in_secs / 2)
        .min(token.expires_in_secs.saturating_sub(MARGIN_HEADROOM_SECS));
    let expires = now_ms
        .saturating_add(token.expires_in_secs.saturating_mul(1000))
        .saturating_sub(margin_secs.saturating_mul(1000));
    OAuthCredentials {
        auth_type: "oauth".into(),
        refresh: token.refresh_token,
        access: token.access_token,
        expires,
        account_id: None,
    }
}

pub fn apply_slow_down(interval_secs: u64) -> u64 {
    interval_secs
        .saturating_add(SLOW_DOWN_STEP_SECS)
        .min(MAX_POLL_INTERVAL_SECS)
}

// ── Bounded body read ────────────────────────────────────────────────────────

/// Read a response body with a hard byte cap. Fail closed if exceeded.
pub async fn read_body_capped(
    response: reqwest::Response,
    cap: usize,
) -> Result<String, KimiAuthError> {
    use futures::StreamExt;
    let mut buf = Vec::new();
    let mut stream = response.bytes_stream();
    while let Some(chunk) = stream.next().await {
        let chunk = chunk.map_err(|_| KimiAuthError::Transport)?;
        if buf.len().saturating_add(chunk.len()) > cap {
            return Err(KimiAuthError::ResponseTooLarge);
        }
        buf.extend_from_slice(&chunk);
    }
    String::from_utf8(buf).map_err(|_| KimiAuthError::Transport)
}

// ── Device-flow state machine ────────────────────────────────────────────────

/// Start device authorization (POST /api/oauth/device_authorization).
pub async fn start_device_authorization<H, C>(
    http: &H,
    clock: &C,
) -> Result<DeviceAuthorization, KimiAuthError>
where
    H: KimiHttp,
    C: KimiClock,
{
    validate_oauth_endpoint(DEVICE_AUTH_URL)?;
    let resp = http
        .post_form(DEVICE_AUTH_URL, &[("client_id", CLIENT_ID)])
        .await?;
    if !(200..300).contains(&resp.status) {
        return Err(KimiAuthError::HttpStatus(resp.status));
    }
    parse_device_code_response(&resp.body, clock.now_millis())
}

/// One poll against the token endpoint.
pub async fn poll_device_token<H>(
    http: &H,
    device_code: &str,
) -> Result<DevicePollOutcome, KimiAuthError>
where
    H: KimiHttp,
{
    validate_oauth_endpoint(TOKEN_URL)?;
    let resp = http
        .post_form(
            TOKEN_URL,
            &[
                ("client_id", CLIENT_ID),
                ("device_code", device_code),
                ("grant_type", DEVICE_GRANT_TYPE),
            ],
        )
        .await?;
    // Upstream contract: pending/denied/expired arrive as OAuth error bodies
    // with 4xx statuses — classify by body first, status second.
    if resp.status >= 500 {
        return Err(KimiAuthError::HttpStatus(resp.status));
    }
    match parse_device_poll_response(&resp.body) {
        Ok(DevicePollOutcome::OtherError) if !(200..300).contains(&resp.status) => {
            Err(KimiAuthError::HttpStatus(resp.status))
        }
        Ok(outcome) => Ok(outcome),
        Err(err) if !(200..300).contains(&resp.status) => {
            let _ = err;
            Err(KimiAuthError::HttpStatus(resp.status))
        }
        Err(err) => Err(err),
    }
}

/// Poll until authorized, denied, expired, cancelled, or deadline.
pub async fn wait_for_device_authorization<H, C, X>(
    http: &H,
    clock: &C,
    authz: &DeviceAuthorization,
    cancel: &X,
) -> Result<KimiTokenSet, KimiAuthError>
where
    H: KimiHttp,
    C: KimiClock,
    X: KimiCancel + ?Sized,
{
    let deadline_ms = authz
        .issued_at_ms
        .saturating_add(authz.expires_in_secs.saturating_mul(1000));
    let mut interval_secs = authz.interval_secs.clamp(1, MAX_POLL_INTERVAL_SECS);

    loop {
        if cancel.is_cancelled() {
            return Err(KimiAuthError::Cancelled);
        }
        if clock.now_millis() >= deadline_ms {
            return Err(KimiAuthError::Expired);
        }

        clock
            .sleep_cancellable(Duration::from_secs(interval_secs), cancel)
            .await?;

        if cancel.is_cancelled() {
            return Err(KimiAuthError::Cancelled);
        }
        if clock.now_millis() >= deadline_ms {
            return Err(KimiAuthError::Expired);
        }

        match poll_device_token(http, &authz.device_code).await? {
            DevicePollOutcome::Pending => continue,
            DevicePollOutcome::SlowDown => {
                interval_secs = apply_slow_down(interval_secs);
            }
            DevicePollOutcome::Authorized { token } => return Ok(token),
            DevicePollOutcome::Denied => return Err(KimiAuthError::AccessDenied),
            DevicePollOutcome::Expired => return Err(KimiAuthError::Expired),
            DevicePollOutcome::OtherError => return Err(KimiAuthError::InvalidTokenResponse),
        }
    }
}

// ── Refresh (rotating; retry transient failures) ─────────────────────────────

/// Classify one refresh response. Pure; drives the retry loop.
pub fn classify_refresh_response(
    status: u16,
    body: &str,
) -> Result<KimiTokenSet, RefreshDisposition> {
    let raw: Option<TokenJson> = serde_json::from_str(body).ok();
    if (200..300).contains(&status) {
        if let Some(raw) = raw {
            if raw.access_token.is_some() {
                return parse_token_success(raw).map_err(RefreshDisposition::Fatal);
            }
        }
        return Err(RefreshDisposition::Fatal(
            KimiAuthError::InvalidTokenResponse,
        ));
    }
    let error_code = raw.and_then(|r| r.error).unwrap_or_default();
    if status == 401 || status == 403 || error_code == "invalid_grant" {
        return Err(RefreshDisposition::Fatal(KimiAuthError::Unauthorized));
    }
    if matches!(status, 429 | 500 | 502 | 503 | 504) {
        return Err(RefreshDisposition::Retry(KimiAuthError::HttpStatus(status)));
    }
    Err(RefreshDisposition::Fatal(KimiAuthError::HttpStatus(status)))
}

/// Whether one refresh attempt should be retried or surfaced.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RefreshDisposition {
    Retry(KimiAuthError),
    Fatal(KimiAuthError),
}

/// Refresh with upstream retry semantics: up to 3 attempts, exponential
/// backoff on 429/5xx, immediate failure on 401/403/`invalid_grant`.
///
/// The caller (`ensure_fresh_provider_token`) persists the returned
/// credentials — including the **rotated** refresh token — atomically.
pub async fn refresh_with<H, C>(
    http: &H,
    clock: &C,
    refresh_token: &str,
) -> Result<OAuthCredentials, KimiAuthError>
where
    H: KimiHttp,
    C: KimiClock,
{
    if refresh_token.trim().is_empty() {
        return Err(KimiAuthError::Unauthorized);
    }
    validate_oauth_endpoint(TOKEN_URL)?;
    let never_cancelled = AtomicBool::new(false);
    let mut last_err = KimiAuthError::Transport;
    for attempt in 0..REFRESH_MAX_ATTEMPTS {
        if attempt > 0 {
            let backoff_ms = REFRESH_BACKOFF_BASE_MS.saturating_mul(1 << (attempt - 1));
            clock
                .sleep_cancellable(Duration::from_millis(backoff_ms), &never_cancelled)
                .await?;
        }
        let resp = match http
            .post_form(
                TOKEN_URL,
                &[
                    ("client_id", CLIENT_ID),
                    ("grant_type", "refresh_token"),
                    ("refresh_token", refresh_token),
                ],
            )
            .await
        {
            Ok(resp) => resp,
            Err(err) => {
                // Transport-level failures are retryable (upstream parity).
                last_err = err;
                continue;
            }
        };
        match classify_refresh_response(resp.status, &resp.body) {
            Ok(token) => return Ok(credentials_from_token(token, clock.now_millis())),
            Err(RefreshDisposition::Retry(err)) => {
                last_err = err;
            }
            Err(RefreshDisposition::Fatal(err)) => return Err(err),
        }
    }
    Err(last_err)
}

// ── Full login (injectable) ──────────────────────────────────────────────────

pub type UserCodeHook<'a> = &'a dyn Fn(&str, &str);

pub struct LoginHooks<'a, B> {
    pub browser: &'a B,
    /// When set, the user_code + verification URI are written here (tests / TUI).
    pub on_user_code: Option<UserCodeHook<'a>>,
}

/// Device start → user prompt → poll → atomic persist.
pub async fn login_with<H, C, B, X>(
    http: &H,
    clock: &C,
    hooks: LoginHooks<'_, B>,
    cancel: &X,
    persist: bool,
) -> Result<OAuthCredentials, KimiAuthError>
where
    H: KimiHttp,
    C: KimiClock,
    B: KimiBrowser,
    X: KimiCancel + ?Sized,
{
    let authz = start_device_authorization(http, clock).await?;
    if let Some(cb) = hooks.on_user_code {
        cb(&authz.user_code, &authz.verification_uri_complete);
    } else {
        eprintln!("\n\x1b[1mKimi Code device login\x1b[0m");
        eprintln!("  Confirm code: \x1b[36m{}\x1b[0m", authz.user_code);
        eprintln!(
            "  At:           \x1b[36m{}\x1b[0m\n",
            authz.verification_uri_complete
        );
    }
    // Open only the validated verification URI (never an arbitrary redirect).
    validate_verification_uri(&authz.verification_uri_complete)?;
    let _ = hooks.browser.open(&authz.verification_uri_complete);

    let token = wait_for_device_authorization(http, clock, &authz, cancel).await?;
    let creds = credentials_from_token(token, clock.now_millis());
    if persist {
        save_provider_auth(PROVIDER, &creds).map_err(|_| KimiAuthError::Persist)?;
    }
    Ok(creds)
}

/// Production login entry (real network + system browser + auth.json).
pub async fn login() -> Result<OAuthCredentials, String> {
    let http = ProductionHttp::new().map_err(|e| e.to_string())?;
    let clock = ProductionClock;
    let browser = ProductionBrowser;
    let cancel = AtomicBool::new(false);
    login_with(
        &http,
        &clock,
        LoginHooks {
            browser: &browser,
            on_user_code: None,
        },
        &cancel,
        true,
    )
    .await
    .map_err(|e| e.to_string())
}

/// Broker refresh: rotate the token pair (no browser). The caller persists
/// the rotated credentials via the provider single-flight gate.
pub async fn refresh_token(_client: &Client, refresh: &str) -> Result<OAuthCredentials, String> {
    let http = ProductionHttp::new().map_err(|e| e.to_string())?;
    let clock = ProductionClock;
    refresh_with(&http, &clock, refresh)
        .await
        .map_err(|e| e.to_string())
}

// ── Production adapters ──────────────────────────────────────────────────────

struct ProductionHttp {
    /// No-redirect client with connect + request timeouts for secret-bearing calls.
    client: Client,
    /// Stable device id resolved once per flow.
    device_id: String,
}

impl ProductionHttp {
    fn new() -> Result<Self, KimiAuthError> {
        let client = Client::builder()
            .redirect(reqwest::redirect::Policy::none())
            .connect_timeout(CONNECT_TIMEOUT)
            .timeout(REQUEST_TIMEOUT)
            .build()
            .map_err(|_| KimiAuthError::Transport)?;
        Ok(Self {
            client,
            device_id: load_or_create_device_id(),
        })
    }
}

#[async_trait]
impl KimiHttp for ProductionHttp {
    async fn post_form(
        &self,
        url: &str,
        form: &[(&str, &str)],
    ) -> Result<InjectedHttpResponse, KimiAuthError> {
        validate_oauth_endpoint(url)?;
        let mut req = self.client.post(url);
        for (name, value) in identity_request_headers(&self.device_id) {
            req = req.header(name, value);
        }
        let response = req
            .form(form)
            .send()
            .await
            .map_err(|_| KimiAuthError::Transport)?;
        let status = response.status().as_u16();
        // Redirects are disabled; a 3xx means fail closed without following.
        if (300..400).contains(&status) {
            return Err(KimiAuthError::UntrustedEndpoint);
        }
        let body = read_body_capped(response, MAX_RESPONSE_BODY_BYTES).await?;
        Ok(InjectedHttpResponse { status, body })
    }
}

struct ProductionClock;

#[async_trait]
impl KimiClock for ProductionClock {
    fn now_millis(&self) -> u64 {
        now_millis()
    }

    async fn sleep_cancellable<X: KimiCancel + ?Sized>(
        &self,
        duration: Duration,
        cancel: &X,
    ) -> Result<(), KimiAuthError> {
        let mut remaining = duration;
        while remaining > Duration::ZERO {
            if cancel.is_cancelled() {
                return Err(KimiAuthError::Cancelled);
            }
            let chunk = if remaining > CANCEL_POLL_QUANTUM {
                CANCEL_POLL_QUANTUM
            } else {
                remaining
            };
            tokio::time::sleep(chunk).await;
            remaining = remaining.saturating_sub(chunk);
        }
        if cancel.is_cancelled() {
            return Err(KimiAuthError::Cancelled);
        }
        Ok(())
    }
}

struct ProductionBrowser;

impl KimiBrowser for ProductionBrowser {
    fn open(&self, url: &str) -> Result<(), KimiAuthError> {
        validate_verification_uri(url)?;
        let _ = open_browser(url);
        Ok(())
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

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

    // ── Test doubles ─────────────────────────────────────────────────────────

    #[derive(Default)]
    struct FakeClock {
        now: Mutex<u64>,
        sleeps: Mutex<Vec<u64>>,
    }

    impl FakeClock {
        fn new(now: u64) -> Self {
            Self {
                now: Mutex::new(now),
                sleeps: Mutex::new(Vec::new()),
            }
        }
    }

    #[async_trait]
    impl KimiClock for FakeClock {
        fn now_millis(&self) -> u64 {
            *self.now.lock().unwrap()
        }
        async fn sleep_cancellable<X: KimiCancel + ?Sized>(
            &self,
            duration: Duration,
            cancel: &X,
        ) -> Result<(), KimiAuthError> {
            if cancel.is_cancelled() {
                return Err(KimiAuthError::Cancelled);
            }
            let ms = duration.as_millis() as u64;
            self.sleeps.lock().unwrap().push(ms);
            *self.now.lock().unwrap() += ms;
            Ok(())
        }
    }

    /// Scripted HTTP: pops responses front-to-back, recording each form body.
    type RecordedForm = (String, Vec<(String, String)>);

    #[derive(Default)]
    struct FakeHttp {
        responses: Mutex<Vec<InjectedHttpResponse>>,
        requests: Mutex<Vec<RecordedForm>>,
    }

    impl FakeHttp {
        fn scripted(responses: Vec<InjectedHttpResponse>) -> Self {
            Self {
                responses: Mutex::new(responses),
                requests: Mutex::new(Vec::new()),
            }
        }
    }

    #[async_trait]
    impl KimiHttp for FakeHttp {
        async fn post_form(
            &self,
            url: &str,
            form: &[(&str, &str)],
        ) -> Result<InjectedHttpResponse, KimiAuthError> {
            self.requests.lock().unwrap().push((
                url.to_string(),
                form.iter()
                    .map(|(k, v)| (k.to_string(), v.to_string()))
                    .collect(),
            ));
            let mut responses = self.responses.lock().unwrap();
            if responses.is_empty() {
                return Err(KimiAuthError::Transport);
            }
            Ok(responses.remove(0))
        }
    }

    struct NoopBrowser;
    impl KimiBrowser for NoopBrowser {
        fn open(&self, _url: &str) -> Result<(), KimiAuthError> {
            Ok(())
        }
    }

    fn ok(body: &str) -> InjectedHttpResponse {
        InjectedHttpResponse {
            status: 200,
            body: body.to_string(),
        }
    }

    fn err(status: u16, body: &str) -> InjectedHttpResponse {
        InjectedHttpResponse {
            status,
            body: body.to_string(),
        }
    }

    const DEVICE_OK: &str = r#"{
        "device_code": "dev-123",
        "user_code": "ABCD-EFGH",
        "verification_uri": "https://www.kimi.com/code/device",
        "verification_uri_complete": "https://www.kimi.com/code/device?user_code=ABCD-EFGH",
        "expires_in": 300,
        "interval": 5
    }"#;

    const TOKEN_OK: &str = r#"{
        "access_token": "acc-1",
        "refresh_token": "ref-1",
        "expires_in": 900,
        "scope": "kimi-code",
        "token_type": "Bearer"
    }"#;

    // ── Endpoint pinning ─────────────────────────────────────────────────────

    #[test]
    fn oauth_endpoint_pinning_accepts_only_exact_urls() {
        assert!(validate_oauth_endpoint(DEVICE_AUTH_URL).is_ok());
        assert!(validate_oauth_endpoint(TOKEN_URL).is_ok());
        for bad in [
            "https://auth.kimi.com/api/oauth/other",
            "http://auth.kimi.com/api/oauth/token",
            "https://auth.kimi.com.evil.example/api/oauth/token",
            "https://api.kimi.com/api/oauth/token",
        ] {
            assert_eq!(
                validate_oauth_endpoint(bad),
                Err(KimiAuthError::UntrustedEndpoint),
                "must reject {bad}"
            );
        }
    }

    #[test]
    fn verification_uri_pinning() {
        assert!(validate_verification_uri("https://www.kimi.com/code/device?user_code=X").is_ok());
        assert!(validate_verification_uri("https://kimi.com/device").is_ok());
        for bad in [
            "http://www.kimi.com/device",
            "https://kimi.com.evil.example/device",
            "https://evilkimi.com/device",
            "https://user@www.kimi.com/device",
            "https://www.kimi.com:8443/device",
            "https://www.kimi.com/device#frag",
        ] {
            assert_eq!(
                validate_verification_uri(bad),
                Err(KimiAuthError::UntrustedEndpoint),
                "must reject {bad}"
            );
        }
    }

    // ── Parsing ──────────────────────────────────────────────────────────────

    #[test]
    fn parses_device_code_response() {
        let authz = parse_device_code_response(DEVICE_OK, 1_000).expect("parse");
        assert_eq!(authz.user_code, "ABCD-EFGH");
        assert_eq!(authz.device_code, "dev-123");
        assert_eq!(authz.expires_in_secs, 300);
        assert_eq!(authz.interval_secs, 5);
        assert_eq!(authz.issued_at_ms, 1_000);
        assert!(authz.verification_uri_complete.contains("user_code"));
    }

    #[test]
    fn device_response_defaults_missing_expires_and_interval() {
        let body = r#"{
            "device_code": "d",
            "user_code": "u",
            "verification_uri_complete": "https://www.kimi.com/code"
        }"#;
        let authz = parse_device_code_response(body, 0).expect("parse");
        assert_eq!(authz.expires_in_secs, DEFAULT_DEVICE_EXPIRES_IN_SECS);
        assert_eq!(authz.interval_secs, DEFAULT_POLL_INTERVAL_SECS);
        assert_eq!(authz.verification_uri, "");
    }

    #[test]
    fn device_response_requires_complete_uri_and_codes() {
        for body in [
            r#"{"device_code":"", "user_code":"u", "verification_uri_complete":"https://kimi.com/x"}"#,
            r#"{"device_code":"d", "user_code":" ", "verification_uri_complete":"https://kimi.com/x"}"#,
            r#"{"device_code":"d", "user_code":"u", "verification_uri_complete":""}"#,
            r#"{"device_code":"d", "user_code":"u"}"#,
            "not-json",
        ] {
            assert!(parse_device_code_response(body, 0).is_err(), "body: {body}");
        }
    }

    #[test]
    fn device_response_rejects_foreign_verification_host() {
        let body = r#"{
            "device_code": "d",
            "user_code": "u",
            "verification_uri_complete": "https://phish.example/kimi"
        }"#;
        assert_eq!(
            parse_device_code_response(body, 0).unwrap_err(),
            KimiAuthError::UntrustedEndpoint
        );
    }

    #[test]
    fn poll_outcomes_map_oauth_error_codes() {
        for (code, expected) in [
            ("authorization_pending", DevicePollOutcome::Pending),
            ("slow_down", DevicePollOutcome::SlowDown),
            ("access_denied", DevicePollOutcome::Denied),
            ("expired_token", DevicePollOutcome::Expired),
            ("some_other", DevicePollOutcome::OtherError),
        ] {
            let body = format!(r#"{{"error":"{code}"}}"#);
            assert_eq!(parse_device_poll_response(&body).unwrap(), expected);
        }
    }

    #[test]
    fn poll_success_requires_rotating_refresh_and_expiry() {
        let ok = parse_device_poll_response(TOKEN_OK).unwrap();
        match ok {
            DevicePollOutcome::Authorized { token } => {
                assert_eq!(token.access_token, "acc-1");
                assert_eq!(token.refresh_token, "ref-1");
                assert_eq!(token.expires_in_secs, 900);
            }
            other => panic!("expected Authorized, got {other:?}"),
        }
        // Missing refresh_token → invalid (Kimi always rotates).
        assert!(parse_device_poll_response(r#"{"access_token":"a","expires_in":900}"#).is_err());
        // Missing/zero expires_in → invalid (upstream contract).
        assert!(parse_device_poll_response(r#"{"access_token":"a","refresh_token":"r"}"#).is_err());
        assert!(parse_device_poll_response(
            r#"{"access_token":"a","refresh_token":"r","expires_in":0}"#
        )
        .is_err());
    }

    #[test]
    fn credentials_bake_upstream_refresh_margin() {
        // 900s token, margin = max(300, 450) = 450s → expires = now + 450s.
        let creds = credentials_from_token(
            KimiTokenSet {
                access_token: "a".into(),
                refresh_token: "r".into(),
                expires_in_secs: 900,
            },
            1_000_000,
        );
        assert_eq!(creds.expires, 1_000_000 + 450 * 1000);
        assert_eq!(creds.auth_type, "oauth");
        // Short-lived token: margin must leave headroom, not zero the expiry.
        let short = credentials_from_token(
            KimiTokenSet {
                access_token: "a".into(),
                refresh_token: "r".into(),
                expires_in_secs: 120,
            },
            0,
        );
        assert_eq!(short.expires, (120 - 60) * 1000);
    }

    #[test]
    fn slow_down_grows_and_caps() {
        assert_eq!(apply_slow_down(5), 10);
        assert_eq!(apply_slow_down(28), 30);
        assert_eq!(apply_slow_down(30), 30);
    }

    // ── Refresh classification ───────────────────────────────────────────────

    #[test]
    fn refresh_classification_matrix() {
        assert!(classify_refresh_response(200, TOKEN_OK).is_ok());
        assert_eq!(
            classify_refresh_response(401, "{}").unwrap_err(),
            RefreshDisposition::Fatal(KimiAuthError::Unauthorized)
        );
        assert_eq!(
            classify_refresh_response(403, "{}").unwrap_err(),
            RefreshDisposition::Fatal(KimiAuthError::Unauthorized)
        );
        assert_eq!(
            classify_refresh_response(400, r#"{"error":"invalid_grant"}"#).unwrap_err(),
            RefreshDisposition::Fatal(KimiAuthError::Unauthorized)
        );
        for status in [429u16, 500, 502, 503, 504] {
            assert_eq!(
                classify_refresh_response(status, "{}").unwrap_err(),
                RefreshDisposition::Retry(KimiAuthError::HttpStatus(status)),
                "status {status} must be retryable"
            );
        }
        assert_eq!(
            classify_refresh_response(418, "{}").unwrap_err(),
            RefreshDisposition::Fatal(KimiAuthError::HttpStatus(418))
        );
        // 200 with garbage body must not be accepted.
        assert_eq!(
            classify_refresh_response(200, "{}").unwrap_err(),
            RefreshDisposition::Fatal(KimiAuthError::InvalidTokenResponse)
        );
    }

    // ── State machine (async, deterministic) ─────────────────────────────────

    #[tokio::test]
    async fn login_happy_path_persist_disabled() {
        let http = FakeHttp::scripted(vec![
            ok(DEVICE_OK),
            err(400, r#"{"error":"authorization_pending"}"#),
            ok(TOKEN_OK),
        ]);
        let clock = FakeClock::new(0);
        let cancel = AtomicBool::new(false);
        let creds = login_with(
            &http,
            &clock,
            LoginHooks {
                browser: &NoopBrowser,
                on_user_code: Some(&|code, uri| {
                    assert_eq!(code, "ABCD-EFGH");
                    assert!(uri.starts_with("https://www.kimi.com/"));
                }),
            },
            &cancel,
            false,
        )
        .await
        .expect("login");
        assert_eq!(creds.access, "acc-1");
        assert_eq!(creds.refresh, "ref-1");

        let requests = http.requests.lock().unwrap();
        assert_eq!(requests.len(), 3);
        assert_eq!(requests[0].0, DEVICE_AUTH_URL);
        assert_eq!(requests[1].0, TOKEN_URL);
        // Poll must carry the device grant type + device code.
        let poll_form = &requests[1].1;
        assert!(poll_form.contains(&("grant_type".into(), DEVICE_GRANT_TYPE.into())));
        assert!(poll_form.contains(&("device_code".into(), "dev-123".into())));
    }

    #[tokio::test]
    async fn slow_down_extends_poll_interval() {
        let http = FakeHttp::scripted(vec![
            ok(DEVICE_OK),
            err(400, r#"{"error":"slow_down"}"#),
            ok(TOKEN_OK),
        ]);
        let clock = FakeClock::new(0);
        let cancel = AtomicBool::new(false);
        let authz = start_device_authorization(&http, &clock).await.unwrap();
        wait_for_device_authorization(&http, &clock, &authz, &cancel)
            .await
            .expect("authorized");
        let sleeps = clock.sleeps.lock().unwrap();
        assert_eq!(*sleeps, vec![5_000, 10_000]);
    }

    #[tokio::test]
    async fn denied_and_expired_terminate() {
        for (body, expected) in [
            (r#"{"error":"access_denied"}"#, KimiAuthError::AccessDenied),
            (r#"{"error":"expired_token"}"#, KimiAuthError::Expired),
        ] {
            let http = FakeHttp::scripted(vec![ok(DEVICE_OK), err(400, body)]);
            let clock = FakeClock::new(0);
            let cancel = AtomicBool::new(false);
            let authz = start_device_authorization(&http, &clock).await.unwrap();
            let got = wait_for_device_authorization(&http, &clock, &authz, &cancel)
                .await
                .unwrap_err();
            assert_eq!(got, expected);
        }
    }

    #[tokio::test]
    async fn deadline_expires_without_server_grant() {
        // expires_in 300s; each pending poll advances 5s of fake time.
        let mut responses = vec![ok(DEVICE_OK)];
        for _ in 0..70 {
            responses.push(err(400, r#"{"error":"authorization_pending"}"#));
        }
        let http = FakeHttp::scripted(responses);
        let clock = FakeClock::new(0);
        let cancel = AtomicBool::new(false);
        let authz = start_device_authorization(&http, &clock).await.unwrap();
        let got = wait_for_device_authorization(&http, &clock, &authz, &cancel)
            .await
            .unwrap_err();
        assert_eq!(got, KimiAuthError::Expired);
    }

    #[tokio::test]
    async fn cancellation_stops_polling() {
        let http = FakeHttp::scripted(vec![ok(DEVICE_OK)]);
        let clock = FakeClock::new(0);
        let cancel = AtomicBool::new(true);
        let authz = start_device_authorization(&http, &clock).await.unwrap();
        let got = wait_for_device_authorization(&http, &clock, &authz, &cancel)
            .await
            .unwrap_err();
        assert_eq!(got, KimiAuthError::Cancelled);
    }

    #[tokio::test]
    async fn server_5xx_during_poll_is_fatal() {
        let http = FakeHttp::scripted(vec![ok(DEVICE_OK), err(502, "bad gateway")]);
        let clock = FakeClock::new(0);
        let cancel = AtomicBool::new(false);
        let authz = start_device_authorization(&http, &clock).await.unwrap();
        let got = wait_for_device_authorization(&http, &clock, &authz, &cancel)
            .await
            .unwrap_err();
        assert_eq!(got, KimiAuthError::HttpStatus(502));
    }

    #[tokio::test]
    async fn refresh_retries_transient_then_succeeds() {
        let http = FakeHttp::scripted(vec![err(503, "{}"), err(429, "{}"), ok(TOKEN_OK)]);
        let clock = FakeClock::new(0);
        let creds = refresh_with(&http, &clock, "old-refresh")
            .await
            .expect("refresh");
        assert_eq!(creds.access, "acc-1");
        // Rotated refresh token must replace the old one.
        assert_eq!(creds.refresh, "ref-1");
        // Backoff: 1s then 2s.
        assert_eq!(*clock.sleeps.lock().unwrap(), vec![1_000, 2_000]);
        // Refresh form carries the refresh grant, never the device grant.
        let requests = http.requests.lock().unwrap();
        for (_, form) in requests.iter() {
            assert!(form.contains(&("grant_type".into(), "refresh_token".into())));
        }
    }

    #[tokio::test]
    async fn refresh_unauthorized_is_immediate_no_retry() {
        let http = FakeHttp::scripted(vec![err(401, "{}"), ok(TOKEN_OK)]);
        let clock = FakeClock::new(0);
        let got = refresh_with(&http, &clock, "dead-refresh")
            .await
            .unwrap_err();
        assert_eq!(got, KimiAuthError::Unauthorized);
        // Exactly one request — the scripted 200 must remain unconsumed.
        assert_eq!(http.requests.lock().unwrap().len(), 1);
    }

    #[tokio::test]
    async fn refresh_exhausts_retries() {
        let http = FakeHttp::scripted(vec![err(500, "{}"), err(500, "{}"), err(500, "{}")]);
        let clock = FakeClock::new(0);
        let got = refresh_with(&http, &clock, "r").await.unwrap_err();
        assert_eq!(got, KimiAuthError::HttpStatus(500));
        assert_eq!(http.requests.lock().unwrap().len(), 3);
    }

    #[tokio::test]
    async fn refresh_rejects_empty_refresh_token() {
        let http = FakeHttp::scripted(vec![ok(TOKEN_OK)]);
        let clock = FakeClock::new(0);
        let got = refresh_with(&http, &clock, "  ").await.unwrap_err();
        assert_eq!(got, KimiAuthError::Unauthorized);
        assert!(http.requests.lock().unwrap().is_empty());
    }

    // ── Identity headers ─────────────────────────────────────────────────────

    #[test]
    fn identity_headers_cover_msh_surface_without_secrets() {
        let headers = identity_request_headers("4d2b0725-9e2e-4b8f-b57b-6011ecf1c3eb");
        let names: Vec<&str> = headers.iter().map(|(name, _)| *name).collect();
        for required in [
            "User-Agent",
            "X-Msh-Platform",
            "X-Msh-Version",
            "X-Msh-Device-Id",
            "X-Msh-Device-Name",
            "X-Msh-Device-Model",
            "X-Msh-Os-Version",
        ] {
            assert!(names.contains(&required), "missing header {required}");
        }
        let get = |name: &str| {
            headers
                .iter()
                .find(|(n, _)| *n == name)
                .map(|(_, v)| v.clone())
                .unwrap()
        };
        assert_eq!(get("X-Msh-Platform"), MSH_PLATFORM);
        assert_eq!(get("User-Agent"), USER_AGENT);
        assert_eq!(
            get("X-Msh-Device-Id"),
            "4d2b0725-9e2e-4b8f-b57b-6011ecf1c3eb"
        );
        for (_, value) in &headers {
            assert!(
                value.chars().all(|c| (' '..='~').contains(&c)),
                "header values must be printable ASCII"
            );
        }
    }

    #[test]
    fn ascii_header_sanitizes_and_falls_back() {
        assert_eq!(ascii_header("  héllo wörld  ", "x"), "hllo wrld");
        assert_eq!(ascii_header("\u{7f}\n\t", "fallback"), "fallback");
        assert_eq!(ascii_header("plain", "x"), "plain");
    }

    #[test]
    fn error_display_never_contains_token_material() {
        // Compile-time shape guarantee: no variant stores a String, so no
        // token can transit. Spot-check Display output anyway.
        for err in [
            KimiAuthError::Unauthorized,
            KimiAuthError::InvalidTokenResponse,
            KimiAuthError::HttpStatus(500),
        ] {
            let text = format!("{err} / {err:?}");
            assert!(!text.contains("acc-"), "{text}");
            assert!(!text.contains("ref-"), "{text}");
        }
    }
}