link-assistant-router 1.10.1

Link.Assistant.Router — Claude MAX OAuth proxy and token gateway for Anthropic APIs
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
use super::{
    CredentialEvidence, Exchange, REFRESH_SKEW_MS, ROTATION_ATTRIBUTION_MS, RecoveryMode, Rejected,
    SubscriptionKey, SubscriptionProvider, SubscriptionToken, TokenCache, exchange_with_recovery,
    refresh_recovery, refresh_state,
};

/// Import errors must classify the failure without echoing token-endpoint
/// bodies, which can contain credential or account material.
fn safe_import_refresh_error(
    provider: SubscriptionProvider,
    error: &super::RefreshError,
) -> super::ImportRefreshFailure {
    let kind = match error {
        super::RefreshError::NoRefreshToken | super::RefreshError::Unsupported => {
            super::ImportRefreshFailureKind::NotAttempted
        }
        error if error.is_invalid_grant() => super::ImportRefreshFailureKind::ExchangeRejected,
        super::RefreshError::Storage(_) => super::ImportRefreshFailureKind::PersistenceUncertain,
        super::RefreshError::Request(_)
        | super::RefreshError::Status(_, _, _)
        | super::RefreshError::Parse(_) => super::ImportRefreshFailureKind::ExchangeUncertain,
    };
    let message = match error {
        super::RefreshError::NoRefreshToken => {
            format!("the {provider} candidate is not refreshable and was not accepted")
        }
        error if error.is_invalid_grant() => {
            let class = error
                .status_class()
                .map_or("terminal_oauth_error", super::RefreshStatusClass::label);
            format!("the {provider} candidate refresh chain was rejected ({class})")
        }
        super::RefreshError::Status(code, _, _) => format!(
            "the {provider} candidate refresh chain was not verified (token endpoint HTTP {code})"
        ),
        super::RefreshError::Request(_) => format!(
            "the {provider} candidate refresh chain was not verified (token endpoint unavailable)"
        ),
        super::RefreshError::Parse(_) => format!(
            "the {provider} candidate refresh chain was not verified (invalid token response)"
        ),
        super::RefreshError::Storage(_) => {
            format!("the {provider} candidate refresh result could not be durably persisted")
        }
        super::RefreshError::Unsupported => {
            format!("the {provider} candidate refresh chain cannot be validated")
        }
    };
    super::ImportRefreshFailure::new(kind, message)
}

impl TokenCache {
    /// Create an empty cache.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Return a non-expired token for `provider`, refreshing if needed.
    ///
    /// Resolution order:
    /// 1. If the on-disk token is still valid, use it (the vendor CLI may have
    ///    refreshed it more recently than our cache).
    /// 2. Otherwise reuse a cached refreshed token while it remains valid.
    /// 3. Otherwise exchange the refresh token and cache the result.
    ///
    /// On refresh failure the (expired) disk token is returned unchanged so the
    /// caller can still attempt the upstream call and surface its error.
    pub async fn get_fresh(
        &self,
        client: &reqwest::Client,
        provider: SubscriptionProvider,
        disk_token: SubscriptionToken,
        now_ms: i64,
    ) -> SubscriptionToken {
        self.get_fresh_for(
            client,
            provider,
            crate::credential_recovery_store::PRIMARY_ACCOUNT,
            disk_token,
            now_ms,
        )
        .await
    }

    /// Account-scoped variant of [`Self::get_fresh`].
    pub async fn get_fresh_for(
        &self,
        client: &reqwest::Client,
        provider: SubscriptionProvider,
        account: &str,
        disk_token: SubscriptionToken,
        now_ms: i64,
    ) -> SubscriptionToken {
        let token_url = super::refresh_token_url(provider);
        self.get_fresh_for_at(client, &token_url, provider, account, disk_token, now_ms)
            .await
    }

    /// Load one registered credential authoritatively, then refresh it if
    /// necessary after releasing the durable load lock.
    ///
    /// Unlike [`Self::get_fresh_for`], storage uncertainty is returned to the
    /// caller instead of being collapsed into a caller-provided fallback.
    pub async fn get_fresh_registered(
        &self,
        client: &reqwest::Client,
        provider: SubscriptionProvider,
        account: &str,
        now_ms: i64,
    ) -> Result<SubscriptionToken, String> {
        let token_url = super::refresh_token_url(provider);
        self.get_fresh_registered_at(client, &token_url, provider, account, now_ms)
            .await
    }

    /// Prove that a registered credential's refresh chain can advance.
    ///
    /// Unlike ordinary pre-flight refresh, this always exchanges the refresh
    /// token even when the current access token is still live. It uses only the
    /// direct OAuth recovery rungs, persists the complete result through the
    /// registered durable store, and returns no token until that persistence
    /// succeeds. Credential import uses this against an isolated candidate
    /// store so a stale refresh link can never replace a working destination
    /// merely because its access token still passes a catalog probe (#385).
    pub async fn validate_refresh_chain_registered(
        &self,
        client: &reqwest::Client,
        provider: SubscriptionProvider,
        account: &str,
        now_ms: i64,
    ) -> Result<SubscriptionToken, String> {
        self.validate_refresh_chain_registered_classified(client, provider, account, now_ms)
            .await
            .map_err(|error| error.to_string())
    }

    /// Classified variant used by machine-readable credential import.
    pub async fn validate_refresh_chain_registered_classified(
        &self,
        client: &reqwest::Client,
        provider: SubscriptionProvider,
        account: &str,
        now_ms: i64,
    ) -> Result<SubscriptionToken, super::ImportRefreshFailure> {
        let token_url = super::refresh_token_url(provider);
        self.validate_refresh_chain_registered_at_classified(
            client, &token_url, provider, account, now_ms,
        )
        .await
    }

    /// Endpoint-overridable variant used by end-to-end import tests.
    #[doc(hidden)]
    pub async fn validate_refresh_chain_registered_at(
        &self,
        client: &reqwest::Client,
        token_url: &str,
        provider: SubscriptionProvider,
        account: &str,
        now_ms: i64,
    ) -> Result<SubscriptionToken, String> {
        self.validate_refresh_chain_registered_at_classified(
            client, token_url, provider, account, now_ms,
        )
        .await
        .map_err(|error| error.to_string())
    }

    /// Endpoint-overridable classified variant used by import contract tests.
    #[doc(hidden)]
    pub async fn validate_refresh_chain_registered_at_classified(
        &self,
        client: &reqwest::Client,
        token_url: &str,
        provider: SubscriptionProvider,
        account: &str,
        now_ms: i64,
    ) -> Result<SubscriptionToken, super::ImportRefreshFailure> {
        let not_attempted = |message| {
            super::ImportRefreshFailure::new(super::ImportRefreshFailureKind::NotAttempted, message)
        };
        let persistence_uncertain = |message| {
            super::ImportRefreshFailure::new(
                super::ImportRefreshFailureKind::PersistenceUncertain,
                message,
            )
        };
        let baseline = self
            .load_authoritative(provider, account)
            .await
            .map_err(not_attempted)?
            .ok_or_else(|| {
                not_attempted(format!("the {provider} candidate credential is absent"))
            })?;
        if baseline.refresh_token.as_deref().is_none_or(str::is_empty) {
            return Err(not_attempted(format!(
                "the {provider} candidate is not refreshable and was not accepted"
            )));
        }
        let attempt = self.attempts.for_subscription(provider, account, &baseline);
        let mut attempt = attempt.lock().await;
        if attempt.reset_if_changed(provider, &baseline) {
            self.forget(&(provider, account.to_string()), provider);
        }
        let store = self
            .store_for_subscription(provider, account)
            .ok_or_else(|| {
                not_attempted(format!(
                    "no durable {provider} candidate store is registered"
                ))
            })?;
        let exchange = Exchange {
            client,
            token_url,
            provider,
            now_ms,
            mode: RecoveryMode::ImportValidation,
        };
        let fresh = exchange_with_recovery(&exchange, Some(&store), None, &baseline)
            .await
            .map_err(|rejection| safe_import_refresh_error(provider, &rejection.error))?
            .token;
        self.accept(&mut attempt, provider, account, &fresh, Some(now_ms));
        drop(attempt);

        // Reread what was durably committed rather than trusting the exchange's
        // in-memory return value. This is the credential later catalog-tested
        // and promoted.
        let durable = self
            .load_authoritative(provider, account)
            .await
            .map_err(persistence_uncertain)?
            .ok_or_else(|| {
                persistence_uncertain(format!(
                    "the durable {provider} candidate disappeared after refresh"
                ))
            })?;
        if fresh.access_token != durable.access_token
            || fresh.refresh_token != durable.refresh_token
        {
            return Err(persistence_uncertain(format!(
                "the durable {provider} candidate did not retain the validated refresh result"
            )));
        }
        Ok(durable)
    }

    /// Testable token-endpoint variant of [`Self::get_fresh_registered`].
    ///
    /// The registered durable store remains authoritative; only the remote
    /// endpoint is replaceable so diagnostics can exercise real transaction
    /// failures without contacting a vendor.
    pub(crate) async fn get_fresh_registered_at(
        &self,
        client: &reqwest::Client,
        token_url: &str,
        provider: SubscriptionProvider,
        account: &str,
        now_ms: i64,
    ) -> Result<SubscriptionToken, String> {
        let disk_token = self
            .load_authoritative(provider, account)
            .await?
            .ok_or_else(|| {
                format!("failed to load {provider} credentials from the registered store")
            })?;
        self.get_fresh_for_at_checked(client, token_url, provider, account, disk_token, now_ms)
            .await
    }

    /// Refresh a baseline that was just returned by [`Self::load_authoritative`].
    ///
    /// This avoids a redundant second locked reload while preserving the rule
    /// that no caller-provided raw credential can bypass the registered store.
    pub(crate) async fn get_fresh_loaded(
        &self,
        client: &reqwest::Client,
        provider: SubscriptionProvider,
        account: &str,
        disk_token: SubscriptionToken,
        now_ms: i64,
    ) -> Result<SubscriptionToken, String> {
        let token_url = super::refresh_token_url(provider);
        self.get_fresh_for_at_checked(client, &token_url, provider, account, disk_token, now_ms)
            .await
    }

    /// Refresh regardless of what the token's own `exp` claim says.
    ///
    /// A vendor may invalidate an access token *before* its stated expiry —
    /// a plan change, a session reset — and answers `401` to a token whose
    /// `exp` is still days away (issue #205). `exp` is therefore only ever an
    /// optimisation for refreshing early; a `401` from the resource endpoint
    /// is the authority on whether a token is actually accepted.
    ///
    /// Returns `None` when no fresher token could be obtained, so a caller can
    /// tell "retry with this" from "there is nothing new to retry with" and
    /// avoid replaying a request with the credential that just failed.
    pub async fn refresh_rejected(
        &self,
        client: &reqwest::Client,
        provider: SubscriptionProvider,
        account: &str,
        disk_token: SubscriptionToken,
        now_ms: i64,
    ) -> Option<SubscriptionToken> {
        let token_url = super::refresh_token_url(provider);
        self.refresh_rejected_at(client, &token_url, provider, account, disk_token, now_ms)
            .await
    }

    pub(crate) async fn refresh_rejected_at(
        &self,
        client: &reqwest::Client,
        token_url: &str,
        provider: SubscriptionProvider,
        account: &str,
        rejected: SubscriptionToken,
        now_ms: i64,
    ) -> Option<SubscriptionToken> {
        let attempt = self.attempts.for_subscription(provider, account, &rejected);
        // The same lock the pre-flight path uses, so concurrent 401s share one
        // exchange instead of each spending the refresh token.
        let mut attempt = attempt.lock().await;

        // A concurrent caller may have refreshed while we waited for the lock.
        // Anything that differs from the token just rejected is worth retrying.
        if let Some(cached) = self.cached_valid_for(provider, account, now_ms)
            && cached.access_token != rejected.access_token
        {
            return Some(cached);
        }
        // A credential this process rotated into moments ago is not evidence
        // of anything yet. Refreshing it spends the only good link of a
        // single-use chain against a verdict that was never about the token
        // (issue #319).
        if let Some(age_ms) = attempt.rotated_within(now_ms, refresh_state::ROTATION_GRACE_MS) {
            tracing::warn!(
                "not refreshing the {provider} credential: this process rotated into it \
                 {}s ago and a refresh chain is single-use, so spending it again would \
                 destroy a token that was known good; retrying the existing one",
                age_ms / 1_000
            );
            return None;
        }
        let base = match self
            .unsuppressed_base(&mut attempt, provider, account, rejected.clone(), now_ms)
            .await
        {
            Ok(Some(base)) => base,
            Ok(None) => return None,
            Err(error) => {
                self.record_refresh_error_for(provider, account, &error);
                return None;
            }
        };
        let exchange = Exchange {
            client,
            token_url,
            provider,
            now_ms,
            mode: RecoveryMode::AfterRejection,
        };
        let result = match self.climb(&exchange, account, &base).await {
            Ok(fresh) => {
                self.accept(&mut attempt, provider, account, &fresh, Some(now_ms));
                // A refresh that returned the same access token has not
                // recovered anything; replaying would just repeat the 401.
                (fresh.access_token != rejected.access_token).then_some(fresh)
            }
            Err(rejection) => {
                // When this process performed the previous rotation, say so.
                // "revoked or already spent elsewhere" sends the operator
                // looking for an external holder that may not exist, while the
                // fatal spend is recorded right here (issue #319).
                let message = attempt
                    .rotated_within(now_ms, ROTATION_ATTRIBUTION_MS)
                    .map_or_else(
                        || rejection.message.clone(),
                        |age_ms| {
                            format!(
                                "{} — note: this process rotated into that refresh token {}s ago, \
                             so it was spent here rather than by another holder",
                                rejection.message,
                                age_ms / 1_000
                            )
                        },
                    );
                let rejection = Rejected {
                    message,
                    ..rejection
                };
                self.record_refresh_error_for(provider, account, &rejection.message);
                if rejection.error.is_invalid_grant() {
                    // healthy -> permanently unauthenticated is a state
                    // transition that needs a human in a browser and will not
                    // self-heal. Logged at WARN it sat below every sane
                    // `level>=ERROR` pipeline, so a twelve-hour outage produced
                    // a clean log (issue #321).
                    self.log_terminal_once_for(provider, account, &rejection.message);
                    attempt.record_terminal_failure();
                    self.record_credential_rejected_for(provider, account);
                    // Per account as well as per provider: the provider-wide
                    // verdict cannot say *which* account of a pool is dead,
                    // and `accounts list` reports one row each (issue #245).
                    self.record_refresh_refused(provider, account, &base);
                } else {
                    tracing::warn!(
                        "refresh after a rejected {provider} token failed: {}",
                        rejection.message
                    );
                    attempt
                        .record_transient_failure_after(now_ms, rejection.error.retry_after_ms());
                }
                None
            }
        };
        drop(attempt);
        result
    }

    pub(super) async fn get_fresh_for_at(
        &self,
        client: &reqwest::Client,
        token_url: &str,
        provider: SubscriptionProvider,
        account: &str,
        disk_token: SubscriptionToken,
        now_ms: i64,
    ) -> SubscriptionToken {
        let fallback = disk_token.clone();
        match self
            .get_fresh_for_at_checked(client, token_url, provider, account, disk_token, now_ms)
            .await
        {
            Ok(token) => token,
            Err(error) => {
                self.record_refresh_error_for(provider, account, &error);
                fallback
            }
        }
    }

    async fn get_fresh_for_at_checked(
        &self,
        client: &reqwest::Client,
        token_url: &str,
        provider: SubscriptionProvider,
        account: &str,
        disk_token: SubscriptionToken,
        now_ms: i64,
    ) -> Result<SubscriptionToken, String> {
        let key = (provider, account.to_string());
        let attempt = self
            .attempts
            .for_subscription(provider, account, &disk_token);
        let mut attempt = attempt.lock().await;

        // Re-authentication replaces at least one credential field. Forget
        // both negative and positive state derived from the previous file.
        if attempt.reset_if_changed(provider, &disk_token) {
            self.forget(&key, provider);
        }
        // Refresh slightly *before* expiry rather than after a request has
        // already failed: a token that expires mid-flight surfaces to the
        // caller as an upstream error, and a credential only ever refreshed
        // reactively can sit unused past its refresh window (issue #203).
        if !disk_token.is_expired(now_ms.saturating_add(REFRESH_SKEW_MS)) {
            return Ok(disk_token);
        }
        if let Some(cached) = self.cached_valid_for(provider, account, now_ms) {
            return Ok(cached);
        }
        let Some(base) = self
            .unsuppressed_base(&mut attempt, provider, account, disk_token.clone(), now_ms)
            .await?
        else {
            return Ok(disk_token);
        };
        if !base.is_expired(now_ms.saturating_add(REFRESH_SKEW_MS)) {
            // The store was already ahead of the token we were handed; there is
            // nothing to exchange.
            self.accept(&mut attempt, provider, account, &base, None);
            return Ok(base);
        }
        let exchange = Exchange {
            client,
            token_url,
            provider,
            now_ms,
            mode: RecoveryMode::Proactive,
        };
        match self.climb(&exchange, account, &base).await {
            Ok(fresh) => {
                self.accept(&mut attempt, provider, account, &fresh, Some(now_ms));
                drop(attempt);
                Ok(fresh)
            }
            Err(rejection) => {
                self.record_refresh_error_for(provider, account, &rejection.message);
                if rejection.error.is_invalid_grant() {
                    self.log_terminal_once_for(provider, account, &rejection.message);
                    attempt.record_terminal_failure();
                    self.record_credential_rejected_for(provider, account);
                    self.record_refresh_refused(provider, account, &base);
                } else {
                    tracing::warn!(
                        "{}",
                        refresh_recovery::refresh_failure_diagnostic(provider, &rejection.message)
                    );
                    // Everything else is retryable. In particular a 429 must
                    // not record rejection evidence: the credential is fine,
                    // and marking it rejected would drop the provider out of
                    // routing until restart (issue #203).
                    attempt
                        .record_transient_failure_after(now_ms, rejection.error.retry_after_ms());
                }
                drop(attempt);
                // The stamped-expired token is returned unchanged on purpose:
                // it may still be honoured by the inference endpoint.
                Ok(disk_token)
            }
        }
    }

    /// Run the recovery ladder for one subscription.
    async fn climb(
        &self,
        exchange: &Exchange<'_>,
        account: &str,
        base: &SubscriptionToken,
    ) -> Result<SubscriptionToken, refresh_recovery::Rejected> {
        let provider = exchange.provider;
        let store = self.store_for_subscription(provider, account);
        let vendor_cli = self.vendor_cli_for(provider, account);
        exchange_with_recovery(exchange, store.as_ref(), vendor_cli.as_ref(), base)
            .await
            .map(|recovered| {
                if let Ok(mut guard) = self.recoveries.lock() {
                    guard.insert(provider, recovered.rung.describe());
                }
                recovered.token
            })
    }

    /// How `provider`'s credential was last resolved, in the operator's terms.
    #[must_use]
    pub fn last_recovery(&self, provider: SubscriptionProvider) -> Option<&'static str> {
        self.recoveries
            .lock()
            .ok()
            .and_then(|guard| guard.get(&provider).copied())
    }

    /// The credential to exchange, or `None` when a cached failure still
    /// suppresses this subscription.
    ///
    /// A suppressed subscription is not simply skipped: the terminal verdict was
    /// reached about *one* chain link, and another holder may have rotated the
    /// chain forward since. Re-reading the store before honouring the
    /// suppression is what stops a single `invalid_grant` from disabling the
    /// subscription until someone re-authenticates by hand (issue #239).
    async fn unsuppressed_base(
        &self,
        attempt: &mut refresh_state::RefreshAttempt,
        provider: SubscriptionProvider,
        account: &str,
        held: SubscriptionToken,
        now_ms: i64,
    ) -> Result<Option<SubscriptionToken>, String> {
        if !attempt.suppresses_attempt(now_ms) {
            return Ok(Some(held));
        }
        let stored = self
            .reload_registered_store_locked(provider, account)
            .await?
            .ok_or_else(|| {
                format!("failed to load {provider} credentials from the registered store")
            })?;
        if !attempt.reset_if_changed(provider, &stored) {
            return Ok(None);
        }
        tracing::info!(
            "the {provider} credential on disk changed since it was last rejected; retrying with \
             it instead of waiting for a manual re-authentication"
        );
        self.forget(&(provider, account.to_string()), provider);
        Ok(Some(stored))
    }

    /// Record a successful resolution: cache the token and clear failure state.
    fn accept(
        &self,
        attempt: &mut refresh_state::RefreshAttempt,
        provider: SubscriptionProvider,
        account: &str,
        token: &SubscriptionToken,
        rotated_at_ms: Option<i64>,
    ) {
        // Remember that *this* process minted the credential, and when. A
        // single-use chain must not be spent again moments later on evidence
        // that is not about the credential, and a chain that does die deserves
        // to be reported as spent here rather than blamed on another holder
        // (issue #319). `None` means the credential was adopted rather than
        // exchanged, so no link was spent and no grace period is owed.
        if let Some(now_ms) = rotated_at_ms {
            // The fingerprint moves to the token we just minted, because the
            // ladder has already written it to the file the next caller reads.
            // Leaving it behind is what let a rotation this process performed
            // look like a re-authentication (issue #319).
            attempt.record_rotation(provider, now_ms, token);
        }
        self.store_for(provider, account, token.clone());
        attempt.record_success();
        self.record_credential_working_for(provider, account);
        if let Ok(mut guard) = self.refresh_errors.lock() {
            guard.remove(&(provider, account.to_string()));
        }
        // A refresh that succeeded settles the question for this account: any
        // earlier refusal was about a link the chain has since moved past.
        self.rejections.clear(provider, account);
    }

    /// Drop cached state derived from a credential that has been replaced.
    pub(super) fn forget(&self, key: &SubscriptionKey, provider: SubscriptionProvider) {
        if let Ok(mut guard) = self.inner.lock() {
            guard.remove(key);
        }
        if let Ok(mut guard) = self.evidence.lock() {
            guard.remove(key);
        }
        if let Ok(mut guard) = self.refresh_errors.lock() {
            guard.remove(key);
        }
        self.rejections.clear(provider, &key.1);
        self.clear_terminal_announcement_for(provider, &key.1);
    }

    /// Clear verdicts derived from a credential that an authoritative reload
    /// proves has been replaced.
    pub(super) async fn reconcile_authoritative_credential(
        &self,
        provider: SubscriptionProvider,
        account: &str,
        credential: &SubscriptionToken,
    ) {
        let key = (provider, account.to_string());
        let attempt = self
            .attempts
            .for_subscription(provider, account, credential);
        if attempt.lock().await.reset_if_changed(provider, credential) {
            self.forget(&key, provider);
        }
    }

    /// Record that an upstream call succeeded with `provider`'s credential.
    pub fn record_credential_working(&self, provider: SubscriptionProvider) {
        self.record_credential_working_for(
            provider,
            crate::credential_recovery_store::PRIMARY_ACCOUNT,
        );
    }

    /// Record that one stable router account served successfully.
    pub fn record_credential_working_for(&self, provider: SubscriptionProvider, account: &str) {
        self.record_evidence_for(provider, account, CredentialEvidence::Working);
        // A provider that is serving again may die again, and that death is a
        // new event that must be announced (issue #321).
        self.clear_terminal_announcement_for(provider, account);
    }
    pub(super) fn cached_valid_for(
        &self,
        provider: SubscriptionProvider,
        account: &str,
        now_ms: i64,
    ) -> Option<SubscriptionToken> {
        let guard = self.inner.lock().ok()?;
        guard
            .get(&(provider, account.to_string()))
            .filter(|token| !token.is_expired(now_ms))
            .cloned()
    }

    pub(super) fn store_for(
        &self,
        provider: SubscriptionProvider,
        account: &str,
        token: SubscriptionToken,
    ) {
        if let Ok(mut guard) = self.inner.lock() {
            guard.insert((provider, account.to_string()), token);
        }
    }
}

#[cfg(test)]
mod import_error_tests {
    use super::*;

    /// Safe-import failures describe the class of failure without echoing an
    /// endpoint body or storage detail that may contain credential material.
    #[test]
    fn import_refresh_errors_are_complete_and_secret_free() {
        let cases = [
            (
                super::super::RefreshError::NoRefreshToken,
                super::super::ImportRefreshFailureKind::NotAttempted,
            ),
            (
                super::super::RefreshError::from_status(
                    400,
                    r#"{"error":"invalid_grant","secret":"must-not-leak"}"#,
                    None,
                ),
                super::super::ImportRefreshFailureKind::ExchangeRejected,
            ),
            (
                super::super::RefreshError::from_status(503, "upstream body must-not-leak", None),
                super::super::ImportRefreshFailureKind::ExchangeUncertain,
            ),
            (
                super::super::RefreshError::Request("request detail must-not-leak".into()),
                super::super::ImportRefreshFailureKind::ExchangeUncertain,
            ),
            (
                super::super::RefreshError::Parse("parse detail must-not-leak".into()),
                super::super::ImportRefreshFailureKind::ExchangeUncertain,
            ),
            (
                super::super::RefreshError::Storage("storage detail must-not-leak".into()),
                super::super::ImportRefreshFailureKind::PersistenceUncertain,
            ),
            (
                super::super::RefreshError::Unsupported,
                super::super::ImportRefreshFailureKind::NotAttempted,
            ),
        ];

        for (error, expected) in cases {
            let failure = safe_import_refresh_error(SubscriptionProvider::Claude, &error);
            let message = failure.to_string();
            assert_eq!(failure.kind(), expected, "{message}");
            assert!(message.contains("claude"), "{message}");
            assert!(!message.contains("must-not-leak"), "{message}");
        }
    }
}