feat(mcp): agents can write and read pull request review guides #18143
Mergedjuliusmarmingemerged 6 commits into
main fromrefs/pull/18143/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
juliusmarminge added labelsmacroscope-review
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/mcp/toolkits/pullRequests/handlers.ts line 442Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -342,3 +418,6 @@342418 ),343419 watch_pull_request: writesThread((input) => setWatching(input, true)),344420 unwatch_pull_request: writesThread((input) => setWatching(input, false)),421+ write_pull_request_guides: McpToolAccess.writesWithCapability((input) =>422+ Effect.gen(function* () {423+ const located = yield* locateAll(input.pullRequests);packages/client-runtime/src/t3ToolSummary.ts line 403OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -399,3 +399,5 @@399399 `linked pull requests${selected.length === 1 ? "" : ` ${times}`}`,400400 );401401 break;402+ case "write-pr-guides":403+ label = phrase("Started", "start", `review guides ${times}`);
- apps/server/src/mcp/toolkits/pullRequests/handlers.ts line 463OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -344,0 +443,21 @@443+ };444+ }445+ const identity = {446+ host: entry.target.host,447+ repository: entry.target.repository,448+ number: entry.target.number,449+ url: entry.target.url,450+ };451+ if (entry.project === undefined) {452+ return {453+ ...identity,454+ outcome: "failed" as const,455+ detail: new PullRequestProjectNotFoundError({456+ repository: entry.target.repository,457+ }).message,458+ };459+ }460+ const reference = guideReference(entry.project, entry.target);461+ // A run already going for this head is joined; asking first tells the two apart.462+ const before = yield* reviewGuides.get(reference).pipe(Effect.option);463+ const wasRunning = Option.isSome(before) && before.value.run?._tag === "Running";
- apps/server/src/mcp/toolkits/pullRequests/handlers.ts line 180OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -155,0 +160,21 @@160+/**161+ * The project a pull request is read through: the caller's own when its checkout is that162+ * repository, else the first project here whose checkout is. Host reads are scoped to a project,163+ * so a repository with no checkout in this environment cannot be guided from it.164+ */165+function projectForPullRequest(166+ projects: ReadonlyArray<OrchestrationProjectShell>,167+ target: { readonly host: string; readonly repository: string },168+ preferred: OrchestrationProjectShell | undefined,169+): OrchestrationProjectShell | undefined {170+ const matches = (project: OrchestrationProjectShell) => {171+ const { host } = projectHostAndProvider(project);172+ const repository =173+ project.repositoryIdentity == null174+ ? null175+ : sourceControlRepositorySelector(project.repositoryIdentity);176+ return (177+ host !== null &&178+ repository !== null &&179+ host.toLowerCase() === target.host.toLowerCase() &&180+ repository.toLowerCase() === target.repository.toLowerCase()
- apps/server/src/mcp/toolkits/pullRequests/handlers.ts lines 440 to 440Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -342,3 +437,4 @@342437 ),343438 watch_pull_request: writesThread((input) => setWatching(input, true)),344439 unwatch_pull_request: writesThread((input) => setWatching(input, false)),440+ write_pull_request_guides: McpToolAccess.writesWithCapability((input) =>
- @@ -162,3 +170,21 @@162170 }163171 }164172173+export class PullRequestProjectNotFoundError extends Schema.TaggedError<PullRequestProjectNotFoundError>()(174+ "PullRequestProjectNotFoundError",175+ { repository: Schema.String },176+) {177+ override get message(): string {178+ return `No project in this environment is a checkout of ${this.repository}, so its pull requests cannot be read here. Add one with t3_project_create.`;179+ }180+}181+182+export class PullRequestGuideFailedError extends Schema.TaggedError<PullRequestGuideFailedError>()(183+ "PullRequestGuideFailedError",184+ { detail: Schema.String },185+) {186+ override get message(): string {187+ return this.detail;188+ }189+}190+
- apps/server/src/mcp/toolkits/pullRequests/tools.ts line 397OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -321,0 +377,21 @@377+ stale: Schema.Boolean.annotate({378+ description:379+ "True when the guide was written for an earlier head than the pull request has now.",380+ }),381+});382+export type ReadPullRequestGuideResult = typeof ReadPullRequestGuideResult.Type;383+384+const WritePullRequestGuidesTool = Tool.make("write_pull_request_guides", {385+ description:386+ "Write review guides for up to 20 pull requests, the tour a maintainer reads in a pull request's Guide tab. Each run reads its change in a temporary checkout and takes minutes, so this returns once the runs are under way; read a guide with read_pull_request_guide to see when it is written. A guide for the same head that is already running is joined, not restarted; a pull request whose guide is current is written again. Each pull request is read through the project in this environment whose checkout has that repository. Fails while review guides are off on this server.",387+ parameters: Schema.Struct({388+ pullRequests: Schema.Array(GuideTarget).check(Schema.isMinLength(1), Schema.isMaxLength(20)),389+ }),390+ success: WritePullRequestGuidesResult,391+ failure: PullRequestToolError,392+ dependencies: guideDependencies,393+})394+ .annotate(Tool.Title, "Write pull request guides")395+ .annotate(Tool.Readonly, false)396+ .annotate(Tool.Destructive, false)397+ .annotate(Tool.Idempotent, true)packages/client-runtime/src/t3ToolSummary.ts line 413OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -399,3 +399,15 @@399399 `linked pull requests${selected.length === 1 ? "" : ` ${times}`}`,400400 );401401 break;402+ case "write-pr-guides": {403+ // One call writes many guides, and reports a refusal per guide rather than failing.404+ const outcomes = selected.flatMap((call) =>405+ Array.isArray(call.output?.guides)406+ ? call.output.guides.map((guide) => asRecord(guide)?.outcome)407+ : [],408+ );409+ const started = outcomes.filter((outcome) => outcome !== "failed").length;410+ const failed = outcomes.length - started;411+ if (outcomes.length > 0 && started === 0) {412+ label = `Could not start ${quantity(failed, "review guide")}`;413+ failedCount += failed;
- apps/server/src/mcp/toolkits/pullRequests/handlers.ts line 473Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -344,0 +453,21 @@453+ url: entry.input.url ?? "",454+ outcome: "failed" as const,455+ detail: entry.failure,456+ };457+ }458+ const identity = {459+ host: entry.target.host,460+ repository: entry.target.repository,461+ number: entry.target.number,462+ url: entry.target.url,463+ };464+ if (entry.project === undefined) {465+ return {466+ ...identity,467+ outcome: "failed" as const,468+ detail: new PullRequestProjectNotFoundError({469+ repository: entry.target.repository,470+ }).message,471+ };472+ }473+ return yield* reviewGuides.generate(guideReference(entry.project, entry.target)).pipe(
juliusmarminge merged this18656a3
juliusmarminge closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed