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
mason/forge:mason/1178-a-pull-s-check-runs-on-its-head-merged-i → main 1 commit fork
10 files
changed +188
−14
docs/APPS.md +7 −1
| @@ -323,7 +323,13 @@ instead of `app`: | ||
| 323 | 323 | |
| 324 | 324 | The forge runs it against every commit a pull proposes — when the pull |
| 325 | 325 | 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 | |
| 327 | 333 | verdict is a run of kind `check`, kept with its log beside the pull, in |
| 328 | 334 | the same place a preview's deploy reports and read by the same code: the |
| 329 | 335 | 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( | ||
| 58 | 58 | line: `merge of ${head.slice(0, 7)} into ${baseBranch}@${baseTip.slice(0, 7)}`, |
| 59 | 59 | }; |
| 60 | 60 | } |
| 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 { | ||
| 487 | 487 | tip = await resolveOid(bare, `refs/heads/${x.pull.baseBranch}`); |
| 488 | 488 | if (tip === undefined) |
| 489 | 489 | 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 }); | |
| 490 | 494 | const merged = await mergeForCheck(bare, x.pull.baseBranch, tip, x.commitOid); |
| 491 | 495 | if (!merged.ok) return { ok: false, error: merged.error }; |
| 492 | 496 | source = merged.commit; |
| @@ -509,13 +513,13 @@ export function startChecks(app: App, options: ChecksOptions): ChecksHandle { | ||
| 509 | 513 | if (declared.value === undefined) |
| 510 | 514 | return { ok: false, error: "this commit declares no check" }; |
| 511 | 515 | // 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). | |
| 515 | 519 | let manifest = declared.value; |
| 516 | let baseTip: string | undefined; | |
| 517 | 520 | 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}`)); | |
| 519 | 523 | if (baseTip === undefined) |
| 520 | 524 | return { ok: false, error: "the pull's base branch could not be found" }; |
| 521 | 525 | const [baseJson, basePackage] = await Promise.all([ |
src/core/runs.ts +12 −1
| @@ -52,6 +52,11 @@ export interface Run { | ||
| 52 | 52 | /** For a deploy: `production` or `pull-<n>`. Empty for an agent run. */ |
| 53 | 53 | readonly env: string; |
| 54 | 54 | 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; | |
| 55 | 60 | /** For an agent run: the branch it works on. */ |
| 56 | 61 | readonly branch: string; |
| 57 | 62 | readonly state: RunState; |
| @@ -92,6 +97,7 @@ interface RunRow { | ||
| 92 | 97 | issue_id: string | null; |
| 93 | 98 | env: string; |
| 94 | 99 | commit_oid: string; |
| 100 | base_oid: string; | |
| 95 | 101 | branch: string; |
| 96 | 102 | state: RunState; |
| 97 | 103 | summary: string; |
| @@ -119,6 +125,7 @@ const toRun = (r: RunRow): Run => ({ | ||
| 119 | 125 | issueId: r.issue_id, |
| 120 | 126 | env: r.env, |
| 121 | 127 | commitOid: r.commit_oid, |
| 128 | baseOid: r.base_oid, | |
| 122 | 129 | branch: r.branch, |
| 123 | 130 | state: r.state, |
| 124 | 131 | summary: r.summary, |
| @@ -335,13 +342,16 @@ export function noteRunProgress( | ||
| 335 | 342 | subscriptionId?: string | undefined; |
| 336 | 343 | /** Whether the shell is held to the sandbox, and why not when it is not. */ |
| 337 | 344 | sandbox?: { enforced: boolean; note: string } | undefined; |
| 345 | /** For a pull's check: the base tip its head is merged into. */ | |
| 346 | baseOid?: string | undefined; | |
| 338 | 347 | }, |
| 339 | 348 | ): void { |
| 340 | 349 | db.query( |
| 341 | 350 | `UPDATE run SET session_id = COALESCE(?2, session_id), cost_usd = COALESCE(?3, cost_usd), |
| 342 | 351 | branch = COALESCE(?4, branch), subscription_id = COALESCE(?5, subscription_id), |
| 343 | 352 | 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) | |
| 345 | 355 | WHERE id = ?1`, |
| 346 | 356 | ).run( |
| 347 | 357 | runId, |
| @@ -351,6 +361,7 @@ export function noteRunProgress( | ||
| 351 | 361 | progress.subscriptionId ?? null, |
| 352 | 362 | progress.sandbox === undefined ? null : progress.sandbox.enforced ? 1 : 0, |
| 353 | 363 | progress.sandbox?.note.slice(0, 300) ?? null, |
| 364 | progress.baseOid ?? null, | |
| 354 | 365 | ); |
| 355 | 366 | } |
| 356 | 367 |
src/core/schema.ts +6
| @@ -1595,6 +1595,12 @@ export const migrations: readonly string[] = [ | ||
| 1595 | 1595 | PRIMARY KEY (repo_id, parent_id, issue_id) |
| 1596 | 1596 | ); |
| 1597 | 1597 | `, |
| 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 | `, | |
| 1598 | 1604 | ]; |
| 1599 | 1605 | |
| 1600 | 1606 | /** |
src/http/mcp.ts +20 −2
| @@ -82,7 +82,8 @@ import { | ||
| 82 | 82 | type Repo, |
| 83 | 83 | } from "../core/repos.ts"; |
| 84 | 84 | 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"; | |
| 86 | 87 | import { scopeAllows } from "../core/tokens.ts"; |
| 87 | 88 | import { getUsersByIds, type User } from "../core/users.ts"; |
| 88 | 89 | import { openapi } from "../contract/openapi.ts"; |
| @@ -412,7 +413,7 @@ const TOOLS: readonly Tool[] = [ | ||
| 412 | 413 | { |
| 413 | 414 | name: "pull", |
| 414 | 415 | 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.", | |
| 416 | 417 | inputSchema: { |
| 417 | 418 | type: "object", |
| 418 | 419 | properties: { |
| @@ -436,6 +437,13 @@ const TOOLS: readonly Tool[] = [ | ||
| 436 | 437 | await pullDir(ctx.app.db, ctx.app.reposRoot, repo, pull), |
| 437 | 438 | pull, |
| 438 | 439 | ); |
| 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; | |
| 439 | 447 | return good({ |
| 440 | 448 | number: pull.number, |
| 441 | 449 | title: pull.title, |
| @@ -443,6 +451,16 @@ const TOOLS: readonly Tool[] = [ | ||
| 443 | 451 | base: pull.baseBranch, |
| 444 | 452 | head: pull.headBranch, |
| 445 | 453 | ...(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 | }, | |
| 446 | 464 | }); |
| 447 | 465 | }, |
| 448 | 466 | }, |
src/web/pages/pull.ts +9 −1
| @@ -22,6 +22,7 @@ import { | ||
| 22 | 22 | import { readFileAt } from "../../apps/checkout.ts"; |
| 23 | 23 | import { parseCheck } from "../../apps/manifest.ts"; |
| 24 | 24 | import { tierLine, type TierState } from "../../checks/tiers.ts"; |
| 25 | import { checkedOn } from "../../checks/merged.ts"; | |
| 25 | 26 | import { getAppByRepo } from "../../core/apps.ts"; |
| 26 | 27 | import { labelsFor, listLabels } from "../../core/issues.ts"; |
| 27 | 28 | import { marksByName } from "../../core/marks.ts"; |
| @@ -211,6 +212,8 @@ function checksLine( | ||
| 211 | 212 | tier = "", |
| 212 | 213 | /** A writer's button to ask for a check again; empty for anyone else. */ |
| 213 | 214 | again: Html | "" = "", |
| 215 | /** The pull's base branch, named beside the commit the check merged into. */ | |
| 216 | baseBranch = "", | |
| 214 | 217 | ): Html { |
| 215 | 218 | if (!isApp && !required && check.state === "none" && !waiting && again === "") |
| 216 | 219 | return html``; |
| @@ -221,7 +224,11 @@ function checksLine( | ||
| 221 | 224 | : html`<span class="where-sep" aria-hidden="true">·</span |
| 222 | 225 | ><a href="${base}/runs/${run.id}" |
| 223 | 226 | >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 | )}`; | |
| 225 | 232 | const body = |
| 226 | 233 | check.state === "ok" |
| 227 | 234 | ? html`<span class="ok">${icon("check")}Checks passed</span>${link}` |
| @@ -320,6 +327,7 @@ function mergeBox( | ||
| 320 | 327 | ${btn("Check again", { size: "sm", icon: "loader" })} |
| 321 | 328 | </form>` |
| 322 | 329 | : "", |
| 330 | pull.baseBranch, | |
| 323 | 331 | ); |
| 324 | 332 | const reopen = html`<form method="post" action="${thread}/state"> |
| 325 | 333 | <input type="hidden" name="state" value="open" /> |
src/web/studio/change.ts +13 −3
| @@ -26,6 +26,7 @@ import { | ||
| 26 | 26 | import { canWrite, type Repo } from "../../core/repos.ts"; |
| 27 | 27 | import { listReviews, type Review } from "../../core/reviews.ts"; |
| 28 | 28 | import { wallOf, wallSentence, type Wall } from "../../contract/policy.ts"; |
| 29 | import { checkedOn } from "../../checks/merged.ts"; | |
| 29 | 30 | import { checkSummary } from "../../core/runs.ts"; |
| 30 | 31 | import { repoDir } from "../../git/repos.ts"; |
| 31 | 32 | import type { Ctx } from "../../http/context.ts"; |
| @@ -315,10 +316,15 @@ async function pullPanels(ctx: Ctx, repo: Repo, pull: Pull) { | ||
| 315 | 316 | // Before a merge the deploy worth linking is the preview's own check; |
| 316 | 317 | // after, it is production's — the preview's run may already be torn |
| 317 | 318 | // 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; | |
| 319 | 320 | if (pull.mergedAt === null) { |
| 320 | 321 | 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 | }; | |
| 322 | 328 | } else { |
| 323 | 329 | const production = |
| 324 | 330 | record === undefined |
| @@ -347,7 +353,11 @@ async function pullPanels(ctx: Ctx, repo: Repo, pull: Pull) { | ||
| 347 | 353 | html`<span class="where-sep" aria-hidden="true">·</span |
| 348 | 354 | ><a href="${base}/runs/${d.id}" |
| 349 | 355 | >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 | )}`, | |
| 351 | 361 | )} |
| 352 | 362 | </p>`, |
| 353 | 363 | ); |
test/check-tiers.test.ts +5 −1
| @@ -476,7 +476,7 @@ describe("a pull's fast check is of the merge", () => { | ||
| 476 | 476 | const fork = await commit("late", [{ path: "late.txt", content: "late\n" }]); |
| 477 | 477 | const conflicting = await commit("clash", [{ path: "shared.txt", content: "mine\n" }]); |
| 478 | 478 | // Main learns what the fast command needs, and moves a file the clash edits. |
| 479 | await commit(undefined, [ | |
| 479 | const tip = await commit(undefined, [ | |
| 480 | 480 | { path: "needs.sh", content: "echo NEEDS-FOUND\n" }, |
| 481 | 481 | { path: "shared.txt", content: "theirs\n" }, |
| 482 | 482 | ]); |
| @@ -499,6 +499,10 @@ describe("a pull's fast check is of the merge", () => { | ||
| 499 | 499 | const clash = await settled(conflicting); |
| 500 | 500 | expect(clash.state).toBe("failed"); |
| 501 | 501 | 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("$ "); | |
| 502 | 506 | }); |
| 503 | 507 | |
| 504 | 508 | test("a writer asks for a check again at the current head", async () => { |
test/checks.test.ts +100
| @@ -25,6 +25,7 @@ import { | ||
| 25 | 25 | cancelRun, |
| 26 | 26 | cancelRunsForPull, |
| 27 | 27 | CHECK_RESTART_LIMIT, |
| 28 | checkSummary, | |
| 28 | 29 | checksForHead, |
| 29 | 30 | claimRun, |
| 30 | 31 | createRun, |
| @@ -38,6 +39,7 @@ import { repoDir } from "../src/git/repos.ts"; | ||
| 38 | 39 | import { STALE_AFTER_MS } from "../src/runs/lease.ts"; |
| 39 | 40 | import { pullWord, servingHistory } from "../src/web/studio/words.ts"; |
| 40 | 41 | import { createUser, type User } from "../src/core/users.ts"; |
| 42 | import { createToken } from "../src/core/tokens.ts"; | |
| 41 | 43 | import type { Repo } from "../src/core/repos.ts"; |
| 42 | 44 | import { serve, type Servers } from "../src/http/server.ts"; |
| 43 | 45 | import { readRunLog } from "../src/runs/log.ts"; |
| @@ -727,6 +729,104 @@ describe("checks", () => { | ||
| 727 | 729 | expect(done.state).toBe("ok"); |
| 728 | 730 | }, 60_000); |
| 729 | 731 | |
| 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 | ||
| 730 | 830 | test("a check that is declared wrong fails with the reason", async () => { |
| 731 | 831 | const wrong = await commitToBranch(app, alice, repo, { |
| 732 | 832 | message: "A wrong check", |
new above
offline