Skip to content

Commit d890af6

Browse files
committed
fix(ci): key label-pr-review-state concurrency per PR
The label-pr-review-state workflow fires on many repo-wide triggers (pull_request_target, pull_request_review, issue_comment, workflow_run, push to main, hourly schedule, workflow_dispatch) but shared a single concurrency group. GitHub keeps only the newest pending run per group even with cancel-in-progress: false, so bursts of unrelated triggers superseded queued per-PR reconciliation runs and they never executed (e.g. PR #1664's reconcile was queue-cancelled 18s after queueing, and a manual dispatch met the same fate), leaving review-state labels to lag up to an hour behind the hourly sweep. Key the concurrency group per PR (pull_request/issue/workflow_run/ dispatch input number), share one group for sweep triggers (schedule and pushes to main), and fall back to a unique per-run group for events that cannot be tied to a PR (fork workflow_run without backfilled pull_requests) so they are never superseded. cancel-in-progress remains false: within one PR, newest-pending-wins is freshest-state-wins. Adds deterministic vitest coverage that evaluates the group expression and proves per-PR group isolation, the shared sweep bucket, and the unique fallback. Fixes #1707 Co-authored-by: Roomote <[email protected]>
1 parent f6af57a commit d890af6

2 files changed

Lines changed: 250 additions & 1 deletion

File tree

‎.github/workflows/label-pr-review-state.yml‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,24 @@ permissions:
3434
checks: read
3535
statuses: write
3636

37+
# One concurrency group per PR so bursts of activity on unrelated PRs cannot
38+
# starve an individual PR's reconciliation (issue #1707). GitHub keeps only the
39+
# newest pending run per group; keyed per PR that becomes freshest-state-wins
40+
# within a PR. Issue/PR numbers share one GitHub sequence, so issue-comment runs
41+
# can never collide with a different PR's group. Sweep triggers (hourly
42+
# schedule, pushes to main) share a single group, and events that cannot be
43+
# tied to a PR (fork workflow_run without backfilled pull_requests) fall back
44+
# to a unique per-run group so they are never superseded.
3745
concurrency:
38-
group: label-pr-review-state
46+
group: >-
47+
label-pr-review-state-${{
48+
github.event.pull_request.number ||
49+
github.event.issue.number ||
50+
github.event.workflow_run.pull_requests[0].number ||
51+
github.event.inputs.pull_request_number ||
52+
((github.event_name == 'schedule' || github.event_name == 'push') && 'sweep') ||
53+
github.run_id
54+
}}
3955
cancel-in-progress: false
4056

4157
jobs:

‎src/services/__tests__/pr-review-state-workflow.test.ts‎

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2236,3 +2236,236 @@ describe("PR review-state workflow", () => {
22362236
})
22372237
})
22382238
})
2239+
2240+
describe("PR review-state workflow concurrency group (#1707)", () => {
2241+
// Minimal evaluator for the exact GitHub expression subset used by
2242+
// workflow.concurrency.group: top-level `||` chains, `&&`, loose `==`,
2243+
// `github.` property paths, `[0]` indexing (null parent -> null),
2244+
// parenthesized operands, and string/number literals. GitHub expressions
2245+
// use loose truthiness, so `||`/`&&` return operand values rather than
2246+
// booleans and missing properties evaluate to null instead of throwing.
2247+
interface GithubExpressionContext {
2248+
event_name: string
2249+
run_id: number
2250+
event: {
2251+
pull_request?: { number: number }
2252+
issue?: { number: number }
2253+
workflow_run?: { pull_requests: Array<{ number: number }> }
2254+
inputs?: { pull_request_number: number }
2255+
}
2256+
}
2257+
2258+
function splitTopLevel(expression: string, operator: "||" | "&&" | "=="): string[] {
2259+
const parts: string[] = []
2260+
let depth = 0
2261+
let inString = false
2262+
let start = 0
2263+
for (let index = 0; index < expression.length; index++) {
2264+
const char = expression[index]
2265+
if (char === "'") {
2266+
inString = !inString
2267+
} else if (!inString && char === "(") {
2268+
depth++
2269+
} else if (!inString && char === ")") {
2270+
depth--
2271+
} else if (!inString && depth === 0 && expression.startsWith(operator, index)) {
2272+
parts.push(expression.slice(start, index))
2273+
index += operator.length - 1
2274+
start = index + 1
2275+
}
2276+
}
2277+
parts.push(expression.slice(start))
2278+
return parts
2279+
}
2280+
2281+
function isTruthy(value: unknown): boolean {
2282+
return value !== null && value !== undefined && value !== false && value !== 0 && value !== ""
2283+
}
2284+
2285+
function looseEquals(left: unknown, right: unknown): boolean {
2286+
if (left === null || left === undefined || right === null || right === undefined) {
2287+
return (left ?? null) === (right ?? null)
2288+
}
2289+
if (typeof left === "string" && typeof right === "string") {
2290+
return left.toLowerCase() === right.toLowerCase()
2291+
}
2292+
return left === right
2293+
}
2294+
2295+
function resolvePath(path: string, github: GithubExpressionContext): unknown {
2296+
let value: unknown = github
2297+
for (const segment of path.split(".")) {
2298+
if (value === null || value === undefined) {
2299+
return null
2300+
}
2301+
const match = /^([A-Za-z_][A-Za-z0-9_]*)(?:\[(\d+)\])?$/.exec(segment)
2302+
if (!match) {
2303+
throw new Error(`Unsupported path segment: ${segment}`)
2304+
}
2305+
value = (value as Record<string, unknown>)[match[1]]
2306+
if (match[2] !== undefined) {
2307+
if (!Array.isArray(value)) {
2308+
return null
2309+
}
2310+
value = value[Number(match[2])] ?? null
2311+
}
2312+
}
2313+
return value ?? null
2314+
}
2315+
2316+
function evaluatePrimary(expression: string, github: GithubExpressionContext): unknown {
2317+
const current = expression.trim()
2318+
if (current.startsWith("(")) {
2319+
// Strip the parentheses only when the first one wraps the whole expression.
2320+
let depth = 0
2321+
for (let index = 0; index < current.length; index++) {
2322+
if (current[index] === "(") {
2323+
depth++
2324+
} else if (current[index] === ")") {
2325+
depth--
2326+
}
2327+
if (depth === 0) {
2328+
if (index === current.length - 1) {
2329+
return evaluateExpression(current.slice(1, -1), github)
2330+
}
2331+
break
2332+
}
2333+
}
2334+
}
2335+
if (current.startsWith("'") && current.endsWith("'")) {
2336+
return current.slice(1, -1)
2337+
}
2338+
if (/^\d+$/.test(current)) {
2339+
return Number(current)
2340+
}
2341+
if (current.startsWith("github.")) {
2342+
return resolvePath(current.slice("github.".length), github)
2343+
}
2344+
return evaluateExpression(current, github)
2345+
}
2346+
2347+
function evaluateComparison(expression: string, github: GithubExpressionContext): unknown {
2348+
const parts = splitTopLevel(expression, "==")
2349+
if (parts.length === 1) {
2350+
return evaluatePrimary(parts[0], github)
2351+
}
2352+
return looseEquals(evaluatePrimary(parts[0], github), evaluatePrimary(parts[1], github))
2353+
}
2354+
2355+
function evaluateAndChain(expression: string, github: GithubExpressionContext): unknown {
2356+
let result: unknown = true
2357+
for (const part of splitTopLevel(expression, "&&")) {
2358+
result = evaluateComparison(part, github)
2359+
if (!isTruthy(result)) {
2360+
return result
2361+
}
2362+
}
2363+
return result
2364+
}
2365+
2366+
function evaluateExpression(expression: string, github: GithubExpressionContext): unknown {
2367+
let result: unknown = null
2368+
for (const part of splitTopLevel(expression, "||")) {
2369+
result = evaluateAndChain(part, github)
2370+
if (isTruthy(result)) {
2371+
return result
2372+
}
2373+
}
2374+
return result
2375+
}
2376+
2377+
function concurrencyGroupFor(github: GithubExpressionContext): string {
2378+
const groupTemplate = workflow.concurrency.group as string
2379+
return groupTemplate.replace(/\$\{\{(.+?)\}\}/s, (_match, expression: string) => {
2380+
const value = evaluateExpression(expression, github)
2381+
return isTruthy(value) ? String(value) : ""
2382+
})
2383+
}
2384+
2385+
function contextFor(overrides: {
2386+
event_name: string
2387+
run_id?: number
2388+
event?: GithubExpressionContext["event"]
2389+
}): GithubExpressionContext {
2390+
return {
2391+
event_name: overrides.event_name,
2392+
run_id: overrides.run_id ?? 21057,
2393+
event: overrides.event ?? {},
2394+
}
2395+
}
2396+
2397+
it("keeps cancel-in-progress disabled", () => {
2398+
expect(workflow.concurrency["cancel-in-progress"]).toBe(false)
2399+
})
2400+
2401+
it("keys every per-PR event type for the same PR to the same group", () => {
2402+
const contexts = [
2403+
contextFor({ event_name: "pull_request_target", event: { pull_request: { number: 1664 } } }),
2404+
contextFor({ event_name: "pull_request_review", event: { pull_request: { number: 1664 } } }),
2405+
// CodeRabbit status comments arrive as issue_comment events on the PR.
2406+
contextFor({ event_name: "issue_comment", event: { issue: { number: 1664 } } }),
2407+
contextFor({ event_name: "workflow_dispatch", event: { inputs: { pull_request_number: 1664 } } }),
2408+
contextFor({
2409+
event_name: "workflow_run",
2410+
event: { workflow_run: { pull_requests: [{ number: 1664 }] } },
2411+
}),
2412+
]
2413+
2414+
for (const context of contexts) {
2415+
expect(concurrencyGroupFor(context)).toBe("label-pr-review-state-1664")
2416+
}
2417+
})
2418+
2419+
it("keys different PRs to different groups so unrelated triggers cannot starve a pending run", () => {
2420+
// The #1707 regression: with one shared group, a trigger for PR B superseded
2421+
// PR A's pending reconcile. Per-PR keys make that supersession impossible.
2422+
const prA = contextFor({ event_name: "pull_request_target", event: { pull_request: { number: 1664 } } })
2423+
const prB = contextFor({ event_name: "pull_request_target", event: { pull_request: { number: 1707 } } })
2424+
2425+
const groupA = concurrencyGroupFor(prA)
2426+
const groupB = concurrencyGroupFor(prB)
2427+
expect(groupA).toBe("label-pr-review-state-1664")
2428+
expect(groupB).toBe("label-pr-review-state-1707")
2429+
expect(groupB).not.toBe(groupA)
2430+
})
2431+
2432+
it("keys issue comments by issue number, matching the PR group for the same numeric id", () => {
2433+
// Issues and PRs share one GitHub number sequence, so an issue-comment run
2434+
// can never collide with a different PR's group.
2435+
const issueComment = contextFor({ event_name: "issue_comment", event: { issue: { number: 1707 } } })
2436+
const prEvent = contextFor({ event_name: "pull_request_target", event: { pull_request: { number: 1707 } } })
2437+
2438+
expect(concurrencyGroupFor(issueComment)).toBe(concurrencyGroupFor(prEvent))
2439+
})
2440+
2441+
it("falls back to unique per-run groups for workflow runs without an associated PR", () => {
2442+
// Fork workflow_run events leave pull_requests empty, as do branch runs with
2443+
// no open PR. They must never be superseded by (or supersede) another run.
2444+
const forkRunA = contextFor({
2445+
event_name: "workflow_run",
2446+
run_id: 35477088534,
2447+
event: { workflow_run: { pull_requests: [] } },
2448+
})
2449+
const forkRunB = contextFor({
2450+
event_name: "workflow_run",
2451+
run_id: 35477090001,
2452+
event: { workflow_run: { pull_requests: [] } },
2453+
})
2454+
2455+
const groupA = concurrencyGroupFor(forkRunA)
2456+
const groupB = concurrencyGroupFor(forkRunB)
2457+
expect(groupA).toBe("label-pr-review-state-35477088534")
2458+
expect(groupB).toBe("label-pr-review-state-35477090001")
2459+
expect(groupA).not.toBe(groupB)
2460+
expect(groupA).not.toBe("label-pr-review-state-1664")
2461+
expect(groupB).not.toBe("label-pr-review-state-1664")
2462+
})
2463+
2464+
it("shares a single sweep group for the hourly schedule and pushes to main", () => {
2465+
const scheduled = contextFor({ event_name: "schedule" })
2466+
const push = contextFor({ event_name: "push" })
2467+
2468+
expect(concurrencyGroupFor(scheduled)).toBe("label-pr-review-state-sweep")
2469+
expect(concurrencyGroupFor(push)).toBe("label-pr-review-state-sweep")
2470+
})
2471+
})

0 commit comments

Comments
 (0)