Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

13.9 KB
Raw Blame
use super::model::{Layout, Snapshot, Tab};
use anyhow::{ensure, Context, Result};
use sha2::{Digest, Sha256};
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
#[cfg(unix)]
use {
    anyhow::bail,
    rustix::net::RecvFlags,
    serde_json::{json, Value},
    std::io::Write,
    std::os::unix::fs::{FileTypeExt, MetadataExt},
    std::os::unix::net::UnixStream,
    std::time::Duration,
};

pub const MAX_BYTES: usize = 16 * 1024 * 1024;

use thinkterm_import::ImportContext;
pub use thinkterm_import::{Preview, PreviewProject, PreviewTerminal, Session};

pub fn config_root(context: &ImportContext) -> Result<PathBuf> {
    let config = context
        .config_home
        .clone()
        .filter(|path| path.is_absolute())
        .unwrap_or_else(|| default_config_home(context));
    Ok(config.join("herdr"))
}

#[cfg(unix)]
fn default_config_home(context: &ImportContext) -> PathBuf {
    context.home.join(".config")
}

/// Herdr's Windows build keeps its folder under %APPDATA%.
#[cfg(windows)]
fn default_config_home(context: &ImportContext) -> PathBuf {
    std::env::var_os("APPDATA")
        .map(PathBuf::from)
        .filter(|path| path.is_absolute())
        .unwrap_or_else(|| context.home.join("AppData").join("Roaming"))
}

#[cfg(unix)]
fn owned(metadata: &fs::Metadata) -> bool {
    metadata.uid() == rustix::process::geteuid().as_raw()
}

/// Windows has no uid to compare. Herdr's folder there is in this user's
/// own profile, which other users cannot write to.
#[cfg(windows)]
fn owned(_: &fs::Metadata) -> bool {
    true
}

pub fn session_dir(context: &ImportContext, name: &str) -> Result<PathBuf> {
    ensure!(
        !name.is_empty()
            && name.len() <= 128
            && !name.contains('/')
            && !name.contains('\\')
            && !name.contains(':')
            && name != "."
            && name != ".."
            && !name.chars().any(char::is_control),
        "Invalid Herdr session name"
    );
    let root = config_root(context)?;
    let path = if name == "default" {
        root
    } else {
        root.join("sessions").join(name)
    };
    let metadata = fs::symlink_metadata(&path).context("Herdr session directory is unavailable")?;
    ensure!(
        metadata.is_dir() && !metadata.file_type().is_symlink(),
        "Herdr session must be a directory"
    );
    ensure!(
        owned(&metadata),
        "Herdr session belongs to a different user"
    );
    Ok(path)
}

/// A saved layout, or on unix a running server to hand its terminals
/// over. Windows imports the saved layout alone.
fn importable(dir: &Path) -> bool {
    dir.join("session.json").exists() || (cfg!(unix) && dir.join("herdr.sock").exists())
}

pub fn discover(context: &ImportContext) -> Result<Vec<Session>> {
    let root = config_root(context)?;
    let mut sessions = Vec::new();
    if importable(&root) {
        sessions.push(Session {
            name: "default".into(),
        });
    }
    match fs::read_dir(root.join("sessions")) {
        Ok(entries) => {
            for entry in entries.take(256) {
                let entry = entry?;
                if entry.file_type()?.is_dir() && importable(&entry.path()) {
                    if let Some(name) = entry.file_name().to_str() {
                        if session_dir(context, name).is_ok() {
                            sessions.push(Session { name: name.into() });
                        }
                    }
                }
            }
        }
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {}
        Err(err) => return Err(err.into()),
    }
    sessions.sort_by(|a, b| a.name.cmp(&b.name));
    Ok(sessions)
}

#[cfg(unix)]
pub fn read_line(stream: &mut UnixStream, limit: usize) -> Result<Vec<u8>> {
    let mut bytes = Vec::new();
    let mut byte = [0];
    loop {
        stream.read_exact(&mut byte)?;
        if byte[0] == b'\n' {
            return Ok(bytes);
        }
        ensure!(bytes.len() < limit, "Herdr response exceeds the size limit");
        bytes.push(byte[0]);
    }
}

/// `read_line` for replies that can run to megabytes: a chunk at a time
/// instead of a read per byte. It peeks first and consumes only through the
/// newline, so whatever follows (descriptors on a handoff) stays queued.
#[cfg(unix)]
pub fn read_large_line(stream: &mut UnixStream, limit: usize) -> Result<Vec<u8>> {
    let mut line = Vec::new();
    let mut chunk = vec![0; limit.saturating_add(1).min(64 * 1024)];
    loop {
        let room = (limit + 1 - line.len()).min(chunk.len());
        let peeked = match rustix::net::recv(&*stream, &mut chunk[..room], RecvFlags::PEEK) {
            Ok((peeked, _)) => peeked,
            Err(rustix::io::Errno::INTR) => continue,
            Err(err) => return Err(std::io::Error::from(err).into()),
        };
        ensure!(
            peeked > 0,
            std::io::Error::from(std::io::ErrorKind::UnexpectedEof)
        );
        let newline = chunk[..peeked].iter().position(|&byte| byte == b'\n');
        let take = newline.map_or(peeked, |at| at + 1);
        stream.read_exact(&mut chunk[..take])?;
        line.extend_from_slice(&chunk[..newline.unwrap_or(take)]);
        ensure!(line.len() <= limit, "Herdr response exceeds the size limit");
        if newline.is_some() {
            return Ok(line);
        }
    }
}

#[cfg(unix)]
fn connect(path: &Path, timeout: Duration) -> Result<UnixStream> {
    let metadata = fs::symlink_metadata(path)?;
    ensure!(
        metadata.file_type().is_socket() && metadata.uid() == rustix::process::geteuid().as_raw(),
        "Invalid Herdr socket owner or type"
    );
    let stream = UnixStream::connect(path)?;
    stream.set_read_timeout(Some(timeout))?;
    stream.set_write_timeout(Some(timeout))?;
    Ok(stream)
}

#[cfg(unix)]
pub fn request(dir: &Path, method: &str, params: Value, timeout: Duration) -> Result<Value> {
    let mut stream = connect(&dir.join("herdr.sock"), timeout)?;
    serde_json::to_writer(
        &mut stream,
        &json!({ "id": "thinkterm:import", "method": method, "params": params }),
    )?;
    stream.write_all(b"\n")?;
    let response: Value = serde_json::from_slice(&read_large_line(&mut stream, MAX_BYTES)?)?;
    if let Some(error) = response.get("error") {
        bail!(
            "Herdr: {}",
            error
                .get("message")
                .and_then(Value::as_str)
                .unwrap_or("request failed")
        );
    }
    response
        .get("result")
        .cloned()
        .context("Herdr returned no result")
}

pub fn saved_snapshot(dir: &Path) -> Result<(Snapshot, String)> {
    let path = dir.join("session.json");
    let metadata = fs::symlink_metadata(&path)?;
    ensure!(
        metadata.is_file() && !metadata.file_type().is_symlink() && owned(&metadata),
        "Invalid Herdr snapshot owner or type"
    );
    let mut bytes = Vec::new();
    fs::File::open(path)?
        .take((MAX_BYTES + 1) as u64)
        .read_to_end(&mut bytes)?;
    ensure!(bytes.len() <= MAX_BYTES, "Herdr snapshot is too large");
    let fingerprint = format!("{:x}", Sha256::digest(&bytes));
    Ok((Snapshot::parse(&bytes)?, fingerprint))
}

#[cfg(unix)]
pub(super) fn live_fingerprint(mut identity: Value) -> Result<String> {
    // GUI and mux builds may enable different serde_json map ordering.
    identity.sort_all_objects();
    Ok(format!(
        "{:x}",
        Sha256::digest(serde_json::to_vec(&identity)?)
    ))
}

fn preview_text(text: &str, limit: usize) -> Option<String> {
    let text: String = text
        .chars()
        .take(limit + 1)
        .map(|c| if c.is_control() { ' ' } else { c })
        .collect();
    let mut chars = text.trim().chars();
    let mut result: String = chars.by_ref().take(limit).collect();
    if chars.next().is_some() {
        result.push('…');
    }
    (!result.is_empty()).then_some(result)
}

#[cfg(unix)]
pub(super) fn live_projects(
    workspaces: &[Value],
    tabs: &[Value],
    panes: &[Value],
) -> Vec<PreviewProject> {
    workspaces
        .iter()
        .map(|workspace| {
            let project_tabs: Vec<_> = tabs
                .iter()
                .filter(|tab| tab["workspace_id"] == workspace["workspace_id"])
                .collect();
            let terminals: Vec<_> = panes
                .iter()
                .filter(|pane| pane["workspace_id"] == workspace["workspace_id"])
                .map(|pane| PreviewTerminal {
                    title: [
                        "title",
                        "label",
                        "terminal_title_stripped",
                        "terminal_title",
                        "display_agent",
                        "agent",
                    ]
                    .iter()
                    .find_map(|key| pane[*key].as_str().and_then(|s| preview_text(s, 256))),
                    tab_name: project_tabs
                        .iter()
                        .find(|tab| tab["tab_id"] == pane["tab_id"])
                        .and_then(|tab| tab["label"].as_str())
                        .and_then(|s| preview_text(s, 256)),
                    cwd: ["foreground_cwd", "cwd"]
                        .iter()
                        .find_map(|key| pane[*key].as_str().and_then(|s| preview_text(s, 4096))),
                })
                .collect();
            PreviewProject {
                threads: 1,
                name: workspace["label"].as_str().unwrap_or("Herdr").into(),
                tabs: project_tabs.len(),
                panes: terminals.len(),
                terminals,
            }
        })
        .collect()
}

pub(super) fn saved_projects(snapshot: &Snapshot) -> Vec<PreviewProject> {
    fn collect_terminals(layout: &Layout, tab: &Tab, terminals: &mut Vec<PreviewTerminal>) {
        match layout {
            Layout::Pane(id) => {
                let pane = &tab.panes[id];
                terminals.push(PreviewTerminal {
                    title: pane.label.as_deref().and_then(|s| preview_text(s, 256)),
                    tab_name: tab
                        .custom_name
                        .as_deref()
                        .and_then(|s| preview_text(s, 256)),
                    cwd: preview_text(&pane.cwd, 4096),
                });
            }
            Layout::Split { first, second, .. } => {
                collect_terminals(first, tab, terminals);
                collect_terminals(second, tab, terminals);
            }
        }
    }

    snapshot
        .workspaces
        .iter()
        .enumerate()
        .map(|(index, workspace)| {
            let mut terminals = Vec::new();
            for tab in &workspace.tabs {
                collect_terminals(&tab.layout, tab, &mut terminals);
            }
            PreviewProject {
                threads: 1,
                name: workspace.display_name(index),
                tabs: workspace.tabs.len(),
                panes: terminals.len(),
                terminals,
            }
        })
        .collect()
}

pub fn preview(context: &ImportContext, name: &str) -> Result<Preview> {
    let dir = session_dir(context, name)?;
    // Herdr's Windows server cannot hand its terminals over. Windows reads
    // the layout it saves a few seconds after each change, running or not.
    #[cfg(unix)]
    if let Some(preview) = live_preview(&dir, name)? {
        return Ok(preview);
    }
    let (snapshot, fingerprint) = saved_snapshot(&dir)?;
    let unavailable = snapshot
        .check_working_directories()
        .err()
        .map(|error| error.to_string());
    Ok(Preview {
        session: name.into(),
        live: false,
        version: None,
        fingerprint,
        projects: saved_projects(&snapshot),
        unavailable,
    })
}

/// The session as a running server reports it, or `None` when none answers.
#[cfg(unix)]
fn live_preview(dir: &Path, name: &str) -> Result<Option<Preview>> {
    let ping = request(dir, "ping", json!({}), Duration::from_secs(3));
    let ping = match ping {
        Ok(ping) => ping,
        Err(error)
            if error
                .chain()
                .filter_map(|e| e.downcast_ref::<std::io::Error>())
                .any(|e| {
                    matches!(
                        e.kind(),
                        std::io::ErrorKind::NotFound | std::io::ErrorKind::ConnectionRefused
                    )
                }) =>
        {
            return Ok(None)
        }
        Err(error) => return Err(error.context("Cannot inspect Herdr; no session was changed")),
    };
    let result = request(dir, "session.snapshot", json!({}), Duration::from_secs(5))?;
    let snapshot = result
        .get("snapshot")
        .context("Herdr did not return its session structure")?;
    let workspaces = snapshot["workspaces"]
        .as_array()
        .context("Missing Herdr workspaces")?;
    let tabs = snapshot["tabs"].as_array().context("Missing Herdr tabs")?;
    let panes = snapshot["panes"]
        .as_array()
        .context("Missing Herdr panes")?;
    ensure!(
        panes.len() <= super::model::MAX_PANES,
        "Herdr session has too many panes"
    );
    let identity = json!({
        "workspaces": workspaces.iter().map(|w| json!([w["workspace_id"], w["label"]])).collect::<Vec<_>>(),
        "tabs": tabs.iter().map(|t| json!([t["tab_id"], t["workspace_id"], t["label"]])).collect::<Vec<_>>(),
        "panes": panes.iter().map(|p| json!([p["pane_id"], p["terminal_id"], p["tab_id"]])).collect::<Vec<_>>()
    });
    let projects = live_projects(workspaces, tabs, panes);
    let available = ping["capabilities"]["live_handoff"]
        .as_bool()
        .unwrap_or(false);
    Ok(Some(Preview {
        session: name.into(),
        live: true,
        version: ping["version"].as_str().map(str::to_string),
        fingerprint: live_fingerprint(identity)?,
        projects,
        unavailable: if panes.is_empty() {
            Some("Herdr session is empty".into())
        } else if !available {
            Some("This running Herdr server does not support live handoff".into())
        } else {
            None
        },
    }))
}