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

A pull whose green was checked on an older main reads "checked against an older main" on its page, in the API and in the MCP #2238

Closed opened this
Closed without merging

scout/forge:scout/2237-a-pull-whose-green-was-checked-on-an-old → main 3 commits fork 2 threads · 2 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
8 files changed +161 −7
src/checks/stale.ts added +19
@@ -0,0 +1,19 @@
1 /**
2 * Whether a green is on today's base. A check records the base tip it was
3 * merged with (`run.baseOid`, #1810); when the base has moved on since, the
4 * green was earned against an older tree. A run that recorded none (made
5 * before #1810, or a default-branch push) and a tip that could not be read
6 * are never called stale: nothing is guessed. Pure; the caller reads the
7 * tip once per request.
8 */
9
10 import type { Run } from "../core/runs.ts";
11
12 export function checkStale(
13 run: Pick<Run, "baseOid"> | null,
14 baseTipOid: string | undefined,
15 ): boolean {
16 if (run === null || run.baseOid === "") return false;
17 if (baseTipOid === undefined || baseTipOid === "") return false;
18 return run.baseOid !== baseTipOid;
19 }
src/http/api.ts +8
@@ -20,6 +20,7 @@ import { DOMAINS } from "../kernel/domains.ts";
2020 import { SOURCES } from "../boards/sources.ts";
2121 import { bodyCapFor, readBounded } from "./limits.ts";
2222 import { askCheck } from "../checks/again.ts";
23 import { checkStale } from "../checks/stale.ts";
2324 import { mayView, mayViewId, sealedScope } from "../core/sealed.ts";
2425 import { changeCapacity, listCapacity } from "../actions/capacity.ts";
2526 import { changeLimit, listLimits } from "../actions/limits.ts";
@@ -320,6 +321,7 @@ import {
320321 } from "../core/issues.ts";
321322 import type { Closing } from "../actions/issues.ts";
322323 import {
324 checkSummary,
323325 checksForHead,
324326 getRun,
325327 isTerminal,
@@ -3228,9 +3230,15 @@ export function apiRoutes(router: Router<Handler>): void {
32283230 { db: ctx.app.db, requireReview: found.repo.requireReview },
32293231 );
32303232 const hold = getHold(ctx.app.db, found.pull.id);
3233 const check = checkSummary(ctx.app.db, found.pull.id, status.headOid);
32313234 return json({
32323235 ...withLabels(ctx, found.pull),
32333236 variantSiblings: variantSiblings(ctx.app.db, [found.pull])[0] ?? [],
3237 // Only a pull that can still land has a green worth qualifying.
outdated

This is computed for merged and closed pulls as well, so every merged pull reads checkStale: true once main moves. Gate it like mergeable just below (open and not merged), and do the same in the MCP pull tool.

3238 checkStale:
3239 found.pull.state === "open" &&
3240 found.pull.mergedAt === null &&
3241 checkStale(check.run, status.baseOid),
32343242 ...(found.pull.state === "open" && found.pull.mergedAt === null
32353243 ? { mergeable: status.mergeable, conflicts: status.conflicts }
32363244 : {}),
src/http/mcp.ts +10 −5
@@ -92,6 +92,8 @@ import { waitsForPerson } from "../crew/review.ts";
9292 import { attachLook } from "../actions/attach_look.ts";
9393 import { lookUrl } from "../core/looks.ts";
9494 import { pullProposalOf } from "../core/proposal_token.ts";
95 import { checkStale } from "../checks/stale.ts";
96 import { resolveOid } from "../git/merge.ts";
9597 import { getPull, pullDir, pullMergeState } from "../core/pulls.ts";
9698 import { repoGraph } from "../core/dependencies.ts";
9799 import { pillarView } from "../core/pillars.ts";
@@ -502,7 +504,7 @@ const TOOLS: readonly Tool[] = [
502504 {
503505 name: "pull",
504506 description:
505 "Read a pull: its title, state and head and base, and for an open pull whether it merges into the base's current head (`mergeable`) and the paths that stop it (`conflicts`), `proposal` ({child, origin}, or null) when a child's proposal token opened it, and `check`: the latest check's state and the base tip it was merged with (`baseOid`, empty when it merged nothing). `mergeable` and `conflicts` are the same fields as GET /api/v1/repos/:owner/:name/pulls/:number; `check` is the latest check's `state` and `run.baseOid` from GET …/pulls/:number/checks.",
507 "Read a pull: its title, state and head and base, and for an open pull whether it merges into the base's current head (`mergeable`) and the paths that stop it (`conflicts`), `proposal` ({child, origin}, or null) when a child's proposal token opened it, and `check`: the latest check's state and the base tip it was merged with (`baseOid`, empty when it merged nothing), and `checkStale`: true when that tip is no longer the base's, so a green is from an older base. `mergeable` and `conflicts` are the same fields as GET /api/v1/repos/:owner/:name/pulls/:number; `check` is the latest check's `state` and `run.baseOid` from GET …/pulls/:number/checks.",
506508 inputSchema: {
507509 type: "object",
508510 properties: {
@@ -522,11 +524,10 @@ const TOOLS: readonly Tool[] = [
522524 : undefined;
523525 if (pull === undefined || !mayView(ctx.app.db, repo, pull, user))
524526 return bad("no_such_pull", `no pull #${String(n)}`);
525 const state = await pullMergeState(
526 await pullDir(ctx.app.db, ctx.app.reposRoot, repo, pull),
527 pull,
528 );
527 const dir = await pullDir(ctx.app.db, ctx.app.reposRoot, repo, pull);
528 const state = await pullMergeState(dir, pull);
529529 const check = checkSummary(ctx.app.db, pull.id, pull.headOid);
530 const tip = await resolveOid(dir, `refs/heads/${pull.baseBranch}`);
530531 return good({
531532 number: pull.number,
532533 title: pull.title,
@@ -535,6 +536,10 @@ const TOOLS: readonly Tool[] = [
535536 head: pull.headBranch,
536537 proposal: pullProposalOf(ctx.app.db, pull.id) ?? null,
537538 check: { state: check.state, baseOid: check.run?.baseOid ?? "" },
539 checkStale:
540 pull.state === "open" &&
541 pull.mergedAt === null &&
542 checkStale(check.run, tip),
538543 ...(state ?? {}),
539544 });
540545 },
src/tui/screens/pull.ts +23 −2
@@ -46,6 +46,8 @@ import {
4646 type EnvState,
4747 type EnvStatus, envsFor } from "../../apps/runtime.ts";
4848 import { proposedByText } from "../../core/proposal_token.ts";
49 import { checkStale } from "../../checks/stale.ts";
50 import { resolveOid } from "../../git/merge.ts";
4951 import { getAppByRepo } from "../../core/apps.ts";
5052 import { getIssue, type Issue } from "../../core/issues.ts";
5153 import { isProposed, pullMayRun } from "../../core/pull_trust.ts";
@@ -227,6 +229,8 @@ export function pullScreen(
227229 let check: CheckSummary = NO_CHECK;
228230 /** The pull's base branch, to name what the check was merged with. */
229231 let checkBase = "";
232 /** The base branch's tip now, to say when the green was on an older one. */
233 let baseTip = "";
230234 /** Agent runs about this pull, newest first. */
231235 const crew = new RunList(f);
232236 const mayWrite = (): boolean => src.capabilities.write && repo !== undefined;
@@ -275,6 +279,16 @@ export function pullScreen(
275279 ? NO_CHECK
276280 : checkSummary(f.db, row.id, row.headOid || (facts?.headOid ?? ""));
277281 checkBase = row?.baseBranch ?? facts?.baseBranch ?? "";
282 baseTip =
283 repo === undefined ||
284 row === undefined ||
285 row.state !== "open" ||
286 row.mergedAt !== null
287 ? ""
288 : ((await resolveOid(
289 await pullDir(f.app.db, f.app.reposRoot, repo, row),
290 `refs/heads/${row.baseBranch}`,
291 )) ?? "");
278292 const runs =
279293 repo === undefined || row === undefined
280294 ? undefined
@@ -757,7 +771,13 @@ export function pullScreen(
757771 // The box is exactly as tall as what is in it: the state, then a row
758772 // for the verdicts when there are any, then a row for the checks when
759773 // a deploy has run or the gate demands one.
760 const checksLine = checksText(check, requireChecks, isApp, checkBase);
774 const checksLine = checksText(
775 check,
776 requireChecks,
777 isApp,
778 checkBase,
779 checkStale(check.run, baseTip),
780 );
761781 const boxH =
762782 3 +
763783 (standingText(standing(), requireReview) === "" ? 0 : 1) +
@@ -1198,6 +1218,7 @@ export function checksText(
11981218 required: boolean,
11991219 isApp: boolean,
12001220 baseBranch = "",
1221 stale = false,
12011222 ): string {
12021223 if (check.state === "none")
12031224 return required
@@ -1215,7 +1236,7 @@ export function checksText(
12151236 }`;
12161237 switch (check.state) {
12171238 case "ok":
1218 return `checks: passed${summary}`;
1239 return `checks: passed${stale ? " against an older " + (baseBranch || "base") : ""}${summary}`;
12191240 case "failed":
12201241 return `checks: failed${summary}`;
12211242 case "running":
src/web/pages/pull.ts +9
@@ -21,6 +21,7 @@ import {
2121 } from "../../actions/pulls.ts";
2222 import { readFileAt } from "../../apps/checkout.ts";
2323 import { parseCheck } from "../../apps/manifest.ts";
24 import { checkStale } from "../../checks/stale.ts";
2425 import { tierLine, type TierState } from "../../checks/tiers.ts";
2526 import { getAppByRepo } from "../../core/apps.ts";
2627 import { labelsFor, listLabels } from "../../core/issues.ts";
@@ -215,6 +216,8 @@ export function checksLine(
215216 conflicts = 0,
216217 /** The pull's base branch, to name what the check was merged with. */
217218 baseBranch = "",
219 /** The passing check was merged with a base tip that is no longer the base's. */
220 stale = false,
218221 ): Html {
219222 if (!isApp && !required && check.state === "none" && !waiting && again === "")
220223 return html``;
@@ -238,6 +241,10 @@ export function checksLine(
238241 ? html`<span class="bad"
239242 >${icon("x")}Conflicts in ${conflicts} ${conflicts === 1 ? "file" : "files"}</span
240243 >${link}`
244 : check.state === "ok" && stale

Right on the page. checksText in src/tui/screens/pull.ts needs the same branch, or the terminal still says checks: passed for this exact case.

245 ? html`<span class="now"
246 >${icon("clock")}Checked against an older ${baseBranch === "" ? "base" : baseBranch}</span
247 >${link}`
241248 : check.state === "ok"
242249 ? html`<span class="ok">${icon("check")}Checks passed</span>${link}`
243250 : check.state === "failed"
@@ -321,6 +328,7 @@ function mergeBox(
321328 tier = "",
322329 ): Html {
323330 const thread = `${repoBase(repo)}/pulls/${pull.number}`;
331 const stale = checkStale(check.run, status.baseOid);
324332 // A reader's head runs nothing until a writer approves it; say so rather
325333 // than leave "no checks yet" waiting for something that will not come.
326334 const checks = checksLine(
@@ -337,6 +345,7 @@ function mergeBox(
337345 : "",
338346 status.conflicts.length,
339347 pull.baseBranch,
348 stale,
340349 );
341350 const reopen = html`<form method="post" action="${thread}/state">
342351 <input type="hidden" name="state" value="open" />
test/check-stale.test.ts added +27
@@ -0,0 +1,27 @@
1 import { expect, test } from "bun:test";
2 import { checkStale } from "../src/checks/stale.ts";
3 import { checksLine } from "../src/web/pages/pull.ts";
4
5 const A = "a".repeat(40);
6 const B = "b".repeat(40);
7
8 test("a check on an older base tip is stale; on the tip it is not", () => {
9 expect(checkStale({ baseOid: A }, B)).toBe(true);
10 expect(checkStale({ baseOid: B }, B)).toBe(false);
11 });
12
13 test("nothing is guessed: no run, no recorded base, or no readable tip is not stale", () => {
14 expect(checkStale(null, B)).toBe(false);
15 expect(checkStale({ baseOid: "" }, B)).toBe(false);
16 expect(checkStale({ baseOid: A }, undefined)).toBe(false);
17 });
18
19 test("a stale green reads as an older main instead of green", () => {
20 const ok = { state: "ok", run: null } as const;
21 const args = ["/a/b", ok, false, true, false, "", "", 0, "main"] as const;
22 const stale = checksLine(...args, true).text;
23 expect(stale).toContain("Checked against an older main");
24 expect(stale).not.toContain('class="ok"');
25 expect(stale).not.toContain("Checks passed");
26 expect(checksLine(...args, false).text).toContain("Checks passed");
27 });
test/checks.test.ts +7
@@ -407,6 +407,13 @@ describe("checks", () => {
407407 // The terminal says the same words (#1810).
408408 expect(checksText(seen, false, true, "main")).toContain(` · ${said}`);
409409 expect(checksText(seen, false, true)).not.toContain("checked against");
410 // A green on an older base is not an unqualified pass in the terminal (#2237).
411 const green = { ...seen, state: "ok" as const };
412 expect(checksText(green, false, true, "main", true)).toContain(
413 "checks: passed against an older main",
414 );
415 expect(checksText(green, false, true, "main", false)).toContain("checks: passed");
416 expect(checksText(green, false, true, "main", false)).not.toContain("older");
410417 expect(
411418 checksForHead(app.db, pull.id, branched.value.commitOid)[0]?.id,
412419 ).toBe(failed.id);
test/mcp.test.ts +58
@@ -487,6 +487,64 @@ describe("the forge over MCP", () => {
487487 expect(read.value.check.baseOid).toBe("abc1234abc1234abc1234abc1234abc1234abc12");
488488 });
489489
490 test("pull says checkStale in the MCP and the API when its check was on an older base (#2237)", async () => {
491 const branched = await commitToBranch(app, alice, game, {
492 branch: "stale-feat",
493 message: "another change",
494 files: [{ path: "c.txt", content: "c\n" }],
495 });
496 if (!branched.ok) throw new Error(JSON.stringify(branched.error));
497 const opened = await openPull(app, alice, game, {
498 title: "Stale",
499 baseBranch: "main",
500 headBranch: "stale-feat",
501 });
502 if (!opened.ok) throw new Error(String(opened.error));
503 const pull = opened.value;
504 const viaApi = async () =>
505 (await (
506 await fetch(`${base}/api/v1/repos/alice/game/pulls/${pull.number}`, {
507 headers: { authorization: `Bearer ${ro}` },
508 })
509 ).json()) as any;
510 const viaMcp = async () =>
511 (await tool(ro, "pull", { repo: "alice/game", number: pull.number })).value;
512 // No check, or one that recorded no base: not stale.
513 expect((await viaMcp()).checkStale).toBe(false);
514 expect((await viaApi()).checkStale).toBe(false);
515 const run = createRun(app.db, {
516 repoId: game.id,
517 kind: "check",
518 issueId: pull.id,
519 env: `pull-${pull.number}`,
520 commitOid: branched.value.commitOid,
521 });
522 expect((await viaMcp()).checkStale).toBe(false);
523 // Checked on the base's tip: not stale.
524 const tip: string = (await viaApi()).status.baseOid;
525 setRunBase(app.db, run.id, tip);
526 expect((await viaMcp()).checkStale).toBe(false);
527 expect((await viaApi()).checkStale).toBe(false);
528 // The base moves on: the green is from an older main.
529 const moved = await commitToBranch(app, alice, game, {
530 message: "main moves",
531 files: [{ path: "d.txt", content: "d\n" }],
532 });
533 if (!moved.ok) throw new Error(JSON.stringify(moved.error));
534 expect((await viaMcp()).checkStale).toBe(true);
535 expect((await viaApi()).checkStale).toBe(true);
536 // A new check on the new tip is current again.
537 setRunBase(app.db, run.id, moved.value.commitOid);
538 expect((await viaMcp()).checkStale).toBe(false);
539 expect((await viaApi()).checkStale).toBe(false);
540 // Stale again, then closed: a pull that cannot land reads false, not noise.
541 setRunBase(app.db, run.id, tip);
542 expect((await viaApi()).checkStale).toBe(true);
543 app.db.run("UPDATE issue SET state = 'closed' WHERE id = ?", [pull.id]);
544 expect((await viaMcp()).checkStale).toBe(false);
545 expect((await viaApi()).checkStale).toBe(false);
546 });
547
490548 test("stop takes a run id from now, and refuses a stale or foreign one", async () => {
491549 const run = createRun(app.db, {
492550 repoId: game.id,

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs