diff --git a/.agents/skills/work-issue/references/ci-and-merge.md b/.agents/skills/work-issue/references/ci-and-merge.md index 3d880351cd..e72a6c9518 100644 --- a/.agents/skills/work-issue/references/ci-and-merge.md +++ b/.agents/skills/work-issue/references/ci-and-merge.md @@ -405,6 +405,12 @@ git fetch origin main It exits 1 when either moved. `unchanged` says which, and `baseGained` lists how many files the base gained and which of them this pull request also +changes. GitHub lists at most 300 files of a comparison and 3000 of a pull +request. When either list was cut short, `baseGained.overlapIncomplete` says +which: `overlapsThisPr` is then a lower bound, and an empty one does not mean +the base moved clear of this pull request. Read both lists from Git instead: +`git diff --name-only "$verified_base" origin/main` for what the base gained, +and `git diff --name-only origin/main...HEAD` for what the pull request changes. A head mismatch requires new inspection. A base mismatch requires assessment of diff --git a/scripts/pr-status.ts b/scripts/pr-status.ts index c9857cacb7..1998a1f9d1 100644 --- a/scripts/pr-status.ts +++ b/scripts/pr-status.ts @@ -10,7 +10,9 @@ * Green is a claim about a SHA: every answer here is about the head it names. * `--expect-head` and `--expect-base` re-check an earlier inspection and exit * 1 when either moved, listing the files the base gained and which of them - * the pull request also changes. `--failures` names each failed job and step; + * the pull request also changes. When GitHub returned only part of either + * list, `baseGained.overlapIncomplete` says so: an empty `overlapsThisPr` + * beside it is not "no overlap". `--failures` names each failed job and step; * with `--logs` it saves each failed job's log and prints only its failing * lines. `reviewRecord` is the newest recorded independent review among the * comments (`scripts/review-receipt.ts`); whether the head needs one, and @@ -73,6 +75,19 @@ export function failingLines(log: string, limit = 30): string[] { return control ? [...cut, `(${control} line${control === 1 ? "" : "s"} of a deliberate expected-failure control left out)`] : cut; } +/** GitHub's comparison of two commits lists at most this many changed files, and does not page them. */ +const COMPARE_FILE_LIMIT = 300; + +/** + * Every file a pull request changes. `gh pr view --json files` stops at 100; + * this list pages, and GitHub ends it at 3000 files, so the caller compares + * its length with the pull request's `changedFiles`. + */ +function changedFiles(gh: Gh, repo: string, pr: number): string[] { + return must(gh, ["api", "--paginate", `repos/${repo}/pulls/${pr}/files?per_page=100`, "--jq", ".[] | .filename | tojson"]) + .split("\n").filter(Boolean).map(line => String(JSON.parse(line))); +} + export interface StatusIo { gh?: Gh; out: (text: string) => void } export async function main(argv: readonly string[], io: StatusIo): Promise { @@ -92,9 +107,9 @@ export async function main(argv: readonly string[], io: StatusIo): Promise io.out(JSON.stringify(value, null, flags.has("pretty") ? 2 : undefined)); const pr = JSON.parse(must(gh, ["pr", "view", String(number), "--repo", repo, "--json", - "state,isDraft,headRefOid,baseRefName,mergeable,mergeStateStatus,reviewDecision,body,files,statusCheckRollup"])) as { + "state,isDraft,headRefOid,baseRefName,mergeable,mergeStateStatus,reviewDecision,body,changedFiles,statusCheckRollup"])) as { state: string; isDraft: boolean; headRefOid: string; baseRefName: string; mergeable: string; mergeStateStatus: string; - reviewDecision: string; body: string; files: Array<{ path: string }>; statusCheckRollup: Rollup[] | null }; + reviewDecision: string; body: string; changedFiles: number; statusCheckRollup: Rollup[] | null }; const runs = (JSON.parse(must(gh, ["run", "list", "--repo", repo, "--commit", pr.headRefOid, "--limit", "50", "--json", "databaseId,workflowName,event,attempt,status,conclusion"])) as Array<{ databaseId: number; workflowName: string; event: string; attempt: number; status: string; conclusion: string }>) .map(run => ({ id: run.databaseId, workflow: run.workflowName, event: run.event, attempt: run.attempt, status: run.status, conclusion: run.conclusion || null })); @@ -149,8 +164,12 @@ export async function main(argv: readonly string[], io: StatusIo): Promise file.path)); - status.baseGained = { files: gained.length, overlapsThisPr: gained.filter(file => mine.has(file)) }; + const mine = changedFiles(gh, repo, number), changed = new Set(mine); + // Either list can be cut short by GitHub. An overlap read from part of a list is not the whole overlap. + const incomplete: string[] = []; + if (gained.length >= COMPARE_FILE_LIMIT) incomplete.push(`the base gained ${COMPARE_FILE_LIMIT} files or more, and GitHub's comparison lists only the first ${COMPARE_FILE_LIMIT}`); + if (mine.length < pr.changedFiles) incomplete.push(`read ${mine.length} of the ${pr.changedFiles} files this pull request changes`); + status.baseGained = { files: gained.length, overlapsThisPr: gained.filter(file => changed.has(file)), ...(incomplete.length ? { overlapIncomplete: incomplete } : {}) }; } } print(status); diff --git a/scripts/skill-tests/pr-status.test.ts b/scripts/skill-tests/pr-status.test.ts index 12d68deeaf..5eb5325d91 100644 --- a/scripts/skill-tests/pr-status.test.ts +++ b/scripts/skill-tests/pr-status.test.ts @@ -70,13 +70,18 @@ describe("reading a job log", () => { }); }); -/** Answers the `gh` calls of one pull request; `overrides` replaces single answers. */ -function github(overrides: Record = {}): Gh { - const view = { state: "OPEN", isDraft: false, headRefOid: HEAD, baseRefName: "main", mergeable: "MERGEABLE", mergeStateStatus: "CLEAN", reviewDecision: "", - body: `Refs #12\n\n${RECEIPT_START}\n{}\n`, files: [{ path: "packages/ithaca/tests/oars.test.ts" }, { path: "docs/voyage.md" }], +/** + * Answers the `gh` calls of one pull request that changes the files in + * `changed`; `overrides` replaces single answers. It caps the two file lists + * where GitHub does: `gh pr view --json files` returns the first 100, and + * `pulls//files` returns the first 3000, one page of 30 unless the call + * asks for more per page and for every page. + */ +function github(overrides: Record = {}, changed: readonly string[] = ["packages/ithaca/tests/oars.test.ts", "docs/voyage.md"]): Gh { + const view: Record = { state: "OPEN", isDraft: false, headRefOid: HEAD, baseRefName: "main", mergeable: "MERGEABLE", mergeStateStatus: "CLEAN", reviewDecision: "", + body: `Refs #12\n\n${RECEIPT_START}\n{}\n`, files: changed.slice(0, 100).map(path => ({ path })), changedFiles: changed.length, statusCheckRollup: [run("proof", "COMPLETED", "SUCCESS"), run("unit (1)", "COMPLETED", "SUCCESS")] }; const answers: Record = { - "pr view": { stdout: JSON.stringify(view) }, "run list": { stdout: JSON.stringify([{ databaseId: 7, workflowName: "CI", event: "pull_request", attempt: 1, status: "completed", conclusion: "failure" }]) }, [`repos/${REPO}/git/ref/heads/main`]: { stdout: `${BASE}\n` }, [`repos/${REPO}/git/ref/pull/42/merge`]: { stdout: `${MERGE}\n` }, @@ -90,8 +95,14 @@ function github(overrides: Record = { ...overrides, }; return args => { - const key = args[0] === "api" ? args.find(arg => arg.startsWith("repos/"))! : `${args[0]} ${args[1]}`; - const answer = answers[key]; + const [key = "", query = ""] = args[0] === "api" ? args.find(arg => arg.startsWith("repos/"))!.split("?") : [`${args[0]} ${args[1]}`]; + let answer = answers[key]; + // Like `gh`, the view holds only the fields the call names. + if (!answer && key === "pr view") answer = { stdout: JSON.stringify(Object.fromEntries(args[args.indexOf("--json") + 1]!.split(",").map(field => [field, view[field]]))) }; + if (!answer && key === `repos/${REPO}/pulls/42/files`) { + const listed = changed.slice(0, 3000), page = Math.min(Number(new URLSearchParams(query).get("per_page") ?? 30), 100); + answer = { stdout: (args.includes("--paginate") ? listed : listed.slice(0, page)).map(path => JSON.stringify(path)).join("\n") }; + } return answer ? { code: answer.code ?? 0, stdout: answer.stdout, stderr: answer.code ? "gh: Not Found (HTTP 404)" : "" } : { code: 1, stdout: "", stderr: `unexpected gh ${args.join(" ")}` }; }; } @@ -141,6 +152,28 @@ describe("one pull request's state", () => { expect(result.json.baseGained).toEqual({ files: 2, overlapsThisPr: ["packages/ithaca/tests/oars.test.ts"] }); }); + // #4114: `gh pr view --json files` returns the first 100 files, so an overlap past them read as none. + test("a moved base's overlap is named when it lies past the first hundred files the pull request changes", async () => { + const changed = [...Array.from({ length: 149 }, (_, index) => `packages/ithaca/src/oar-${index}.ts`), "packages/pylos/src/sands.ts"]; + const result = await status(github({}, changed), "--expect-base", OLD_BASE); + expect(result.json.baseGained).toEqual({ files: 2, overlapsThisPr: ["packages/pylos/src/sands.ts"] }); + }); + + test("an overlap list read from fewer files than the pull request changes says that it is incomplete", async () => { + // The pull request's file list ends at 3000 files; the overlapping file is the 3001st. + const changed = [...Array.from({ length: 3000 }, (_, index) => `packages/ithaca/src/oar-${index}.ts`), "packages/pylos/src/sands.ts"]; + const result = await status(github({}, changed), "--expect-base", OLD_BASE); + expect(result.json.baseGained).toEqual({ files: 2, overlapsThisPr: [], + overlapIncomplete: ["read 3000 of the 3001 files this pull request changes"] }); + }); + + test("an overlap list read from a comparison at GitHub's 300-file limit says that it is incomplete", async () => { + const gained = ["docs/voyage.md", ...Array.from({ length: 299 }, (_, index) => `packages/pylos/src/dune-${index}.ts`)]; + const result = await status(github({ [`repos/${REPO}/compare/${OLD_BASE}...${BASE}`]: { stdout: JSON.stringify(gained) } }), "--expect-base", OLD_BASE); + expect(result.json.baseGained).toEqual({ files: 300, overlapsThisPr: ["docs/voyage.md"], + overlapIncomplete: ["the base gained 300 files or more, and GitHub's comparison lists only the first 300"] }); + }); + test("a moved head fails the recheck", async () => { expect((await status(github(), "--expect-head", "e".repeat(40))).code).toBe(1); });