Public: anyone can read this; the crew can change it.
Merge gate gap: #16507 (opened by trevato at 10:55Z) merged through MCP `merg… #16533
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16508-merge-gate-gap-16507-opened-by-trevato-a → main 0 commits fork
12 files
changed +161
−13
src/actions/pull_slices.ts +2 −1
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | * slice is an ancestor of it: it lands only what that verdict covered. |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | import { reviewsDemanded } from "../crew/review.ts"; | |
| 15 | 16 | import { strangerGuard } from "../crew/stranger.ts"; |
| 16 | 17 | import type { App } from "../app.ts"; |
| 17 | 18 | import { addComment as addIssueComment } from "../core/issues.ts"; |
| @@ -93,7 +94,7 @@ export async function landSlice( | ||
| 93 | 94 | // A stranger's slice is read against the guard as the whole pull is (#9575). |
| 94 | 95 | const guarded = await strangerGuard(app, repo, live, upTo); |
| 95 | 96 | if (guarded.length > 0) return err({ kind: "guarded", paths: guarded }); |
| 96 | const verdict = reviewGate({ db: app.db, requireReview: repo.requireReview }, live, status.headOid); | |
| 97 | const verdict = reviewGate({ db: app.db, requireReview: reviewsDemanded(app.db, repo) }, live, status.headOid); | |
| 97 | 98 | if (!verdict.mergeable) return err("review_required"); |
| 98 | 99 | const check = checkSummary(app.db, live.id, status.headOid); |
| 99 | 100 | if ( |
src/actions/pulls.ts +2 −1
| @@ -21,6 +21,7 @@ import type { App } from "../app.ts"; | ||
| 21 | 21 | import { applyBoxPass, holdPass, landHeldPass, noteLandedDone, tickDelivery, type BoxPass } from "./box_pass.ts"; |
| 22 | 22 | import { landVision } from "../crew/vision.ts"; |
| 23 | 23 | import { crewRole } from "../core/agents.ts"; |
| 24 | import { reviewsDemanded } from "../crew/review.ts"; | |
| 24 | 25 | import { |
| 25 | 26 | getIssueById, |
| 26 | 27 | setAssignee, |
| @@ -607,7 +608,7 @@ export async function mergePull( | ||
| 607 | 608 | ...(at === baseDir ? {} : { sealedFrom: at }), |
| 608 | 609 | gate: { |
| 609 | 610 | db: app.db, |
| 610 | requireReview: repo.requireReview, | |
| 611 | requireReview: reviewsDemanded(app.db, repo), | |
| 611 | 612 | requireChecks: repo.requireChecks && opts.pastGate !== true, |
| 612 | 613 | freshGreen: opts.pastGate !== true, |
| 613 | 614 | // Only an app's first change, which the crew's gate decided (`crew/first_change.ts`); never a person's own call. |
src/core/pulls.ts +3 −2
| @@ -1282,10 +1282,11 @@ export async function mergePull( | ||
| 1282 | 1282 | }); |
| 1283 | 1283 | return ok({ mergeOid: carrier, carried: true }); |
| 1284 | 1284 | } |
| 1285 | if (status.reviews !== null && !status.reviews.mergeable) | |
| 1286 | return err("review_required"); | |
| 1285 | // The named team is the more precise refusal, so it answers before the general one. | |
| 1287 | 1286 | const teams = input.gate?.teamsHolding?.(status.headOid) ?? []; |
| 1288 | 1287 | if (teams.length > 0) return err({ kind: "team_review_required", teams }); |
| 1288 | if (status.reviews !== null && !status.reviews.mergeable) | |
| 1289 | return err("review_required"); | |
| 1289 | 1290 | // The check is the latest deploy run of *this* head. Read here, not |
| 1290 | 1291 | // handed in, for the same reason as the review verdict above. A landing |
| 1291 | 1292 | // the merge queue gated (`expect`) is not held for a green on an older |
src/crew/autonomy.ts +2 −1
| @@ -2469,7 +2469,8 @@ async function landGated( | ||
| 2469 | 2469 | ms: 0, |
| 2470 | 2470 | }); |
| 2471 | 2471 | const strategy = await landingStrategy(app, repo, pull, expect.baseOid); |
| 2472 | const merged = await mergePull(app, by, repo, pull, strategy, expect, review === "first" ? { pastReviews: true } : {}); | |
| 2472 | const pastReviews = review === "first" || review === "lapsed"; | |
| 2473 | const merged = await mergePull(app, by, repo, pull, strategy, expect, pastReviews ? { pastReviews: true } : {}); | |
| 2473 | 2474 | if (!merged.ok) { |
| 2474 | 2475 | const why = mergeWhy(merged.error); |
| 2475 | 2476 | log.info("green but not landed", { |
src/crew/review.ts +7
| @@ -149,6 +149,13 @@ export const reviewsOff = ( | ||
| 149 | 149 | seated?: ReadonlyMap<string, readonly Agent[]>, |
| 150 | 150 | ): boolean => !repo.requireReview && ((seated ?? reviewersForRepos(db, [repo.id])).get(repo.id)?.length ?? 0) === 0; |
| 151 | 151 | |
| 152 | /** | |
| 153 | * The one rule for landing (#16508): every pull needs an approving review of its head unless | |
| 154 | * reviews are off (`reviewsOff`). `mergePull`, the merge tool and the queue all land through it. | |
| 155 | */ | |
| 156 | export const reviewsDemanded = (db: Database, repo: Pick<Repo, "id" | "requireReview">): boolean => | |
| 157 | !reviewsOff(db, repo); | |
| 158 | ||
| 152 | 159 | /** `reviewersOf` for many repositories at once, by repo; one with none is absent. */ |
| 153 | 160 | export const reviewersForRepos = (db: Database, repoIds: readonly string[]): Map<string, Agent[]> => |
| 154 | 161 | crewWithRole(db, repoIds, "reviewer"); |
src/http/api/pulls.ts +2 −1
| @@ -7,6 +7,7 @@ | ||
| 7 | 7 | * the rules live in `actions/pulls.ts` and its neighbours. |
| 8 | 8 | */ |
| 9 | 9 | |
| 10 | import { reviewsDemanded } from "../../crew/review.ts"; | |
| 10 | 11 | import { pullWaitsFor } from "../../flow/waits_for.ts"; |
| 11 | 12 | import { parseAttestation } from "../../core/attestation.ts"; |
| 12 | 13 | import { reviewWords } from "../../actions/review_words.ts"; |
| @@ -368,7 +369,7 @@ export function pullRoutes(router: Router<Handler>): void { | ||
| 368 | 369 | // Include the diff so a reviewer (human or agent) reads the change without |
| 369 | 370 | // a second round trip, and the review gate beside it so a client knows |
| 370 | 371 | // whether merging is even on offer. |
| 371 | const gate = { db: ctx.app.db, requireReview: found.repo.requireReview }; | |
| 372 | const gate = { db: ctx.app.db, requireReview: reviewsDemanded(ctx.app.db, found.repo) }; | |
| 372 | 373 | const { status, diff } = await timePhaseAsync(ctx.request, "diff", async () => { |
| 373 | 374 | const dir = await pullDirOf(ctx, found.repo, found.pull); |
| 374 | 375 | return wantDiff === "0" |
test/box-pass.test.ts +1 −1
| @@ -81,7 +81,7 @@ describe("the reviewer's pass over the boxes", () => { | ||
| 81 | 81 | return getPull(app.db, repo.id, opened.value.number)!; |
| 82 | 82 | }; |
| 83 | 83 | const land = async (pull: Pull, strategy: "squash" | "merge" = "squash") => { |
| 84 | const merged = await mergePull(app, alice, fresh(), pull, strategy); | |
| 84 | const merged = await mergePull(app, alice, fresh(), pull, strategy, undefined, { pastReviews: true }); | |
| 85 | 85 | if (!merged.ok) throw new Error(String(merged.error)); |
| 86 | 86 | }; |
| 87 | 87 | const approve = async (as: User, pull: Pull, pass: Parameters<typeof passOf>) => { |
test/holds.test.ts +1 −1
| @@ -507,7 +507,7 @@ describe("a held pull and the rules", () => { | ||
| 507 | 507 | |
| 508 | 508 | test("a person's own merge still works on a held pull", async () => { |
| 509 | 509 | const pull = await greenPull("by-hand", f.alice); |
| 510 | approve(pull, f.alice); | |
| 510 | approve(pull, quill); | |
| 511 | 511 | hold(pull); |
| 512 | 512 | const landed = await mergePull(f.app, f.alice, fresh(), pull, "squash"); |
| 513 | 513 | expect(landed.ok).toBe(true); |
test/merge-needs-review.test.ts added +135
| @@ -0,0 +1,135 @@ | ||
| 1 | /** | |
| 2 | * One rule for every landing (#16508): a pull needs an approving review of its head unless | |
| 3 | * reviews are off. A reviewer seated with `requireReview` unset is "reviews on": a fresh | |
| 4 | * pull with a green check must not land unreviewed. With no reviewer and no ask, it may. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { createApp, type App } from "../src/app.ts"; | |
| 11 | import { mergePull, openPull, submitReview } from "../src/actions/pulls.ts"; | |
| 12 | import { landSlice } from "../src/actions/pull_slices.ts"; | |
| 13 | import { nameTeams } from "../src/actions/issue_teams.ts"; | |
| 14 | import { openIssue } from "../src/actions/issues.ts"; | |
| 15 | import { saveTeam } from "../src/core/teams.ts"; | |
| 16 | import { createToken } from "../src/core/tokens.ts"; | |
| 17 | import { serve, type Servers } from "../src/http/server.ts"; | |
| 18 | import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts"; | |
| 19 | import { addCrew, createAgentRow } from "../src/core/agents.ts"; | |
| 20 | import { getPull } from "../src/core/pulls.ts"; | |
| 21 | import { getRepo, type Repo } from "../src/core/repos.ts"; | |
| 22 | import { createUser, type User } from "../src/core/users.ts"; | |
| 23 | import { reviewsDemanded } from "../src/crew/review.ts"; | |
| 24 | ||
| 25 | let dir: string; | |
| 26 | let app: App; | |
| 27 | let alice: User; | |
| 28 | let juniper: User; | |
| 29 | let repo: Repo; | |
| 30 | let n = 0; | |
| 31 | ||
| 32 | const open = async () => { | |
| 33 | const branch = `b-${++n}`; | |
| 34 | const c = await commitToBranch(app, alice, repo, { branch, message: "c", files: [{ path: `${branch}.txt`, content: "x\n" }] }); | |
| 35 | if (!c.ok) throw new Error(JSON.stringify(c.error)); | |
| 36 | const o = await openPull(app, alice, repo, { title: branch, baseBranch: "main", headBranch: branch }); | |
| 37 | if (!o.ok) throw new Error(String(o.error)); | |
| 38 | return o.value; | |
| 39 | }; | |
| 40 | ||
| 41 | beforeAll(async () => { | |
| 42 | dir = await mkdtemp(join(tmpdir(), "forge-merge-review-")); | |
| 43 | app = createApp({ dataDir: dir, port: 0 }); | |
| 44 | const a = await createUser(app.db, { origin: "seed", trusted: true, username: "alice", email: "[email protected]", password: "a-good-passphrase", adminIfFirst: true }); | |
| 45 | if (!a.ok) throw new Error(a.error); | |
| 46 | alice = a.value; | |
| 47 | const j = await createUser(app.db, { origin: "agent", trusted: true, username: "juniper", email: "[email protected]", password: "", agent: true }); | |
| 48 | if (!j.ok) throw new Error(j.error); | |
| 49 | juniper = j.value; | |
| 50 | createAgentRow(app.db, { userId: juniper.id, brief: "Reviews.", roles: ["reviewer"] }); | |
| 51 | const r = await createRepo(app, alice, { name: "game", visibility: "public" }); | |
| 52 | if (!r.ok) throw new Error(String(r.error)); | |
| 53 | await commitToBranch(app, alice, r.value, { message: "seed", files: [{ path: "README.md", content: "# g\n" }] }); | |
| 54 | repo = getRepo(app.db, "alice", "game")!; | |
| 55 | }, 30_000); | |
| 56 | ||
| 57 | afterAll(async () => { | |
| 58 | app.close(); | |
| 59 | await rm(dir, { recursive: true, force: true }); | |
| 60 | }); | |
| 61 | ||
| 62 | test("reviews off (no ask, no reviewer): a pull lands unreviewed", async () => { | |
| 63 | expect(reviewsDemanded(app.db, repo)).toBe(false); | |
| 64 | const pull = await open(); | |
| 65 | const merged = await mergePull(app, alice, repo, pull, "merge"); | |
| 66 | expect(merged.ok).toBe(true); | |
| 67 | }); | |
| 68 | ||
| 69 | test("a reviewer is seated and requireReview is unset: merge refuses until an approval of the head", async () => { | |
| 70 | addCrew(app.db, repo.id, juniper.id, alice.id, "reviewer"); | |
| 71 | repo = getRepo(app.db, "alice", "game")!; | |
| 72 | expect(repo.requireReview).toBe(false); | |
| 73 | expect(reviewsDemanded(app.db, repo)).toBe(true); | |
| 74 | const pull = await open(); | |
| 75 | const refused = await mergePull(app, alice, repo, pull, "merge"); | |
| 76 | expect(refused.ok ? "landed" : refused.error).toBe("review_required"); | |
| 77 | const head = getPull(app.db, repo.id, pull.number)!; | |
| 78 | const approved = await submitReview(app, juniper, repo, head, { state: "approved" }); | |
| 79 | expect(approved.ok).toBe(true); | |
| 80 | const merged = await mergePull(app, alice, repo, getPull(app.db, repo.id, pull.number)!, "merge"); | |
| 81 | expect(merged.ok).toBe(true); | |
| 82 | }); | |
| 83 | ||
| 84 | test("landSlice: with a reviewer seated and requireReview unset, a slice needs the head's approval", async () => { | |
| 85 | const forked = await forkRepo(app, alice, repo, "game-fork"); | |
| 86 | if (!forked.ok) throw new Error(String(forked.error)); | |
| 87 | const branch = `slice-${++n}`; | |
| 88 | const one = await commitToBranch(app, alice, forked.value, { branch, message: "one", files: [{ path: `${branch}-1.txt`, content: "1\n" }] }); | |
| 89 | if (!one.ok) throw new Error(JSON.stringify(one.error)); | |
| 90 | const two = await commitToBranch(app, alice, forked.value, { branch, message: "two", files: [{ path: `${branch}-2.txt`, content: "2\n" }] }); | |
| 91 | if (!two.ok) throw new Error(JSON.stringify(two.error)); | |
| 92 | const o = await openPull(app, alice, repo, { title: branch, baseBranch: "main", headBranch: branch, headRepo: forked.value }); | |
| 93 | if (!o.ok) throw new Error(String(o.error)); | |
| 94 | expect(repo.requireReview).toBe(false); | |
| 95 | const refused = await landSlice(app, alice, repo, o.value, one.value.commitOid); | |
| 96 | expect(refused.ok ? "landed" : refused.error).toBe("review_required"); | |
| 97 | const head = getPull(app.db, repo.id, o.value.number)!; | |
| 98 | expect((await submitReview(app, juniper, repo, head, { state: "approved" })).ok).toBe(true); | |
| 99 | const landed = await landSlice(app, alice, repo, getPull(app.db, repo.id, o.value.number)!, one.value.commitOid); | |
| 100 | expect(landed.ok).toBe(true); | |
| 101 | }); | |
| 102 | ||
| 103 | test("the pull read reports reviews.mergeable false before approval and true after", async () => { | |
| 104 | const servers: Servers = await serve(app); | |
| 105 | try { | |
| 106 | const token = createToken(app.db, alice.id, "rw", "rw").token; | |
| 107 | const pull = await open(); | |
| 108 | const read = async () => { | |
| 109 | const res = await fetch(`${servers.url}/api/v1/repos/alice/game/pulls/${pull.number}`, { headers: { authorization: `Bearer ${token}` } }); | |
| 110 | return ((await res.json()) as { status: { reviews: { mergeable: boolean } } }).status.reviews.mergeable; | |
| 111 | }; | |
| 112 | expect(repo.requireReview).toBe(false); | |
| 113 | expect(await read()).toBe(false); | |
| 114 | const head = getPull(app.db, repo.id, pull.number)!; | |
| 115 | expect((await submitReview(app, juniper, repo, head, { state: "approved" })).ok).toBe(true); | |
| 116 | expect(await read()).toBe(true); | |
| 117 | } finally { | |
| 118 | servers.stop(); | |
| 119 | } | |
| 120 | }); | |
| 121 | ||
| 122 | test("a named team that holds with no review answers team_review_required, not review_required", async () => { | |
| 123 | saveTeam(app.db, "design", { charter: "", dial: 0, members: [juniper.id] }); | |
| 124 | const issue = openIssue(app, alice, repo, { title: "A chart" }); | |
| 125 | if (!issue.ok) throw new Error(issue.error); | |
| 126 | const named = nameTeams(app, alice, repo, issue.value, ["design"]); | |
| 127 | if (!named.ok) throw new Error(named.error); | |
| 128 | const branch = `team-${++n}`; | |
| 129 | const c = await commitToBranch(app, alice, repo, { branch, message: "c", files: [{ path: `${branch}.txt`, content: "x\n" }] }); | |
| 130 | if (!c.ok) throw new Error(JSON.stringify(c.error)); | |
| 131 | const o = await openPull(app, alice, repo, { title: branch, body: `Closes #${issue.value.number}`, baseBranch: "main", headBranch: branch }); | |
| 132 | if (!o.ok) throw new Error(String(o.error)); | |
| 133 | const refused = await mergePull(app, alice, repo, o.value, "merge"); | |
| 134 | expect(refused.ok ? "landed" : refused.error).toEqual({ kind: "team_review_required", teams: ["Design"] }); | |
| 135 | }); |
test/review-gate.test.ts +2 −2
| @@ -167,8 +167,8 @@ describe("the review gate", () => { | ||
| 167 | 167 | expect(await landIfGreen(app, fresh(), moved)).toBe(false); |
| 168 | 168 | expect(getPull(app.db, repo.id, pull.number)?.mergedAt).toBeNull(); |
| 169 | 169 | |
| 170 | // A person's approval of the new head counts the same. | |
| 171 | approve(moved, alice); | |
| 170 | // The reviewer's approval of the new head lands it. | |
| 171 | approve(moved, quill); | |
| 172 | 172 | expect(await landIfGreen(app, fresh(), moved)).toBe(true); |
| 173 | 173 | expect(getPull(app.db, repo.id, pull.number)?.mergedAt).not.toBeNull(); |
| 174 | 174 | }); |
test/stranger-pull.test.ts +1 −1
| @@ -397,6 +397,6 @@ describe("a stranger's pull", () => { | ||
| 397 | 397 | if (!opened.ok) throw new Error(String(opened.error)); |
| 398 | 398 | const pull = getPull(app.db, lib.id, opened.value.number)!; |
| 399 | 399 | expect(isStrangerPull(app.db, lib, pull)).toBe(false); |
| 400 | expect((await mergePull(app, owner, lib, pull, "squash")).ok).toBe(true); | |
| 400 | expect((await mergePull(app, owner, lib, pull, "squash", undefined, { pastReviews: true })).ok).toBe(true); | |
| 401 | 401 | }); |
| 402 | 402 | }); |
test/team-merge-gate.test.ts +3 −2
| @@ -78,7 +78,7 @@ describe("a named team's review gates the merge", () => { | ||
| 78 | 78 | }; |
| 79 | 79 | const approve = (pull: Pull, by: string) => |
| 80 | 80 | submitReview(app.db, { issueId: pull.id, authorId: id(by), state: "approved", commitOid: fresh(pull).headOid }); |
| 81 | const land = (pull: Pull, opts: { pastGate?: boolean } = {}) => | |
| 81 | const land = (pull: Pull, opts: { pastGate?: boolean; pastReviews?: boolean } = {}) => | |
| 82 | 82 | mergePull(app, alice, repo, fresh(pull), "merge", undefined, opts); |
| 83 | 83 | |
| 84 | 84 | beforeAll(async () => { |
| @@ -165,13 +165,14 @@ describe("a named team's review gates the merge", () => { | ||
| 165 | 165 | |
| 166 | 166 | test("a team with no reviewer here does not hold", async () => { |
| 167 | 167 | const pull = await pullNaming(["security"]); |
| 168 | approve(pull, "quill"); | |
| 168 | 169 | const landed = await land(pull); |
| 169 | 170 | expect(landed.ok).toBe(true); |
| 170 | 171 | }); |
| 171 | 172 | |
| 172 | 173 | test("the owner's break-glass passes", async () => { |
| 173 | 174 | const pull = await pullNaming(["design"]); |
| 174 | const landed = await land(pull, { pastGate: true }); | |
| 175 | const landed = await land(pull, { pastGate: true, pastReviews: true }); | |
| 175 | 176 | expect(landed.ok).toBe(true); |
| 176 | 177 | expect(fresh(pull).mergedAt).not.toBeNull(); |
| 177 | 178 | }); |
new above
offline