pitboard-core 0.21.1

The engine behind Pitboard: switches Claude Code, Codex and Claude Desktop between accounts by parking and restoring their logins. Its API serves Pitboard's own front ends.
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
//! Where a home keeps its parked logins of Claude Code and Codex, as `pitboard vault` says it,
//! and a move of them between the forms its system offers, as Linux offers the Secret Service
//! and files.
//!
//! A move is recorded before anything is copied (`vault.moving_to`). Every park this home's
//! records name is written into the new form and read back, the save that names the new form
//! lists what leaves the old one (`vault.leaving`), and each is then deleted from there and taken
//! off the list. Every change settles first, and every renewal finishes a move first, so a move cut
//! short at any step is finished forward before anything else writes a park. A park
//! another home keeps in a vault every home shares is that home's, and stays where it is.

use super::Settled;
use crate::context::Context;
use crate::error::{Error, Result};
use crate::host::{Keyring, Vault};
use crate::permit::Permit;
use crate::state::{self, Leaving, ParkKind, State, VaultForm};
use crate::warning::Warning;
use crate::{fault, pending, store};
use std::path::PathBuf;

/// Where a home keeps its parked logins of Claude Code and Codex, from its files and from what
/// answers on this machine.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Keeping {
    /// `None` where the vault this home opens is none a person looks in.
    pub place: Option<Vault>,
    /// Whether every Pitboard directory of the Mac, or of the session on Linux, parks there.
    pub shared: bool,
    /// The folder of files this home keeps them in, or may move them to.
    pub folder: Option<PathBuf>,
    /// Whether a Secret Service answers on this session's bus, where this system parks in one.
    pub secret_service: Option<bool>,
    /// What answers for it, which the store is named for.
    pub keyring: Option<Keyring>,
    /// The form a move cut short goes to, which the next change finishes.
    pub moving_to: Option<VaultForm>,
    /// How many parks a move left in the form it moved from, which the next change deletes.
    pub leaving: Option<(VaultForm, usize)>,
}

/// What a move of the parks did.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Moved {
    /// The form the parks are kept in now.
    pub to: VaultForm,
    /// Whether they were kept another way before.
    pub changed: bool,
    /// The parked logins this home holds there now, from the form they were in.
    pub moved: usize,
    /// Those still in the form they were in, which the next change deletes from there.
    pub leaving: usize,
}

pub(crate) fn keeping(ctx: &Context) -> Result<Keeping> {
    let state = state::load(ctx)?;
    let host = ctx.host();
    let form = store::home_form(ctx)?;
    let place = store::vault_place(ctx);
    let in_files = matches!(place, Some(Vault::Files | Vault::SealedFiles))
        || host.vault_forms().contains(&VaultForm::Files);
    let recorded = state.vault.as_ref();
    Ok(Keeping {
        place,
        shared: host.vault_is_shared(form),
        folder: in_files.then(|| store::vault_dir(ctx)),
        secret_service: host
            .vault_forms()
            .contains(&VaultForm::SecretService)
            .then(|| host.answers(ctx, VaultForm::SecretService)),
        // After `answers`, whose question starts a keyring D-Bus can start.
        keyring: host
            .vault_forms()
            .contains(&VaultForm::SecretService)
            .then(|| host.keyring(ctx)),
        moving_to: recorded.and_then(|vault| vault.moving_to),
        leaving: recorded
            .and_then(|vault| vault.leaving.as_ref())
            .map(|leaving| (leaving.from, leaving.parks.len())),
    })
}

/// Moves every park this home holds into `to`. Refused where the system keeps parks one way, and
/// before anything is recorded where either form does not answer, as a locked login keyring.
pub(crate) fn move_to(settled: Settled, to: VaultForm) -> Result<(Moved, Vec<Warning>)> {
    let Settled {
        _exclusive,
        mut state,
        ctx,
        permit,
    } = settled;
    let host = ctx.host();
    if !host.vault_forms().contains(&to) {
        return Err(Error::VaultFormUnsupported { form: to });
    }
    let recorded = state.vault.as_ref().map(|vault| vault.form);
    let from = recorded
        .or_else(|| store::new_home_form(&ctx))
        .unwrap_or(to);
    if from == to {
        // A home that records no form yet takes the one its first save finds, which may not be
        // this: the person's command records it now.
        if recorded.is_none() {
            state.vault = Some(state::Vault::kept_as(to));
            state::save(&ctx, permit, &state)?;
        }
        return Ok(left(&ctx, permit, &mut state, to, false, 0));
    }
    // A move whose names were still in two forms would delete the parks it had just written.
    take_out(&ctx, permit, &mut state)?;
    let names = held(&ctx, &state);
    host.vault(&ctx, Some(from)).list()?;
    let there = host.vault(&ctx, Some(to)).list()?.unwrap_or_default();
    if host.vault_is_shared(Some(to)) {
        for name in names.iter().filter(|name| there.contains(name)) {
            state.written_elsewhere(name);
        }
    }
    state.vault = Some(state::Vault {
        form: from,
        moving_to: Some(to),
        leaving: None,
    });
    state::save(&ctx, permit, &state)?;
    fault::point("vault.recorded");
    let moved = forward(&ctx, permit, &mut state)?;
    Ok(left(&ctx, permit, &mut state, to, true, moved))
}

// What a move comes to once what it left where it was is deleted, as far as that goes now.
fn left(
    ctx: &Context,
    permit: Permit,
    state: &mut State,
    to: VaultForm,
    changed: bool,
    moved: usize,
) -> (Moved, Vec<Warning>) {
    let _ = take_out(ctx, permit, state);
    let leaving = leaving(state);
    let warnings = (leaving > 0)
        .then_some(Warning::ParksPendingRemoval(leaving))
        .into_iter()
        .collect();
    let moved = Moved {
        to,
        changed,
        moved,
        leaving,
    };
    (moved, warnings)
}

/// What settling does with a move of the parks: a move cut short is finished, and a change the
/// store refuses it for is refused with it; what a move left where it was is deleted, and tried
/// again at the next change where that fails.
pub(crate) fn finish(ctx: &Context, permit: Permit, state: &mut State) -> Result<()> {
    if state
        .vault
        .as_ref()
        .is_some_and(|vault| vault.moving_to.is_some())
    {
        forward(ctx, permit, state)?;
    }
    let _ = take_out(ctx, permit, state);
    Ok(())
}

/// How many parks a move left in the form it moved from.
pub(crate) fn leaving(state: &State) -> usize {
    state
        .vault
        .as_ref()
        .and_then(|vault| vault.leaving.as_ref())
        .map_or(0, |leaving| leaving.parks.len())
}

// Every name copied into the form a move goes to, then the save that names that form, and so
// how many parks this home holds there now. Each write reads its park back.
fn forward(ctx: &Context, permit: Permit, state: &mut State) -> Result<usize> {
    let Some(state::Vault {
        form: from,
        moving_to: Some(to),
        ..
    }) = state.vault.clone()
    else {
        return Ok(0);
    };
    let host = ctx.host();
    let (out, into) = (host.vault(ctx, Some(from)), host.vault(ctx, Some(to)));
    let names = held(ctx, state);
    let mut moved = 0;
    for name in &names {
        // There already, for the home that wrote it (`move_to`).
        if host.vault_is_shared(Some(to)) && state.is_foreign(name) {
            moved += 1;
            continue;
        }
        let Some(login) = out.read(name)? else {
            continue;
        };
        into.write(permit, name, &login)?;
        moved += 1;
        fault::point("vault.copied");
    }
    let mut parks: Vec<String> = names
        .iter()
        .filter(|name| !(host.vault_is_shared(Some(from)) && state.is_foreign(name)))
        .cloned()
        .collect();
    parks.extend(
        state
            .discarded
            .iter()
            .filter(|dropped| dropped.kind == ParkKind::Vault)
            .map(|dropped| dropped.service.clone()),
    );
    parks.sort();
    parks.dedup();
    state
        .discarded
        .retain(|dropped| dropped.kind != ParkKind::Vault);
    if !host.vault_is_shared(Some(to)) {
        for name in &names {
            state.used_here(name);
        }
    }
    state.vault = Some(state::Vault {
        form: to,
        moving_to: None,
        leaving: (!parks.is_empty()).then_some(Leaving { from, parks }),
    });
    state::save(ctx, permit, state)?;
    fault::point("vault.named");
    Ok(moved)
}

// Each park a move left deleted from the form it left, and listed no more. One that resists stays
// listed, and the first failure is said.
fn take_out(ctx: &Context, permit: Permit, state: &mut State) -> Result<()> {
    let Some(Leaving { from, parks }) =
        state.vault.as_ref().and_then(|vault| vault.leaving.clone())
    else {
        return Ok(());
    };
    let out = ctx.host().vault(ctx, Some(from));
    let mut failed = None;
    let mut kept = Vec::new();
    for name in &parks {
        match out.delete(permit, name) {
            Ok(()) => fault::point("vault.deleted"),
            Err(why) => {
                failed.get_or_insert(why);
                kept.push(name.clone());
            }
        }
    }
    if kept.len() < parks.len()
        && let Some(vault) = state.vault.as_mut()
    {
        vault.leaving = (!kept.is_empty()).then_some(Leaving { from, parks: kept });
        state::save(ctx, permit, state)?;
    }
    failed.map_or(Ok(()), |why| Err(why.into()))
}

// Every vault name this home's records name: each account's park, each renewal's copy, and each
// name written down before a login was written into it.
fn held(ctx: &Context, state: &State) -> Vec<String> {
    let mut names: Vec<String> = state
        .accounts
        .iter()
        .filter_map(|account| account.parked.as_ref())
        .filter(|park| park.kind == ParkKind::Vault)
        .map(|park| park.service.clone())
        .chain(
            state
                .renewing
                .iter()
                .map(|renewing| renewing.service.clone()),
        )
        .chain(pending::vault_names(ctx))
        .collect();
    names.sort();
    names.dedup();
    names
}

#[cfg(test)]
#[allow(
    clippy::disallowed_methods,
    reason = "a test reads the state file a home saved"
)]
mod tests {
    use super::super::harness::{
        self, NOW, Rig, account, machine_on, oauth, owner, parked_services, two_parked_on,
    };
    use super::super::{Due, renew_due, renew_parked, repair, settle, switch};
    use super::*;
    use crate::context::ScratchMachine;
    use crate::host::memory::MemoryHost;
    use crate::provider;
    use crate::state::Key;
    use crate::store::memory::Fault;
    use std::sync::Arc;

    fn moved(m: &Rig, to: VaultForm) -> Result<(Moved, Vec<Warning>)> {
        move_to(settle(&m.ctx, Permit::for_a_test(), None)?.0, to)
    }

    fn recorded(m: &Rig) -> Option<state::Vault> {
        state::load(&m.ctx).expect("state").vault
    }

    fn in_files(m: &Rig) -> Vec<String> {
        m.mem.files_vault(&m.ctx).services()
    }

    fn in_the_secret_service(m: &Rig) -> Vec<String> {
        m.mem.vault().services()
    }

    fn state_file(m: &Rig) -> Vec<u8> {
        std::fs::read(crate::home::dir(&m.ctx).join("state.json")).expect("saved")
    }

    // Every park this home holds is in `to`, none is left where it was, and nothing waits.
    fn all_in(m: &Rig, to: VaultForm, parks: &[String], after: &str) {
        assert_eq!(recorded(m), Some(state::Vault::kept_as(to)), "{after}");
        let (there, left) = match to {
            VaultForm::SecretService => (in_the_secret_service(m), in_files(m)),
            VaultForm::Files => (in_files(m), in_the_secret_service(m)),
        };
        assert_eq!(there, parks, "{after}");
        assert!(left.is_empty(), "{after}: {left:?}");
        harness::hold(m, after);
    }

    fn kept_on(host: Arc<MemoryHost>) -> (Keeping, PathBuf) {
        let scratch = tempfile::tempdir().expect("a scratch home");
        let root = scratch.path().to_path_buf();
        let machine = ScratchMachine::on(host, &root, |ctx| {
            ctx.with_pitboard_home(root.join(".pitboard"))
        });
        let kept = keeping(&machine.ctx).expect("read");
        (kept, root.join(".pitboard/vault"))
    }

    // As macOS keeps them, as Windows does, and as Linux does each way, a Secret Service there
    // or not.
    #[test]
    fn the_vault_says_where_a_home_keeps_its_parks_and_who_else_parks_there() {
        let (kept, _) = kept_on(MemoryHost::new());
        assert_eq!(
            kept,
            Keeping {
                place: Some(Vault::Keychain),
                shared: true,
                folder: None,
                secret_service: None,
                keyring: None,
                moving_to: None,
                leaving: None,
            }
        );

        let (kept, folder) = kept_on(MemoryHost::sealed());
        assert_eq!(
            kept,
            Keeping {
                place: Some(Vault::SealedFiles),
                shared: false,
                folder: Some(folder),
                secret_service: None,
                keyring: None,
                moving_to: None,
                leaving: None,
            }
        );

        let (kept, folder) = kept_on(MemoryHost::by_form());
        assert_eq!(
            kept,
            Keeping {
                place: Some(Vault::SecretService),
                shared: true,
                folder: Some(folder),
                secret_service: Some(true),
                keyring: Some(Keyring::Login),
                moving_to: None,
                leaving: None,
            }
        );

        let host = MemoryHost::by_form();
        host.secret_service_answers(false);
        let (kept, folder) = kept_on(host);
        assert_eq!(
            kept,
            Keeping {
                place: Some(Vault::Files),
                shared: false,
                folder: Some(folder),
                secret_service: Some(false),
                keyring: Some(Keyring::Login),
                moving_to: None,
                leaving: None,
            }
        );
    }

    // The person's command is the one thing that sets the form. On a home with no state file
    // yet, where its parks would go that way already, it is recorded all the same, so a Secret
    // Service that answers later changes nothing.
    #[test]
    fn a_move_on_a_new_home_records_the_form_though_nothing_moves() {
        let host = MemoryHost::by_form();
        host.secret_service_answers(false);
        let scratch = tempfile::tempdir().expect("a scratch home");
        let root = scratch.path().to_path_buf();
        let machine = ScratchMachine::on(Arc::clone(&host), &root, |ctx| {
            ctx.with_pitboard_home(root.join(".pitboard"))
        });
        let settled = settle(&machine.ctx, Permit::for_a_test(), None)
            .expect("nothing to settle")
            .0;

        let (done, said) = move_to(settled, VaultForm::Files).expect("kept");

        assert_eq!(
            done,
            Moved {
                to: VaultForm::Files,
                changed: false,
                moved: 0,
                leaving: 0,
            }
        );
        assert!(said.is_empty(), "{said:?}");
        host.secret_service_answers(true);
        assert_eq!(
            state::vault_form(&machine.ctx).expect("read"),
            Some(VaultForm::Files)
        );
        assert_eq!(
            store::home_form(&machine.ctx).expect("read"),
            Some(VaultForm::Files)
        );
    }

    // Into files and back, with a switch after each: the parks are read where the record says.
    #[test]
    fn a_move_to_files_leaves_every_park_in_files_and_none_in_the_secret_service() {
        let m = two_parked_on(MemoryHost::by_form(), "move-to-files");
        let parks = parked_services(&m);
        all_in(&m, VaultForm::SecretService, &parks, "before");

        let (done, said) = moved(&m, VaultForm::Files).expect("moved");
        assert_eq!(
            done,
            Moved {
                to: VaultForm::Files,
                changed: true,
                moved: 2,
                leaving: 0,
            }
        );
        assert!(said.is_empty(), "{said:?}");
        all_in(&m, VaultForm::Files, &parks, "in files");

        let (done, _) = moved(&m, VaultForm::SecretService).expect("moved back");
        assert_eq!((done.changed, done.moved), (true, 2));
        all_in(&m, VaultForm::SecretService, &parks, "back");

        let (done, _) = moved(&m, VaultForm::SecretService).expect("there already");
        assert_eq!((done.changed, done.moved), (false, 0));
        let settled = settle(&m.ctx, Permit::for_a_test(), None)
            .expect("settled")
            .0;
        switch(settled, &m.key("there")).expect("switched");
        assert_eq!(in_the_secret_service(&m), parked_services(&m));
        assert!(in_files(&m).is_empty());
    }

    #[test]
    fn a_move_to_the_secret_service_leaves_every_park_there_and_none_in_files() {
        let host = MemoryHost::by_form();
        host.secret_service_answers(false);
        let m = two_parked_on(Arc::clone(&host), "move-to-the-secret-service");
        host.secret_service_answers(true);
        let parks = parked_services(&m);
        all_in(&m, VaultForm::Files, &parks, "before");

        let (done, _) = moved(&m, VaultForm::SecretService).expect("moved");
        assert_eq!((done.changed, done.moved, done.leaving), (true, 2, 0));
        all_in(
            &m,
            VaultForm::SecretService,
            &parks,
            "in the Secret Service",
        );
    }

    // `there` of another home on the session's bus, and the park of `here` that home wrote, which
    // `repair` gave back here: neither leaves the Secret Service, and that home reads both.
    #[test]
    fn a_move_leaves_another_homes_parks_where_they_are() {
        let host = MemoryHost::by_form();
        let m = machine_on(Arc::clone(&host), "move-another-home");
        let other = m
            .ctx
            .clone()
            .with_pitboard_home(m.ctx_home().join(".pitboard-elsewhere"));
        let tool = provider::document(m.which);
        let mut theirs = State::default();
        for (label, refresh) in [("there", "there-elsewhere"), ("here", "here-elsewhere")] {
            m.api.owned_by(&format!("access-{refresh}"), owner(label));
            let service =
                crate::park::reserve(&other, Permit::for_a_test(), label).expect("a free name");
            let park = crate::park::store_at(
                &other,
                Permit::for_a_test(),
                tool,
                &service,
                &oauth(refresh, 30),
            )
            .expect("parked");
            theirs.accounts.push(account(label, label, Some(park)));
        }
        state::save(&other, Permit::for_a_test(), &theirs).expect("saved");
        let given = repair(
            settle(&m.ctx, Permit::for_a_test(), None)
                .expect("settled")
                .0,
        )
        .expect("repaired")
        .0
        .given_back;
        assert_eq!(given.len(), 1, "{given:?}");
        let ours = parked_services(&m);
        let elsewhere: Vec<String> = theirs
            .accounts
            .iter()
            .filter_map(|a| a.parked.as_ref())
            .map(|park| park.service.clone())
            .collect();
        let read_there = || {
            for account in &theirs.accounts {
                let park = account.parked.as_ref().expect("parked");
                crate::park::load(&other, tool, &Key::new(m.which, &account.label), park)
                    .expect("that home reads its own park");
            }
        };

        moved(&m, VaultForm::Files).expect("moved");

        assert_eq!(in_files(&m), ours);
        let mut session = elsewhere.clone();
        session.sort();
        assert_eq!(in_the_secret_service(&m), session);
        assert!(state::load(&m.ctx).expect("state").foreign.is_empty());
        read_there();
        harness::hold(&m, "in files");

        moved(&m, VaultForm::SecretService).expect("moved back");

        assert!(in_files(&m).is_empty(), "{:?}", in_files(&m));
        let mut session: Vec<String> = elsewhere.iter().chain(&ours).cloned().collect();
        session.sort();
        session.dedup();
        assert_eq!(in_the_secret_service(&m), session);
        assert_eq!(
            state::load(&m.ctx).expect("state").foreign,
            vec![given[0].1.clone()],
            "the park that home wrote is that home's to delete"
        );
        read_there();
    }

    // macOS keeps parks in the keychain alone and Windows in sealed files alone.
    #[test]
    fn a_move_is_refused_where_the_system_keeps_one_form() {
        let said = match crate::host::OS {
            crate::host::Os::MacOs => {
                "On macOS, Pitboard keeps parked logins only in the login keychain"
            }
            crate::host::Os::Windows => {
                "On Windows, Pitboard keeps parked logins only in files sealed to your Windows \
                 account"
            }
            crate::host::Os::Linux => "Pitboard keeps parked logins one way on this computer",
        };
        for (host, name) in [
            (MemoryHost::new(), "move-one-way-keychain"),
            (MemoryHost::sealed(), "move-one-way-sealed"),
        ] {
            let m = machine_on(Arc::clone(&host), name);
            let before = (state_file(&m), host.vault().services());
            for (to, into) in [
                (VaultForm::Files, "files"),
                (VaultForm::SecretService, "the login keyring"),
            ] {
                let refused = moved(&m, to).expect_err("one way only");
                assert_eq!(refused.code(), "vault_form_unsupported");
                assert_eq!(
                    refused.to_string(),
                    format!("{said}, so they cannot be moved into {into}. Nothing has changed.")
                );
            }
            assert_eq!((state_file(&m), host.vault().services()), before);
        }
    }

    // How a login keyring stops answering: locked, or nothing on the bus for it.
    type Locking = (&'static str, fn(&MemoryHost));

    // Asked before anything is recorded, both ways, whether it is locked or nothing answers.
    #[test]
    fn a_move_into_a_locked_keyring_changes_nothing() {
        let lockings: [Locking; 2] = [
            ("locked", |host| host.vault().fault_all(Fault::Locked)),
            ("gone", |host| host.secret_service_answers(false)),
        ];
        for (how, lock) in lockings {
            let host = MemoryHost::by_form();
            host.secret_service_answers(false);
            let m = two_parked_on(Arc::clone(&host), &format!("move-into-{how}"));
            host.secret_service_answers(true);
            lock(&host);
            let before = (state_file(&m), in_files(&m), in_the_secret_service(&m));

            let refused = moved(&m, VaultForm::SecretService).expect_err("the keyring refuses");

            assert_eq!(refused.code(), "credential_store_locked", "{how}");
            assert_eq!(
                (state_file(&m), in_files(&m), in_the_secret_service(&m)),
                before,
                "{how}"
            );

            let host = MemoryHost::by_form();
            let m = two_parked_on(Arc::clone(&host), &format!("move-out-of-{how}"));
            lock(&host);
            let before = (state_file(&m), in_files(&m), in_the_secret_service(&m));

            let refused = moved(&m, VaultForm::Files).expect_err("the keyring refuses");

            assert_eq!(refused.code(), "credential_store_locked", "{how}");
            assert_eq!(
                (state_file(&m), in_files(&m), in_the_secret_service(&m)),
                before,
                "{how}"
            );
        }
    }

    // The keyring locks once the parks are in files: they are read there, and the copies left in
    // the keyring wait, said, until a change finds it open.
    #[test]
    fn what_a_move_cannot_delete_where_it_was_is_listed_and_deleted_by_the_next_change() {
        let host = MemoryHost::by_form();
        let m = two_parked_on(Arc::clone(&host), "move-left-behind");
        let parks = parked_services(&m);
        let locks = {
            let host = Arc::clone(&host);
            move || host.vault().fault_all(Fault::Locked)
        };

        let (done, said) =
            fault::meanwhile("vault.named", locks, || moved(&m, VaultForm::Files)).expect("moved");

        assert_eq!((done.changed, done.moved, done.leaving), (true, 2, 2));
        assert_eq!(said.len(), 1, "{said:?}");
        assert_eq!(said[0].code(), "parks_pending_removal");
        assert_eq!(in_files(&m), parks);
        assert_eq!(in_the_secret_service(&m), parks);
        let kept = keeping(&m.ctx).expect("read");
        assert_eq!(kept.leaving, Some((VaultForm::SecretService, 2)));
        assert_eq!(kept.place, Some(Vault::Files));
        let refused = moved(&m, VaultForm::SecretService).expect_err("still leaving the keyring");
        assert_eq!(refused.code(), "credential_store_locked");

        host.vault().heal_all();
        settle(&m.ctx, Permit::for_a_test(), None).expect("settled");
        all_in(&m, VaultForm::Files, &parks, "once the keyring opens");
    }

    // Every park due, its access lapsed, and each renewal answered.
    fn due_on(host: Arc<MemoryHost>, name: &str) -> Rig {
        let m = two_parked_on(host, name);
        harness::renews(&m, "there-refresh", "there-refresh-2");
        harness::renews(&m, "away-refresh", "away-refresh-2");
        let mut state = state::load(&m.ctx).expect("state");
        for account in &mut state.accounts {
            if let Some(park) = account.parked.as_mut() {
                park.access_expires_at = Some(NOW - 60);
            }
        }
        state::save(&m.ctx, Permit::for_a_test(), &state).expect("saved");
        m
    }

    fn renewals(run: &[(Key, super::super::Renewal)]) -> Vec<&'static str> {
        run.iter().map(|(_, outcome)| outcome.code()).collect()
    }

    // The renewal inside every `pitboard` writes a park, which a move cut short would leave in
    // the form it is leaving, so it finishes the move first, under the lock it holds, as a change
    // does. Waiting for a change instead let parks lapse where only the schedule runs.
    #[test]
    fn a_renewal_finishes_a_move_cut_short_before_it_renews() {
        for scheduled in [false, true] {
            let m = due_on(MemoryHost::by_form(), &format!("move-renewal-{scheduled}"));
            let died = fault::killing("vault.copied", || moved(&m, VaultForm::Files));
            assert_eq!(died.unwrap_err(), "vault.copied");
            let ctx = if scheduled {
                m.ctx.clone().started_by_the_schedule()
            } else {
                m.ctx.clone()
            };

            let run = renew_due(&ctx, Permit::for_a_test(), Due::ToStayAlive);

            assert_eq!(renewals(&run), ["renewed", "renewed"], "{scheduled}");
            all_in(
                &m,
                VaultForm::Files,
                &parked_services(&m),
                "renewed in files",
            );
            assert_eq!(
                crate::schedule::refused(&m.ctx, crate::schedule::Job::Renew),
                None
            );
        }
    }

    // As any store's refusal of a renewal: nothing is renewed or moved, and a run the schedule
    // started keeps it for doctor to say, where a run by hand keeps nothing.
    #[test]
    fn a_renewal_the_store_keeps_from_finishing_a_move_renews_nothing_and_says_so() {
        let m = due_on(MemoryHost::by_form(), "move-renewal-locked");
        let died = fault::killing("vault.copied", || moved(&m, VaultForm::Files));
        assert_eq!(died.unwrap_err(), "vault.copied");
        m.mem.vault().fault_all(Fault::Locked);
        let before = (state_file(&m), in_files(&m));

        assert!(renew_parked(&m.ctx, Permit::for_a_test()).is_empty());
        assert_eq!(
            crate::schedule::refused(&m.ctx, crate::schedule::Job::Renew),
            None,
            "by hand"
        );
        let scheduled = m.ctx.clone().started_by_the_schedule();
        assert!(renew_due(&scheduled, Permit::for_a_test(), Due::ToStayAlive).is_empty());

        assert_eq!((state_file(&m), in_files(&m)), before);
        let kept =
            crate::schedule::refused(&m.ctx, crate::schedule::Job::Renew).expect("kept for doctor");
        assert_eq!(
            kept.codes.into_iter().collect::<Vec<_>>(),
            ["credential_store_locked"]
        );
        assert_eq!(kept.vault, Some(Vault::SecretService));
    }
}