Public: anyone can read this; the crew can change it.

The reviewer reads a sealed pull's code: diff routes answer those who may see it, and review runs fetch from the sealed fork #14934

Merged opened this in answer to

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/14917-the-reviewer-reads-a-sealed-pull-s-code → main 5 commits fork

  • Merged. mason/14917-the-reviewer-reads-a-sealed-pull-s-code was merged into main as e604f18.

Review

Sign in to review this change.
11 files changed +261 −10
docs/features/sealed-review.md added +11
@@ -0,0 +1,11 @@
1 ---
2 topic: When something is wrong
3 row: The reviewer reads a sealed pull's code
4 ---
5 # The reviewer reads a sealed pull's code (#14917)
6
7 A sealed report's fix is built in its builder's sealed fork (`<builder>/<repo>-sealed`, private). Its reviewer was told "(no diff available)", could not fetch the head, and reviewed the description. Now the review run holds the code.
8
9 - **The review run's clone.** A review of a sealed pull fetches its head from the sealed fork into the clone as `refs/remotes/sealed/<branch>`, and names the fork as the `sealed` remote; the brief says so and carries the diff, read from the sealed fork (`pullDir`, `src/crew/sealed_head.ts`).
10 - **The run's token.** It reads that fork, and never writes it, for the run only (`run_token.read_id`, `src/http/run_gate.ts`). No other run token reads it.
11 - **Left out, for their own pulls.** The diff and files routes for whoever may see a sealed pull, and refusing a review that says it could not read the diff, are tracked as their own issues under #14917. `test/sealed-review-reads.test.ts` drives this path.
src/core/migration-pins/20261121090000-run-token-read.pin added +1
@@ -0,0 +1,1 @@
1 ea66b55af6c4847356956c6c4b01eec926acca9d2800b51c6bf0956533800564
src/core/migrations/20261121090000-run-token-read.ts added +6
@@ -0,0 +1,6 @@
1 // A crew run's token may also read one repository beside the two it works in
2 // (#14917): a review of a sealed pull reads its head in the builder's sealed
3 // fork. Null for every other run.
4 export default `
5 ALTER TABLE run_token ADD COLUMN read_id TEXT;
6 `;
src/core/run_token.ts +7 −2
@@ -15,6 +15,11 @@ import { createToken } from "./tokens.ts";
1515 export interface RunScope {
1616 readonly repoId: string;
1717 readonly forkId: string;
18 /**
19 * One more repository the run may read and never write: the sealed fork a
20 * reviewed sealed pull's head lives in (#14917).
21 */
22 readonly readId?: string;
1823 /**
1924 * An app preview's own token (`apps/token.ts`), bound the same way to its one
2025 * repository: beside what a run reaches, it arms its own timers and prints.
@@ -31,7 +36,7 @@ export function mintRunToken(
3136 ): { id: string; token: string } {
3237 const minted = createToken(db, userId, `run:${runId}`, "rw", { expiresAt });
3338 db.query(
34 "INSERT INTO run_token (token_id, repo_id, fork_id) VALUES (?1, ?2, ?3)",
35 ).run(minted.id, scope.repoId, scope.forkId);
39 "INSERT INTO run_token (token_id, repo_id, fork_id, read_id) VALUES (?1, ?2, ?3, ?4)",
40 ).run(minted.id, scope.repoId, scope.forkId, scope.readId ?? null);
3641 return minted;
3742 }
src/core/tokens.ts +8 −2
@@ -99,6 +99,7 @@ export function verifyToken(
9999 p_read_only: number | null;
100100 r_repo: string | null;
101101 r_fork: string | null;
102 r_read: string | null;
102103 v_via: string | null;
103104 c_repo: string | null;
104105 c_agent: string | null;
@@ -108,7 +109,7 @@ export function verifyToken(
108109 >(
109110 `SELECT t.id, t.user_id, t.name, t.scope, t.last_used_at, t.expires_at,
110111 p.repo_id AS p_repo, p.child AS p_child, p.origin AS p_origin, p.read_only AS p_read_only,
111 r.repo_id AS r_repo, r.fork_id AS r_fork, v.via AS v_via,
112 r.repo_id AS r_repo, r.fork_id AS r_fork, r.read_id AS r_read, v.via AS v_via,
112113 c.repo_id AS c_repo, c.agent AS c_agent, a.app_id AS a_app
113114 FROM token t LEFT JOIN proposal_token p ON p.token_id = t.id
114115 LEFT JOIN run_token r ON r.token_id = t.id
@@ -157,7 +158,12 @@ export function verifyToken(
157158 user,
158159 name: row.name,
159160 scope: row.scope,
160 run: { repoId: row.r_repo, forkId: row.r_fork!, ...(row.a_app !== null ? { app: true as const } : {}) },
161 run: {
162 repoId: row.r_repo,
163 forkId: row.r_fork!,
164 ...(row.r_read !== null ? { readId: row.r_read } : {}),
165 ...(row.a_app !== null ? { app: true as const } : {}),
166 },
161167 });
162168 // An outside agent's token is bound to one repository (`http/contribute_gate.ts`).
163169 if (row.c_repo !== null)
src/crew/brief.ts +11
@@ -321,6 +321,11 @@ export interface TaskInput extends BriefInput {
321321 * diff (#9034).
322322 */
323323 readonly sourceNote?: readonly string[] | undefined;
324 /**
325 * For `review` of a sealed pull: the ref its head was fetched to in the clone, from its sealed
326 * fork (#14917).
327 */
328 readonly sealedRef?: string | undefined;
324329 /** For `review`: the change touches `src/web/`, so the review is held to docs/DESIGN.md. */
325330 readonly design?: boolean | undefined;
326331 /** For `review`: an earlier review run on this pull ended with no verdict. */
@@ -625,6 +630,12 @@ function taskBody(input: TaskInput): string {
625630 ...input.sourceNote.map((n) => `- ${n}`),
626631 "",
627632 ]),
633 ...(input.sealedRef === undefined
634 ? []
635 : [
636 `This pull is sealed and its head lives in its builder's sealed fork: your clone holds it at \`${input.sealedRef}\` (\`git fetch ${input.sealedRef.split("/")[0]}\` brings it again), so read the code there, not only the diff below.`,
637 "",
638 ]),
628639 ...fenced(input.reviewDiff ?? "(no diff available)", "diff"),
629640 );
630641 // The conversation is context for the verdict, never the task.
src/crew/runner.ts +30 −5
@@ -71,7 +71,7 @@ import {
7171 sourceFollowing,
7272 upstreamFollowing,
7373 } from "../actions/upstream_follow.ts";
74 import { getPull, listPulls, type Pull } from "../core/pulls.ts";
74 import { getPull, listPulls, type Pull, pullDir } from "../core/pulls.ts";
7575 import {
7676 canWrite,
7777 forkParentFor,
@@ -120,6 +120,7 @@ import {
120120 onBase,
121121 } from "./already.ts";
122122 import { touchesDesign } from "./review.ts";
123 import { fetchSealedHead, SEALED_REMOTE, sealedHeadRepo } from "./sealed_head.ts";
123124 import { BUDGETS, budgetSummary } from "../kernel/budgets.ts";
124125 import { createLog, holdValues, redactValues, releaseValues } from "../kernel/log.ts";
125126 import { err, ok, type Result } from "../kernel/result.ts";
@@ -1055,6 +1056,9 @@ async function work(
10551056 // brief, its clone is its own fork at a scratch branch, and it pushes
10561057 // nothing. The pull's branch is not its to move, and it never tries.
10571058 const reviewing = reason === "review";
1059 // The builder's sealed fork a reviewed sealed pull's head lives in (#14917).
1060 const sealedHead =
1061 reviewing && pull !== undefined ? sealedHeadRepo(app.db, repo, pull) : undefined;
10581062 // A person's such pull is theirs to push, and stays open (#7572).
10591063 if (pull !== undefined && !reviewing && pull.headRepoId === repo.id)
10601064 return {
@@ -1130,7 +1134,12 @@ async function work(
11301134 app.db,
11311135 agent.user.id,
11321136 run.id,
1133 { repoId: repo.id, forkId: fork.value.id },
1137 {
1138 repoId: repo.id,
1139 forkId: fork.value.id,
1140 // A reviewer of a sealed pull reads the fork its head lives in, for this run (#14917).
1141 ...(sealedHead === undefined ? {} : { readId: sealedHead.id }),
1142 },
11341143 expires,
11351144 );
11361145 // Offered to this forge's two addresses and to no other host the run's git reaches.
@@ -1231,6 +1240,19 @@ async function work(
12311240 );
12321241 watch.lap("clone");
12331242 if (!prepared.ok) return { state: "failed", summary: prepared.error };
1243 if (sealedHead !== undefined && pull !== undefined) {
1244 const fetched = await fetchSealedHead(
1245 dir,
1246 sealedHead,
1247 pull.headBranch,
1248 { local: options.localUrl, agent: options.forgeUrl },
1249 { ...gitAuth, ...identity },
1250 GIT_TRANSFER_MS,
1251 );
1252 if (!fetched.ok)
1253 return { state: "failed", summary: `fetch of the sealed head failed: ${fetched.error}` };
1254 write(`sealed head: ${sealedHead.ownerName}/${sealedHead.name} ${pull.headBranch} fetched as ${fetched.ref}`);
1255 }
12341256 // Counted once made: a clone that failed is no hop on the line.
12351257 ran.hops.record(refresh, options.localUrl);
12361258 // Dependencies before the agent starts, so the type check has its types;
@@ -1340,8 +1362,9 @@ async function work(
13401362 foreignPull: foreign
13411363 ? {
13421364 branch: foreign.headBranch,
1343 diff: (await reviewInputs(app, repo, foreign, undefined, bare))
1344 .reviewDiff,
1365 diff: (
1366 await reviewInputs(app, repo, foreign, undefined, await pullDir(app.db, app.reposRoot, repo, foreign))
1367 ).reviewDiff,
13451368 }
13461369 : undefined,
13471370 resolution:
@@ -1358,8 +1381,10 @@ async function work(
13581381 : undefined,
13591382 ...(reason === "review" && pull
13601383 ? {
1361 ...(await reviewInputs(app, repo, pull, record, bare)),
1384 // A sealed pull's head is in its builder's sealed fork, not the base (#14917).
1385 ...(await reviewInputs(app, repo, pull, record, await pullDir(app.db, app.reposRoot, repo, pull))),
13621386 sourceNote: await reviewSourceNote(app, repo, pull, bare),
1387 ...(sealedHead === undefined ? {} : { sealedRef: `${SEALED_REMOTE}/${pull.headBranch}` }),
13631388 boxes: boxesOf(app, repo, pull),
13641389 retry:
13651390 app.db
src/crew/sealed_head.ts added +62
@@ -0,0 +1,62 @@
1 /**
2 * Where a reviewer reads a sealed pull's code (#14917). The fix for a sealed
3 * report is built in its builder's sealed fork (`core/sealed.ts`), private,
4 * read by the base's writers and crew. A reviewer's clone is of the base, so
5 * on its own it never holds the head it is asked to judge, and the review
6 * rested on the pull's description. Here: which fork holds the head, and the
7 * fetch that brings it into the review run's clone, over the run's own token,
8 * which the run gate lets read that fork for the run (`RunScope.readId`).
9 */
10
11 import type { Pull } from "../core/pulls.ts";
12 import { getRepoById, type Repo } from "../core/repos.ts";
13 import type { Database } from "bun:sqlite";
14 import { runGit } from "../git/env.ts";
15 import { isSafeRef } from "../git/guard.ts";
16
17 /** The remote a review clone names the sealed fork by, and the ref the head lands at. */
18 export const SEALED_REMOTE = "sealed";
19
20 /**
21 * The builder's sealed fork that `pull`'s head lives in, when `pull` is a
22 * sealed pull on `repo` proposed from one; undefined for every other pull.
23 */
24 export function sealedHeadRepo(
25 db: Database,
26 repo: Repo,
27 pull: Pick<Pull, "sealed" | "crossRepo" | "headRepoId">,
28 ): Repo | undefined {
29 if (!pull.sealed || !pull.crossRepo) return undefined;
30 const head = getRepoById(db, pull.headRepoId);
31 return head !== undefined && head.sealedFor === repo.id ? head : undefined;
32 }
33
34 /**
35 * Fetch the pull's head branch from its sealed fork into the clone at `dir`,
36 * as `refs/remotes/sealed/<branch>`, and name the fork as the `sealed` remote
37 * at `agentUrl` so the reviewer can fetch it again itself. `url` is where the
38 * forge answers on this machine; `env` signs git in with the run's token.
39 */
40 export async function fetchSealedHead(
41 dir: string,
42 head: Repo,
43 branch: string,
44 urls: { readonly local: string; readonly agent: string },
45 env: Record<string, string>,
46 timeoutMs: number,
47 ): Promise<{ ok: true; ref: string } | { ok: false; error: string }> {
48 if (!isSafeRef(branch)) return { ok: false, error: `unsafe branch name ${branch}` };
49 const path = `${head.ownerName}/${head.name}.git`;
50 const ref = `refs/remotes/${SEALED_REMOTE}/${branch}`;
51 const fetched = await runGit(
52 ["fetch", "--quiet", `${urls.local}/${path}`, `+refs/heads/${branch}:${ref}`],
53 { cwd: dir, env, timeoutMs },
54 );
55 if (!fetched.ok)
56 return { ok: false, error: fetched.error.stderr.slice(0, 200) };
57 // The remote is the agent's to fetch from again; a kept clone has it already.
58 const set = await runGit(["remote", "set-url", SEALED_REMOTE, `${urls.agent}/${path}`], { cwd: dir, env });
59 if (!set.ok)
60 await runGit(["remote", "add", SEALED_REMOTE, `${urls.agent}/${path}`], { cwd: dir, env });
61 return { ok: true, ref };
62 }
src/http/run_gate.ts +10 −1
@@ -15,7 +15,9 @@
1515 * Inside its own repositories the routes that reach out of it are refused too:
1616 * forking or copying it elsewhere, proposing to the parent, syncing from an
1717 * origin, moving a sealed report into a new private repository, and minting a
18 * proposal token (a credential that would outlive the run). A pull's
18 * proposal token (a credential that would outlive the run). A review of a
19 * sealed pull also reads, and never writes, the sealed fork its head lives in
20 * (`RunScope.readId`, #14917): the run reads the code it is to judge. A pull's
1921 * `headRepo` is pinned to the run's two repositories in the pulls handler.
2022 * The handlers still apply the crew member's own access on top.
2123 */
@@ -116,6 +118,13 @@ export function runGate(
116118 return undefined;
117119 if (gitRead(method, url, parent)) return undefined;
118120 }
121 // The one repository the run reads beside its own: a sealed pull's head, for its reviewer.
122 const read = scope.readId === undefined ? undefined : getRepoById(app.db, scope.readId);
123 if (read !== undefined) {
124 if ((method === "GET" || method === "HEAD") && under(path, "/api/v1/repos", read) !== undefined)
125 return undefined;
126 if (gitRead(method, url, read)) return undefined;
127 }
119128 // A path one of these repositories moved from (#570): the run was briefed
120129 // before the move, so it passes on to the 308 that names the new path,
121130 // which this gate then judges like any other. A push is refused there.
test/helpers/crew-standin.ts +1
@@ -301,6 +301,7 @@ exit 0
301301 fi
302302 if grep -q "You are the reviewer on this crew" "${promptFile}"; then
303303 if grep -q SLOW-REVIEW "${promptFile}"; then exec sleep 60; fi
304 git for-each-ref --format='%(objectname) %(refname)' > "${dataDir}/review.refs"
304305 path=$(grep -o 'api/v1/repos/[^"\\]*/review-states' "${promptFile}" | head -1)
305306 curl -sS -X POST -H "authorization: Bearer $FORGE_TOKEN" -H "content-type: application/json" "\${FORGE_URL%/}/$path" -d '{"state":"changes_requested","body":"Name the fence."}' > "${dataDir}/review.out"
306307 echo '{"type":"result","subtype":"success","is_error":false,"result":"Reviewed.","total_cost_usd":0.004,"num_turns":1,"session_id":"fake-3","terminal_reason":"completed"}'
test/sealed-review-reads.test.ts added +114
@@ -0,0 +1,114 @@
1 /**
2 * The reviewer reads a sealed pull's code (#14917). A sealed pull's head lives
3 * in its builder's sealed fork; its reviewer was told "(no diff available)",
4 * the diff routes did not exist, and the review rested on the description.
5 * Here: a builder seals a fix from its sealed fork and it goes to review. The
6 * review run's token reads that fork, its clone holds the head commit, and
7 * its brief carries the diff.
8 */
9
10 import { seatAs } from "./helpers/seat.ts";
11 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
12 import { readFileSync } from "node:fs";
13 import { join } from "node:path";
14 import type { App } from "../src/app.ts";
15 import { addToCrew, createAgent } from "../src/actions/crew.ts";
16 import { openIssue } from "../src/actions/issues.ts";
17 import { openPull } from "../src/actions/pulls.ts";
18 import { commitToBranch, sealedForkFor } from "../src/actions/repos.ts";
19 import { queueAgentRun } from "../src/crew/dispatch.ts";
20 import { getIssue } from "../src/core/issues.ts";
21 import type { Agent } from "../src/core/agents.ts";
22 import type { Pull } from "../src/core/pulls.ts";
23 import type { Repo } from "../src/core/repos.ts";
24 import { mintRunToken } from "../src/core/run_token.ts";
25 import type { User } from "../src/core/users.ts";
26 import {
27 agentRunSettled,
28 startStandInCrew,
29 stopStandInCrew,
30 type StandInCrew,
31 } from "./helpers/crew-standin.ts";
32
33 describe("a sealed pull's reviewer reads its code", () => {
34 let standIn: StandInCrew;
35 let app: App;
36 let alice: User;
37 let bot: User;
38 let reviewer: User;
39 let reviewerAgent: Agent;
40 let repo: Repo;
41 let sealedFork: Repo;
42 let headOid = "";
43
44 /** A sealed fix from the builder's sealed fork, opened by the builder. */
45 async function sealedPull(title: string, branch: string): Promise<Pull> {
46 const report = openIssue(app, alice, repo, { title: `${title} report`, sealed: true });
47 if (!report.ok) throw new Error(String(report.error));
48 const pushed = await commitToBranch(app, bot, sealedFork, {
49 branch,
50 base: "main",
51 message: "Close the hole",
52 files: [{ path: `${branch}.ts`, content: "export const closed = true;\n" }],
53 });
54 if (!pushed.ok) throw new Error(JSON.stringify(pushed.error));
55 const opened = await openPull(app, bot, repo, {
56 title,
57 body: `Closes #${report.value.number}`,
58 baseBranch: "main",
59 headBranch: branch,
60 headRepo: sealedFork,
61 });
62 if (!opened.ok) throw new Error(String(opened.error));
63 expect(opened.value.sealed).toBe(true);
64 return opened.value;
65 }
66
67 let pull: Pull;
68
69 beforeAll(async () => {
70 standIn = await startStandInCrew();
71 ({ app, alice, bot, repo } = standIn);
72 const made = await createAgent(app, alice, { username: "juniper", brief: "Reads for security." });
73 if (!made.ok) throw new Error(String(made.error));
74 reviewerAgent = made.value;
75 reviewer = made.value.user;
76 expect(addToCrew(app, alice, repo, "juniper").ok).toBe(true);
77 seatAs(app.db, repo.id, reviewer.id, "reviewer");
78 const fork = await sealedForkFor(app, bot, repo);
79 if (!fork.ok) throw new Error(String(fork.error));
80 sealedFork = fork.value;
81 pull = await sealedPull("SEALED-REVIEW close the hole", "fix-hole");
82 headOid = pull.headOid;
83 }, 30_000);
84
85 afterAll(() => stopStandInCrew(standIn));
86
87 test("a review run's token reads the sealed fork its pull's head lives in, and no run token else", async () => {
88 const own = await sealedForkFor(app, reviewer, repo);
89 if (!own.ok) throw new Error(String(own.error));
90 const refs = `/${sealedFork.ownerName}/${sealedFork.name}.git/info/refs?service=git-upload-pack`;
91 const expires = new Date(Date.now() + 60_000).toISOString();
92 const granted = mintRunToken(app.db, reviewer.id, "r-granted", { repoId: repo.id, forkId: own.value.id, readId: sealedFork.id }, expires);
93 const plain = mintRunToken(app.db, reviewer.id, "r-plain", { repoId: repo.id, forkId: own.value.id }, expires);
94 const basic = (t: string) => ({ authorization: `Basic ${btoa(`juniper:${t}`)}` });
95 expect((await fetch(`${standIn.servers.url}${refs}`, { headers: basic(granted.token) })).status).toBe(200);
96 expect((await fetch(`${standIn.servers.url}${refs}`, { headers: basic(plain.token) })).status).toBe(404);
97 // Read, never write.
98 const push = `/${sealedFork.ownerName}/${sealedFork.name}.git/info/refs?service=git-receive-pack`;
99 expect((await fetch(`${standIn.servers.url}${push}`, { headers: basic(granted.token) })).status).toBe(404);
100 });
101
102 test("the review run's clone holds the head commit, and its brief the diff", async () => {
103 const issue = getIssue(app.db, repo.id, pull.number)!;
104 expect(queueAgentRun(app, repo, issue, reviewerAgent, "review") ?? null).not.toBeNull();
105 const run = await agentRunSettled(app, repo, (r) => r.reason === "review" && r.issueId === issue.id);
106 expect(run.state).toBe("ok");
107 const refs = readFileSync(join(standIn.dataDir, "review.refs"), "utf8");
108 expect(refs).toContain(`${headOid} refs/remotes/sealed/fix-hole`);
109 const prompt = readFileSync(standIn.promptFile, "utf8");
110 expect(prompt).not.toContain("(no diff available)");
111 expect(prompt).toContain("export const closed = true;");
112 expect(prompt).toContain("sealed/fix-hole");
113 }, 30_000);
114 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs