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

A pull's check runs on its head merged into today's base, and records the base it checked #1536

Closed opened this
Closed without merging

scout/forge:scout/1178-a-pull-s-check-runs-on-its-head-merged-i → main 3 commits fork 3 threads · 3 unresolved

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
12 files changed +196 −7
src/checks/service.ts +2
@@ -490,6 +490,8 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
490490 const merged = await mergeForCheck(bare, x.pull.baseBranch, tip, x.commitOid);
491491 if (!merged.ok) return { ok: false, error: merged.error };
492492 source = merged.commit;
493 if (x.runId !== undefined)
494 noteRunProgress(app.db, x.runId, { baseOid: tip });
493495 mergeLine = merged.line;
494496 }
495497 const run = { id: x.id, commitOid: source };
src/contract/openapi.ts +6
@@ -51,6 +51,12 @@ export const ROUTES: readonly Route[] = [
5151 auth: true,
5252 body: { title: "string", head: "string", headRepo: "string" },
5353 },
54 {
55 method: "get",
56 path: `${REPO}/pulls/{number}`,
57 auth: false,
58 summary: "Read a pull: its diff, mergeability, and `check` (the verdict at its head and the `baseOid` it was merged into when checked)",
59 },
5460 {
5561 method: "post",
5662 path: `${REPO}/pulls/{number}/reviews`,
src/core/runs.ts +32 −1
@@ -52,6 +52,8 @@ export interface Run {
5252 /** For a deploy: `production` or `pull-<n>`. Empty for an agent run. */
5353 readonly env: string;
5454 readonly commitOid: string;
55 /** The base tip a pull's check merged the head into; '' when it merged nothing. */
56 readonly baseOid: string;
5557 /** For an agent run: the branch it works on. */
5658 readonly branch: string;
5759 readonly state: RunState;
@@ -92,6 +94,7 @@ interface RunRow {
9294 issue_id: string | null;
9395 env: string;
9496 commit_oid: string;
97 base_oid: string;
9598 branch: string;
9699 state: RunState;
97100 summary: string;
@@ -119,6 +122,7 @@ const toRun = (r: RunRow): Run => ({
119122 issueId: r.issue_id,
120123 env: r.env,
121124 commitOid: r.commit_oid,
125 baseOid: r.base_oid,
122126 branch: r.branch,
123127 state: r.state,
124128 summary: r.summary,
@@ -335,13 +339,16 @@ export function noteRunProgress(
335339 subscriptionId?: string | undefined;
336340 /** Whether the shell is held to the sandbox, and why not when it is not. */
337341 sandbox?: { enforced: boolean; note: string } | undefined;
342 /** The base tip a pull's check merged the head into. */
343 baseOid?: string | undefined;
338344 },
339345 ): void {
340346 db.query(
341347 `UPDATE run SET session_id = COALESCE(?2, session_id), cost_usd = COALESCE(?3, cost_usd),
342348 branch = COALESCE(?4, branch), subscription_id = COALESCE(?5, subscription_id),
343349 sandbox_enforced = COALESCE(?6, sandbox_enforced),
344 sandbox_note = COALESCE(?7, sandbox_note)
350 sandbox_note = COALESCE(?7, sandbox_note),
351 base_oid = COALESCE(?8, base_oid)
345352 WHERE id = ?1`,
346353 ).run(
347354 runId,
@@ -351,6 +358,7 @@ export function noteRunProgress(
351358 progress.subscriptionId ?? null,
352359 progress.sandbox === undefined ? null : progress.sandbox.enforced ? 1 : 0,
353360 progress.sandbox?.note.slice(0, 300) ?? null,
361 progress.baseOid ?? null,
354362 );
355363 }
356364
@@ -731,6 +739,29 @@ export function checkSummary(
731739 : { state: "none", run: null };
732740 }
733741
742 /** A pull's check as the API and the MCP read it. */
743 export interface CheckReading {
744 state: CheckSummary["state"];
745 head: string;
746 /** The base commit the head was merged into when checked; null when none. */
747 baseOid: string | null;
748 summary: string | null;
749 }
750
751 export function checkReading(
752 db: Database,
753 issueId: string,
754 headOid: string,
755 ): CheckReading {
756 const { state, run } = checkSummary(db, issueId, headOid);
757 return {
758 state,
759 head: headOid,
760 baseOid: run?.baseOid || null,
761 summary: run?.summary ?? null,
762 };
763 }
764
734765 /**
735766 * The latest run per issue for a set of issues — one query, for a list that
736767 * shows a check mark beside every pull without asking per row.
src/core/schema.ts +6
@@ -1595,6 +1595,12 @@ export const migrations: readonly string[] = [
15951595 PRIMARY KEY (repo_id, parent_id, issue_id)
15961596 );
15971597 `,
1598 // 76 — the base a pull's check was run against (#1178): the check is of the
1599 // head merged into this commit, so the verdict names it. '' for a run that
1600 // merged nothing (a push to a branch).
1601 `
1602 ALTER TABLE run ADD COLUMN base_oid TEXT NOT NULL DEFAULT '';
1603 `,
15981604 ];
15991605
16001606 /**
src/http/api.ts +2
@@ -281,6 +281,7 @@ import {
281281 } from "../core/issues.ts";
282282 import type { Closing } from "../actions/issues.ts";
283283 import {
284 checkReading,
284285 checksForHead,
285286 getRun,
286287 isTerminal,
@@ -2819,6 +2820,7 @@ export function apiRoutes(router: Router<Handler>): void {
28192820 : {}),
28202821 status,
28212822 diff,
2823 check: checkReading(ctx.app.db, found.pull.id, found.pull.headOid),
28222824 hold: hold === undefined ? null : holdJson(hold),
28232825 });
28242826 });
src/http/mcp.ts +3 −2
@@ -82,7 +82,7 @@ import {
8282 type Repo,
8383 } from "../core/repos.ts";
8484 import { plannerDeclines } from "../crew/autonomy.ts";
85 import { listActiveRuns } from "../core/runs.ts";
85 import { checkReading, listActiveRuns } from "../core/runs.ts";
8686 import { scopeAllows } from "../core/tokens.ts";
8787 import { getUsersByIds, type User } from "../core/users.ts";
8888 import { openapi } from "../contract/openapi.ts";
@@ -412,7 +412,7 @@ const TOOLS: readonly Tool[] = [
412412 {
413413 name: "pull",
414414 description:
415 "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`). The same two fields as GET /api/v1/repos/:owner/:name/pulls/:number.",
415 "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`), and its `check`: the verdict at its head and the `baseOid` it was checked against (the head merged into that commit). The same fields as GET /api/v1/repos/:owner/:name/pulls/:number.",
416416 inputSchema: {
417417 type: "object",
418418 properties: {
@@ -443,6 +443,7 @@ const TOOLS: readonly Tool[] = [
443443 base: pull.baseBranch,
444444 head: pull.headBranch,
445445 ...(state ?? {}),
446 check: checkReading(ctx.app.db, pull.id, pull.headOid),
446447 });
447448 },
448449 },
src/tui/screens/pull.ts +13 −2
@@ -643,7 +643,12 @@ export function pullScreen(
643643 // The box is exactly as tall as what is in it: the state, then a row
644644 // for the verdicts when there are any, then a row for the checks when
645645 // a deploy has run or the gate demands one.
646 const checksLine = checksText(check, requireChecks, isApp);
646 const checksLine = checksText(
647 check,
648 requireChecks,
649 isApp,
650 facts?.baseBranch,
651 );
647652 const boxH =
648653 3 +
649654 (standingText(standing(), requireReview) === "" ? 0 : 1) +
@@ -1044,6 +1049,7 @@ export function checksText(
10441049 check: CheckSummary,
10451050 required: boolean,
10461051 isApp: boolean,
1052 baseName = "the base",
10471053 ): string {
10481054 if (check.state === "none")
10491055 return required
@@ -1052,7 +1058,12 @@ export function checksText(
10521058 ? "checks: no deploy of this head yet"
10531059 : "";
10541060 const run = check.run;
1055 const summary = run === null || run.summary === "" ? "" : ` · ${run.summary}`;
1061 const merged =
1062 run === null || run.baseOid === ""
1063 ? ""
1064 : ` · checked on ${baseName}@${run.baseOid.slice(0, 7)}`;
1065 const summary =
1066 (run === null || run.summary === "" ? "" : ` · ${run.summary}`) + merged;
10561067 switch (check.state) {
10571068 case "ok":
10581069 return `checks: passed${summary}`;
src/web/pages/pull.ts +10 −1
@@ -210,6 +210,7 @@ function checksLine(
210210 tier = "",
211211 /** A writer's button to ask for a check again; empty for anyone else. */
212212 again: Html | "" = "",
213 baseName = "the base",
213214 ): Html {
214215 if (!isApp && !required && check.state === "none" && !waiting && again === "")
215216 return html``;
@@ -220,7 +221,14 @@ function checksLine(
220221 : html`<span class="where-sep" aria-hidden="true">·</span
221222 ><a href="${base}/runs/${run.id}"
222223 >deploy ${run.commitOid.slice(0, 7)}</a
223 >`;
224 >${when(

No test renders this line. Add a page test where a pull's check has merged and assert the page shows checked on main@<7 chars of baseOid>. The terminal's check line in src/tui/screens/pull.ts (~1048–1080) also needs the same words.

225 run.baseOid !== "",
226 () =>
227 html`<span class="where-sep" aria-hidden="true">·</span
228 ><span class="base-oid"
229 >checked on ${baseName}@${run.baseOid.slice(0, 7)}</span
230 >`,
231 )}`;
224232 const body =
225233 check.state === "ok"
226234 ? html`<span class="ok">${icon("check")}Checks passed</span>${link}`
@@ -319,6 +327,7 @@ function mergeBox(
319327 ${btn("Check again", { size: "sm", icon: "loader" })}
320328 </form>`
321329 : "",
330 pull.baseBranch,
322331 );
323332 const reopen = html`<form method="post" action="${thread}/state">
324333 <input type="hidden" name="state" value="open" />
test/checks.test.ts +51
@@ -9,6 +9,7 @@ import { mkdtemp, rm } from "node:fs/promises";
99 import { tmpdir } from "node:os";
1010 import { join } from "node:path";
1111 import { createApp, type App } from "../src/app.ts";
12 import { askCheck } from "../src/checks/again.ts";
1213 import { mergePull, openPull, submitReview } from "../src/actions/pulls.ts";
1314 import { setIssueState } from "../src/actions/issues.ts";
1415 import { commitToBranch, createRepo, forkRepo } from "../src/actions/repos.ts";
@@ -26,6 +27,7 @@ import {
2627 cancelRunsForPull,
2728 CHECK_RESTART_LIMIT,
2829 checksForHead,
30 checkSummary,
2931 claimRun,
3032 createRun,
3133 getRun,
@@ -398,6 +400,55 @@ describe("checks", () => {
398400 expect(passed.reason).toBe("push");
399401 }, 60_000);
400402
403 test("a pull green alone but red merged with a newer base reads red, and names the base it was checked on", async () => {
404 const gate = await commitToBranch(app, alice, repo, {
405 message: "The base forbids BAD.md",
406 files: [{ path: "forge.json", content: checkJson("test ! -f BAD.md") }],
407 });
408 if (!gate.ok) throw new Error(JSON.stringify(gate.error));
409 await settled((r) => r.commitOid === gate.value.commitOid);
410 const branched = await commitToBranch(app, alice, repo, {
411 branch: "merged-red",
412 message: "Green on its own",
413 files: [{ path: "own.txt", content: "own\n" }],
414 });
415 if (!branched.ok) throw new Error(JSON.stringify(branched.error));
416 const opened = await openPull(app, alice, repo, {
417 title: "Green alone",
418 baseBranch: "main",
419 headBranch: "merged-red",
420 });
421 if (!opened.ok) throw new Error(String(opened.error));
422 const pull = opened.value;
423 const green = await settled(
424 (r) => r.issueId === pull.id && r.commitOid === branched.value.commitOid,
425 );
426 expect(green.state).toBe("ok");
427 expect(green.baseOid).toBe(gate.value.commitOid);
428
429 const moved = await commitToBranch(app, alice, repo, {
430 message: "The base learns BAD.md",
431 files: [{ path: "BAD.md", content: "bad\n" }],
432 });
433 if (!moved.ok) throw new Error(JSON.stringify(moved.error));
434 await settled((r) => r.commitOid === moved.value.commitOid);
435 const again = await askCheck(app, alice, repo, pull);
436 if (!again.ok) throw new Error(again.message);
437 const red = await settled((r) => r.id === again.run.id);
438 expect(red.state).toBe("failed");
439 expect(red.baseOid).toBe(moved.value.commitOid);
440 expect(red.baseOid).not.toBe(green.baseOid);
441 expect(checkSummary(app.db, pull.id, branched.value.commitOid).state).toBe(
442 "failed",
443 );
444 // The merge is never written to a branch.
445 expect(
446 listRuns(app.db, repo.id, { kind: "check" }).find(
447 (r) => r.env === "main" && r.commitOid === moved.value.commitOid,
448 )?.baseOid,
449 ).toBe("");
450 }, 60_000);
451
401452 test("a bun test failure in the check step is summarised: how many passed, how many failed, and their names", async () => {
402453 const gate = await commitToBranch(app, alice, repo, {
403454 message: "The base's check runs bun test",
test/pull-check-again.test.ts +54
@@ -12,6 +12,9 @@ import { openPull } from "../src/actions/pulls.ts";
1212 import { commitToBranch, createRepo } from "../src/actions/repos.ts";
1313 import { startChecks, type ChecksHandle } from "../src/checks/service.ts";
1414 import { createToken } from "../src/core/tokens.ts";
15 import { checkSummary } from "../src/core/runs.ts";
16 import { getPull } from "../src/core/pulls.ts";
17 import { getRepo } from "../src/core/repos.ts";
1518 import { createSession } from "../src/core/sessions.ts";
1619 import { createUser, type User } from "../src/core/users.ts";
1720 import { serve, type Servers } from "../src/http/server.ts";
@@ -129,3 +132,54 @@ describe("asking again over HTTP", () => {
129132 expect(await page(alice, closedNumber)).not.toContain("Check again");
130133 });
131134 });
135
136 describe("a check that merged names the base it checked", () => {
137 test("the page, the REST read and the MCP all say which base commit", async () => {
138 const repo = getRepo(app.db, "alice", "gate");
139 const pull = repo && getPull(app.db, repo.id, openNumber);
140 if (!repo || !pull) throw new Error("no pull");
141 const deadline = Date.now() + 30_000;
142 let baseOid = "";
143 while (Date.now() < deadline) {
144 const { state, run } = checkSummary(app.db, pull.id, pull.headOid);
145 if (state === "ok" && run && run.baseOid !== "") {
146 baseOid = run.baseOid;
147 break;
148 }
149 await Bun.sleep(100);
150 }
151 expect(baseOid).toMatch(/^[0-9a-f]{40}$/);
152
153 const html = await page(alice, openNumber);
154 expect(html).toContain(`checked on main@${baseOid.slice(0, 7)}`);
155
156 const rest = (await (
157 await fetch(`${servers.url}/api/v1/repos/alice/gate/pulls/${openNumber}`)
158 ).json()) as { check: { state: string; head: string; baseOid: string } };
159 expect(rest.check).toMatchObject({
160 state: "ok",
161 head: pull.headOid,
162 baseOid,
163 });
164
165 const res = await fetch(`${servers.url}/mcp`, {
166 method: "POST",
167 headers: {
168 "content-type": "application/json",
169 accept: "application/json, text/event-stream",
170 authorization: `Bearer ${writerToken}`,
171 },
172 body: JSON.stringify({
173 jsonrpc: "2.0",
174 id: 1,
175 method: "tools/call",
176 params: {
177 name: "pull",
178 arguments: { repo: "alice/gate", number: openNumber },
179 },
180 }),
181 });
182 const text = await res.text();
183 expect(text).toContain(baseOid);
184 }, 40_000);
185 });
test/pull-mergeable.test.ts +1
@@ -140,6 +140,7 @@ describe("a pull says whether it merges", () => {
140140 const seen = await mcp("pull", { repo: "dev/app", number: 1 });
141141 expect(seen.mergeable).toBe(false);
142142 expect(seen.conflicts).toEqual(["code.txt"]);
143 expect(seen.check).toMatchObject({ state: "none", baseOid: null });

This only checks the empty case. Also add a case where the check has merged into the base, and assert check.baseOid equals the base tip that was checked.

143144
144145 // The base moves to hold the same line the pull wants.
145146 await write("alpha\nBETA\ngamma\n", "the same edit");
test/tui-apps.test.ts +16 −1
@@ -44,7 +44,7 @@ import { appendRunLog } from "../src/runs/log.ts";
4444 import type { App, Screen } from "../src/tui/app/app.ts";
4545 import { createForgeApp, forgeCommands } from "../src/tui/forge.ts";
4646 import { issueScreen } from "../src/tui/screens/issue.ts";
47 import { pullScreen } from "../src/tui/screens/pull.ts";
47 import { checksText, pullScreen } from "../src/tui/screens/pull.ts";
4848 import { local } from "../src/tui/screens/where.ts";
4949 import type { Session, TuiIdentity } from "../src/tui/session.ts";
5050 import {
@@ -496,3 +496,18 @@ describe("terminal: apps, runs and the crew", () => {
496496 }
497497 }, 30_000);
498498 });
499
500 describe("terminal: the check line names the base it checked", () => {
501 const ran = (baseOid: string) =>
502 ({
503 state: "ok",
504 run: { summary: "", baseOid, commitOid: "a".repeat(40) } as Run,
505 }) as const;
506
507 test("a merged check says checked on <base>@<oid>; a plain one says nothing of it", () => {
508 expect(checksText(ran("b".repeat(40)), false, true, "main")).toBe(
509 "checks: passed · checked on main@bbbbbbb",
510 );
511 expect(checksText(ran(""), false, true, "main")).toBe("checks: passed");
512 });
513 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs