ai-usagebar 1.39.1

Omarchy/Waybar widgets + TUI for tracking multi-provider AI plan usage
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
//! Read Devin quota status and keep only normalized metrics in the local cache.
//!
//! The CLI key travels only as the `metadata.apiKey` field of the JSON request
//! body, to the fixed Cognition host. Neither the raw response nor the
//! credential is persisted.

use std::io::ErrorKind;
use std::sync::LazyLock;
use std::time::Duration;

use crate::cache::{Cache, acquire_lock_async, atomic_write};
use crate::error::{AppError, Result};
use crate::usage::{DevinSnapshot, UsageWindow};
use crate::vendor::{MAX_BODY_BYTES, read_body_capped};

use super::creds::{API_ORIGIN, Credentials};
use super::types::{DAILY_WINDOW, WEEKLY_WINDOW, parse_response};

const HTTP_TIMEOUT: Duration = Duration::from_secs(10);
const LOCK_TIMEOUT: Duration = Duration::from_secs(15);
// Devin-owned login marker: only the 16-hex truncated SHA-256 fingerprint,
// stored in plain text like the payload's account field, never the CLI key.
const ACCOUNT_FILE: &str = ".account";
const USER_STATUS_PATH: &str = "/exa.seat_management_pb.SeatManagementService/GetUserStatus";

#[derive(Debug, Clone)]
pub struct Endpoints {
    pub base: String,
}

impl Endpoints {
    pub fn user_status_url(&self) -> String {
        format!("{}{}", self.base.trim_end_matches('/'), USER_STATUS_PATH)
    }
}

impl Default for Endpoints {
    fn default() -> Self {
        Self {
            base: API_ORIGIN.to_string(),
        }
    }
}

pub type FetchOutcome = crate::outcome::Outcome<DevinSnapshot>;

/// The client refuses every redirect, so the CLI key is never forwarded or
/// re-sent through one, including a same-origin redirect.
fn build_client(timeout: Duration) -> reqwest::Result<reqwest::Client> {
    reqwest::Client::builder()
        .timeout(timeout)
        .redirect(reqwest::redirect::Policy::none())
        .build()
}

/// Devin keeps its own client instead of the process-wide one that the widget
/// and TUI share: that client follows redirects, and this provider must never
/// send the CLI key through one.
static CLIENT: LazyLock<Option<reqwest::Client>> =
    LazyLock::new(|| build_client(HTTP_TIMEOUT).ok());

fn http_client() -> Result<&'static reqwest::Client> {
    CLIENT
        .as_ref()
        .ok_or_else(|| AppError::Other("Devin HTTP client initialization failed".into()))
}

/// Production entry point; resolves the official CLI file on every call,
/// including cache hits, so a changed login cannot inherit another account's
/// cached quota. Credentials are resolved before any client is built, so a
/// missing login costs nothing.
pub async fn fetch_snapshot(
    cfg: &crate::config::DevinConfig,
    cache: &Cache,
    cache_ttl: Duration,
) -> Result<FetchOutcome> {
    let credentials = super::resolve_credentials(cfg)?;
    fetch_snapshot_with(
        http_client()?,
        &credentials,
        &super::client_version(),
        cache,
        &Endpoints::default(),
        cache_ttl,
    )
    .await
}

async fn fetch_snapshot_with(
    client: &reqwest::Client,
    credentials: &Credentials,
    client_version: &str,
    cache: &Cache,
    endpoints: &Endpoints,
    cache_ttl: Duration,
) -> Result<FetchOutcome> {
    cache.ensure_dir()?;
    let _lock = acquire_lock_async(&cache.lock_path(), LOCK_TIMEOUT).await?;

    bind_cache_login(cache, &credentials.fingerprint)?;

    if let Some(bytes) = cache.fresh_payload(cache_ttl)?
        && let Ok(outcome) = reuse_cache(&bytes, cache, false, &credentials.fingerprint)
    {
        return Ok(outcome);
    }

    match usage_call(client, credentials, client_version, endpoints).await {
        Ok(snapshot) => {
            cache.write_payload(&serde_json::to_vec(&snapshot_to_cache(
                &snapshot,
                &credentials.fingerprint,
            ))?)?;
            Ok(crate::outcome::Outcome::fresh(snapshot))
        }
        Err(error) if error.is_transient() => {
            fallback_silent(cache, &credentials.fingerprint, error)
        }
        Err(error) => {
            cache.mark_stale();
            let (code, message) = error_to_pair(&error);
            let diagnostic = cache.write_last_error(code, &message);
            fallback_with_error(cache, diagnostic, &credentials.fingerprint, error)
        }
    }
}

// Called under the fetch lock. Cache::forget/clear_backoff do not own .account;
// it may survive either operation and is reconciled here before cache reuse.
fn bind_cache_login(cache: &Cache, fingerprint: &str) -> Result<()> {
    let account_path = cache.dir().join(ACCOUNT_FILE);
    let account = match std::fs::read(&account_path) {
        Ok(bytes) => Some(bytes),
        Err(error) if error.kind() == ErrorKind::NotFound => None,
        Err(error) => return Err(AppError::io_at(&account_path, error)),
    };
    let payload = cache.maybe_payload()?;
    let valid_payload = payload
        .as_deref()
        .is_some_and(|bytes| parse_cache_at(bytes, fingerprint).is_ok());
    let same_login = match account.as_deref() {
        Some(account) => account == fingerprint.as_bytes(),
        None => valid_payload,
    };

    // The credential behind this cache changed hands or was never bound: the
    // sidecars describe a previous or unestablished login and must not shape
    // the new one's refresh; a valid current-login payload is retained.
    if !same_login
        || payload
            .as_deref()
            .is_some_and(|bytes| belongs_to_other_login(bytes, fingerprint))
    {
        // Strict cleanup intentionally stays Devin-local until a separate
        // shared-cache change can centralize ownership and the sidecar list
        // with Cache::forget.
        for path in [
            cache.payload_path(),
            cache.stale_path(),
            cache.last_error_path(),
            cache.retry_after_path(),
        ] {
            if valid_payload && path == cache.payload_path() {
                continue;
            }
            if let Err(error) = std::fs::remove_file(&path)
                && error.kind() != ErrorKind::NotFound
            {
                return Err(AppError::io_at(path, error));
            }
        }
    }
    if account.as_deref() != Some(fingerprint.as_bytes()) {
        atomic_write(&account_path, fingerprint.as_bytes())?;
    }
    Ok(())
}

async fn usage_call(
    client: &reqwest::Client,
    credentials: &Credentials,
    client_version: &str,
    endpoints: &Endpoints,
) -> Result<DevinSnapshot> {
    let body = serde_json::json!({
        "metadata": {
            "apiKey": &credentials.api_key,
            "ideName": "devin",
            "ideVersion": client_version,
            "extensionVersion": client_version,
            "locale": "en"
        }
    });
    // The client's own timeout bounds the request; the error is rebuilt from
    // fixed text so nothing from the request (URL, headers) can reach a log.
    let response = client
        .post(endpoints.user_status_url())
        .header(reqwest::header::ACCEPT, "application/json")
        .header(reqwest::header::CONTENT_TYPE, "application/json")
        .header("connect-protocol-version", "1")
        .json(&body)
        .send()
        .await
        .map_err(|error| {
            AppError::Transport(if error.is_timeout() {
                "Devin status request timed out".into()
            } else {
                "Devin status request failed".into()
            })
        })?;

    let status = response.status();
    let bytes = read_body_capped(response, MAX_BODY_BYTES).await?;
    if !status.is_success() {
        // The response body is never copied into the error: a 401/403 from a
        // key-bearing endpoint may echo account detail. The real status is
        // kept, and `Cache::write_last_error` redacts 401/403 messages.
        return Err(AppError::Http {
            status: status.as_u16(),
            body: format!("Devin status endpoint returned HTTP {}", status.as_u16()),
        });
    }
    parse_response(&bytes)
}

/// Only an HTTP status or a schema message survives into `.last_error` and the
/// tooltip. Everything else (I/O while reading the body, client setup) is
/// collapsed to fixed text on purpose: those errors can carry a path or a
/// request detail, and this is a key-bearing request. Model Studio exposes
/// `e.to_string()` here; the loss of diagnosis is accepted for Devin.
fn error_to_pair(error: &AppError) -> (u16, String) {
    match error {
        AppError::Http { status, body } => (*status, body.clone()),
        AppError::Schema(message) => (0, message.clone()),
        _ => (0, "Devin status request failed; details suppressed".into()),
    }
}

/// True only for a payload that names a *different* login. A payload that
/// does not parse or carries no identity is left for the normal replace-on-
/// success path rather than treated as another account's.
fn belongs_to_other_login(bytes: &[u8], fingerprint: &str) -> bool {
    serde_json::from_slice::<serde_json::Value>(bytes)
        .ok()
        .and_then(|value| {
            value
                .get("account")
                .and_then(serde_json::Value::as_str)
                .map(|account| account != fingerprint)
        })
        .unwrap_or(false)
}

fn fallback_silent(cache: &Cache, fingerprint: &str, original: AppError) -> Result<FetchOutcome> {
    crate::outcome::fallback(cache, None, original, |bytes| {
        parse_cache_at(bytes, fingerprint)
    })
}

fn fallback_with_error(
    cache: &Cache,
    last_error: (u16, String),
    fingerprint: &str,
    original: AppError,
) -> Result<FetchOutcome> {
    crate::outcome::fallback(cache, Some(last_error), original, |bytes| {
        parse_cache_at(bytes, fingerprint)
    })
}

fn reuse_cache(
    bytes: &[u8],
    cache: &Cache,
    stale: bool,
    fingerprint: &str,
) -> Result<FetchOutcome> {
    let snapshot = parse_cache_at(bytes, fingerprint)?;
    Ok(crate::outcome::Outcome::cached(snapshot, cache, stale))
}

fn snapshot_to_cache(snapshot: &DevinSnapshot, fingerprint: &str) -> serde_json::Value {
    let window = |window: &Option<UsageWindow>| match window {
        Some(window) => serde_json::json!({
            "used_pct": window.utilization_pct,
            "reset_unix": window.resets_at.map(|at| at.timestamp()),
        }),
        None => serde_json::Value::Null,
    };
    serde_json::json!({
        "account": fingerprint,
        "daily": window(&snapshot.daily),
        "weekly": window(&snapshot.weekly),
        "overage_balance_micros": snapshot.overage_balance_micros,
    })
}

fn parse_cache_at(bytes: &[u8], fingerprint: &str) -> Result<DevinSnapshot> {
    let value: serde_json::Value = serde_json::from_slice(bytes)
        .map_err(|_| AppError::Schema("Devin cache was invalid JSON".into()))?;
    if value.get("account").and_then(serde_json::Value::as_str) != Some(fingerprint) {
        return Err(AppError::Schema(
            "Devin cache belongs to a different login; refetching".into(),
        ));
    }
    let invalid = |field: &str| AppError::Schema(format!("Devin cache: invalid {field}"));
    let window = |field: &str, duration| -> Result<Option<UsageWindow>> {
        let Some(value) = value.get(field) else {
            return Err(invalid(field));
        };
        if value.is_null() {
            return Ok(None);
        }
        let pct = value
            .get("used_pct")
            .and_then(serde_json::Value::as_i64)
            .filter(|pct| (0..=100).contains(pct))
            .ok_or_else(|| invalid(field))? as i32;
        let reset = match value.get("reset_unix") {
            None | Some(serde_json::Value::Null) => None,
            Some(value) => {
                let seconds = value
                    .as_i64()
                    .filter(|seconds| *seconds >= 0)
                    .ok_or_else(|| invalid(field))?;
                Some(chrono::DateTime::from_timestamp(seconds, 0).ok_or_else(|| invalid(field))?)
            }
        };
        Ok(Some(UsageWindow {
            utilization_pct: pct,
            resets_at: reset,
            window_duration: duration,
        }))
    };
    let overage_balance_micros = match value.get("overage_balance_micros") {
        Some(serde_json::Value::Null) => None,
        Some(value) => Some(value.as_i64().ok_or_else(|| invalid("overage balance"))?),
        None => return Err(invalid("overage balance")),
    };
    Ok(DevinSnapshot {
        daily: window("daily", DAILY_WINDOW)?,
        weekly: window("weekly", WEEKLY_WINDOW)?,
        overage_balance_micros,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    const CANARY: &str = "DevinSecretCanary-593827";
    /// Distinct from the fallback, so the body matchers prove the caller's
    /// version reaches the request rather than a constant.
    const TEST_CLIENT_VERSION: &str = "3000.11.3";

    fn credentials(secret: &str) -> (TempDir, Credentials) {
        let dir = TempDir::new().unwrap();
        let path = dir.path().join("credentials.toml");
        std::fs::write(&path, format!("windsurf_api_key = {secret:?}\n")).unwrap();
        let credentials = super::super::creds::read_from(&path).unwrap();
        (dir, credentials)
    }

    fn cache_fixture() -> (TempDir, Cache) {
        let dir = TempDir::new().unwrap();
        let cache = Cache::at(dir.path().join("devin"));
        cache.ensure_dir().unwrap();
        (dir, cache)
    }

    /// The production client itself, so the redirect tests exercise the real
    /// no-redirect policy rather than a test-local copy of it.
    fn safe_client() -> reqwest::Client {
        http_client().unwrap().clone()
    }

    fn client_with_timeout(timeout: Duration) -> reqwest::Client {
        build_client(timeout).unwrap()
    }

    fn response_body() -> String {
        serde_json::json!({
            "userStatus": {"planStatus": {
                "dailyQuotaRemainingPercent": 100,
                "dailyQuotaResetAtUnix": "1791014400",
                "weeklyQuotaRemainingPercent": "69",
                "weeklyQuotaResetAtUnix": 1791100800,
                "overageBalanceMicros": "9168615"
            }}
        })
        .to_string()
    }

    fn request_mock(server: &mut mockito::ServerGuard, secret: &str) -> mockito::Mock {
        server
            .mock("POST", USER_STATUS_PATH)
            .match_header("content-type", "application/json")
            .match_header("connect-protocol-version", "1")
            .match_body(mockito::Matcher::Json(serde_json::json!({
                "metadata": {
                    "apiKey": secret,
                    "ideName": "devin",
                    "ideVersion": TEST_CLIENT_VERSION,
                    "extensionVersion": TEST_CLIENT_VERSION,
                    "locale": "en"
                }
            })))
    }

    fn snapshot() -> DevinSnapshot {
        DevinSnapshot {
            daily: Some(UsageWindow {
                utilization_pct: 42,
                resets_at: chrono::DateTime::from_timestamp(1_791_014_400, 0),
                window_duration: DAILY_WINDOW,
            }),
            weekly: None,
            overage_balance_micros: Some(9_168_615),
        }
    }

    #[tokio::test]
    async fn request_uses_the_cli_protocol_and_cache_contains_only_normalized_values() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let mut server = mockito::Server::new_async().await;
        let request = request_mock(&mut server, CANARY)
            .with_status(200)
            .with_body(response_body())
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        let outcome = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints { base: server.url() },
            Duration::ZERO,
        )
        .await
        .unwrap();
        request.assert_async().await;
        assert_eq!(outcome.snapshot.daily.as_ref().unwrap().utilization_pct, 0);
        assert_eq!(
            outcome.snapshot.weekly.as_ref().unwrap().utilization_pct,
            31
        );
        assert_eq!(outcome.snapshot.overage_balance_micros, Some(9_168_615));
        let stored = std::fs::read_to_string(cache.payload_path()).unwrap();
        assert!(!stored.contains(CANARY));
        assert!(!stored.contains("userStatus"));
        assert!(!stored.contains("dailyQuotaRemainingPercent"));
        assert_eq!(
            parse_cache_at(stored.as_bytes(), &credentials.fingerprint).unwrap(),
            outcome.snapshot
        );
    }

    #[tokio::test]
    async fn authentication_errors_hide_response_bodies_from_cache_and_fallback() {
        for status in [401, 403] {
            let (_credential_dir, credentials) = credentials(CANARY);
            let mut server = mockito::Server::new_async().await;
            let request = server
                .mock("POST", USER_STATUS_PATH)
                .with_status(status)
                .with_body(format!("credential echo: {CANARY}"))
                .create_async()
                .await;
            let (_cache_dir, cache) = cache_fixture();
            cache
                .write_payload(
                    &serde_json::to_vec(&snapshot_to_cache(&snapshot(), &credentials.fingerprint))
                        .unwrap(),
                )
                .unwrap();

            let outcome = fetch_snapshot_with(
                &safe_client(),
                &credentials,
                TEST_CLIENT_VERSION,
                &cache,
                &Endpoints { base: server.url() },
                Duration::ZERO,
            )
            .await
            .unwrap();
            request.assert_async().await;
            assert!(outcome.stale);
            let (code, error) = outcome.last_error.unwrap();
            assert_eq!(code, status as u16);
            assert_eq!(error, crate::error::AUTH_FAILURE_MESSAGE);
            assert!(!error.contains(CANARY));
            for item in std::fs::read_dir(cache.dir()).unwrap() {
                let item = item.unwrap();
                if item.file_type().unwrap().is_file() {
                    let bytes = std::fs::read(item.path()).unwrap();
                    assert!(!String::from_utf8_lossy(&bytes).contains(CANARY));
                }
            }
        }
    }

    #[tokio::test]
    async fn missing_cli_credentials_fail_before_creating_or_touching_the_cache() {
        let dir = TempDir::new().unwrap();
        let credential_path = dir.path().join("missing-credentials.toml");
        let cache_dir = dir.path().join("cache");
        let cache = Cache::at(cache_dir.clone());
        let cfg = crate::config::DevinConfig {
            credentials_path: Some(credential_path),
            ..Default::default()
        };

        let error = fetch_snapshot(&cfg, &cache, Duration::ZERO)
            .await
            .unwrap_err();

        assert!(matches!(error, AppError::Credentials(_)));
        assert!(!cache_dir.exists());
    }

    #[tokio::test]
    async fn a_timed_out_response_body_is_a_transient_transport_error() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let mut server = mockito::Server::new_async().await;
        let request = request_mock(&mut server, CANARY)
            .with_status(200)
            .with_chunked_body(|writer| {
                writer.write_all(b"{")?;
                std::thread::sleep(Duration::from_millis(500));
                writer.write_all(b"}")
            })
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();

        let error = fetch_snapshot_with(
            &client_with_timeout(Duration::from_millis(200)),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints { base: server.url() },
            Duration::ZERO,
        )
        .await
        .unwrap_err();

        request.assert_async().await;
        assert!(matches!(error, AppError::Transport(_)), "{error:?}");
        assert!(!cache.payload_path().exists());
    }

    #[tokio::test]
    async fn a_cross_origin_redirect_is_not_followed_with_the_cli_key() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let mut server = mockito::Server::new_async().await;
        let request = request_mock(&mut server, CANARY)
            .with_status(307)
            .with_header("location", "https://attacker.invalid/collect")
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        let error = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints { base: server.url() },
            Duration::ZERO,
        )
        .await
        .unwrap_err();
        request.assert_async().await;
        assert!(matches!(error, AppError::Http { status: 307, .. }));
        assert!(!error.to_string().contains(CANARY));
    }

    #[tokio::test]
    async fn a_same_origin_redirect_is_not_followed_with_the_cli_key() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let mut server = mockito::Server::new_async().await;
        let destination = format!("{}/redirected", server.url());
        let request = request_mock(&mut server, CANARY)
            .with_status(307)
            .with_header("location", &destination)
            .create_async()
            .await;
        let redirected = server
            .mock("POST", "/redirected")
            .expect(0)
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        let error = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints { base: server.url() },
            Duration::ZERO,
        )
        .await
        .unwrap_err();
        request.assert_async().await;
        redirected.assert_async().await;
        assert!(matches!(error, AppError::Http { status: 307, .. }));
        assert!(!error.to_string().contains(CANARY));
    }

    #[tokio::test]
    async fn a_cache_for_another_login_is_refetched_not_reused() {
        let (_old_dir, old_credentials) = credentials("old-account-key");
        let (_new_dir, credentials) = credentials("new-account-key");
        let mut server = mockito::Server::new_async().await;
        let request = request_mock(&mut server, "new-account-key")
            .with_status(200)
            .with_body(response_body())
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        let mut old_snapshot = snapshot();
        old_snapshot.daily.as_mut().unwrap().utilization_pct = 99;
        cache
            .write_payload(
                &serde_json::to_vec(&snapshot_to_cache(
                    &old_snapshot,
                    &old_credentials.fingerprint,
                ))
                .unwrap(),
            )
            .unwrap();
        let outcome = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints { base: server.url() },
            Duration::from_secs(3600),
        )
        .await
        .unwrap();
        request.assert_async().await;
        assert_eq!(outcome.snapshot.daily.unwrap().utilization_pct, 0);
    }

    #[tokio::test]
    async fn an_auth_rejection_without_a_cache_keeps_the_real_status() {
        for status in [401, 403] {
            let (_credential_dir, credentials) = credentials(CANARY);
            let mut server = mockito::Server::new_async().await;
            let request = server
                .mock("POST", USER_STATUS_PATH)
                .with_status(status)
                .with_body(format!("credential echo: {CANARY}"))
                .create_async()
                .await;
            let (_cache_dir, cache) = cache_fixture();

            let error = fetch_snapshot_with(
                &safe_client(),
                &credentials,
                TEST_CLIENT_VERSION,
                &cache,
                &Endpoints { base: server.url() },
                Duration::ZERO,
            )
            .await
            .unwrap_err();

            request.assert_async().await;
            assert!(
                matches!(&error, AppError::Http { status: got, .. } if *got == status as u16),
                "{error:?}"
            );
            assert!(!error.to_string().contains(CANARY));
            assert!(
                error
                    .user_message()
                    .contains(crate::error::AUTH_FAILURE_MESSAGE)
            );
            assert_eq!(
                cache.read_last_error(),
                Some((
                    status as u16,
                    crate::error::AUTH_FAILURE_MESSAGE.to_string()
                ))
            );
        }
    }

    #[tokio::test]
    async fn a_changed_login_forgets_the_previous_logins_payload_and_backoff() {
        let (_old_dir, old_credentials) = credentials("old-account-key");
        let (_new_dir, credentials) = credentials("new-account-key");
        let mut server = mockito::Server::new_async().await;
        let request = request_mock(&mut server, "new-account-key")
            .with_status(200)
            .with_body(response_body())
            .expect(1)
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        cache
            .write_payload(
                &serde_json::to_vec(&snapshot_to_cache(
                    &snapshot(),
                    &old_credentials.fingerprint,
                ))
                .unwrap(),
            )
            .unwrap();
        cache.mark_stale();
        cache.write_last_error(500, "previous login failed");
        cache.note_rate_limit_at(std::time::SystemTime::now());
        assert!(cache.backoff_remaining().is_some());

        let outcome = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints { base: server.url() },
            Duration::from_secs(3600),
        )
        .await
        .unwrap();

        request.assert_async().await;
        assert!(!outcome.stale);
        assert!(outcome.last_error.is_none());
        assert_eq!(outcome.snapshot.daily.unwrap().utilization_pct, 0);
        assert!(!cache.is_stale());
        assert!(cache.read_last_error().is_none());
        assert!(cache.backoff_remaining().is_none());
        let stored = std::fs::read(cache.payload_path()).unwrap();
        assert!(parse_cache_at(&stored, &credentials.fingerprint).is_ok());
    }

    #[tokio::test]
    async fn a_cold_cache_backoff_is_scoped_to_the_login_across_reopens() {
        let (_old_dir, old_credentials) = credentials(CANARY);
        let (_new_dir, new_credentials) = credentials("new-account-key");
        let mut server = mockito::Server::new_async().await;
        let old_request = request_mock(&mut server, CANARY)
            .with_status(429)
            .expect(1)
            .create_async()
            .await;
        let new_request = request_mock(&mut server, "new-account-key")
            .with_status(200)
            .with_body(response_body())
            .expect(1)
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        let endpoints = Endpoints { base: server.url() };
        let client = safe_client();

        let first = fetch_snapshot_with(
            &client,
            &old_credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &endpoints,
            Duration::ZERO,
        )
        .await
        .unwrap_err();
        assert!(matches!(first, AppError::Http { status: 429, .. }));
        assert!(!cache.payload_path().exists());
        assert!(cache.backoff_remaining().is_some());
        let retry_after = std::fs::read(cache.retry_after_path()).unwrap();

        let cache = Cache::at(cache.dir().to_path_buf());
        let same_login = fetch_snapshot_with(
            &client,
            &old_credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &endpoints,
            Duration::ZERO,
        )
        .await
        .unwrap_err();
        assert!(matches!(same_login, AppError::Http { status: 429, .. }));
        assert_eq!(
            std::fs::read(cache.retry_after_path()).unwrap(),
            retry_after
        );
        old_request.assert_async().await;

        let cache = Cache::at(cache.dir().to_path_buf());
        let changed_login = fetch_snapshot_with(
            &client,
            &new_credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &endpoints,
            Duration::from_secs(3600),
        )
        .await
        .unwrap();
        new_request.assert_async().await;
        assert!(!changed_login.stale);
        assert!(changed_login.last_error.is_none());
        assert!(!cache.is_stale());
        assert!(cache.read_last_error().is_none());
        assert!(cache.backoff_remaining().is_none());
        let stored = std::fs::read(cache.payload_path()).unwrap();
        assert_eq!(
            parse_cache_at(&stored, &new_credentials.fingerprint).unwrap(),
            changed_login.snapshot
        );
        let account = std::fs::read_to_string(cache.dir().join(ACCOUNT_FILE)).unwrap();
        assert_eq!(account, new_credentials.fingerprint);
        assert_eq!(account.len(), 16);
        assert!(
            account
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
        );
        assert!(!account.contains(CANARY));
        assert!(!account.contains("new-account-key"));
    }

    #[test]
    fn unowned_backoff_is_discarded_once_when_migrating_a_cold_cache() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let (_cache_dir, cache) = cache_fixture();
        cache.mark_stale();
        cache.write_last_error(429, "legacy rate limit");
        assert!(!cache.payload_path().exists());
        assert!(!cache.dir().join(ACCOUNT_FILE).exists());
        assert!(cache.backoff_remaining().is_some());

        bind_cache_login(&cache, &credentials.fingerprint).unwrap();
        assert!(!cache.is_stale());
        assert!(cache.read_last_error().is_none());
        assert!(cache.backoff_remaining().is_none());
        assert_eq!(
            std::fs::read_to_string(cache.dir().join(ACCOUNT_FILE)).unwrap(),
            credentials.fingerprint
        );

        cache.write_last_error(429, "current login rate limit");
        let retry_after = std::fs::read(cache.retry_after_path()).unwrap();
        let cache = Cache::at(cache.dir().to_path_buf());
        bind_cache_login(&cache, &credentials.fingerprint).unwrap();
        assert_eq!(
            std::fs::read(cache.retry_after_path()).unwrap(),
            retry_after
        );
        assert_eq!(
            cache.read_last_error(),
            Some((429, "current login rate limit".into()))
        );
    }

    #[test]
    fn failed_cache_invalidation_does_not_record_a_new_login() {
        let (_cache_dir, cache) = cache_fixture();
        let old_fingerprint = crate::cache::fingerprint_of("old-account-key");
        let new_fingerprint = crate::cache::fingerprint_of("new-account-key");
        let account_path = cache.dir().join(ACCOUNT_FILE);
        std::fs::write(&account_path, &old_fingerprint).unwrap();
        cache
            .write_payload(
                &serde_json::to_vec(&snapshot_to_cache(&snapshot(), &old_fingerprint)).unwrap(),
            )
            .unwrap();
        cache.write_last_error(429, "old login rate limit");
        // remove_file rejects a directory on Unix and Windows. The payload is
        // removed first, so this exercises partial cleanup without committing
        // the new identity.
        std::fs::create_dir(cache.stale_path()).unwrap();

        let error = bind_cache_login(&cache, &new_fingerprint).unwrap_err();
        assert!(matches!(error, AppError::Io { .. }));
        assert!(!cache.payload_path().exists());
        assert!(cache.stale_path().is_dir());
        assert!(cache.last_error_path().exists());
        assert!(cache.retry_after_path().exists());
        assert_eq!(
            std::fs::read_to_string(account_path).unwrap(),
            old_fingerprint
        );
    }

    #[test]
    fn a_valid_current_payload_survives_an_old_login_marker() {
        let (_cache_dir, cache) = cache_fixture();
        let old_fingerprint = crate::cache::fingerprint_of("old-account-key");
        let fingerprint = crate::cache::fingerprint_of("new-account-key");
        let payload = serde_json::to_vec(&snapshot_to_cache(&snapshot(), &fingerprint)).unwrap();
        cache.write_payload(&payload).unwrap();
        std::fs::write(cache.dir().join(ACCOUNT_FILE), old_fingerprint).unwrap();
        cache.mark_stale();
        cache.write_last_error(429, "old login rate limit");

        bind_cache_login(&cache, &fingerprint).unwrap();

        assert_eq!(cache.maybe_payload().unwrap(), Some(payload));
        assert!(!cache.stale_path().exists());
        assert!(!cache.last_error_path().exists());
        assert!(!cache.retry_after_path().exists());
        assert_eq!(
            std::fs::read_to_string(cache.dir().join(ACCOUNT_FILE)).unwrap(),
            fingerprint
        );
    }

    #[test]
    fn a_surviving_login_marker_after_forget_is_safe_to_rebind() {
        let (_cache_dir, cache) = cache_fixture();
        let fingerprint = crate::cache::fingerprint_of("old-account-key");
        bind_cache_login(&cache, &fingerprint).unwrap();
        cache.write_last_error(429, "old login rate limit");
        cache.forget();
        assert_eq!(
            std::fs::read_to_string(cache.dir().join(ACCOUNT_FILE)).unwrap(),
            fingerprint
        );

        bind_cache_login(&cache, &fingerprint).unwrap();
        assert!(!cache.payload_path().exists());
        assert!(!cache.retry_after_path().exists());

        let new_fingerprint = crate::cache::fingerprint_of("new-account-key");
        bind_cache_login(&cache, &new_fingerprint).unwrap();
        assert_eq!(
            std::fs::read_to_string(cache.dir().join(ACCOUNT_FILE)).unwrap(),
            new_fingerprint
        );
    }

    #[tokio::test]
    async fn the_same_login_keeps_its_backoff() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let mut server = mockito::Server::new_async().await;
        let request = request_mock(&mut server, CANARY)
            .expect(0)
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        cache
            .write_payload(
                &serde_json::to_vec(&snapshot_to_cache(&snapshot(), &credentials.fingerprint))
                    .unwrap(),
            )
            .unwrap();
        cache.note_rate_limit_at(std::time::SystemTime::now());

        let outcome = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints { base: server.url() },
            Duration::ZERO,
        )
        .await
        .unwrap();

        request.assert_async().await;
        assert_eq!(outcome.snapshot, snapshot());
        assert!(cache.backoff_remaining().is_some());
        assert_eq!(
            std::fs::read_to_string(cache.dir().join(ACCOUNT_FILE)).unwrap(),
            credentials.fingerprint
        );
    }

    #[tokio::test]
    async fn a_corrupt_cache_is_replaced_by_a_successful_fetch() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let mut server = mockito::Server::new_async().await;
        let request = request_mock(&mut server, CANARY)
            .with_status(200)
            .with_body(response_body())
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        cache.write_payload(b"not-json").unwrap();

        let outcome = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints { base: server.url() },
            Duration::from_secs(3600),
        )
        .await
        .unwrap();

        request.assert_async().await;
        assert!(!outcome.stale);
        assert_eq!(outcome.snapshot.daily.unwrap().utilization_pct, 0);
        let stored = std::fs::read(cache.payload_path()).unwrap();
        assert!(parse_cache_at(&stored, &credentials.fingerprint).is_ok());
    }

    #[tokio::test]
    async fn an_expired_cache_is_refreshed_and_replaced() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let mut server = mockito::Server::new_async().await;
        let request = request_mock(&mut server, CANARY)
            .with_status(200)
            .with_body(response_body())
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        cache
            .write_payload(
                &serde_json::to_vec(&snapshot_to_cache(&snapshot(), &credentials.fingerprint))
                    .unwrap(),
            )
            .unwrap();
        std::fs::OpenOptions::new()
            .write(true)
            .open(cache.payload_path())
            .unwrap()
            .set_times(std::fs::FileTimes::new().set_modified(std::time::SystemTime::UNIX_EPOCH))
            .unwrap();

        let outcome = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints { base: server.url() },
            Duration::from_secs(3600),
        )
        .await
        .unwrap();

        request.assert_async().await;
        assert!(!outcome.stale);
        assert_eq!(outcome.snapshot.daily.unwrap().utilization_pct, 0);
    }

    #[tokio::test]
    async fn rate_limits_arm_backoff_and_skip_the_next_network_request() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let mut server = mockito::Server::new_async().await;
        let request = request_mock(&mut server, CANARY)
            .with_status(429)
            .with_body("rate-limit-body-must-not-be-persisted")
            .expect(1)
            .create_async()
            .await;
        let (_cache_dir, cache) = cache_fixture();
        cache
            .write_payload(
                &serde_json::to_vec(&snapshot_to_cache(&snapshot(), &credentials.fingerprint))
                    .unwrap(),
            )
            .unwrap();
        let endpoints = Endpoints { base: server.url() };

        let first = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &endpoints,
            Duration::ZERO,
        )
        .await
        .unwrap();
        assert!(first.stale);
        assert_eq!(first.last_error.as_ref().map(|(code, _)| *code), Some(429));
        assert!(cache.backoff_remaining().is_some());
        assert!(
            !std::fs::read_to_string(cache.last_error_path())
                .unwrap()
                .contains("rate-limit-body-must-not-be-persisted")
        );

        let second = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &endpoints,
            Duration::ZERO,
        )
        .await
        .unwrap();
        request.assert_async().await;
        assert!(second.stale);
        assert_eq!(second.last_error.as_ref().map(|(code, _)| *code), Some(429));
    }

    #[tokio::test]
    async fn a_fresh_identity_matching_cache_does_not_touch_the_network() {
        let (_credential_dir, credentials) = credentials(CANARY);
        let (_cache_dir, cache) = cache_fixture();
        cache
            .write_payload(
                &serde_json::to_vec(&snapshot_to_cache(&snapshot(), &credentials.fingerprint))
                    .unwrap(),
            )
            .unwrap();
        let outcome = fetch_snapshot_with(
            &safe_client(),
            &credentials,
            TEST_CLIENT_VERSION,
            &cache,
            &Endpoints {
                base: "http://127.0.0.1:1".into(),
            },
            Duration::from_secs(60),
        )
        .await
        .unwrap();
        assert_eq!(outcome.snapshot, snapshot());
        assert!(!outcome.stale);
    }

    #[test]
    fn cache_requires_identity_and_valid_metric_shapes() {
        let snapshot = snapshot();
        let encoded = serde_json::to_vec(&snapshot_to_cache(&snapshot, "account-a")).unwrap();
        assert_eq!(parse_cache_at(&encoded, "account-a").unwrap(), snapshot);
        assert!(parse_cache_at(&encoded, "account-b").is_err());
        for invalid in [
            serde_json::json!({"account":"account-a","daily":{"used_pct":101},"weekly":null,"overage_balance_micros":null}),
            serde_json::json!({"account":"account-a","daily":null,"weekly":null}),
            serde_json::json!({"account":"account-a","daily":{"used_pct":1,"reset_unix":-1},"weekly":null,"overage_balance_micros":null}),
        ] {
            assert!(parse_cache_at(invalid.to_string().as_bytes(), "account-a").is_err());
        }
    }
}