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 #1573

Closed opened this
Closed without merging

mason/forge:mason/1178-a-pull-s-check-runs-on-its-head-merged-i → main 1 commit fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
10 files changed +188 −14
docs/APPS.md +7 −1
@@ -323,7 +323,13 @@ instead of `app`:
323323
324324 The forge runs it against every commit a pull proposes — when the pull
325325 opens, and each time its branch moves, a fork's included — and against
326 every push to the default branch, in a fresh export of that commit. The
326 every push to the default branch, in a fresh export of that commit. A
327 pull's commit is checked merged into its base's current tip — a merge
328 commit no ref points at, so green means the merge is green — and the run
329 records that tip as `baseOid`, which the pull page and the MCP `pull`
330 tool show as "checked on main@abc1234". A pull that does not merge runs
331 no command: its check fails at once, naming the paths in the way. A push
332 to the default branch is checked on its own commit, `baseOid` empty. The
327333 verdict is a run of kind `check`, kept with its log beside the pull, in
328334 the same place a preview's deploy reports and read by the same code: the
329335 inbox badge, the pull's status, the crew. A failing check on an agent's
src/checks/merged.ts +7
@@ -58,3 +58,10 @@ export async function mergeForCheck(
5858 line: `merge of ${head.slice(0, 7)} into ${baseBranch}@${baseTip.slice(0, 7)}`,
5959 };
6060 }
61
62 /**
63 * The words a pull's check is read by: which base its head was merged into
64 * for the verdict, as `main@abc1234`. Empty for a run that merged nothing.
65 */
66 export const checkedOn = (run: { baseOid: string }, baseBranch: string): string =>
67 run.baseOid === "" ? "" : `checked on ${baseBranch}@${run.baseOid.slice(0, 7)}`;
src/checks/service.ts +9 −5
@@ -487,6 +487,10 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
487487 tip = await resolveOid(bare, `refs/heads/${x.pull.baseBranch}`);
488488 if (tip === undefined)
489489 return { ok: false, error: `the base ${x.pull.baseBranch} could not be found` };
490 // Named before the merge is tried: a pull that does not merge says
491 // which base it does not merge into.
492 if (x.runId !== undefined)
493 noteRunProgress(app.db, x.runId, { baseOid: tip });
490494 const merged = await mergeForCheck(bare, x.pull.baseBranch, tip, x.commitOid);
491495 if (!merged.ok) return { ok: false, error: merged.error };
492496 source = merged.commit;
@@ -509,13 +513,13 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle {
509513 if (declared.value === undefined)
510514 return { ok: false, error: "this commit declares no check" };
511515 // A pull does not choose its own gate: the commands and the timeout come
512 // from the base branch's `forge.json` where the pull left it. The
513 // branch's own supplies only what it needs to run (install, egress,
514 // limits).
516 // from the base branch's `forge.json` where the pull left it — for the
517 // fast tier, the very tip its head was merged into. The branch's own
518 // supplies only what it needs to run (install, egress, limits).
515519 let manifest = declared.value;
516 let baseTip: string | undefined;
517520 if (x.pull !== undefined) {
518 baseTip = await resolveOid(bare, `refs/heads/${x.pull.baseBranch}`);
521 const baseTip =
522 tip ?? (await resolveOid(bare, `refs/heads/${x.pull.baseBranch}`));
519523 if (baseTip === undefined)
520524 return { ok: false, error: "the pull's base branch could not be found" };
521525 const [baseJson, basePackage] = await Promise.all([
src/core/runs.ts +12 −1
@@ -52,6 +52,11 @@ 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 /**
56 * For a pull's check: the base tip its head was merged into, the commit
57 * the verdict is of being that merge. '' when nothing was merged.
58 */
59 readonly baseOid: string;
5560 /** For an agent run: the branch it works on. */
5661 readonly branch: string;
5762 readonly state: RunState;
@@ -92,6 +97,7 @@ interface RunRow {
9297 issue_id: string | null;
9398 env: string;
9499 commit_oid: string;
100 base_oid: string;
95101 branch: string;
96102 state: RunState;
97103 summary: string;
@@ -119,6 +125,7 @@ const toRun = (r: RunRow): Run => ({
119125 issueId: r.issue_id,
120126 env: r.env,
121127 commitOid: r.commit_oid,
128 baseOid: r.base_oid,
122129 branch: r.branch,
123130 state: r.state,
124131 summary: r.summary,
@@ -335,13 +342,16 @@ export function noteRunProgress(
335342 subscriptionId?: string | undefined;
336343 /** Whether the shell is held to the sandbox, and why not when it is not. */
337344 sandbox?: { enforced: boolean; note: string } | undefined;
345 /** For a pull's check: the base tip its head is merged into. */
346 baseOid?: string | undefined;
338347 },
339348 ): void {
340349 db.query(
341350 `UPDATE run SET session_id = COALESCE(?2, session_id), cost_usd = COALESCE(?3, cost_usd),
342351 branch = COALESCE(?4, branch), subscription_id = COALESCE(?5, subscription_id),
343352 sandbox_enforced = COALESCE(?6, sandbox_enforced),
344 sandbox_note = COALESCE(?7, sandbox_note)
353 sandbox_note = COALESCE(?7, sandbox_note),
354 base_oid = COALESCE(?8, base_oid)
345355 WHERE id = ?1`,
346356 ).run(
347357 runId,
@@ -351,6 +361,7 @@ export function noteRunProgress(
351361 progress.subscriptionId ?? null,
352362 progress.sandbox === undefined ? null : progress.sandbox.enforced ? 1 : 0,
353363 progress.sandbox?.note.slice(0, 300) ?? null,
364 progress.baseOid ?? null,
354365 );
355366 }
356367
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 merged its head into (#1178): the verdict
1599 // is of that merge, so the run names it. '' for a check of a branch's own
1600 // commit, which merges nothing.
1601 `
1602 ALTER TABLE run ADD COLUMN base_oid TEXT NOT NULL DEFAULT '';
1603 `,
15981604 ];
15991605
16001606 /**
src/http/mcp.ts +20 −2
@@ -82,7 +82,8 @@ 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 { checksForHead, listActiveRuns } from "../core/runs.ts";
86 import { checkedOn } from "../checks/merged.ts";
8687 import { scopeAllows } from "../core/tokens.ts";
8788 import { getUsersByIds, type User } from "../core/users.ts";
8889 import { openapi } from "../contract/openapi.ts";
@@ -412,7 +413,7 @@ const TOOLS: readonly Tool[] = [
412413 {
413414 name: "pull",
414415 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.",
416 "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. `check` is the newest check of the head: its state, its summary, and `baseOid`, the base commit the head was merged into for that verdict (`checkedOn` says it as base@oid); null when the head has no check.",
416417 inputSchema: {
417418 type: "object",
418419 properties: {
@@ -436,6 +437,13 @@ const TOOLS: readonly Tool[] = [
436437 await pullDir(ctx.app.db, ctx.app.reposRoot, repo, pull),
437438 pull,
438439 );
440 // A pull's verdict is of its head merged into a base: name which. A
441 // private app's runs are for those who may enter it (`mayReadRuns`).
442 const checked = mayReadRuns(ctx.app, user, repo)
443 ? checksForHead(ctx.app.db, pull.id, pull.headOid).find(
444 (run) => run.kind === "check",
445 )
446 : undefined;
439447 return good({
440448 number: pull.number,
441449 title: pull.title,
@@ -443,6 +451,16 @@ const TOOLS: readonly Tool[] = [
443451 base: pull.baseBranch,
444452 head: pull.headBranch,
445453 ...(state ?? {}),
454 check:
455 checked === undefined
456 ? null
457 : {
458 state: checked.state,
459 summary: checked.summary,
460 commitOid: checked.commitOid,
461 baseOid: checked.baseOid,
462 checkedOn: checkedOn(checked, pull.baseBranch),
463 },
446464 });
447465 },
448466 },
src/web/pages/pull.ts +9 −1
@@ -22,6 +22,7 @@ import {
2222 import { readFileAt } from "../../apps/checkout.ts";
2323 import { parseCheck } from "../../apps/manifest.ts";
2424 import { tierLine, type TierState } from "../../checks/tiers.ts";
25 import { checkedOn } from "../../checks/merged.ts";
2526 import { getAppByRepo } from "../../core/apps.ts";
2627 import { labelsFor, listLabels } from "../../core/issues.ts";
2728 import { marksByName } from "../../core/marks.ts";
@@ -211,6 +212,8 @@ function checksLine(
211212 tier = "",
212213 /** A writer's button to ask for a check again; empty for anyone else. */
213214 again: Html | "" = "",
215 /** The pull's base branch, named beside the commit the check merged into. */
216 baseBranch = "",
214217 ): Html {
215218 if (!isApp && !required && check.state === "none" && !waiting && again === "")
216219 return html``;
@@ -221,7 +224,11 @@ function checksLine(
221224 : html`<span class="where-sep" aria-hidden="true">·</span
222225 ><a href="${base}/runs/${run.id}"
223226 >deploy ${run.commitOid.slice(0, 7)}</a
224 >`;
227 >${when(
228 checkedOn(run, baseBranch),
229 (on) => html`<span class="where-sep" aria-hidden="true">·</span
230 ><span class="checked-on">${on}</span>`,
231 )}`;
225232 const body =
226233 check.state === "ok"
227234 ? html`<span class="ok">${icon("check")}Checks passed</span>${link}`
@@ -320,6 +327,7 @@ function mergeBox(
320327 ${btn("Check again", { size: "sm", icon: "loader" })}
321328 </form>`
322329 : "",
330 pull.baseBranch,
323331 );
324332 const reopen = html`<form method="post" action="${thread}/state">
325333 <input type="hidden" name="state" value="open" />
src/web/studio/change.ts +13 −3
@@ -26,6 +26,7 @@ import {
2626 import { canWrite, type Repo } from "../../core/repos.ts";
2727 import { listReviews, type Review } from "../../core/reviews.ts";
2828 import { wallOf, wallSentence, type Wall } from "../../contract/policy.ts";
29 import { checkedOn } from "../../checks/merged.ts";
2930 import { checkSummary } from "../../core/runs.ts";
3031 import { repoDir } from "../../git/repos.ts";
3132 import type { Ctx } from "../../http/context.ts";
@@ -315,10 +316,15 @@ async function pullPanels(ctx: Ctx, repo: Repo, pull: Pull) {
315316 // Before a merge the deploy worth linking is the preview's own check;
316317 // after, it is production's — the preview's run may already be torn
317318 // down, and the word above is judged by production regardless.
318 let deploy: { id: string; commitOid: string } | undefined;
319 let deploy: { id: string; commitOid: string; on?: string } | undefined;
319320 if (pull.mergedAt === null) {
320321 const run = checkSummary(db, pull.id, pull.headOid).run;
321 if (run) deploy = { id: run.id, commitOid: run.commitOid };
322 if (run)
323 deploy = {
324 id: run.id,
325 commitOid: run.commitOid,
326 on: checkedOn(run, pull.baseBranch),
327 };
322328 } else {
323329 const production =
324330 record === undefined
@@ -347,7 +353,11 @@ async function pullPanels(ctx: Ctx, repo: Repo, pull: Pull) {
347353 html`<span class="where-sep" aria-hidden="true">·</span
348354 ><a href="${base}/runs/${d.id}"
349355 >deploy ${d.commitOid.slice(0, 7)}</a
350 >`,
356 >${when(
357 d.on,
358 (on) => html`<span class="where-sep" aria-hidden="true">·</span
359 ><span class="checked-on">${on}</span>`,
360 )}`,
351361 )}
352362 </p>`,
353363 );
test/check-tiers.test.ts +5 −1
@@ -476,7 +476,7 @@ describe("a pull's fast check is of the merge", () => {
476476 const fork = await commit("late", [{ path: "late.txt", content: "late\n" }]);
477477 const conflicting = await commit("clash", [{ path: "shared.txt", content: "mine\n" }]);
478478 // Main learns what the fast command needs, and moves a file the clash edits.
479 await commit(undefined, [
479 const tip = await commit(undefined, [
480480 { path: "needs.sh", content: "echo NEEDS-FOUND\n" },
481481 { path: "shared.txt", content: "theirs\n" },
482482 ]);
@@ -499,6 +499,10 @@ describe("a pull's fast check is of the merge", () => {
499499 const clash = await settled(conflicting);
500500 expect(clash.state).toBe("failed");
501501 expect(clash.summary).toContain("does not merge into main: shared.txt");
502 // It names the base it does not merge into, and runs nothing (#1178).
503 expect(clash.baseOid).toBe(tip);
504 expect(late.baseOid).toBe(tip);
505 expect(readRunLog(dataDir, clash.id).lines.join("\n")).not.toContain("$ ");
502506 });
503507
504508 test("a writer asks for a check again at the current head", async () => {
test/checks.test.ts +100
@@ -25,6 +25,7 @@ import {
2525 cancelRun,
2626 cancelRunsForPull,
2727 CHECK_RESTART_LIMIT,
28 checkSummary,
2829 checksForHead,
2930 claimRun,
3031 createRun,
@@ -38,6 +39,7 @@ import { repoDir } from "../src/git/repos.ts";
3839 import { STALE_AFTER_MS } from "../src/runs/lease.ts";
3940 import { pullWord, servingHistory } from "../src/web/studio/words.ts";
4041 import { createUser, type User } from "../src/core/users.ts";
42 import { createToken } from "../src/core/tokens.ts";
4143 import type { Repo } from "../src/core/repos.ts";
4244 import { serve, type Servers } from "../src/http/server.ts";
4345 import { readRunLog } from "../src/runs/log.ts";
@@ -727,6 +729,104 @@ describe("checks", () => {
727729 expect(done.state).toBe("ok");
728730 }, 60_000);
729731
732 test("a pull green alone but red merged with a newer base reads red, and names the base it was checked on (#1178)", async () => {
733 // The base's check: red only where both halves are in one tree.
734 const gate = await commitToBranch(app, alice, repo, {
735 message: "A check of two halves",
736 files: [
737 {
738 path: "forge.json",
739 content: checkJson("test ! -f LEFT.md || test ! -f RIGHT.md"),
740 },
741 ],
742 });
743 if (!gate.ok) throw new Error(JSON.stringify(gate.error));
744 const left = await commitToBranch(app, alice, repo, {
745 branch: "left",
746 message: "The left half",
747 files: [{ path: "LEFT.md", content: "left\n" }],
748 });
749 if (!left.ok) throw new Error(JSON.stringify(left.error));
750 const head = left.value.commitOid;
751 const opened = await openPull(app, alice, repo, {
752 title: "Left",
753 baseBranch: "main",
754 headBranch: "left",
755 });
756 if (!opened.ok) throw new Error(String(opened.error));
757 const pull = opened.value;
758 const green = await settled(
759 (r) => r.issueId === pull.id && r.commitOid === head,
760 );
761 expect(green.state).toBe("ok");
762 expect(green.baseOid).toBe(gate.value.commitOid);
763 // A push to the default branch is checked on its own commit: no merge, no base.
764 const own = await settled(
765 (r) => r.issueId === null && r.commitOid === gate.value.commitOid,
766 );
767 expect(own.baseOid).toBe("");
768
769 // The base learns the other half; the pull's head has not moved.
770 const right = await commitToBranch(app, alice, repo, {
771 message: "The right half",
772 files: [{ path: "RIGHT.md", content: "right\n" }],
773 });
774 if (!right.ok) throw new Error(JSON.stringify(right.error));
775 const tip = right.value.commitOid;
776 const refs = (): string =>
777 Bun.spawnSync(["git", "for-each-ref", "--format=%(refname) %(objectname)"], {
778 cwd: repoDir(app.reposRoot, "alice", "lib"),
779 }).stdout.toString();
780 const before = refs();
781 await checks.request(repo, `pull-${pull.number}`, head, "asked", pull.id);
782 const red = await settled(
783 (r) => r.issueId === pull.id && r.commitOid === head && r.id !== green.id,
784 );
785 expect(red.state).toBe("failed");
786 expect(red.commitOid).toBe(head);
787 expect(red.baseOid).toBe(tip);
788 expect(checkSummary(app.db, pull.id, head).state).toBe("failed");
789 expect(readRunLog(dataDir, red.id).lines.join("\n")).toContain(
790 `merge of ${head.slice(0, 7)} into main@${tip.slice(0, 7)}`,
791 );
792 // The merge is checked, never written to a branch or any other ref.
793 expect(refs()).toBe(before);
794
795 // The page says which base the verdict is of, and so does the MCP.
796 const page = await (
797 await fetch(`${servers.url}/alice/lib/pulls/${pull.number}`)
798 ).text();
799 expect(page).toContain(`checked on main@${tip.slice(0, 7)}`);
800 const token = createToken(app.db, alice.id, "mcp", "ro").token;
801 const res = await fetch(`${servers.url}/mcp`, {
802 method: "POST",
803 headers: {
804 "content-type": "application/json",
805 accept: "application/json, text/event-stream",
806 authorization: `Bearer ${token}`,
807 },
808 body: JSON.stringify({
809 jsonrpc: "2.0",
810 id: 1,
811 method: "tools/call",
812 params: { name: "pull", arguments: { repo: "alice/lib", number: pull.number } },
813 }),
814 });
815 const body = (await res.json()) as {
816 result: { content: { text: string }[] };
817 };
818 const seen = JSON.parse(body.result.content[0]!.text) as {
819 check: { state: string; commitOid: string; baseOid: string; checkedOn: string };
820 };
821 expect(seen.check).toEqual({
822 state: "failed",
823 summary: red.summary,
824 commitOid: head,
825 baseOid: tip,
826 checkedOn: `checked on main@${tip.slice(0, 7)}`,
827 } as typeof seen.check);
828 }, 60_000);
829
730830 test("a check that is declared wrong fails with the reason", async () => {
731831 const wrong = await commitToBranch(app, alice, repo, {
732832 message: "A wrong check",

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs