pitboard-core 0.15.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
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
//! What keeps a tool's login in memory while it runs, and what makes each kind take a switch.
//!
//! A tool whose running sessions never notice a switch (`Adoption::RestartRequired`) names
//! the kinds of process that run its program. One program can run in very different
//! places: Codex's runs in a terminal, inside OpenAI's ChatGPT app, as a background app
//! server and inside an editor's Codex extension. Each takes a switch its own way, and
//! advice meant for one is wrong for another: quitting a terminal session does nothing for
//! an app that stays open in the menu bar after its windows close.
//!
//! The kinds are told apart by where the program runs from, which the process list says.
//! Nothing here names a tool; a tool's own list does, most particular kind first and ending
//! with one that is anywhere, so every process is one kind and none is guessed to be an app.

use crate::context::Context;
use crate::host::{OS, Process};
use std::path::Path;

/// One kind of process that holds a tool's login.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Holder {
    /// A stable name in snake case, for a program to tell kinds apart by.
    pub kind: &'static str,
    /// How a sentence names what is running.
    pub noun: Noun,
    /// Where its program runs from.
    pub location: Location,
    /// What makes it take a switch.
    pub remedy: Remedy,
}

/// How a sentence names one kind of holder.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Noun {
    /// Each process is one of them, counted: "1 `codex` session", "2 `codex` sessions".
    Counted {
        one: &'static str,
        many: &'static str,
    },
    /// One thing however many processes it runs: an app that starts two of the program is
    /// still one app.
    One(&'static str),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Location {
    // A directory of exactly this name, such as an app bundle.
    Within(&'static str),
    // An editor extension's folder is named for its version.
    WithinPrefixed(&'static str),
    // A Windows package's family, wherever the package is installed.
    Package(&'static str),
    Anywhere,
}

/// What makes a kind of holder take a switch.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Remedy {
    /// Quit it and start it again.
    Restart,
    /// Quit the app the way its system quits an app, and open it again. Closing its windows
    /// is not enough. The menu bar app can do both for the person; the command line only
    /// says so.
    ReopenApp {
        /// The app, as its own system names it.
        app: AppId,
        /// The app as a person knows it: "ChatGPT".
        name: &'static str,
    },
    /// Run this command.
    Run(&'static str),
    /// Do this, somewhere Pitboard cannot reach.
    Do(&'static str),
}

// Which system's id is part of the id: a program in a Mac app's bundle runs only on a Mac, so a
// test whose machine runs one has that Mac app on whatever system the test runs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum AppId {
    MacBundle(&'static str),
    // An Application User Model ID, `<package family>!<app>`.
    WindowsAumid(&'static str),
}

impl AppId {
    pub fn as_str(self) -> &'static str {
        match self {
            AppId::MacBundle(id) | AppId::WindowsAumid(id) => id,
        }
    }

    // A Mac app stays open after its last window closes.
    fn reopen(self, name: &str) -> String {
        match self {
            AppId::MacBundle(_) => format!("quit {name} with Command-Q and open it again"),
            AppId::WindowsAumid(_) => {
                format!("quit {name} from its notification-area icon and open it again")
            }
        }
    }
}

/// The processes of one kind of holder that are running.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Holding {
    pub holder: Holder,
    /// Their process ids, in the order the process list gave them.
    pub pids: Vec<u32>,
}

impl Location {
    // Never by the program's own name. A prefix is compared as that many first characters.
    fn holds(self, process: &Process) -> bool {
        let mut directories = process.path.ancestors().skip(1).filter_map(Path::file_name);
        match self {
            Location::Anywhere => true,
            Location::Package(family) => process
                .package
                .as_deref()
                .is_some_and(|its| its.eq_ignore_ascii_case(family)),
            Location::Within(name) => {
                directories.any(|dir| OS.same_path(Path::new(dir), Path::new(name)))
            }
            Location::WithinPrefixed(prefix) => directories.any(|dir| {
                let head: String = dir
                    .to_string_lossy()
                    .chars()
                    .take(prefix.chars().count())
                    .collect();
                OS.same_path(Path::new(&head), Path::new(prefix))
            }),
        }
    }
}

// Each to the first holder whose location holds it. A kind with nothing running is left out.
pub(crate) fn classify(processes: &[Process], holders: &[Holder]) -> Vec<Holding> {
    let mut holding: Vec<Holding> = holders
        .iter()
        .map(|&holder| Holding {
            holder,
            pids: Vec::new(),
        })
        .collect();
    for process in processes {
        if let Some(found) = holding
            .iter_mut()
            .find(|h| h.holder.location.holds(process))
        {
            found.pids.push(process.pid);
        }
    }
    holding.retain(|h| !h.pids.is_empty());
    holding
}

/// What is running `program` on this machine, by kind. `None` where the process list could
/// not be read, which is not the same as nothing running.
pub(crate) fn find(ctx: &Context, program: &str, holders: &[Holder]) -> Option<Vec<Holding>> {
    ctx.host()
        .processes(program)
        .map(|processes| classify(&processes, holders))
}

impl Holding {
    /// How many things are running: processes for a counted kind, one for the rest.
    fn things(&self) -> usize {
        match self.holder.noun {
            Noun::Counted { .. } => self.pids.len(),
            Noun::One(_) => 1,
        }
    }

    /// "2 `codex` sessions", "the ChatGPT app".
    pub fn phrase(&self) -> String {
        match self.holder.noun {
            Noun::Counted { one, many } => match self.pids.len() {
                1 => format!("1 {one}"),
                n => format!("{n} {many}"),
            },
            Noun::One(name) => name.to_string(),
        }
    }

    /// What to do, as a clause. Said alone it can say "it" or "them"; beside others it has
    /// to name what it is about.
    fn clause(&self, alone: bool) -> String {
        let them = if self.things() == 1 { "it" } else { "them" };
        match self.holder.remedy {
            Remedy::Restart if alone => format!("quit {them} and start again"),
            Remedy::Restart => {
                let what = match self.holder.noun {
                    Noun::Counted { one, many } => {
                        format!("the {}", if self.things() == 1 { one } else { many })
                    }
                    Noun::One(name) => name.to_string(),
                };
                format!("quit {what} and start {them} again")
            }
            Remedy::ReopenApp { app, name } => app.reopen(name),
            Remedy::Run(command) => format!("run `{command}`"),
            Remedy::Do(instruction) => instruction.to_string(),
        }
    }
}

/// Everything running, as one noun phrase: "2 `codex` sessions and the ChatGPT app".
pub(crate) fn described(holding: &[Holding]) -> String {
    crate::words::listed(holding.iter().map(Holding::phrase).collect())
}

/// The same, with the pids of each: "2 `codex` sessions (pid 41, 42)".
pub(crate) fn described_with_pids(holding: &[Holding]) -> String {
    crate::words::listed(
        holding
            .iter()
            .map(|h| format!("{} (pid {})", h.phrase(), some_of(&h.pids)))
            .collect(),
    )
}

/// Whether what is running reads as more than one thing, for "is" or "are".
pub(crate) fn plural(holding: &[Holding]) -> bool {
    holding.iter().map(Holding::things).sum::<usize>() > 1
}

/// One sentence saying what makes everything running take a switch, with `purpose`:
/// "Quit them and start again to use the new account." Several kinds are each said in turn,
/// apart, since one clause can have an "and" of its own: "To use the new account: quit the
/// `codex` sessions and start them again; quit ChatGPT with Command-Q and open it again."
pub(crate) fn remedies(holding: &[Holding], purpose: &str) -> String {
    match holding {
        [] => String::new(),
        [one] => format!("{} {purpose}.", capitalised(&one.clause(true))),
        many => format!(
            "{}: {}.",
            capitalised(purpose),
            many.iter()
                .map(|h| h.clause(false))
                .collect::<Vec<_>>()
                .join("; ")
        ),
    }
}

/// `text` with its first letter a capital, to begin a sentence with.
pub(crate) fn capitalised(text: &str) -> String {
    let mut chars = text.chars();
    chars.next().map_or_else(String::new, |first| {
        first.to_uppercase().chain(chars).collect()
    })
}

/// A few pids and how many more, because somebody with twenty sessions open needs to know
/// there are twenty, not which twenty.
pub(crate) fn some_of(pids: &[u32]) -> String {
    const SHOWN: usize = 3;
    let named: Vec<String> = pids.iter().take(SHOWN).map(u32::to_string).collect();
    match pids.len().saturating_sub(SHOWN) {
        0 => named.join(", "),
        more => format!("{} and {more} more", named.join(", ")),
    }
}

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

    const APP: Holder = Holder {
        kind: "app",
        noun: Noun::One("the App"),
        location: Location::Within("App.app"),
        remedy: Remedy::ReopenApp {
            app: AppId::MacBundle("com.example.app"),
            name: "App",
        },
    };
    const EXTENSION: Holder = Holder {
        kind: "extension",
        noun: Noun::One("the extension"),
        location: Location::WithinPrefixed("vendor.tool-"),
        remedy: Remedy::Do("reload the editor's window"),
    };
    const SESSION: Holder = Holder {
        kind: "session",
        noun: Noun::Counted {
            one: "`tool` session",
            many: "`tool` sessions",
        },
        location: Location::Anywhere,
        remedy: Remedy::Restart,
    };
    const HOLDERS: &[Holder] = &[APP, EXTENSION, SESSION];

    fn at(pid: u32, path: &str) -> Process {
        Process {
            pid,
            path: PathBuf::from(path),
            package: None,
        }
    }

    fn kinds(holding: &[Holding]) -> Vec<(&str, Vec<u32>)> {
        holding
            .iter()
            .map(|h| (h.holder.kind, h.pids.clone()))
            .collect()
    }

    #[test]
    fn each_process_is_the_first_kind_whose_place_holds_it() {
        let holding = classify(
            &[
                at(1, "tool"),
                at(
                    2,
                    "/Applications/App.app/Contents/Helpers/Tool.app/Contents/MacOS/tool",
                ),
                at(3, "/home/a/.editor/extensions/vendor.tool-1.2.3/bin/tool"),
                at(4, "/usr/local/bin/tool"),
            ],
            HOLDERS,
        );
        assert_eq!(
            kinds(&holding),
            [
                ("app", vec![2]),
                ("extension", vec![3]),
                ("session", vec![1, 4])
            ]
        );
    }

    #[test]
    fn only_the_directories_a_program_is_in_say_where_it_runs() {
        let holding = classify(&[at(1, "/usr/bin/App.app")], HOLDERS);
        assert_eq!(kinds(&holding), [("session", vec![1])]);
        assert!(!Location::WithinPrefixed("vendor.tool-").holds(&at(1, "vendor.tool-9")));
    }

    #[test]
    fn a_place_is_named_as_this_system_compares_paths() {
        let holding = classify(
            &[
                at(1, "/Applications/app.app/x/tool"),
                at(2, "/e/Vendor.Tool-1/bin/tool"),
            ],
            HOLDERS,
        );
        match OS {
            crate::host::Os::MacOs | crate::host::Os::Linux => {
                assert_eq!(kinds(&holding), [("session", vec![1, 2])]);
            }
            crate::host::Os::Windows => {
                assert_eq!(kinds(&holding), [("app", vec![1]), ("extension", vec![2])]);
            }
        }
        assert!(Location::WithinPrefixed("vendor.tool-").holds(&at(1, "/e/vendor.tool-1/t")));
        assert!(
            !Location::WithinPrefixed("vendor.tool-").holds(&at(1, "/e/vendor.tool/t")),
            "a name shorter than the prefix does not start with it"
        );
    }

    /// A Mac app is named by its bundle id and quit with Command-Q, since closing its last
    /// window leaves it running: the id is its own system's, whichever system finds it.
    #[test]
    fn an_app_is_named_and_quit_its_own_system_s_way() {
        let mac = AppId::MacBundle("com.example.app");
        assert_eq!(mac.as_str(), "com.example.app");
        assert_eq!(
            mac.reopen("App"),
            "quit App with Command-Q and open it again"
        );
        let app = classify(&[at(1, "/Applications/App.app/x/tool")], HOLDERS);
        assert_eq!(
            app.first().map(|h| h.holder.remedy),
            Some(Remedy::ReopenApp {
                app: mac,
                name: "App"
            })
        );
    }

    // By its family, so a package installed elsewhere is still the app, and a path is not.
    #[test]
    fn a_windows_app_is_its_package_and_is_quit_from_its_notification_area_icon() {
        const STORE: Holder = Holder {
            kind: "app",
            noun: Noun::One("the App"),
            location: Location::Package("Vendor.App_0000000000000"),
            remedy: Remedy::ReopenApp {
                app: AppId::WindowsAumid("Vendor.App_0000000000000!App"),
                name: "App",
            },
        };
        let packaged = |pid, family: &str| Process {
            package: Some(family.into()),
            ..at(pid, "D:/Apps/Vendor.App/tool.exe")
        };
        let holding = classify(
            &[
                packaged(1, "vendor.app_0000000000000"),
                packaged(2, "Vendor.Other_0000000000000"),
                at(
                    3,
                    "C:/Program Files/WindowsApps/Vendor.App_1.0.0.0_x64__0000000000000/tool.exe",
                ),
            ],
            &[STORE, SESSION],
        );
        assert_eq!(kinds(&holding), [("app", vec![1]), ("session", vec![2, 3])]);
        assert_eq!(
            AppId::WindowsAumid("Vendor.App_0000000000000!App").as_str(),
            "Vendor.App_0000000000000!App"
        );
        assert_eq!(
            remedies(&holding[..1], "to use the new account"),
            "Quit App from its notification-area icon and open it again to use the new account."
        );
    }

    #[test]
    fn nothing_running_is_nothing_held() {
        assert!(classify(&[], HOLDERS).is_empty());
        assert!(
            classify(&[at(1, "tool")], &[APP]).is_empty(),
            "and a process no kind claims is left out"
        );
    }

    #[test]
    fn an_app_is_one_thing_however_many_processes_it_runs() {
        let holding = classify(
            &[
                at(7, "/Applications/App.app/x/tool"),
                at(8, "/Applications/App.app/y/tool"),
            ],
            HOLDERS,
        );
        assert_eq!(described(&holding), "the App");
        assert!(!plural(&holding));
        assert_eq!(described_with_pids(&holding), "the App (pid 7, 8)");
    }

    /// Alone, a kind's advice is what it always was. Beside others, each names what it is
    /// about, and the purpose leads.
    #[test]
    fn what_to_do_is_said_once_for_each_kind() {
        let sessions = classify(&[at(1, "tool"), at(2, "tool")], HOLDERS);
        assert_eq!(described(&sessions), "2 `tool` sessions");
        assert!(plural(&sessions));
        assert_eq!(
            remedies(&sessions, "to use the new account"),
            "Quit them and start again to use the new account."
        );

        let one = classify(&[at(1, "tool")], HOLDERS);
        assert_eq!(described(&one), "1 `tool` session");
        assert_eq!(
            remedies(&one, "to go on"),
            "Quit it and start again to go on."
        );

        let all = classify(
            &[
                at(1, "tool"),
                at(2, "/Applications/App.app/x/tool"),
                at(3, "/e/vendor.tool-1/bin/tool"),
            ],
            HOLDERS,
        );
        assert_eq!(
            described(&all),
            "the App, the extension and 1 `tool` session"
        );
        assert_eq!(
            remedies(&all, "to use the new account"),
            "To use the new account: quit App with Command-Q and open it again; reload the \
             editor's window; quit the `tool` session and start it again."
        );
    }

    #[test]
    fn a_command_is_said_as_one() {
        let holding = classify(
            &[at(1, "/x/daemon/tool")],
            &[Holder {
                kind: "daemon",
                noun: Noun::One("the daemon"),
                location: Location::Within("daemon"),
                remedy: Remedy::Run("tool daemon restart"),
            }],
        );
        assert_eq!(
            remedies(&holding, "to take a switch"),
            "Run `tool daemon restart` to take a switch."
        );
    }

    #[test]
    fn some_pids_stand_for_many() {
        assert_eq!(some_of(&[4321, 99]), "4321, 99");
        let many: Vec<u32> = (1..=23).collect();
        assert_eq!(some_of(&many), "1, 2, 3 and 20 more");
    }
}