pitboard-core 0.14.0

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
// Read by a change only, with the app seen closed and its folder held. Nothing is recorded.

use super::super::{Error, Result, shape};
use super::there;
use crate::context::Context;
use crate::error::Then;
use crate::provider::ProviderId;
use crate::provider::folder::{Folder, SignedIn, Unread};
use crate::state::{Key, ParkKind, State};
use std::path::Path;

pub(crate) enum Holds {
    Empty,
    SignedOut,
    // emptied: a token cache holds the value the app leaves after a Log out (E1b)
    SignedIn {
        session: SignedIn,
        named: Option<String>,
        emptied: bool,
    },
}

pub(crate) enum Verdict {
    // rows 3, 5, 10. `leftovers`: the app left a signed-out jar, which an arrive sets aside
    Nobody {
        leftovers: bool,
    },
    // rows 13, 16. `as_named`: a session Pitboard had not seen, taken as the app names it
    Account {
        key: Key,
        session: SignedIn,
        as_named: bool,
    },
    Unsure(Unsure),
}

pub(crate) enum Unsure {
    // 12
    Unfinished,
    // 14, 15
    RecordedUnderAnother {
        recorded: Key,
        named: Named,
    },
    // 17
    AlsoParked {
        key: Key,
        session: SignedIn,
    },
    // 18, 19
    Unseen {
        named: Named,
        session: SignedIn,
    },
    // 20: `also_in` is the folder under `parks` that holds the same session
    HeldTwice {
        named: Named,
        session: SignedIn,
        also_in: String,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Named {
    Enrolled(Key),
    NotEnrolled { uuid: String },
}

pub(crate) fn look(ctx: &Context, state: &State, app: &'static dyn Folder) -> Result<Verdict> {
    let root = app.root(ctx).map_err(|e| shape(app.id(), e))?;
    // Row 3, nobody, as row 5: the caller that refuses it looks first.
    if !there(app.id(), &root)? {
        return Ok(Verdict::Nobody { leftovers: false });
    }
    if let Some(unfit) = app.refuses(&root) {
        return Err(Error::LoginFolderUnknown {
            tool: app.id(),
            what: unfit.to_string(),
            then: Then::Nothing,
        });
    }
    let holds = holds(ctx, app, &root)?;
    Ok(verdict(state, app.id(), holds))
}

pub(crate) fn holds(ctx: &Context, app: &dyn Folder, root: &Path) -> Result<Holds> {
    let tool = app.id();
    let unread = |unread: Unread| match unread {
        Unread::Unknown { what, then } => Error::LoginFolderUnknown { tool, what, then },
        Unread::Unreadable(detail) => Error::LoginFolderUnreadable { tool, detail },
    };
    let settings = app.settings(root).map_err(|refused| match refused {
        Unread::Unreadable(detail) => Error::LoginFolderUnknown {
            tool,
            what: format!("has a settings file Pitboard could not read ({detail})"),
            then: Then::Nothing,
        },
        known => unread(known),
    })?;
    let keys = settings.account_keys(app);
    let caches: Vec<&str> = app
        .keys()
        .iter()
        .copied()
        .filter(|key| *key != app.naming_key())
        .collect();
    let jar = root.join(app.items()[0]);
    if !there(tool, &jar)? {
        let mut item = None;
        for name in &app.items()[1..] {
            if there(tool, &root.join(name))? {
                item = Some(name);
                break;
            }
        }
        let key = app.keys().iter().find(|key| keys.get(key).is_some());
        return match item.or(key) {
            None => Ok(Holds::Empty),
            Some(name) => Err(Error::LoginFolderUnknown {
                tool,
                what: format!("has no cookie store and still holds `{name}`"),
                then: Then::OpenAndQuit,
            }),
        };
    }
    let session = app.session(ctx, &jar).map_err(unread)?;
    let emptied = |key: &&str| keys.get(key).is_some_and(|value| app.emptied(key, value));
    let Some(session) = session else {
        return if caches
            .iter()
            .all(|key| keys.get(key).is_none() || emptied(key))
        {
            Ok(Holds::SignedOut)
        } else {
            Err(Error::LoginFolderUnknown {
                tool,
                what: "holds no session and still holds a login's tokens".into(),
                then: Then::SignInAndEnrol,
            })
        };
    };
    Ok(Holds::SignedIn {
        session,
        named: settings.named(app),
        emptied: caches.iter().any(emptied),
    })
}

// Pure: no clock, no disk.
pub(crate) fn verdict(state: &State, tool: ProviderId, holds: Holds) -> Verdict {
    let (session, named, emptied) = match holds {
        Holds::Empty => return Verdict::Nobody { leftovers: false },
        Holds::SignedOut => return Verdict::Nobody { leftovers: true },
        Holds::SignedIn {
            session,
            named,
            emptied,
        } => (session, named, emptied),
    };
    let named = named.map(|uuid| {
        state
            .accounts
            .iter()
            .find(|a| a.provider() == tool && a.account_uuid == uuid)
            .map_or(Named::NotEnrolled { uuid }, |a| Named::Enrolled(a.key()))
    });
    let recorded = state
        .in_use(tool)
        .filter(|record| !record.login.is_empty() && record.login == session.login)
        .and_then(|record| record.owner.as_ref())
        .and_then(|owner| state.account_of(tool, owner))
        .map(crate::state::Account::key);
    // The fingerprint is Pitboard's own, whatever the caches hold.
    if let (Some(recorded), Some(named)) = (&recorded, &named) {
        return match named {
            Named::Enrolled(key) if key == recorded => Verdict::Account {
                key: key.clone(),
                session,
                as_named: false,
            },
            _ => Verdict::Unsure(Unsure::RecordedUnderAnother {
                recorded: recorded.clone(),
                named: named.clone(),
            }),
        };
    }
    let Some(named) = named else {
        return Verdict::Unsure(Unsure::Unfinished);
    };
    if let Some(also_in) = held_here(state, tool, &session.login) {
        return Verdict::Unsure(Unsure::HeldTwice {
            named,
            session,
            also_in,
        });
    }
    if emptied {
        return Verdict::Unsure(Unsure::Unfinished);
    }
    let in_use = state.account_in_use(tool).map(crate::state::Account::key);
    match named {
        Named::Enrolled(key) if Some(&key) == in_use.as_ref() => {
            let parked = state.get(&key).is_some_and(|a| a.parked.is_some());
            if parked {
                Verdict::Unsure(Unsure::AlsoParked { key, session })
            } else {
                Verdict::Account {
                    key,
                    session,
                    as_named: true,
                }
            }
        }
        named => Verdict::Unsure(Unsure::Unseen { named, session }),
    }
}

// The folder under `parks` of this home that holds `login`: an account's park or an aside.
fn held_here(state: &State, tool: ProviderId, login: &str) -> Option<String> {
    state
        .accounts
        .iter()
        .filter(|a| a.provider() == tool)
        .filter_map(|a| a.parked.as_ref())
        .find(|park| park.kind == ParkKind::Folder && park.login == login)
        .map(|park| park.service.clone())
        .or_else(|| {
            state
                .aside
                .iter()
                .find(|aside| aside.tool == tool && aside.login.as_deref() == Some(login))
                .map(|aside| aside.name.clone())
        })
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::in_use::InUse;
    use crate::state::{Account, Aside, Detail, Park, SetAside};

    const TOOL: ProviderId = ProviderId::Desktop;

    fn account(label: &str, parked: Option<&str>) -> Account {
        Account {
            label: label.into(),
            id: format!("desktop_{label}-uuid"),
            account_uuid: format!("{label}-uuid"),
            email: String::new(),
            parked: parked.map(|login| Park {
                service: format!("park-of-{label}"),
                kind: ParkKind::Folder,
                parked_at: 1,
                login: login.into(),
                access_expires_at: None,
                refresh_expires_at: None,
                session_expires_at: None,
            }),
            last_used_at: None,
            replaced_at: None,
            plan: None,
            detail: Detail::Desktop {},
        }
    }

    // `work` and `home` enrolled, `work` recorded in use with the session `seen`.
    fn state(works_park: Option<&str>, homes_park: Option<&str>) -> State {
        let mut state = State::default();
        state.accounts.push(account("work", works_park));
        state.accounts.push(account("home", homes_park));
        state.in_use.insert(
            TOOL.code().into(),
            InUse {
                owner: Some(state.accounts[0].owner()),
                login: "seen".into(),
                known_at: 1,
                named: Some("desktop_work-uuid".into()),
            },
        );
        state
    }

    fn signed_in(login: &str, named: Option<&str>, emptied: bool) -> Holds {
        Holds::SignedIn {
            session: SignedIn {
                login: login.into(),
                expires_at: None,
            },
            named: named.map(|label| format!("{label}-uuid")),
            emptied,
        }
    }

    fn said(verdict: Verdict) -> String {
        let name = |named: &Named| match named {
            Named::Enrolled(key) => key.label.clone(),
            Named::NotEnrolled { uuid } => format!("nobody enrolled ({uuid})"),
        };
        match verdict {
            Verdict::Nobody { .. } => "nobody".into(),
            Verdict::Account { key, session, .. } => {
                format!("{} with {}", key.label, session.login)
            }
            Verdict::Unsure(Unsure::Unfinished) => "unfinished".into(),
            Verdict::Unsure(Unsure::RecordedUnderAnother { recorded, named }) => {
                format!("recorded {}, named {}", recorded.label, name(&named))
            }
            Verdict::Unsure(Unsure::AlsoParked { key, session }) => {
                format!("{} with {}, and parked", key.label, session.login)
            }
            Verdict::Unsure(Unsure::Unseen { named, session }) => {
                format!("unseen {}, named {}", session.login, name(&named))
            }
            Verdict::Unsure(Unsure::HeldTwice {
                named,
                session,
                also_in,
            }) => format!(
                "{} also in {also_in}, named {}",
                session.login,
                name(&named)
            ),
        }
    }

    // Rows 6 to 9 and 11 are `holds`' refusals and never reach `verdict`.
    #[test]
    fn each_row_of_the_table_is_read_as_it_says() {
        let plain = || state(None, None);
        let mut nobody_recorded = plain();
        nobody_recorded.in_use.clear();
        let mut set_aside = plain();
        set_aside.aside.push(Aside {
            name: "pitboard-aside-1".into(),
            tool: TOOL,
            at: 1,
            why: SetAside::GivenUp,
            account: None,
            login: Some("kept".into()),
        });
        let mut another_tools = plain();
        another_tools.accounts[1].detail = Detail::Codex { workspace_id: None };
        let rows = [
            ("5", plain(), Holds::Empty, "nobody"),
            ("10", plain(), Holds::SignedOut, "nobody"),
            (
                "12, no name",
                plain(),
                signed_in("seen", None, false),
                "unfinished",
            ),
            (
                "12, emptied",
                plain(),
                signed_in("new", Some("work"), true),
                "unfinished",
            ),
            (
                "12, emptied, another",
                plain(),
                signed_in("new", Some("home"), true),
                "unfinished",
            ),
            (
                "12, emptied, nobody's",
                plain(),
                signed_in("new", Some("other"), true),
                "unfinished",
            ),
            (
                "13",
                plain(),
                signed_in("seen", Some("work"), false),
                "work with seen",
            ),
            (
                "13, emptied",
                plain(),
                signed_in("seen", Some("work"), true),
                "work with seen",
            ),
            (
                "13, parked too",
                state(Some("older"), None),
                signed_in("seen", Some("work"), false),
                "work with seen",
            ),
            (
                "14",
                plain(),
                signed_in("seen", Some("home"), false),
                "recorded work, named home",
            ),
            (
                "14, emptied",
                plain(),
                signed_in("seen", Some("home"), true),
                "recorded work, named home",
            ),
            (
                "15",
                plain(),
                signed_in("seen", Some("other"), false),
                "recorded work, named nobody enrolled (other-uuid)",
            ),
            (
                "16",
                plain(),
                signed_in("new", Some("work"), false),
                "work with new",
            ),
            (
                "17",
                state(Some("older"), None),
                signed_in("new", Some("work"), false),
                "work with new, and parked",
            ),
            (
                "18",
                plain(),
                signed_in("new", Some("home"), false),
                "unseen new, named home",
            ),
            (
                "18, parked",
                state(None, Some("older")),
                signed_in("new", Some("home"), false),
                "unseen new, named home",
            ),
            (
                "18, nobody recorded",
                nobody_recorded,
                signed_in("new", Some("work"), false),
                "unseen new, named work",
            ),
            (
                "19",
                plain(),
                signed_in("new", Some("other"), false),
                "unseen new, named nobody enrolled (other-uuid)",
            ),
            (
                "19, another tool's account",
                another_tools,
                signed_in("new", Some("home"), false),
                "unseen new, named nobody enrolled (home-uuid)",
            ),
            (
                "20, a park",
                state(None, Some("kept")),
                signed_in("kept", Some("home"), false),
                "kept also in park-of-home, named home",
            ),
            (
                "20, the account in use's park",
                state(Some("kept"), None),
                signed_in("kept", Some("work"), false),
                "kept also in park-of-work, named work",
            ),
            (
                "20, an aside",
                set_aside,
                signed_in("kept", Some("other"), false),
                "kept also in pitboard-aside-1, named nobody enrolled (other-uuid)",
            ),
            (
                "20, emptied",
                state(None, Some("kept")),
                signed_in("kept", Some("home"), true),
                "kept also in park-of-home, named home",
            ),
        ];
        for (row, state, holds, expected) in rows {
            assert_eq!(said(verdict(&state, TOOL, holds)), expected, "row {row}");
        }
    }
}