agent86-core 1.2.3

The agent behind agent86: a safe agent runtime you can embed
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
//! API key resolution and secret redaction, matching the Python `secrets.py`.
//!
//! Precedence is strict: the environment variable named by a provider's
//! `api_key_env` wins, and only when it is unset is the OS keyring consulted.
//! Keyring entries use service `agent86` and the config section name
//! (`openrouter`, `groq`, ...) as the account, so keys stored by the Python
//! implementation are found here too.

/// The keyring service name shared with the Python implementation.
pub const SERVICE_NAME: &str = "agent86";

/// Somewhere API keys may be stored, consulted after the environment.
pub trait SecretStore {
    /// The secret for `account` under `service`, or `None` when absent or unreadable.
    fn get_password(&self, service: &str, account: &str) -> Option<String>;

    /// Store (silently overwriting) the secret for `account` under `service`.
    fn set_password(
        &self,
        _service: &str,
        _account: &str,
        _secret: &str,
    ) -> Result<(), SecretStoreError> {
        Err(SecretStoreError::read_only())
    }

    /// Delete the secret for `account`; `Ok(false)` when none was stored.
    fn delete_password(&self, _service: &str, _account: &str) -> Result<bool, SecretStoreError> {
        Err(SecretStoreError::read_only())
    }

    /// Whether keys can be kept here at all, as Python's `keyring_available`; it
    /// tells "no backend" apart from "no key stored".
    fn available(&self) -> bool {
        true
    }
}

/// The keyring refused to store or delete a secret.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{0}")]
pub struct SecretStoreError(pub String);

impl SecretStoreError {
    fn read_only() -> Self {
        Self("this secret store is read-only".to_owned())
    }
}

/// The operating system's credential store: Windows Credential Manager, the
/// macOS Keychain, or the Secret Service on Linux and other Unix systems.
///
/// Every failure, including a missing backend on a headless machine, reads as
/// "no key stored", so lookups degrade to environment-only as in Python.
#[derive(Debug, Clone, Copy, Default)]
pub struct OsKeyring;

impl SecretStore for OsKeyring {
    fn get_password(&self, service: &str, account: &str) -> Option<String> {
        os_keyring::get_password(service, account)
    }

    fn set_password(
        &self,
        service: &str,
        account: &str,
        secret: &str,
    ) -> Result<(), SecretStoreError> {
        os_keyring::set_password(service, account, secret).map_err(SecretStoreError)
    }

    fn delete_password(&self, service: &str, account: &str) -> Result<bool, SecretStoreError> {
        os_keyring::delete_password(service, account).map_err(SecretStoreError)
    }
}

/// A store the vault can hold on to and use from any thread.
pub type SharedStore = std::sync::Arc<dyn SecretStore + Send + Sync>;

/// A store with nothing in it, for callers that must not touch the OS keyring.
#[derive(Debug, Clone, Copy, Default)]
pub struct NoSecretStore;

impl SecretStore for NoSecretStore {
    fn get_password(&self, _service: &str, _account: &str) -> Option<String> {
        None
    }

    fn available(&self) -> bool {
        false
    }
}

/// Resolve a provider's API key: the environment variable first, then `store`.
///
/// `provider_name` is the config section name, which is the keyring account.
/// A missing `api_key_env` means a keyless endpoint, so neither the environment
/// nor the keyring is consulted and stray keyring entries cannot leak into it.
pub fn resolve_api_key(
    provider_name: &str,
    api_key_env: Option<&str>,
    store: &dyn SecretStore,
) -> Option<String> {
    let name = api_key_env.filter(|name| !name.is_empty())?;
    if let Some(value) = std::env::var(name).ok().filter(|value| !value.is_empty()) {
        return Some(value);
    }
    store.get_password(SERVICE_NAME, provider_name)
}

/// One generic credential in the Windows Credential Manager.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VaultCredential {
    pub username: String,
    pub password: String,
}

/// Generic credentials addressed by target name, as in the Windows Credential Manager.
pub trait CredentialVault {
    fn read(&self, target: &str) -> Option<VaultCredential>;
    /// Create or replace the credential at `target`, including its user name.
    fn write(&self, target: &str, username: &str, password: &str) -> Result<(), String>;
    fn delete(&self, target: &str) -> Result<(), String>;
}

/// Find `account`'s secret using the Python `keyring` package's Windows layout.
///
/// Python stores the most recently saved account under target name `service`
/// and moves any account it displaces to `account@service`. A lookup therefore
/// tries `service` first, accepting it only when its user name matches, and
/// then `account@service`.
pub fn resolve_windows_credential(
    vault: &dyn CredentialVault,
    service: &str,
    account: &str,
) -> Option<String> {
    match vault.read(service) {
        Some(credential) if credential.username == account => Some(credential.password),
        _ => vault
            .read(&compound_target(account, service))
            .map(|credential| credential.password),
    }
}

/// Store `account`'s secret in the Python `keyring` package's Windows layout.
///
/// As in Python, a different account already at `service` is first moved to
/// `<its account>@service`, and the new secret takes the `service` target.
/// Unlike Python, re-saving the same account does not leave a stale copy of the
/// old secret at `account@service`; lookups check `service` first either way.
pub fn store_windows_credential(
    vault: &dyn CredentialVault,
    service: &str,
    account: &str,
    secret: &str,
) -> Result<(), String> {
    if let Some(existing) = vault.read(service) {
        if existing.username != account {
            let target = compound_target(&existing.username, service);
            vault.write(&target, &existing.username, &existing.password)?;
        }
    }
    vault.write(service, account, secret)
}

/// Delete `account`'s secret from both Windows targets, as Python does.
///
/// Only credentials whose user name is `account` are touched. Returns whether
/// anything was deleted.
pub fn delete_windows_credential(
    vault: &dyn CredentialVault,
    service: &str,
    account: &str,
) -> Result<bool, String> {
    let mut deleted = false;
    for target in [service.to_owned(), compound_target(account, service)] {
        if vault.read(&target).is_some_and(|c| c.username == account) {
            vault.delete(&target)?;
            deleted = true;
        }
    }
    Ok(deleted)
}

fn compound_target(account: &str, service: &str) -> String {
    format!("{account}@{service}")
}

#[cfg(all(feature = "os-keyring", windows))]
mod os_keyring {
    use super::{
        delete_windows_credential, resolve_windows_credential, store_windows_credential,
        CredentialVault, VaultCredential,
    };
    use keyring_core::api::CredentialStoreApi;
    use keyring_core::Entry;
    use std::collections::HashMap;
    use std::sync::Arc;
    use windows_native_keyring_store::Store;

    /// The Credential Manager, addressed by explicit target names.
    struct Vault {
        store: Arc<Store>,
        service: String,
    }

    impl Vault {
        fn open(service: &str) -> Result<Self, String> {
            let store = Store::new().map_err(|error| error.to_string())?;
            Ok(Self {
                store,
                service: service.to_owned(),
            })
        }

        fn entry(&self, target: &str, username: &str) -> Result<Entry, String> {
            let modifiers = HashMap::from([("target", target)]);
            self.store
                .build(&self.service, username, Some(&modifiers))
                .map_err(|error| error.to_string())
        }
    }

    impl CredentialVault for Vault {
        fn read(&self, target: &str) -> Option<VaultCredential> {
            let entry = self.entry(target, &self.service).ok()?;
            let password = entry.get_password().ok()?;
            let username = entry
                .get_attributes()
                .ok()?
                .remove("username")
                .unwrap_or_default();
            Some(VaultCredential { username, password })
        }

        fn write(&self, target: &str, username: &str, password: &str) -> Result<(), String> {
            // The store leaves UserName empty on a credential created under an
            // explicit target, and keeps the old one when overwriting, so set it
            // explicitly: lookups match on it, as Python's do.
            let entry = self.entry(target, username)?;
            entry
                .set_password(password)
                .map_err(|error| error.to_string())?;
            entry
                .update_attributes(&HashMap::from([("username", username)]))
                .map_err(|error| error.to_string())
        }

        fn delete(&self, target: &str) -> Result<(), String> {
            let entry = self.entry(target, &self.service)?;
            entry.delete_credential().map_err(|error| error.to_string())
        }
    }

    pub fn get_password(service: &str, account: &str) -> Option<String> {
        resolve_windows_credential(&Vault::open(service).ok()?, service, account)
    }

    pub fn set_password(service: &str, account: &str, secret: &str) -> Result<(), String> {
        store_windows_credential(&Vault::open(service)?, service, account, secret)
    }

    pub fn delete_password(service: &str, account: &str) -> Result<bool, String> {
        delete_windows_credential(&Vault::open(service)?, service, account)
    }
}

#[cfg(all(feature = "os-keyring", target_os = "macos"))]
mod os_keyring {
    use keyring_core::api::CredentialStoreApi;
    use keyring_core::Entry;

    /// A generic password with the service and account attributes, as Python stores it.
    fn entry(service: &str, account: &str) -> Result<Entry, String> {
        let store = apple_native_keyring_store::keychain::Store::new()
            .map_err(|error| error.to_string())?;
        store
            .build(service, account, None)
            .map_err(|error| error.to_string())
    }

    pub fn get_password(service: &str, account: &str) -> Option<String> {
        entry(service, account).ok()?.get_password().ok()
    }

    pub fn set_password(service: &str, account: &str, secret: &str) -> Result<(), String> {
        entry(service, account)?
            .set_password(secret)
            .map_err(|error| error.to_string())
    }

    pub fn delete_password(service: &str, account: &str) -> Result<bool, String> {
        super::delete_entry(entry(service, account)?)
    }
}

#[cfg(all(feature = "os-keyring", unix, not(target_os = "macos")))]
mod os_keyring {
    use keyring_core::api::CredentialStoreApi;
    use keyring_core::Entry;

    /// An item matched on its `service` and `username` attributes, as Python stores it.
    fn entry(service: &str, account: &str) -> Result<Entry, String> {
        let store =
            zbus_secret_service_keyring_store::Store::new().map_err(|error| error.to_string())?;
        store
            .build(service, account, None)
            .map_err(|error| error.to_string())
    }

    pub fn get_password(service: &str, account: &str) -> Option<String> {
        entry(service, account).ok()?.get_password().ok()
    }

    pub fn set_password(service: &str, account: &str, secret: &str) -> Result<(), String> {
        entry(service, account)?
            .set_password(secret)
            .map_err(|error| error.to_string())
    }

    pub fn delete_password(service: &str, account: &str) -> Result<bool, String> {
        super::delete_entry(entry(service, account)?)
    }
}

/// No keyring: an unsupported platform, or a build without the `os-keyring`
/// feature. Lookups read as "no key stored", as a headless machine's do.
#[cfg(any(not(feature = "os-keyring"), not(any(windows, unix))))]
mod os_keyring {
    #[cfg(feature = "os-keyring")]
    const UNSUPPORTED: &str = "no OS keyring is supported on this platform";
    #[cfg(not(feature = "os-keyring"))]
    const UNSUPPORTED: &str = "agent86-core was built without the os-keyring feature";

    pub fn get_password(_service: &str, _account: &str) -> Option<String> {
        None
    }

    pub fn set_password(_service: &str, _account: &str, _secret: &str) -> Result<(), String> {
        Err(UNSUPPORTED.to_owned())
    }

    pub fn delete_password(_service: &str, _account: &str) -> Result<bool, String> {
        Err(UNSUPPORTED.to_owned())
    }
}

/// Delete one keyring entry; a missing entry is `Ok(false)`, not an error.
#[cfg(all(feature = "os-keyring", unix))]
fn delete_entry(entry: keyring_core::Entry) -> Result<bool, String> {
    match entry.delete_credential() {
        Ok(()) => Ok(true),
        Err(keyring_core::Error::NoEntry) => Ok(false),
        Err(error) => Err(error.to_string()),
    }
}

/// Check a pasted API key before it is stored, returning it trimmed.
///
/// Catches the mistakes that make a stored key silently fail later: nothing
/// entered, embedded whitespace, or a key pasted more than once (a terminal
/// whose hidden prompt misbehaves can repeat a paste).
pub fn validate_api_key(raw: &str) -> Result<String, String> {
    let key = raw.trim();
    if key.is_empty() {
        return Err("No key was entered.".to_owned());
    }
    if key.chars().any(char::is_whitespace) {
        return Err("The key contains whitespace; paste only the key itself.".to_owned());
    }
    let probe_len = 16;
    if key.len() >= probe_len * 2 && key.is_char_boundary(probe_len) {
        let probe = &key[..probe_len];
        // From the second character, not the second byte: a paste that starts with
        // a multi-byte character (a smart quote) must not panic the slice.
        let first = key.chars().next().map_or(0, char::len_utf8);
        if key[first..].contains(probe) {
            return Err(format!(
                "The key looks like it was pasted more than once (its first {probe_len} characters \
                 appear again). Paste it once and try again."
            ));
        }
    }
    Ok(key.to_owned())
}

/// Replacement text for a redacted secret, matching the Python implementation.
pub const REDACTED: &str = "***redacted***";

/// Credential prefixes and the minimum length of the body that follows them.
///
/// Deliberately conservative: a false positive only makes an error message less
/// precise, while a false negative leaks a key.
const KEY_SHAPES: &[KeyShape] = &[
    KeyShape::new("sk-", 16, is_sk_body),
    KeyShape::new("gsk_", 20, is_alphanumeric_body),
    KeyShape::new("xai-", 20, is_alphanumeric_body),
    KeyShape::new("AIza", 20, is_google_body),
];

struct KeyShape {
    prefix: &'static str,
    min_body: usize,
    is_body: fn(char) -> bool,
}

impl KeyShape {
    const fn new(prefix: &'static str, min_body: usize, is_body: fn(char) -> bool) -> Self {
        Self {
            prefix,
            min_body,
            is_body,
        }
    }
}

/// Return `text` with every known secret and key-shaped token replaced.
///
/// Known secrets shorter than eight characters are ignored, as in the Python
/// implementation, so that short values cannot blank out unrelated text.
pub fn redact(text: &str, secrets: &[&str]) -> String {
    let mut out = text.to_owned();
    for secret in secrets {
        if secret.len() >= 8 {
            out = out.replace(secret, REDACTED);
        }
    }
    redact_key_shapes(&out, REDACTED)
}

/// Replace every key-shaped token (Python's `_KEY_SHAPES`) in `text` with `replacement`.
pub fn redact_key_shapes(text: &str, replacement: &str) -> String {
    let mut out = String::with_capacity(text.len());
    let mut rest = text;
    let mut previous: Option<char> = None;

    'scan: while let Some(ch) = rest.chars().next() {
        if !previous.is_some_and(is_word_char) {
            for &KeyShape {
                prefix,
                min_body,
                is_body,
            } in KEY_SHAPES
            {
                if let Some(after) = rest.strip_prefix(prefix) {
                    let body = &after[..after.find(|c: char| !is_body(c)).unwrap_or(after.len())];
                    // A regex `\b` would backtrack off trailing punctuation such as `.`.
                    let body_len = body.trim_end_matches(|c: char| !is_word_char(c)).len();
                    let token_end = prefix.len() + body_len;
                    let next = rest[token_end..].chars().next();
                    if body_len >= min_body && !next.is_some_and(is_word_char) {
                        out.push_str(replacement);
                        previous = rest[..token_end].chars().next_back();
                        rest = &rest[token_end..];
                        continue 'scan;
                    }
                }
            }
        }
        out.push(ch);
        previous = Some(ch);
        rest = &rest[ch.len_utf8()..];
    }
    out
}

fn is_word_char(ch: char) -> bool {
    ch.is_alphanumeric() || ch == '_'
}

fn is_alphanumeric_body(ch: char) -> bool {
    ch.is_ascii_alphanumeric()
}

fn is_sk_body(ch: char) -> bool {
    ch.is_ascii_alphanumeric() || matches!(ch, '.' | '_' | '-')
}

fn is_google_body(ch: char) -> bool {
    ch.is_ascii_alphanumeric() || matches!(ch, '_' | '-')
}

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

    use std::collections::HashMap;

    /// An in-memory store keyed by (service, account).
    struct FakeStore(HashMap<(String, String), String>);

    impl FakeStore {
        fn with(account: &str, secret: &str) -> Self {
            let key = (SERVICE_NAME.to_owned(), account.to_owned());
            Self(HashMap::from([(key, secret.to_owned())]))
        }
    }

    impl SecretStore for FakeStore {
        fn get_password(&self, service: &str, account: &str) -> Option<String> {
            self.0
                .get(&(service.to_owned(), account.to_owned()))
                .cloned()
        }
    }

    #[test]
    fn the_environment_wins_over_the_keyring() {
        let store = FakeStore::with("openrouter", "from-keyring");
        std::env::set_var("AGENT86_TEST_ENV_WINS", "from-env");
        let key = resolve_api_key("openrouter", Some("AGENT86_TEST_ENV_WINS"), &store);
        std::env::remove_var("AGENT86_TEST_ENV_WINS");
        assert_eq!(key.as_deref(), Some("from-env"));
    }

    #[test]
    fn the_keyring_is_used_when_the_variable_is_unset_or_empty() {
        let store = FakeStore::with("openrouter", "from-keyring");
        let unset = resolve_api_key("openrouter", Some("AGENT86_TEST_UNSET_VAR"), &store);
        assert_eq!(unset.as_deref(), Some("from-keyring"));

        std::env::set_var("AGENT86_TEST_EMPTY_VAR", "");
        let empty = resolve_api_key("openrouter", Some("AGENT86_TEST_EMPTY_VAR"), &store);
        std::env::remove_var("AGENT86_TEST_EMPTY_VAR");
        assert_eq!(empty.as_deref(), Some("from-keyring"));

        assert_eq!(
            resolve_api_key("groq", Some("AGENT86_TEST_UNSET_VAR"), &store),
            None
        );
    }

    #[test]
    fn keyless_endpoints_never_consult_the_keyring() {
        let store = FakeStore::with("ollama", "stray");
        assert_eq!(resolve_api_key("ollama", None, &store), None);
        assert_eq!(resolve_api_key("ollama", Some(""), &store), None);
    }

    /// An in-memory Credential Manager following the `CredentialVault` contract.
    #[derive(Default)]
    struct FakeVault(std::cell::RefCell<HashMap<String, VaultCredential>>);

    impl FakeVault {
        fn with(entries: &[(&str, &str, &str)]) -> Self {
            let vault = Self::default();
            for (target, username, password) in entries {
                vault.0.borrow_mut().insert(
                    (*target).to_owned(),
                    VaultCredential {
                        username: (*username).to_owned(),
                        password: (*password).to_owned(),
                    },
                );
            }
            vault
        }

        fn get(&self, target: &str) -> Option<(String, String)> {
            let entries = self.0.borrow();
            let credential = entries.get(target)?;
            Some((credential.username.clone(), credential.password.clone()))
        }

        fn targets(&self) -> Vec<String> {
            let mut targets: Vec<String> = self.0.borrow().keys().cloned().collect();
            targets.sort();
            targets
        }
    }

    impl CredentialVault for FakeVault {
        fn read(&self, target: &str) -> Option<VaultCredential> {
            self.0.borrow().get(target).cloned()
        }

        fn write(&self, target: &str, username: &str, password: &str) -> Result<(), String> {
            let credential = VaultCredential {
                username: username.to_owned(),
                password: password.to_owned(),
            };
            self.0.borrow_mut().insert(target.to_owned(), credential);
            Ok(())
        }

        fn delete(&self, target: &str) -> Result<(), String> {
            self.0.borrow_mut().remove(target);
            Ok(())
        }
    }

    fn pair(username: &str, password: &str) -> Option<(String, String)> {
        Some((username.to_owned(), password.to_owned()))
    }

    #[test]
    fn windows_lookups_follow_the_python_keyring_layout() {
        // Python keeps the latest account at "agent86" and moves older ones to "<account>@agent86".
        let vault = FakeVault::with(&[
            ("agent86", "groq", "groq-key"),
            ("openrouter@agent86", "openrouter", "openrouter-key"),
        ]);
        assert_eq!(
            resolve_windows_credential(&vault, "agent86", "groq").as_deref(),
            Some("groq-key")
        );
        assert_eq!(
            resolve_windows_credential(&vault, "agent86", "openrouter").as_deref(),
            Some("openrouter-key")
        );
        assert_eq!(
            resolve_windows_credential(&vault, "agent86", "openai"),
            None
        );
    }

    #[test]
    fn windows_lookups_find_a_lone_account_under_the_service_name() {
        let vault = FakeVault::with(&[("agent86", "openrouter", "only-key")]);
        assert_eq!(
            resolve_windows_credential(&vault, "agent86", "openrouter").as_deref(),
            Some("only-key")
        );
        assert_eq!(resolve_windows_credential(&vault, "agent86", "groq"), None);
    }

    #[test]
    fn windows_stores_move_the_previous_account_aside_like_python() {
        let vault = FakeVault::default();
        store_windows_credential(&vault, "agent86", "groq", "groq-key").unwrap();
        store_windows_credential(&vault, "agent86", "openrouter", "openrouter-key").unwrap();

        assert_eq!(vault.get("agent86"), pair("openrouter", "openrouter-key"));
        assert_eq!(vault.get("groq@agent86"), pair("groq", "groq-key"));
        for account in ["groq", "openrouter"] {
            assert_eq!(
                resolve_windows_credential(&vault, "agent86", account),
                Some(format!("{account}-key"))
            );
        }
    }

    #[test]
    fn windows_resaving_an_account_replaces_it_without_a_stale_copy() {
        let vault = FakeVault::with(&[("agent86", "openrouter", "old-key")]);
        store_windows_credential(&vault, "agent86", "openrouter", "new-key").unwrap();

        assert_eq!(vault.get("agent86"), pair("openrouter", "new-key"));
        assert_eq!(vault.targets(), ["agent86"]);
    }

    #[test]
    fn windows_resaving_a_moved_account_takes_the_service_target_again() {
        let vault = FakeVault::with(&[
            ("agent86", "groq", "groq-key"),
            ("openrouter@agent86", "openrouter", "old-key"),
        ]);
        store_windows_credential(&vault, "agent86", "openrouter", "new-key").unwrap();

        assert_eq!(vault.get("agent86"), pair("openrouter", "new-key"));
        assert_eq!(vault.get("groq@agent86"), pair("groq", "groq-key"));
        assert_eq!(
            resolve_windows_credential(&vault, "agent86", "openrouter").as_deref(),
            Some("new-key")
        );
    }

    #[test]
    fn windows_deletes_touch_only_the_named_account() {
        let vault = FakeVault::with(&[
            ("agent86", "groq", "groq-key"),
            ("openrouter@agent86", "openrouter", "openrouter-key"),
        ]);

        assert!(delete_windows_credential(&vault, "agent86", "openrouter").unwrap());
        assert_eq!(vault.targets(), ["agent86"]);
        assert!(!delete_windows_credential(&vault, "agent86", "openrouter").unwrap());
        assert!(!delete_windows_credential(&vault, "agent86", "openai").unwrap());
        assert_eq!(vault.get("agent86"), pair("groq", "groq-key"));
    }

    #[test]
    fn pasted_keys_are_checked_before_storing() {
        let key = "sk-or-v1-0123456789abcdef0123456789abcdef0123456789abcdef0123456789ab";
        assert_eq!(validate_api_key(&format!("  {key}\r\n")).unwrap(), key);
        assert!(validate_api_key("   \n").is_err());
        assert!(validate_api_key("sk-or-v1-abc def").is_err());

        let repeated = key.repeat(4);
        let error = validate_api_key(&repeated).unwrap_err();
        assert!(error.contains("pasted more than once"), "{error}");

        // A paste wrapped in smart quotes starts with a three-byte character; this
        // used to panic slicing at byte 1.
        let quoted = format!("\u{201c}{key}\u{201d}");
        assert_eq!(validate_api_key(&quoted).unwrap(), quoted);
        let doubled = format!("\u{201c}{key}\u{201c}{key}");
        assert!(validate_api_key(&doubled).is_err());
    }

    #[test]
    fn known_secrets_and_key_shapes_are_redacted() {
        let text = "key my-secret-value and sk-or-v1-abcdef0123456789abcdef in body";
        assert_eq!(
            redact(text, &["my-secret-value"]),
            "key ***redacted*** and ***redacted*** in body"
        );
    }

    #[test]
    fn short_secrets_and_short_tokens_are_left_alone() {
        assert_eq!(redact("abc sk-short", &["abc"]), "abc sk-short");
    }

    #[test]
    fn key_prefixes_inside_words_are_not_tokens() {
        let text = "task-0123456789abcdef0123 gsk_0123456789abcdefghijXYZ";
        assert_eq!(
            redact(text, &[]),
            "task-0123456789abcdef0123 ***redacted***"
        );
    }

    #[test]
    fn trailing_punctuation_is_kept() {
        assert_eq!(
            redact("bad key sk-0123456789abcdef0123.", &[]),
            "bad key ***redacted***."
        );
    }
}