O1: Rust crate, profiles, persistent run state (#9)

This commit was merged in pull request #9.
This commit is contained in:
hermes committed 2026-11-12 23:19:05 +00:00
1 parent 29c08130c2
commit fb434c670c
9 files changed
+1128

No files matched your search

+106
View File
@@ -0,0 +1,106 @@
mod profile;
mod run;
mod state;
use anyhow::{Result, bail};
use clap::{Parser, Subcommand};
use std::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,
},
/// 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 main() -> Result<std::process::ExitCode> {
match Cli::parse().cmd {
Cmd::Run {
task,
repo,
profile,
} => {
let p = profile::Profile::load(&profile)?;
let mut r = run::new_run(&task, &repo, &profile)?;
run::execute(&mut r, &p, &task)?;
println!(
"{} {}{}",
r.id,
serde_json::to_value(r.status)?.as_str().unwrap(),
r.reason
.as_ref()
.map(|x| format!(" ({x})"))
.unwrap_or_default()
);
Ok(if r.status == state::Status::Done {
0
} else {
1
}
.into())
}
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} {:<8} {:>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())
}
}
}