diff --git a/server/src/__tests__/chat-channels.integration.test.ts b/server/src/__tests__/chat-channels.integration.test.ts index 918e6802c09..51fab29687c 100644 --- a/server/src/__tests__/chat-channels.integration.test.ts +++ b/server/src/__tests__/chat-channels.integration.test.ts @@ -7546,12 +7546,37 @@ describeEmbeddedPostgres("chat channel control-plane integration", () => { f.setSupplementalProviderFetch(async (input) => String(input).endsWith("/pulls/81") ? Response.json({ ...payload.pull_request, head: { sha: "c".repeat(40) } }) : undefined); - await expect(githubChatReviewService(db, f.providerFetch).execute({ - companyId: f.companyId, - agentId: f.assignedAgentId, - issueId: task.id, - runId: run.id, - }, "read_pull_request", { section: "metadata" })).rejects.toThrow("older pull request head"); + const staleSession = { companyId: f.companyId, agentId: f.assignedAgentId, issueId: task.id, runId: run.id }; + const superseded = githubChatReviewService(db, f.providerFetch).execute(staleSession, "read_pull_request", { section: "metadata" }); + await expect(superseded).rejects.toMatchObject({ + status: 409, + details: { reasonCode: "github_review_superseded", retryable: false, requestedCommit: "b".repeat(40), + currentCommit: "c".repeat(40), newerReviewQueued: true }, + }); + await expect(superseded).rejects.toThrow(/older pull request head.*newer automatic review for that commit is already queued.*Stop this review/s); + await f.service.handleWebhook(f.endpoint.publicId, "github", signedGitHubWebhookRequest({ + event: "pull_request", delivery: randomUUID(), webhookSecret: f.webhookSecret, + payload: { ...payload, action: "synchronize", before: "c".repeat(40), + pull_request: { ...payload.pull_request, head: { sha: "d".repeat(40) } } }, + })); + const waiting = (await readChatDeliveriesByEndpointId(f.endpoint.id)).filter((row) => + (row.normalizedEvent.githubAutomatic as { context?: { headSha?: string } } | undefined)?.context?.headSha === "d".repeat(40)); + expect(waiting).toHaveLength(1); + expect(waiting[0]).toMatchObject({ conversationId: null }); + f.setSupplementalProviderFetch(async (input) => String(input).endsWith("/pulls/81") + ? Response.json({ ...payload.pull_request, head: { sha: "d".repeat(40) } }) + : undefined); + await expect(githubChatReviewService(db, f.providerFetch).execute(staleSession, "read_pull_request", { section: "metadata" })) + .rejects.toMatchObject({ status: 409, details: { reasonCode: "github_review_superseded", currentCommit: "d".repeat(40), newerReviewQueued: true } }); + f.setSupplementalProviderFetch(async (input) => String(input).endsWith("/pulls/81") + ? Response.json({ ...payload.pull_request, head: { sha: "e".repeat(40) } }) + : undefined); + const unqueued = githubChatReviewService(db, f.providerFetch).execute(staleSession, "read_pull_request", { section: "metadata" }); + await expect(unqueued).rejects.toMatchObject({ + status: 409, + details: { reasonCode: "github_review_superseded", currentCommit: "e".repeat(40), newerReviewQueued: false }, + }); + await expect(unqueued).rejects.toThrow(/no newer automatic review is queued.*Stop this review/s); }); async function reviewCheckWorkerFixture(status: "running" | "failed" = "failed") { const f = await toolReplyFixture("github:paperclipai/paperclip:418"); diff --git a/server/src/services/chat-github-reviews.ts b/server/src/services/chat-github-reviews.ts index de85cd9e110..4d35860c46d 100644 --- a/server/src/services/chat-github-reviews.ts +++ b/server/src/services/chat-github-reviews.ts @@ -16,7 +16,7 @@ import { githubReviewSummary } from "./chat-channels/github/review-heading.js"; import { githubPublicationRetry, GITHUB_INLINE_RETRY_WINDOW_MS, GITHUB_INLINE_MAX_ATTEMPTS } from "./chat-channels/github/retry.js"; import { createHash } from "node:crypto"; import { githubResponseCommentService, writeGitHubResponseComment } from "./chat-github-response-comments.js"; -import { and, desc, eq, gt, isNull, or, sql } from "drizzle-orm"; +import { and, desc, eq, gt, inArray, isNull, or, sql } from "drizzle-orm"; import { z } from "zod"; import { toolInvocations, @@ -334,6 +334,46 @@ export function githubChatReviewService(db: Db, fetchImpl = fetch) { github(`${prefix}${path}`, options), }; } + async function supersededAutomaticReview( + source: Awaited>, + requestedCommit: string, + currentCommit: string, + ) { + const [newer] = await db + .select({ id: chatDeliveries.id }) + .from(chatDeliveries) + .where( + and( + eq(chatDeliveries.companyId, source.endpoint.companyId), + eq(chatDeliveries.endpointId, source.endpoint.id), + or( + eq(chatDeliveries.conversationId, source.conversation.id), + isNull(chatDeliveries.conversationId), + ), + gt(chatDeliveries.createdAt, source.delivery.createdAt), + inArray(chatDeliveries.state, ["received", "processing", "processed", "retry"]), + sql`${chatDeliveries.normalizedEvent}->'githubAutomatic'->'context'->>'repositoryId' = ${source.repositoryId}`, + sql`${chatDeliveries.normalizedEvent}->'githubAutomatic'->'context'->>'pullNumber' = ${String(source.number)}`, + sql`${chatDeliveries.normalizedEvent}->'githubAutomatic'->'context'->>'headSha' = ${currentCommit}`, + ), + ) + .limit(1); + const next = newer + ? "a newer automatic review for that commit is already queued on this task" + : "no newer automatic review is queued for it. An authorized person can request a manual review"; + return conflict( + `This automatic review was requested for an older pull request head (${requestedCommit.slice(0, 7)}). ` + + `The pull request now points to ${currentCommit.slice(0, 7)}, and ${next}. ` + + "Stop this review: do not call review tools again and do not post the review as an ordinary comment.", + { + reasonCode: "github_review_superseded", + retryable: false, + requestedCommit, + currentCommit, + newerReviewQueued: !!newer, + }, + ); + } async function reviewForHead( source: Awaited>, pull: Pull, @@ -633,9 +673,7 @@ export function githubChatReviewService(db: Db, fetchImpl = fetch) { const automatic = source.delivery.normalizedEvent.githubAutomatic as { context: GitHubReviewEventContext } | undefined; if (automatic && automatic.context.headSha !== pull.head.sha) - throw conflict( - "This automatic review was requested for an older pull request head. Continue with the newest authorized PR event or an authorized manual request.", - ); + throw await supersededAutomaticReview(source, automatic.context.headSha, pull.head.sha); if (name === "read_pull_request") { const parsed = readSchema.parse(input); if (parsed.section === "metadata") {