diff --git a/README.md b/README.md index 5832bfcf..f5302c5e 100644 --- a/README.md +++ b/README.md @@ -1643,8 +1643,11 @@ branch's rounds, `risk`, the head's tier with its classification, `record`, and `next`. After each round, `quest review` rewrites the `## Review` section of the open PR -from the branch into the base, found among the upstream's PRs from the -repository the base's remote pushes to, and prints `record `. The record +from the branch into the base, found among the upstream's PRs: the one from +the repository the base's remote pushes to, else the only one from another +repository whose pushed head the checked-out `HEAD` contains, as in a +maintainer's checkout of a contributor's fork PR. It +prints `record `. The record names the reviewed head and the base's short SHA, the head's risk tier and who classified it, then for each reviewer its harness, model, and effort, its Reviewer line ending in `, shadow` for the diff --git a/quest/m0/fork-review-record.md b/quest/m0/fork-review-record.md deleted file mode 100644 index 8a3ad3be..00000000 --- a/quest/m0/fork-review-record.md +++ /dev/null @@ -1,33 +0,0 @@ -# [S] quest review records on a contributor's fork PR - -## Goal - -`quest review` in a maintainer's checkout of a contributor's fork PR finds -that PR, reads its `## Models` size, and writes its `## Review` record, so -nobody writes one by hand. Contributors reviewing in their own fork's clone -keep working as today. - -## Plan - -Decided 2026-11-17 (maintainer, `/quest-plan` after `/quest-run`): ✅ m0 -(rejected: m1), since every fork PR's local review now needs a hand-written -record that `/quest-merge` then trusts; ✅ fix the lookup (rejected: a -`--pr ` flag; dropping it). - -- Cause: `openPull` in `src/cli/review-record.ts` matches only an open PR - whose head repository is the one the base's remote pushes to. In a - maintainer's checkout that is the upstream, not the contributor's fork, so - it prints `record skipped: no open PR from ...`, and `pullModels` returns - null, so the round runs at the default size. Both iterates on - [#761](https://github.com/moq-dev/quest/pull/761) hit this: they reviewed - at size M instead of the body's S and wrote the record by hand. -- Those iterates worked in a detached checkout with `--branch` and pushed to - the fork by URL, so the checkout has no push remote naming the fork, and - the reviewed head is not pushed yet when the round ends. -- Recommended: match the open PR into the base whose head branch is the - reviewed branch, from any head repository; prefer the push repository's - when several match, and skip naming every candidate when the choice is - still ambiguous. -- Test: a stub forge with an open PR from `contributor/quest:` - into the upstream gets its record rewritten and its size read from a - maintainer's checkout whose remote is the upstream. diff --git a/src/cli/main.ts b/src/cli/main.ts index 23d0c5c8..54cab558 100644 --- a/src/cli/main.ts +++ b/src/cli/main.ts @@ -1714,12 +1714,12 @@ export function main(argv: string[], out: Output): number | Promise { "with the best long-term approach worked out from first principles before fixing them, `next respond` to fix or refute the findings, or `next done`. A reply without a valid score " + "fails as any malformed reply does. --json prints " + "one object: `rounds`, one per reviewer, `tallies`, every reviewer's findings across the branch's rounds, `risk`, the head's tier with its classification, `skipped`, each reviewer passed over with its reason, `record`, and `next`.\n\n" + - "After each round, it rewrites the `## Review` section of the branch's open PR into the base, as the " + + "After each round, it rewrites the `## Review` section of the branch's open PR into the base, the one from the repository the base's remote pushes to, else the only one from another repository whose pushed head `HEAD` contains, as the " + "user the GitHub token names, keeping the rest of the body, and prints `record `: the reviewed " + "head, named in full only once no reviewer reports a finding, the base, the head's risk tier and reason, " + "the classifier's tier and who set it, a `- Skipped:` line for each reviewer passed over, and each reviewer, its Reviewer line ending in `, shadow` for the shadow, its " + "session and round, score, the tier it raised the risk to and why, its tally of findings across the branch's rounds, and each finding with its disposition. With no " + - "such PR, or a remote off GitHub, it prints `record skipped: ` and changes nothing.\n\n" + + "such PR, several from other repositories that `HEAD` contains, or a remote off GitHub, it prints `record skipped: ` and changes nothing.\n\n" + "Exits 0 once the round finishes, whatever it found, 2 for invalid arguments " + "or a checkout that cannot be reviewed, 3 when no eligible reviewer could run, each not installed, logged " + "out, its login rejected, or at its usage limit, and 1 for other failures, including a PR GitHub will not show or a record it " + diff --git a/src/cli/review-record.ts b/src/cli/review-record.ts index 7ac5f180..ad0602bf 100644 --- a/src/cli/review-record.ts +++ b/src/cli/review-record.ts @@ -5,6 +5,7 @@ import { recorded } from "./models-record"; import { type ClaimDependencies, upstream } from "./claims"; import { pullsFrom } from "../github/claims"; import { git } from "./git"; +import { contains } from "./land"; import { requireRemote } from "./github"; import { type PullModels, @@ -296,9 +297,9 @@ export function withReview(body: string, record: string): string { export type Recorded = { url: string } | { skipped: string }; /** - * The open PR from `branch` into the base `/` names, found among the upstream's PRs from the - * repository the base's remote pushes to, as claims find a branch's PR; or why there is none. It reads the forge - * itself, never an index that may lag, since its body is rewritten. + * The open PR from `branch` into the base `/` names, or why there is none: the one from the repository + * the base's remote pushes to, else the only one from any repository whose head `HEAD` contains, as a maintainer's + * checkout of a contributor's fork PR finds it. It reads the forge itself, never an index that may lag, since its body is rewritten. */ async function openPull( root: string, @@ -314,10 +315,23 @@ async function openPull( if (hosting === null) return { skipped: `${remote} is not on GitHub or Kixel Quest` }; const { repository, head } = hosting; const name = `${head.owner}/${head.repo}`.toLowerCase(); - const pull = pullsFrom((await (await hosting.live()).pulls(repository)).value, branch).find( - (each) => each.base === base && each.head?.toLowerCase() === name, + const candidates = pullsFrom((await (await hosting.live()).pulls(repository)).value, branch).filter( + (each) => each.base === base, ); - if (pull === undefined) return { skipped: `no open PR from ${head.owner}:${branch} into ${base}` }; + const own = candidates.find((each) => each.head?.toLowerCase() === name); + // Another repository's PR counts only when this checkout holds its pushed head, so a stranger's PR from a + // branch of the same name neither sets the round's size nor receives its record. + const held = candidates.filter((each) => each.sha !== undefined && contains(top, each.sha, "HEAD")); + const pull = own ?? (held.length === 1 ? held[0] : undefined); + if (pull === undefined) + return { + skipped: + candidates.length === 0 + ? `no open PR from ${branch} into ${base}` + : held.length === 0 + ? `no open PR from ${branch} into ${base} from ${head.owner} or with a head this checkout holds: ${candidates.map((each) => each.url).join(", ")}` + : `several open PRs from ${branch} into ${base} with heads this checkout holds, none from ${head.owner}: ${held.map((each) => each.url).join(", ")}`, + }; return { url: pull.url, body: pull.body, diff --git a/src/core/forge.ts b/src/core/forge.ts index 3d9c8406..228f7ab6 100644 --- a/src/core/forge.ts +++ b/src/core/forge.ts @@ -210,6 +210,8 @@ export interface OpenPull { base: string; /** The head repository as `owner/repo`, or `null` once a fork is deleted. */ head: string | null; + /** The head commit, where the forge reports it. */ + sha?: string; draft: boolean; /** Epoch milliseconds. The earliest open PR on a branch holds the claim. */ created: number; @@ -604,6 +606,7 @@ const openPull = shape({ branch: name, base: name, head: nullable(name), + sha: maybe(sha), draft: boolean, created: count, updated: count, diff --git a/src/github/claims.ts b/src/github/claims.ts index 2010e631..680c7cd8 100644 --- a/src/github/claims.ts +++ b/src/github/claims.ts @@ -24,6 +24,7 @@ export async function openPullRequests(github: GitHubReader, repository: Reposit branch: string(head.ref), base: string(object(row.base).ref), head: repo === null ? null : string(repo.full_name), + ...(typeof head.sha === "string" ? { sha: head.sha } : {}), draft: row.draft === true, created: Date.parse(string(row.created_at)), updated: Date.parse(string(row.updated_at)), diff --git a/test/review.test.ts b/test/review.test.ts index 34ac31bd..29b99ee7 100644 --- a/test/review.test.ts +++ b/test/review.test.ts @@ -1093,7 +1093,8 @@ fakes("a change to the applicable instructions starts a new session that weighs * `upstream/widget`, whose root holds the quest tree, and the PRs are upstream's. Returns the PR bodies * each PATCH wrote. */ -type FakePull = { number: number; body: string | null }; +/** An open PR from `feature` into `main`, from `acme/widget` unless `head` names another repository. */ +type FakePull = { number: number; body: string | null; head?: string; sha?: string }; function onGitHub(repo: string, pulls: FakePull[], fork = false): { body: string }[] { const local = git(repo, "remote", "get-url", "origin"); git(repo, "remote", "set-url", "origin", "https://github.com/acme/widget.git"); @@ -1119,10 +1120,10 @@ function onGitHub(repo: string, pulls: FakePull[], fork = false): { body: string return url.pathname.endsWith("/contents/quest/README.md") ? json({}) : json({}, 404); if (url.pathname === `/repos/${owner}/widget/pulls`) { expect(url.searchParams.get("state")).toBe("open"); - const row = (p: FakePull, ref: string, base: string) => ({ + const row = ({ head, sha, ...p }: FakePull, ref: string, base: string) => ({ ...p, html_url: `https://github.com/${owner}/widget/pull/${p.number}`, - head: { ref, repo: { full_name: "acme/widget" } }, + head: { ref, repo: { full_name: head ?? "acme/widget" }, ...(sha === undefined ? {} : { sha }) }, base: { ref: base }, draft: false, created_at: "2026-10-01T00:00:00Z", @@ -1548,7 +1549,7 @@ fakes("a branch with no open PR, or a body without a Review section, is reported try { const none = await quest("--root", s.repo, "review"); expect(none.code).toBe(0); - expect(none.stdout).toContain("record skipped: no open PR from acme:feature into main\n"); + expect(none.stdout).toContain("record skipped: no open PR from feature into main\n"); pulls.push({ number: 8, body: null }); vi.stubEnv("FAKE_REPLY", JSON.stringify({ score: 10, rationale: "Lands as is.", findings: [] })); @@ -1565,6 +1566,58 @@ fakes("a branch with no open PR, or a body without a Review section, is reported } }); +fakes("a maintainer's checkout records on a contributor's fork PR and reads its size", async () => { + const s = setup(); + const pushed = git(s.repo, "rev-parse", "HEAD~1"); + const pull = { number: 12, body: "Body.\n\n## Models\n\n- Size: S\n", head: "contributor/widget", sha: pushed }; + // A stranger's PR from a branch of the same name, whose head this checkout lacks. + const stranger = { + number: 13, + body: "Stranger.\n\n## Models\n\n- Size: XS\n", + head: "stranger/widget", + sha: "f".repeat(40), + }; + const pulls: FakePull[] = [stranger]; + onGitHub(s.repo, pulls); + vi.stubEnv("FAKE_REPLY", JSON.stringify({ score: 10, rationale: "Lands as is.", findings: [] })); + try { + const alone = await quest("--root", s.repo, "review"); + expect(alone.code, alone.stderr).toBe(0); + expect(alone.stdout).toContain( + "record skipped: no open PR from feature into main from acme or with a head this checkout holds: https://github.com/acme/widget/pull/13\n", + ); + // Its size went unread: no size picks the default. + expect(alone.stdout).not.toContain(" low shadow\n"); + expect(stranger.body).toBe("Stranger.\n\n## Models\n\n- Size: XS\n"); + + pulls.push(pull); + const result = await quest("--root", s.repo, "review"); + expect(result.code, result.stderr).toBe(0); + expect(result.stdout).toContain("record https://github.com/acme/widget/pull/12\n"); + // Size S picks the shadow one effort below medium. + expect(result.stdout).toContain(" low shadow\n"); + expect(pull.body).toMatch(/^Body\.\n\n## Models\n\n- Size: S\n\n## Review\n\nRewritten by `quest review`/); + expect(stranger.body).toBe("Stranger.\n\n## Models\n\n- Size: XS\n"); + + // A second contributor's PR whose head this checkout also holds leaves no way to tell them apart. + pulls.push({ number: 15, body: "Other.", head: "someone/widget", sha: pushed }); + const ambiguous = await quest("--root", s.repo, "review", "--size", "S"); + expect(ambiguous.code, ambiguous.stderr).toBe(0); + expect(ambiguous.stdout).toContain( + "record skipped: several open PRs from feature into main with heads this checkout holds, none from acme: https://github.com/acme/widget/pull/12, https://github.com/acme/widget/pull/15\n", + ); + expect(pulls[2]!.body).toBe("Other."); + + // The push repository's own PR wins over any fork's. + pulls.push({ number: 14, body: "Own." }); + const own = await quest("--root", s.repo, "review", "--size", "S"); + expect(own.stdout).toContain("record https://github.com/acme/widget/pull/14\n"); + expect(pulls[2]!.body).toBe("Other."); + } finally { + vi.unstubAllGlobals(); + } +}); + fakes("a review from a subdirectory of a fork records on the upstream PR", async () => { const s = setup(); const pull = { number: 11, body: "Body." };