diff --git a/.github/workflows/label-pr-review-state-fork-review-followup.yml b/.github/workflows/label-pr-review-state-fork-review-followup.yml index 3bcd55dc21..e3f7235ddb 100644 --- a/.github/workflows/label-pr-review-state-fork-review-followup.yml +++ b/.github/workflows/label-pr-review-state-fork-review-followup.yml @@ -20,10 +20,44 @@ permissions: actions: write pull-requests: read -# Each completed run gets its own group: one run can resolve several PRs, and -# a shared key would let an unrelated completion supersede queued dispatches. +# Coalesce by target, not by triggering run (2026-11-18 storm: this workflow +# started 243 runs in 78 minutes and dispatched 73 of them). Keying per +# triggering run id gave every completion its own group, so nothing ever +# coalesced: the six required checks of one PR all completing at the same head +# meant six queued dispatches of the same work. GitHub keeps at most one +# running plus one pending run per group, so keying the group by the target the +# completed run evaluated collapses a burst into a single dispatch of the +# freshest state: first by the PR number GitHub associates with the run, then, +# for fork runs whose pull_requests GitHub leaves empty (issue #2133), by the +# branch identity the resolver below actually matches on. +# +# The fork fallback must include the branch, not just the head SHA. Two fork PRs +# can carry the same commit on different branches (a cherry-picked stack does +# exactly that), and the resolver distinguishes them only by head branch: a +# SHA-only key would put both PRs in one group, so a pending run for the second +# branch would be superseded by a later completion for the first and its +# reconciliation would never dispatch. head_branch, together with the head +# repository owner it belongs to, is the resolver's branch identity, so two +# branches never share a group. Two distinct branches carrying the same commit +# do not coalesce, which is the intended correction: they are different PR +# targets. Repeated completions for one target share branch and SHA and still +# coalesce. +# +# cancel-in-progress stays false: a running dispatch must finish every PR it +# resolved, so only pending duplicates are dropped. The run id is the last +# fallback so a payload with neither an associated PR nor a head branch and SHA +# keeps a unique group instead of every such run colliding on one key. concurrency: - group: label-pr-review-state-fork-review-followup-${{ github.event.workflow_run.id }} + group: >- + label-pr-review-state-fork-review-followup-${{ + github.event.workflow_run.pull_requests[0].number || + ((github.event.workflow_run.head_branch && github.event.workflow_run.head_sha) && + format('{0}:{1}@{2}', + github.event.workflow_run.head_repository.owner.login || 'unknown', + github.event.workflow_run.head_branch, + github.event.workflow_run.head_sha)) || + github.event.workflow_run.id + }} cancel-in-progress: false jobs: diff --git a/.github/workflows/label-pr-review-state.yml b/.github/workflows/label-pr-review-state.yml index 459dcf50bd..1d8adbd220 100644 --- a/.github/workflows/label-pr-review-state.yml +++ b/.github/workflows/label-pr-review-state.yml @@ -41,8 +41,24 @@ on: # writes (issue #2046). The privileged follow-up for those runs lives in # label-pr-review-state-fork-review-followup.yml: GitHub rejects a workflow # that lists itself in workflow_run.workflows (issue #2086). + # + # A workflow belongs in this list only when its completion can change the + # answer for a required status check, because the reconcile below reads + # only the required checks from the branch ruleset and never inspects which + # workflow completed, its conclusion, or its outputs. "Code QA Roo Code" + # publishes check-translations, compile, knip and both platform-unit-test + # rows; "E2E Tests (Mocked)" publishes e2e-mock. Those six are the entire + # required set on main, so no completion of any other workflow can change a + # label this workflow writes. + # + # "Webview Visual Regression" was in this list but no workflow has that + # display name: the visual workflow is "Visual Regression" + # (visual-regression.yml), so the entry never matched anything and only + # looked like coverage. "CodeQL Advanced" does run, but it publishes none of + # the six required checks, so each of its completions started a full + # reconciliation that could only recompute the labels already written. workflow_run: - workflows: ["Code QA Roo Code", "E2E Tests (Mocked)", "Webview Visual Regression", "CodeQL Advanced"] + workflows: ["Code QA Roo Code", "E2E Tests (Mocked)"] types: [completed] permissions: @@ -94,9 +110,22 @@ concurrency: jobs: resolve: name: Zoo Code / resolve PRs to reconcile + # Loop guard (self-triggered label writes): the reconciliation writes the + # labels it also listens for, so every mutating run used to start one + # more run through the labeled/unlabeled pull_request_target events it + # caused itself. Reconciliation is idempotent, so a run woken only by + # this workflow's own label write would recompute the state it just + # wrote and write it again; skipping it breaks that loop. Only label + # events authored by github-actions[bot] for a label this workflow + # manages are skipped: a human edit to one of those labels, and any + # other bot or workflow label write, still reconciles. if: >- - github.event_name != 'issue_comment' || - (github.event.issue.pull_request && github.event.comment.user.login == 'coderabbitai[bot]') + (github.event_name != 'issue_comment' || + (github.event.issue.pull_request && github.event.comment.user.login == 'coderabbitai[bot]')) && + !(github.event_name == 'pull_request_target' && + contains(fromJSON('["labeled", "unlabeled"]'), github.event.action) && + github.event.sender.login == 'github-actions[bot]' && + contains(fromJSON('["awaiting-author", "awaiting-coderabbit", "awaiting-ready", "awaiting-maintainer", "awaiting-review", "coderabbit-review-active", "community-approved", "has-conflicts", "stale-inactive", "stale-awaiting-author"]'), github.event.label.name)) runs-on: ubuntu-latest outputs: pr_numbers: ${{ steps.resolve.outputs.pr_numbers }} diff --git a/src/services/__tests__/pr-review-state-workflow.test.ts b/src/services/__tests__/pr-review-state-workflow.test.ts index af8ad70f3d..920d0441b6 100644 --- a/src/services/__tests__/pr-review-state-workflow.test.ts +++ b/src/services/__tests__/pr-review-state-workflow.test.ts @@ -10,6 +10,12 @@ const repositoryRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)) const workflow = parse( fs.readFileSync(path.join(repositoryRoot, ".github/workflows/label-pr-review-state.yml"), "utf8"), ) +const forkReviewFollowupWorkflow = parse( + fs.readFileSync( + path.join(repositoryRoot, ".github/workflows/label-pr-review-state-fork-review-followup.yml"), + "utf8", + ), +) const codeRabbitConfig = parse(fs.readFileSync(path.join(repositoryRoot, ".coderabbit.yaml"), "utf8")) const workflowScript = workflow.jobs.reconcile.steps[0].with.script as string const resolveScript = workflow.jobs.resolve.steps[0].with.script as string @@ -3581,9 +3587,19 @@ describe("PR review-state workflow concurrency groups (#1707)", () => { head?: { repo?: { full_name?: string } | null } base?: { repo?: { full_name?: string } | null } } - issue?: { number: number } - workflow_run?: { id?: number; pull_requests?: Array<{ number: number }> } + issue?: { number: number; pull_request?: unknown } + workflow_run?: { + id?: number + pull_requests?: Array<{ number: number }> + head_branch?: string | null + head_sha?: string | null + head_repository?: { owner?: { login?: string } | null } | null + } inputs?: { pull_request_number: number } + action?: string + label?: { name?: string } | null + sender?: { login?: string } | null + comment?: { user?: { login?: string } | null } | null } } @@ -3633,6 +3649,57 @@ describe("PR review-state workflow concurrency groups (#1707)", () => { return left === right } + /** + * Splits a function call's argument source on top-level commas. Strings are + * skipped so a JSON array literal inside fromJSON stays one argument. + */ + function splitArguments(source: string): string[] { + const args: string[] = [] + let depth = 0 + let inString = false + let start = 0 + for (let index = 0; index < source.length; index++) { + const char = source[index] + if (char === "'") { + inString = !inString + } else if (!inString && char === "(") { + depth++ + } else if (!inString && char === ")") { + depth-- + } else if (!inString && depth === 0 && char === ",") { + args.push(source.slice(start, index)) + start = index + 1 + } + } + args.push(source.slice(start)) + return args.map((argument) => argument.trim()).filter((argument) => argument.length > 0) + } + + /** Evaluates the GitHub expression functions the job conditions use. */ + function evaluateFunctionCall(name: string, source: string, roots: ExpressionRoots): unknown { + const args = splitArguments(source).map((argument) => evaluateExpression(argument, roots)) + if (name === "contains") { + const [haystack, needle] = args + if (Array.isArray(haystack)) { + return haystack.some((item) => looseEquals(item, needle)) + } + if (typeof haystack === "string" && typeof needle === "string") { + return haystack.includes(needle) + } + return false + } + if (name === "fromJSON") { + return JSON.parse(String(args[0])) + } + if (name === "format") { + const [template, ...values] = args + return String(template).replace(/\{(\d+)\}/g, (_match, index: string) => + String(values[Number(index)] ?? ""), + ) + } + throw new Error(`Unsupported expression function: ${name}`) + } + function resolvePath(path: string, roots: ExpressionRoots): unknown { let value: unknown = roots for (const segment of path.split(".")) { @@ -3656,6 +3723,10 @@ describe("PR review-state workflow concurrency groups (#1707)", () => { function evaluatePrimary(expression: string, roots: ExpressionRoots): unknown { const current = expression.trim() + // GitHub's unary ! negates loose truthiness, exactly like the && and || chains. + if (current.startsWith("!")) { + return !isTruthy(evaluatePrimary(current.slice(1), roots)) + } if (current.startsWith("(")) { // Strip the parentheses only when the first one wraps the whole expression. let depth = 0 @@ -3682,6 +3753,10 @@ describe("PR review-state workflow concurrency groups (#1707)", () => { if (/^(github|matrix)\./.test(current)) { return resolvePath(current, roots) } + const functionCall = /^([A-Za-z_][A-Za-z0-9_]*)\s*\(([\s\S]*)\)$/.exec(current) + if (functionCall) { + return evaluateFunctionCall(functionCall[1], functionCall[2], roots) + } return evaluateExpression(current, roots) } @@ -4144,4 +4219,233 @@ describe("PR review-state workflow concurrency groups (#1707)", () => { "Create label failed", ) }) + + describe("resolve job loop guard on self-triggered label writes", () => { + // Mirrors the labels the reconciliation itself writes. The guard only refuses + // to wake on this workflow's own writes for these labels. + const MANAGED_LABELS = [ + "awaiting-author", + "awaiting-coderabbit", + "awaiting-ready", + "awaiting-maintainer", + "awaiting-review", + "coderabbit-review-active", + "community-approved", + "has-conflicts", + "stale-inactive", + "stale-awaiting-author", + ] + + // GitHub evaluates a job-level if as an expression, without the + // dollar-brace wrapper, so the template evaluator is applied to the raw + // condition. This is the public entry point the harness previously never + // exercised: the embedded scripts were run, the job predicate was not. + function resolveIfFor(github: GithubExpressionContext): boolean { + return isTruthy(evaluateExpression(String(workflow.jobs.resolve.if), { github })) + } + + function labelEvent(options: { action: string; sender: string; labelName?: string }): GithubExpressionContext { + return contextFor({ + event_name: "pull_request_target", + event: { + pull_request: { number: 1664 }, + action: options.action, + sender: { login: options.sender }, + ...(options.labelName === undefined ? {} : { label: { name: options.labelName } }), + }, + }) + } + + it("keeps the guard's actions and label list in sync with the workflow", () => { + // Drift guard: the two fromJSON lists in the condition are the guard's + // whole input vocabulary, so pin them explicitly. + const lists = [...String(workflow.jobs.resolve.if).matchAll(/fromJSON\('(\[[^']*\])'\)/g)].map((match) => + JSON.parse(match[1]), + ) + + expect(lists[0]).toEqual(["labeled", "unlabeled"]) + expect(lists[1]).toEqual(MANAGED_LABELS) + }) + + it("rejects labeled and unlabeled events the reconciliation wrote itself", () => { + for (const action of ["labeled", "unlabeled"]) { + for (const labelName of MANAGED_LABELS) { + expect(resolveIfFor(labelEvent({ action, labelName, sender: "github-actions[bot]" }))).toBe(false) + } + } + }) + + it("accepts those label events when a human or another bot wrote them", () => { + for (const sender of ["edelauna", "MarkSwanson", "coderabbitai[bot]", "codecov[bot]"]) { + for (const action of ["labeled", "unlabeled"]) { + expect(resolveIfFor(labelEvent({ action, labelName: "awaiting-author", sender }))).toBe(true) + } + } + }) + + it("accepts a github-actions label event for a label this workflow does not manage", () => { + for (const labelName of ["dependencies", "bug", "pinned", "do-not-close"]) { + expect(resolveIfFor(labelEvent({ action: "labeled", labelName, sender: "github-actions[bot]" }))).toBe( + true, + ) + } + }) + + it("accepts a labeled event whose payload carries no label name", () => { + // Boundary: an unclassifiable label event must not be rejected, or a + // payload change would silently stop reconciliation. + expect(resolveIfFor(labelEvent({ action: "labeled", sender: "github-actions[bot]" }))).toBe(true) + expect(resolveIfFor(labelEvent({ action: "unlabeled", sender: "github-actions[bot]" }))).toBe(true) + }) + + it("accepts pull_request_target events that carry no label at all", () => { + for (const action of [ + "opened", + "reopened", + "ready_for_review", + "converted_to_draft", + "synchronize", + "review_requested", + "review_request_removed", + ]) { + expect(resolveIfFor(labelEvent({ action, sender: "github-actions[bot]" }))).toBe(true) + } + }) + + it("still gates issue_comment on the CodeRabbit author", () => { + const issueComment = (login: string, onPullRequest = true): GithubExpressionContext => + contextFor({ + event_name: "issue_comment", + event: { + issue: { number: 1664, ...(onPullRequest ? { pull_request: {} } : {}) }, + comment: { user: { login } }, + }, + }) + + // GitHub's == is case-insensitive for strings. + expect(resolveIfFor(issueComment("coderabbitai[bot]"))).toBe(true) + expect(resolveIfFor(issueComment("CodeRabbitAI[bot]"))).toBe(true) + expect(resolveIfFor(issueComment("edelauna"))).toBe(false) + expect(resolveIfFor(issueComment("github-actions[bot]"))).toBe(false) + expect(resolveIfFor(issueComment("coderabbitai[bot]", false))).toBe(false) + }) + + it("accepts every other trigger unchanged", () => { + expect(resolveIfFor(contextFor({ event_name: "pull_request_review" }))).toBe(true) + expect( + resolveIfFor( + contextFor({ event_name: "workflow_run", event: { workflow_run: { id: 7, pull_requests: [] } } }), + ), + ).toBe(true) + expect(resolveIfFor(contextFor({ event_name: "schedule" }))).toBe(true) + expect(resolveIfFor(contextFor({ event_name: "push" }))).toBe(true) + expect( + resolveIfFor( + contextFor({ event_name: "workflow_dispatch", event: { inputs: { pull_request_number: 1664 } } }), + ), + ).toBe(true) + }) + }) + + describe("fork review follow-up concurrency key", () => { + // GitHub keeps one running plus one pending run per group, so the group key + // decides which completions coalesce and which may supersede each other. + function followupGroupFor(github: GithubExpressionContext): string { + return evaluateTemplate(String(forkReviewFollowupWorkflow.concurrency.group), { github }) + } + + function forkRun(options: { + branch?: string | null + sha?: string | null + owner?: string | null + associatedPrNumbers?: number[] + runId?: number + }): GithubExpressionContext { + return contextFor({ + event_name: "workflow_run", + run_id: 99000, + event: { + workflow_run: { + id: options.runId ?? 777000111, + pull_requests: (options.associatedPrNumbers ?? []).map((number) => ({ number })), + head_branch: options.branch ?? null, + head_sha: options.sha ?? null, + // owner: null models a payload with no head repository at all. + head_repository: + options.owner === null ? null : { owner: { login: options.owner ?? "Zoo-Code-Org" } }, + }, + }, + }) + } + + it("keeps cancel-in-progress disabled", () => { + expect(forkReviewFollowupWorkflow.concurrency["cancel-in-progress"]).toBe(false) + }) + + it("keys two branches carrying the same commit to different groups", () => { + // A cherry-picked stack puts one commit on two branches. A SHA-only key + // would put both fork PRs in one group, so a later completion for one + // branch would supersede the other branch's pending dispatch and that + // PR would never be reconciled. + const branchA = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA })) + const branchB = followupGroupFor(forkRun({ branch: "feature/b", sha: SHA })) + + expect(branchA).toContain("feature/a") + expect(branchB).toContain("feature/b") + expect(branchA).not.toBe(branchB) + }) + + it("includes the head owner in the fallback group", () => { + const upstream = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, owner: "Zoo-Code-Org" })) + const contributor = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, owner: "contributor" })) + + expect(upstream).toContain("Zoo-Code-Org:feature/a") + expect(contributor).toContain("contributor:feature/a") + expect(contributor).not.toBe(upstream) + }) + + it("uses the unknown owner fallback when the payload carries no head repository", () => { + // Review runs report the base repository as head_repository (issue #2133), so + // this covers the key expression's fallback rather than a production split. + const missingOwner = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, owner: null })) + const knownOwner = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, owner: "Zoo-Code-Org" })) + + expect(missingOwner).toContain("unknown:feature/a") + expect(missingOwner).not.toBe(knownOwner) + }) + + it("still coalesces repeated completions when the owner is unknown", () => { + const first = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, owner: null, runId: 11 })) + const second = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, owner: null, runId: 12 })) + + expect(second).toBe(first) + }) + + it("coalesces repeated completions for the same target onto one group", () => { + const first = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, runId: 1 })) + const second = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, runId: 2 })) + + expect(second).toBe(first) + // A push to the same branch is a new target: its pending dispatch must + // not be cancelled by an older head's queued run. + expect(followupGroupFor(forkRun({ branch: "feature/a", sha: OLD_SHA, runId: 3 }))).not.toBe(first) + }) + + it("prefers the associated PR number over the branch fallback", () => { + const withPr = followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, associatedPrNumbers: [1662] })) + expect(withPr).toMatch(/-1662$/) + expect(withPr).not.toContain("feature/a") + expect( + followupGroupFor(forkRun({ branch: "feature/a", sha: SHA, associatedPrNumbers: [1662], runId: 5 })), + ).toBe(withPr) + }) + + it("keeps a unique group when the payload has neither an associated PR nor a branch and SHA", () => { + const missing = followupGroupFor(forkRun({})) + expect(missing).toMatch(/-777000111$/) + expect(followupGroupFor(forkRun({ runId: 777000222 }))).not.toBe(missing) + // Half a pair is not enough: the fallback must not fabricate a target. + expect(followupGroupFor(forkRun({ branch: "feature/a", sha: null }))).toMatch(/-777000111$/) + }) + }) })