From d4c6e9133b228624ad42992cfc8f73f59f34f3fe Mon Sep 17 00:00:00 2001 From: Johannes Kresner Date: Sat, 14 Nov 2026 14:07:49 +0100 Subject: [PATCH] Add orch session --resume to continue an unfinished session Co-Authored-By: Claude Sonnet 5.5 --- src/main.rs | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8e44e96..9b89ce5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -115,10 +115,13 @@ enum Cmd { #[arg(long)] repo: PathBuf, /// Machine file name, without .toml (looked up in REPO/.hx/machines, then ~/.hx/machines). - #[arg(long)] - machine: String, - #[arg(long)] - name: String, + #[arg(long, required_unless_present = "resume")] + machine: Option, + #[arg(long, required_unless_present = "resume")] + name: Option, + /// 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, /// Value for {{scope}} in machine commands. #[arg(long, default_value = "")] scope: String, @@ -233,17 +236,43 @@ fn main() -> Result { repo, machine, name, + resume, scope, profile, } => { let profile = profile::Profile::load(&profile)?; let repo = repo.canonicalize()?; - let m = machine::Machine::find(&machine, &repo)?; let db = db::Db::open(&repo.join(".hx/orch.db"))?; - // 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 s = runner::create_after(&db, &wts, &name, "", &machine, &after)?; + 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"))