Merge branch 'feat/tui-implementation'

This commit is contained in:
johannes committed 2026-11-14 14:08:40 +01:00
commit f1ab948bb4
29 files changed
+11321 -8

No files matched your search

+181 -3
View File
@@ -1,10 +1,21 @@
mod acp;
mod adapter;
mod budget;
mod checks;
mod db;
mod detail;
mod goal;
mod junit;
mod lock;
mod machine;
mod policy;
mod profile;
mod report;
mod run;
mod runner;
mod state;
mod tui;
mod view;
use anyhow::{Context, Result, bail};
use clap::{Parser, Subcommand};
@@ -14,10 +25,13 @@ use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
#[derive(Parser)]
#[command(version, about = "Run hx coding agents on tasks")]
#[command(
version,
about = "Run hx coding agents on tasks; without a subcommand, opens the TUI"
)]
struct Cli {
#[command(subcommand)]
cmd: Cmd,
cmd: Option<Cmd>,
}
#[derive(Subcommand)]
@@ -62,6 +76,59 @@ enum Cmd {
#[arg(long)]
check: bool,
},
/// Show or set the goal sentence; `--export` also prints the acceptance checks (for machine steps).
Goal {
/// New goal text.
text: Option<String>,
#[arg(long)]
export: bool,
/// Repo with the goal (default: ORCH_DB of a machine step, else the current directory).
#[arg(long)]
repo: Option<PathBuf>,
},
/// Add an acceptance check: a shell command, pass = exit 0.
Check {
name: String,
cmd: String,
/// Session that is responsible for it (its `run-acceptance` step runs it before merging).
#[arg(long)]
owner: Option<String>,
#[arg(long)]
repo: Option<PathBuf>,
},
/// Run acceptance checks in the current directory (a worktree), or with --main in a clean copy of main.
Checks {
/// Only the checks this session owns.
#[arg(long)]
owner: Option<String>,
/// All checks on main; the results are recorded for the goal view.
#[arg(long)]
main: bool,
#[arg(long)]
repo: Option<PathBuf>,
},
/// Create a state-machine session in REPO and run it to the end. Waits for `--after` sessions to merge.
Session {
/// Sessions that must be merged before this one starts, comma separated.
#[arg(long, value_delimiter = ',')]
after: Vec<String>,
#[arg(long)]
repo: PathBuf,
/// Machine file name, without .toml (looked up in REPO/.hx/machines, then ~/.hx/machines).
#[arg(long, required_unless_present = "resume")]
machine: Option<String>,
#[arg(long, required_unless_present = "resume")]
name: Option<String>,
/// Continue this unfinished session (e.g. s1) from its saved state instead of creating one.
#[arg(long, conflicts_with_all = ["machine", "name", "after"])]
resume: Option<String>,
/// Value for {{scope}} in machine commands.
#[arg(long, default_value = "")]
scope: String,
/// Profile that says how to start hx for agent steps.
#[arg(long, default_value = "default")]
profile: String,
},
/// hx extension that enforces a run's tool allowlist (started by hx, not by hand).
#[command(hide = true)]
Guard { policy: PathBuf },
@@ -93,12 +160,123 @@ pub fn state_dir() -> PathBuf {
env_dir("ORCH_STATE_DIR", "XDG_STATE_HOME", ".local/state")
}
/// The goal database: `--repo`, else `ORCH_DB` (set by the runner for machine steps), else `./.hx/orch.db`.
fn open_db(repo: Option<&Path>) -> Result<db::Db> {
if repo.is_none()
&& let Some(p) = std::env::var_os("ORCH_DB")
{
return db::Db::open(Path::new(&p));
}
let dir = match repo {
Some(r) => r.to_path_buf(),
None => std::env::current_dir()?,
};
db::Db::open(&dir.join(".hx/orch.db"))
}
fn code(ok: bool) -> std::process::ExitCode {
u8::from(!ok).into()
}
fn main() -> Result<std::process::ExitCode> {
match Cli::parse().cmd {
let Some(cmd) = Cli::parse().cmd else {
tui::run()?;
return Ok(code(true));
};
match cmd {
Cmd::Goal { text, export, repo } => {
let db = open_db(repo.as_deref())?;
if let Some(t) = text {
db.set_goal(&t)?;
}
println!("{}", db.goal()?.unwrap_or_else(|| "no goal set".into()));
if export {
println!("\nAcceptance checks (all must pass on main):");
for c in db.checks()? {
let owner = c.owner.map_or(String::new(), |o| format!(" [{o}]"));
println!("- {}{owner}: {}", c.name, c.cmd);
}
}
Ok(code(true))
}
Cmd::Check {
name,
cmd,
owner,
repo,
} => {
let id = open_db(repo.as_deref())?.add_check(&name, &cmd, owner.as_deref())?;
println!("check {id}: {name}");
Ok(code(true))
}
Cmd::Checks { owner, main, repo } => {
let db = open_db(repo.as_deref())?;
let results = if main {
let repo = match (repo, std::env::var_os("ORCH_REPO")) {
(Some(r), _) => r,
(None, Some(r)) => r.into(),
(None, None) => std::env::current_dir()?,
};
checks::run_on_main(&db, &repo)?
} else {
checks::run(&db, &std::env::current_dir()?, owner.as_deref(), false)?
};
for r in &results {
if r.pass {
println!("✓ {}", r.check.name);
} else {
println!("✗ {} (exit {})", r.check.name, r.exit.unwrap_or(-1));
print!("{}", r.output);
}
}
Ok(code(results.iter().all(|r| r.pass)))
}
Cmd::Session {
after,
repo,
machine,
name,
resume,
scope,
profile,
} => {
let profile = profile::Profile::load(&profile)?;
let repo = repo.canonicalize()?;
let db = db::Db::open(&repo.join(".hx/orch.db"))?;
let s = if let Some(id) = resume {
let s = db
.session(&id)?
.with_context(|| format!("no session {id}"))?;
anyhow::ensure!(
!db.events(Some(&id), 0)?.iter().any(|e| e.kind == "end"),
"session {id} already ended; start a new one"
);
anyhow::ensure!(
s.pid
.is_none_or(|p| !Path::new(&format!("/proc/{p}")).exists()
|| p == std::process::id()),
"session {id} is still running (pid {})",
s.pid.unwrap_or(0)
);
s
} else {
// shortcut: worktrees are grouped by repo directory name; two repos with one name would share ids
let base = repo.file_name().unwrap_or_default();
let wts = state_dir().join("worktrees").join(base);
let (machine, name) = (machine.unwrap_or_default(), name.unwrap_or_default());
machine::Machine::find(&machine, &repo)?;
runner::create_after(&db, &wts, &name, "", &machine, &after)?
};
let task = db
.tasks()?
.into_iter()
.find(|t| t.id == s.task_id)
.with_context(|| format!("no task {}", s.task_id))?;
let m = machine::Machine::find(&task.machine, &repo)?;
let status = runner::run_session(&db, &m, &s.id, &repo, &scope, &profile)?;
println!("{} {} -> {status}", s.id, s.branch);
Ok(code(status == "merged"))
}
Cmd::Run {
task,
repo,