Public: anyone can read this; the crew can change it.

A pull that fails a rule test on today's main still landed: the feature-notes index was red on main four times in an hour #12698

Merged opened this in answer to

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

Merged

mason/forge:mason/12656-a-pull-that-fails-a-rule-test-on-today-s → main 5 commits fork 2 threads · 2 unresolved

  • Merged. mason/12656-a-pull-that-fails-a-rule-test-on-today-s was merged into main as d359945.

Review

Sign in to review this change.
12 files changed +142 −19
src/actions/pulls.ts +1
@@ -571,6 +571,7 @@ export async function mergePull(
571571 db: app.db,
572572 requireReview: repo.requireReview,
573573 requireChecks: repo.requireChecks && opts.pastGate !== true,
574 freshGreen: opts.pastGate !== true,

Test that pastGate === true leaves freshGreen false; nothing covers it now.

574575 // Only an app's first change, which the crew's gate decided (`crew/first_change.ts`); never a person's own call.
575576 ...(opts.pastReviews === true && !repo.requireReview ? { pastReviews: true } : {}),
576577 // A named team with a reviewer here reads this head first (#10080).
src/checks/stale.ts +4
@@ -5,6 +5,10 @@
55 * read, is not called stale — the reading says only what it knows.
66 */
77
8 /** What a person is told when a green check of an older base holds a merge (#12656). */
9 export const CHECK_STALE_REFUSAL =
10 "Its check ran on an older main: re-run the check, then merge. Nothing was merged.";
11
812 export function checkStale(
913 run: { readonly baseOid: string } | null,
1014 baseTip: string | undefined,
src/core/pulls.ts +14 −7
@@ -837,6 +837,13 @@ export interface ReviewGate {
837837 * Read live inside the merge like the review verdict. @default false
838838 */
839839 readonly requireChecks?: boolean;
840 /**
841 * A green check of an older base holds the landing even where checks are
842 * not required (#12656): what was checked is not the tree that lands, and a
843 * rule main gained since would be broken by it. Not asked by the owner's
844 * break-glass. @default false
845 */
846 readonly freshGreen?: boolean;
840847 /**
841848 * An app's first change lands on green whatever the reviewer asked
842849 * (`crew/first_change.ts`): the verdict is not read. @default false
@@ -1139,6 +1146,8 @@ export type MergePullError =
11391146 | "review_required"
11401147 /** The repo requires a passing deploy of this head, and there is none yet. */
11411148 | "checks_required"
1149 /** The head is green, but the check ran on an older base than main now is (#12656). */
1150 | "check_stale"
11421151 /** A team the pull's issue names has a reviewer here and has not approved this head (#10080). */
11431152 | { readonly kind: "team_review_required"; readonly teams: readonly string[] };
11441153
@@ -1250,13 +1259,11 @@ export async function mergePull(
12501259 // the proof a re-check would give (#3316). The head must still be green.
12511260 if (input.gate !== undefined) {
12521261 const check = checkSummary(input.gate.db, pull.id, status.headOid);
1253 if (
1254 checksHold(
1255 input.gate.requireChecks === true,
1256 check.state,
1257 input.expect === undefined && checkStale(check.run, status.baseOid),
1258 )
1259 )
1262 const stale = input.expect === undefined && checkStale(check.run, status.baseOid);
1263 // A green of an older base is told apart from no green: re-run, not wait.
1264 if (check.state === "ok" && stale && (input.gate.requireChecks === true || input.gate.freshGreen === true))
outdated

A stale green returns the same checks_required as a missing check. Give the stale case its own reason so the person is told to re-run the check, not left guessing.

1265 return err("check_stale");
1266 if (checksHold(input.gate.requireChecks === true, check.state, stale))
12601267 return err("checks_required");
12611268 }
12621269 // A kept fork lands its combined branch, brought up to the base first:
src/fleet/verbs.ts +2
@@ -11,6 +11,7 @@
1111 */
1212
1313 import { NOT_A_MEMBER } from "../core/roles.ts";
14 import { CHECK_STALE_REFUSAL } from "../checks/stale.ts";
1415 import { AGENT_NOT_ON_CREW } from "../actions/crew_limit_words.ts";
1516 import type { App } from "../app.ts";
1617 import {
@@ -124,6 +125,7 @@ const WORDS: Record<string, string> = {
124125 assignee_cannot_read: "the assignee cannot read this repository",
125126 agent_not_on_crew: AGENT_NOT_ON_CREW,
126127 already_merged: "a merged pull stays closed",
128 check_stale: CHECK_STALE_REFUSAL,
127129 reason_required: "a bug closed without a fix says why: add a reason",
128130 invalid_note: "a reason is one sentence of at most 300 characters",
129131 finished: "already finished",
src/http/api.ts +2 −1
@@ -285,7 +285,7 @@ import {
285285 type ListRunsOptions,
286286 type Run,
287287 } from "../core/runs.ts";
288 import { checkStale } from "../checks/stale.ts";
288 import { CHECK_STALE_REFUSAL, checkStale } from "../checks/stale.ts";
289289 import { resolveOid } from "../git/merge.ts";
290290 import { readForgeUsage, readUsage, usageDays, USAGE_DAYS_MAX } from "../actions/usage.ts";
291291 import { readReturns, reportSpend, RETURNS_DAYS } from "../actions/returns.ts";
@@ -687,6 +687,7 @@ export function originAware(error: string, status = 422): Response {
687687 422,
688688 "a bug closed without a fix says why: send `note` (one sentence, at most 300 characters)",
689689 );
690 if (error === "check_stale") return apiError("check_stale", 409, CHECK_STALE_REFUSAL);
690691 if (error === "planner_paused") return apiError("planner_paused", 409, PAUSE_REFUSAL);
691692 if (error === "mirrored")
692693 return apiError(
src/http/respond.ts +1
@@ -56,6 +56,7 @@ const HINTS_BY_CODE: Record<string, string> = {
5656 held: "This pull is on hold: ask the person who placed the hold to release it, then repeat the call.",
5757 review_required: "This pull needs an approving review first: ask a reviewer, read GET its reviews to see what is missing, then repeat the call.",
5858 checks_required: "The pull's head needs a passing check or deploy first: push a fix or wait for the check, then repeat the call.",
59 check_stale: "The pull's check ran on an older main: re-run the check on the current head, then repeat the call.",
5960 invalid: "A field failed validation: fix the field the message names (see the route's body in GET /contract/openapi.json) and send the whole body again.",
6061 not_admin: "This route is for administrators only: use an administrator's token; a different scope will not help.",
6162 forbidden: "Your token's scope or your role on this resource does not allow this: a read-only (`ro`) token cannot write, so mint an `rw` token, or ask an owner for the role the message names.",
src/tui/screens/common.ts +2
@@ -7,6 +7,7 @@
77 * of them. Nothing here knows about repos or issues — that is the point.
88 */
99
10 import { CHECK_STALE_REFUSAL } from "../../checks/stale.ts";
1011 import { PASSWORD_TOO_LONG } from "../../core/hash-gate.ts";
1112 import { refusalText } from "../../http/refusal.ts";
1213 import { fullSentence, isFullError, noteFullHere } from "../../kernel/full_disk.ts";
@@ -1059,6 +1060,7 @@ const MESSAGES: Record<string, string> = {
10591060 // Apps, runs and the crew (docs/APPS.md). `checks_required` is the merge
10601061 // gate refusing, so like `review_required` it names what to do about it.
10611062 checks_required: "this needs a passing deploy of its current head first",
1063 check_stale: CHECK_STALE_REFUSAL,
10621064 invalid_slug: "a slug is lowercase letters, digits and single hyphens",
10631065 slug_taken: "that slug is taken by another app",
10641066 already_app: "this repository is already an app",
src/web/pages/pull.ts +2 −1
@@ -50,7 +50,7 @@ import {
5050 import { isProposed, pullMayRun } from "../../core/pull_trust.ts";
5151 import { checkWords } from "../../checks/summary.ts";
5252 import { runningProgress } from "../../checks/progress.ts";
53 import { checkStale, checksHold } from "../../checks/stale.ts";
53 import { CHECK_STALE_REFUSAL, checkStale, checksHold } from "../../checks/stale.ts";
5454 import { checkSummary, type CheckSummary } from "../../core/runs.ts";
5555 import {
5656 anchorKey,
@@ -1356,6 +1356,7 @@ export function mergeFailure(error: AccessError | MergePullError): string {
13561356 // dismissed a second later, or a reviewer asked for changes in between.
13571357 if (error === "review_required")
13581358 return "This pull request needs an approving review of its current head, and does not have one. Nothing was merged.";
1359 if (error === "check_stale") return CHECK_STALE_REFUSAL;
13591360 if (error === "checks_required")
13601361 return "This pull request needs a passing deploy of its current head, and does not have one yet. Nothing was merged.";
13611362 if (error === "forbidden" || error === "unauthorized")
test/landsim.test.ts +10
@@ -44,6 +44,7 @@ import {
4444 type Scenario,
4545 type Violation,
4646 type World,
47 ruleAfterCheck,
4748 } from "./support/landsim.ts";
4849
4950 /**
@@ -116,6 +117,15 @@ describe("the landing simulator", () => {
116117 });
117118 });
118119
120 // #12656: a green of an older base does not land on a main that gained a rule since,
121 // whether or not the repository requires checks.
122 test("a pull checked before main gained a rule is not landed on it (#12656)", async () => {
123 const out = await runScenario(world, ruleAfterCheck());
124 expect(out.violations.map((v) => `${v.cls}: ${v.message}`)).toEqual([]);
125 expect(out.landed).toBe(0);
126 expect([...out.refusals.keys()]).toContain("land: check_stale");
127 }, 60_000);
128
119129 // #12104: when two changes add lines at the same place, a kept fork's landing keeps both sides.
120130 test(
121131 "two fork pulls that add lines at the end of one file both land, both sides present (#12104)",
test/merge-stale-green.test.ts +36 −3
@@ -12,6 +12,9 @@ import { getPull } from "../src/core/pulls.ts";
1212 import { getRepo } from "../src/core/repos.ts";
1313 import { getUserByName } from "../src/core/users.ts";
1414 import { mergeable } from "../src/git/merge.ts";
15 import { errorText } from "../src/tui/screens/common.ts";
16 import { mergeFailure } from "../src/web/pages/pull.ts";
17 import { CHECK_STALE_REFUSAL } from "../src/checks/stale.ts";
1518 import { repoDir } from "../src/git/repos.ts";
1619
1720 const gitEnv = gitClientEnv();
@@ -133,7 +136,7 @@ test("the API merge refuses a green checked against an older base when checks ar
133136
134137 const refused = await call("/pulls/1/merge", { method: "POST", body: "{}" });
135138 expect(refused.status).toBe(409);
136 expect(((await refused.json()) as any).error).toBe("checks_required");
139 expect(((await refused.json()) as any).error).toBe("check_stale");
137140
138141 // A green against the current tip lands.
139142 setRunBase(app.db, run.id, await gitOk(["rev-parse", "main"]));
@@ -217,7 +220,7 @@ test("the MCP merge tool refuses a green checked against an older base, and land
217220 const tip = await moveMain("before-mcp.txt");
218221
219222 const refused = await mcpMerge(pull.number);
220 expect(refused.error).toStartWith("checks_required");
223 expect(refused.error).toContain("check ran on an older main: re-run the check");
221224 expect(merged(pull.number)).toBeNull();
222225 expect(await gitOk(["ls-remote", "origin", "refs/heads/main"])).toStartWith(tip);
223226
@@ -240,7 +243,7 @@ test("the merge queue's gated landing (expect) is not held by a green on an olde
240243 // The same pull, without the queue's guard, is still refused.
241244 const held = await mergePull(app, dev, repo, p, "merge");
242245 expect(held.ok).toBe(false);
243 if (!held.ok) expect(held.error).toBe("checks_required");
246 if (!held.ok) expect(held.error).toBe("check_stale");
244247
245248 const landed = await mergePull(app, dev, repo, p, "merge", {
246249 baseOid: tip,
@@ -248,3 +251,33 @@ test("the merge queue's gated landing (expect) is not held by a green on an olde
248251 });
249252 expect(landed.ok).toBe(true);
250253 });
254
255 test("a stale green is refused in words a person can act on, on the API, the page and the terminal", async () => {
256 const pull = await greenPull("by-words");
257 await moveMain("before-words.txt");
258
259 const refused = await call(`/pulls/${pull.number}/merge`, { method: "POST", body: "{}" });
260 expect(refused.status).toBe(409);
261 const body = (await refused.json()) as { error: string; message: string };
262 expect(body.error).toBe("check_stale");
263 expect(body.message).toBe(CHECK_STALE_REFUSAL);
264 expect(CHECK_STALE_REFUSAL).toContain("ran on an older main: re-run the check, then merge");
265
266 expect(mergeFailure("check_stale")).toBe(CHECK_STALE_REFUSAL);
267 expect(errorText("check_stale")).toBe(CHECK_STALE_REFUSAL);
268 });
269
270 test("the owner's break-glass is not asked for a fresh green: a stale green still lands past the gate", async () => {
271 const pull = await greenPull("by-glass");
272 await moveMain("before-glass.txt");
273 const repo = getRepo(app.db, "dev", "app")!;
274 const p = getPull(app.db, repo.id, pull.number)!;
275 const dev = getUserByName(app.db, "dev")!;
276
277 const held = await mergePull(app, dev, repo, p, "merge");
278 expect(held.ok).toBe(false);
279 if (!held.ok) expect(held.error).toBe("check_stale");
280
281 const glass = await mergePull(app, dev, repo, p, "merge", undefined, { pastGate: true });
282 expect(glass.ok).toBe(true);
283 });
test/stale-green.test.ts +1 −1
@@ -143,7 +143,7 @@ describe("stale greens land in one candidate", () => {
143143 try {
144144 const merged = await mergePull(app, alice, fresh(), pull, "squash");
145145 expect(merged.ok).toBe(false);
146 if (!merged.ok) expect(merged.error).toBe("checks_required");
146 if (!merged.ok) expect(merged.error).toBe("check_stale");
147147 } finally {
148148 app.db.query("UPDATE repo SET autonomy = 'autonomous' WHERE id = ?1").run(repo.id);
149149 }
test/support/landsim.ts +67 −6
@@ -82,7 +82,8 @@ export type Edit =
8282 | { k: "rename"; file: string; to: string }
8383 | { k: "delete"; file: string }
8484 | { k: "addfile"; file: string; tag: string; n: number }
85 | { k: "mode"; file: string };
85 | { k: "mode"; file: string }
86 | { k: "write"; file: string; content: string };
8687
8788 export interface PullSpec {
8889 id: number;
@@ -113,6 +114,8 @@ export interface Scenario {
113114 files: Record<string, string>;
114115 pulls: PullSpec[];
115116 steps: Step[];
117 /** The repository's `require_checks` (the default is on; production runs with it off). */
118 requireChecks?: boolean;
116119 }
117120
118121 // ---------------------------------------------------------------------------
@@ -319,6 +322,8 @@ const editWords = (e: Edit): string => {
319322 return `add ${e.file} with ${e.n} line(s) [${e.tag}]`;
320323 case "mode":
321324 return `chmod +x ${e.file}`;
325 case "write":
326 return `write ${e.file}`;
322327 }
323328 };
324329
@@ -385,6 +390,11 @@ export function applyEdits(base: Tree, edits: readonly Edit[]): FileWrite[] {
385390 const touched = new Set<string>();
386391 for (const e of edits) {
387392 const f = cur.get(e.file);
393 if (e.k === "write") {
394 cur.set(e.file, { content: e.content, mode: cur.get(e.file)?.mode ?? "100644" });
395 touched.add(e.file);
396 continue;
397 }
388398 if (e.k === "addfile") {
389399 if (cur.has(e.file)) continue;
390400 const lines = Array.from({ length: e.n }, (_, i) =>
@@ -534,6 +544,22 @@ async function trackedLines(dir: string, rev: string): Promise<Set<string>> {
534544 return out;
535545 }
536546
547 /**
548 * The always-run rule the simulator stands in for `test/feature-orphans.test.ts`: once a tree holds
549 * `notes/INDEX.md`, every other `notes/*.md` is named in it.
550 * The files that break it, none when it holds.
551 */
552 export async function orphanNotes(dir: string, rev: string): Promise<string[]> {
553 const tree = await readTree(dir, rev);
554 const index = tree.get("notes/INDEX.md");
555 if (index === undefined) return [];
556 return [...tree.keys()].filter((f) => /^notes\/[^/]+\.md$/.test(f) && f !== "notes/INDEX.md" && !index.content.includes(f.slice(6)));
557 }
558
559 /** What the stub check and gate judge a tree red for: conflict markers, or a rule it breaks. */
560 const isRed = async (dir: string, rev: string): Promise<boolean> =>
561 (await hasMarkers(dir, rev)) || (await orphanNotes(dir, rev)).length > 0;
562
537563 const hasMarkers = async (dir: string, rev: string): Promise<boolean> =>
538564 (await git(dir, ["grep", "-I", "-q", "-E", "^(<<<<<<<|>>>>>>>) ", rev, "--"]))
539565 .code === 0;
@@ -800,7 +826,7 @@ export async function createWorld(): Promise<World> {
800826 for (const l of chain.out.split("\n"))
801827 if (/^[0-9a-f]{40}$/.test(l)) trees.add(l);
802828 if (process.env["LANDSIM_DEBUG"]) console.log("GATE", commitOid, JSON.stringify(chain.out), chain.err);
803 const red = await hasMarkers(dir, commitOid);
829 const red = await isRed(dir, commitOid);
804830 return red
805831 ? { ok: false, error: "red", lines: ["(fail) conflict markers"] }
806832 : { ok: true, error: "", lines: [] };
@@ -896,9 +922,9 @@ export class Sim {
896922 if (!made.ok) throw new Error(`createRepo: ${String(made.error)}`);
897923 app.db
898924 .query(
899 "UPDATE repo SET require_review = 1, require_checks = 1, autonomy = 'autonomous', autonomy_by = ?2 WHERE id = ?1",
925 "UPDATE repo SET require_review = 1, require_checks = ?3, autonomy = 'autonomous', autonomy_by = ?2 WHERE id = ?1",
900926 )
901 .run(made.value.id, world.alice.id);
927 .run(made.value.id, world.alice.id, this.sc.requireChecks === false ? 0 : 1);
902928 this.repo = getRepoById(app.db, made.value.id)!;
903929 this.dir = repoDir(app.reposRoot, this.repo.ownerName, this.repo.name);
904930 const seeded = await commitToBranch(app, world.alice, this.repo, {
@@ -1144,6 +1170,11 @@ export class Sim {
11441170 return getPull(this.app.db, this.repo.id, st.number)!;
11451171 }
11461172
1173 /** Whether `tip` is the commit a pull's landing made. */
1174 private pushedByLanding(tip: string): boolean {
1175 return [...this.pulls.values()].some((st) => this.row(st).mergedOid === tip);
1176 }
1177
11471178 private isOpen(st: PullState): boolean {
11481179 const row = this.row(st);
11491180 return row.state === "open" && row.mergedAt === null;
@@ -1181,11 +1212,11 @@ export class Sim {
11811212 source = combined.oid;
11821213 builtOn = combined.baseOid === "" ? status.baseOid : combined.baseOid;
11831214 tree = (await git(this.dir, ["rev-parse", `${source}^{tree}`])).out.trim();
1184 ok = !(await hasMarkers(this.dir, tree));
1215 ok = !(await isRed(this.dir, tree));
11851216 } else {
11861217 const merged = await mergeable(this.dir, status.baseOid, status.headOid);
11871218 tree = merged.ok ? merged.value.treeOid : "";
1188 ok = merged.ok && !(await hasMarkers(this.dir, tree));
1219 ok = merged.ok && !(await isRed(this.dir, tree));
11891220 }
11901221 const run = createRun(app.db, {
11911222 repoId: this.repo.id,
@@ -1310,6 +1341,15 @@ export class Sim {
13101341 cls: "noMarkersInMain",
13111342 message: `main at ${tip.slice(0, 10)} holds conflict markers`,
13121343 });
1344 // A landing must not turn main red on a rule it already had;
1345 // a tip a person pushed that breaks one is theirs.
1346 const orphans = await orphanNotes(this.dir, tip);
1347 if (orphans.length > 0 && this.pushedByLanding(tip))
1348 this.violate({
1349 invariant: "ruleHoldsOnMain",
1350 cls: "ruleHoldsOnMain",
1351 message: `main at ${tip.slice(0, 10)} breaks the notes rule after a landing: ${orphans.join(", ")}`,
1352 });
13131353 }
13141354
13151355 private async auditLanding(st: PullState, row: Pull): Promise<void> {
@@ -1562,6 +1602,27 @@ export function additiveForkPair(): Scenario {
15621602 return pairScript(1, files, [mk(1), mk(2)]);
15631603 }
15641604
1605 /**
1606 * #12656: a pull is checked and approved while main has no rule; main then gains the rule
1607 * (`notes/INDEX.md`, an index every note must be named in); the pull adds a note the index
1608 * does not name. The forge's check is of the older base, and the repository does not require
1609 * checks (production's setting): a person lands it, and main would be red on the rule.
1610 */
1611 export function ruleAfterCheck(): Scenario {
1612 const files = { "README.md": "# Project ~r0.h\n- item ~r0.0\n", "notes/a.md": "A note.\n" };
1613 const pulls: PullSpec[] = [
1614 { id: 1, who: "dave", fork: false, point: 0, edits: [{ k: "write", file: "notes/b.md", content: "Another note.\n" }] },
1615 ];
1616 const steps: Step[] = [
1617 { op: "open", p: 1 },
1618 { op: "check", p: 1 },
1619 { op: "approve", p: 1 },
1620 { op: "main", edits: [{ k: "write", file: "notes/INDEX.md", content: "- a.md\n" }] },
1621 { op: "land", p: 1, how: "squash" },
1622 ];
1623 return { seed: 12656, files, pulls, steps, requireChecks: false };
1624 }
1625
15651626 function pairScript(
15661627 seed: number,
15671628 files: Record<string, string>,

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs