Public: anyone can read this; the crew can change it.
A reviewer fetches a cross-repo pull's head before it reviews #14971
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/14932-a-reviewer-fetches-a-cross-repo-pull-s-h → main 3 commits fork
3 files
changed +320
−0
src/crew/review_head.ts added +112
| @@ -0,0 +1,112 @@ | ||
| 1 | /** | |
| 2 | * The pull's head, in the reviewer's clone, before the review runs (#14932). | |
| 3 | * | |
| 4 | * A reviewer's clone is its own fork at a scratch branch cut from the base's | |
| 5 | * `refs/heads/*`. A cross-repo pull's head lives in another fork, which the | |
| 6 | * run's token cannot read (`http/run_gate.ts`), so the head was never there: | |
| 7 | * the reviewer saw an empty diff, said so, and its "changes requested" | |
| 8 | * parked a green pull for nothing. | |
| 9 | * | |
| 10 | * So the forge brings it, not the agent. The base's snapshot of the head | |
| 11 | * (`refs/pull/N/head`) is brought up to the fork's branch first, on this | |
| 12 | * machine's disk (`refreshPullHead`) — no credential for the fork is minted | |
| 13 | * and no remote of the clone names it, so the reviewer gains no access to the | |
| 14 | * fork, read or write. The clone then fetches the snapshot from the base over | |
| 15 | * the run's own read of it, and the scratch branch is checked out at the head: | |
| 16 | * `git diff origin/<base>...HEAD` is the pull's diff, the same files | |
| 17 | * `GET /pulls/N` shows. | |
| 18 | * | |
| 19 | * A head that still cannot be had is no verdict: the run says why, the pull | |
| 20 | * gets one line saying so (`noteUnreviewed`), and the run ends failed, which | |
| 21 | * the review sweep counts as an attempt and sends once more. | |
| 22 | */ | |
| 23 | ||
| 24 | import type { Database } from "bun:sqlite"; | |
| 25 | import { addComment, listComments } from "../core/issues.ts"; | |
| 26 | import { refreshPullHead } from "../core/following.ts"; | |
| 27 | import { getPull, type Pull } from "../core/pulls.ts"; | |
| 28 | import type { Repo } from "../core/repos.ts"; | |
| 29 | import { isSafeRef } from "../git/guard.ts"; | |
| 30 | import { runCloneGit } from "./clone_git.ts"; | |
| 31 | ||
| 32 | export interface ReviewHeadInput { | |
| 33 | readonly forge: { db: Database; reposRoot: string }; | |
| 34 | readonly repo: Repo; | |
| 35 | readonly pull: Pull; | |
| 36 | /** The reviewer's clone. */ | |
| 37 | readonly dir: string; | |
| 38 | /** The base repository's git address as the forge fetches it (`localUrl`). */ | |
| 39 | readonly url: string; | |
| 40 | /** The reviewer's scratch branch, checked out at the head. */ | |
| 41 | readonly branch: string; | |
| 42 | readonly env: Record<string, string>; | |
| 43 | readonly timeoutMs?: number; | |
| 44 | } | |
| 45 | ||
| 46 | export type ReviewHead = | |
| 47 | | { ok: true; oid: string } | |
| 48 | | { ok: false; oid: string; why: string }; | |
| 49 | ||
| 50 | /** Where the base keeps the pull's head: its snapshot for a fork's pull, else the branch. */ | |
| 51 | export function headRefOf(pull: Pick<Pull, "crossRepo" | "number" | "headBranch">): string { | |
| 52 | return pull.crossRepo ? `refs/pull/${pull.number}/head` : `refs/heads/${pull.headBranch}`; | |
| 53 | } | |
| 54 | ||
| 55 | /** Check the reviewer's clone out at the pull's head oid, fetched first when it is not there. */ | |
| 56 | export async function checkoutReviewHead(input: ReviewHeadInput): Promise<ReviewHead> { | |
| 57 | const { forge, repo, dir, env } = input; | |
| 58 | // The fork's push reaches the base's snapshot by a fetch on this disk. | |
| 59 | if (input.pull.crossRepo) await refreshPullHead(forge, repo, input.pull); | |
| 60 | const pull = getPull(forge.db, repo.id, input.pull.number) ?? input.pull; | |
| 61 | const oid = pull.headOid; | |
| 62 | const short = oid.slice(0, 12); | |
| 63 | if (!/^[0-9a-f]{40,64}$/.test(oid)) return { ok: false, oid, why: `the pull's head ${short} is not an object id` }; | |
| 64 | const ref = headRefOf(pull); | |
| 65 | if (!isSafeRef(ref)) return { ok: false, oid, why: `the pull's head ref is not a safe name` }; | |
| 66 | const local = `refs/pull/${pull.number}/head`; | |
| 67 | const has = async (): Promise<boolean> => | |
| 68 | (await runCloneGit(dir, ["cat-file", "-e", `${oid}^{commit}`], { env })).ok; | |
| 69 | const opts = { env, timeoutMs: input.timeoutMs ?? 10 * 60_000 }; | |
| 70 | let failure = ""; | |
| 71 | if (!(await has())) { | |
| 72 | const byRef = await runCloneGit(dir, ["fetch", "--quiet", input.url, `+${ref}:${local}`], opts); | |
| 73 | if (!byRef.ok) failure = byRef.error.stderr; | |
| 74 | } | |
| 75 | // The ref may have moved past the row's head, or be gone: ask for the oid itself. | |
| 76 | if (!(await has())) { | |
| 77 | const byOid = await runCloneGit(dir, ["fetch", "--quiet", input.url, oid], opts); | |
| 78 | if (!byOid.ok) failure = byOid.error.stderr; | |
| 79 | } | |
| 80 | if (!(await has())) | |
| 81 | return { | |
| 82 | ok: false, | |
| 83 | oid, | |
| 84 | why: `head ${short} is not in ${repo.ownerName}/${repo.name}'s ${ref}${failure === "" ? "" : `: ${oneLine(failure).slice(0, 160)}`}`, | |
| 85 | }; | |
| 86 | await runCloneGit(dir, ["update-ref", local, oid], { env }); | |
| 87 | await runCloneGit(dir, ["reset", "--quiet", "--hard"], { env }); | |
| 88 | const checked = await runCloneGit(dir, ["checkout", "--quiet", "-B", input.branch, oid], { env }); | |
| 89 | if (!checked.ok) | |
| 90 | return { ok: false, oid, why: `checkout of head ${short} failed: ${oneLine(checked.error.stderr).slice(0, 160)}` }; | |
| 91 | return { ok: true, oid }; | |
| 92 | } | |
| 93 | ||
| 94 | /** The line a pull gets when its head could not be reviewed; its start is how it is found again. */ | |
| 95 | export function unreviewedLine(oid: string, why: string): string { | |
| 96 | return `Not reviewed: head ${oid.slice(0, 12)} could not be fetched into the reviewer's clone (${why}). This is no verdict; the review is tried once more.`; | |
| 97 | } | |
| 98 | ||
| 99 | /** Say on the pull, once per head, why it was not reviewed. Answers whether it said it now. */ | |
| 100 | export function noteUnreviewed( | |
| 101 | db: Database, | |
| 102 | pull: Pick<Pull, "id">, | |
| 103 | authorId: string, | |
| 104 | oid: string, | |
| 105 | why: string, | |
| 106 | ): boolean { | |
| 107 | const start = `Not reviewed: head ${oid.slice(0, 12)} `; | |
| 108 | if (listComments(db, pull.id).some((c) => c.body.startsWith(start))) return false; | |
| 109 | return addComment(db, { issueId: pull.id, authorId, body: unreviewedLine(oid, why) }).ok; | |
| 110 | } | |
| 111 | ||
| 112 | const oneLine = (text: string): string => text.replace(/\s+/g, " ").trim(); |
src/crew/runner.ts +22
| @@ -105,6 +105,7 @@ import { installPinHook } from "./pin_hook.ts"; | ||
| 105 | 105 | import { cloneWithMirror } from "./mirror.ts"; |
| 106 | 106 | import { Hops } from "./hops.ts"; |
| 107 | 107 | import { isSafeRef } from "../git/guard.ts"; |
| 108 | import { checkoutReviewHead, noteUnreviewed } from "./review_head.ts"; | |
| 108 | 109 | import { runGitCapture } from "../git/env.ts"; |
| 109 | 110 | import { looksForEnv } from "../core/looks.ts"; |
| 110 | 111 | import { aheadCount, contentOnBase, resolveOid } from "../git/merge.ts"; |
| @@ -1234,6 +1235,27 @@ async function work( | ||
| 1234 | 1235 | if (!prepared.ok) return { state: "failed", summary: prepared.error }; |
| 1235 | 1236 | // Counted once made: a clone that failed is no hop on the line. |
| 1236 | 1237 | ran.hops.record(refresh, options.localUrl); |
| 1238 | // A reviewer reads the pull's head, not main: fetched by the forge from | |
| 1239 | // the base's snapshot of it, never from the fork (`review_head.ts`). A | |
| 1240 | // sealed pull's snapshot lives apart from the base, out of this reach. | |
| 1241 | if (reviewing && pull !== undefined && !pull.sealed) { | |
| 1242 | const head = await checkoutReviewHead({ | |
| 1243 | forge: app, | |
| 1244 | repo, | |
| 1245 | pull, | |
| 1246 | dir, | |
| 1247 | url: `${options.localUrl}/${repo.ownerName}/${repo.name}.git`, | |
| 1248 | branch, | |
| 1249 | env: { ...gitAuth, ...identity }, | |
| 1250 | timeoutMs: GIT_TRANSFER_MS, | |
| 1251 | }); | |
| 1252 | if (!head.ok) { | |
| 1253 | write(`not reviewed: ${head.why}`); | |
| 1254 | noteUnreviewed(app.db, pull, agent.user.id, head.oid, head.why); | |
| 1255 | return { state: "failed", summary: `not reviewed: ${head.why}` }; | |
| 1256 | } | |
| 1257 | write(`reviewing head ${head.oid.slice(0, 12)}, checked out as ${branch}`); | |
| 1258 | } | |
| 1237 | 1259 | // Dependencies before the agent starts, so the type check has its types; |
| 1238 | 1260 | // a failure is logged and the run goes on. |
| 1239 | 1261 | const deps = await installDependencies( |
test/review-head-fetch.test.ts added +186
| @@ -0,0 +1,186 @@ | ||
| 1 | /** | |
| 2 | * A reviewer fetches a cross-repo pull's head before it reviews (#14932). | |
| 3 | * | |
| 4 | * One agent's pull from its fork, pushed again after it opened, so the base's | |
| 5 | * snapshot is a push behind: the reviewer's clone, made the way the runner | |
| 6 | * makes it (the base over HTTP with the run's token), is checked out at the | |
| 7 | * head and its diff against the base names the same files as `GET /pulls/N`. | |
| 8 | * The fork is never one of the clone's remotes. A head nobody has is no | |
| 9 | * verdict: one line on the pull, said once. | |
| 10 | */ | |
| 11 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 12 | import { chmod, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; | |
| 13 | import { tmpdir } from "node:os"; | |
| 14 | import { join } from "node:path"; | |
| 15 | import { addToCrew, createAgent } from "../src/actions/crew.ts"; | |
| 16 | import { openPull } from "../src/actions/pulls.ts"; | |
| 17 | import { commitToBranch, createRepo } from "../src/actions/repos.ts"; | |
| 18 | import { createApp, type App } from "../src/app.ts"; | |
| 19 | import { listComments } from "../src/core/issues.ts"; | |
| 20 | import { getPull, type Pull } from "../src/core/pulls.ts"; | |
| 21 | import type { Repo } from "../src/core/repos.ts"; | |
| 22 | import { mintRunToken } from "../src/core/run_token.ts"; | |
| 23 | import { createRun, getRun } from "../src/core/runs.ts"; | |
| 24 | import type { User } from "../src/core/users.ts"; | |
| 25 | import { runGitAuth } from "../src/crew/git_auth.ts"; | |
| 26 | import { checkoutReviewHead, noteUnreviewed } from "../src/crew/review_head.ts"; | |
| 27 | import { executeAgentRun, forkFor } from "../src/crew/runner.ts"; | |
| 28 | import { runGitCapture } from "../src/git/env.ts"; | |
| 29 | import { forgeHttp } from "../src/http/server.ts"; | |
| 30 | import { readRunLog } from "../src/runs/log.ts"; | |
| 31 | import { seedAdmin } from "./helpers/admin.ts"; | |
| 32 | import { gitClientEnv } from "./helpers/git-client.ts"; | |
| 33 | import { quietLogs } from "./helpers/globals.ts"; | |
| 34 | ||
| 35 | quietLogs(); | |
| 36 | ||
| 37 | let dataDir: string; | |
| 38 | let app: App; | |
| 39 | let server: ReturnType<typeof Bun.serve>; | |
| 40 | let repo: Repo; | |
| 41 | let authorFork: Repo; | |
| 42 | let pull: Pull; | |
| 43 | let reviewer: User; | |
| 44 | let env: Record<string, string>; | |
| 45 | let url: string; | |
| 46 | let token: string; | |
| 47 | ||
| 48 | const git = async (dir: string, ...args: string[]): Promise<string> => { | |
| 49 | const out = await runGitCapture(args, { cwd: dir, env: gitClientEnv(env) }); | |
| 50 | if (out.code !== 0) throw new Error(`git ${args.join(" ")}: ${out.stderr}`); | |
| 51 | return out.stdout.trim(); | |
| 52 | }; | |
| 53 | ||
| 54 | async function cloneAsReviewer(): Promise<string> { | |
| 55 | const dir = await mkdtemp(join(dataDir, "clone-")); | |
| 56 | await git(dataDir, "clone", "--quiet", `${url}/alice/lib.git`, dir); | |
| 57 | return dir; | |
| 58 | } | |
| 59 | ||
| 60 | beforeAll(async () => { | |
| 61 | process.env["FORGE_CELLS"] = "off"; | |
| 62 | dataDir = await mkdtemp(join(tmpdir(), "forge-review-head-")); | |
| 63 | app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 64 | const admin = await seedAdmin(app, { username: "alice", email: "[email protected]", password: "correct-horse-battery" }); | |
| 65 | if (!admin.ok) throw new Error(admin.error); | |
| 66 | const alice = admin.value; | |
| 67 | const made = await createRepo(app, alice, { name: "lib", visibility: "private", description: "" }); | |
| 68 | if (!made.ok) throw new Error(String(made.error)); | |
| 69 | repo = made.value; | |
| 70 | const first = await commitToBranch(app, alice, repo, { | |
| 71 | message: "start", | |
| 72 | files: [{ path: "forge.json", content: "{}" }, { path: "src/a.ts", content: "export const a = 1;\n" }], | |
| 73 | }); | |
| 74 | if (!first.ok) throw new Error(JSON.stringify(first.error)); | |
| 75 | const agent = async (name: string) => { | |
| 76 | const made = await createAgent(app, alice, { username: name }); | |
| 77 | if (!made.ok) throw new Error(JSON.stringify(made.error)); | |
| 78 | if (!addToCrew(app, alice, repo, name).ok) throw new Error("crew"); | |
| 79 | const fork = await forkFor(app, made.value, repo, () => {}); | |
| 80 | if (!fork.ok) throw new Error(fork.error); | |
| 81 | return { agent: made.value, fork: fork.value }; | |
| 82 | }; | |
| 83 | const author = await agent("juniper"); | |
| 84 | const seat = await agent("sentinel"); | |
| 85 | authorFork = author.fork; | |
| 86 | reviewer = seat.agent.user; | |
| 87 | const push = async (message: string, path: string) => { | |
| 88 | const done = await commitToBranch(app, author.agent.user, authorFork, { | |
| 89 | branch: "juniper/1-work", | |
| 90 | base: "main", | |
| 91 | message, | |
| 92 | files: [{ path, content: `export const x = "${message}";\n` }], | |
| 93 | }); | |
| 94 | if (!done.ok) throw new Error(JSON.stringify(done.error)); | |
| 95 | }; | |
| 96 | await push("one", "src/one.ts"); | |
| 97 | const opened = await openPull(app, author.agent.user, repo, { | |
| 98 | title: "work", | |
| 99 | body: "", | |
| 100 | baseBranch: "main", | |
| 101 | headBranch: "juniper/1-work", | |
| 102 | headRepo: authorFork, | |
| 103 | }); | |
| 104 | if (!opened.ok) throw new Error(JSON.stringify(opened.error)); | |
| 105 | // Pushed again after the pull opened: the base's snapshot is behind the fork. | |
| 106 | await push("two", "src/two.ts"); | |
| 107 | pull = getPull(app.db, repo.id, opened.value.number)!; | |
| 108 | expect(pull.crossRepo).toBe(true); | |
| 109 | ||
| 110 | const http = forgeHttp(app); | |
| 111 | server = Bun.serve({ port: 0, hostname: "127.0.0.1", fetch: (r) => http.handle(r) }); | |
| 112 | url = `http://127.0.0.1:${server.port}`; | |
| 113 | token = mintRunToken(app.db, reviewer.id, "run-review", { repoId: repo.id, forkId: seat.fork.id }, new Date(Date.now() + 3_600_000).toISOString()).token; | |
| 114 | env = runGitAuth([url], reviewer.username, token); | |
| 115 | }, 60_000); | |
| 116 | ||
| 117 | afterAll(async () => { | |
| 118 | server?.stop(true); | |
| 119 | app?.close(); | |
| 120 | await rm(dataDir, { recursive: true, force: true }); | |
| 121 | }); | |
| 122 | ||
| 123 | describe("a reviewer's clone of a cross-repo pull", () => { | |
| 124 | test("is checked out at the fork's head and diffs to the same files as GET /pulls/N", async () => { | |
| 125 | const dir = await cloneAsReviewer(); | |
| 126 | const head = await checkoutReviewHead({ forge: app, repo, pull, dir, url: `${url}/alice/lib.git`, branch: "sentinel/review", env }); | |
| 127 | if (!head.ok) throw new Error(head.why); | |
| 128 | const forkTip = await git(join(app.reposRoot, authorFork.ownerName, `${authorFork.name}.git`), "rev-parse", "refs/heads/juniper/1-work"); | |
| 129 | expect(head.oid).toBe(forkTip); | |
| 130 | expect(await git(dir, "rev-parse", "HEAD")).toBe(forkTip); | |
| 131 | expect(await git(dir, "rev-parse", "--abbrev-ref", "HEAD")).toBe("sentinel/review"); | |
| 132 | const seen = (await git(dir, "diff", "--name-only", "origin/main...HEAD")).split("\n").sort(); | |
| 133 | const res = await http("GET", `/api/v1/repos/alice/lib/pulls/${pull.number}`); | |
| 134 | const api = ((await res.json()) as { diff: { files: { path: string }[] } }).diff.files.map((f) => f.path).sort(); | |
| 135 | expect(seen).toEqual(["src/one.ts", "src/two.ts"]); | |
| 136 | expect(seen).toEqual(api); | |
| 137 | // Nothing of the fork reaches the clone's config: no remote, no URL. | |
| 138 | expect(await git(dir, "config", "--get-regexp", "^remote\\.")).not.toContain("juniper"); | |
| 139 | }, 60_000); | |
| 140 | ||
| 141 | test("a review run's agent starts in a clone at the head, with the pull's files in its diff", async () => { | |
| 142 | const seen = join(dataDir, "seen.txt"); | |
| 143 | const bin = join(dataDir, "claude-review"); | |
| 144 | await writeFile( | |
| 145 | bin, | |
| 146 | `#!/bin/sh\n{ git rev-parse HEAD; git diff --name-only origin/main...HEAD; } > '${seen}'\necho done\nexit 1\n`, | |
| 147 | ); | |
| 148 | await chmod(bin, 0o755); | |
| 149 | const run = createRun(app.db, { repoId: repo.id, kind: "agent", issueId: pull.id, actorId: reviewer.id, reason: "review" }); | |
| 150 | await executeAgentRun(app, getRun(app.db, run.id)!, { | |
| 151 | forgeUrl: url, localUrl: url, binary: bin, concurrency: 1, budgetUsd: 1, runTimeoutMs: 30_000, | |
| 152 | }); | |
| 153 | const head = getPull(app.db, repo.id, pull.number)!.headOid; | |
| 154 | expect((await readFile(seen, "utf8")).trim().split("\n")).toEqual([head, "src/one.ts", "src/two.ts"]); | |
| 155 | expect(readRunLog(app.config.dataDir, run.id).lines.join("\n")).toContain(`reviewing head ${head.slice(0, 12)}`); | |
| 156 | }, 60_000); | |
| 157 | ||
| 158 | test("a head nobody has is not reviewed, and the pull says why once", async () => { | |
| 159 | const dir = await cloneAsReviewer(); | |
| 160 | const ghost = "f".repeat(40); | |
| 161 | app.db.query("UPDATE pull SET head_oid = ?2 WHERE issue_id = ?1").run(pull.id, ghost); | |
| 162 | const gone = getPull(app.db, repo.id, pull.number)!; | |
| 163 | const head = await checkoutReviewHead({ forge: app, repo, pull: gone, dir, url: `${url}/alice/lib.git`, branch: "sentinel/review", env }); | |
| 164 | expect(head.ok).toBe(false); | |
| 165 | if (head.ok) return; | |
| 166 | expect(head.why).toContain(`head ${ghost.slice(0, 12)}`); | |
| 167 | expect(noteUnreviewed(app.db, gone, reviewer.id, head.oid, head.why)).toBe(true); | |
| 168 | expect(noteUnreviewed(app.db, gone, reviewer.id, head.oid, head.why)).toBe(false); | |
| 169 | const lines = listComments(app.db, pull.id).filter((c) => c.body.startsWith("Not reviewed:")); | |
| 170 | expect(lines).toHaveLength(1); | |
| 171 | expect(lines[0]!.body).toContain("This is no verdict"); | |
| 172 | // Through the runner: a failed run, no verdict, and still the one line. | |
| 173 | const run = createRun(app.db, { repoId: repo.id, kind: "agent", issueId: pull.id, actorId: reviewer.id, reason: "review" }); | |
| 174 | await executeAgentRun(app, getRun(app.db, run.id)!, { | |
| 175 | forgeUrl: url, localUrl: url, binary: "/bin/false", concurrency: 1, budgetUsd: 1, runTimeoutMs: 30_000, | |
| 176 | }); | |
| 177 | const after = getRun(app.db, run.id)!; | |
| 178 | expect(after.state).toBe("failed"); | |
| 179 | expect(after.summary).toStartWith(`not reviewed: head ${ghost.slice(0, 12)}`); | |
| 180 | expect(listComments(app.db, pull.id).filter((c) => c.body.startsWith("Not reviewed:"))).toHaveLength(1); | |
| 181 | }, 60_000); | |
| 182 | }); | |
| 183 | ||
| 184 | function http(method: string, path: string): Promise<Response> { | |
| 185 | return fetch(`${url}${path}`, { method, headers: { authorization: `Bearer ${token}` } }); | |
| 186 | } |
new above
offline