plannotator-tui 0.12.1

Annotate Markdown in the terminal - standalone, and as a Herdr plugin.
//! PID metadata and cwd/mtime discovery after no exact path or id was supplied.

use std::path::{Path, PathBuf};
#[cfg(unix)]
use std::process::Command;

use anyhow::{Context, Result, bail};
#[cfg(unix)]
use plannotator_tui_hosts::copilot;
use plannotator_tui_hosts::{Host, Match, Message, antigravity, claude, commandcode, droid, opencode, pi};

use super::LastOptions;
use super::exact;
use super::readers;
use super::roots::Roots;

/// How a transcript was chosen, so the UI can say when nothing identified it exactly.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum Discovery {
    /// An explicit path, an exact session id, a thread or conversation id, the session
    /// registered for the agent's pid, or the file the agent process itself has open: this
    /// transcript is the session, not a guess.
    Exact,
    /// The newest transcript filed under the agent's working directory. Sessions that share
    /// one directory are indistinguishable here.
    Folder,
    /// The newest session the host recorded, not scoped to a directory.
    Session,
}

pub(super) fn read(
    host: Host,
    options: &LastOptions,
    cwd: &Path,
    roots: &Roots,
    pick: usize,
) -> Result<(PathBuf, Vec<Message>, Discovery)> {
    match host {
        Host::ClaudeCode => {
            let (path, rung) = find_claude_transcript(options.pid, cwd, roots)?;
            let messages = readers::claude_messages(&path, pick)?;
            Ok((path, messages, discovery_of(rung)))
        }
        Host::Codex => {
            #[cfg(target_os = "linux")]
            if let Some(pid) = options.pid {
                let path = find_codex_transcript(pid)?;
                let (path, messages) = readers::explicit(host, &path, None, options.pid, pick)?;
                return Ok((path, messages, Discovery::Exact));
            }
            let thread = std::env::var("CODEX_THREAD_ID").ok().filter(|thread| !thread.is_empty());
            let discovery = if thread.is_some() { Discovery::Exact } else { Discovery::Session };
            let (path, messages) = readers::codex_thread(&roots.codex_home, thread.as_deref(), pick)?;
            Ok((path, messages, discovery))
        }
        Host::Copilot => {
            let (path, rung) = find_copilot_session(options.pid, cwd, roots)?;
            let messages = readers::copilot_messages(&path, pick)?;
            Ok((path, messages, discovery_of(rung)))
        }
        Host::Droid => {
            let path = find_droid_transcript(cwd, roots)?;
            let messages = readers::droid_messages(&path, pick)?;
            Ok((path, messages, Discovery::Folder))
        }
        Host::Pi => {
            // A running Pi with our extension lists its pid and transcript in the Pi link
            // registry: that is the session, even when another Pi shares the folder.
            // Until that session has a saved reply, the folder's newest transcript is
            // another session's, so say why instead of opening it.
            let registered =
                options.pid.and_then(|pid| crate::pi::live_entry_for_pid(&crate::pi::root(), pid));
            if let Some(entry) = registered {
                let (id, pid) = (&entry.id, entry.pid);
                let path = match entry.transcript {
                    None => bail!(
                        "Pi session {id} (pid {pid}) is not being saved (pi --no-session): it has no transcript to read"
                    ),
                    Some(path) if !path.is_file() => {
                        bail!("Pi session {id} (pid {pid}) has not saved a reply yet")
                    }
                    Some(path) => path,
                };
                let messages = readers::pi_messages(&path, Some(pid), pick)?;
                return Ok((path, messages, Discovery::Exact));
            }
            let path = find_pi_transcript(cwd, roots, ".pi/agent", "pi")?;
            let messages = readers::pi_messages(&path, None, pick)?;
            Ok((path, messages, Discovery::Folder))
        }
        Host::CommandCode => {
            let path = find_commandcode_transcript(cwd, roots)?;
            let messages = readers::commandcode_messages(&path, pick)?;
            Ok((path, messages, Discovery::Folder))
        }
        Host::Antigravity => {
            // Inside an `agy` shell its exported conversation id is exact, as Codex's thread id is.
            let conversation =
                std::env::var(antigravity::CONVERSATION_ENV).ok().filter(|id| !id.trim().is_empty());
            if let Some(id) = conversation {
                let session = exact::resolve(host, &id, cwd, roots)?;
                let (path, messages) = readers::exact(host, &id, session, options.pid, pick)?;
                return Ok((path, messages, Discovery::Exact));
            }
            let path = antigravity::find_transcript(&roots.antigravity_home, cwd)?;
            let messages = readers::antigravity_messages(&path, pick)?;
            Ok((path, messages, Discovery::Folder))
        }
        Host::Omp => bail!(
            "OMP session discovery without an exact path or id is unsupported; pass --session or --session-id"
        ),
        Host::Hermes => bail!("hermes needs a session id (Herdr provides it; or pass --session-id)"),
        Host::OpenCode => opencode_for_cwd(cwd, roots, pick),
    }
}

/// What a resolver's rung means for the footer: a session registered for the agent's pid
/// is the session; a cwd or folder rung is the folder's newest transcript; an unscoped
/// rung is just the newest session the host has.
fn discovery_of(rung: Match) -> Discovery {
    match rung {
        Match::Session => Discovery::Exact,
        Match::Cwd | Match::Folder => Discovery::Folder,
        Match::Newest => Discovery::Session,
    }
}

/// A selected Linux process is a stronger hint than an inherited thread id or the newest
/// rollout. Missing or ambiguous descriptors must not silently select another session.
/// Codex keeps its subagents' rollouts (reviews, guardians) open too; those are ignored.
#[cfg(target_os = "linux")]
fn find_codex_transcript(pid: u32) -> Result<PathBuf> {
    let directory = PathBuf::from(format!("/proc/{pid}/fd"));
    let entries = std::fs::read_dir(&directory)
        .with_context(|| format!("reading Codex process {pid} descriptors in {}", directory.display()))?;
    let mut transcripts = std::collections::BTreeSet::new();
    for entry in entries {
        let entry = entry.with_context(|| format!("reading {}", directory.display()))?;
        // Descriptors can close between readdir and readlink.
        let Ok(path) = std::fs::read_link(entry.path()) else { continue };
        if path.extension().is_some_and(|extension| extension == "jsonl")
            && path
                .file_name()
                .and_then(|name| name.to_str())
                .is_some_and(|name| name.starts_with("rollout-"))
            && path.is_file()
            && !plannotator_tui_hosts::codex::is_subagent(&path)
        {
            transcripts.insert(path);
        }
    }
    if transcripts.len() != 1 {
        bail!(
            "Codex process {pid} has {} open transcripts; pass --session or --session-id to select one",
            transcripts.len()
        );
    }
    transcripts.into_iter().next().with_context(|| format!("no transcript for Codex process {pid}"))
}

fn find_claude_transcript(pid: Option<u32>, cwd: &Path, roots: &Roots) -> Result<(PathBuf, Match)> {
    let sessions_dir = roots.claude_config.join("sessions");
    let projects_dir = roots.claude_config.join("projects");
    let (start_pid, table) = process_context(pid);
    claude::find_transcript(&sessions_dir, &projects_dir, cwd, &table, start_pid).ok_or_else(|| {
        anyhow::anyhow!(
            "no Claude Code transcript for pid {start_pid} (looked in {} and {})",
            sessions_dir.display(),
            projects_dir.join(claude::project_slug(cwd)).display()
        )
    })
}

#[cfg(unix)]
fn find_copilot_session(pid: Option<u32>, cwd: &Path, roots: &Roots) -> Result<(PathBuf, Match)> {
    let (start_pid, table) = process_context(pid);
    copilot::find_session(&roots.copilot_home, cwd, &table, start_pid, is_copilot_process).ok_or_else(|| {
        anyhow::anyhow!(
            "no Copilot CLI session for pid {start_pid} or {} (looked in {})",
            cwd.display(),
            roots.copilot_home.join("session-state").display()
        )
    })
}

#[cfg(not(unix))]
fn find_copilot_session(_pid: Option<u32>, _cwd: &Path, _roots: &Roots) -> Result<(PathBuf, Match)> {
    bail!("Copilot session discovery without --session-id is unsupported on Windows; pass --session-id")
}

/// Does `pid` still name a Copilot process? Locks outlive sessions and pids get reused.
#[cfg(unix)]
fn is_copilot_process(pid: u32) -> bool {
    Command::new("ps")
        .args(["-o", "comm=", "-p", &pid.to_string()])
        .output()
        .ok()
        .filter(|output| output.status.success())
        .map(|output| String::from_utf8_lossy(&output.stdout).trim().to_owned())
        .is_some_and(|name| name.rsplit('/').next().unwrap_or(&name).starts_with("copilot"))
}

fn find_droid_transcript(cwd: &Path, roots: &Roots) -> Result<PathBuf> {
    droid::find_transcript(&roots.factory_config, cwd).ok_or_else(|| {
        anyhow::anyhow!(
            "no Droid session for {} (looked in {})",
            cwd.display(),
            roots.factory_config.join("sessions").join(claude::project_slug(cwd)).display()
        )
    })
}

fn find_pi_transcript(cwd: &Path, roots: &Roots, default_agent_dir: &str, label: &str) -> Result<PathBuf> {
    let sessions_dir = roots.pi_sessions(default_agent_dir);
    pi::find_transcript(&sessions_dir, cwd).ok_or_else(|| {
        anyhow::anyhow!("no {label} session for {} (looked in {})", cwd.display(), sessions_dir.display())
    })
}

fn find_commandcode_transcript(cwd: &Path, roots: &Roots) -> Result<PathBuf> {
    let projects = roots.commandcode_projects();
    commandcode::find_transcript(&projects, cwd).ok_or_else(|| {
        anyhow::anyhow!(
            "no Command Code session started in {} (looked in {})",
            cwd.display(),
            projects.join(commandcode::project_slug(cwd)).display()
        )
    })
}

#[cfg(unix)]
fn process_context(pid: Option<u32>) -> (u32, Vec<(u32, u32)>) {
    (pid.unwrap_or_else(std::os::unix::process::parent_id), process_table())
}

#[cfg(not(unix))]
fn process_context(pid: Option<u32>) -> (u32, Vec<(u32, u32)>) {
    (pid.unwrap_or(0), Vec::new())
}

/// One POSIX `ps` snapshot; an empty table when it cannot run.
#[cfg(unix)]
fn process_table() -> Vec<(u32, u32)> {
    Command::new("ps")
        .args(["-eo", "pid=,ppid="])
        .output()
        .ok()
        .filter(|output| output.status.success())
        .map(|output| claude::parse_ps(&String::from_utf8_lossy(&output.stdout)))
        .unwrap_or_default()
}

fn opencode_for_cwd(cwd: &Path, roots: &Roots, pick: usize) -> Result<(PathBuf, Vec<Message>, Discovery)> {
    let databases = roots.opencode_databases();
    let mut best: Option<(PathBuf, opencode::Found)> = None;
    for database in &databases {
        if let Ok(found) = opencode::find_session(database, cwd)
            && best.as_ref().is_none_or(|(_, current)| found.updated > current.updated)
        {
            best = Some((database.clone(), found));
        }
    }
    let (database, found) = best.with_context(|| {
        format!("no OpenCode session for {} in {}", cwd.display(), exact::describe(&databases))
    })?;
    let messages = opencode::messages_for_session(&database, &found.id, found.schema, pick)?;
    Ok((database, messages, Discovery::Folder))
}

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

    #[test]
    fn only_a_pid_registered_session_counts_as_exact() {
        assert_eq!(discovery_of(Match::Session), Discovery::Exact);
        assert_eq!(discovery_of(Match::Cwd), Discovery::Folder);
        assert_eq!(discovery_of(Match::Folder), Discovery::Folder);
        assert_eq!(discovery_of(Match::Newest), Discovery::Session);
    }
}