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

A stale green holds back the inbox's look ask and disables the merge button (after #1179) #2274

Parked opened this
Closed without merging

scout/forge:scout/2028-a-stale-green-holds-back-the-inbox-s-loo → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +164 −10
src/crew/stale_asks.ts +28 −5
@@ -18,6 +18,7 @@ import { joinMany, pullStatus } from "../core/pulls.ts";
1818 import { getIssuesByIds } from "../core/issues.ts";
1919 import { getRepoById } from "../core/repos.ts";
2020 import { latestRunsForIssues } from "../core/runs.ts";
21 import { checkStale } from "../checks/stale.ts";
2122 import { resolveOid } from "../git/merge.ts";
2223 import { repoDir } from "../git/repos.ts";
2324
@@ -27,6 +28,8 @@ interface Known {
2728 /** The pull's head the answer was taken at, as the database names it. */
2829 readonly head: string;
2930 readonly stale: boolean;
31 /** The base's tip when it was last read; empty when it could not be. */
32 readonly base: string;
3033 /** The files that stop it merging; 0 when it merges or is simply already in. */
3134 readonly conflicts: number;
3235 /** When a sweep last saw it go from not merging to merging: its ask is new from then. */
@@ -51,6 +54,21 @@ export function isStalePull(
5154 return known !== undefined && known.stale && known.head === headOid;
5255 }
5356
57 /**
58 * Whether this pull's green was checked against an older tip of its base, as
59 * the last sweep read that tip (#1179's `checkStale`). A tip not yet read is
60 * no answer, and the green stands.
61 */
62 export function staleGreen(
63 db: Database,
64 pull: { readonly id: string; readonly headOid: string },
65 check: { readonly baseOid: string } | null | undefined,
66 ): boolean {
67 const known = knownByDb.get(db)?.get(pull.id);
68 if (known === undefined || known.head !== pull.headOid) return false;
69 return checkStale(check ?? null, known.base);
70 }
71
5472 /**
5573 * How many files stop this pull, at this head, from merging, as the last
5674 * sweep found it: 0 when it merges, or when no sweep has answered yet.
@@ -96,7 +114,6 @@ export async function sweepStaleAsks(app: App): Promise<void> {
96114 .query<{ id: string }, [number]>(
97115 `SELECT issue.id AS id FROM issue JOIN repo ON repo.id = issue.repo_id
98116 WHERE issue.kind = 'pull' AND issue.state = 'open'
99 AND repo.autonomy <> 'autonomous'
100117 ORDER BY issue.updated_at DESC LIMIT ?1`,
101118 )
102119 .all(SWEEP_LIMIT)
@@ -115,10 +132,14 @@ export async function sweepStaleAsks(app: App): Promise<void> {
115132 (await resolveOid(dir, `refs/heads/${pull.baseBranch}`)) ?? "";
116133 const between = `${pull.headOid}:${base}`;
117134 if (known.get(pull.id)?.between === between) continue;
118 const status = await pullStatus(dir, pull);
135 // An autonomous repository settles its own mergeability; only the
136 // base tip is read there, for the stale green.
137 const status =
138 repo.autonomy === "autonomous" ? undefined : await pullStatus(dir, pull);
119139 const stale =
120 status.ahead === 0 ||
121 (!status.mergeable && status.conflicts.length > 0);
140 status !== undefined &&
141 (status.ahead === 0 ||
142 (!status.mergeable && status.conflicts.length > 0));
122143 const was = known.get(pull.id);
123144 const mergesSince =
124145 !stale && was?.stale === true && was.head === pull.headOid
@@ -130,7 +151,9 @@ export async function sweepStaleAsks(app: App): Promise<void> {
130151 between,
131152 head: pull.headOid,
132153 stale,
133 conflicts: status.mergeable ? 0 : status.conflicts.length,
154 base,
155 conflicts:
156 status === undefined || status.mergeable ? 0 : status.conflicts.length,
134157 ...(mergesSince === undefined ? {} : { mergesSince }),
135158 });
136159 } catch (error) {
src/web/pages/pull.ts +6 −2
@@ -490,9 +490,13 @@ function mergeBox(
490490 // The check gate, `mergePull`'s `checks_required`: the deploy of the
491491 // commit that is head right now must have passed. Read live here so the
492492 // button is honest — the refusal itself happens in the merge.
493 if (repo.requireChecks && check.state !== "ok") {
493 if (repo.requireChecks && (check.state !== "ok" || stale)) {
494494 const why =
495 check.state === "failed"
495 check.state === "ok"
496 ? html`This head was checked against an older
497 <span class="mono">${pull.baseBranch}</span>; checking again. The
498 merge unlocks when the check passes.`
499 : check.state === "failed"
496500 ? html`The deploy of this head failed. Push a fix — a passing deploy is
497501 required before merging.`
498502 : check.state === "running" || check.state === "queued"
src/web/studio/asks.ts +6 −3
@@ -58,7 +58,7 @@ import {
5858 import { breakerParked, WAITING_NOTES } from "../../crew/breaker.ts";
5959 import { CHECK_FAILED_LIMIT } from "../../crew/dispatch.ts";
6060 import { handoverLines, handoverOf, type Handover } from "../../crew/guard.ts";
61 import { conflictsOf, isStalePull, mergesAgainSince } from "../../crew/stale_asks.ts";
61 import { conflictsOf, isStalePull, mergesAgainSince, staleGreen } from "../../crew/stale_asks.ts";
6262 import { reviewAttempts, waitsForPerson, waitsForReviewer, type ReviewWait } from "../../crew/review.ts";
6363 import type { Ctx } from "../../http/context.ts";
6464 import { repoBase } from "../chrome.ts";
@@ -379,7 +379,9 @@ function askCandidatesFor(
379379 repo.autonomy !== "autonomous" &&
380380 // A green pull that no longer merges as it stands cannot be shipped:
381381 // bringing it up to date is work, not a person's decision.
382 !isStalePull(db, pull.id, pull.headOid)
382 !isStalePull(db, pull.id, pull.headOid) &&
383 // A green checked against an older base is a check still to come.
384 !staleGreen(db, pull, check)
383385 ) {
384386 // An agent's green pull waits for its reviewer first: it is the
385387 // crew's to wait on, and reaches the owner with the verdict (#1602).
@@ -403,7 +405,8 @@ function askCandidatesFor(
403405 } else if (
404406 check?.state === "ok" &&
405407 repo.autonomy === "autonomous" &&
406 !isStalePull(db, pull.id, pull.headOid)
408 !isStalePull(db, pull.id, pull.headOid) &&
409 !staleGreen(db, pull, check)
407410 ) {
408411 // An autonomous repository lands its own green pulls once reviewed;
409412 // an agent's change with no reviewer, or whose review lapsed, waits
test/check-stale-gate.test.ts added +124
@@ -0,0 +1,124 @@
1 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
2 import { mkdtemp, rm } from "node:fs/promises";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { openPull } from "../src/actions/pulls.ts";
6 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
7 import { createApp, type App } from "../src/app.ts";
8 import { claimRun, createRun, finishRun, setRunBase } from "../src/core/runs.ts";
9 import { getPull, type Pull } from "../src/core/pulls.ts";
10 import { updateRepoSettings, type Repo } from "../src/core/repos.ts";
11 import { createSession } from "../src/core/sessions.ts";
12 import { createUser, type User } from "../src/core/users.ts";
13 import { sweepStaleAsks } from "../src/crew/stale_asks.ts";
14 import { resolveOid } from "../src/git/merge.ts";
15 import { repoDir } from "../src/git/repos.ts";
16 import { serve, type Servers } from "../src/http/server.ts";
17 import { askLinesFor } from "../src/web/studio/asks.ts";
18 import { quietLogs } from "./helpers/globals.ts";
19
20 quietLogs();
21
22 describe("a green checked against an older main waits for its re-check", () => {
23 let dataDir: string;
24 let app: App;
25 let servers: Servers;
26 let alice: User;
27 let repo: Repo;
28 let pull: Pull;
29 let cookie: string;
30
31 const put = async (branch: string, path: string, content: string) => {
32 const done = await commitToBranch(app, alice, repo, {
33 branch,
34 message: `edit ${path}`,
35 files: [{ path, content }],
36 });
37 if (!done.ok) throw new Error(JSON.stringify(done.error));
38 };
39 const mainTip = async () =>
40 (await resolveOid(repoDir(app.reposRoot, "alice", "lib"), "refs/heads/main"))!;
41 const green = async (baseOid: string) => {
42 const run = createRun(app.db, {
43 repoId: repo.id,
44 kind: "check",
45 issueId: pull.id,
46 env: "pull-1",
47 commitOid: pull.headOid,
48 });
49 claimRun(app.db, run.id);
50 finishRun(app.db, run.id, { state: "ok", summary: "all green" });
51 setRunBase(app.db, run.id, baseOid);
52 };
53 const looks = () =>
54 askLinesFor(app.db, alice).some((r) => r.items.includes(`look:${pull.id}`));
55 const mergeButton = async () => {
56 const res = await fetch(`${servers.url}/alice/lib/pulls/${pull.number}/files`, {
57 headers: { cookie },
58 });
59 const page = await res.text();
60 const at = page.indexOf("Merge pull request");
61 expect(at).toBeGreaterThan(-1);
62 return { page, tag: page.slice(page.lastIndexOf("<button", at), at) };
63 };
64
65 beforeAll(async () => {
66 dataDir = await mkdtemp(join(tmpdir(), "forge-stale-gate-"));
67 app = createApp({ dataDir, port: 0, host: "forge.test" });
68 servers = await serve(app);
69 const made = await createUser(app.db, {
70 trusted: true,
71 username: "alice",
72 email: "[email protected]",
73 password: "a-good-passphrase",
74 adminIfFirst: true,
75 });
76 if (!made.ok) throw new Error(made.error);
77 alice = made.value;
78 cookie = `forge_session=${createSession(app.db, alice.id).token}`;
79 const r = await createRepo(app, alice, { name: "lib", visibility: "public" });
80 if (!r.ok) throw new Error(String(r.error));
81 repo = updateRepoSettings(app.db, r.value, { requireChecks: true });
82 await put("main", "code.txt", "top\nbase\n");
83 await put("feat", "feat.txt", "feature\n");
84 const opened = await openPull(app, alice, repo, {
85 title: "Feat",
86 baseBranch: "main",
87 headBranch: "feat",
88 });
89 if (!opened.ok) throw new Error(String(opened.error));
90 pull = getPull(app.db, repo.id, opened.value.number)!;
91 await green(await mainTip());
92 await sweepStaleAsks(app);
93 }, 120_000);
94
95 afterAll(async () => {
96 servers.stop();
97 app.close();
98 await rm(dataDir, { recursive: true, force: true });
99 });
100
101 test("a green on the current main asks for a look and offers the merge", async () => {
102 expect(looks()).toBe(true);
103 const { tag } = await mergeButton();
104 expect(tag).not.toContain("disabled");
105 });
106
107 test("main moves: no look ask, and the merge button is disabled", async () => {
108 await put("main", "other.txt", "moved\n");
109 await sweepStaleAsks(app);
110 expect(looks()).toBe(false);
111 const { page, tag } = await mergeButton();
112 expect(tag).toContain("disabled");
113 expect(page).toContain("Waiting on checks");
114 expect(page).toContain("checked against an older");
115 });
116
117 test("a re-check green against the new main brings both back", async () => {
118 await green(await mainTip());
119 await sweepStaleAsks(app);
120 expect(looks()).toBe(true);
121 const { tag } = await mergeButton();
122 expect(tag).not.toContain("disabled");
123 });
124 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs