Files
orch/src/checks.rs
T
johannesandClaude Sonnet 5.5 7bf0986640 Add Needs-you queue and goal view with acceptance checks
- needs table: human steps, permission requests and agent questions wait
  for an answer written to the database; a/c/d, y/Y/n, r keys in the TUI
- goal view (g): task graph, critical path, decisions, acceptance checks;
  e edit goal, + add check, R run all checks on main
- checks.rs: acceptance checks on a branch or on main in a clean worktree
- runner: wait for --after dependencies, one session in merge at a time,
  all checks again on main after each merge
- orch goal / check / checks commands, ORCH_DB and {{repo}} for machine steps
- example machines merge with git merge --ff-only

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-14 12:38:46 +01:00

160 lines
5.6 KiB
Rust

//! Acceptance checks (ORCHESTRATOR.md section 2): shell commands, pass = exit 0. A goal is done when every
//! check is green on main, so results on main are what the goal view shows; a session can also run its own
//! checks on its branch before merging (`run-acceptance` step) without touching the recorded result.
use crate::db::{CheckRow, Db};
use crate::run::{git, tail};
use anyhow::Result;
use serde_json::json;
use std::path::Path;
use std::process::Command;
/// Pseudo session id for goal-level events (check results on main).
pub const GOAL: &str = "goal";
pub struct Outcome {
pub check: CheckRow,
pub pass: bool,
pub exit: Option<i32>,
pub output: String,
}
/// Runs the checks in `dir`. `owner` limits to one session's checks. `record` stores the result as the check's
/// status on main; it is only for runs that really are on main.
pub fn run(db: &Db, dir: &Path, owner: Option<&str>, record: bool) -> Result<Vec<Outcome>> {
let mut out = vec![];
for c in db.checks()? {
if owner.is_some() && c.owner.as_deref() != owner {
continue;
}
let o = Command::new("sh")
.args(["-c", &format!("({}) 2>&1", c.cmd)])
.current_dir(dir)
.output()?;
let (pass, exit) = (o.status.success(), o.status.code());
let output = tail(&o.stdout, 2000);
if record {
db.set_check_result(c.id, pass)?;
}
let session = owner.unwrap_or(GOAL);
db.emit(
session,
"check",
json!({
"name": c.name, "pass": pass, "exit": exit,
"where": if record { "main" } else { "branch" },
"output": if pass { String::new() } else { output.clone() },
}),
)?;
out.push(Outcome {
check: c,
pass,
exit,
output,
});
}
Ok(out)
}
/// All checks, recorded, in a clean detached worktree of the repo's HEAD (main after a merge). The
/// user's own checkout is never touched.
pub fn run_on_main(db: &Db, repo: &Path) -> Result<Vec<Outcome>> {
if db.checks()?.is_empty() {
return Ok(vec![]);
}
let tmp = repo.join(format!(".hx/main-check-{}", crate::state::now_ms()));
let lock_path = repo.join(".git/orch-worktree.lock");
{
let _l = crate::lock::lock(&lock_path)?; // git races on its own locks
git(
repo,
&[
"worktree",
"add",
"--detach",
&tmp.to_string_lossy(),
"HEAD",
],
)?;
}
let res = run(db, &tmp, None, true);
let _l = crate::lock::lock(&lock_path)?;
let _ = git(
repo,
&["worktree", "remove", "--force", &tmp.to_string_lossy()],
);
res
}
#[cfg(test)]
mod tests {
use super::*;
fn repo(tag: &str) -> (std::path::PathBuf, Db) {
let dir = std::env::temp_dir().join(format!("orch-checks-{tag}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
git(&dir, &["init", "-q", "-b", "main"]).unwrap();
std::fs::write(dir.join("present.txt"), "x").unwrap();
git(&dir, &["add", "-A"]).unwrap();
git(&dir, &["commit", "-qm", "init"]).unwrap();
let db = Db::open(&dir.join(".hx/orch.db")).unwrap();
(dir, db)
}
#[test]
fn main_run_records_in_a_clean_worktree_and_leaves_the_checkout_alone() {
let (dir, db) = repo("main");
db.add_check("file present", "test -f present.txt", None)
.unwrap();
db.add_check("untracked junk absent", "test ! -f junk.txt", Some("s1"))
.unwrap();
db.add_check("always red", "echo nope; exit 3", None)
.unwrap();
// junk in the user's checkout must not be visible to the checks: they run in a clean worktree
std::fs::write(dir.join("junk.txt"), "dirty").unwrap();
let r = run_on_main(&db, &dir).unwrap();
assert_eq!(
r.iter().map(|o| o.pass).collect::<Vec<_>>(),
[true, true, false]
);
assert_eq!(r[2].exit, Some(3));
let rows = db.checks().unwrap();
let st: Vec<_> = rows
.iter()
.map(|c| c.last_status.as_deref().unwrap())
.collect();
assert_eq!(st, ["pass", "pass", "fail"]);
assert!(rows[0].last_run_at.is_some());
assert!(dir.join("junk.txt").exists(), "checkout untouched");
let leftovers: Vec<_> = std::fs::read_dir(dir.join(".hx"))
.unwrap()
.flatten()
.filter(|e| e.file_name().to_string_lossy().starts_with("main-check"))
.collect();
assert!(leftovers.is_empty(), "temporary worktree removed");
let ev = db.events(Some(GOAL), 0).unwrap();
assert_eq!(ev.len(), 3);
assert_eq!(ev[2].payload["output"], "nope\n");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn branch_run_filters_by_owner_and_does_not_record() {
let (dir, db) = repo("branch");
db.add_check("mine", "true", Some("s1")).unwrap();
db.add_check("theirs", "false", Some("s2")).unwrap();
db.add_check("unowned", "false", None).unwrap();
let r = run(&db, &dir, Some("s1"), false).unwrap();
assert_eq!(r.len(), 1);
assert!(r[0].pass);
assert!(db.checks().unwrap().iter().all(|c| c.last_status.is_none()));
assert_eq!(
db.events(Some("s1"), 0).unwrap()[0].payload["where"],
"branch"
);
assert_eq!(db.events(Some(GOAL), 0).unwrap().len(), 0);
let _ = std::fs::remove_dir_all(&dir);
}
}