kaeru 0.1.3

Reopens your terminal tabs, and the coding agent sessions in them, after the terminal closes or the computer restarts.
//! Warp. It runs all of its windows and tabs in one `warp.exe` process (verify
//! on macOS and Linux), starts each tab's shell as a direct child of that
//! process, and sets `TERM_PROGRAM=WarpTerminal` and
//! `WARP_TERMINAL_SESSION_UUID`, an id for the tab that stays the same when
//! Warp restores the tab after it closed. Warp restores its own tabs, so kaeru
//! doesn't reopen them; it resumes the agent session in the restored tab.

use std::path::Path;

use anyhow::{Result, bail};

use super::{Env, TabToOpen, TerminalAdapter};
use crate::engine::rules::TerminalShape;
use crate::procs::ProcInfo;

/// The adapter for Warp.
pub struct Warp;

impl TerminalAdapter for Warp {
    fn id(&self) -> &'static str {
        "warp"
    }

    fn name(&self) -> &'static str {
        "Warp"
    }

    fn shape(&self) -> TerminalShape {
        TerminalShape::Tabs
    }

    fn find(&self, env: &Env, lineage: &[ProcInfo]) -> Option<ProcInfo> {
        // Programs started from a Warp tab inherit these variables, so a
        // tab's shell is also a direct child of Warp.
        if env.get("TERM_PROGRAM").map(String::as_str) != Some("WarpTerminal") {
            return None;
        }
        lineage
            .get(1)
            .filter(|parent| {
                parent.name.eq_ignore_ascii_case("warp.exe")
                    || parent.name.eq_ignore_ascii_case("warp")
            })
            .cloned()
    }

    fn tab_id(&self, env: &Env) -> Option<String> {
        env.get("WARP_TERMINAL_SESSION_UUID").cloned()
    }

    fn restores_its_own_tabs(&self) -> bool {
        true
    }

    fn reopen(&self, _exe: Option<&Path>, _tabs: &[TabToOpen]) -> Result<()> {
        bail!("Warp reopens its own tabs, and kaeru resumes their agent sessions there")
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::procs::{fake::FakeTable, lineage};

    fn warp_env() -> Env {
        Env::from([
            ("TERM_PROGRAM".to_string(), "WarpTerminal".to_string()),
            (
                "WARP_TERMINAL_SESSION_UUID".to_string(),
                "00000000000040008000000000000004".to_string(),
            ),
        ])
    }

    #[test]
    fn a_shell_that_warp_started_is_a_tab_with_warp_s_id() {
        let table = FakeTable::new(&[
            (20, Some(10), "powershell.exe", Some(200)),
            (10, None, "warp.exe", Some(100)),
        ]);
        let found = Warp.find(&warp_env(), &lineage(&table, 20));
        assert_eq!(found.map(|process| process.pid), Some(10));
        assert_eq!(
            Warp.tab_id(&warp_env()).as_deref(),
            Some("00000000000040008000000000000004")
        );
    }

    #[test]
    fn a_shell_inside_a_warp_tab_is_not_a_tab() {
        let table = FakeTable::new(&[
            (30, Some(20), "pwsh.exe", Some(300)),
            (20, Some(10), "powershell.exe", Some(200)),
            (10, None, "warp.exe", Some(100)),
        ]);
        assert!(Warp.find(&warp_env(), &lineage(&table, 30)).is_none());
        // Without Warp's variable, a warp.exe is some other program.
        let table = FakeTable::new(&[
            (20, Some(10), "powershell.exe", Some(200)),
            (10, None, "warp.exe", Some(100)),
        ]);
        assert!(Warp.find(&Env::new(), &lineage(&table, 20)).is_none());
    }
}