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

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/16508-merge-gate-gap-16507-opened-by-trevato-a → main 0 commits fork

  • Merged. scout/16508-merge-gate-gap-16507-opened-by-trevato-a was merged into main as 89dd42a.

Review

Sign in to review this change.
12 files changed +161 −13
src/actions/pull_slices.ts +2 −1
@@ -12,6 +12,7 @@
1212 * slice is an ancestor of it: it lands only what that verdict covered.
1313 */
1414
15 import { reviewsDemanded } from "../crew/review.ts";
1516 import { strangerGuard } from "../crew/stranger.ts";
1617 import type { App } from "../app.ts";
1718 import { addComment as addIssueComment } from "../core/issues.ts";
@@ -93,7 +94,7 @@ export async function landSlice(
9394 // A stranger's slice is read against the guard as the whole pull is (#9575).
9495 const guarded = await strangerGuard(app, repo, live, upTo);
9596 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);
9798 if (!verdict.mergeable) return err("review_required");
9899 const check = checkSummary(app.db, live.id, status.headOid);
99100 if (
src/actions/pulls.ts +2 −1
@@ -21,6 +21,7 @@ import type { App } from "../app.ts";
2121 import { applyBoxPass, holdPass, landHeldPass, noteLandedDone, tickDelivery, type BoxPass } from "./box_pass.ts";
2222 import { landVision } from "../crew/vision.ts";
2323 import { crewRole } from "../core/agents.ts";
24 import { reviewsDemanded } from "../crew/review.ts";
2425 import {
2526 getIssueById,
2627 setAssignee,
@@ -607,7 +608,7 @@ export async function mergePull(
607608 ...(at === baseDir ? {} : { sealedFrom: at }),
608609 gate: {
609610 db: app.db,
610 requireReview: repo.requireReview,
611 requireReview: reviewsDemanded(app.db, repo),
611612 requireChecks: repo.requireChecks && opts.pastGate !== true,
612613 freshGreen: opts.pastGate !== true,
613614 // 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(
12821282 });
12831283 return ok({ mergeOid: carrier, carried: true });
12841284 }
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.
12871286 const teams = input.gate?.teamsHolding?.(status.headOid) ?? [];
12881287 if (teams.length > 0) return err({ kind: "team_review_required", teams });
1288 if (status.reviews !== null && !status.reviews.mergeable)
1289 return err("review_required");
12891290 // The check is the latest deploy run of *this* head. Read here, not
12901291 // handed in, for the same reason as the review verdict above. A landing
12911292 // 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(
24692469 ms: 0,
24702470 });
24712471 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 } : {});
24732474 if (!merged.ok) {
24742475 const why = mergeWhy(merged.error);
24752476 log.info("green but not landed", {
src/crew/review.ts +7
@@ -149,6 +149,13 @@ export const reviewsOff = (
149149 seated?: ReadonlyMap<string, readonly Agent[]>,
150150 ): boolean => !repo.requireReview && ((seated ?? reviewersForRepos(db, [repo.id])).get(repo.id)?.length ?? 0) === 0;
151151
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
152159 /** `reviewersOf` for many repositories at once, by repo; one with none is absent. */
153160 export const reviewersForRepos = (db: Database, repoIds: readonly string[]): Map<string, Agent[]> =>
154161 crewWithRole(db, repoIds, "reviewer");
src/http/api/pulls.ts +2 −1
@@ -7,6 +7,7 @@
77 * the rules live in `actions/pulls.ts` and its neighbours.
88 */
99
10 import { reviewsDemanded } from "../../crew/review.ts";
1011 import { pullWaitsFor } from "../../flow/waits_for.ts";
1112 import { parseAttestation } from "../../core/attestation.ts";
1213 import { reviewWords } from "../../actions/review_words.ts";
@@ -368,7 +369,7 @@ export function pullRoutes(router: Router<Handler>): void {
368369 // Include the diff so a reviewer (human or agent) reads the change without
369370 // a second round trip, and the review gate beside it so a client knows
370371 // 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) };
372373 const { status, diff } = await timePhaseAsync(ctx.request, "diff", async () => {
373374 const dir = await pullDirOf(ctx, found.repo, found.pull);
374375 return wantDiff === "0"
test/box-pass.test.ts +1 −1
@@ -81,7 +81,7 @@ describe("the reviewer's pass over the boxes", () => {
8181 return getPull(app.db, repo.id, opened.value.number)!;
8282 };
8383 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 });
8585 if (!merged.ok) throw new Error(String(merged.error));
8686 };
8787 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", () => {
507507
508508 test("a person's own merge still works on a held pull", async () => {
509509 const pull = await greenPull("by-hand", f.alice);
510 approve(pull, f.alice);
510 approve(pull, quill);
511511 hold(pull);
512512 const landed = await mergePull(f.app, f.alice, fresh(), pull, "squash");
513513 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", () => {
167167 expect(await landIfGreen(app, fresh(), moved)).toBe(false);
168168 expect(getPull(app.db, repo.id, pull.number)?.mergedAt).toBeNull();
169169
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);
172172 expect(await landIfGreen(app, fresh(), moved)).toBe(true);
173173 expect(getPull(app.db, repo.id, pull.number)?.mergedAt).not.toBeNull();
174174 });
test/stranger-pull.test.ts +1 −1
@@ -397,6 +397,6 @@ describe("a stranger's pull", () => {
397397 if (!opened.ok) throw new Error(String(opened.error));
398398 const pull = getPull(app.db, lib.id, opened.value.number)!;
399399 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);
401401 });
402402 });
test/team-merge-gate.test.ts +3 −2
@@ -78,7 +78,7 @@ describe("a named team's review gates the merge", () => {
7878 };
7979 const approve = (pull: Pull, by: string) =>
8080 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 } = {}) =>
8282 mergePull(app, alice, repo, fresh(pull), "merge", undefined, opts);
8383
8484 beforeAll(async () => {
@@ -165,13 +165,14 @@ describe("a named team's review gates the merge", () => {
165165
166166 test("a team with no reviewer here does not hold", async () => {
167167 const pull = await pullNaming(["security"]);
168 approve(pull, "quill");
168169 const landed = await land(pull);
169170 expect(landed.ok).toBe(true);
170171 });
171172
172173 test("the owner's break-glass passes", async () => {
173174 const pull = await pullNaming(["design"]);
174 const landed = await land(pull, { pastGate: true });
175 const landed = await land(pull, { pastGate: true, pastReviews: true });
175176 expect(landed.ok).toBe(true);
176177 expect(fresh(pull).mergedAt).not.toBeNull();
177178 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs