From eef23f88e98393abad0caa10d170f49f42c83ce1 Mon Sep 17 00:00:00 2001 From: Hyo Date: Wed, 18 Nov 2026 01:25:51 +0900 Subject: [PATCH 1/4] feat: show review progress on the pull request and accept @gitdog Each review keeps one status comment on the pull request: reviewing when it starts, then ready for approval, reviewed with a link, no new changes, cancelled, or why it could not finish, with reasons safe for public pull requests. This replaces the separate limit and no-change notices. Commands also run as @gitdog and after another review bot's command on the same line. --- README.md | 9 +- convex/github/helpers.ts | 28 ++++ convex/pullRequests/commands.ts | 92 ++++++++++- convex/pullRequests/internalAction.ts | 141 ++++++++++------ convex/pullRequests/internalMutation.ts | 82 ++++------ convex/schema.ts | 2 + packages/cli/docs/REFERENCE.md | 3 +- public/llms-full.txt | 2 +- public/llms.txt | 2 +- test/convex/pullRequests/commands.test.ts | 6 + test/convex/pullRequests/lifecycle.test.ts | 179 ++++++++++++++------- 11 files changed, 380 insertions(+), 166 deletions(-) diff --git a/README.md b/README.md index e369ea9..1aa00f2 100644 --- a/README.md +++ b/README.md @@ -90,8 +90,13 @@ a pull request comment, or from the CLI: | `@gitdogbot resolve` | `gitdog reviews resolve ` | Resolves every Gitdog review thread | | `@gitdogbot help` | `gitdog reviews --help` | Lists the commands | -A command is a comment line that starts with the mention, in the pull request -conversation; replies inside a review thread are not read. Collaborators can run +A command is a comment line that starts with the mention, or follows another +review bot's command on the same line (`@coderabbitai review @gitdogbot +review`), in the pull request conversation; replies inside a review thread are +not read. `@gitdog` works as shorthand, but it also notifies the GitHub user +of that name, so `@gitdogbot` is preferred. While a review runs, Gitdog keeps +one status comment on the pull request up to date: reviewing, ready for +approval, reviewed, or why it could not finish. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again, and pull requests opened by bots such as Dependabot are not reviewed. diff --git a/convex/github/helpers.ts b/convex/github/helpers.ts index cda6fae..575c917 100644 --- a/convex/github/helpers.ts +++ b/convex/github/helpers.ts @@ -486,6 +486,34 @@ export async function addIssueComment( return data.id; } +// Replace the body of an issue or pull request comment +export async function updateIssueComment( + owner: string, + repo: string, + commentId: number, + body: string, + token: string, +): Promise { + const response = await fetchGitHubApi( + `https://api.github.com/repos/${owner}/${repo}/issues/comments/${commentId}`, + { + method: "PATCH", + headers: { + Accept: "application/vnd.github.v3+json", + Authorization: `Bearer ${token}`, + "User-Agent": "Gitdog", + "Content-Type": "application/json", + }, + body: JSON.stringify({ body }), + }, + ); + if (!response.ok) { + await response.body?.cancel().catch(() => undefined); + throw new Error(`Failed to update issue comment (HTTP ${response.status})`); + } + await response.body?.cancel().catch(() => undefined); +} + // Update issue title and/or body export async function updateIssue( owner: string, diff --git a/convex/pullRequests/commands.ts b/convex/pullRequests/commands.ts index b46731d..f13592f 100644 --- a/convex/pullRequests/commands.ts +++ b/convex/pullRequests/commands.ts @@ -43,10 +43,11 @@ export function parseReviewCommand( (line) => !line.trimStart().startsWith(">") && !/^( {4}|\t)/.test(line), ) .join("\n"); - // Only a mention that starts a line is a command, so "@gitdogbot review" - // quoted in a sentence does not run one. + // A command starts a line, or follows another review bot's command on it, + // as in "@coderabbitai review @gitdogbot review"; "@gitdogbot review" + // inside a sentence does not run one. "@gitdog" is accepted as shorthand. const match = new RegExp( - `^[ \\t]*@${slug}(?=\\s|[:,]|$)[:,]?[ \\t]*([^\\n]*)`, + `^[ \\t]*(?:@(?:[\\w-]*(?:bot|ai|copilot|assist)|[\\w-]+\\[bot\\])(?:[ \\t]+[a-z-]+){0,2}[ \\t]+){0,2}@(?:${slug}|gitdog)(?=\\s|[:,]|$)[:,]?[ \\t]*([^@\\n]*)`, "im", ).exec(text); if (!match) return null; @@ -79,6 +80,89 @@ export function helpText(slug: string): string { `- \`${mention} full review\`: review the whole pull request again`, `- \`${mention} resolve\`: resolve every Gitdog review thread`, `- \`${mention} help\`: show this list`, - "Collaborators can run every command; the pull request author can run all but a full review.", + "Collaborators can run every command; the pull request author can run all but a full review. `@gitdog` works as shorthand.", ].join("\n"); } + +type StatusAction = { + status: string; + errorMessage?: string; + reviewRequested?: boolean; + executionResult?: { + published?: boolean; + reason?: string; + reviewUrl?: string; + }; + proposedChanges?: { + source?: { headSha?: string }; + changedFiles?: number; + reviewedSince?: string; + }; +}; + +// Failures safe to show on a public pull request, in the owner's words. +function failureReason(message: string | undefined): string { + switch (true) { + case !message: + return "Gitdog could not complete the review."; + case message!.startsWith("Not enough tokens remain") || + message!.startsWith("Token limit reached"): + return "The token limit of the account that connected this repository has been reached."; + // A user's own provider key is the owner's business, not the public's. + case message!.startsWith("Your "): + return "The AI provider settings of the account that connected this repository need attention."; + case message!.startsWith("GitHub publication outcome requires inspection"): + return "Gitdog could not confirm that the review was posted. Check the reviews above before asking again."; + case /split this PR/.test(message!): + return message!; + default: + return "Gitdog could not complete the review."; + } +} + +/** + * The status comment for a review in its current state, or null when the + * state needs no comment (a successful review is the review itself, but an + * existing status comment is still updated to point at it). + */ +export function reviewStatusBody( + action: StatusAction, + slug: string, +): string | null { + const sha = action.proposedChanges?.source?.headSha ?? "this pull request"; + const files = action.proposedChanges?.changedFiles; + switch (action.status) { + case "pending": + return `**Reviewing** ${sha}${ + typeof files === "number" + ? ` · ${files} changed ${files === 1 ? "file" : "files"}` + : "" + }\n\nThe review will be posted here when it is done.`; + case "pending_approval": + return `**Review ready for approval** for ${sha}\n\nThe repository owner can post it from Gitdog.`; + case "approved": + return `**Posting the review** for ${sha}`; + case "completed": + if (action.executionResult?.published) + return `**Reviewed** ${sha}${ + action.executionResult.reviewUrl + ? ` · [see the review](${action.executionResult.reviewUrl})` + : "" + }`; + if (action.executionResult?.reason === "no_new_changes") + return `**No new changes to review** in ${sha}${ + action.proposedChanges?.reviewedSince + ? ` since ${action.proposedChanges.reviewedSince}` + : "" + }\n\nNothing in the pull request diff changed, so no review was posted. Comment \`@${slug} full review\` to review the whole pull request again.`; + return null; + case "failed": + if (action.errorMessage?.includes("superseded")) + return `**Review cancelled** for ${sha}\n\nA newer push replaced this commit.`; + return `**Review not completed** for ${sha}\n\n${failureReason( + action.errorMessage, + )} Comment \`@${slug} review\` to try again.`; + default: + return null; + } +} diff --git a/convex/pullRequests/internalAction.ts b/convex/pullRequests/internalAction.ts index acfb4d2..3b09f54 100644 --- a/convex/pullRequests/internalAction.ts +++ b/convex/pullRequests/internalAction.ts @@ -1,6 +1,6 @@ import { internalAction } from "../_generated/server"; import { internal } from "../_generated/api"; -import type { Id } from "../_generated/dataModel"; +import type { Doc, Id } from "../_generated/dataModel"; import type { ActionCtx } from "../_generated/server"; import { v } from "convex/values"; import { @@ -57,10 +57,11 @@ import { } from "./review"; import { addIssueComment, + updateIssueComment, GITHUB_API_VERSION, fetchGitHubAppJsonWithRetry, } from "../github/helpers"; -import { helpText } from "./commands"; +import { helpText, reviewStatusBody } from "./commands"; import { getConfiguredGitHubAppSlug } from "../github/botIdentity"; type Source = { headSha: string; baseSha: string; title: string; body: string }; @@ -327,6 +328,13 @@ export const generate = internalAction({ }); return; } + // Show on the pull request that a review is running, once per review. + if ( + !previous && + !verificationOnly && + initial.action.statusCommentId === undefined + ) + await postReviewingStatus(ctx, access, initial, data); const fileKeys = new Set(previous?.fileKeys); if (batched) { // Keep the complete file inventory bounded even when paths are long. @@ -640,18 +648,6 @@ export const generate = internalAction({ : undefined), ), }); - // Out of tokens: tell the pull request instead of failing silently. - // The notice skips rechecks, which promise not to post. - if ( - (cause instanceof ReviewQuotaError && - cause.reason === "insufficient") || - (cause instanceof ChatBillingError && cause.reason === "limit_reached") - ) - await ctx.scheduler.runAfter( - 0, - internal.pullRequests.internalAction.noticeLimit, - { actionId: args.actionId }, - ); } }, }); @@ -1170,6 +1166,49 @@ export const dismissStaleApproval = internalAction({ }, }); +/** Best effort: the review runs whether or not GitHub takes the comment. */ +async function postReviewingStatus( + ctx: ActionCtx, + access: VerifiedRepositoryAccess, + state: { + action: Doc<"ticketActions">; + ticket: Doc<"tickets">; + }, + data: { changedFiles: number }, +) { + const slug = getConfiguredGitHubAppSlug(); + if (!slug) return; + const body = reviewStatusBody( + { + ...state.action, + proposedChanges: { + ...state.action.proposedChanges, + changedFiles: data.changedFiles, + }, + }, + slug, + ); + if (!body) return; + try { + const commentId = await addIssueComment( + access.owner, + access.repo, + state.ticket.githubNumber, + body, + access.token, + ); + await ctx.runMutation( + internal.pullRequests.internalMutation.recordStatusComment, + { actionId: state.action._id, commentId }, + ); + } catch (error) { + console.warn( + "Review status comment could not be posted:", + error instanceof Error ? error.message : "unknown error", + ); + } +} + async function reply( access: VerifiedRepositoryAccess, number: number, @@ -1336,36 +1375,19 @@ export const runCommand = internalAction({ ).catch(() => undefined); throw error; }); + // A started review announces itself in its status comment. + if (result.command !== "resolve" && result.status === "started") return; return reply(access, args.number, commandReply(result, slug)); } } }, }); -/** Tell a pull request, at most once a day, that its review limit was hit. */ -export const noticeLimit = internalAction({ - args: { actionId: v.id("ticketActions") }, - handler: async (ctx, args): Promise => { - const target = await ctx.runMutation( - internal.pullRequests.internalMutation.claimLimitNotice, - args, - ); - const slug = getConfiguredGitHubAppSlug(); - if (!target || !slug) return; - const access = await resolveVerifiedRepositoryAccess( - ctx, - target.repositoryId, - ); - await reply( - access, - target.number, - `**Review limit reached**\n\nGitdog could not review${target.headSha ? ` ${target.headSha}` : " this pull request"} because the token limit of the account that connected this repository has been reached. Once more tokens are available, comment \`@${slug} review\` to try again.`, - ); - }, -}); - -/** Answer a requested review that found no pull request changes to review. */ -export const noticeNoChanges = internalAction({ +/** + * Bring a review's status comment up to date: post it for a failure or a + * no-change follow-up that has none yet, and otherwise edit it in place. + */ +export const settleStatusComment = internalAction({ args: { actionId: v.id("ticketActions") }, handler: async (ctx, args): Promise => { const state = await ctx.runQuery( @@ -1373,19 +1395,42 @@ export const noticeNoChanges = internalAction({ args, ); const slug = getConfiguredGitHubAppSlug(); - if (!state || !slug) return; - const source = sourceOf(state.action.proposedChanges?.source); - const since = state.action.proposedChanges?.reviewedSince; + if (!state || !slug || state.action.reviewVerificationOnly === true) return; + const body = reviewStatusBody(state.action, slug); + const existing = state.action.statusCommentId; + // Only something a reader is waiting on gets a new comment; a posted + // review or a cancelled one speaks for itself. + const postNew = + existing === undefined && + body !== null && + (state.action.status === "failed" + ? !state.action.errorMessage?.includes("superseded") + : state.action.executionResult?.reason === "no_new_changes"); + if (!body || (existing === undefined && !postNew)) return; const access = await resolveVerifiedRepositoryAccess( ctx, state.repository._id, ); - await reply( - access, - state.ticket.githubNumber, - `**No new changes to review** in ${source.headSha}${ - typeof since === "string" ? ` since ${since}` : "" - }\n\nNothing in the pull request diff changed, so no review was posted. Comment \`@${slug} full review\` to review the whole pull request again.`, - ); + if (existing !== undefined) + await updateIssueComment( + access.owner, + access.repo, + existing, + body, + access.token, + ); + else { + const commentId = await addIssueComment( + access.owner, + access.repo, + state.ticket.githubNumber, + body, + access.token, + ); + await ctx.runMutation( + internal.pullRequests.internalMutation.recordStatusComment, + { actionId: args.actionId, commentId }, + ); + } }, }); diff --git a/convex/pullRequests/internalMutation.ts b/convex/pullRequests/internalMutation.ts index 1920a0d..6006fe9 100644 --- a/convex/pullRequests/internalMutation.ts +++ b/convex/pullRequests/internalMutation.ts @@ -166,6 +166,15 @@ async function observeDroppedPush(ctx: MutationCtx, ticketId: Id<"tickets">) { ); } +/** Bring a review's status comment on the pull request up to date. */ +async function settleStatus(ctx: MutationCtx, actionId: Id<"ticketActions">) { + await ctx.scheduler.runAfter( + 0, + internal.pullRequests.internalAction.settleStatusComment, + { actionId }, + ); +} + function hasAttemptedReview(action: Doc<"ticketActions">) { if (action.reviewGenerationToken !== undefined) return true; // Keep the slot available when a queued review was superseded before its worker started. @@ -493,13 +502,16 @@ async function observePullRequest( (action.reviewVerificationOnly !== true || commitChanged) && ["pending", "pending_approval", "approved"].includes(action.status) && action.executionClaimedAt === undefined - ) + ) { await ctx.db.patch(action._id, { status: "failed", errorMessage: SUPERSEDED_REVIEW, ...ENDED_BATCH, completedAt: now, }); + if (action.statusCommentId !== undefined) + await settleStatus(ctx, action._id); + } } await ctx.db.patch(ticket._id, { pullRequestObservedAt: observedAt, @@ -908,12 +920,7 @@ export const propose = internalMutation({ reviewFailureDetails: `Stage · Token quota check (review batch ${page})\nReason · ${quota.message}`, completedAt: Date.now(), }); - if (quota.reason === "insufficient") - await ctx.scheduler.runAfter( - 0, - internal.pullRequests.internalAction.noticeLimit, - { actionId: action._id }, - ); + await settleStatus(ctx, action._id); return false; } } @@ -1055,16 +1062,9 @@ export const propose = internalMutation({ // A review that ended unpublished is done, so a dropped push can be // reviewed now. One waiting for approval keeps it: reviewing the newer // head would supersede the review before the owner sees it. - else if (unpublished) { - // A requested review answered "started", so it says it found nothing. - if (nothingNew && action.reviewRequested === true) - await ctx.scheduler.runAfter( - 0, - internal.pullRequests.internalAction.noticeNoChanges, - { actionId: action._id }, - ); - await observeDroppedPush(ctx, action.ticketId); - } + else if (unpublished) await observeDroppedPush(ctx, action.ticketId); + // The status comment says the review is ready, waiting or found nothing. + await settleStatus(ctx, action._id); return true; }, }); @@ -1213,6 +1213,7 @@ export const finish = internalMutation({ } : undefined, }); + await settleStatus(ctx, action._id); await observeDroppedPush(ctx, action.ticketId); }, }); @@ -1246,7 +1247,10 @@ export const expire = internalMutation({ ? "Review exceeded its processing deadline" : "GitHub publication outcome requires inspection; automatic retries are disabled", }); - if (action) await observeDroppedPush(ctx, action.ticketId); + if (action) { + await settleStatus(ctx, action._id); + await observeDroppedPush(ctx, action.ticketId); + } }, }); @@ -1300,12 +1304,7 @@ export const completeUnposted = internalMutation({ completedAt: Date.now(), executionResult: { published: false, reason: "no_new_changes" }, }); - if (action.reviewRequested === true) - await ctx.scheduler.runAfter( - 0, - internal.pullRequests.internalAction.noticeNoChanges, - { actionId: action._id }, - ); + await settleStatus(ctx, action._id); await observeDroppedPush(ctx, action.ticketId); }, }); @@ -1381,36 +1380,13 @@ export const recordCommand = internalMutation({ }, }); -/** Claim the once-a-day notice that this PR hit its review limit. */ -export const claimLimitNotice = internalMutation({ - args: { actionId: v.id("ticketActions") }, +/** Remember the status comment posted for a review, once. */ +export const recordStatusComment = internalMutation({ + args: { actionId: v.id("ticketActions"), commentId: v.number() }, handler: async (ctx, args) => { const action = await ctx.db.get(args.actionId); - const ticket = action ? await ctx.db.get(action.ticketId) : null; - const repo = ticket?.repositoryId - ? await ctx.db.get(ticket.repositoryId) - : null; - if ( - !ticket?.repositoryId || - ticket.ticketType !== "pull_request" || - ticket.status !== "open" || - ticket.deletedAt !== undefined || - action?.reviewVerificationOnly === true || - !repo?.isActive || - repo.settings.pullRequestReviewsEnabled !== true - ) - return null; - const now = Date.now(); - if ( - ticket.pullRequestLimitNoticeAt !== undefined && - ticket.pullRequestLimitNoticeAt > now - COMMAND_PAUSE_MS - ) - return null; - await ctx.db.patch(ticket._id, { pullRequestLimitNoticeAt: now }); - return { - repositoryId: ticket.repositoryId, - number: ticket.githubNumber, - headSha: action?.proposedChanges?.source?.headSha as string | undefined, - }; + if (!action || action.statusCommentId !== undefined) return false; + await ctx.db.patch(action._id, { statusCommentId: args.commentId }); + return true; }, }); diff --git a/convex/schema.ts b/convex/schema.ts index 69529b9..3c3bf5f 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -961,6 +961,8 @@ const applicationTables = { reviewVerificationOnly: v.optional(v.boolean()), // Started by a "@gitdogbot review" comment or the CLI. reviewRequested: v.optional(v.boolean()), + // The pull request comment that shows this review's progress and outcome. + statusCommentId: v.optional(v.number()), reviewRecheckOfActionId: v.optional(v.id("ticketActions")), // The last finished review of this PR: a later push reviews only the // files changed since its head. diff --git a/packages/cli/docs/REFERENCE.md b/packages/cli/docs/REFERENCE.md index 3c14c01..5c4f243 100644 --- a/packages/cli/docs/REFERENCE.md +++ b/packages/cli/docs/REFERENCE.md @@ -385,7 +385,8 @@ review. The `reviews` commands match the pull request comments `@gitdogbot review` (commits since the last review), `@gitdogbot full review` and `@gitdogbot resolve` (resolves Gitdog's threads); the CLI prints the result instead of replying on GitHub. On GitHub, commands go in the pull request -conversation, not in replies inside a review thread. `reviews request` asks before spending tokens +conversation, not in replies inside a review thread; `@gitdog` works as +shorthand for `@gitdogbot`. `reviews request` asks before spending tokens (skip with `--yes`) and prints the queued review's action id. `reviews recheck` reruns a failed review on the latest commit without posting. diff --git a/public/llms-full.txt b/public/llms-full.txt index b3b14ab..c542538 100644 --- a/public/llms-full.txt +++ b/public/llms-full.txt @@ -49,7 +49,7 @@ Gitdog reviews a pull request once, when it is opened or marked ready for review | `@gitdogbot resolve` | `gitdog reviews resolve ` | Resolves every Gitdog review thread | | `@gitdogbot help` | `gitdog reviews --help` | Lists the commands | -A command is a comment line that starts with the mention. Commands go in the pull request conversation; replies inside a review thread are not read, and pull requests opened by bots such as Dependabot are not reviewed. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again. +A command is a comment line that starts with the mention, or follows another review bot's command on the same line. `@gitdog` works as shorthand. While a review runs, Gitdog keeps one status comment on the pull request up to date. Commands go in the pull request conversation; replies inside a review thread are not read, and pull requests opened by bots such as Dependabot are not reviewed. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again. In issues and discussions, mention `@gitdogbot` with a question to get a reply grounded in the repository. diff --git a/public/llms.txt b/public/llms.txt index d5ba734..3cd4337 100644 --- a/public/llms.txt +++ b/public/llms.txt @@ -25,7 +25,7 @@ Gitdog reviews a pull request once, when it is opened or marked ready for review | `@gitdogbot resolve` | `gitdog reviews resolve ` | Resolves every Gitdog review thread | | `@gitdogbot help` | `gitdog reviews --help` | Lists the commands | -A command is a comment line that starts with the mention. Commands go in the pull request conversation; replies inside a review thread are not read, and pull requests opened by bots such as Dependabot are not reviewed. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again. +A command is a comment line that starts with the mention, or follows another review bot's command on the same line. `@gitdog` works as shorthand. While a review runs, Gitdog keeps one status comment on the pull request up to date. Commands go in the pull request conversation; replies inside a review thread are not read, and pull requests opened by bots such as Dependabot are not reviewed. Collaborators can run every command; the pull request author, who may work from a fork, can run all but a full review. A review that failed or ran out of tokens can be requested again. In issues and discussions, mention `@gitdogbot` with a question to get a reply grounded in the repository. diff --git a/test/convex/pullRequests/commands.test.ts b/test/convex/pullRequests/commands.test.ts index 93b181a..1441a6c 100644 --- a/test/convex/pullRequests/commands.test.ts +++ b/test/convex/pullRequests/commands.test.ts @@ -22,6 +22,12 @@ describe("pull request commands", () => { ["@gitdogbot help", "help"], ["@gitdogbot", "help"], ["@gitdogbot summarize this", "unknown"], + ["@gitdog review", "review"], + ["@coderabbitai review @gitdogbot review", "review"], + ["@coderabbitai full review @gitdog full review", "full_review"], + ["@gitdogbot review @coderabbitai review", "review"], + ["@alice I think @gitdogbot review is broken", null], + ["@gitdoghouse review", null], ["no mention here", null], ["@gitdogbot-dev review", null], ["email@gitdogbot review", null], diff --git a/test/convex/pullRequests/lifecycle.test.ts b/test/convex/pullRequests/lifecycle.test.ts index 0bb89c5..cbf8966 100644 --- a/test/convex/pullRequests/lifecycle.test.ts +++ b/test/convex/pullRequests/lifecycle.test.ts @@ -42,6 +42,11 @@ const settings = { }; async function fixture() { const t = convexTest(schema, modules); + // Review status comments go through these, apart from the review request. + const statusComments = { + add: vi.spyOn(helpers, "addIssueComment").mockResolvedValue(901), + update: vi.spyOn(helpers, "updateIssueComment").mockResolvedValue(), + }; const ids = await t.run(async (ctx) => { const userId = await ctx.db.insert("users", {}); // The one-time trial is already used, so quota checks see only the @@ -117,7 +122,7 @@ async function fixture() { full, }) ).actionId; - return { t, ...ids, observe, request }; + return { t, ...ids, observe, request, statusComments }; } const findings = [ { @@ -429,23 +434,105 @@ describe("review commands", () => { }), ).toBe("unavailable"); }); - it.each([ - ["a recheck", { reviewVerificationOnly: true }, {}], - ["a closed pull request", {}, { status: "closed" as const }], - ])("posts no limit notice for %s", async (_case, action, ticket) => { - const f = await fixture(); - const actionId = (await f.observe())!; - await f.t.run(async (ctx) => { - const current = (await ctx.db.get(actionId))!; - await ctx.db.patch(actionId, action); - await ctx.db.patch(current.ticketId, ticket); + describe("status comment", () => { + const settle = (f: Awaited>, actionId: string) => + f.t.action(internal.pullRequests.internalAction.settleStatusComment, { + actionId: actionId as never, + }); + async function withAccess(f: Awaited>) { + const identity = + await import("../../../convex/github/repositoryIdentity"); + const repository = (await f.t.run((ctx) => ctx.db.get(f.repositoryId)))!; + vi.spyOn(identity, "resolveVerifiedRepositoryAccess").mockResolvedValue({ + repository, + installation: {} as never, + githubRepositoryId: 123, + githubRepo: "owner/repo", + owner: "owner", + repo: "repo", + token: "fixture-token", + }); + } + it("posts a failure with a reason safe for a public pull request", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage: + "Your Anthropic API key was rejected. Replace or remove it in Gitdog settings.", + }), + ); + await settle(f, actionId); + const body = f.statusComments.add.mock.calls[0][3]; + expect(body).toContain("**Review not completed**"); + expect(body).toContain("AI provider settings"); + expect(body).not.toContain("Anthropic API key"); + expect( + (await f.t.run((ctx) => ctx.db.get(actionId)))?.statusCommentId, + ).toBe(901); + }); + it("says when the token limit stopped a review", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage: + "Not enough tokens remain for this pull request review. No review was published.", + }), + ); + await settle(f, actionId); + expect(f.statusComments.add.mock.calls[0][3]).toContain( + "token limit of the account", + ); + }); + it("edits the reviewing comment in place when the review ends", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + statusCommentId: 55, + status: "completed", + executionResult: { + published: true, + reviewUrl: + "https://github.com/owner/repo/pull/12#pullrequestreview-1", + }, + }), + ); + await settle(f, actionId); + expect(f.statusComments.add).not.toHaveBeenCalled(); + expect(f.statusComments.update.mock.calls[0][2]).toBe(55); + expect(f.statusComments.update.mock.calls[0][3]).toContain( + "**Reviewed**", + ); + }); + it("posts nothing for a recheck or a cancelled review without a comment", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + reviewVerificationOnly: true, + errorMessage: "GitHub 502", + }), + ); + await settle(f, actionId); + await f.t.run((ctx) => + ctx.db.patch(actionId, { + reviewVerificationOnly: undefined, + errorMessage: + "Pull request changed or closed; this review is superseded", + }), + ); + await settle(f, actionId); + expect(f.statusComments.add).not.toHaveBeenCalled(); }); - expect( - await f.t.mutation( - internal.pullRequests.internalMutation.claimLimitNotice, - { actionId }, - ), - ).toBeNull(); }); it("pauses a commenter who sends too many commands", async () => { const f = await fixture(); @@ -528,27 +615,14 @@ describe("review commands", () => { const r = await replies(f); await r.run("help"); expect(r.posted()).toContain("`@gitdogbot full review`"); + // A started review answers in its own status comment, not a reply. await r.run("review"); - expect(r.posted()).toBe(`**Review started** for ${next.headSha}`); + expect(r.comment).toHaveBeenCalledTimes(1); await r.run("review"); expect(r.posted()).toContain("**A review is already running.**"); await r.run("pause_now"); expect(r.posted()).toContain("@octo sent too many Gitdog commands"); - expect(r.comment).toHaveBeenCalledTimes(4); - }); - it("posts the limit notice when a review runs out of tokens", async () => { - const f = await fixture(); - const actionId = (await f.observe())!; - const r = await replies(f); - await f.t.action(internal.pullRequests.internalAction.noticeLimit, { - actionId, - }); - await f.t.action(internal.pullRequests.internalAction.noticeLimit, { - actionId, - }); - expect(r.comment).toHaveBeenCalledTimes(1); - expect(r.posted()).toContain("**Review limit reached**"); - expect(r.posted()).toContain("`@gitdogbot review`"); + expect(r.comment).toHaveBeenCalledTimes(3); }); it("runs the same review commands from the CLI without posting a reply", async () => { const f = await reviewed(); @@ -576,22 +650,6 @@ describe("review commands", () => { }), ).rejects.toThrow("not authorized"); }); - it("tells a pull request about its limit at most once a day", async () => { - const f = await fixture(); - const actionId = (await f.observe())!; - const claim = () => - f.t.mutation(internal.pullRequests.internalMutation.claimLimitNotice, { - actionId, - }); - expect(await claim()).toMatchObject({ - repositoryId: f.repositoryId, - number: 12, - headSha: source.headSha, - }); - expect(await claim()).toBeNull(); - vi.advanceTimersByTime(24 * 60 * 60_000 + 1); - expect(await claim()).not.toBeNull(); - }); }); describe("PR review durable lifecycle", () => { it("does not enqueue bot-authored pull requests through observation", async () => { @@ -2676,6 +2734,15 @@ describe("batched PR reviews", () => { }; } + it("shows a reviewing comment once, when the review starts", async () => { + const f = await batchFixture(); + await f.generate(); + expect(f.statusComments.add).toHaveBeenCalledTimes(1); + expect(f.statusComments.add.mock.calls[0][3]).toContain("**Reviewing**"); + expect((await f.getAction())?.statusCommentId).toBe(901); + await f.generate(2); + expect(f.statusComments.add).toHaveBeenCalledTimes(1); + }); it("combines findings from a 61-file PR into one review body after all batches finish", async () => { const f = await batchFixture(); await f.generate(); @@ -2891,7 +2958,7 @@ describe("batched PR reviews", () => { ctx.db.system.query("_scheduled_functions").collect(), ); expect( - scheduled.filter((job) => job.name.includes("noticeLimit")), + scheduled.filter((job) => job.name.includes("settleStatusComment")), ).toHaveLength(1); }); @@ -3236,9 +3303,9 @@ describe("batched PR reviews", () => { // No review is posted; the requested review says it found nothing new. const urls = f.fetch.mock.calls.map(([url]) => String(url)); expect(urls.some((url) => url.includes("/reviews"))).toBe(false); - expect( - urls.filter((url) => url.endsWith("/issues/12/comments")), - ).toHaveLength(1); + expect(f.statusComments.add.mock.calls.at(-1)?.[3]).toContain( + "**No new changes to review**", + ); expect(await f.t.run((ctx) => ctx.db.get(f.nextId))).toMatchObject({ status: "completed", executionResult: { published: false, reason: "no_new_changes" }, @@ -3251,9 +3318,9 @@ describe("batched PR reviews", () => { expect(f.batches).toEqual([]); const urls = f.fetch.mock.calls.map(([url]) => String(url)); expect(urls.some((url) => url.includes("/reviews"))).toBe(false); - expect( - JSON.parse(f.fetch.mock.calls.at(-1)![1]!.body as string).body, - ).toContain("**No new changes to review**"); + expect(f.statusComments.add.mock.calls.at(-1)?.[3]).toContain( + "**No new changes to review**", + ); expect(await f.t.run((ctx) => ctx.db.get(f.nextId))).toMatchObject({ status: "completed", executionResult: { published: false, reason: "no_new_changes" }, From 2d08b4706c8a6e7095cc6bb9fb25a11aaecc93c8 Mon Sep 17 00:00:00 2001 From: Hyo Date: Wed, 18 Nov 2026 01:47:17 +0900 Subject: [PATCH 2/4] fix: keep status comments accurate and quiet, and narrow chained commands Status comments change only on outcomes: an expiry that ended nothing, a running review and a review about to post leave them alone, a late settle re-reads the review before editing, and rejection, a recheck supersede, an approval-setting change and a review that ended before its comment was recorded all update it. A pull request that keeps failing hears about automatic reviews at most once a day, cancellations read as cancelled, and an oversized pull request gets no retry hint. A chained command must follow a known review bot, a chained thank-you posts nothing, and @gitdog belongs to the production app only. --- README.md | 3 +- convex/pullRequests/commands.ts | 83 ++++++++++---- convex/pullRequests/internalAction.ts | 107 ++++++++++++------ convex/pullRequests/internalMutation.ts | 39 ++++++- convex/schema.ts | 4 +- convex/ticketActions/internalMutation.ts | 7 ++ docs/github-app-setup.md | 5 +- public/llms-full.txt | 2 +- public/llms.txt | 2 +- test/convex/pullRequests/commands.test.ts | 7 ++ test/convex/pullRequests/lifecycle.test.ts | 123 +++++++++++++++++++++ 11 files changed, 314 insertions(+), 68 deletions(-) diff --git a/README.md b/README.md index 1aa00f2..c9d937f 100644 --- a/README.md +++ b/README.md @@ -102,8 +102,7 @@ but a full review. A review that failed or ran out of tokens can be requested again, and pull requests opened by bots such as Dependabot are not reviewed. In an issue or discussion, mention `@gitdogbot` with a question to get a reply -grounded in the repository. When the account runs out of tokens, Gitdog says so -on the pull request at most once a day. Someone who sends more than 10 commands +grounded in the repository. When a review cannot finish, for example because the account ran out of tokens, its status comment says why; for automatic reviews Gitdog posts at most one such comment a day per pull request. Someone who sends more than 10 commands an hour or 40 a day to one repository is paused there for 24 hours. ## Get started diff --git a/convex/pullRequests/commands.ts b/convex/pullRequests/commands.ts index f13592f..d255289 100644 --- a/convex/pullRequests/commands.ts +++ b/convex/pullRequests/commands.ts @@ -2,6 +2,20 @@ import { getConfiguredGitHubAppSlug } from "../github/botIdentity"; export type ReviewCommand = "review" | "full_review" | "resolve" | "help"; +// Review bots whose commands another may follow on the same line. Any +// GitHub App account, named "...[bot]", counts too. +const REVIEW_BOTS = [ + "coderabbitai", + "copilot", + "gemini-code-assist", + "sourcery-ai", + "greptile-apps", + "greptileai", + "codeant-ai", + "qodo-merge", + "[\\w-]+\\[bot\\]", +].join("|"); + // Commands one commenter may send to one repository before being paused. export const COMMANDS_PER_HOUR = 10; export const COMMANDS_PER_DAY = 40; @@ -44,20 +58,26 @@ export function parseReviewCommand( ) .join("\n"); // A command starts a line, or follows another review bot's command on it, - // as in "@coderabbitai review @gitdogbot review"; "@gitdogbot review" - // inside a sentence does not run one. "@gitdog" is accepted as shorthand. + // as in "@coderabbitai review @gitdogbot review". "@gitdog" is shorthand + // for the production app only, so a development app never answers it. + const names = slug === "gitdogbot" ? `${slug}|gitdog` : slug; const match = new RegExp( - `^[ \\t]*(?:@(?:[\\w-]*(?:bot|ai|copilot|assist)|[\\w-]+\\[bot\\])(?:[ \\t]+[a-z-]+){0,2}[ \\t]+){0,2}@(?:${slug}|gitdog)(?=\\s|[:,]|$)[:,]?[ \\t]*([^@\\n]*)`, + `^[ \\t]*((?:@(?:${REVIEW_BOTS})(?:[ \\t]+[a-z-]+){0,2}[ \\t]+){0,3})@(?:${names})(?=\\s|[:,]|$)[:,]?[ \\t]*([^@\\n]*)`, "im", ).exec(text); if (!match) return null; - const words = match[1] + const chained = match[1].length > 0; + const words = match[2] .toLowerCase() .replace(/[^a-z\s]/g, " ") .trim() .split(/\s+/) .filter(Boolean); switch (true) { + // A chained mention without a command is a thank-you, not a request. + case chained && + !["help", "review", "full", "resolve"].includes(words[0] ?? ""): + return null; case words.length === 0 || words[0] === "help": return "help"; case words[0] === "full" && words[1] === "review": @@ -87,7 +107,6 @@ export function helpText(slug: string): string { type StatusAction = { status: string; errorMessage?: string; - reviewRequested?: boolean; executionResult?: { published?: boolean; reason?: string; @@ -100,23 +119,45 @@ type StatusAction = { }; }; -// Failures safe to show on a public pull request, in the owner's words. -function failureReason(message: string | undefined): string { +// A review cancelled because the pull request moved on, not one to retry. +export function isCancelledReview(message: string | undefined): boolean { + return ( + message !== undefined && + (message.includes("superseded") || + /^Pull request changed (before|during|after)/.test(message)) + ); +} + +// Failures safe to show on a public pull request, in the owner's words. The +// second value says whether asking again can help. +function failureReason(message: string | undefined): [string, boolean] { switch (true) { case !message: - return "Gitdog could not complete the review."; + return ["Gitdog could not complete the review.", true]; case message!.startsWith("Not enough tokens remain") || message!.startsWith("Token limit reached"): - return "The token limit of the account that connected this repository has been reached."; + return [ + "The token limit of the account that connected this repository has been reached.", + true, + ]; // A user's own provider key is the owner's business, not the public's. case message!.startsWith("Your "): - return "The AI provider settings of the account that connected this repository need attention."; + return [ + "The AI provider settings of the account that connected this repository need attention.", + true, + ]; case message!.startsWith("GitHub publication outcome requires inspection"): - return "Gitdog could not confirm that the review was posted. Check the reviews above before asking again."; + return [ + "Gitdog could not confirm that the review was posted. Check the reviews above before asking again.", + false, + ]; case /split this PR/.test(message!): - return message!; + return [ + "This pull request is too large to review completely. Split it into smaller pull requests.", + false, + ]; default: - return "Gitdog could not complete the review."; + return ["Gitdog could not complete the review.", true]; } } @@ -142,6 +183,8 @@ export function reviewStatusBody( return `**Review ready for approval** for ${sha}\n\nThe repository owner can post it from Gitdog.`; case "approved": return `**Posting the review** for ${sha}`; + case "rejected": + return `**Review not posted** for ${sha}\n\nThe repository owner chose not to post this review.`; case "completed": if (action.executionResult?.published) return `**Reviewed** ${sha}${ @@ -156,12 +199,14 @@ export function reviewStatusBody( : "" }\n\nNothing in the pull request diff changed, so no review was posted. Comment \`@${slug} full review\` to review the whole pull request again.`; return null; - case "failed": - if (action.errorMessage?.includes("superseded")) - return `**Review cancelled** for ${sha}\n\nA newer push replaced this commit.`; - return `**Review not completed** for ${sha}\n\n${failureReason( - action.errorMessage, - )} Comment \`@${slug} review\` to try again.`; + case "failed": { + if (isCancelledReview(action.errorMessage)) + return `**Review cancelled** for ${sha}\n\nThe pull request changed before the review finished.`; + const [reason, retry] = failureReason(action.errorMessage); + return `**Review not completed** for ${sha}\n\n${reason}${ + retry ? ` Comment \`@${slug} review\` to try again.` : "" + }`; + } default: return null; } diff --git a/convex/pullRequests/internalAction.ts b/convex/pullRequests/internalAction.ts index 3b09f54..b5fb0b9 100644 --- a/convex/pullRequests/internalAction.ts +++ b/convex/pullRequests/internalAction.ts @@ -61,7 +61,12 @@ import { GITHUB_API_VERSION, fetchGitHubAppJsonWithRetry, } from "../github/helpers"; -import { helpText, reviewStatusBody } from "./commands"; +import { + COMMAND_PAUSE_MS, + helpText, + isCancelledReview, + reviewStatusBody, +} from "./commands"; import { getConfiguredGitHubAppSlug } from "../github/botIdentity"; type Source = { headSha: string; baseSha: string; title: string; body: string }; @@ -332,7 +337,13 @@ export const generate = internalAction({ if ( !previous && !verificationOnly && - initial.action.statusCommentId === undefined + initial.action.statusCommentId === undefined && + // After a recent automatic failure, a retry on the next push stays + // quiet until it has something to show. + (initial.action.reviewRequested === true || + initial.ticket.pullRequestLimitNoticeAt === undefined || + initial.ticket.pullRequestLimitNoticeAt < + Date.now() - COMMAND_PAUSE_MS) ) await postReviewingStatus(ctx, access, initial, data); const fileKeys = new Set(previous?.fileKeys); @@ -1384,34 +1395,43 @@ export const runCommand = internalAction({ }); /** - * Bring a review's status comment up to date: post it for a failure or a - * no-change follow-up that has none yet, and otherwise edit it in place. + * Bring a review's status comment up to date once its outcome is known: post + * one for a failure or a no-change follow-up that has none yet, and + * otherwise edit it in place. A running review is left alone; its comment + * already says so. */ export const settleStatusComment = internalAction({ args: { actionId: v.id("ticketActions") }, handler: async (ctx, args): Promise => { - const state = await ctx.runQuery( - internal.pullRequests.internalQuery.get, - args, - ); const slug = getConfiguredGitHubAppSlug(); - if (!state || !slug || state.action.reviewVerificationOnly === true) return; - const body = reviewStatusBody(state.action, slug); - const existing = state.action.statusCommentId; - // Only something a reader is waiting on gets a new comment; a posted - // review or a cancelled one speaks for itself. - const postNew = - existing === undefined && - body !== null && - (state.action.status === "failed" - ? !state.action.errorMessage?.includes("superseded") - : state.action.executionResult?.reason === "no_new_changes"); - if (!body || (existing === undefined && !postNew)) return; + const read = () => + ctx.runQuery(internal.pullRequests.internalQuery.get, args); + const first = await read(); + if ( + !first || + !slug || + first.action.reviewVerificationOnly === true || + first.action.status === "pending" || + first.action.status === "approved" + ) + return; const access = await resolveVerifiedRepositoryAccess( ctx, - state.repository._id, + first.repository._id, ); - if (existing !== undefined) + // Re-read right before writing, so a late settle never overwrites a + // newer outcome with an older one. + const state = await read(); + if ( + !state || + state.action.status === "pending" || + state.action.status === "approved" + ) + return; + const body = reviewStatusBody(state.action, slug); + if (!body) return; + const existing = state.action.statusCommentId; + if (existing !== undefined) { await updateIssueComment( access.owner, access.repo, @@ -1419,18 +1439,37 @@ export const settleStatusComment = internalAction({ body, access.token, ); - else { - const commentId = await addIssueComment( - access.owner, - access.repo, - state.ticket.githubNumber, - body, - access.token, - ); - await ctx.runMutation( - internal.pullRequests.internalMutation.recordStatusComment, - { actionId: args.actionId, commentId }, - ); + return; } + // Only something a reader is waiting on gets a new comment: a failure + // worth retrying or a no-change follow-up, while reviews stay on. An + // automatic review says so at most once a day per pull request. + const failed = state.action.status === "failed"; + if ( + (failed + ? isCancelledReview(state.action.errorMessage) + : // Only a requested review was promised an answer. + state.action.executionResult?.reason !== "no_new_changes" || + state.action.reviewRequested !== true) || + state.ticket.status !== "open" || + state.repository.settings.pullRequestReviewsEnabled !== true || + (state.action.reviewRequested !== true && + !(await ctx.runMutation( + internal.pullRequests.internalMutation.claimAutomaticNotice, + { ticketId: state.ticket._id }, + ))) + ) + return; + const commentId = await addIssueComment( + access.owner, + access.repo, + state.ticket.githubNumber, + body, + access.token, + ); + await ctx.runMutation( + internal.pullRequests.internalMutation.recordStatusComment, + { actionId: args.actionId, commentId }, + ); }, }); diff --git a/convex/pullRequests/internalMutation.ts b/convex/pullRequests/internalMutation.ts index 6006fe9..b08d056 100644 --- a/convex/pullRequests/internalMutation.ts +++ b/convex/pullRequests/internalMutation.ts @@ -269,13 +269,16 @@ export const queueVerification = internalMutation({ action.executionClaimedAt === undefined && (!matches(action) || (!args.open && action.reviewVerificationOnly !== true)) - ) + ) { await ctx.db.patch(action._id, { status: "failed", errorMessage: SUPERSEDED_REVIEW, ...ENDED_BATCH, completedAt: Date.now(), }); + if (action.statusCommentId !== undefined) + await settleStatus(ctx, action._id); + } } await ctx.db.patch(ticket._id, { pullRequestObservedAt: Math.max( @@ -454,7 +457,7 @@ async function observePullRequest( ticket.githubTitle !== source.title || ticket.githubBody !== source.body; // A requested review a push cancelled before it ran carries over to the - // new head, so "Review started" is still answered with a review. + // new head, so the command is still answered with a review. if ( !request && args.open && @@ -1115,6 +1118,7 @@ export const claim = internalMutation({ action.proposedChanges?.findings?.length !== 0 ) { await ctx.db.patch(action._id, { status: "pending_approval" }); + await settleStatus(ctx, action._id); return false; } const user = await ctx.db.get(repo.userId); @@ -1226,13 +1230,13 @@ export const expire = internalMutation({ }, handler: async (ctx, args) => { const action = await ctx.db.get(args.actionId); - if ( + const expired = action?.actionType === "review" && (args.claimedAt === undefined ? action.status === "pending" && isBatchCursor(action.reviewBatch, args) : action.status === "approved" && - action.executionClaimedAt === args.claimedAt) - ) + action.executionClaimedAt === args.claimedAt); + if (action && expired) await ctx.db.patch(action._id, { status: "failed", completedAt: Date.now(), @@ -1248,7 +1252,8 @@ export const expire = internalMutation({ : "GitHub publication outcome requires inspection; automatic retries are disabled", }); if (action) { - await settleStatus(ctx, action._id); + // Only an expiry that ended the review changes what the comment says. + if (expired) await settleStatus(ctx, action._id); await observeDroppedPush(ctx, action.ticketId); } }, @@ -1380,6 +1385,26 @@ export const recordCommand = internalMutation({ }, }); +/** + * Allow one new status comment a day for a pull request's automatic + * reviews, so a pull request that keeps failing is not flooded. + */ +export const claimAutomaticNotice = internalMutation({ + args: { ticketId: v.id("tickets") }, + handler: async (ctx, args) => { + const ticket = await ctx.db.get(args.ticketId); + const now = Date.now(); + if ( + !ticket || + (ticket.pullRequestLimitNoticeAt !== undefined && + ticket.pullRequestLimitNoticeAt > now - COMMAND_PAUSE_MS) + ) + return false; + await ctx.db.patch(ticket._id, { pullRequestLimitNoticeAt: now }); + return true; + }, +}); + /** Remember the status comment posted for a review, once. */ export const recordStatusComment = internalMutation({ args: { actionId: v.id("ticketActions"), commentId: v.number() }, @@ -1387,6 +1412,8 @@ export const recordStatusComment = internalMutation({ const action = await ctx.db.get(args.actionId); if (!action || action.statusCommentId !== undefined) return false; await ctx.db.patch(action._id, { statusCommentId: args.commentId }); + // The review may have ended while its comment was being posted. + if (action.status !== "pending") await settleStatus(ctx, action._id); return true; }, }); diff --git a/convex/schema.ts b/convex/schema.ts index 3c3bf5f..d76d062 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -849,8 +849,8 @@ const applicationTables = { pullRequestDroppedAt: v.optional(v.number()), // The commit Gitdog last approved; a push past it withdraws the approval. pullRequestApprovedSha: v.optional(v.string()), - // When Gitdog last told this PR its review limit was reached, so the - // notice is posted at most once a day. + // When Gitdog last posted a status comment for a failed automatic + // review, so a pull request that keeps failing hears it once a day. pullRequestLimitNoticeAt: v.optional(v.number()), // GitHub reference diff --git a/convex/ticketActions/internalMutation.ts b/convex/ticketActions/internalMutation.ts index 11100ba..93b2377 100644 --- a/convex/ticketActions/internalMutation.ts +++ b/convex/ticketActions/internalMutation.ts @@ -1424,6 +1424,13 @@ export const rejectPendingApproval = internalMutation({ } await ctx.db.patch(args.actionId, { status: "rejected" }); + // A rejected pull request review says so in its status comment. + if (action.actionType === "review" && action.statusCommentId !== undefined) + await ctx.scheduler.runAfter( + 0, + internal.pullRequests.internalAction.settleStatusComment, + { actionId: args.actionId }, + ); }, }); diff --git a/docs/github-app-setup.md b/docs/github-app-setup.md index 55dcd69..92e5cd5 100644 --- a/docs/github-app-setup.md +++ b/docs/github-app-setup.md @@ -84,12 +84,11 @@ review. Later reviews run when the author or a collaborator comments `@ review` (changes since the last review) or `@ full review`; `@ resolve` resolves Gitdog's threads and `@ help` lists the commands; `gitdog reviews request|resolve` runs the same from the CLI. -A command is a comment line that starts with the mention. The pull request +A command is a comment line that starts with the mention, or follows another review bot's command on the same line; `@gitdog` works as shorthand in production. The pull request author can run every command except a full review, which needs a collaborator. These arrive through the **Issue comments** event. A commenter who sends more than 10 commands an hour or 40 a day to one -repository is paused there for 24 hours. When the account runs out of tokens, -Gitdog says so on the pull request at most once a day. +repository is paused there for 24 hours. When a review cannot finish, for example because the account ran out of tokens, its status comment says why; for automatic reviews Gitdog posts at most one such comment a day per pull request. ### 6. Installation Settings diff --git a/public/llms-full.txt b/public/llms-full.txt index c542538..df9d226 100644 --- a/public/llms-full.txt +++ b/public/llms-full.txt @@ -54,7 +54,7 @@ A command is a comment line that starts with the mention, or follows another rev In issues and discussions, mention `@gitdogbot` with a question to get a reply grounded in the repository. - A review that finds no issues in fully reviewed changes approves the pull request; a later push, including updating the branch, withdraws the approval until `@gitdogbot review` reviews the new commits. -- When the account runs out of tokens, Gitdog says so on the pull request at most once a day. +- When a review cannot finish, for example because the account ran out of tokens, its status comment says why; for automatic reviews Gitdog posts at most one such comment a day per pull request. - Someone who sends more than 10 commands an hour or 40 a day to one repository is paused there for 24 hours. ### Gitdog CLI diff --git a/public/llms.txt b/public/llms.txt index 3cd4337..f73b202 100644 --- a/public/llms.txt +++ b/public/llms.txt @@ -30,7 +30,7 @@ A command is a comment line that starts with the mention, or follows another rev In issues and discussions, mention `@gitdogbot` with a question to get a reply grounded in the repository. - A review that finds no issues in fully reviewed changes approves the pull request; a later push, including updating the branch, withdraws the approval until `@gitdogbot review` reviews the new commits. -- When the account runs out of tokens, Gitdog says so on the pull request at most once a day. +- When a review cannot finish, for example because the account ran out of tokens, its status comment says why; for automatic reviews Gitdog posts at most one such comment a day per pull request. - Someone who sends more than 10 commands an hour or 40 a day to one repository is paused there for 24 hours. ## Pricing diff --git a/test/convex/pullRequests/commands.test.ts b/test/convex/pullRequests/commands.test.ts index 1441a6c..9dbb42b 100644 --- a/test/convex/pullRequests/commands.test.ts +++ b/test/convex/pullRequests/commands.test.ts @@ -27,6 +27,11 @@ describe("pull request commands", () => { ["@coderabbitai full review @gitdog full review", "full_review"], ["@gitdogbot review @coderabbitai review", "review"], ["@alice I think @gitdogbot review is broken", null], + ["@mai I think @gitdogbot review is broken", null], + ["@kai please @gitdogbot review", null], + ["@coderabbitai and @gitdogbot, thanks for the reviews", null], + ["@coderabbitai @gitdogbot thanks", null], + ["@renovate[bot] rebase @gitdogbot review", "review"], ["@gitdoghouse review", null], ["no mention here", null], ["@gitdogbot-dev review", null], @@ -37,6 +42,8 @@ describe("pull request commands", () => { expect(parseReviewCommand(body, "gitdogbot")).toBe(expected); }); it("uses the configured app slug", () => { + // The shorthand belongs to the production app only. + expect(parseReviewCommand("@gitdog review", "gitdogbot-dev")).toBeNull(); expect(parseReviewCommand("@gitdogbot-dev review", "gitdogbot-dev")).toBe( "review", ); diff --git a/test/convex/pullRequests/lifecycle.test.ts b/test/convex/pullRequests/lifecycle.test.ts index cbf8966..7352589 100644 --- a/test/convex/pullRequests/lifecycle.test.ts +++ b/test/convex/pullRequests/lifecycle.test.ts @@ -511,6 +511,129 @@ describe("review commands", () => { "**Reviewed**", ); }); + it("posts a failure of an automatic review at most once a day", async () => { + const f = await fixture(); + await withAccess(f); + const fail = async (headSha: string) => { + const id = (await f.observe({ ...source, headSha }))!; + await f.t.run((ctx) => + ctx.db.patch(id, { + status: "failed", + errorMessage: "Review exceeded its processing deadline", + }), + ); + await settle(f, id); + }; + await fail("c".repeat(40)); + await fail("d".repeat(40)); + expect(f.statusComments.add).toHaveBeenCalledTimes(1); + vi.advanceTimersByTime(24 * 60 * 60_000 + 1); + await fail("e".repeat(40)); + expect(f.statusComments.add).toHaveBeenCalledTimes(2); + }); + it.each([ + [ + "a review that changed under it as cancelled", + "Pull request changed before review", + "**Review cancelled**", + "try again", + ], + [ + "an oversized pull request without a retry hint", + "Review covers at most 300 changed files; split this PR for complete coverage", + "too large to review completely. Split it", + "try again", + ], + ])("words %s", async (_case, errorMessage, expected, absent) => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage, + statusCommentId: 7, + }), + ); + await settle(f, actionId); + const body = f.statusComments.update.mock.calls[0][3]; + expect(body).toContain(expected); + expect(body).not.toContain(absent); + }); + it("says when the owner rejects a review", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "pending_approval", + statusCommentId: 7, + }), + ); + await f.t.mutation( + internal.ticketActions.internalMutation.rejectPendingApproval, + { actionId }, + ); + await drainScheduledFunctions(f.t); + expect(f.statusComments.update.mock.calls.at(-1)?.[3]).toContain( + "**Review not posted**", + ); + }); + it("leaves a running review's comment alone", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => ctx.db.patch(actionId, { statusCommentId: 7 })); + await settle(f, actionId); + expect(f.statusComments.update).not.toHaveBeenCalled(); + }); + it("settles a review that ended before its comment was recorded", async () => { + const f = await fixture(); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage: + "Pull request changed or closed; this review is superseded", + }), + ); + await f.t.mutation( + internal.pullRequests.internalMutation.recordStatusComment, + { actionId, commentId: 9 }, + ); + const scheduled = await f.t.run((ctx) => + ctx.db.system.query("_scheduled_functions").collect(), + ); + expect( + scheduled.some((job) => job.name.includes("settleStatusComment")), + ).toBe(true); + }); + it("does not settle when an expiry finds the review already moved on", async () => { + const f = await fixture(); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "completed", + executionResult: { published: true }, + }), + ); + const before = ( + await f.t.run((ctx) => + ctx.db.system.query("_scheduled_functions").collect(), + ) + ).length; + await f.t.mutation(internal.pullRequests.internalMutation.expire, { + actionId, + }); + const after = await f.t.run((ctx) => + ctx.db.system.query("_scheduled_functions").collect(), + ); + expect( + after + .slice(before) + .some((job) => job.name.includes("settleStatusComment")), + ).toBe(false); + }); it("posts nothing for a recheck or a cancelled review without a comment", async () => { const f = await fixture(); await withAccess(f); From a3cc0f6f8f927330d2d4ba63f739837972fc8724 Mon Sep 17 00:00:00 2001 From: Hyo Date: Wed, 18 Nov 2026 02:08:23 +0900 Subject: [PATCH 3/4] fix: record cancelled reviews explicitly and throttle failed automatic reviews Stored failure messages are rewritten for display, so matching them never detected a cancelled review. Generation and publication now record reviewCancelled when the pull request moved on, and a cancelled request still offers a retry. An automatic failure that edits its existing comment starts the daily quiet window too, settle decides before fetching a GitHub token, a settle-posted comment no longer triggers a second settle, and a first review with nothing reviewable says so instead of reporting no new changes. Chained full requires review, the help text offers @gitdog only in production, and Cursor and Codium are known review bots. --- convex/pullRequests/commands.ts | 41 ++++++---- convex/pullRequests/errors.ts | 16 ++++ convex/pullRequests/internalAction.ts | 95 +++++++++++++--------- convex/pullRequests/internalMutation.ts | 23 +++++- convex/schema.ts | 2 + test/convex/pullRequests/commands.test.ts | 3 + test/convex/pullRequests/lifecycle.test.ts | 57 ++++++++++++- 7 files changed, 181 insertions(+), 56 deletions(-) diff --git a/convex/pullRequests/commands.ts b/convex/pullRequests/commands.ts index d255289..cd1dd1c 100644 --- a/convex/pullRequests/commands.ts +++ b/convex/pullRequests/commands.ts @@ -13,6 +13,8 @@ const REVIEW_BOTS = [ "greptileai", "codeant-ai", "qodo-merge", + "codiumai-agent", + "cursor", "[\\w-]+\\[bot\\]", ].join("|"); @@ -76,7 +78,10 @@ export function parseReviewCommand( switch (true) { // A chained mention without a command is a thank-you, not a request. case chained && - !["help", "review", "full", "resolve"].includes(words[0] ?? ""): + !( + ["help", "review", "resolve"].includes(words[0] ?? "") || + (words[0] === "full" && words[1] === "review") + ): return null; case words.length === 0 || words[0] === "help": return "help"; @@ -100,13 +105,16 @@ export function helpText(slug: string): string { `- \`${mention} full review\`: review the whole pull request again`, `- \`${mention} resolve\`: resolve every Gitdog review thread`, `- \`${mention} help\`: show this list`, - "Collaborators can run every command; the pull request author can run all but a full review. `@gitdog` works as shorthand.", + "Collaborators can run every command; the pull request author can run all but a full review.", + ...(slug === "gitdogbot" ? ["`@gitdog` works as shorthand."] : []), ].join("\n"); } type StatusAction = { status: string; errorMessage?: string; + reviewCancelled?: boolean; + reviewRequested?: boolean; executionResult?: { published?: boolean; reason?: string; @@ -119,12 +127,14 @@ type StatusAction = { }; }; -// A review cancelled because the pull request moved on, not one to retry. -export function isCancelledReview(message: string | undefined): boolean { +// A review cancelled because the pull request moved on, not a failure. +export function isCancelledReview(action: { + errorMessage?: string; + reviewCancelled?: boolean; +}): boolean { return ( - message !== undefined && - (message.includes("superseded") || - /^Pull request changed (before|during|after)/.test(message)) + action.reviewCancelled === true || + action.errorMessage?.includes("superseded") === true ); } @@ -193,15 +203,18 @@ export function reviewStatusBody( : "" }`; if (action.executionResult?.reason === "no_new_changes") - return `**No new changes to review** in ${sha}${ - action.proposedChanges?.reviewedSince - ? ` since ${action.proposedChanges.reviewedSince}` - : "" - }\n\nNothing in the pull request diff changed, so no review was posted. Comment \`@${slug} full review\` to review the whole pull request again.`; + return action.proposedChanges?.reviewedSince + ? `**No new changes to review** in ${sha} since ${action.proposedChanges.reviewedSince}\n\nNothing in the pull request diff changed, so no review was posted. Comment \`@${slug} full review\` to review the whole pull request again.` + : `**Nothing to review** in ${sha}\n\nThe pull request has no changes Gitdog can review, such as only deleted or binary files.`; return null; case "failed": { - if (isCancelledReview(action.errorMessage)) - return `**Review cancelled** for ${sha}\n\nThe pull request changed before the review finished.`; + if (isCancelledReview(action)) + return `**Review cancelled** for ${sha}\n\nThe pull request changed or was closed before the review finished.${ + // A request is answered either way; an automatic review is not. + action.reviewRequested + ? ` Comment \`@${slug} review\` to review the latest commit.` + : "" + }`; const [reason, retry] = failureReason(action.errorMessage); return `**Review not completed** for ${sha}\n\n${reason}${ retry ? ` Comment \`@${slug} review\` to try again.` : "" diff --git a/convex/pullRequests/errors.ts b/convex/pullRequests/errors.ts index 6498ea9..cc19ec3 100644 --- a/convex/pullRequests/errors.ts +++ b/convex/pullRequests/errors.ts @@ -33,6 +33,22 @@ export function reviewFailureCause(error: unknown): unknown { return error instanceof ReviewRequestError ? error.cause : error; } +// Thrown when the pull request or its review moved on mid-run: the review +// was cancelled, not failed. +const LIFECYCLE_CHANGES = new Set([ + "Review source, settings or lifecycle changed", + "Pull request changed before review", + "Pull request changed while reading its diff", + "Pull request changed during review", + "Pull request changed after the review was generated", + "Pull request changed before publication", +]); + +export function isLifecycleChange(error: unknown): boolean { + const cause = reviewFailureCause(error); + return cause instanceof Error && LIFECYCLE_CHANGES.has(cause.message); +} + export class ReviewResponseFormatError extends Error { constructor() { super("Review response did not match the required JSON format"); diff --git a/convex/pullRequests/internalAction.ts b/convex/pullRequests/internalAction.ts index b5fb0b9..4eca551 100644 --- a/convex/pullRequests/internalAction.ts +++ b/convex/pullRequests/internalAction.ts @@ -37,6 +37,7 @@ import { reviewWithContext } from "./workflow"; import { REVIEW_QUOTA_FAILURES } from "./internalMutation"; import { isReviewGroupModelFailure, + isLifecycleChange, isReviewRefusal, ReviewQuotaError, reviewGenerationError, @@ -634,6 +635,7 @@ export const generate = internalAction({ await ctx.runMutation(internal.pullRequests.internalMutation.finish, { actionId: args.actionId, stage: "generation", + cancelled: isLifecycleChange(error), page: args.page, group: args.group, error: @@ -1118,6 +1120,8 @@ export const publish = internalAction({ await ctx.runMutation(internal.pullRequests.internalMutation.finish, { actionId: args.actionId, stage: claimed ? "publication" : "preflight", + // A pull request that moved on before the post was cancelled. + cancelled: !claimed && isLifecycleChange(error), claimedAt: claimed || undefined, error: !claimed ? "Review is stale or GitHub access is unavailable; no review was published" @@ -1210,7 +1214,7 @@ async function postReviewingStatus( ); await ctx.runMutation( internal.pullRequests.internalMutation.recordStatusComment, - { actionId: state.action._id, commentId }, + { actionId: state.action._id, commentId, reviewing: true }, ); } catch (error) { console.warn( @@ -1404,17 +1408,38 @@ export const settleStatusComment = internalAction({ args: { actionId: v.id("ticketActions") }, handler: async (ctx, args): Promise => { const slug = getConfiguredGitHubAppSlug(); + if (!slug) return; const read = () => ctx.runQuery(internal.pullRequests.internalQuery.get, args); + // Whether this state changes the pull request at all, decided before + // any GitHub token is fetched. + const plan = (state: NonNullable>>) => { + const { action } = state; + if ( + action.reviewVerificationOnly === true || + action.status === "pending" || + action.status === "approved" + ) + return null; + const body = reviewStatusBody(action, slug); + if (!body) return null; + if (action.statusCommentId !== undefined) + return { body, commentId: action.statusCommentId }; + // Only something a reader is waiting on gets a new comment: a failure + // worth retrying or a requested no-change result, while reviews stay on. + const wanted = + action.status === "failed" + ? !isCancelledReview(action) + : action.executionResult?.reason === "no_new_changes" && + action.reviewRequested === true; + return wanted && + state.ticket.status === "open" && + state.repository.settings.pullRequestReviewsEnabled === true + ? { body, commentId: undefined } + : null; + }; const first = await read(); - if ( - !first || - !slug || - first.action.reviewVerificationOnly === true || - first.action.status === "pending" || - first.action.status === "approved" - ) - return; + if (!first || !plan(first)) return; const access = await resolveVerifiedRepositoryAccess( ctx, first.repository._id, @@ -1422,49 +1447,43 @@ export const settleStatusComment = internalAction({ // Re-read right before writing, so a late settle never overwrites a // newer outcome with an older one. const state = await read(); - if ( - !state || - state.action.status === "pending" || - state.action.status === "approved" - ) - return; - const body = reviewStatusBody(state.action, slug); - if (!body) return; - const existing = state.action.statusCommentId; - if (existing !== undefined) { + const next = state && plan(state); + if (!state || !next) return; + const { action } = state; + // An automatic review that failed counts toward the daily allowance + // whether it edits its comment or needs a new one. + const automaticFailure = + action.reviewRequested !== true && + action.status === "failed" && + !isCancelledReview(action); + if (next.commentId !== undefined) { await updateIssueComment( access.owner, access.repo, - existing, - body, + next.commentId, + next.body, access.token, ); + if (automaticFailure) + await ctx.runMutation( + internal.pullRequests.internalMutation.stampAutomaticNotice, + { ticketId: state.ticket._id }, + ); return; } - // Only something a reader is waiting on gets a new comment: a failure - // worth retrying or a no-change follow-up, while reviews stay on. An - // automatic review says so at most once a day per pull request. - const failed = state.action.status === "failed"; if ( - (failed - ? isCancelledReview(state.action.errorMessage) - : // Only a requested review was promised an answer. - state.action.executionResult?.reason !== "no_new_changes" || - state.action.reviewRequested !== true) || - state.ticket.status !== "open" || - state.repository.settings.pullRequestReviewsEnabled !== true || - (state.action.reviewRequested !== true && - !(await ctx.runMutation( - internal.pullRequests.internalMutation.claimAutomaticNotice, - { ticketId: state.ticket._id }, - ))) + automaticFailure && + !(await ctx.runMutation( + internal.pullRequests.internalMutation.claimAutomaticNotice, + { ticketId: state.ticket._id }, + )) ) return; const commentId = await addIssueComment( access.owner, access.repo, state.ticket.githubNumber, - body, + next.body, access.token, ); await ctx.runMutation( diff --git a/convex/pullRequests/internalMutation.ts b/convex/pullRequests/internalMutation.ts index b08d056..8233f44 100644 --- a/convex/pullRequests/internalMutation.ts +++ b/convex/pullRequests/internalMutation.ts @@ -1175,6 +1175,8 @@ export const finish = internalMutation({ inlineFindings: v.optional(v.boolean()), // Earlier Gitdog approvals GitHub refused to dismiss. keptApprovals: v.optional(v.array(v.number())), + // The pull request moved on mid-review; the review was cancelled. + cancelled: v.optional(v.boolean()), }, handler: async (ctx, args) => { const action = await ctx.db.get(args.actionId); @@ -1199,6 +1201,7 @@ export const finish = internalMutation({ return; await ctx.db.patch(action._id, { status: args.error ? "failed" : "completed", + ...(args.error && args.cancelled ? { reviewCancelled: true } : {}), ...ENDED_BATCH, errorMessage: args.error, reviewFailureDetails: args.error ? args.reviewFailureDetails : undefined, @@ -1405,15 +1408,31 @@ export const claimAutomaticNotice = internalMutation({ }, }); +/** Start the daily quiet window after an automatic review failed. */ +export const stampAutomaticNotice = internalMutation({ + args: { ticketId: v.id("tickets") }, + handler: async (ctx, args) => { + const ticket = await ctx.db.get(args.ticketId); + if (ticket) + await ctx.db.patch(ticket._id, { pullRequestLimitNoticeAt: Date.now() }); + }, +}); + /** Remember the status comment posted for a review, once. */ export const recordStatusComment = internalMutation({ - args: { actionId: v.id("ticketActions"), commentId: v.number() }, + args: { + actionId: v.id("ticketActions"), + commentId: v.number(), + // A "Reviewing" comment, which a review that has since ended must update. + reviewing: v.optional(v.boolean()), + }, handler: async (ctx, args) => { const action = await ctx.db.get(args.actionId); if (!action || action.statusCommentId !== undefined) return false; await ctx.db.patch(action._id, { statusCommentId: args.commentId }); // The review may have ended while its comment was being posted. - if (action.status !== "pending") await settleStatus(ctx, action._id); + if (args.reviewing && action.status !== "pending") + await settleStatus(ctx, action._id); return true; }, }); diff --git a/convex/schema.ts b/convex/schema.ts index d76d062..0d1e26f 100644 --- a/convex/schema.ts +++ b/convex/schema.ts @@ -963,6 +963,8 @@ const applicationTables = { reviewRequested: v.optional(v.boolean()), // The pull request comment that shows this review's progress and outcome. statusCommentId: v.optional(v.number()), + // The pull request moved on mid-review, so the review was cancelled. + reviewCancelled: v.optional(v.boolean()), reviewRecheckOfActionId: v.optional(v.id("ticketActions")), // The last finished review of this PR: a later push reviews only the // files changed since its head. diff --git a/test/convex/pullRequests/commands.test.ts b/test/convex/pullRequests/commands.test.ts index 9dbb42b..1ce43e3 100644 --- a/test/convex/pullRequests/commands.test.ts +++ b/test/convex/pullRequests/commands.test.ts @@ -32,6 +32,9 @@ describe("pull request commands", () => { ["@coderabbitai and @gitdogbot, thanks for the reviews", null], ["@coderabbitai @gitdogbot thanks", null], ["@renovate[bot] rebase @gitdogbot review", "review"], + ["@coderabbitai review @gitdogbot full", null], + ["@coderabbitai @gitdogbot full stop", null], + ["@cursor review @gitdogbot review", "review"], ["@gitdoghouse review", null], ["no mention here", null], ["@gitdogbot-dev review", null], diff --git a/test/convex/pullRequests/lifecycle.test.ts b/test/convex/pullRequests/lifecycle.test.ts index 7352589..942f9d2 100644 --- a/test/convex/pullRequests/lifecycle.test.ts +++ b/test/convex/pullRequests/lifecycle.test.ts @@ -534,7 +534,7 @@ describe("review commands", () => { it.each([ [ "a review that changed under it as cancelled", - "Pull request changed before review", + "Source preflight: The pull request changed before verification finished.", "**Review cancelled**", "try again", ], @@ -553,6 +553,7 @@ describe("review commands", () => { status: "failed", errorMessage, statusCommentId: 7, + ...(expected.includes("cancelled") ? { reviewCancelled: true } : {}), }), ); await settle(f, actionId); @@ -579,6 +580,39 @@ describe("review commands", () => { "**Review not posted**", ); }); + it("records a review the pull request moved past as cancelled", async () => { + const f = await fixture(); + const actionId = (await f.observe())!; + await f.t.mutation(internal.pullRequests.internalMutation.finish, { + actionId, + stage: "generation", + error: "Source preflight: changed", + cancelled: true, + }); + expect(await f.t.run((ctx) => ctx.db.get(actionId))).toMatchObject({ + status: "failed", + reviewCancelled: true, + }); + }); + it("counts an edited automatic failure toward the daily allowance", async () => { + const f = await fixture(); + await withAccess(f); + const actionId = (await f.observe())!; + await f.t.run((ctx) => + ctx.db.patch(actionId, { + status: "failed", + errorMessage: + "Not enough tokens remain for this pull request review. No review was published.", + statusCommentId: 7, + }), + ); + await settle(f, actionId); + expect(f.statusComments.update).toHaveBeenCalledTimes(1); + expect( + (await f.t.run((ctx) => ctx.db.query("tickets").first())) + ?.pullRequestLimitNoticeAt, + ).toBeTypeOf("number"); + }); it("leaves a running review's comment alone", async () => { const f = await fixture(); await withAccess(f); @@ -599,7 +633,7 @@ describe("review commands", () => { ); await f.t.mutation( internal.pullRequests.internalMutation.recordStatusComment, - { actionId, commentId: 9 }, + { actionId, commentId: 9, reviewing: true }, ); const scheduled = await f.t.run((ctx) => ctx.db.system.query("_scheduled_functions").collect(), @@ -2857,6 +2891,25 @@ describe("batched PR reviews", () => { }; } + it.each([ + [false, 0], + [true, 1], + ])( + "stays quiet within a day of an automatic failure unless requested (requested %s)", + async (requested, posts) => { + const f = await batchFixture(); + await f.t.run(async (ctx) => { + const action = (await ctx.db.get(f.actionId))!; + await ctx.db.patch(action.ticketId, { + pullRequestLimitNoticeAt: Date.now(), + }); + if (requested) + await ctx.db.patch(f.actionId, { reviewRequested: true }); + }); + await f.generate(); + expect(f.statusComments.add).toHaveBeenCalledTimes(posts); + }, + ); it("shows a reviewing comment once, when the review starts", async () => { const f = await batchFixture(); await f.generate(); From e9964c6b5333328205ff3b1c3f3797d896b475b3 Mon Sep 17 00:00:00 2001 From: Hyo Date: Wed, 18 Nov 2026 02:30:30 +0900 Subject: [PATCH 4/4] fix: record a pull request closed after the claim as a cancelled review Nothing is posted until the review request goes out, so a lifecycle change after the claim is a cancellation, not a GitHub failure. Also drop binary files from the nothing-to-review wording, since a binary-only pull request never reaches it. --- convex/pullRequests/commands.ts | 2 +- convex/pullRequests/internalAction.ts | 5 +++-- test/convex/pullRequests/lifecycle.test.ts | 26 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/convex/pullRequests/commands.ts b/convex/pullRequests/commands.ts index cd1dd1c..f987c28 100644 --- a/convex/pullRequests/commands.ts +++ b/convex/pullRequests/commands.ts @@ -205,7 +205,7 @@ export function reviewStatusBody( if (action.executionResult?.reason === "no_new_changes") return action.proposedChanges?.reviewedSince ? `**No new changes to review** in ${sha} since ${action.proposedChanges.reviewedSince}\n\nNothing in the pull request diff changed, so no review was posted. Comment \`@${slug} full review\` to review the whole pull request again.` - : `**Nothing to review** in ${sha}\n\nThe pull request has no changes Gitdog can review, such as only deleted or binary files.`; + : `**Nothing to review** in ${sha}\n\nThe pull request has no changes Gitdog can review, such as only deleted files.`; return null; case "failed": { if (isCancelledReview(action)) diff --git a/convex/pullRequests/internalAction.ts b/convex/pullRequests/internalAction.ts index 4eca551..694559f 100644 --- a/convex/pullRequests/internalAction.ts +++ b/convex/pullRequests/internalAction.ts @@ -1120,8 +1120,9 @@ export const publish = internalAction({ await ctx.runMutation(internal.pullRequests.internalMutation.finish, { actionId: args.actionId, stage: claimed ? "publication" : "preflight", - // A pull request that moved on before the post was cancelled. - cancelled: !claimed && isLifecycleChange(error), + // A pull request that moved on before anything could be posted was + // cancelled. + cancelled: !mayHavePosted && isLifecycleChange(error), claimedAt: claimed || undefined, error: !claimed ? "Review is stale or GitHub access is unavailable; no review was published" diff --git a/test/convex/pullRequests/lifecycle.test.ts b/test/convex/pullRequests/lifecycle.test.ts index 942f9d2..b97b92d 100644 --- a/test/convex/pullRequests/lifecycle.test.ts +++ b/test/convex/pullRequests/lifecycle.test.ts @@ -2096,6 +2096,32 @@ describe("approval", () => { "GitHub did not accept the review; no review was published", ); }); + it("records a pull request closed after the claim as cancelled", async () => { + const f = await publicationFixture(); + await github([]); + const live = await f.live.getMockImplementation()!( + {} as never, + 12 as never, + ); + // The read before the claim still sees the reviewed head. + f.live.mockResolvedValueOnce(live as never).mockResolvedValue({ + ...live, + state: "closed", + } as never); + f.fetch.mockClear(); + await f.t.action(internal.pullRequests.internalAction.publish, { + actionId: f.actionId, + manual: true, + }); + expect(f.fetch).not.toHaveBeenCalled(); + expect( + (await f.t.run((ctx) => ctx.db.get(f.actionId)))?.executionClaimedAt, + ).toBeDefined(); + expect(await f.t.run((ctx) => ctx.db.get(f.actionId))).toMatchObject({ + status: "failed", + reviewCancelled: true, + }); + }); it("records an earlier approval GitHub refused to dismiss", async () => { const f = await publicationFixture(); await github([], { dismissFails: true });