fix(rollout-gate): measure a pull request from its merge base (#391)

The gate read a PR's dependency change as depsAt(pr.base.sha) ->
depsAt(pr.head.sha). pr.base.sha is the base branch as GitHub last
recorded it, not where the branch forked. A branch that forked before the
base bumped a shared package therefore read as reverting the bump, even
when the PR touches no manifest or lockfile.

bitbaum/diplodoctor#59 (tests, a script, a workflow, .env.example) forked
at aebf711, which locks @bitbaum/chatkit 0.21.2. #58 then moved main to
0.21.3, and pr.base.sha is 8965fa3 (#58). The gate saw 0.21.3 -> 0.21.2
and held #59 until a canary deployed 0.21.2 (sweep run 41157383295:
"@bitbaum/chatkit@0.21.2: wait for bitbaum/kivvi to deploy this
artifact"). Both canaries already run 0.21.3 with green CI, browser smoke
and deploy, so that could never happen. The sweep runs the gate before it
refreshes a behind branch, so the update that would have cleared it was
never reached either.

The baseline is now the merge base from the compare API: the parent of
exactly the diff a squash merge lands. An unreadable merge base throws,
which holds the PR like any other missing evidence. Nothing about what the
gate requires of a real dependency change is relaxed.


Claude-Session: https://claude.ai/code/session_019MDtmuJhiHPASaR5k8raL9

Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
CatoandClaude authored and GitHub committed 2026-10-31 05:24:27 +01:00
1 parent f1a6140a8c
commit a74122abb4
4 files changed
+90 -2

No files matched your search

+3 -1
View File
@@ -22,7 +22,9 @@ deployment restarts the observation period. Each consumer must still pass all of
Known first-party packages without a designated canary wait for configuration;
they do not silently promote. Lock-only pnpm root dependency changes are checked,
including pinned Git artifacts; npm lockfile v2/v3 direct dependencies are also
supported. Unsupported lock formats, unavailable API evidence and checker errors
supported. A pull request's change is measured from its merge base, so a branch
that forked before the base moved a dependency is not read as reverting it.
Unsupported lock formats, unavailable API evidence and checker errors
hold automatic merging. Changed shared artifacts in nested pnpm importers hold automatic promotion
until workspace-specific canary evidence is supported. Standalone nested dependency manifest changes and nested lockfile updates
with shared dependencies likewise hold automatic promotion. The gate does not
+46
View File
@@ -35,6 +35,7 @@ const manifest = v => JSON.stringify({name:'fixture-app',dependencies:{'@bitbaum
let result;
if (verb !== 'api') throw Error('unexpected CLI invocation');
if (path === 'repos/bitbaum/example/pulls/1') result={base:{sha:'a'.repeat(40)},head:{sha:'b'.repeat(40)}};
else if (path.startsWith('repos/bitbaum/example/compare/'+'a'.repeat(40)+'...'+'b'.repeat(40))) result={merge_base_commit:{sha:'a'.repeat(40)}};
else if (path.includes('/pulls/1/files?')) result=[{filename:(project==='.'?'':project+'/')+'package.json'},{filename:(project==='.'?'':project+'/')+'pnpm-lock.yaml'}];
else if (path === 'repos/bitbaum/biaslens') result={default_branch:'main'};
else if (path === 'repos/bitbaum/biaslens/commits/main') result={sha:tip};
@@ -133,6 +134,7 @@ const encode = value => ({content:Buffer.from(value).toString('base64')});
let result;
if(verb!=='api') throw Error('unexpected CLI invocation');
if(path==='repos/bitbaum/loki/pulls/1') result={base:{sha:base},head:{sha:head}};
else if(path.startsWith('repos/bitbaum/loki/compare/'+base+'...'+head)) result={merge_base_commit:{sha:base}};
else if(path.includes('/pulls/1/files?')) result=[{filename:'scripts/site-template/package.json'},{filename:'scripts/site-template/pnpm-lock.yaml'}];
else if(path==='repos/bitbaum/kivvi') result={default_branch:'main'};
else if(path==='repos/bitbaum/kivvi/commits/main') result={sha:tip};
@@ -182,3 +184,47 @@ console.log(JSON.stringify(result));
rmSync(dir, { recursive: true, force: true });
}
});
// bitbaum/diplodoctor#59 (2026-10-31): a branch forked before the base moved
// @bitbaum/chatkit 0.21.2 -> 0.21.3 never touched a manifest, yet measuring it
// from `pr.base.sha` read as a downgrade to 0.21.2, which no canary would ever
// run again. The sweep holds before it refreshes a behind branch, so the PR
// could never clear. The PR's change is measured from its merge base.
test("a stale branch that does not touch the lockfile is not read as a downgrade", () => {
const dir = mkdtempSync(join(tmpdir(), "rollout-stale-cli-"));
const fork = "e".repeat(40);
try {
writeFileSync(
join(dir, "gh"),
`#!${process.execPath}
const [verb, path] = process.argv.slice(2);
const base='a'.repeat(40), head='b'.repeat(40), fork=${JSON.stringify(fork)};
const lock = v => "lockfileVersion: '9.0'\\nimporters:\\n .:\\n dependencies:\\n '@bitbaum/chatkit':\\n specifier: ^"+v+"\\n version: "+v+"\\n";
const manifest = v => JSON.stringify({name:'fixture-app',dependencies:{'@bitbaum/chatkit':'^'+v}});
let result;
if (verb !== 'api') throw Error('unexpected CLI invocation');
if (path === 'repos/bitbaum/example/pulls/1') result={base:{sha:base},head:{sha:head}};
else if (path.startsWith('repos/bitbaum/example/compare/'+base+'...'+head)) result={merge_base_commit:{sha:fork}};
else if (path.includes('/pulls/1/files?')) result=[{filename:'lib/auth/recovery.test.ts'}];
else if (path.includes('/contents/')) {
const [file,ref]=/^repos\\/bitbaum\\/example\\/contents\\/(.+)\\?ref=(.+)$/.exec(path).slice(1);
const v = ref===base ? '0.21.3' : '0.21.2';
const content = file==='package.json'?manifest(v):file==='pnpm-lock.yaml'?lock(v):null;
if (content!==null) result={content:Buffer.from(content).toString('base64')};
}
if(result===undefined){console.error('404');process.exit(1)}
console.log(JSON.stringify(result));
`,
{ mode: 0o755 },
);
const run = spawnSync(
process.execPath,
[new URL("./package-rollout-gate.mjs", import.meta.url).pathname, "bitbaum/example", "1"],
{ encoding: "utf8", env: { ...process.env, PATH: dir + ":" + process.env.PATH } },
);
assert.equal(run.status, 0, run.stdout + run.stderr);
assert.doesNotMatch(run.stdout, /chatkit@0\.21\.2/);
} finally {
rmSync(dir, { recursive: true, force: true });
}
});
+25 -1
View File
@@ -523,9 +523,33 @@ export async function canaryDeploymentEvidence({
};
}
/**
* The commit the pull request's own change is measured from. `pr.base.sha` is
* the base branch as GitHub last recorded it, not where the branch forked: a
* branch that predates a dependency bump on the base would read as reverting
* it, and the gate would hold a PR that never touches a manifest. The merge
* base is the parent of exactly the diff a squash merge lands.
*/
export function pullRequestBaseline(pr, readCompare) {
const compare = readCompare(pr.base.sha, pr.head.sha);
const sha = compare?.merge_base_commit?.sha;
if (!/^[0-9a-f]{40}$/.test(sha ?? ""))
throw new Error("merge base of the pull request is unavailable");
return sha;
}
async function main() {
const [repo, number] = process.argv.slice(2);
const pr = api(`repos/${repo}/pulls/${number}`);
const pullRequest = api(`repos/${repo}/pulls/${number}`);
const pr = {
...pullRequest,
base: {
...pullRequest.base,
sha: pullRequestBaseline(pullRequest, (base, head) =>
api(`repos/${repo}/compare/${base}...${head}?per_page=1`),
),
},
};
// Avoid requiring a Node lockfile from repositories with no root Node manifest.
const manifest = content(repo, "package.json", pr.head.sha);
// Package repositories publish artifacts; their downstream application consumers
+16
View File
@@ -592,3 +592,19 @@ test("multiple named jobs reuse the exact run and reject a missing guard", () =>
[],
);
});
test("the pull request baseline is its merge base, and an unreadable one holds", async () => {
const { pullRequestBaseline } = await import("./package-rollout-gate.mjs");
const pr = { base: { sha: "a".repeat(40) }, head: { sha: "b".repeat(40) } };
const seen = [];
assert.equal(
pullRequestBaseline(pr, (base, head) => {
seen.push([base, head]);
return { merge_base_commit: { sha: "e".repeat(40) } };
}),
"e".repeat(40),
);
assert.deepEqual(seen, [["a".repeat(40), "b".repeat(40)]]);
for (const reply of [undefined, {}, { merge_base_commit: { sha: "main" } }])
assert.throws(() => pullRequestBaseline(pr, () => reply), /merge base/);
});