171 lines
5.2 KiB
Rust
171 lines
5.2 KiB
Rust
mod adapter;
|
|
mod profile;
|
|
mod run;
|
|
mod state;
|
|
|
|
use anyhow::{Context, Result, bail};
|
|
use clap::{Parser, Subcommand};
|
|
use std::path::{Path, PathBuf};
|
|
|
|
#[derive(Parser)]
|
|
#[command(version, about = "Run hx coding agents on tasks")]
|
|
struct Cli {
|
|
#[command(subcommand)]
|
|
cmd: Cmd,
|
|
}
|
|
|
|
#[derive(Subcommand)]
|
|
enum Cmd {
|
|
/// Run one task (Markdown file) in a fresh git worktree of REPO.
|
|
Run {
|
|
task: PathBuf,
|
|
#[arg(long)]
|
|
repo: PathBuf,
|
|
#[arg(long, default_value = "default")]
|
|
profile: String,
|
|
},
|
|
/// Run every task the profile's source lists, one after another (parallel pool: O4).
|
|
Pull {
|
|
#[arg(long, default_value = "default")]
|
|
profile: String,
|
|
/// Stop after N claimed tasks.
|
|
#[arg(long)]
|
|
limit: Option<usize>,
|
|
},
|
|
/// List runs, or show one.
|
|
Status { id: Option<String> },
|
|
/// Print the raw log (hx events, stderr, gate output) of a run.
|
|
Logs { id: String },
|
|
}
|
|
|
|
fn env_dir(var: &str, xdg: &str, fallback: &str) -> PathBuf {
|
|
if let Some(d) = std::env::var_os(var) {
|
|
return d.into();
|
|
}
|
|
if let Some(d) = std::env::var_os(xdg) {
|
|
return PathBuf::from(d).join("orch");
|
|
}
|
|
PathBuf::from(std::env::var_os("HOME").unwrap_or_default())
|
|
.join(fallback)
|
|
.join("orch")
|
|
}
|
|
|
|
pub fn config_dir() -> PathBuf {
|
|
env_dir("ORCH_CONFIG_DIR", "XDG_CONFIG_HOME", ".config")
|
|
}
|
|
|
|
pub fn state_dir() -> PathBuf {
|
|
env_dir("ORCH_STATE_DIR", "XDG_STATE_HOME", ".local/state")
|
|
}
|
|
|
|
fn code(ok: bool) -> std::process::ExitCode {
|
|
u8::from(!ok).into()
|
|
}
|
|
|
|
fn main() -> Result<std::process::ExitCode> {
|
|
match Cli::parse().cmd {
|
|
Cmd::Run {
|
|
task,
|
|
repo,
|
|
profile,
|
|
} => {
|
|
let p = profile::Profile::load(&profile)?;
|
|
let id = task.file_stem().and_then(|s| s.to_str()).unwrap_or("task");
|
|
let t = adapter::parse_task_md(id, &std::fs::read_to_string(&task)?);
|
|
let r = process(&t, &repo, &profile, &p, None)?;
|
|
Ok(code(r.status == state::Status::Done))
|
|
}
|
|
Cmd::Pull { profile, limit } => {
|
|
let p = profile::Profile::load(&profile)?;
|
|
let src = adapter::source(p.source.as_ref().context("profile has no [source]")?)?;
|
|
let (mut ok, mut n) = (true, 0);
|
|
for t in src.list()? {
|
|
if limit.is_some_and(|l| n >= l) {
|
|
break;
|
|
}
|
|
let Some(repo) = t.repo.clone().or_else(|| p.repo.clone()) else {
|
|
eprintln!("{}: no repo (task `repo` or profile `repo`), skipped", t.id);
|
|
ok = false;
|
|
continue;
|
|
};
|
|
if !src.claim(&t)? {
|
|
continue;
|
|
}
|
|
n += 1;
|
|
let r = process(&t, &repo, &profile, &p, Some(src.as_ref()))?;
|
|
ok &= r.status == state::Status::Done;
|
|
}
|
|
Ok(code(ok))
|
|
}
|
|
Cmd::Status { id: Some(id) } => {
|
|
println!("{}", serde_json::to_string_pretty(&state::Run::load(&id)?)?);
|
|
Ok(0.into())
|
|
}
|
|
Cmd::Status { id: None } => {
|
|
for r in state::Run::list()? {
|
|
let st = serde_json::to_value(r.status)?;
|
|
let end = r.ended.unwrap_or_else(state::now);
|
|
println!(
|
|
"{:<28} {:<11} {:>5}s {:>8} tok {}{}",
|
|
r.id,
|
|
st.as_str().unwrap(),
|
|
end.saturating_sub(r.started),
|
|
r.input_tokens + r.output_tokens,
|
|
r.title,
|
|
r.reason.map(|x| format!(" [{x}]")).unwrap_or_default()
|
|
);
|
|
}
|
|
Ok(0.into())
|
|
}
|
|
Cmd::Logs { id } => {
|
|
if id.contains('/') || id.starts_with('.') {
|
|
bail!("bad run id");
|
|
}
|
|
print!("{}", std::fs::read_to_string(state::log_path(&id))?);
|
|
Ok(0.into())
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One task through the pipeline: run, publish via the sink when done, report to the source.
|
|
/// A failing adapter never loses the run: the error is printed and kept in `published`.
|
|
fn process(
|
|
t: &adapter::Task,
|
|
repo: &Path,
|
|
profile: &str,
|
|
p: &profile::Profile,
|
|
src: Option<&dyn adapter::Source>,
|
|
) -> Result<state::Run> {
|
|
let mut r = run::new_run(t, repo, profile)?;
|
|
run::execute(&mut r, p, t)?;
|
|
if r.status == state::Status::Done {
|
|
match adapter::sink(p.sink.as_ref()).and_then(|s| s.publish(t, &r)) {
|
|
Ok(note) => r.published = note,
|
|
Err(e) => {
|
|
eprintln!("{}: publish failed: {e:#}", r.id);
|
|
r.published = Some(format!("publish-failed: {e:#}"));
|
|
}
|
|
}
|
|
r.save()?;
|
|
}
|
|
if let Some(src) = src
|
|
&& let Err(e) = src.report(t, &r)
|
|
{
|
|
eprintln!("{}: report failed: {e:#}", r.id);
|
|
}
|
|
println!(
|
|
"{} {}{}{}",
|
|
r.id,
|
|
serde_json::to_value(r.status)?.as_str().unwrap(),
|
|
r.reason
|
|
.as_ref()
|
|
.map(|x| format!(" ({x})"))
|
|
.unwrap_or_default(),
|
|
r.published
|
|
.as_ref()
|
|
.map(|x| format!(" -> {x}"))
|
|
.unwrap_or_default()
|
|
);
|
|
Ok(r)
|
|
}
|