pitboard-core 0.22.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
//! What holds Antigravity's login while it runs (`agy_holders`): the App, whose language
//! server keeps the session in memory, the IDE, and every agy, the hub the IDE starts
//! among them. Each renews the login from memory and writes it back, over a switch.

use crate::holder::{AppId, Holder, Location, Noun, Remedy};
use crate::host::{Listed, Os};
use std::path::Component;

/// What holds it, by kind, most particular first: the App, which Pitboard can ask to quit on
/// macOS (bundle `com.google.antigravity`, App 2.36.0's `Info.plist`), the IDE, and every other
/// program of Antigravity's, agy and the hub among them, which the person closes.
pub(crate) const HOLDERS: &[Holder] = &[
    Holder {
        kind: "antigravity_app",
        noun: Noun::One("the Antigravity app"),
        location: Location::Within("Antigravity.app"),
        remedy: Remedy::ReopenApp {
            app: AppId::MacBundle("com.google.antigravity"),
            name: "Antigravity",
        },
    },
    Holder {
        kind: "antigravity_ide",
        noun: Noun::One("the Antigravity IDE"),
        location: Location::Within("Antigravity IDE.app"),
        remedy: Remedy::Do("quit the Antigravity IDE"),
    },
    Holder {
        kind: "antigravity_program",
        noun: Noun::Counted {
            one: "other Antigravity program, such as `agy`",
            many: "other Antigravity programs, such as `agy`",
        },
        location: Location::Anywhere,
        remedy: Remedy::Restart,
    },
];

// The programs by their file names: agy as its installer names it and as its archive does, the
// language server the App runs, and the IDE's own program (IDE 2.8.0's `applicationName`).
const PROGRAMS: [&str; 4] = ["agy", "antigravity", "language_server", "antigravity-ide"];

// The bundles whose every program counts, on macOS: the App's and the IDE's.
const BUNDLES: [&str; 2] = ["Antigravity.app", "Antigravity IDE.app"];

/// Whether `one` holds Antigravity's login on `os`. Read broadly on purpose: a process counted
/// that holds nothing costs a person a quit, one missed costs them the switch.
pub(crate) fn holds(os: Os, one: &Listed) -> bool {
    let named = |name: &str| match os {
        Os::Windows => {
            let name = name.to_ascii_lowercase();
            let name = name.strip_suffix(".exe").unwrap_or(&name).to_owned();
            PROGRAMS.contains(&name.as_str())
        }
        Os::MacOs | Os::Linux => PROGRAMS.contains(&name),
    };
    let in_a_bundle = os == Os::MacOs
        && one.path.components().any(|part| match part {
            Component::Normal(name) => BUNDLES.iter().any(|bundle| name == *bundle),
            _ => false,
        });
    in_a_bundle
        || one
            .path
            .file_name()
            .and_then(std::ffi::OsStr::to_str)
            .is_some_and(named)
}

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

    fn at(path: &str) -> Listed {
        Listed {
            pid: 812,
            parent: 1,
            path: PathBuf::from(path),
            package: None,
            placed: true,
        }
    }

    #[test]
    fn every_part_of_antigravity_holds_its_login_and_nothing_else_does() {
        for path in [
            "/Users/a/.local/bin/agy",
            "/Applications/Antigravity.app/Contents/Resources/bin/language_server",
            "/Applications/Antigravity.app/Contents/MacOS/Antigravity",
            "/Applications/Antigravity IDE.app/Contents/Frameworks/Code Helper.app/x",
        ] {
            assert!(holds(Os::MacOs, &at(path)), "{path}");
        }
        assert!(holds(Os::Linux, &at("/home/a/.local/bin/agy")));
        assert!(holds(
            Os::Linux,
            &at("/opt/antigravity-ide/antigravity-ide")
        ));
        assert!(
            holds(Os::Windows, &at("AGY.EXE")),
            "Windows names a program in any case"
        );
        assert!(holds(Os::Windows, &at("language_server.exe")));
        for path in [
            "/usr/bin/claude",
            "/Applications/Claude.app/Contents/MacOS/Claude",
            "/home/a/agy-notes/vim",
            "/usr/bin/agyx",
        ] {
            assert!(!holds(Os::MacOs, &at(path)), "{path}");
            assert!(!holds(Os::Linux, &at(path)), "{path}");
        }
    }
}

#[cfg(test)]
mod refusing {
    use crate::context::ScratchMachine;
    use crate::error::Error;
    use crate::provider::agy::engine::Agy;

    // A switch, and a sign-in before its browser opens, are refused while any of it runs,
    // and while whether any does cannot be told.
    #[test]
    fn antigravitys_login_moves_only_while_nothing_holds_it() {
        let home = tempfile::tempdir().unwrap();
        let m = ScratchMachine::at(home.path(), |ctx| ctx);
        assert!(crate::switch::unheld(&m.ctx, &Agy).is_ok());

        m.host.runs_at("agy", &["/Users/a/.local/bin/agy"]);
        m.host.runs_at(
            "language_server",
            &["/Applications/Antigravity.app/Contents/Resources/bin/language_server"],
        );
        match crate::switch::unheld(&m.ctx, &Agy) {
            Err(Error::LoginHeld { holding, .. }) => {
                assert_eq!(holding.len(), 2, "{holding:?}");
                assert!(holding.iter().any(|one| one.starts_with("agy (pid ")));
                let said = Error::LoginHeld {
                    tool: crate::provider::ProviderId::Agy,
                    holding,
                }
                .to_string();
                assert!(said.contains("Antigravity's login is in use by"), "{said}");
                assert!(said.ends_with("Nothing has changed."), "{said}");
            }
            other => panic!("{other:?}"),
        }

        m.host.without_a_process_list();
        assert!(matches!(
            crate::switch::unheld(&m.ctx, &Agy),
            Err(Error::AppStateUnknown {
                app: "Antigravity",
                ..
            })
        ));
    }

    #[test]
    fn claude_code_and_codex_switch_whatever_runs() {
        let home = tempfile::tempdir().unwrap();
        let m = ScratchMachine::at(home.path(), |ctx| ctx);
        m.host.without_a_process_list();
        for tool in [
            crate::provider::documents()
                .find(|tool| tool.id() == crate::provider::ProviderId::Claude)
                .unwrap(),
            crate::provider::documents()
                .find(|tool| tool.id() == crate::provider::ProviderId::Codex)
                .unwrap(),
        ] {
            assert!(crate::switch::unheld(&m.ctx, tool).is_ok());
        }
    }
}

#[cfg(test)]
mod saying {
    use crate::context::ScratchMachine;
    use crate::provider::ProviderId;
    use crate::switch::{StillHolding, still_holding};

    // What holds the sign-in is said by kind, the App first, so the apps can quit it.
    #[test]
    fn what_holds_antigravitys_login_is_said_by_kind() {
        let home = tempfile::tempdir().unwrap();
        let m = ScratchMachine::at(home.path(), |ctx| ctx);
        assert!(matches!(
            still_holding(&m.ctx, ProviderId::Agy),
            StillHolding::Nothing
        ));
        m.host.runs_at(
            "language_server",
            &["/Applications/Antigravity.app/Contents/Resources/bin/language_server"],
        );
        m.host.runs_at("agy", &["agy", "/Users/a/.local/bin/agy"]);
        let StillHolding::These(holding) = still_holding(&m.ctx, ProviderId::Agy) else {
            panic!("held");
        };
        let kinds: Vec<&str> = holding.iter().map(|held| held.holder.kind).collect();
        assert_eq!(kinds, ["antigravity_app", "antigravity_program"]);
        assert_eq!(holding[1].pids.len(), 2);
        assert!(ProviderId::Agy.waits_for_holders());
        assert!(!ProviderId::Codex.waits_for_holders());
    }
}