Skip to main content

vitrify_cli/cmd/run_status/
mod.rs

1//! `vitrify run status`: what a run is doing, unit by unit. Everything comes
2//! from what vitrify records itself — `run.json`, `gate.json`,
3//! `events.jsonl`, the worker logs, process liveness, and each worker
4//! worktree's git state and front matter — never from a harness's output.
5//! It writes no file, takes no lock, and runs git only with
6//! `--no-optional-locks`, so it can be pointed at a live run from a second
7//! terminal without disturbing it.
8
9pub mod follow;
10pub mod render;
11pub mod snapshot;
12
13use std::path::{Path, PathBuf};
14
15use vitrify_core::CoreError;
16use vitrify_core::model::UtcTime;
17
18use crate::cli::StatusArgs;
19use crate::cmd;
20use crate::git::Git;
21use crate::out::emit;
22
23pub fn run(args: &StatusArgs) -> Result<u8, CoreError> {
24    let root = primary_root();
25    let job = resolve_job(&root, args.job.as_deref())?;
26    if args.follow {
27        return follow::run(&root, &job, args);
28    }
29    let snap = snapshot::take(&root, &job, UtcTime::now(), args.quiet_after)?;
30    if args.json {
31        emit(&render::json(&snap).dumps_indent2());
32    } else {
33        for line in render::human(&snap) {
34            emit(&line);
35        }
36    }
37    // a run was shown, whatever its state
38    Ok(0)
39}
40
41/// The primary checkout, whichever checkout the verb runs in: a run's
42/// records live in its `.vitrify/`.
43pub fn primary_root() -> PathBuf {
44    Git::new(cmd::cwd())
45        .primary_root()
46        .unwrap_or_else(|_| cmd::repo_root())
47}
48
49/// The job to show: `--job`, else the latest run recorded in this
50/// repository. Only the job ID is taken from `.vitrify/run.json`: two runs
51/// at once take turns writing that file, so everything else is read from
52/// the job's own copy.
53pub fn resolve_job(root: &Path, job: Option<&str>) -> Result<String, CoreError> {
54    if let Some(job) = job {
55        // a name, never a path out of the logs directory
56        if job.is_empty() || job.contains('/') || job == ".." {
57            return Err(CoreError::refusal(
58                2,
59                "error: --job takes a job ID such as run-20261116T140212Z-p48113",
60            ));
61        }
62        return Ok(job.to_string());
63    }
64    let path = root.join(".vitrify").join("run.json");
65    let none = || {
66        CoreError::refusal(
67            4,
68            "error: no run recorded in this repository → start and watch one: vitrify brief run",
69        )
70    };
71    let text = match std::fs::read_to_string(&path) {
72        Ok(text) => text,
73        Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Err(none()),
74        Err(e) => return Err(cannot_read(&path, e)),
75    };
76    let v: serde_json::Value = serde_json::from_str(&text).map_err(|e| cannot_read(&path, e))?;
77    match v["job_id"].as_str() {
78        Some(job) if !job.is_empty() && !job.contains('/') && job != ".." => Ok(job.to_string()),
79        _ => Err(none()),
80    }
81}
82
83/// The exit-1 refusal for a record that exists but cannot be read.
84pub fn cannot_read(path: &Path, e: impl std::fmt::Display) -> CoreError {
85    CoreError::refusal(1, format!("error: cannot read {}: {e}", path.display()))
86}