O3: task sources and result sinks, cmd adapter protocol (closes #4)
This commit is contained in:
1 parent
2f79b69977
commit
6e1979fe7a
10 files changed
+594
-76
No files matched your search
+83
-19
@@ -1,10 +1,11 @@
|
||||
mod adapter;
|
||||
mod profile;
|
||||
mod run;
|
||||
mod state;
|
||||
|
||||
use anyhow::{Result, bail};
|
||||
use anyhow::{Context, Result, bail};
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about = "Run hx coding agents on tasks")]
|
||||
@@ -23,6 +24,14 @@ enum Cmd {
|
||||
#[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.
|
||||
@@ -49,6 +58,10 @@ 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 {
|
||||
@@ -57,23 +70,32 @@ fn main() -> Result<std::process::ExitCode> {
|
||||
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
|
||||
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;
|
||||
}
|
||||
.into())
|
||||
Ok(code(ok))
|
||||
}
|
||||
Cmd::Status { id: Some(id) } => {
|
||||
println!("{}", serde_json::to_string_pretty(&state::Run::load(&id)?)?);
|
||||
@@ -84,7 +106,7 @@ fn main() -> Result<std::process::ExitCode> {
|
||||
let st = serde_json::to_value(r.status)?;
|
||||
let end = r.ended.unwrap_or_else(state::now);
|
||||
println!(
|
||||
"{:<28} {:<8} {:>5}s {:>8} tok {}{}",
|
||||
"{:<28} {:<11} {:>5}s {:>8} tok {}{}",
|
||||
r.id,
|
||||
st.as_str().unwrap(),
|
||||
end.saturating_sub(r.started),
|
||||
@@ -104,3 +126,45 @@ fn main() -> Result<std::process::ExitCode> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
Reference in new issue
Block a user