TUI hosts sessions: new-session form, named worktrees, resume, faster permissions

- Use the terminal's own colors and background; the key bar and the Needs-you
  panel wrap, fixed-width panels shrink on narrow terminals.
- Sessions run on threads of the TUI (tui/host.rs), capped by the profile's
  `workers`; quitting asks for `yes` and suspends them. New `suspend` command
  keeps the state so a session can be resumed.
- `N` opens a new-session form: machine, name, --after, a named branch or
  worktree (place.rs; never deleted by orch), and a "keep for a PR" option.
- `u` resumes suspended sessions and ones whose process died.
- `A` allows identical permission requests across sessions, `P` saves an allow
  rule to the profile with toml_edit (comments kept).
- Fix clippy lint in report.rs so tests/all.sh passes; add GLOSSARY.md.

Includes the already staged `orch init` work (init.rs, example profile).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
johannesandClaude Sonnet 5.5 committed 2026-11-14 18:04:41 +01:00
1 parent 5a80bc9fe1
commit cfb735cdb3
22 files changed
+1623 -115

No files matched your search

+159 -3
View File
@@ -80,6 +80,37 @@ pub fn create_after(
Ok(s)
}
/// 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.
pub fn create_in(
db: &Db,
worktrees: &Path,
name: &str,
machine: &str,
after: &[String],
place: &crate::place::Place,
no_merge: bool,
) -> Result<Session> {
use crate::place::Place;
crate::place::ensure_free(db, place)?;
let mut s = create_after(db, worktrees, name, "", machine, after)?;
match place {
Place::Fresh => {}
Place::Branch(b) => s.branch = b.clone(),
Place::Worktree { path, branch } => {
s.branch = branch.clone();
s.worktree = path.to_string_lossy().into_owned();
}
}
if *place != Place::Fresh {
db.set_location(&s.id, &s.branch, &s.worktree)?;
}
if no_merge {
db.emit(&s.id, "options", json!({"no_merge": true}))?;
}
Ok(s)
}
fn render(cmd: &str, s: &Session, scope: &str, repo: &Path) -> String {
cmd.replace("{{session}}", &s.id)
.replace("{{scope}}", scope)
@@ -96,10 +127,25 @@ 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"))?;
git(
// a branch the user named may exist already (a ticket branch): check it out instead of creating it
let exists = git(
repo,
&["worktree", "add", "-b", &s.branch, &s.worktree, "HEAD"],
)?;
&[
"rev-parse",
"--verify",
"--quiet",
&format!("refs/heads/{}", s.branch),
],
)
.is_ok();
if exists {
git(repo, &["worktree", "add", &s.worktree, &s.branch])?;
} else {
git(
repo,
&["worktree", "add", "-b", &s.branch, &s.worktree, "HEAD"],
)?;
}
Ok(())
}
@@ -108,6 +154,8 @@ fn ensure_worktree(repo: &Path, s: &Session) -> Result<()> {
enum Stop {
/// Abandon the current step and continue in this state.
Goto { state: String, why: String },
/// Stop working but keep the state, so `--resume` continues here (the TUI host is closing).
Suspend,
/// End the session.
End {
status: &'static str,
@@ -244,6 +292,10 @@ impl<'a> Ctl<'a> {
"goto" => {
fail(format!("no state {target:?}"))?;
}
"suspend" => {
self.set_stop(Stop::Suspend);
p.interrupt = true;
}
"abort" => {
self.set_stop(match &self.on_abort {
Some(s) => Stop::Goto {
@@ -858,6 +910,9 @@ pub fn run_session(
match wait_for_dependencies(&ctl, &s, &task) {
Ok(()) => {}
Err(e) if e.is::<Stopped>() => {
if matches!(ctl.take_stop(), Some(Stop::Suspend)) {
return suspend(db, id, "");
}
let why = "stopped while blocked on its dependencies".to_string();
return finish(db, id, "", "aborted", Some(why));
}
@@ -883,6 +938,10 @@ 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);
for _ in 0..MAX_TRANSITIONS {
let st = m
.state(&cur)
@@ -892,6 +951,15 @@ pub fn run_session(
db.emit(id, "state", json!({"from": from, "to": cur}))?;
}
resumed = false;
if no_merge && st.end.as_deref() == Some("merged") {
return finish(
db,
id,
&cur,
"done",
Some("not merged: kept for a pull request".into()),
);
}
db.set_session(id, &cur, "running")?;
cx.ctl.enter_state(&cur);
// the state that ends the session as `merged` is the merge: one session at a time, first come first served
@@ -922,6 +990,7 @@ pub fn run_session(
};
from = cur.clone();
match outcome {
Outcome::Stop(Stop::Suspend) => return suspend(db, id, &cur),
Outcome::Stop(Stop::End { status, reason }) => {
return finish(db, id, &cur, status, Some(reason));
}
@@ -1035,6 +1104,14 @@ fn enter_merge_queue(ctl: &Ctl, id: &str) -> Result<()> {
}
}
/// Leaves the session unfinished in `state`: no `end` event, no pid, so `--resume` picks it up.
fn suspend(db: &Db, id: &str, state: &str) -> Result<String> {
db.set_session(id, state, "suspended")?;
db.set_pid(id, None)?;
db.emit(id, "suspend", json!({"state": state}))?;
Ok("suspended".into())
}
fn finish(db: &Db, id: &str, state: &str, status: &str, reason: Option<String>) -> Result<String> {
db.set_session(id, state, status)?;
db.set_pid(id, None)?;
@@ -1275,6 +1352,43 @@ next = "done"
})
}
#[test]
fn suspending_a_session_that_waits_on_a_need_closes_the_need_and_resume_asks_again() {
let e = env("suspend-need");
let m = Machine::parse(&format!(
r#"name = "m"
[[state]]
name = "review"
kind = "start"
steps = [{{ name = "wait-approval", run = "human", sets = "waiting" }}]
next = "done"
{TAIL}"#
))
.unwrap();
create(&e.db, &e.dir.join("wt"), "t", "", "m").unwrap();
let path = e.dir.join("orch.db");
let user = std::thread::spawn(move || {
let db = Db::open(&path).unwrap();
assert!(wait_until(10, || !db.pending_needs().unwrap().is_empty()));
db.add_command("s1", "suspend", &json!({})).unwrap();
assert!(wait_until(10, || db.pending_needs().unwrap().is_empty()));
});
assert_eq!(
run_session(&e.db, &m, "s1", &e.repo, "", &plain()).unwrap(),
"suspended"
);
user.join().unwrap();
assert!(e.db.pending_needs().unwrap().is_empty(), "no stale item");
// resumed: the review is asked again, and an answer finishes the session
let user = answerer(e.dir.join("orch.db"), &["approve"]);
assert_eq!(
run_session(&e.db, &m, "s1", &e.repo, "", &plain()).unwrap(),
"merged"
);
assert_eq!(user.join().unwrap().len(), 1);
let _ = std::fs::remove_dir_all(&e.dir);
}
#[test]
fn human_review_waits_in_the_queue_and_changes_requested_loops_back() {
let e = env("human");
@@ -1806,6 +1920,48 @@ next = "done"
let _ = std::fs::remove_dir_all(&e.dir);
}
#[test]
fn suspend_keeps_the_state_and_resume_continues_it() {
let e = env("suspend");
let m = ctl_machine(ABORTABLE);
create(&e.db, &e.dir.join("wt"), "t", "", "m").unwrap();
let u = user(
&e,
|ev| has(ev, "state"),
|db| {
db.add_command("s1", "suspend", &json!({})).unwrap();
},
);
assert_eq!(
run_session(&e.db, &m, "s1", &e.repo, "", &plain()).unwrap(),
"suspended"
);
u.join().unwrap();
let s = e.db.session("s1").unwrap().unwrap();
assert_eq!(
(s.status.as_str(), s.state.as_str(), s.pid),
("suspended", "a", None)
);
assert!(
!kinds(&e.db, "s1").contains(&"end".to_string()),
"not ended, so it can be resumed"
);
// resumed in state a; the sleep is cut short by a second suspend
let u = user(
&e,
|ev| has(ev, "resume"),
|db| {
db.add_command("s1", "suspend", &json!({})).unwrap();
},
);
assert_eq!(
run_session(&e.db, &m, "s1", &e.repo, "", &plain()).unwrap(),
"suspended"
);
u.join().unwrap();
let _ = std::fs::remove_dir_all(&e.dir);
}
#[test]
fn force_state_jumps_and_an_unknown_state_is_refused() {
let e = env("force");