Skip to content

Commit 6e09ac5

Browse files
t3dotggclaude
andcommitted
feat(scripts): check PRs against current main right before merging
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5e9d564 commit 6e09ac5

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ For authorized mobile verification, a missing or outdated native client is an in
150150
- UI changes need before/after images. Motion or timing needs a short video.
151151
- Upload PR evidence to GitHub. Never commit PR-only screenshots or assets such as `.github/pr-assets/`.
152152
- One request is one PR. Split it only when the maintainer asks. Outside contributions follow the stricter [one problem per PR](CONTRIBUTING.md#one-problem) rule.
153+
- Right before you merge, run `vp run pr-merge-check <pr>...` with every PR you are about to merge, then run the `gh pr merge --match-head-commit` commands it prints, at once and in that order. Green PR CI can be days old, and GitHub reports `CLEAN` here even when a PR is far behind `main`, so `mergeStateStatus` is not a freshness check.
153154
- When babysitting: call `watch_pull_request` and end your turn. T3 Code wakes you on checks, comments, or conflicts. Do not poll or sleep. Verify each bot finding against the source, fix real ones, dismiss false positives with a written reason. Stay quiet when nothing is new. Stop when the bots are green on the latest commit.
154155

155156
## Documentation

‎docs/operations/development.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ exports pass at about 10 s. When the files are exactly those of the last passing
160160
run's summary instead of running again. Any change to a file, the install, or a generated file runs
161161
the checks again.
162162

163+
`vp run pr-merge-check <pr>...` runs the same checks on GitHub's current `main` with the given PRs
164+
merged in order, in a temporary worktree. Run it right before merging, with the whole batch, then
165+
run the merge commands it prints. They are pinned to the tested heads. With `origin` on GitCafe, it
166+
fetches from the `github` remote, because the mirror can lag about a minute. The ruleset does not require branches
167+
to be up to date, because main takes too many merges a day, so a PR's green CI can be days old.
168+
163169
While you iterate, run single checks:
164170

165171
```sh

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"tc": "vp run -r --cache --concurrency-limit 4 typecheck",
2929
"check:changed": "node scripts/check-changed.ts",
3030
"report:renders": "node scripts/render-metrics.ts",
31+
"pr-merge-check": "node scripts/pr-merge-check.ts",
3132
"lint": "vp lint --report-unused-disable-directives",
3233
"knip": "knip --preprocessor ./scripts/knip-schemas.ts",
3334
"knip:check": "knip --include files,dependencies --no-config-hints && knip --workspace apps/server --workspace apps/desktop --workspace apps/web --workspace packages/client-runtime --workspace packages/contracts --workspace packages/effect-acp --workspace packages/effect-codex-app-server --workspace packages/shared --workspace packages/ssh --workspace packages/tailscale --workspace packages/provider-core --workspace packages/provider-acp --workspace packages/provider-acp-registry --workspace packages/provider-testing --workspace packages/provider-pi --workspace packages/provider-muse --workspace packages/provider-opencode --workspace packages/provider-cursor --workspace packages/provider-fx --workspace packages/provider-grok --workspace packages/source-control-core --workspace packages/source-control-definitions --workspace packages/source-control-azure-devops --workspace packages/source-control-bitbucket --workspace packages/source-control-forgejo --workspace packages/source-control-gitcafe --workspace packages/source-control-github --workspace packages/source-control-gitlab --workspace packages/source-control-testing --exports --preprocessor ./scripts/knip-schemas.ts --no-config-hints",

‎scripts/pr-merge-check.ts‎

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// @effect-diagnostics nodeBuiltinImport:off globalConsole:off
2+
// Checks that pull requests still pass together on current main, right before
3+
// they merge. Main takes hundreds of merges a day, so the ruleset cannot require
4+
// branches to be up to date, and GitHub reports CLEAN for a PR that is far
5+
// behind. A PR's green CI can be days old. Most breaks on main were two changes
6+
// that each passed alone: a renamed type, a removed dependency, a new request
7+
// an older test did not stub.
8+
//
9+
// vp run pr-merge-check <pr> [<pr>...]
10+
//
11+
// Merges GitHub's main and each PR head, in order, in a temporary worktree
12+
// next to this checkout, then runs check:changed against that main. With
13+
// `origin` on the GitCafe mirror, which can lag about a minute, fetches use the
14+
// `github` remote. Pass every PR of a batch in one run. Exit 0 prints the merge commands,
15+
// pinned to the tested heads. Run them now, in order: each new commit on main
16+
// makes the result older.
17+
import * as NodeChildProcess from "node:child_process";
18+
import * as NodeFS from "node:fs";
19+
import * as NodePath from "node:path";
20+
21+
const root = NodePath.resolve(import.meta.dirname, "..");
22+
const prs = process.argv.slice(2);
23+
if (prs.length === 0 || prs.some((pr) => !/^\d+$/.test(pr))) {
24+
console.error("usage: vp run pr-merge-check <pr> [<pr>...]");
25+
process.exit(1);
26+
}
27+
28+
// `vp run` puts this checkout's node_modules/.bin first on PATH. Left there,
29+
// the merged worktree would run this checkout's Vitest against its own test
30+
// files, and two Vitest copies fail every suite.
31+
const env = {
32+
...process.env,
33+
PATH: (process.env.PATH ?? "")
34+
.split(NodePath.delimiter)
35+
.filter((entry) => !entry.startsWith(`${root}${NodePath.sep}`))
36+
.join(NodePath.delimiter),
37+
};
38+
39+
const run = (command: string, args: ReadonlyArray<string>, cwd: string) =>
40+
NodeChildProcess.spawnSync(command, args, { cwd, env, stdio: "inherit" }).status === 0;
41+
42+
const git = (cwd: string, ...args: Array<string>) =>
43+
NodeChildProcess.execFileSync("git", args, { cwd, encoding: "utf8" }).trim();
44+
45+
const hasGitHubRemote =
46+
NodeChildProcess.spawnSync("git", ["remote", "get-url", "github"], { cwd: root }).status === 0;
47+
const remote = hasGitHubRemote ? "github" : "origin";
48+
// Signing can prompt or fail, which would read as a conflict.
49+
const merge = ["-c", "commit.gpgsign=false", "merge", "--quiet", "--no-ff"];
50+
51+
// The worktree has its own FETCH_HEAD, so a fetch in this checkout or in
52+
// another run cannot swap the commit under test.
53+
const dir = NodePath.resolve(root, "..", `${NodePath.basename(root)}-merge-check-${process.pid}`);
54+
git(root, "worktree", "add", "--quiet", "--detach", "--no-checkout", dir);
55+
// A terminal signal reaches the running child too. Staying alive lets
56+
// `finally` remove the worktree. SIGKILL still leaves it behind.
57+
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) process.on(signal, () => {});
58+
59+
const fetchHead = (ref: string) => {
60+
git(dir, "fetch", "--quiet", remote, ref);
61+
return git(dir, "rev-parse", "FETCH_HEAD");
62+
};
63+
64+
const heads: Array<{ pr: string; sha: string }> = [];
65+
let ok = false;
66+
try {
67+
const base = fetchHead("refs/heads/main");
68+
git(dir, "checkout", "--quiet", "--detach", base);
69+
const conflict = prs.find((pr) => {
70+
const sha = fetchHead(`pull/${pr}/head`);
71+
heads.push({ pr, sha });
72+
return !run("git", [...merge, "-m", `merge-check #${pr}`, sha], dir);
73+
});
74+
if (conflict) {
75+
console.error(`\npr-merge-check: #${conflict} does not merge cleanly onto main.`);
76+
} else {
77+
// A lockfile that two PRs edited can merge as text and still not match
78+
// the manifests. The frozen install fails on that.
79+
ok =
80+
run("vp", ["i", "--frozen-lockfile", "--", "--yes"], dir) &&
81+
run("node", ["scripts/check-changed.ts", base], dir);
82+
}
83+
} finally {
84+
git(root, "worktree", "remove", "--force", dir);
85+
NodeFS.rmSync(dir, { recursive: true, force: true });
86+
}
87+
88+
const list = prs.map((pr) => `#${pr}`).join(" ");
89+
if (!ok) {
90+
console.log(`\npr-merge-check: ${list} fail on main. Do not merge.`);
91+
process.exit(1);
92+
}
93+
console.log(`\npr-merge-check: ${list} pass on main. Merge them now, in this order:`);
94+
for (const { pr, sha } of heads)
95+
console.log(` gh pr merge ${pr} --squash --match-head-commit ${sha}`);

0 commit comments

Comments
 (0)