feat: orch up starts issue sessions in existing worktrees
orch up MANIFEST reads an issue manifest (a path, or NAME in manifest_dir, default <config>/manifests, set in <config>/config.toml) and starts one detached session per listed worktree, across repos. Each worktree brings its own machine, profile and .hx/task.md; the task text now reaches the agent. Sessions live in <state>/issues.db; orch issues shows them grouped by issue. Also: liveness checks use kill(pid, 0) instead of /proc (broken on macOS), the same branch name in two repos no longer counts as the same place, and the merge queue column ticket is renamed to issue (migrated on open). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
440b19180e
commit
20f88d88df
16 files changed
+791
-65
No files matched your search
+49
-13
@@ -15,7 +15,7 @@ use serde_json::{Value, json};
|
||||
use std::cell::{Cell, RefCell};
|
||||
use std::io::Read;
|
||||
use std::os::unix::process::CommandExt;
|
||||
use std::path::Path;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
@@ -82,10 +82,12 @@ pub fn create_after(
|
||||
|
||||
/// Like [`create_after`], working in a branch or worktree the user named. With `no_merge` the session stops at
|
||||
/// `done` where its machine would run the state that ends as `merged`, and leaves the branch for a pull request.
|
||||
#[allow(clippy::too_many_arguments)] // shortcut: one caller each in the TUI form and `orch up`
|
||||
pub fn create_in(
|
||||
db: &Db,
|
||||
worktrees: &Path,
|
||||
name: &str,
|
||||
subtitle: &str,
|
||||
machine: &str,
|
||||
after: &[String],
|
||||
place: &crate::place::Place,
|
||||
@@ -93,7 +95,7 @@ pub fn create_in(
|
||||
) -> Result<Session> {
|
||||
use crate::place::Place;
|
||||
crate::place::ensure_free(db, place)?;
|
||||
let mut s = create_after(db, worktrees, name, "", machine, after)?;
|
||||
let mut s = create_after(db, worktrees, name, subtitle, machine, after)?;
|
||||
match place {
|
||||
Place::Fresh => {}
|
||||
Place::Branch(b) => s.branch = b.clone(),
|
||||
@@ -127,7 +129,7 @@ fn ensure_worktree(repo: &Path, s: &Session) -> Result<()> {
|
||||
std::fs::create_dir_all(wt.parent().context("worktree has no parent")?)?;
|
||||
// git races on its own locks when worktrees are added in parallel
|
||||
let _l = crate::lock::lock(&repo.join(".git/orch-worktree.lock"))?;
|
||||
// a branch the user named may exist already (a ticket branch): check it out instead of creating it
|
||||
// a branch the user named may exist already (an issue branch): check it out instead of creating it
|
||||
let exists = git(
|
||||
repo,
|
||||
&[
|
||||
@@ -386,6 +388,8 @@ struct Ctx<'a> {
|
||||
repo: &'a Path,
|
||||
ctl: Ctl<'a>,
|
||||
agent: Option<Acp>,
|
||||
/// The task text (`orch up` reads it from the worktree's task file); empty for other sessions.
|
||||
body: String,
|
||||
}
|
||||
|
||||
enum Outcome {
|
||||
@@ -676,7 +680,7 @@ fn run_command(cx: &Ctx, st: &State, step: &Step) -> Result<StepRes> {
|
||||
}
|
||||
|
||||
/// Instruction text for a prompt kind. The kind itself is sent too, so hx can use its own prompts.
|
||||
fn instruction(kind: &str, goal: Option<&str>, t: &Task) -> String {
|
||||
fn instruction(kind: &str, goal: Option<&str>, t: &Task, body: &str) -> String {
|
||||
let lead = match kind {
|
||||
"plan" => "Plan the work for this task. Report the plan with hx/plan.",
|
||||
"implement" => "Implement this task, following the plan.",
|
||||
@@ -689,6 +693,9 @@ fn instruction(kind: &str, goal: Option<&str>, t: &Task) -> String {
|
||||
if let Some(g) = goal {
|
||||
s.push_str(&format!("\nGoal: {g}"));
|
||||
}
|
||||
if !body.trim().is_empty() {
|
||||
s.push_str(&format!("\n\n{}", body.trim()));
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
@@ -730,7 +737,7 @@ fn run_agent(cx: &mut Ctx, st: &State, step: &Step) -> Result<StepRes> {
|
||||
let agent = cx.agent.as_mut().expect("spawned above");
|
||||
let r: StepResult = agent.step(
|
||||
kind,
|
||||
&instruction(kind, goal.as_deref(), cx.task),
|
||||
&instruction(kind, goal.as_deref(), cx.task, &cx.body),
|
||||
context,
|
||||
json!({}),
|
||||
&mut sink,
|
||||
@@ -887,6 +894,34 @@ impl Sink for DbSink<'_> {
|
||||
}
|
||||
}
|
||||
|
||||
/// What session `id` runs with: its machine, repo and profile. A issue session (`orch up`) carries its own repo
|
||||
/// and profile file in its options; any other session uses `repo` and `profile`.
|
||||
pub fn launch(
|
||||
db: &Db,
|
||||
id: &str,
|
||||
repo: &Path,
|
||||
profile: &Profile,
|
||||
) -> Result<(Machine, PathBuf, Profile)> {
|
||||
let s = db
|
||||
.session(id)?
|
||||
.with_context(|| format!("no session {id}"))?;
|
||||
let task = db
|
||||
.tasks()?
|
||||
.into_iter()
|
||||
.find(|t| t.id == s.task_id)
|
||||
.with_context(|| format!("no task {}", s.task_id))?;
|
||||
let o = db.options(id)?;
|
||||
let repo = o
|
||||
.get("repo")
|
||||
.and_then(Value::as_str)
|
||||
.map_or_else(|| repo.to_path_buf(), PathBuf::from);
|
||||
let profile = match o.get("profile").and_then(Value::as_str) {
|
||||
Some(p) => Profile::load_path(Path::new(p))?,
|
||||
None => profile.clone(),
|
||||
};
|
||||
Ok((Machine::find(&task.machine, &repo)?, repo, profile))
|
||||
}
|
||||
|
||||
/// Runs the session to the end of its machine and returns the final status (`merged`, `escalated`, `failed`, ...).
|
||||
/// Resumes at `current_state` when the session was started before (orchestrator restart).
|
||||
pub fn run_session(
|
||||
@@ -929,6 +964,12 @@ pub fn run_session(
|
||||
repo,
|
||||
ctl,
|
||||
agent: None,
|
||||
body: db
|
||||
.options(id)?
|
||||
.get("task")
|
||||
.and_then(Value::as_str)
|
||||
.unwrap_or_default()
|
||||
.into(),
|
||||
};
|
||||
let mut resumed = !s.state.is_empty();
|
||||
let (mut cur, mut from) = if resumed {
|
||||
@@ -938,10 +979,7 @@ pub fn run_session(
|
||||
(m.start().name.clone(), String::new())
|
||||
};
|
||||
db.set_pid(id, Some(std::process::id()))?;
|
||||
let no_merge = db
|
||||
.events(Some(id), 0)?
|
||||
.iter()
|
||||
.any(|e| e.kind == "options" && e.payload["no_merge"] == true);
|
||||
let no_merge = db.options(id)?.get("no_merge") == Some(&json!(true));
|
||||
for _ in 0..MAX_TRANSITIONS {
|
||||
let st = m
|
||||
.state(&cur)
|
||||
@@ -1074,9 +1112,7 @@ fn alive(db: &Db, session: &str) -> bool {
|
||||
let Some(pid) = db.session(session).ok().flatten().and_then(|s| s.pid) else {
|
||||
return false;
|
||||
};
|
||||
// SAFETY: signal 0 only checks that the process exists.
|
||||
let r = unsafe { libc::kill(pid as i32, 0) };
|
||||
r == 0 || std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
|
||||
crate::state::pid_alive(pid)
|
||||
}
|
||||
|
||||
/// Joins the merge queue and blocks until this session is at the head.
|
||||
@@ -1795,7 +1831,7 @@ end = "merged"
|
||||
let e = env("stale");
|
||||
let m = one_step(r#"{ name = "w", run = "computer", cmd = "true" }"#);
|
||||
create(&e.db, &e.dir.join("wt"), "t", "", "m").unwrap();
|
||||
// a crashed orchestrator left its ticket at the head; its pid is gone
|
||||
// a crashed orchestrator left its issue at the head; its pid is gone
|
||||
e.db.add_session(&Session {
|
||||
id: "s9".into(),
|
||||
task_id: "s1".into(),
|
||||
|
||||
Reference in new issue
Block a user