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

The MCP `pull` tool omits review verdict bodies: on #16496 juniper asked for… #16527

Merged opened this in answer to

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

Merged

scout/forge:scout/16505-the-mcp-pull-tool-omits-review-verdict-b → main 0 commits fork 3 threads · 3 unresolved

  • Merged. scout/16505-the-mcp-pull-tool-omits-review-verdict-b was merged into main as 050fab7.

Review

Sign in to review this change.
6 files changed +141 −5
src/core/reviews.ts +11
@@ -353,6 +353,17 @@ function latestByAuthor(reviews: readonly Review[]): Map<string, Review> {
353353 return latest;
354354 }
355355
356 /** Each reviewer's current verdict, when it was passed on `headOid`: what the head stands on. */
357 export function reviewsOnHead(db: Database, issueId: string, headOid: string): Review[] {
358 return [...latestReviewByAuthor(db, issueId).values()].filter((r) => r.commitOid === headOid);
359 }
360
361 /** The words naming the review that asks for changes on `headOid`, or null when none does. */
362 export function blockingReviewWords(db: Database, issueId: string, headOid: string): string | null {
363 const r = reviewsOnHead(db, issueId, headOid).find((x) => x.state === "changes_requested");
364 return r === undefined ? null : `${r.originAuthor ?? r.authorName} asked for changes (review ${r.id})`;
365 }
366
356367 export interface ReviewSummary {
357368 /** Distinct reviewers whose current verdict approves *this* head. */
358369 readonly approvals: number;
src/flow/next_actor.ts +5 −2
@@ -8,7 +8,7 @@ import type { Database } from "bun:sqlite";
88 import { getHold } from "../core/holds.ts";
99 import type { Pull } from "../core/pulls.ts";
1010 import { checkSummary, type CheckSummary, type RunState } from "../core/runs.ts";
11 import { reviewSummary } from "../core/reviews.ts";
11 import { blockingReviewWords, reviewSummary } from "../core/reviews.ts";
1212 import type { Repo } from "../core/repos.ts";
1313 import { reviewsOff } from "../crew/review.ts";
1414 import { isFirstChange } from "../crew/first_change.ts";
@@ -37,6 +37,8 @@ export interface NextInput {
3737 readonly reviewOff?: boolean;
3838 /** The app's first change (`crew/first_change.ts`): it lands on green whatever the reviewer asked. */
3939 readonly firstChange?: boolean;
40 /** Who asked for changes on this head, and which review (`blockingReviewWords`). */
41 readonly askedBy?: string | null;
4042 }
4143
4244 const WAITING = {
@@ -56,7 +58,7 @@ export function nextActor(i: NextInput): Next {
5658 return { who: "queue", why: WAITING[i.check] };
5759 }
5860 if (i.changesRequested > 0)
59 return { who: "builder", why: "changes were asked on this head" };
61 return { who: "builder", why: i.askedBy == null ? "changes were asked on this head" : `changes were asked on this head: ${i.askedBy}` };
6062 if (i.check === "failed") return { who: "builder", why: "its check is red" };
6163 if (i.wall != null && i.wall.length > 0) return { who: "person", why: wallAsk(i.wall) };
6264 if (i.approvals > 0) {
@@ -105,6 +107,7 @@ export function nextOfPull(
105107 approvals: reviews.approvals,
106108 pushedSince: reviews.stale,
107109 wall: wallWords(db, pull.id, live.headOid),
110 askedBy: reviews.changesRequested > 0 ? blockingReviewWords(db, pull.id, live.headOid) : null,
108111 firstChange: reviews.changesRequested > 0 && repo !== undefined && isFirstChange(db, repo, pull),
109112 },
110113 () => repo !== undefined && reviewsOff(db, repo),
src/flow/path.ts +2 −1
@@ -17,7 +17,7 @@ import { latestDecisionsFor } from "../core/decisions.ts";
1717 import { holdsFor } from "../core/holds.ts";
1818 import { getPull, type Pull } from "../core/pulls.ts";
1919 import type { Repo } from "../core/repos.ts";
20 import { reviewSummariesFor, reviewSummary } from "../core/reviews.ts";
20 import { blockingReviewWords, reviewSummariesFor, reviewSummary } from "../core/reviews.ts";
2121 import { checksForHeads, checkSummary, type CheckSummary, type RunState } from "../core/runs.ts";
2222 import { reviewsOff } from "../crew/review.ts";
2323 import { isFirstChange } from "../crew/first_change.ts";
@@ -191,6 +191,7 @@ export function stepsOfStored(db: Database, repo: Repo, pulls: readonly Pull[]):
191191 approvals: r.approvals,
192192 pushedSince: r.stale,
193193 wall: wallWordsOf(walls.get(pull.id), pull.headOid),
194 askedBy: r.changesRequested > 0 ? blockingReviewWords(db, pull.id, pull.headOid) : null,

The batched path has no test; add one that fails without askedBy. Also this runs one query per pull with changes requested: check it is not a problem on a large list.

194195 firstChange: r.changesRequested > 0 && isFirstChange(db, repo, pull),
195196 },
196197 reviewOff,
src/http/mcp/tools/merge.ts +3 −1
@@ -4,6 +4,7 @@ import { mergePull } from "../../../actions/pulls.ts";
44 import { getPull, pullDir, pullMergeState } from "../../../core/pulls.ts";
55 import { checkSummary } from "../../../core/runs.ts";
66 import { canWrite } from "../../../core/repos.ts";
7 import { blockingReviewWords } from "../../../core/reviews.ts";
78 import { canVerdict } from "../../../core/verdict.ts";
89 import { reviewWords } from "../../../actions/review_words.ts";
910
@@ -115,7 +116,8 @@ export const tool: Tool = {
115116 // A team reviewer judges and does not land: the page's words (#12631).
116117 const words =
117118 code === "forbidden" && canVerdict(ctx.app.db, repo, user) ? reviewWords.reviewerLands : mergeFailure(error);
118 return bad(code, words);
119 const blocker = code === "review_required" ? blockingReviewWords(ctx.app.db, pull.id, pull.headOid) : null;
120 return bad(code, blocker === null ? words : `${words} Blocking: ${blocker}.`);
119121 }
120122 return good({
121123 dryRun: false,
src/http/mcp/tools/pull.ts +12 −1
@@ -14,6 +14,7 @@ import { pullWaitsFor } from "../../../flow/waits_for.ts";
1414 import { landingLine } from "../../../flow/landing_line.ts";
1515 import { pullLooks } from "../../../looks/readable.ts";
1616 import { reviewSteps } from "../../../actions/review_steps.ts";
17 import { reviewsOnHead } from "../../../core/reviews.ts";
1718 import { isCrew } from "../../../core/agents.ts";
1819 import { markReaders } from "../../../crew/direct.ts";
1920 import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts";
@@ -22,10 +23,12 @@ import { good, bad, type Tool, REPO_ARG, repoArg, isAnswer } from "../shared.ts"
2223 * The comments `pull` carries by default: the summary comes first, older ones by `commentsNext`.
2324 */
2425 const PULL_COMMENTS = 10;
26 /** A review's body is cut here in the answer; the pull page has the rest. */
27 const REVIEW_BODY = 1000;
2528
2629 export const tool: Tool = {
2730 name: "pull",
28 doc: "`{ repo, number }`: a pull's title, state, head and base, `summary` (two or three sentences: what changed, what was verified), and for an open pull whether it merges into the base's current head (`mergeable`) and the paths that stop it (`conflicts`), and `next: { who, why }` (open only; who acts next: builder, reviewer, queue or person), `step: { step, why, done }` (check, review, land or live; null once closed unlanded), and `comments` (the newest 10: author, body, at, `untrusted` on an outsider's), `commentsTotal` and `commentsNext` (older: `GET …/issues/:number/comments?cursor=`), and `looks` (newest first: `{ id, width, says, by, commit, url, at }`, the same as `GET …/pulls/:number/looks`), and `eta: { minutes, says }` (approved open pull only, else null), `waitsFor: { cause, says, action, line }` (green, unlanded 6 h+; else null), and `check: { state, baseOid }`: the latest check's state and the base tip it was merged with (empty when it merged nothing), `checkStale` (the base has moved since that check) and `checkedAgainst` (that base tip, or null; the same as `GET …/pulls/:number`); `mergeable` and `conflicts` are the same fields as `GET …/pulls/:number`, `check` is `state` and `run.baseOid` from `GET …/pulls/:number/checks`; `reviewSteps` (the review steps), draws the `ui://forge/pull` card: `#N title`, state with check and conflicts while open, `head → base` linked to the pull, and the preview link when the pull has an environment",
31 doc: "`{ repo, number }`: a pull's title, state, head and base, `summary` (two or three sentences: what changed, what was verified), and for an open pull whether it merges into the base's current head (`mergeable`) and the paths that stop it (`conflicts`), and `next: { who, why }` (open only; who acts next: builder, reviewer, queue or person), `step: { step, why, done }` (check, review, land or live; null once closed unlanded), and `reviews` (each reviewer's verdict on the current head: id, author, verdict, body, at), and `comments` (the newest 10: author, body, at, `untrusted` on an outsider's), `commentsTotal` and `commentsNext` (older: `GET …/issues/:number/comments?cursor=`), and `looks` (newest first: `{ id, width, says, by, commit, url, at }`, the same as `GET …/pulls/:number/looks`), and `eta: { minutes, says }` (approved open pull only, else null), `waitsFor: { cause, says, action, line }` (green, unlanded 6 h+; else null), and `check: { state, baseOid }`: the latest check's state and the base tip it was merged with (empty when it merged nothing), `checkStale` (the base has moved since that check) and `checkedAgainst` (that base tip, or null; the same as `GET …/pulls/:number`); `mergeable` and `conflicts` are the same fields as `GET …/pulls/:number`, `check` is `state` and `run.baseOid` from `GET …/pulls/:number/checks`; `reviewSteps` (the review steps), draws the `ui://forge/pull` card: `#N title`, state with check and conflicts while open, `head → base` linked to the pull, and the preview link when the pull has an environment",
2932 description:
3033 "A pull: title, state, head, base; open adds `mergeable`, `conflicts`, `check`.",
3134 inputSchema: {
@@ -68,6 +71,14 @@ export const tool: Tool = {
6871 at: c.createdAt,
6972 ...("untrusted" in c && c.untrusted !== undefined ? { untrusted: true } : {}),
7073 })),
74 reviews: (marking ? markReaders(db, repo, reviewsOnHead(db, pull.id, pull.headOid)) : reviewsOnHead(db, pull.id, pull.headOid)).map((r) => ({
75 id: r.id,
76 author: r.originAuthor ?? r.authorName,
outdated

body is unbounded; cap or truncate it, and mark untrusted when the author cannot write, like the comments above.

77 verdict: r.state,
78 body: r.body.length > REVIEW_BODY ? `${r.body.slice(0, REVIEW_BODY)}… (cut; the rest is on the pull page)` : r.body,
79 at: r.createdAt,
80 ...("untrusted" in r && r.untrusted !== undefined ? { untrusted: true } : {}),
81 })),
7182 commentsTotal: total,
7283 commentsNext: comments.length > 0 && comments.length < total ? comments[0]!.id : null,
7384 looks: pullLooks(ctx.app, repo, pull, user),
test/mcp-pull-verdicts.test.ts added +108
@@ -0,0 +1,108 @@
1 /**
2 * MCP `pull` lists each reviewer's verdict on the head (a verdict on an older head is left out, a
outdated

Stale header: this file tests reviews and the blocking-review wording, not reviewSteps. Fix it.

3 * long body is cut) and `next.why` names the review that blocks; `stepsOfStored` says the same.
4 */
5 import { afterAll, beforeAll, expect, test } from "bun:test";
6 import { mkdtemp, rm } from "node:fs/promises";
7 import { tmpdir } from "node:os";
8 import { join } from "node:path";
9 import { commitToBranch } from "../src/actions/repos.ts";
10 import { createApp, type App } from "../src/app.ts";
11 import { stepsOfStored } from "../src/flow/path.ts";
12 import { createPull, getPull } from "../src/core/pulls.ts";
13 import { createRepo, getRepoById } from "../src/core/repos.ts";
14 import { submitReview } from "../src/core/reviews.ts";
15 import { createToken } from "../src/core/tokens.ts";
16 import { createUser } from "../src/core/users.ts";
17 import { repoDir } from "../src/git/repos.ts";
18 import { serve, type Servers } from "../src/http/server.ts";
19 import { quietLogs } from "./helpers/globals.ts";
20 import { mcpTool } from "./helpers/mcp.ts";
21
22 quietLogs();
23
24 let dataDir: string;
25 let app: App;
26 let servers: Servers;
27 let token = "";
28 let asked = "";
29 const numbers: Record<string, number> = {};
30 let repoId = "";
31
32 const ok = <T>(r: { ok: true; value: T } | { ok: false; error: unknown }): T => {
33 if (!r.ok) throw new Error(JSON.stringify(r.error));
34 return r.value;
35 };
36
37 beforeAll(async () => {
38 dataDir = await mkdtemp(join(tmpdir(), "forge-mcp-verdict-"));
39 app = createApp({ dataDir, port: 0 });
40 servers = await serve(app);
41 const make = async (username: string, adminIfFirst = false) =>
42 ok(await createUser(app.db, { origin: "seed", trusted: true, username, email: `${username}@forge.local`, password: "a-good-passphrase", adminIfFirst }));
43 const ada = await make("ada", true);
44 const juniper = await make("juniper");
45 const repo = ok(await createRepo(app.db, app.reposRoot, { ownerId: ada.id, name: "vault", visibility: "public" }));
46 ok(await commitToBranch(app, ada, repo, { message: "seed", files: [{ path: "a.txt", content: "a\n" }] }));
47 ok(await commitToBranch(app, ada, repo, { message: "one", branch: "one", files: [{ path: "o.txt", content: "o\n" }] }));
48 const pull = ok(await createPull(app.db, repoDir(app.reposRoot, ada.username, "vault"), { repoId: repo.id, authorId: ada.id, title: "Pull one", baseBranch: "main", headBranch: "one" }));
49 asked = ok(submitReview(app.db, { issueId: pull.id, authorId: juniper.id, state: "changes_requested", body: "Document the flag in the README", commitOid: pull.headOid })).id;
50 const open = async (name: string, state: "approved" | "changes_requested", opts: { oid?: string; body?: string } = {}) => {
51 ok(await commitToBranch(app, ada, repo, { message: name, branch: name, files: [{ path: `${name}.txt`, content: `${name}\n` }] }));
52 const p = ok(await createPull(app.db, repoDir(app.reposRoot, ada.username, "vault"), { repoId: repo.id, authorId: ada.id, title: name, baseBranch: "main", headBranch: name }));
53 ok(submitReview(app.db, { issueId: p.id, authorId: juniper.id, state, body: opts.body ?? "", commitOid: opts.oid ?? p.headOid }));
54 return p.number;
55 };
56 repoId = repo.id;
57 numbers.approved = await open("two", "approved");
58 numbers.stale = await open("three", "changes_requested", { oid: "0".repeat(40) });
59 numbers.long = await open("four", "changes_requested", { body: "x".repeat(5000) });
60 token = createToken(app.db, ada.id, "rw", "rw").token;
61 });
62
63 afterAll(async () => {
64 await servers.stop();
65 await rm(dataDir, { recursive: true, force: true });
66 });
67
68 test("pull shows each review on the head and names the blocking one in next.why", async () => {
69 const r = await mcpTool(servers.url, token, "pull", { repo: "ada/vault", number: 1 });
70 expect(r.value.reviews).toEqual([
71 expect.objectContaining({ id: asked, author: "juniper", verdict: "changes_requested", body: "Document the flag in the README" }),
72 ]);
73 expect(r.value.next.who).toBe("builder");
74 expect(r.value.next.why).toContain("juniper");
75 expect(r.value.next.why).toContain(asked);
76 });
77
78 test("the merge refusal review_required names the blocking review", async () => {
79 const r = await mcpTool(servers.url, token, "merge", { repo: "ada/vault", number: 1, dryRun: false });
80 expect(r.error).toContain("juniper");
81 expect(r.error).toContain(asked);
82 });
83
84 test("an approval on the head is listed as approved and next.why stays plain", async () => {
85 const r = await mcpTool(servers.url, token, "pull", { repo: "ada/vault", number: numbers.approved });
86 expect(r.value.reviews).toEqual([expect.objectContaining({ author: "juniper", verdict: "approved" })]);
87 expect(r.value.next.why).not.toContain("asked for changes");
88 });
89
90 test("a verdict on an older head is not listed and names no blocker", async () => {
91 const r = await mcpTool(servers.url, token, "pull", { repo: "ada/vault", number: numbers.stale });
92 expect(r.value.reviews).toEqual([]);
93 expect(r.value.next.why).not.toContain("juniper");
94 });
95
96 test("a long review body is cut with a pointer to the pull page", async () => {
97 const r = await mcpTool(servers.url, token, "pull", { repo: "ada/vault", number: numbers.long });
98 const body: string = r.value.reviews[0].body;
99 expect(body.length).toBeLessThan(1200);
100 expect(body).toContain("pull page");
101 });
102
103 test("stepsOfStored names the blocking review as stepOfStored does", () => {
104 const repo = getRepoById(app.db, repoId)!;
105 const p = getPull(app.db, repoId, 1)!;
106 const step = stepsOfStored(app.db, repo, [p]).get(p.id);
107 expect(JSON.stringify(step)).toContain(asked);
108 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs