swarmy 0.2.12

Real-time analytics dashboard for coding agents (Claude Code, Antigravity, Codex): live swarm, history, API-equivalent spend, weekly limits, and a vault for OAuth logins
Documentation
//! Per-harness transcript discovery and incremental chunk reading for the index.
//!
//! Keeps harness dispatch in one place so `index::Index` stays harness-agnostic and future
//! harnesses (e.g. OpenCode) plug in here without growing `src/index/mod.rs`.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use rusqlite::Connection;

use crate::agy::summaries as agy_summaries;
use crate::claude::files::{self, FileKind, TranscriptFile};
use crate::claude::seats::{Seat, SeatId};
use crate::claude::transcript;
use crate::core::harness::Harness;

/// The dirs a session's files were listed from (with their mtimes), and the files.
pub(super) type ListedDirs = (Vec<(PathBuf, Option<SystemTime>)>, Vec<TranscriptFile>);

/// Lists every transcript file belonging to `seat`, dispatching by `seat.harness`.
pub(super) fn list_seat_files(seat: &Seat) -> Vec<TranscriptFile> {
    match seat.harness {
        Harness::Claude => files::list(seat),
        Harness::Agy => {
            let metas = agy_summaries::read_all(&seat.dir);
            crate::agy::meta::list_transcripts(&seat.id, &seat.dir, &metas)
        }
        Harness::Codex => crate::codex::meta::list_transcripts(&seat.id, &seat.dir),
    }
}

/// Reads the new bytes of `f` starting at `offset` using the harness-specific transcript parser.
pub(super) fn read_transcript_chunk(
    f: &TranscriptFile,
    offset: u64,
) -> std::io::Result<transcript::Chunk> {
    match f.harness {
        Harness::Claude => transcript::read_from(&f.path, offset),
        Harness::Agy => {
            let conv_id = if f.agent_id.is_empty() {
                f.session_id.as_str()
            } else {
                f.agent_id.as_str()
            };
            crate::agy::transcript::read_from(&f.path, offset, conv_id, f.default_cwd.as_deref())
        }
        Harness::Codex => crate::codex::rollout::read_from(&f.path, offset, &f.session_id),
    }
}

/// Lists transcript files recently modified in the `files` table (`mtime >= since`), hydrating
/// Antigravity files with targeted `conversation_summaries.db` metadata and Codex files with
/// their thread names.
pub(super) fn recent_files(
    conn: &Connection,
    seats: &[Seat],
    since: i64,
) -> anyhow::Result<Vec<TranscriptFile>> {
    let mut out: Vec<TranscriptFile> = Vec::new();
    let mut agy_recent: HashMap<SeatId, Vec<String>> = HashMap::new();
    {
        let mut st = conn.prepare(
            "SELECT path, seat, session_id, agent_id, kind FROM files WHERE mtime >= ?1",
        )?;
        let rows = st.query_map([since], |r| {
            let kind: String = r.get(4)?;
            let seat_id: SeatId = r.get(1)?;
            let session_id: String = r.get(2)?;
            let agent_id: String = r.get(3)?;
            let path = PathBuf::from(r.get::<_, String>(0)?);
            Ok((path, seat_id, session_id, agent_id, kind))
        })?;
        for r in rows {
            let (path, seat_id, session_id, agent_id, kind) = r?;
            // A file of a seat this process does not know (a harness turned off, a seat found
            // only through another process's environment) is not read: which parser fits it is
            // unknown, and a wrong one would move the offset past its spend.
            let Some(seat) = seats.iter().find(|s| s.id == seat_id) else {
                continue;
            };
            match seat.harness {
                Harness::Agy => {
                    let cid = if agent_id.is_empty() {
                        session_id
                    } else {
                        agent_id
                    };
                    agy_recent.entry(seat_id).or_default().push(cid);
                    continue;
                }
                Harness::Codex => {
                    let titles = crate::codex::meta::titles(&seat.dir);
                    out.extend(crate::codex::meta::transcript_file(&seat.id, path, &titles));
                    continue;
                }
                Harness::Claude => {}
            }
            // `files.kind` holds the tag only: a workflow agent's run is its dir, as listed.
            let kind = match kind.as_str() {
                "main" => FileKind::Main,
                "subagent" => FileKind::Subagent,
                // "workflow", the only other tag the column's CHECK allows.
                _workflow => FileKind::Workflow(files::workflow_run_of(&path)),
            };
            out.push(TranscriptFile {
                path,
                seat: seat_id,
                session_id,
                agent_id,
                kind,
                harness: Harness::Claude,
                default_cwd: None,
                session_title: None,
                inline_meta: None,
            });
        }
    }
    for (seat_id, cids) in agy_recent {
        if let Some(seat) = seats.iter().find(|s| s.id == seat_id) {
            let metas = agy_summaries::read_all(&seat.dir);
            for cid in &cids {
                if let Some(tf) =
                    crate::agy::meta::transcript_file(&seat.id, &seat.dir, cid, &metas)
                {
                    out.push(tf);
                }
            }
        }
    }
    Ok(out)
}

/// Lists the files of a live session, caching Claude directory listings by directory mtime.
pub(super) fn session_files(
    cache: &mut HashMap<(SeatId, String), ListedDirs>,
    seat: &Seat,
    sid: &str,
) -> Vec<TranscriptFile> {
    match seat.harness {
        Harness::Agy => {
            let metas = agy_summaries::read_all(&seat.dir);
            return crate::agy::meta::list_session_transcripts(&seat.id, &seat.dir, sid, &metas);
        }
        // A thread is one rollout, found once and remembered (`codex::meta::find_rollout`).
        Harness::Codex => {
            let titles = crate::codex::meta::titles(&seat.dir);
            return crate::codex::meta::find_rollout(&seat.dir, sid)
                .and_then(|p| crate::codex::meta::transcript_file(&seat.id, p, &titles))
                .into_iter()
                .collect();
        }
        Harness::Claude => {}
    }
    let key = (seat.id.clone(), sid.to_string());
    let mtime = |p: &Path| std::fs::metadata(p).and_then(|m| m.modified()).ok();
    if let Some((dirs, cached)) = cache.get(&key)
        && !dirs.is_empty()
        && dirs.iter().all(|(d, t)| mtime(d) == *t)
    {
        return cached.clone();
    }
    let list = files::list_session(seat, sid);
    // Watch the dirs that hold these files (main dir, subagents, workflow runs).
    let mut dirs: Vec<PathBuf> = list
        .iter()
        .filter_map(|f| f.path.parent().map(Path::to_path_buf))
        .collect();
    // Also dirs that may not exist yet: creating them is how a first subagent shows up.
    for f in list.iter().filter(|f| f.kind == FileKind::Main) {
        if let Some(pdir) = f.path.parent() {
            let base = pdir.join(sid);
            dirs.push(base.join("subagents").join("workflows"));
            dirs.push(base.join("subagents"));
            dirs.push(base);
        }
    }
    dirs.sort();
    dirs.dedup();
    let stamped = dirs
        .into_iter()
        .map(|d| {
            let t = mtime(&d);
            (d, t)
        })
        .collect();
    cache.insert(key, (stamped, list.clone()));
    list
}

/// A woken agent answers within this long, or never: Claude Code woke 2,485 agents on the
/// machine this was measured on, 2,484 answered within four minutes (99.9% within 130 s), and
/// none of the rest ever did (the one exception was taken up again by a new message after 1.6
/// hours). One that has not answered by then did no more work and is finished; were it to
/// answer later, its new lines make it unfinished again.
const WAKE_ANSWERED_WITHIN_MS: i64 = 10 * 60 * 1000;

/// Whether the agent of an unchanged transcript finished since it was read. Claude Code writes
/// `"finished": true` into the agent's `meta.json` a moment after the agent's last transcript
/// line, so a refresh between the two has read the whole transcript with the old meta, and the
/// transcript never changes again; a workflow agent is finished by its run's record, written
/// when the run ends (`files::run_ended`). Cheap enough for every pass: a `stat` of the meta per
/// unfinished agent, which is read only when it is not older than the transcript (the one
/// written at the spawn is), and one look at each run's record per pass (`runs`: a run's agents
/// share it, and it can be large). An agent whose state the listing brought along (agy's
/// conversation summaries, `inline_meta`) costs nothing more: agy marks a subagent idle or
/// killed there, not with a transcript line. A wake-up (`woken`: when the agent was woken and has
/// not answered since, and `now`) that is not answered in time finishes it too.
pub(super) fn finished_late(
    f: &TranscriptFile,
    transcript: &std::fs::Metadata,
    (woken, now): (Option<i64>, i64),
    runs: &mut HashMap<PathBuf, bool>,
) -> bool {
    if woken.is_some_and(|t| now - t >= WAKE_ANSWERED_WITHIN_MS) {
        return true;
    }
    if let Some(meta) = &f.inline_meta {
        return meta.ended();
    }
    if run_ended(f, runs) {
        return true;
    }
    let Some(path) = f.meta_path() else {
        return false;
    };
    // No meta, or no times on this file system: nothing says it finished.
    let newer = match (
        std::fs::metadata(&path).and_then(|m| m.modified()),
        transcript.modified(),
    ) {
        (Ok(meta), Ok(transcript)) => meta >= transcript,
        (Err(_), Ok(_) | Err(_)) | (Ok(_), Err(_)) => false,
    };
    newer && files::read_meta(&path).is_some_and(|m| m.ended())
}

/// Whether the workflow run of `f`'s agent ended (`files::run_ended`), its record read once per
/// pass (`runs`): a run's agents share it, and it can be a few hundred KB.
pub(super) fn run_ended(f: &TranscriptFile, runs: &mut HashMap<PathBuf, bool>) -> bool {
    files::run_record(f).is_some_and(|record| {
        *runs
            .entry(record)
            .or_insert_with_key(|r| files::run_ended(r))
    })
}