pitboard-core 0.11.0

The engine behind Pitboard: parking and restoring Claude Code and Codex logins. 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
//! What a switch does when it should not happen at all.
//!
//! A switch used to ask Anthropic twice about the login it was throwing away and never
//! once about the login it was installing. So an account whose refresh chain had been
//! revoked, signed out elsewhere, or refused installed cleanly, read back cleanly, and
//! reported a switch; the person found out the next time they ran `claude`, by which point
//! the login they had left was parked and the one they had arrived at did not work.

use super::harness::{audit_lines, machine, owner, recover, state_file};
use super::*;
use crate::api::scripted::Trouble;
use crate::service::Permit;

/// The account is kept, the label is kept, and the copy that cannot work is dropped, so the
/// way back is one sign-in rather than an enrolment.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
fn a_parked_login_anthropic_refuses_is_not_installed() {
    let m = machine("refused-park");
    m.api
        .token_trouble("access-there-refresh", Trouble::Unauthorized);
    m.api.renew_trouble("there-refresh", Trouble::InvalidGrant);
    let live_before = m.mem.live().peek(&m.service);

    let settled = settle(&m.ctx, Permit::for_a_test(), None)
        .expect("nothing to recover")
        .0;
    let failed = switch(
        settled,
        &crate::state::Key::new(crate::provider::ProviderId::Claude, "there"),
    )
    .expect_err("a refused login is not a switch");
    assert!(
        matches!(failed, Error::ParkedLoginRefused { .. }),
        "got {failed:?}"
    );

    assert_eq!(
        m.mem.live().peek(&m.service),
        live_before,
        "the login that was working is exactly where it was"
    );
    let state = state::load(&m.ctx).expect("state");
    assert!(
        state
            .get(&crate::state::Key::new(
                crate::provider::ProviderId::Claude,
                "here"
            ))
            .expect("account")
            .parked
            .is_none(),
        "and it was never parked, because nothing was taken away"
    );
    let there = state
        .get(&crate::state::Key::new(
            crate::provider::ProviderId::Claude,
            "there",
        ))
        .expect("account");
    assert_eq!(there.email, "there@example.com", "the account is kept");
    assert!(there.parked.is_none(), "the copy that cannot work is not");
    assert!(
        state
            .discarded
            .iter()
            .any(|s| s.starts_with("pitboard-park-there-")),
        "the dead copy is listed for deletion, so a delete that fails is retried"
    );

    recover(&m).expect("the next command settles");
    assert!(
        m.mem.vault().services().is_empty(),
        "and that is when it goes"
    );
}

/// A park filed under the wrong label is the failure the identity check exists to prevent,
/// caught on the way in rather than after both accounts have moved.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
fn a_parked_login_that_belongs_to_another_account_is_refused() {
    let m = machine("misfiled-park");
    m.api
        .owned_by("access-there-refresh", owner("somebody-else"));
    let live_before = m.mem.live().peek(&m.service);
    let parked_before = m.mem.vault().services();

    let settled = settle(&m.ctx, Permit::for_a_test(), None)
        .expect("nothing to recover")
        .0;
    let failed = switch(
        settled,
        &crate::state::Key::new(crate::provider::ProviderId::Claude, "there"),
    )
    .expect_err("it is not that account's login");
    assert!(
        matches!(failed, Error::ParkedLoginBelongsElsewhere { .. }),
        "got {failed:?}"
    );

    assert_eq!(m.mem.live().peek(&m.service), live_before);
    assert_eq!(
        m.mem.vault().services(),
        parked_before,
        "nothing is deleted over a park Pitboard will not use"
    );
    recover(&m).expect("and the machine is untouched");
}

/// With nobody to ask about the login going in, the answer is to do nothing. The login that
/// is working is worth more than the switch.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
fn a_switch_will_not_install_a_login_it_could_not_ask_about() {
    let m = machine("offline-in");
    m.api
        .token_trouble("access-there-refresh", Trouble::Offline);
    let live_before = m.mem.live().peek(&m.service);
    let parked_before = m.mem.vault().services();

    let settled = settle(&m.ctx, Permit::for_a_test(), None)
        .expect("nothing to recover")
        .0;
    let failed = switch(
        settled,
        &crate::state::Key::new(crate::provider::ProviderId::Claude, "there"),
    )
    .expect_err("nobody could be asked");
    assert!(
        matches!(
            failed,
            Error::IdentityUnverifiable { .. } | Error::RenewalFailed { .. }
        ),
        "got {failed:?}"
    );

    assert_eq!(m.mem.live().peek(&m.service), live_before);
    assert_eq!(m.mem.vault().services(), parked_before);
    assert!(!journal::pending(&m.ctx), "and nothing was begun");
}

/// The ordinary path, kept honest: a park that answers for the account it is filed under is
/// installed, and asking about it costs one round trip and no writes.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
fn a_park_that_answers_for_its_own_account_is_installed() {
    let m = machine("proved");
    let settled = settle(&m.ctx, Permit::for_a_test(), None)
        .expect("nothing to recover")
        .0;
    let (outcome, _) = switch(
        settled,
        &crate::state::Key::new(crate::provider::ProviderId::Claude, "there"),
    )
    .expect("a switch");
    assert!(matches!(outcome, Outcome::Switched { .. }), "{outcome:?}");

    let asked = m.api.asked();
    assert!(
        asked.iter().any(
            |q| matches!(q, crate::api::scripted::Asked::Owner(t) if t == "access-there-refresh")
        ),
        "the login going in is asked about, not only the one coming out: {asked:?}"
    );
}

/// The login going out is the one Anthropic last named for the store, by its refresh
/// token's fingerprint, so only the login going in is asked about. The login going out was
/// asked about on every switch, which also refused to move a login whose session had lapsed
/// though its account was known.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
fn a_switch_from_a_login_already_identified_asks_only_about_the_login_going_in() {
    let m = machine("known-going-out");
    let settled = settle(&m.ctx, Permit::for_a_test(), None)
        .expect("nothing to recover")
        .0;
    let (outcome, _) = switch(settled, &m.key("there")).expect("a switch");
    assert!(matches!(outcome, Outcome::Switched { .. }), "{outcome:?}");
    assert_eq!(
        m.api.asked(),
        [crate::api::scripted::Asked::Owner(
            "access-there-refresh".into()
        )]
    );
}

/// Two situations with one message until now. Nobody signed in is an ordinary state with an
/// ordinary answer. Claude Code's config naming somebody as signed in while Pitboard finds
/// no login anywhere it looks means Pitboard is looking in the wrong place, and writing a
/// login there would put it where nobody reads.
#[test]
#[cfg_attr(windows, ignore = "W22: switching Claude Code on Windows")]
fn a_login_pitboard_cannot_find_is_not_the_same_as_nobody_being_signed_in() {
    let m = machine("elsewhere");

    // Claude Code's config still says who is signed in; the login is not in any store.
    m.mem.live().delete_everything();
    let recorded = state_file(&m);
    let settled = settle(&m.ctx, Permit::for_a_test(), None)
        .expect("nothing to recover")
        .0;
    let failed = switch(
        settled,
        &crate::state::Key::new(crate::provider::ProviderId::Claude, "there"),
    )
    .expect_err("there is nothing to move");
    match &failed {
        Error::LiveCredentialElsewhere { email } => assert_eq!(email, "here@example.com"),
        other => panic!("got {other:?}"),
    }
    assert_eq!(failed.code(), "live_credential_elsewhere");
    // Nothing is known of whose login the store holds, so nothing is recorded of it.
    assert_eq!(state_file(&m), recorded);
    assert!(crate::audit::read(&m.ctx, 100).is_empty());

    // With nothing in the config either, nobody is signed in and that is all it says.
    std::fs::write(m.ctx_home().join(".claude.json"), "{}").expect("a config");
    let settled = settle(&m.ctx, Permit::for_a_test(), None)
        .expect("nothing to recover")
        .0;
    let failed = switch(
        settled,
        &crate::state::Key::new(crate::provider::ProviderId::Claude, "there"),
    )
    .expect_err("still nothing to move");
    assert!(
        matches!(failed, Error::LiveCredentialAbsent { .. }),
        "got {failed:?}"
    );
    // A sign-out outside Pitboard, which took the only login `here` had.
    assert_eq!(
        audit_lines(&m, "in-use"),
        [
            (String::new(), "signed_out_outside".to_string()),
            ("here".to_string(), "login_replaced".to_string()),
        ]
    );
}

/// While `.credentials.json` sits behind the keychain, a session already running keeps the
/// login it holds until that login is next renewed, whatever the file holds, as the
/// register's `fallback_file_pins_session_login` reads 2.1.294: it looks at the file and
/// never reads it to decide. A switch says so, naming the file, in place of the 33 seconds
/// it takes without one, also where Pitboard cannot read what the file holds.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
#[cfg_attr(
    target_os = "linux",
    ignore = "on Linux the file is Claude Code's only store, never behind a keychain"
)]
fn a_switch_with_a_file_behind_the_keychain_says_sessions_take_it_at_renewal() {
    let switched = |m: &super::harness::Machine| {
        let settled = settle(&m.ctx, Permit::for_a_test(), None)
            .expect("nothing to recover")
            .0;
        switch(settled, &m.key("there")).expect("a switch").0
    };

    let m = machine("adoption-nothing-behind");
    assert!(
        matches!(
            switched(&m),
            Outcome::Switched {
                adoption: provider::Adoption::PollingWithin(33),
                ..
            }
        ),
        "nothing behind the keychain"
    );

    for (name, unreadable) in [
        ("adoption-file-behind", false),
        ("adoption-unreadable-behind", true),
    ] {
        let m = machine(name);
        let file = provider::claude::live::credential_file(&m.ctx);
        let behind = m.mem.file_at(file.clone());
        behind.plant(&m.service, "{}");
        if unreadable {
            behind.fault(
                &m.service,
                crate::store::memory::Fault::UnreadableContents("permission denied".into()),
            );
        }
        let outcome = switched(&m);
        let Outcome::Switched { adoption, .. } = outcome else {
            panic!("{name}: a switch, not {outcome:?}");
        };
        assert_eq!(adoption, provider::Adoption::AtRenewal { file }, "{name}");
    }
}

/// What Claude Code's config names, by account.
fn config_names_now(m: &super::harness::Machine) -> String {
    let config: serde_json::Value = serde_json::from_str(
        &std::fs::read_to_string(m.ctx_home().join(".claude.json")).expect("a config"),
    )
    .expect("JSON");
    config["oauthAccount"]["accountUuid"]
        .as_str()
        .unwrap_or_default()
        .to_owned()
}

/// Claude Code's config names another account than the one whose login it has stored, as a
/// Claude Code process started on another login leaves it, and `/status` in Claude Code shows
/// that one. Using the account in use writes it there again, as a switch to it does, keeping
/// a copy of the config first, and moves no login. It changed nothing.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
fn using_the_account_in_use_writes_claude_codes_config_again() {
    let m = machine("use-names-again");
    super::harness::config_names(&m, "there");
    let live = m.live();
    let pitboard = crate::service::Pitboard::new(m.ctx.clone());

    let done = pitboard.switch_to("here").expect("already in use");

    assert!(
        matches!(
            done.value,
            Outcome::AlreadyActive {
                config_updated: true,
                ..
            }
        ),
        "{:?}",
        done.value
    );
    assert!(done.warnings.is_empty(), "{:?}", done.warnings);
    assert_eq!(config_names_now(&m), "here");
    assert_eq!(m.live(), live, "no login moved");
    let backups = std::fs::read_dir(crate::home::dir(&m.ctx).join("backups"))
        .map(|entries| entries.count())
        .unwrap_or_default();
    assert_eq!(backups, 1, "a copy of the config is kept first");
    assert_eq!(
        audit_lines(&m, "use"),
        [("here".to_string(), "config_updated".to_string())]
    );
    let read = pitboard.status_offline().expect("a read of what is known");
    let codes: Vec<&str> = read.warnings.iter().map(Warning::code).collect();
    assert!(codes.is_empty(), "{codes:?}");

    let again = pitboard.switch_to("here").expect("already in use");
    assert!(
        matches!(
            again.value,
            Outcome::AlreadyActive {
                config_updated: false,
                ..
            }
        ),
        "{:?}",
        again.value
    );
    assert!(again.warnings.is_empty(), "{:?}", again.warnings);
    assert_eq!(
        audit_lines(&m, "use"),
        [
            ("here".to_string(), "config_updated".to_string()),
            ("here".to_string(), "already_active".to_string()),
        ]
    );
}

/// Where the config cannot be written, the account is still the one in use and the command
/// says why the config was not, as a switch does; the config and what Pitboard records of
/// it are as they were, so every read still says it names another account.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
fn a_config_that_could_not_be_written_again_is_said() {
    let m = machine("use-names-again-refused");
    super::harness::config_names(&m, "there");
    std::fs::write(crate::home::dir(&m.ctx).join("backups"), "").expect("in the way");
    let config = std::fs::read(m.ctx_home().join(".claude.json")).expect("a config");
    let pitboard = crate::service::Pitboard::new(m.ctx.clone());

    let done = pitboard.switch_to("here").expect("already in use");

    assert!(
        matches!(
            done.value,
            Outcome::AlreadyActive {
                config_updated: false,
                ..
            }
        ),
        "{:?}",
        done.value
    );
    let codes: Vec<&str> = done.warnings.iter().map(Warning::code).collect();
    assert_eq!(codes, ["config_backup_failed"]);
    assert_eq!(
        std::fs::read(m.ctx_home().join(".claude.json")).expect("a config"),
        config
    );
    assert_eq!(
        audit_lines(&m, "use"),
        [("here".to_string(), "already_active".to_string())]
    );
    let read = pitboard.status_offline().expect("a read of what is known");
    let codes: Vec<&str> = read.warnings.iter().map(Warning::code).collect();
    assert_eq!(codes, ["config_names_another"]);
}

/// A switch whose login landed and whose config could not be written records the config as
/// naming what it named before, as `use` does, so a read from files alone says the config
/// names another account, not that something may have signed in.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
fn a_switch_whose_config_could_not_be_written_is_said_to_name_another() {
    let m = machine("switch-config-refused");
    std::fs::write(crate::home::dir(&m.ctx).join("backups"), "").expect("in the way");
    let pitboard = crate::service::Pitboard::new(m.ctx.clone());

    let done = pitboard.switch_to("there").expect("a switch");

    assert!(
        matches!(done.value, Outcome::Switched { .. }),
        "{:?}",
        done.value
    );
    let codes: Vec<&str> = done.warnings.iter().map(Warning::code).collect();
    assert_eq!(codes, ["config_backup_failed"]);
    assert_eq!(config_names_now(&m), "here");
    let read = pitboard.status_offline().expect("a read of what is known");
    let codes: Vec<&str> = read.warnings.iter().map(Warning::code).collect();
    assert_eq!(codes, ["config_names_another"]);
}

/// The app's button writes the account in use into Claude Code's config as `use` does, and is
/// recorded as `use`. Pressed once another account is in use, as after a switch that landed
/// before the press, it writes nothing and moves no login, where `use` would switch back.
#[test]
#[cfg_attr(
    windows,
    ignore = "W23: Claude Code's Credential Manager store, which a machine in memory plays"
)]
fn the_config_is_written_only_while_the_account_is_in_use() {
    let m = machine("name-again");
    super::harness::config_names(&m, "there");
    let pitboard = crate::service::Pitboard::new(m.ctx.clone());

    let done = pitboard.update_config("here").expect("in use");
    assert!(
        matches!(
            done.value,
            Outcome::AlreadyActive {
                config_updated: true,
                ..
            }
        ),
        "{:?}",
        done.value
    );
    assert_eq!(config_names_now(&m), "here");

    pitboard.switch_to("there").expect("a switch");
    let live = m.live();
    let config = std::fs::read(m.ctx_home().join(".claude.json")).expect("a config");
    let refused = pitboard.update_config("here").expect_err("not in use");
    assert!(
        matches!(refused.error, Error::AccountNotInUse { .. }),
        "{:?}",
        refused.error
    );
    assert_eq!(m.live(), live, "no login moved");
    assert_eq!(
        std::fs::read(m.ctx_home().join(".claude.json")).expect("a config"),
        config
    );
    assert_eq!(
        audit_lines(&m, "use"),
        [
            ("here".to_string(), "config_updated".to_string()),
            ("there".to_string(), "ok".to_string()),
            ("here".to_string(), "account_not_in_use".to_string()),
        ]
    );
}