Files
Johannes KresnerandClaude Opus 5.5 20f88d88df
build / version (push) Successful in 1m20s
build / build (aarch64-apple-darwin) (push) Successful in 5m55s
build / build (x86_64-unknown-linux-musl) (push) Successful in 4m1s
build / release (push) Successful in 21s
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>
2026-11-16 15:56:58 +01:00

114 lines
4.3 KiB
Python

"""orch up: one manifest, worktrees in two repos, detached sessions in the issues database, rerun, kill."""
import json, os, pathlib, sqlite3, subprocess, sys, tempfile, time
root = pathlib.Path(__file__).resolve().parent.parent
orch = sys.argv[1] if len(sys.argv) > 1 else str(root / "target/debug/orch")
tmp = pathlib.Path(tempfile.mkdtemp())
cfg, state = tmp / "cfg", tmp / "state"
env = {**os.environ, "ORCH_CONFIG_DIR": str(cfg), "ORCH_STATE_DIR": str(state)}
QUICK = """name = "quick"
[[state]]
name = "work"
kind = "start"
steps = [{ name = "w", run = "computer", cmd = "cp .hx/task.md seen.txt" }]
next = "merge"
[[state]]
name = "merge"
kind = "exit"
steps = [{ name = "never", run = "computer", cmd = "touch merged-it" }]
end = "merged"
"""
SLOW = QUICK.replace('"quick"', '"slow"').replace("cp .hx/task.md seen.txt", "sleep 60")
def git(cwd, *a):
subprocess.run(["git", "-c", "user.name=t", "-c", "user.email=t@t", *a], cwd=cwd, check=True, capture_output=True)
def worktree(repo_name, machine):
repo = tmp / repo_name
repo.mkdir()
git(repo, "init", "-q", "-b", "main")
(repo / "f").write_text("x")
git(repo, "add", "-A")
git(repo, "commit", "-qm", "init")
wt = tmp / f"{repo_name}-abc"
git(repo, "worktree", "add", "-q", "-b", "abc-1", str(wt))
(wt / ".hx/machines").mkdir(parents=True)
(wt / ".hx/profiles").mkdir(parents=True)
(wt / ".hx/machines/abc.toml").write_text(machine)
(wt / ".hx/profiles/abc.toml").write_text("max_retries = 0\n")
(wt / ".hx/task.md").write_text(f"do the {repo_name} part\n")
return repo, wt
api, api_wt = worktree("api", QUICK)
web, web_wt = worktree("web", SLOW)
# the manifest lives where config.toml says, not in the default place
issues = tmp / "my-manifests"
issues.mkdir()
cfg.mkdir()
(cfg / "config.toml").write_text(f'manifest_dir = "{issues}"\n')
entry = lambda wt, name: f'[[session]]\nworktree = "{wt}"\nname = "{name}"\nmachine = "abc"\nprofile = "abc"\n'
(issues / "ABC-1.toml").write_text('issue = "ABC-1"\n' + entry(api_wt, "api part") + entry(web_wt, "web part"))
def up(name="ABC-1"):
return subprocess.run([orch, "up", name], env=env, capture_output=True, text=True, timeout=30)
def sessions():
c = sqlite3.connect(state / "issues.db")
rows = c.execute("select id, worktree, status, pid from sessions order by id").fetchall()
c.close()
return {r[0]: {"worktree": r[1], "status": r[2], "pid": r[3]} for r in rows}
def wait(sid, statuses, secs=20):
end = time.time() + secs
while time.time() < end:
s = sessions().get(sid)
if s and s["status"] in statuses:
return s
time.sleep(0.1)
raise AssertionError(f"{sid} never reached {statuses}: {sessions()} {(state / 'logs' / f'issue-{sid}.log').read_text()}")
# a broken entry starts nothing
(issues / "BAD.toml").write_text('issue = "BAD"\n' + entry(api_wt, "ok") + entry(tmp / "nope", "missing"))
p = up("BAD")
assert p.returncode != 0 and "missing" in p.stderr, p.stderr
assert not (state / "issues.db").exists() or not sessions(), "nothing may start when one entry is broken"
p = up()
assert p.returncode == 0, p.stderr
assert "s1 api part: started" in p.stdout and "s2 web part: started" in p.stdout, p.stdout
wait("s1", {"done"})
assert (api_wt / "seen.txt").read_text() == "do the api part\n", "the step ran in the user's worktree"
assert not (api_wt / "merged-it").exists(), "stops at done, no merge"
wait("s2", {"running"})
c = sqlite3.connect(state / "issues.db")
opts = [json.loads(r[0]) for r in c.execute("select payload from events where session_id = 's1' and kind = 'options'")]
merged = {k: v for o in opts for k, v in o.items()}
assert merged["issue"] == "ABC-1" and merged["task"] == "do the api part\n" and merged["no_merge"] is True, merged
assert merged["repo"] == str(api.resolve()), merged
sub = c.execute("select subtitle from tasks where id = 's2'").fetchone()[0]
assert sub == "web", sub
# running it again starts nothing twice
p = up()
assert "s1 api part: done already" in p.stdout and "s2 web part: running already" in p.stdout, p.stdout
assert len(sessions()) == 2
# kill, as the TUI's k does: an abort command through the database reaches the detached process
c.execute("insert into commands(session_id, kind, payload, at) values ('s2', 'abort', '{}', 0)")
c.commit()
s = wait("s2", {"aborted", "failed"})
c.close()
print("issues ok", s["status"])