egrix 0.1.0

Find leaked credentials and suspicious network destinations in AI agent transcripts. Local, offline, redacted by default.
//! Where each agent keeps its conversation history. Adding an agent is one
//! entry in `roots`; nothing else in the crate knows a transcript path.

use crate::model::Agent;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

/// Larger files are skipped: transcripts stay far below this, and the cap keeps
/// a stray multi-gigabyte blob out of memory.
const MAX_FILE_BYTES: u64 = 50 * 1024 * 1024;

const TEXT_EXTENSIONS: &[&str] = &["jsonl", "json", "ndjson", "log", "md", "txt", "yaml", "yml"];

struct Root {
    dir: PathBuf,
    /// When set, only files with these names are transcripts in this root.
    only: Option<&'static [&'static str]>,
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Transcript {
    pub agent: Agent,
    pub path: PathBuf,
}

fn roots(agent: Agent, home: &Path) -> Vec<Root> {
    let root = |parts: &[&str], only| Root { dir: parts.iter().fold(home.to_path_buf(), |p, s| p.join(s)), only };
    match agent {
        Agent::Claude => vec![root(&[".claude", "projects"], None)],
        Agent::Gemini => vec![root(&[".gemini", "tmp"], Some(&["logs.json", "checkpoint.json"])), root(&[".gemini", "history"], None)],
        Agent::Copilot => vec![root(&[".copilot", "session-state"], None), root(&[".copilot", "logs"], None)],
        Agent::Antigravity => vec![root(&[".gemini", "antigravity"], None)],
    }
}

/// Every transcript file of the selected agents, in a stable order.
pub fn discover(home: &Path, only: Option<Agent>) -> Vec<Transcript> {
    let mut out = Vec::new();
    for agent in Agent::ALL.into_iter().filter(|a| only.is_none_or(|o| o == *a)) {
        for root in roots(agent, home) {
            for entry in WalkDir::new(&root.dir).follow_links(false).into_iter().flatten() {
                if !entry.file_type().is_file() {
                    continue;
                }
                let path = entry.path();
                let wanted = match root.only {
                    Some(names) => path.file_name().and_then(|n| n.to_str()).is_some_and(|n| names.contains(&n)),
                    None => path
                        .extension()
                        .and_then(|e| e.to_str())
                        .is_some_and(|e| TEXT_EXTENSIONS.contains(&e.to_ascii_lowercase().as_str())),
                };
                let small = entry.metadata().is_ok_and(|m| m.len() <= MAX_FILE_BYTES);
                if wanted && small {
                    out.push(Transcript { agent, path: path.to_path_buf() });
                }
            }
        }
    }
    out.sort();
    out
}