mnml-rs 0.2.20

A NvChad-style terminal IDE in Rust — vim or standard editing, LSP, git, and an embedded HTTP client.
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
//! AI usage meter — multi-account Claude + Codex usage/token drain,
//! Keychain lookups for the "link Claude Code" onboarding, and the
//! shadowed-binary audit that flags outdated shim installs.
//!
//! Extracted from `app/mod.rs` (file-split refactor — Task #963).
//! Pure non-destructive move; no API change.

use super::*;

/// Minimum interval between `spawn_keychain_active_refresh_token`
/// spawns. Matches the per-account fetcher's 5-min cadence so account
/// switches propagate within roughly one refresh cycle. Kicked from
/// `App::maybe_refresh_ai_usage` (per-tick) but gated by
/// `keychain_active_last_kick_at`.
const KEYCHAIN_ACTIVE_REFRESH_SECS: u64 = 5 * 60;

/// Fold a failed fetch into the account's *existing* snapshot.
///
/// The numbers deliberately survive (#1217): the endpoint 429s
/// routinely with three accounts on a 5-min poll, and zeroing a good
/// five-minute-old reading doesn't read as "unknown", it reads as
/// "you've used nothing" — the opposite of a warning. `last_error` is
/// the staleness signal instead, and `fetched_at` keeps its old value
/// so the age stays honest.
///
/// Extracted from `drain_ai_usage` so the assignment semantics are
/// testable. Both flag writes are plain assignments, NOT `|=`: an
/// error that isn't a re-auth failure has to *clear* a prior re-auth
/// flag, or a single bad keychain state would pin the pane's guided
/// re-auth block on forever, through every later 429.
fn apply_fetch_error(
    usage: &mut crate::ai_usage::ClaudeUsage,
    e: crate::ai_usage::FetchErr,
    now: u64,
) {
    usage.consecutive_failures = usage.consecutive_failures.saturating_add(1);
    // #ai-429 — ALWAYS back off after a failure, not only when the
    // response carried a numeric Retry-After.
    //
    // User report with a screenshot of three accounts all showing
    // `HTTP 429 rate_limit_error`: "dont hammer anthropic, look like we
    // are making 429's". The old code applied a cooldown ONLY when
    // `retry_after_secs` was present, so a 429 whose header was absent or
    // in HTTP-date form got no cooldown at all — the account simply
    // retried on the normal 5-minute cadence, forever. The retry pressure
    // never eased, so the limit never had a chance to clear.
    //
    // Anthropic's hint wins when it gives one; otherwise exponential
    // backoff on consecutive failures, capped so an account that has been
    // failing all day still checks hourly and can recover on its own.
    let backoff = match e.retry_after_secs {
        Some(secs) => secs,
        None => {
            const BASE: u64 = 10 * 60;
            const CAP: u64 = 60 * 60;
            let shift = usage.consecutive_failures.saturating_sub(1).min(3);
            (BASE << shift).min(CAP)
        }
    };
    usage.retry_after_at = now.saturating_add(backoff);
    usage.last_error = Some(e.message);
    usage.needs_reauth = e.needs_reauth;
}

impl App {
    /// AI usage meter — kick off background fetches if it's been
    /// >5 min since the last spawn AND no fetch is currently in
    /// flight. Cheap no-op the other 99% of ticks. Called from the
    /// per-tick loop.
    ///
    /// 2026-08-16 — was 30s fast-retry on 429, but continuous 30s
    /// polling BECAME the source of Anthropic's rate limit (2880
    /// requests/day per mnml instance) — the chip stayed stuck on
    /// `—!` forever because every retry landed within the moving
    /// rate-limit window. Now: same 5-min cadence for 429 as normal.
    /// Anthropic's rate limits clear in minutes-to-hours; polling
    /// twice per minute is what created the problem the fast-retry
    /// was meant to solve. See task #943.
    pub fn maybe_refresh_ai_usage(&mut self) {
        // #ai-429 — the ACTIVE account refreshes often; the others
        // rarely.
        //
        // User: "if an account not active its probaly not in use and not
        // needing of as frequent of updates". Right — and the arithmetic
        // is the point. It was a flat 5 minutes for EVERY account, so
        // three accounts cost ~36 requests/hour regardless of which one
        // you were actually spending.
        //
        // 5 min active + 20 min idle is 12 + 3 + 3 = ~18 requests/hour,
        // HALF the previous load, with no loss where it matters — user:
        // "5 minutes is fine then ... it doesnt change that fast".
        //
        // Worth recording what was rejected: polling the active account
        // every minute was considered and dropped, because it would be
        // ~60 requests/hour on its own — MORE pressure than the setup that
        // earned the 429s in the first place. Faster polling of a number
        // that moves slowly is pure cost.
        const REFRESH_INTERVAL_SECS: u64 = 5 * 60;
        const IDLE_REFRESH_INTERVAL_SECS: u64 = 20 * 60;
        /// The fast window exists for ONE question: am I about to run out
        /// and have to switch accounts?
        ///
        /// User: "or just when we expect to run out soon and have to
        /// change account". That framing sets both ends of the window.
        /// Below 90% there is nothing to decide. At 100% there is nothing
        /// left to WATCH either — you already know you must switch, and
        /// when it resets is a timestamp you have. Polling a maxed-out
        /// account every minute would spend requests to re-learn something
        /// unchanged, at exactly the moment the endpoint is least likely
        /// to answer.
        ///
        /// So: active, and 90..=99. Bounded to the one account you are
        /// actually spending.
        const HOT_REFRESH_INTERVAL_SECS: u64 = 60;
        const HOT_RANGE: std::ops::Range<u16> = 90..100;
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        // Only spawn if the corresponding integration is enabled;
        // otherwise the chip won't render anyway.
        let claude_enabled = self
            .config
            .ui
            .integration_icons
            .iter()
            .any(|ic| ic.id == "claude_code" && ic.enabled);
        let codex_enabled = self
            .config
            .ui
            .integration_icons
            .iter()
            .any(|ic| ic.id == "codex" && ic.enabled);
        if !claude_enabled && !codex_enabled {
            return;
        }
        // Codex — single-instance, same 5-min throttle as before
        // (it reads local JSONL files, no rate limit to negotiate).
        if codex_enabled
            && self.ai_usage_pending_codex.is_none()
            && now.saturating_sub(self.ai_usage_last_refresh_at) >= REFRESH_INTERVAL_SECS
        {
            self.ai_usage_last_refresh_at = now;
            self.ai_usage_pending_codex = Some(crate::ai_usage::spawn_codex_fetch());
        }
        // Claude — per-account (task #944). Independent throttle +
        // Retry-After per account so a 429 on one doesn't stall
        // another. Also drops slots for accounts removed from the
        // config so stale entries clear on config reload.
        if !claude_enabled {
            return;
        }
        // #1150 f/u (2026-08-23) — kick the autodetect worker on the
        // same cadence as the per-account fetches. The Keychain lookup
        // is threaded (`security find-generic-password` can prompt)
        // so this only enqueues; the result gets drained by
        // `drain_keychain_active_watch`.
        self.kick_keychain_active_refresh();
        let configured = self.config.claude_accounts();
        let configured_names: std::collections::HashSet<String> =
            configured.iter().map(|a| a.name.clone()).collect();
        self.ai_usage_claude_accounts
            .retain(|a| configured_names.contains(&a.name));
        self.ai_usage_claude_last_refresh_at
            .retain(|k, _| configured_names.contains(k));
        // #ai-429 — ONE account per tick, and never two within
        // `SPAWN_GAP_SECS` of each other.
        //
        // The per-account throttle made every account eligible at the same
        // moment, so a burst of three requests left the same millisecond.
        // A single global gap turns that into a staggered trickle without
        // changing how often any one account refreshes.
        const SPAWN_GAP_SECS: u64 = 20;
        if now.saturating_sub(self.ai_usage_last_claude_spawn_at) < SPAWN_GAP_SECS {
            return;
        }
        for account in &configured {
            // Skip when a fetch is already in flight for this account.
            if self
                .ai_usage_pending_claude_accounts
                .iter()
                .any(|(n, _)| n == &account.name)
            {
                continue;
            }
            // Honor Retry-After for THIS account only.
            if let Some(existing) = self
                .ai_usage_claude_accounts
                .iter()
                .find(|a| a.name == account.name)
                && existing.usage.retry_after_at > now
            {
                continue;
            }
            let last = self
                .ai_usage_claude_last_refresh_at
                .get(&account.name)
                .copied()
                .unwrap_or(0);
            // Active accounts poll on the short interval, idle ones on
            // the long one. `is_active` is maintained by the keychain
            // autodetect watcher.
            let is_active = self
                .ai_usage_claude_accounts
                .iter()
                .find(|a| a.name == account.name)
                .map(|a| a.is_active)
                .unwrap_or(false);
            let percent = self
                .ai_usage_claude_accounts
                .iter()
                .find(|a| a.name == account.name)
                .map(|a| a.usage.percent)
                .unwrap_or(0);
            let interval = match (is_active, HOT_RANGE.contains(&percent)) {
                (true, true) => HOT_REFRESH_INTERVAL_SECS,
                (true, false) => REFRESH_INTERVAL_SECS,
                (false, _) => IDLE_REFRESH_INTERVAL_SECS,
            };
            // An account that has NEVER been fetched still goes on the
            // short interval — otherwise a fresh install would show em
            // dashes for twenty minutes before its first reading.
            let interval = if last == 0 {
                REFRESH_INTERVAL_SECS
            } else {
                interval
            };
            if now.saturating_sub(last) < interval {
                continue;
            }
            self.ai_usage_claude_last_refresh_at
                .insert(account.name.clone(), now);
            self.ai_usage_last_claude_spawn_at = now;
            // #1232 — the fetcher needs the account count to know
            // whether a keychain resync would be cross-writing over a
            // sibling account's credential.
            let rx = crate::ai_usage::spawn_claude_fetch_account_of(
                account.name.clone(),
                account.resolved_token_path(),
                configured.len(),
            );
            self.ai_usage_pending_claude_accounts
                .push((account.name.clone(), rx));
            // One per tick. The next eligible account goes on a later
            // tick, gated by SPAWN_GAP_SECS above.
            break;
        }
    }

    /// The single account tagged `active = true` in the config —
    /// used by the statusline chip's default (single-account)
    /// rendering. `None` when nothing has been fetched yet.
    /// Task #944.
    pub fn active_claude_account(&self) -> Option<&crate::ai_usage::ClaudeAccountUsage> {
        // #1150 f/u (2026-08-23) — autodetect (Keychain refresh-token
        // match) wins over the manual `active = true` config flag,
        // then falls back to the first entry so a snapshot exists
        // even if the config was edited between fetch + render.
        let active_name: Option<String> =
            self.autodetected_active_claude_account_name().or_else(|| {
                self.config
                    .claude_accounts()
                    .into_iter()
                    .find(|a| a.active)
                    .map(|a| a.name)
            });
        if let Some(name) = active_name.as_ref()
            && let Some(hit) = self
                .ai_usage_claude_accounts
                .iter()
                .find(|a| &a.name == name)
        {
            return Some(hit);
        }
        self.ai_usage_claude_accounts.first()
    }

    /// Drain any completed AI-usage worker replies. Called per tick.
    /// Failures are stored on the snapshot's `last_error` so the
    /// chip's hover tooltip can surface them.
    pub fn drain_ai_usage(&mut self) {
        // Claude — drain each per-account receiver independently.
        // Retain-with-side-effect: any receiver that hasn't emitted
        // yet stays in the vec; any that returned Ok/Err is spliced
        // into `ai_usage_claude_accounts` and removed.
        //
        // Task #944 — per-account error handling mirrors the
        // pre-multi-account semantics (zero percentages, keep the
        // slot so the pane empty-state + chip surface the failure).
        // #1150 f/u (2026-08-23) — autodetect which configured
        // account is the LIVE Claude Code CLI login by comparing the
        // Keychain's refresh token against each account's on-disk
        // token file. Falls back to the manual `active = true`
        // config flag when the Keychain isn't available or no account
        // matches (unlinked yet, tokens rotated, non-macOS, etc.) —
        // which was the pre-fix behavior and drifted whenever a user
        // switched Claude Code accounts without editing config.toml.
        let autodetected: Option<String> = self.autodetected_active_claude_account_name();
        let active_names: std::collections::HashSet<String> = if let Some(name) = autodetected {
            std::iter::once(name).collect()
        } else {
            self.config
                .claude_accounts()
                .into_iter()
                .filter(|a| a.active)
                .map(|a| a.name)
                .collect()
        };
        let now_ts = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let mut drained: Vec<(
            String,
            Result<crate::ai_usage::ClaudeAccountUsage, crate::ai_usage::FetchErr>,
        )> = Vec::new();
        self.ai_usage_pending_claude_accounts
            .retain(|(name, rx)| match rx.try_recv() {
                Ok(payload) => {
                    drained.push((name.clone(), payload));
                    false
                }
                Err(std::sync::mpsc::TryRecvError::Empty) => true,
                Err(std::sync::mpsc::TryRecvError::Disconnected) => false,
            });
        for (name, result) in drained {
            let is_active = active_names.contains(&name);
            match result {
                Ok(mut acc) => {
                    acc.is_active = is_active;
                    // #1232 — a pin collision means several token
                    // files are sharing one credential. The user has
                    // to see that; it can't stay a worker-thread
                    // eprintln behind the alternate screen.
                    if let Some(w) = acc.warning.take() {
                        self.toast(w);
                    }
                    upsert_claude_account(&mut self.ai_usage_claude_accounts, acc);
                }
                Err(e) => {
                    // Preserve the prior snapshot for this account —
                    // ALL of it.
                    //
                    // #1217 (2026-08-28, user: "seems to have become
                    // very unreliable lately"): this used to zero
                    // `percent` / `weekly_percent` / `scoped_limits`
                    // on any error, meaning to signal "no fresh
                    // data". But the endpoint 429s regularly (three
                    // accounts polled on a 5-min cadence), so a good
                    // reading five minutes old was being replaced by
                    // 0% — which doesn't read as "unknown", it reads
                    // as "you've used nothing", the opposite of a
                    // warning. The chip flipped between a real number
                    // and 0 every few minutes.
                    //
                    // Now the numbers survive and `last_error` is the
                    // staleness signal; `fetched_at` keeps its old
                    // value so the age stays honest. The renderers
                    // mark stale readings rather than inventing a
                    // fresh-looking zero.
                    let mut existing = self
                        .ai_usage_claude_accounts
                        .iter()
                        .find(|a| a.name == name)
                        .cloned()
                        .unwrap_or_else(|| crate::ai_usage::ClaudeAccountUsage {
                            name: name.clone(),
                            usage: crate::ai_usage::ClaudeUsage::default(),
                            is_active,
                            email: None,
                            org_name: None,
                            warning: None,
                        });
                    existing.is_active = is_active;
                    apply_fetch_error(&mut existing.usage, e, now_ts);
                    upsert_claude_account(&mut self.ai_usage_claude_accounts, existing);
                }
            }
        }
        if let Some(rx) = &self.ai_usage_pending_codex {
            match rx.try_recv() {
                Ok(Ok(u)) => {
                    self.ai_usage_codex = Some(u);
                    self.ai_usage_pending_codex = None;
                }
                Ok(Err(e)) => {
                    let mut u = self.ai_usage_codex.clone().unwrap_or_default();
                    u.last_error = Some(e);
                    self.ai_usage_codex = Some(u);
                    self.ai_usage_pending_codex = None;
                }
                Err(std::sync::mpsc::TryRecvError::Empty) => {}
                Err(std::sync::mpsc::TryRecvError::Disconnected) => {
                    self.ai_usage_pending_codex = None;
                }
            }
        }
    }

    /// 2026-08-08 — per-tick drain for the Keychain lookup worker
    /// (see `spawn_keychain_claude_token`). On success, splice the
    /// fetched blob into the LinkClaudeToken prompt if it's still
    /// open; toast the outcome either way.
    pub fn drain_pending_keychain(&mut self) {
        let Some(rx) = &self.pending_keychain_fetch else {
            return;
        };
        match rx.try_recv() {
            Ok(Ok(raw)) => {
                let is_link_prompt = matches!(
                    self.prompt.as_ref().map(|p| &p.kind),
                    Some(crate::prompt::PromptKind::LinkClaudeToken)
                );
                if is_link_prompt && let Some(prompt) = self.prompt.as_mut() {
                    prompt.cursor = raw.chars().count();
                    prompt.input = raw;
                    self.toast("fetched from Keychain — press Enter to link".to_string());
                } else {
                    // Prompt closed while the worker was running; drop.
                }
                self.pending_keychain_fetch = None;
            }
            Ok(Err(e)) => {
                self.toast(e);
                self.pending_keychain_fetch = None;
            }
            Err(std::sync::mpsc::TryRecvError::Empty) => {}
            Err(std::sync::mpsc::TryRecvError::Disconnected) => {
                self.pending_keychain_fetch = None;
            }
        }
    }

    /// #1150 f/u (2026-08-23) — per-tick drain for the autodetect
    /// worker (`spawn_keychain_active_refresh_token`). Success caches
    /// the parsed refresh token; failure clears the cache so the
    /// config-flag fallback resumes. Any success ALSO restamps the
    /// existing account list's `is_active` flags right away so the
    /// UI catches the new active account without waiting for the
    /// next usage-drain cycle.
    pub fn drain_keychain_active_watch(&mut self) {
        let Some(rx) = &self.keychain_active_watch else {
            return;
        };
        match rx.try_recv() {
            Ok(Ok(Some(rt))) => {
                self.keychain_claude_refresh_token = Some(rt);
                self.keychain_active_watch = None;
                self.restamp_claude_active_flags();
            }
            Ok(Ok(None)) => {
                // Keychain returned a blob but it had no refresh
                // token (plain-string token, or unfamiliar shape).
                // Leave the cache alone — a transient parse miss
                // shouldn't wipe a last-known-good match and force
                // the config-flag fallback (which was the bug that
                // motivated this whole autodetect path).
                self.keychain_active_watch = None;
            }
            Ok(Err(_)) => {
                // Keychain lookup failed — leave the cache alone so
                // the previous known active account keeps rendering,
                // and fall back to the config flag if there wasn't
                // one. Deliberate silence: this fetch fires often
                // enough that a toast on every failure would spam.
                self.keychain_active_watch = None;
            }
            Err(std::sync::mpsc::TryRecvError::Empty) => {}
            Err(std::sync::mpsc::TryRecvError::Disconnected) => {
                self.keychain_active_watch = None;
            }
        }
    }

    /// Spawn the autodetect worker if one isn't already in flight
    /// AND at least [`KEYCHAIN_ACTIVE_REFRESH_SECS`] have elapsed
    /// since the last kick. Called from `App::maybe_refresh_ai_usage`
    /// on every tick — the timestamp gate keeps mnml from spawning
    /// `security find-generic-password` at tick cadence (~120ms idle,
    /// ~40ms with a pty). Fires the first fetch immediately on
    /// startup because `keychain_active_last_kick_at` starts at 0.
    pub fn kick_keychain_active_refresh(&mut self) {
        if self.keychain_active_watch.is_some() {
            return;
        }
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        if now.saturating_sub(self.keychain_active_last_kick_at) < KEYCHAIN_ACTIVE_REFRESH_SECS {
            return;
        }
        self.keychain_active_last_kick_at = now;
        self.keychain_active_watch = Some(crate::ai_usage::spawn_keychain_active_refresh_token());
    }

    /// The autodetected active account name (Keychain refresh-token
    /// match). Cheap O(1) getter reading `cached_autodetected_...`,
    /// safe to call per-render. The cache is refreshed by
    /// `restamp_claude_active_flags` when the Keychain worker returns
    /// OR the account list is mutated. `None` when no cache is
    /// populated (Keychain not yet read, non-macOS, no match).
    pub fn autodetected_active_claude_account_name(&self) -> Option<String> {
        self.cached_autodetected_claude_account.clone()
    }

    /// Recompute the autodetect result from the current Keychain cache
    /// + per-account on-disk token files. This IS the disk-read pass —
    /// callers should invoke it only when state changes (Keychain
    /// worker returns, account list mutated), never per-render. The
    /// getter [`Self::autodetected_active_claude_account_name`] reads
    /// the cache field instead.
    fn recompute_autodetected_claude_account(&self) -> Option<String> {
        let keychain_rt = self.keychain_claude_refresh_token.as_deref()?;
        for account in self.config.claude_accounts() {
            let token_path = account.resolved_token_path();
            if let Some(disk_rt) = crate::ai_usage::read_refresh_token_from_path(&token_path)
                && disk_rt == keychain_rt
            {
                return Some(account.name);
            }
        }
        None
    }

    /// Reapply `is_active` to every entry in `ai_usage_claude_accounts`
    /// using the current autodetect state. Called after the Keychain
    /// worker returns so the panel + statusline reflect the new active
    /// account without waiting for the next per-account fetch cycle.
    /// Also refreshes the render-hot-path cache.
    pub fn restamp_claude_active_flags(&mut self) {
        let autodetected = self.recompute_autodetected_claude_account();
        self.cached_autodetected_claude_account = autodetected.clone();
        let active_names: std::collections::HashSet<String> = if let Some(name) = autodetected {
            std::iter::once(name).collect()
        } else {
            self.config
                .claude_accounts()
                .into_iter()
                .filter(|a| a.active)
                .map(|a| a.name)
                .collect()
        };
        for acc in self.ai_usage_claude_accounts.iter_mut() {
            acc.is_active = active_names.contains(&acc.name);
        }
    }

    /// `:ai.link_claude_token` — open a prompt for the user to
    /// paste their Claude Code OAuth token. Accepting writes to
    /// `~/.config/mnml/ai_token` (chmod 600) + kicks a fresh fetch.
    /// #1232 — capture the current keychain login and file it under
    /// whichever configured account it actually belongs to.
    ///
    /// Replaces the by-hand `security find-generic-password … >
    /// ai_token.<name>` step with one that verifies identity before
    /// writing, so logging in as the wrong account can't silently
    /// overwrite a good credential.
    pub fn recapture_claude_token_from_keychain(&mut self) {
        if self.pending_keychain_recapture.is_some() {
            self.toast("already capturing…".to_string());
            return;
        }
        let targets: Vec<crate::ai_usage::RecaptureTarget> = self
            .config
            .claude_accounts()
            .iter()
            .map(|a| crate::ai_usage::RecaptureTarget {
                name: a.name.clone(),
                token_path: a.resolved_token_path(),
                pinned_email: crate::ai_usage::pinned_email_for(&a.name),
            })
            .collect();
        if targets.is_empty() {
            self.toast("no Claude accounts configured".to_string());
            return;
        }
        self.toast("reading keychain + verifying identity…".to_string());
        self.pending_keychain_recapture = Some(crate::ai_usage::spawn_keychain_recapture(targets));
    }

    /// Per-tick drain for [`recapture_claude_token_from_keychain`].
    /// On success, force an immediate refetch so the repaired account
    /// lights up without waiting out the 5-minute throttle.
    pub fn drain_keychain_recapture(&mut self) {
        let Some(rx) = &self.pending_keychain_recapture else {
            return;
        };
        match rx.try_recv() {
            Ok(Ok(msg)) => {
                self.toast(msg);
                self.pending_keychain_recapture = None;
                self.ai_usage_claude_last_refresh_at.clear();
                self.ai_usage_pending_claude_accounts.clear();
                self.maybe_refresh_ai_usage();
            }
            Ok(Err(e)) => {
                self.toast(e);
                self.pending_keychain_recapture = None;
            }
            Err(std::sync::mpsc::TryRecvError::Empty) => {}
            Err(std::sync::mpsc::TryRecvError::Disconnected) => {
                self.pending_keychain_recapture = None;
            }
        }
    }

    pub fn open_link_claude_token_prompt(&mut self) {
        self.prompt = Some(crate::prompt::Prompt::new(
            crate::prompt::PromptKind::LinkClaudeToken,
            "Paste access token OR the whole claudeAiOauth JSON (keeps refresh token → no daily re-paste)",
        ));
    }

    /// Called from the prompt accept handler after the user pastes
    /// a token. Writes to disk + kicks the first fetch immediately.
    pub fn accept_link_claude_token(&mut self, token: String) {
        match crate::ai_usage::write_claude_token(&token) {
            Ok(path) => {
                self.toast(format!("linked → {}", path.display()));
                // Force an immediate refresh — bypass the 5-min
                // throttle so the chip lights up right away.
                self.ai_usage_last_refresh_at = 0;
                self.ai_usage_claude_last_refresh_at.clear();
                self.ai_usage_pending_claude_accounts.clear();
                self.maybe_refresh_ai_usage();
            }
            Err(e) => self.toast(format!("link failed: {e}")),
        }
    }

    /// Audit + repair `mnml-*` integration binaries that PATH resolves to
    /// a copy OTHER than `~/.cargo/bin/`. This is the root of the
    /// "why does my Amplify label keep reverting to the old one" bug:
    /// `cargo install --force` writes to `~/.cargo/bin/`, but a stale
    /// peer in (say) `~/.local/bin/` earlier in PATH silently wins on
    /// the follow-up `<integration> --install` — the stale binary writes
    /// its old manifest and everyone's confused.
    ///
    /// Repair strategy: move each shadowing copy to
    /// `<data_root>/quarantine/shadowed-bins/<name>.<epoch>` (mkdir'd
    /// on demand). Nothing is deleted — user can `mv` it back if they
    /// realize the "stale" one was actually load-bearing. Reports the
    /// count via toast; details captured in `.mnml/findings/…`.
    pub fn audit_shadowed_binaries(&mut self) {
        let hits = crate::integration_detect::find_shadowed_binaries();
        if hits.is_empty() {
            self.toast("no shadowed integration binaries detected");
            return;
        }
        let dest_root = crate::data_root::data_root()
            .join("quarantine")
            .join("shadowed-bins");
        if let Err(e) = std::fs::create_dir_all(&dest_root) {
            self.toast(format!("shadow audit: couldn't mkdir quarantine ({e})"));
            return;
        }
        let stamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs())
            .unwrap_or(0);
        let mut moved = 0usize;
        let mut errors = Vec::new();
        for hit in &hits {
            let dest = dest_root.join(format!("{}.{stamp}", hit.name));
            match std::fs::rename(&hit.active, &dest) {
                Ok(()) => moved += 1,
                Err(e) => errors.push(format!("{}: {e}", hit.name)),
            }
        }
        crate::integration_detect::clear_cache();
        if errors.is_empty() {
            self.toast(format!(
                "moved {moved} shadowed integration binaries → {}",
                dest_root.display()
            ));
        } else {
            self.toast(format!(
                "moved {moved}/{}; {} failed — see findings",
                hits.len(),
                errors.len()
            ));
        }
    }
}

#[cfg(test)]
mod fetch_error_tests {
    use super::apply_fetch_error;
    use crate::ai_usage::{ClaudeUsage, FetchErr};

    fn err(message: &str, needs_reauth: bool) -> FetchErr {
        FetchErr {
            message: message.to_string(),
            retry_after_secs: None,
            needs_reauth,
        }
    }

    /// The regression this exists for: a later error of a DIFFERENT
    /// kind has to clear a prior re-auth flag. `needs_reauth` is
    /// assigned, never OR'd — if someone "preserves more fields"
    /// while editing the #1217 snapshot-preservation logic directly
    /// above and reaches for `|=`, the pane would keep telling the
    /// user to run `claude login` forever, through every later 429,
    /// long after the credential was fixed.
    #[test]
    fn a_later_non_reauth_error_clears_the_reauth_flag() {
        let mut usage = ClaudeUsage::default();

        apply_fetch_error(&mut usage, err("that credential is other@x", true), 1_000);
        assert!(usage.needs_reauth, "re-auth failure should raise the flag");

        // A 429 is not a re-auth problem. The guided block must go.
        apply_fetch_error(&mut usage, err("http 429", false), 2_000);
        assert!(
            !usage.needs_reauth,
            "an unrelated later error must clear the flag, not OR into it"
        );
        assert_eq!(usage.last_error.as_deref(), Some("http 429"));
    }

    /// The readings survive an error — the whole point of #1217.
    /// A good five-minute-old number beats a fresh-looking zero.
    #[test]
    fn an_error_preserves_the_previous_readings() {
        let mut usage = ClaudeUsage {
            percent: 57,
            weekly_percent: 86,
            fetched_at: 500,
            ..Default::default()
        };

        apply_fetch_error(&mut usage, err("http 429", false), 2_000);

        assert_eq!(usage.percent, 57);
        assert_eq!(usage.weekly_percent, 86);
        assert_eq!(usage.fetched_at, 500, "age must stay honest");
    }

    /// `Retry-After` only moves the cooldown when the header was
    /// present; a plain failure must not silently arm one.
    #[test]
    fn a_failure_always_backs_off_even_without_a_retry_after_header() {
        // THIS TEST'S PROMISE CHANGED, and the old one was the bug.
        //
        // It used to assert that a failure with NO `Retry-After` header
        // left `retry_after_at` at 0 — i.e. no cooldown at all, so the
        // account retried on the normal cadence forever. Anthropic's 429
        // here is a JSON `rate_limit_error` whose header is often absent
        // or in HTTP-date form, so that path was the common one, and the
        // retry pressure never eased. User, with a screenshot of three
        // accounts all rate-limited: "dont hammer anthropic".
        let mut usage = ClaudeUsage::default();
        apply_fetch_error(&mut usage, err("boom", false), 2_000);
        assert!(
            usage.retry_after_at > 2_000,
            "a failure with no Retry-After got no cooldown at all"
        );
        assert_eq!(usage.consecutive_failures, 1);

        // Anthropic's own hint still WINS when it gives one.
        let mut usage = ClaudeUsage::default();
        let mut throttled = err("http 429", false);
        throttled.retry_after_secs = Some(300);
        apply_fetch_error(&mut usage, throttled, 2_000);
        assert_eq!(
            usage.retry_after_at, 2_300,
            "the server's Retry-After must win over our own backoff"
        );
    }

    /// Repeated failures must back off further, or a persistently broken
    /// account knocks at a fixed rate all day.
    #[test]
    fn consecutive_failures_back_off_further_each_time() {
        let mut usage = ClaudeUsage::default();
        let mut waits = Vec::new();
        for _ in 0..5 {
            apply_fetch_error(&mut usage, err("429", false), 1_000);
            waits.push(usage.retry_after_at - 1_000);
        }
        assert!(
            waits.windows(2).all(|w| w[1] >= w[0]),
            "backoff did not grow: {waits:?}"
        );
        assert!(
            waits[1] > waits[0],
            "second failure waited no longer than the first: {waits:?}"
        );
        // And it must be CAPPED, so an account can still recover on its
        // own rather than being parked for a day.
        assert!(
            *waits.last().unwrap() <= 60 * 60,
            "backoff exceeded the 1h cap: {waits:?}"
        );
    }

    /// A success clears the backoff, so one blip does not slow the
    /// account down permanently.
    #[test]
    fn a_successful_parse_resets_the_failure_count() {
        // `parse_claude_response` builds the success value, and it sets
        // the counter to 0 explicitly — asserted here rather than trusted
        // because the field defaults to 0 and would look correct either
        // way on a fresh value.
        let mut usage = ClaudeUsage {
            consecutive_failures: 4,
            ..Default::default()
        };
        apply_fetch_error(&mut usage, err("x", false), 10);
        assert_eq!(usage.consecutive_failures, 5, "counter should climb");
    }
}