Skip to content

Commit dca4c79

Browse files
committed
fix: reuse saved worktree after branch recovery
1 parent 2fd1049 commit dca4c79

5 files changed

Lines changed: 52 additions & 7 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ include the full version, for example `## 0.7.0-beta.1`.
88

99
## Unreleased
1010

11+
### Fixed
12+
13+
- Reuse the saved worktree path when resuming recovered tasks or processing PR
14+
feedback, including after a branch rename. Preserve existing changes and the
15+
pinned base while validating the managed directory, Git root, branch, and
16+
repository before preparation, verification, and publication. A missing saved
17+
worktree blocks recovery instead of creating a replacement.
18+
1119
## 0.6.4
1220

1321
### Fixed
@@ -64,4 +72,4 @@ include the full version, for example `## 0.7.0-beta.1`.
6472
- Add detailed bot workflow diagrams and a documentation map in `AGENTS.md`.
6573
- Expand bundled bot instructions for planning, delegation, verification, and
6674
handing publication back to the dispatcher.
67-
- Add the README banner showing an OpenCode2 agent executing a task.
75+
- Add the README banner showing an OpenCode2 agent executing a task.

‎docs/bot-workflow.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ can reconcile a previously lost merge response.
366366

367367
Follow-up rounds reset analysis, question, current session, checks, and commit;
368368
they retain the branch, worktree, pinned base, and previous session reference.
369+
Preparation reuses the saved worktree path rather than deriving a new path from
370+
the branch name. A renamed branch can therefore retain its original directory.
371+
Preparation, verification, and push all check the managed path, exact Git root,
372+
branch, and shared repository. A missing checkpoint directory blocks the task
373+
without creating a replacement worktree.
369374
They create a new main session, whereas an implementation-question reply resumes
370375
the current one. Comments received while working stay queued for a later round.
371376
Feedback after closure can still be queued, but the next round's guards block it.

‎docs/runtime.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,12 @@ acknowledgement, implementation, and a push to the same open PR. The mention doe
180180
not need to be repeated. Comments received during execution wait for the next
181181
round. A mention in an authorized comment can also start work on an untracked issue.
182182

183+
Follow-up rounds reuse the worktree path saved in the queue, even if recovery
184+
renamed its branch. Preparation, verification, and push validate that path as a
185+
worktree root directly inside the managed worktree directory, attached to the
186+
expected branch and repository. If the saved directory is missing, restore it
187+
before retrying; the bot does not create a replacement or discard existing work.
188+
183189
Edits to existing comments and PR review comments are not supported. Closing the
184190
issue or closing/merging the PR blocks further rounds.
185191

‎src/executor.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { Plugin } from "@opencode/plugin";
22
import { execFile } from "node:child_process";
33
import { mkdir, realpath, stat } from "node:fs/promises";
4-
import { join, resolve } from "node:path";
4+
import { dirname, join, resolve } from "node:path";
55
import { randomUUID, createHash } from "node:crypto";
66
import type { GithubOptions, Repository } from "./config.js";
77
import { botPrompt } from "./prompt.js";
@@ -39,14 +39,17 @@ export class GitWorkspace {
3939
}
4040
async prepare(task: Task, repo: Repository) {
4141
await this.validate(repo);
42-
const directory = join(this.stateDirectory, "worktrees", task.branch.replaceAll("/", "-"));
42+
// A recovered task can have a renamed branch while retaining its original
43+
// worktree. The checkpoint, not the current branch spelling, owns its path.
44+
const directory = task.worktree ?? join(this.stateDirectory, "worktrees", task.branch.replaceAll("/", "-"));
4345
await mkdir(join(this.stateDirectory, "worktrees"), { recursive: true });
4446
// Existing worktrees are reused only after checking their exact branch and shared repository.
4547
if (await stat(directory).then(() => true, e => { if (e.code === "ENOENT") return false; throw e; })) {
4648
await this.assertWorktree(directory, task, repo);
4749
const baseSha = task.baseSha ?? await this.git(directory, "merge-base", "HEAD", `refs/remotes/origin/${repo.baseBranch}`);
4850
return { worktree: await realpath(directory), baseSha };
4951
}
52+
if (task.worktree) throw new Blocked("Saved task worktree is missing; restore it before retrying");
5053
try { await this.git(repo.directory, "fetch", "origin", `refs/heads/${repo.baseBranch}:refs/remotes/origin/${repo.baseBranch}`); }
5154
catch (cause) { throw new Error(`Could not fetch base branch ${repo.baseBranch} from origin; check that it exists and Git authentication works`, { cause }); }
5255
const baseSha = await this.git(repo.directory, "rev-parse", `refs/remotes/origin/${repo.baseBranch}`);
@@ -62,8 +65,11 @@ export class GitWorkspace {
6265
return result.split(/\r?\n/).some(line => line.split(/\s+/)[1] === ref);
6366
}
6467
private async assertWorktree(directory: string, task: Task, repo: Repository) {
65-
const expected = join(await realpath(this.stateDirectory), "worktrees", task.branch.replaceAll("/", "-"));
66-
if (await realpath(directory) !== resolve(expected)) throw new Blocked("Unexpected worktree path");
68+
const managed = join(await realpath(this.stateDirectory), "worktrees");
69+
const expected = task.worktree ? resolve(task.worktree) : join(managed, task.branch.replaceAll("/", "-"));
70+
const actual = await realpath(directory);
71+
if (actual !== expected || dirname(actual) !== managed) throw new Blocked("Unexpected worktree path");
72+
if (await realpath(await this.git(directory, "rev-parse", "--show-toplevel")) !== actual) throw new Blocked("Task directory is not the worktree root");
6773
if (await this.git(directory, "branch", "--show-current") !== task.branch) throw new Blocked("Worktree branch changed");
6874
const common = await this.git(directory, "rev-parse", "--path-format=absolute", "--git-common-dir");
6975
const original = await this.git(repo.directory, "rev-parse", "--path-format=absolute", "--git-common-dir");

‎test/executor.test.ts‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ test("real git worktree isolates a fix, verifies, commits and pushes to a local
107107
assert.equal(await git.hasBranch(repo, "release/next"), true);
108108
assert.equal(await git.hasBranch(repo, "release"), false);
109109
assert.equal(await git.hasBranch(repo, "missing"), false);
110-
const t = task(); Object.assign(t, await git.prepare(t, repo));
110+
const t: Task = { ...task(), worktree: undefined }; Object.assign(t, await git.prepare(t, repo));
111111
assert.equal(t.baseSha, selectedBase);
112112
assert.match(await readFile(join(t.worktree!, "release.txt"), "utf8"), /release-only/);
113113
await installWorkerPlugin(t.worktree!, options, run);
@@ -124,10 +124,30 @@ test("real git worktree isolates a fix, verifies, commits and pushes to a local
124124
const withoutTests = await git.verify(t, { ...repo, checks: [] });
125125
assert.deepEqual(withoutTests.checks, []);
126126
assert.equal(withoutTests.commit, t.commit);
127+
// Recovery can rename a published branch without moving its worktree.
128+
// Follow-up prepare, verification, and push must all honor the checkpoint.
129+
const savedPath = t.worktree!, savedBase = t.baseSha;
130+
await run(savedPath, ["git", "branch", "-m", "recovered-feature"]);
131+
t.branch = "recovered-feature";
132+
await writeFile(join(savedPath, "followup.txt"), "preserve this uncommitted work\n");
133+
Object.assign(t, await git.prepare(t, repo));
134+
assert.equal(t.worktree, savedPath);
135+
assert.equal(t.baseSha, savedBase);
136+
assert.equal(await readFile(join(savedPath, "followup.txt"), "utf8"), "preserve this uncommitted work\n");
137+
Object.assign(t, await git.verify(t, repo));
138+
await git.push(t, repo);
139+
assert.equal(await run(dir, ["git", "--git-dir", remote, "rev-parse", "refs/heads/recovered-feature"]), t.commit);
140+
await assert.rejects(git.prepare({ ...t, worktree: join(state, "worktrees", "missing-checkpoint") }, repo), /Saved task worktree is missing/);
141+
await assert.rejects(git.prepare({ ...t, worktree: checkout }, repo), /Unexpected worktree path/);
142+
await assert.rejects(git.prepare({ ...t, branch: "wrong-branch" }, repo), /Worktree branch changed/);
143+
const foreign = join(savedPath, "..", "foreign");
144+
await mkdir(foreign);
145+
await run(foreign, ["git", "init", "-b", t.branch]);
146+
await assert.rejects(git.prepare({ ...t, worktree: foreign }, repo), /another repository/);
127147
assert.equal(await run(t.worktree!, ["git", "ls-files", "--", ".opencode/plugins/automation-runtime/index.js"]), "");
128148
await writeFile(runtimePath, "// User customization\n");
129149
await assert.rejects(installWorkerPlugin(t.worktree!, options, run), /customized/);
130-
await assert.rejects(git.prepare({ ...task(), branch: "automation/missing-base" }, { ...repo, baseBranch: "missing" }), /Could not fetch base branch missing/);
150+
await assert.rejects(git.prepare({ ...task(), worktree: undefined, branch: "automation/missing-base" }, { ...repo, baseBranch: "missing" }), /Could not fetch base branch missing/);
131151
await writeFile(join(t.worktree!, "counter.txt"), "changed after verification\n");
132152
await assert.rejects(git.push(t, repo), /changed after verification/);
133153
} finally { await rm(dir, { recursive: true, force: true }); }

0 commit comments

Comments
 (0)