Skip to content

Commit f6a32db

Browse files
shivamhwpmaria-rcksclaude
authored
fix(server): stop PR watch flapping while workflows await approval (pingdotgg#24599)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 20bb685 commit f6a32db

10 files changed

Lines changed: 263 additions & 30 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4158,6 +4158,12 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
41584158
...(existing.endedWatch?.owedChecks !== undefined && {
41594159
owedChecks: existing.endedWatch.owedChecks,
41604160
}),
4161+
...(existing.endedWatch?.awaitingApproval !== undefined && {
4162+
awaitingApproval: existing.endedWatch.awaitingApproval,
4163+
}),
4164+
...(existing.endedWatch?.approvalAnnounced !== undefined && {
4165+
approvalAnnounced: existing.endedWatch.approvalAnnounced,
4166+
}),
41614167
...(existing.endedWatch?.checksAwaitedSince !== undefined && {
41624168
checksAwaitedSince: existing.endedWatch.checksAwaitedSince,
41634169
}),

‎apps/server/src/orchestration-v2/PullRequestWatchReactor.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ function watchesEqual(left: ThreadPullRequestWatch, right: ThreadPullRequestWatc
178178
left.passed === right.passed &&
179179
left.passedChecks.join("\n") === right.passedChecks.join("\n") &&
180180
(left.owedChecks ?? []).join("\n") === (right.owedChecks ?? []).join("\n") &&
181+
(left.awaitingApproval ?? []).join("\n") === (right.awaitingApproval ?? []).join("\n") &&
182+
(left.approvalAnnounced ?? false) === (right.approvalAnnounced ?? false) &&
181183
left.remarksThrough === right.remarksThrough &&
182184
left.remarkIds.join("\n") === right.remarkIds.join("\n") &&
183185
left.conflicting === right.conflicting &&

‎apps/server/src/orchestration-v2/pullRequestWatch.test.ts‎

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ const watch = (overrides: Partial<ThreadPullRequestWatch> = {}): ThreadPullReque
3131
...overrides,
3232
});
3333

34+
const run = (id: number) => `https://github.com/o/r/actions/runs/${id}`;
35+
3436
const check = (name: string, status: PullRequestCheck["status"]): PullRequestCheck => ({
3537
name,
3638
status,
@@ -79,6 +81,136 @@ const remark = (
7981
const noRemarks: ReadonlyArray<PullRequestComment> = [];
8082

8183
describe("evaluatePullRequestWatch", () => {
84+
it("waits for workflow approval once, through reads that miss it, until the run finishes", () => {
85+
const bot = check("bot", "success");
86+
const approval = { ...check("CI", "action-required"), url: run(1) };
87+
const gated = detail({ checks: [bot, approval] });
88+
const first = evaluatePullRequestWatch(watch(), gated, noRemarks);
89+
assert.deepEqual(first.changes, [{ kind: "checks-awaiting-approval", checks: [approval] }]);
90+
91+
// GitHub's approval read can answer empty while the rest of the checks read fine.
92+
const missing = evaluatePullRequestWatch(first.next, detail({ checks: [bot] }), noRemarks);
93+
assert.deepEqual(missing.changes, []);
94+
assert.isFalse(missing.next.passed);
95+
assert.deepEqual(evaluatePullRequestWatch(missing.next, gated, noRemarks).changes, []);
96+
const later = { ...check("Preview", "action-required"), url: run(2) };
97+
assert.deepEqual(
98+
evaluatePullRequestWatch(missing.next, detail({ checks: [bot, approval, later] }), noRemarks)
99+
.changes,
100+
[],
101+
);
102+
const unnamed = { ...check("Docs", "action-required"), url: null };
103+
const named = detail({ checks: [bot, unnamed] });
104+
const alone = evaluatePullRequestWatch(watch(), named, noRemarks);
105+
assert.deepEqual(alone.changes, [{ kind: "checks-awaiting-approval", checks: [unnamed] }]);
106+
const placeholder = { ...unnamed, name: "Workflow approval status", placeholder: true };
107+
const unlisted = evaluatePullRequestWatch(
108+
alone.next,
109+
detail({ checks: [bot, placeholder] }),
110+
noRemarks,
111+
);
112+
assert.deepEqual(unlisted.changes, []);
113+
assert.isFalse(unlisted.next.passed);
114+
assert.deepEqual(evaluatePullRequestWatch(unlisted.next, named, noRemarks).changes, []);
115+
const dropped = evaluatePullRequestWatch(alone.next, detail({ checks: [bot] }), noRemarks);
116+
assert.deepEqual(dropped.changes, []);
117+
assert.isFalse(dropped.next.passed);
118+
assert.deepEqual(dropped.next.awaitingApproval, [unnamed.name]);
119+
assert.deepEqual(evaluatePullRequestWatch(dropped.next, named, noRemarks).changes, []);
120+
const namedPending = evaluatePullRequestWatch(
121+
dropped.next,
122+
detail({
123+
checks: [
124+
{ ...bot, required: true },
125+
{ ...unnamed, status: "pending" },
126+
],
127+
}),
128+
noRemarks,
129+
);
130+
assert.deepEqual(namedPending.changes, []);
131+
assert.isFalse(namedPending.next.passed);
132+
const namedDone = detail({ checks: [bot, { ...unnamed, status: "success" }] });
133+
const namedFinished = evaluatePullRequestWatch(namedPending.next, namedDone, noRemarks);
134+
assert.deepEqual(namedFinished.changes, [{ kind: "checks-passed", count: 2, required: false }]);
135+
assert.deepEqual(namedFinished.next.awaitingApproval, []);
136+
assert.deepEqual(
137+
evaluatePullRequestWatch(namedFinished.next, namedDone, noRemarks).changes,
138+
[],
139+
);
140+
const pushed = detail({ headSha: "bbbbbbbbbb", checks: [bot] });
141+
assert.deepEqual(evaluatePullRequestWatch(missing.next, pushed, noRemarks).changes, [
142+
{ kind: "checks-passed", count: 1, required: false },
143+
]);
144+
145+
// Approved, the run's jobs carry their own names under the run's URL.
146+
const job = { ...check("lint", "success"), url: `${run(1)}/job/7` };
147+
const pending = { ...check("test", "pending"), url: `${run(1)}/job/8` };
148+
const required = { ...bot, required: true };
149+
const running = evaluatePullRequestWatch(
150+
missing.next,
151+
detail({ checks: [required, job, pending] }),
152+
noRemarks,
153+
);
154+
assert.deepEqual(running.changes, []);
155+
assert.isFalse(running.next.passed);
156+
assert.deepEqual(running.next.awaitingApproval, [run(1)]);
157+
const overlap = evaluatePullRequestWatch(
158+
running.next,
159+
detail({ checks: [required, job, { ...pending, url: `${run(2)}/job/9` }] }),
160+
noRemarks,
161+
);
162+
assert.deepEqual(overlap.changes, []);
163+
assert.isFalse(overlap.next.passed);
164+
assert.deepEqual(overlap.next.awaitingApproval, [run(1)]);
165+
const unlinked = evaluatePullRequestWatch(
166+
overlap.next,
167+
detail({ checks: [required, job, { ...pending, url: null }] }),
168+
noRemarks,
169+
);
170+
assert.deepEqual(unlinked.changes, []);
171+
assert.isFalse(unlinked.next.passed);
172+
assert.deepEqual(unlinked.next.awaitingApproval, [run(1)]);
173+
const advisory = evaluatePullRequestWatch(
174+
unlinked.next,
175+
detail({ checks: [required, job, { ...pending, url: "https://ci.example/test" }] }),
176+
noRemarks,
177+
);
178+
assert.deepEqual(advisory.changes, [{ kind: "checks-passed", count: 1, required: true }]);
179+
assert.deepEqual(advisory.next.awaitingApproval, []);
180+
const completed = detail({ checks: [required, job, { ...pending, status: "success" }] });
181+
const finished = evaluatePullRequestWatch(unlinked.next, completed, noRemarks);
182+
assert.deepEqual(finished.changes, [{ kind: "checks-passed", count: 1, required: true }]);
183+
assert.deepEqual(finished.next.awaitingApproval, []);
184+
assert.deepEqual(evaluatePullRequestWatch(finished.next, completed, noRemarks).changes, []);
185+
186+
const done = evaluatePullRequestWatch(missing.next, detail({ checks: [bot, job] }), noRemarks);
187+
assert.deepEqual(done.changes, [{ kind: "checks-passed", count: 2, required: false }]);
188+
});
189+
190+
it("does not pass or fail while the host cannot tell whether workflows await approval", () => {
191+
const bot = check("bot", "success");
192+
const unknown = {
193+
...check("Workflow approval status", "action-required"),
194+
url: null,
195+
placeholder: true,
196+
};
197+
const unread = evaluatePullRequestWatch(watch(), detail({ checks: [bot, unknown] }), noRemarks);
198+
assert.deepEqual(unread.changes, []);
199+
assert.isFalse(unread.next.passed);
200+
const read = evaluatePullRequestWatch(unread.next, detail({ checks: [bot] }), noRemarks);
201+
assert.deepEqual(read.changes, [{ kind: "checks-passed", count: 1, required: false }]);
202+
const blocked = evaluatePullRequestWatch(
203+
read.next,
204+
detail({ checks: [{ ...bot, status: "pending" }, unknown] }),
205+
noRemarks,
206+
);
207+
assert.isFalse(blocked.next.passed);
208+
assert.deepEqual(
209+
evaluatePullRequestWatch(blocked.next, detail({ checks: [bot] }), noRemarks).changes,
210+
[{ kind: "checks-passed", count: 1, required: false }],
211+
);
212+
});
213+
82214
it("reports each failure at once, even while another check never finishes", () => {
83215
const bot = check("CodeRabbit", "pending");
84216
const first = detail({ checks: [check("lint", "failure"), check("test", "pending"), bot] });
@@ -95,6 +227,13 @@ describe("evaluatePullRequestWatch", () => {
95227

96228
const pushed = detail({ ...second, headSha: "bbbbbbbbbb" });
97229
assert.equal(evaluatePullRequestWatch(test.next, pushed, noRemarks).changes.length, 1);
230+
231+
const approval = { ...check("CI", "action-required"), url: run(1) };
232+
const gated = detail({ checks: [approval, check("lint", "failure")] });
233+
assert.deepEqual(evaluatePullRequestWatch(watch(), gated, noRemarks).changes, [
234+
{ kind: "checks-failed", failed: [check("lint", "failure")] },
235+
{ kind: "checks-awaiting-approval", checks: [approval] },
236+
]);
98237
});
99238

100239
it("reports passed once the required checks pass, whatever the others do", () => {
@@ -429,6 +568,15 @@ describe("evaluatePullRequestWatch", () => {
429568
assert.isTrue(anHourLater(legacy, detail({ checks: [] })).every((kinds) => kinds.length === 0));
430569
const pushed = anHourLater(legacy, detail({ headSha: "bbbbbbbbbb", checks: [] }));
431570
assert.deepEqual(pushed.flat(), ["no-checks"]);
571+
const unknown = detail({
572+
checks: [{ ...check("Workflow approval status", "action-required"), placeholder: true }],
573+
});
574+
assert.deepEqual(anHourLater(watch(), unknown).flat(), []);
575+
const unread = evaluate(watch(), unknown, noRemarks, 0);
576+
assert.deepEqual(
577+
evaluate(unread.next, detail({ checks: [] }), noRemarks, 60 * 60_000).changes,
578+
[{ kind: "no-checks", minutes: 60 }],
579+
);
432580
});
433581

434582
it("never reports missing checks on a host where an empty list says nothing", () => {

‎apps/server/src/orchestration-v2/pullRequestWatch.ts‎

Lines changed: 71 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const SNIPPET_LENGTH = 200;
1717
export type PullRequestWatchChange =
1818
| { readonly kind: "checks-failed"; readonly failed: ReadonlyArray<PullRequestCheck> }
1919
| { readonly kind: "checks-passed"; readonly count: number; readonly required: boolean }
20+
| { readonly kind: "checks-awaiting-approval"; readonly checks: ReadonlyArray<PullRequestCheck> }
2021
| { readonly kind: "remarks"; readonly remarks: ReadonlyArray<PullRequestComment> }
2122
| { readonly kind: "no-checks"; readonly minutes: number }
2223
| { readonly kind: "conflicting" };
@@ -27,9 +28,12 @@ export interface PullRequestWatchReport {
2728
readonly exhausted: boolean;
2829
}
2930

30-
// action-required is a finished check needing human input.
3131
const isFailedCheck = (check: PullRequestCheck) =>
32-
check.status === "failure" || check.status === "cancelled" || check.status === "action-required";
32+
check.status === "failure" || check.status === "cancelled";
33+
34+
// A GitHub Actions job's URL extends its workflow run's URL; their names need not match.
35+
const workflowRunOf = (url: string | null) => url?.match(/^.*\/actions\/runs\/\d+/)?.[0] ?? null;
36+
const approvalKey = (check: PullRequestCheck) => workflowRunOf(check.url) ?? check.name;
3337

3438
export function evaluatePullRequestWatch(
3539
watch: ThreadPullRequestWatch,
@@ -50,15 +54,59 @@ export function evaluatePullRequestWatch(
5054
let passed = headMoved ? false : watch.passed;
5155
let passedChecks = headMoved ? [] : watch.passedChecks;
5256
let owedChecks = headMoved ? [] : (watch.owedChecks ?? []);
53-
if (detail.checks.length > 0) {
54-
const failed = detail.checks.filter(isFailedCheck);
57+
let awaitingApproval = headMoved ? [] : (watch.awaitingApproval ?? []);
58+
let approvalAnnounced = headMoved ? false : (watch.approvalAnnounced ?? false);
59+
// An unavailable approval read cannot establish success or that no checks exist.
60+
const unread = detail.checks.some((check) => check.placeholder === true);
61+
const checks = detail.checks.filter((check) => check.placeholder !== true);
62+
const failed = checks.filter(isFailedCheck);
63+
const awaiting = checks.filter((check) => check.status === "action-required");
64+
if (checks.length > 0) {
5565
const newlyFailed = failed.filter((check) => !failedChecks.includes(check.name));
5666
if (newlyFailed.length > 0) changes.push({ kind: "checks-failed", failed: newlyFailed });
67+
if (!approvalAnnounced && awaiting.length > 0) {
68+
changes.push({ kind: "checks-awaiting-approval", checks: awaiting });
69+
approvalAnnounced = true;
70+
}
5771
failedChecks = failed.map((check) => check.name);
5872

59-
const required = detail.checks.filter((check) => check.required === true);
60-
const gate = required.length > 0 ? required : detail.checks;
61-
const passedNow = gate.every((check) => check.status !== "pending" && !isFailedCheck(check));
73+
// Approval reads can omit gated runs. Keep each hold until its jobs finish.
74+
const awaitingNow = [...new Set(awaiting.map(approvalKey))];
75+
const nameUnfinished = (name: string) => {
76+
const matching = checks.filter((check) => check.name === name);
77+
return (
78+
matching.length === 0 ||
79+
matching.some((check) => check.status === "pending" || check.status === "action-required")
80+
);
81+
};
82+
// GitHub deduplicates jobs across runs, so a visible pending job may hide one in an awaited run.
83+
// A pending check without a URL could be such a job; one with another host's URL cannot.
84+
const jobsPending = checks.some(
85+
(check) =>
86+
check.status === "pending" && (check.url === null || workflowRunOf(check.url) !== null),
87+
);
88+
const finished = new Set(
89+
jobsPending
90+
? []
91+
: checks
92+
.filter((check) => check.status !== "action-required" && check.status !== "pending")
93+
.map((check) => workflowRunOf(check.url)),
94+
);
95+
awaitingApproval = [
96+
...awaitingNow,
97+
...awaitingApproval.filter(
98+
(key) =>
99+
!awaitingNow.includes(key) &&
100+
(workflowRunOf(key) === key ? !finished.has(key) : nameUnfinished(key)),
101+
),
102+
];
103+
}
104+
const required = checks.filter((check) => check.required === true);
105+
const gate = required.length > 0 ? required : checks;
106+
const passedNow =
107+
awaitingApproval.length === 0 &&
108+
gate.every((check) => check.status !== "pending" && !isFailedCheck(check));
109+
if (checks.length > 0 && !(unread && passedNow)) {
62110
const gateNames = gate.map((check) => check.name);
63111
const told = passed && passedChecks.length === 0 ? gateNames : passedChecks;
64112
// Required checks can finish between sweeps; detect new names. With no required checks, ignore advisory gate growth.
@@ -67,23 +115,25 @@ export function evaluatePullRequestWatch(
67115
// Only those are owed, so later advisory churn never wakes. One turned required is gate news.
68116
const owed =
69117
passedNow && passed
70-
? detail.checks
118+
? checks
71119
.filter((check) => check.required !== true && owedChecks.includes(check.name))
72120
.map((check) => check.name)
73121
: [];
74122
const settled =
75-
failed.length === 0 && detail.checks.every((check) => check.status !== "pending");
123+
failed.length === 0 &&
124+
awaitingApproval.length === 0 &&
125+
checks.every((check) => check.status !== "pending");
76126
owedChecks = owed;
77127
if (passedNow && (!passed || gateGrew)) {
78128
changes.push({ kind: "checks-passed", count: gate.length, required: required.length > 0 });
79129
owedChecks =
80130
required.length > 0
81-
? detail.checks
131+
? checks
82132
.filter((check) => check.required !== true && check.status === "pending")
83133
.map((check) => check.name)
84134
: [];
85135
} else if (settled && owed.length > 0) {
86-
changes.push({ kind: "checks-passed", count: detail.checks.length, required: false });
136+
changes.push({ kind: "checks-passed", count: checks.length, required: false });
87137
owedChecks = [];
88138
}
89139
passed = passedNow;
@@ -99,9 +149,9 @@ export function evaluatePullRequestWatch(
99149
const awaitedBefore = headMoved
100150
? DateTime.formatIso(DateTime.makeUnsafe(now))
101151
: (watch.checksAwaitedSince ?? null);
102-
checksAwaitedSince = detail.checks.length > 0 ? null : awaitedBefore;
152+
checksAwaitedSince = checks.length > 0 ? null : awaitedBefore;
103153
const awaitedMs = checksAwaitedSince === null ? 0 : now - Date.parse(checksAwaitedSince);
104-
if (awaitedMs >= PULL_REQUEST_WATCH_NO_CHECKS_MS) {
154+
if (!unread && awaitedMs >= PULL_REQUEST_WATCH_NO_CHECKS_MS) {
105155
changes.push({ kind: "no-checks", minutes: Math.round(awaitedMs / 60_000) });
106156
checksAwaitedSince = null;
107157
}
@@ -148,6 +198,8 @@ export function evaluatePullRequestWatch(
148198
passed,
149199
passedChecks,
150200
owedChecks,
201+
awaitingApproval,
202+
approvalAnnounced,
151203
remarksThrough,
152204
remarkIds,
153205
conflicting,
@@ -190,6 +242,11 @@ function changeLines(
190242
` - ${check.name}${check.status === "failure" ? "" : ` (${check.status})`}${check.url ? ` ${check.url}` : ""}`,
191243
),
192244
];
245+
case "checks-awaiting-approval":
246+
return [
247+
`- Checks are waiting for approval or action from a maintainer${context.commit}:`,
248+
...listed(change.checks, (check) => ` - ${check.name}${check.url ? ` ${check.url}` : ""}`),
249+
];
193250
case "checks-passed":
194251
return [
195252
`- All ${change.count} ${change.required ? "required " : ""}${change.count === 1 ? "check" : "checks"} passed${context.commit}.`,
@@ -216,6 +273,7 @@ function changeLines(
216273
const SUMMARY: Record<PullRequestWatchChange["kind"], string> = {
217274
"checks-failed": "checks failed",
218275
"checks-passed": "checks passed",
276+
"checks-awaiting-approval": "checks await approval",
219277
remarks: "new comments",
220278
"no-checks": "no checks reported",
221279
conflicting: "merge conflict",

0 commit comments

Comments
 (0)