From 1289aefed1cce9c6387d18e0978504359d8f79bc Mon Sep 17 00:00:00 2001 From: Chris Huber Date: Sat, 5 Dec 2026 00:15:41 +0000 Subject: [PATCH] fix(publisher): replace stale runner-owned publication branch instead of refusing --- .../runner-workspace-publisher.mjs | 16 +++++++++----- tests/runner-workspace-publisher.test.mjs | 21 +++++++++++++++++-- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/.github/scripts/run-agent-task/runner-workspace-publisher.mjs b/.github/scripts/run-agent-task/runner-workspace-publisher.mjs index af4467fc..7737261d 100644 --- a/.github/scripts/run-agent-task/runner-workspace-publisher.mjs +++ b/.github/scripts/run-agent-task/runner-workspace-publisher.mjs @@ -68,6 +68,7 @@ export async function publishRunnerWorkspace({ request, changedFiles, publicatio let pull = null let parent = baseSha let reusedPull = false + let replacedSha = "" if (existing) { const pulls = await openPulls(head) pull = pulls.find((candidate) => isExactPull(candidate, head)) || null @@ -89,10 +90,15 @@ export async function publishRunnerWorkspace({ request, changedFiles, publicatio if (pull) { const existingSha = string(existing?.object?.sha) const comparison = await api("GET", `/compare/${baseSha}...${existingSha}`) - if (!existingSha || !["ahead", "identical"].includes(string(comparison.status)) || string(comparison.merge_base_commit?.sha) !== baseSha) { - throw new Error(`Existing publication branch ${head} has an open pull request but does not descend from current base ${base}@${baseSha}; rebase the pull request before publishing again.`) + if (!existingSha) throw new Error(`Existing publication branch ${head} did not resolve to a commit SHA.`) + if (["ahead", "identical"].includes(string(comparison.status)) && string(comparison.merge_base_commit?.sha) === baseSha) { + parent = existingSha + } else { + // The publication branch is runner-owned and the workspace was seeded + // from the current base, so a stale branch is replaced by a commit on + // the current base (force-updated ref) while the open PR is kept. + replacedSha = existingSha } - parent = existingSha reusedPull = true } } @@ -110,9 +116,9 @@ export async function publishRunnerWorkspace({ request, changedFiles, publicatio } const nextTree = await api("POST", "/git/trees", { base_tree: string(parentCommit.tree?.sha), tree }) const commit = await api("POST", "/git/commits", { message: string(config.commit_message || request.workload?.label || "Apply agent task changes"), tree: string(nextTree.sha), parents: [parent] }) - if (pull) await api("PATCH", `/git/refs/heads/${head.split("/").map(encodeURIComponent).join("/")}`, { sha: string(commit.sha), force: false }) + if (pull) await api("PATCH", `/git/refs/heads/${head.split("/").map(encodeURIComponent).join("/")}`, { sha: string(commit.sha), force: Boolean(replacedSha) }) else await api("POST", "/git/refs", { ref: `refs/heads/${head}`, sha: string(commit.sha) }) pull ||= await api("POST", "/pulls", { title: string(config.title || request.workload?.label || "Apply agent task changes"), head, base, body: publicationPullBody(config, captured) }) if (!isExactPull(pull, head) || !/^https:\/\/github\.com\//.test(string(pull.html_url))) throw new Error("GitHub publication response did not match the requested repository and branches.") - return { schema: "wp-codebox/runner-workspace-publication-result/v1", success: true, status: "published", backend: "github-rest", branch: { base, base_sha: baseSha, head, name: head }, commit: { sha: string(commit.sha), parent_sha: parent }, pull_request: { number: pull.number, url: pull.html_url, reused: reusedPull, opened: !reusedPull } } + return { schema: "wp-codebox/runner-workspace-publication-result/v1", success: true, status: "published", backend: "github-rest", branch: { base, base_sha: baseSha, head, name: head }, commit: { sha: string(commit.sha), parent_sha: parent, ...(replacedSha ? { replaced_sha: replacedSha } : {}) }, pull_request: { number: pull.number, url: pull.html_url, reused: reusedPull, opened: !reusedPull } } } diff --git a/tests/runner-workspace-publisher.test.mjs b/tests/runner-workspace-publisher.test.mjs index 1aa12efb..bec7e377 100644 --- a/tests/runner-workspace-publisher.test.mjs +++ b/tests/runner-workspace-publisher.test.mjs @@ -77,6 +77,8 @@ for (const defaultBranch of [undefined, "invalid branch"]) { assert.deepEqual(treeRequest?.[2]?.tree, [{ path: "README.md", mode: "100644", type: "blob", sha: "blob" }], "the prior branch tree remains the base while only approved changed files are replaced") const commitRequest = mock.calls.find(([url, method]) => new URL(url).pathname.endsWith("/git/commits") && method === "POST") assert.deepEqual(commitRequest?.[2]?.parents, ["old"], "existing branch commits must use the existing branch head as their parent") + assert.equal(mock.calls.find(([, method]) => method === "PATCH")?.[2]?.force, false, "descending branches are fast-forwarded, never forced") + assert.equal(result.commit.replaced_sha, undefined) } { const mock = fetchMock(true, "main", undefined, false) @@ -93,9 +95,24 @@ for (const defaultBranch of [undefined, "invalid branch"]) { assert.equal(result.branch.head, "wp-codebox/agent-task/run-1-base", "a PR for a different base must not be reused") } { + // wp-codebox#2638: a runner-owned branch with an open PR that no longer + // descends from base is replaced by a commit on the current base. const mock = fetchMock(true, "main", undefined, true, "diverged") - await assert.rejects(() => publishRunnerWorkspace({ request, changedFiles: ["README.md"], publicationFiles, token: "secret", fetchImpl: mock.fetch }), /does not descend from current base/) - assert(!mock.calls.some(([, method]) => method === "POST" || method === "PATCH"), "an unsafe open PR branch must fail before publication writes") + const result = await publishRunnerWorkspace({ request, changedFiles: ["README.md"], publicationFiles, token: "secret", fetchImpl: mock.fetch }) + assert.equal(result.branch.head, "wp-codebox/agent-task/run-1", "a stale open-PR branch keeps its name") + assert.equal(result.pull_request.number, 4, "the open PR is kept") + assert.equal(result.pull_request.reused, true) + assert.equal(result.pull_request.opened, false) + assert.equal(result.commit.parent_sha, "base") + assert.equal(result.commit.replaced_sha, "old", "the superseded branch head is reported") + const treeRequest = mock.calls.find(([url, method]) => new URL(url).pathname.endsWith("/git/trees") && method === "POST") + assert.equal(treeRequest?.[2]?.base_tree, "tree", "replacement must build on the current base tree") + const commitRequest = mock.calls.find(([url, method]) => new URL(url).pathname.endsWith("/git/commits") && method === "POST") + assert.deepEqual(commitRequest?.[2]?.parents, ["base"], "replacement commit must be parented on the current base") + const refUpdate = mock.calls.find(([, method]) => method === "PATCH") + assert.deepEqual(refUpdate?.[2], { sha: "commit", force: true }, "the branch ref is force-updated to the replacement commit") + assert(!mock.calls.some(([url, method]) => method === "POST" && new URL(url).pathname.endsWith("/pulls")), "no new PR is opened") + assert(!mock.calls.some(([url, method]) => method === "POST" && new URL(url).pathname.endsWith("/git/refs")), "no new ref is created") } { const mock = fetchMock(true, "main", undefined, false, "ahead", "main", true)