diff --git a/server/src/__tests__/chat-channels/github-review-publications.integration.test.ts b/server/src/__tests__/chat-channels/github-review-publications.integration.test.ts index e32fab5537e..76a9569b66b 100644 --- a/server/src/__tests__/chat-channels/github-review-publications.integration.test.ts +++ b/server/src/__tests__/chat-channels/github-review-publications.integration.test.ts @@ -18,6 +18,7 @@ vi.mock("@chat-adapter/telegram", async (importOriginal) => { }); import { chatHistoryActionBlocks } from "../helpers/chat-history-proof.js"; import { githubChatReviewService } from "../../services/chat-github-reviews.js"; +import { githubAutomaticReviewEvent } from "../../services/chat-github-events.js"; import { githubResponseCommentService } from "../../services/chat-github-response-comments.js"; import { githubBotToolsForSession } from "../../services/chat-github-tools.js"; import { resolveRunScopedMentionedSkillKeys } from "../../services/heartbeat/run-preparation.js"; @@ -50,6 +51,40 @@ describeEmbeddedPostgres("chat channels: github review publications", () => { } = rev; describe("GitHub agent review publications", () => { + it.each([ + ["closed", "e", "already merged or closed, so no review is needed", "finish the task as done"], + ["closed", "f", "already merged or closed, so no review is needed", "finish the task as done"], + ["open", "e", "this review is no longer needed", "finish the task as done"], + ["queued", "e", "a newer automatic review for that commit is already queued", "leave the task open for the queued review"], + ] as const)("tells a superseded automatic review to stop without waiting on a person: %s, requested head %s", + async (state, requestedHead, reason, ending) => { + const f = await toolReplyFixture("github:paperclipai/paperclip:418"); + const [delivery] = await db.select().from(chatDeliveries).where(eq(chatDeliveries.endpointId, f.endpoint.id)); + const context = githubAutomaticReviewEvent({ action: "opened", repository: { id: 97531, full_name: "paperclipai/paperclip" }, + sender: { id: 42, login: "octocat" }, pull_request: { number: 418, title: "Review", body: "", draft: false, + base: { sha: "a".repeat(40), ref: "master" }, head: { sha: requestedHead.repeat(40) }, user: { id: 42, login: "octocat" }, labels: [] } }, + randomUUID())!; + await db.update(chatDeliveries).set({ normalizedEvent: { ...delivery.normalizedEvent, githubAutomatic: { context } } }) + .where(eq(chatDeliveries.id, delivery.id)); + if (state === "queued") await db.insert(chatDeliveries).values({ companyId: delivery.companyId, endpointId: delivery.endpointId, + conversationId: delivery.conversationId, providerEventId: `newer-${randomUUID()}`, deduplicationKey: `newer-${randomUUID()}`, + eventKind: delivery.eventKind, state: "received", createdAt: new Date(delivery.createdAt.getTime() + 60_000), + normalizedEvent: { ...delivery.normalizedEvent, githubAutomatic: { context: { ...context, headSha: "f".repeat(40) } } } }); + f.setSupplementalProviderFetch(async input => String(input).endsWith("/pulls/418") + ? Response.json({ number: 418, title: "Fix", body: null, state: state === "queued" ? "open" : state, draft: false, head: { sha: "f".repeat(40) }, + base: { sha: "a".repeat(40), ref: "master" }, user: { id: 42, login: "octocat", type: "User" }, labels: [] }) + : undefined); + const error = await githubChatReviewService(db, f.providerFetch).execute(f.session, "read_pull_request", { section: "metadata" }) + .then(() => null, (caught: unknown) => caught as { message: string; details?: Record }); + expect(error?.message).toContain(reason); + expect(error?.message).toContain(ending); + if (state === "queued") expect(error?.message).not.toContain("finish the task as done"); + if (requestedHead === "f") expect(error?.message).not.toContain("older pull request head"); + expect(error?.message).not.toContain("An authorized person can request"); + expect(error?.details).toMatchObject({ reasonCode: "github_review_superseded", pullRequestClosed: state === "closed", + newerReviewQueued: state === "queued" }); + }); + it("loads only the current configured instruction skills, never provider links or another company", async () => { const f = await reviewBotFixture(); const other = await seedCompany(); diff --git a/server/src/services/chat-github-reviews.ts b/server/src/services/chat-github-reviews.ts index 4d35860c46d..791f1075d38 100644 --- a/server/src/services/chat-github-reviews.ts +++ b/server/src/services/chat-github-reviews.ts @@ -337,9 +337,11 @@ export function githubChatReviewService(db: Db, fetchImpl = fetch) { async function supersededAutomaticReview( source: Awaited>, requestedCommit: string, - currentCommit: string, + pull: Pull, ) { - const [newer] = await db + const currentCommit = pull.head.sha; + const closed = pull.state === "closed"; + const [newer] = closed || requestedCommit === currentCommit ? [] : await db .select({ id: chatDeliveries.id }) .from(chatDeliveries) .where( @@ -358,19 +360,27 @@ export function githubChatReviewService(db: Db, fetchImpl = fetch) { ), ) .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"; + const target = requestedCommit === currentCommit + ? `This automatic review was requested for ${requestedCommit.slice(0, 7)}, and ` + : `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 `; + const next = closed + ? "the pull request is already merged or closed, so no review is needed. Stop this review and finish the task as done" + : newer + ? "a newer automatic review for that commit is already queued on this task. Stop this run and leave the task " + + "open for the queued review" + : "no newer automatic review is queued for it, so this review is no longer needed. Stop this review and finish " + + "the task as done"; 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.", + `${target}${next}: do not call review tools again, do not post the review as an ordinary comment, ` + + "and do not block the task or ask anyone to request a review.", { reasonCode: "github_review_superseded", retryable: false, requestedCommit, currentCommit, newerReviewQueued: !!newer, + pullRequestClosed: closed, }, ); } @@ -672,8 +682,8 @@ export function githubChatReviewService(db: Db, fetchImpl = fetch) { const pull = await api.request(`/pulls/${source.number}`); const automatic = source.delivery.normalizedEvent.githubAutomatic as { context: GitHubReviewEventContext } | undefined; - if (automatic && automatic.context.headSha !== pull.head.sha) - throw await supersededAutomaticReview(source, automatic.context.headSha, pull.head.sha); + if (automatic && (automatic.context.headSha !== pull.head.sha || pull.state === "closed")) + throw await supersededAutomaticReview(source, automatic.context.headSha, pull); if (name === "read_pull_request") { const parsed = readSchema.parse(input); if (parsed.section === "metadata") {