Skip to content

The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.

fix(tools): never seed a worktree from a stale remote-tracking ref; sync the worktree to a rewritten PR branch

Zhuowei Wang61d94e93e9fc
diff --git a/tools/linear-autofix/src/cli.mjs b/tools/linear-autofix/src/cli.mjs
index 5ffde4f..977a970 100755
--- a/tools/linear-autofix/src/cli.mjs
+++ b/tools/linear-autofix/src/cli.mjs
@@ -91,6 +91,7 @@ async function reset(identifier, deleteRemote) {
console.log(`worktree removed: ${dir}`);
if (await git.deleteLocalBranch(branch))
console.log(`local branch deleted: ${branch}`);
+ await git.deleteTrackingRef(branch);
if (deleteRemote) {
await git.deleteRemoteBranch(branch);
console.log(`remote branch deleted: ${config.remote}/${branch}`);
diff --git a/tools/linear-autofix/src/git.mjs b/tools/linear-autofix/src/git.mjs
index eaff943..0678909 100644
--- a/tools/linear-autofix/src/git.mjs
+++ b/tools/linear-autofix/src/git.mjs
@@ -19,9 +19,14 @@ async function refExists(ref) {
}
/**
- * Make sure a worktree for `branch` exists at `dir`, based on the remote base
- * branch. Reuses a local or remote branch of that name when present so a
- * crashed run or a pre-existing PR branch is picked up rather than clobbered.
+ * Make sure a worktree for `branch` exists at `dir`.
+ *
+ * A branch that exists on the remote (checked with ls-remote, never with the
+ * local remote-tracking ref, which survives the remote branch's deletion) or
+ * locally is resumed; otherwise the branch is cut from `origin/<base>`. A
+ * stale remote-tracking ref is deleted first: one left behind by a deleted
+ * branch once seeded a worktree with the wrong history (#631). A freshly
+ * created worktree must start inside `origin/<base>`, or the call fails.
*/
export async function ensureWorktree(dir, branch) {
const base = `${config.remote}/${config.baseBranch}`;
@@ -43,20 +48,20 @@ export async function ensureWorktree(dir, branch) {
}
throw new Error(`${dir} exists but is not a registered worktree`);
}
- await git(
- [
+ const trackingRef = `refs/remotes/${config.remote}/${branch}`;
+ if (await remoteBranchExists(branch)) {
+ await git([
"fetch",
"--quiet",
config.remote,
- `+refs/heads/${branch}:refs/remotes/${config.remote}/${branch}`,
- ],
- {
- allowFailure: true,
- },
- );
+ `+refs/heads/${branch}:${trackingRef}`,
+ ]);
+ } else {
+ await git(["update-ref", "-d", trackingRef], { allowFailure: true });
+ }
if (await refExists(`refs/heads/${branch}`)) {
await git(["worktree", "add", dir, branch]);
- } else if (await refExists(`refs/remotes/${config.remote}/${branch}`)) {
+ } else if (await refExists(trackingRef)) {
await git([
"worktree",
"add",
@@ -68,10 +73,51 @@ export async function ensureWorktree(dir, branch) {
]);
} else {
await git(["worktree", "add", "-b", branch, dir, base]);
+ const { code } = await gitIn(
+ dir,
+ ["merge-base", "--is-ancestor", "HEAD", base],
+ {
+ allowFailure: true,
+ },
+ );
+ if (code !== 0) {
+ throw new Error(`new worktree ${dir} does not start inside ${base}`);
+ }
}
return { created: true };
}
+/**
+ * Bring the worktree to the remote branch's state when they diverged: a
+ * human may have rewritten the PR branch (for example to drop commits). The
+ * remote is the truth for a PR branch. Returns what happened.
+ */
+export async function syncWorktreeToRemote(dir, branch) {
+ if (!(await remoteBranchExists(branch))) return "no-remote";
+ await git([
+ "fetch",
+ "--quiet",
+ config.remote,
+ `+refs/heads/${branch}:refs/remotes/${config.remote}/${branch}`,
+ ]);
+ const remote = `${config.remote}/${branch}`;
+ const local = await headSha(dir);
+ const remoteHead = (await gitIn(dir, ["rev-parse", remote])).stdout.trim();
+ if (local === remoteHead) return "in-sync";
+ if (await isDirty(dir))
+ throw new Error(`worktree ${dir} is dirty; cannot sync to ${remote}`);
+ const { code: remoteIsBehind } = await gitIn(
+ dir,
+ ["merge-base", "--is-ancestor", remote, "HEAD"],
+ {
+ allowFailure: true,
+ },
+ );
+ if (remoteIsBehind === 0) return "ahead"; // local commits not pushed yet; keep them
+ await gitIn(dir, ["reset", "--hard", remote]);
+ return "reset-to-remote";
+}
+
export async function removeWorktree(dir) {
await git(["worktree", "remove", "--force", dir], { allowFailure: true });
}
@@ -197,3 +243,10 @@ export async function remoteBranchExists(branch) {
);
return stdout.trim() !== "";
}
+
+/** Drop the local remote-tracking ref for a branch (stale after the remote branch was deleted). */
+export async function deleteTrackingRef(branch) {
+ await git(["update-ref", "-d", `refs/remotes/${config.remote}/${branch}`], {
+ allowFailure: true,
+ });
+}
diff --git a/tools/linear-autofix/src/review.mjs b/tools/linear-autofix/src/review.mjs
index d35a42c..3fe5fea 100644
--- a/tools/linear-autofix/src/review.mjs
+++ b/tools/linear-autofix/src/review.mjs
@@ -177,6 +177,14 @@ export async function review(issue, record) {
const dir = worktreePath(record.identifier);
await git.ensureWorktree(dir, record.branch);
+ const sync = await git.syncWorktreeToRemote(dir, record.branch);
+ if (sync === "reset-to-remote") {
+ note(
+ record,
+ "worktree reset to the remote branch",
+ "the PR branch was rewritten outside the bot",
+ );
+ }
await git.installDeps(dir);
try {
await git.mergeBase(dir);