Skip to content

Commit 072254d

Browse files
vkpdevelopermaria-rcksjuliusmarminge
authored
fix(server): baseline existing pull request checks when watching (pingdotgg#23522)
Co-authored-by: maria-rcks <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent a135972 commit 072254d

6 files changed

Lines changed: 275 additions & 40 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4709,6 +4709,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
47094709
passedChecks: existing.endedWatch.passedChecks,
47104710
conflicting: existing.endedWatch.conflicting,
47114711
}),
4712+
awaitingBaseline: existing.endedWatch
4713+
? existing.endedWatch.awaitingBaseline
4714+
: "all",
47124715
...(existing.endedWatch?.owedChecks !== undefined && {
47134716
owedChecks: existing.endedWatch.owedChecks,
47144717
}),

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ function watchesEqual(left: ThreadPullRequestWatch, right: ThreadPullRequestWatc
203203
left.remarkIds.join("\n") === right.remarkIds.join("\n") &&
204204
left.conflicting === right.conflicting &&
205205
left.wakes === right.wakes &&
206+
left.awaitingBaseline === right.awaitingBaseline &&
206207
left.checksAwaitedSince === right.checksAwaitedSince
207208
);
208209
}

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

Lines changed: 207 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,203 @@ const remark = (
8181
const noRemarks: ReadonlyArray<PullRequestComment> = [];
8282

8383
describe("evaluatePullRequestWatch", () => {
84+
it.each([false, true])("baselines already-passed checks (required: %s)", (required) => {
85+
const green = detail({ checks: [{ ...check("test", "success"), required }] });
86+
const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), green, noRemarks);
87+
assert.deepEqual(baseline.changes, []);
88+
assert.equal(baseline.next.headSha, green.headSha);
89+
assert.isTrue(baseline.next.passed);
90+
assert.deepEqual(baseline.next.passedChecks, ["test"]);
91+
assert.isFalse(baseline.exhausted);
92+
assert.deepEqual(evaluatePullRequestWatch(baseline.next, green, noRemarks).changes, []);
93+
94+
const pending = evaluatePullRequestWatch(
95+
baseline.next,
96+
detail({ checks: [{ ...check("test", "pending"), required }] }),
97+
noRemarks,
98+
);
99+
assert.deepEqual(pending.changes, []);
100+
const failed = evaluatePullRequestWatch(
101+
pending.next,
102+
detail({ checks: [{ ...check("test", "failure"), required }] }),
103+
noRemarks,
104+
);
105+
assert.equal(failed.changes[0]?.kind, "checks-failed");
106+
const passed = evaluatePullRequestWatch(failed.next, green, noRemarks);
107+
assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 1, required }]);
108+
assert.deepEqual(
109+
evaluatePullRequestWatch(passed.next, { ...green, headSha: "bbbbbbbbbb" }, noRemarks).changes,
110+
[{ kind: "checks-passed", count: 1, required }],
111+
);
112+
});
113+
114+
it("baselines existing failures and conflicts, then reports new ones", () => {
115+
const broken = detail({
116+
checks: [check("lint", "failure"), check("test", "pending")],
117+
mergeability: "conflicting",
118+
});
119+
const baseline = evaluatePullRequestWatch(
120+
watch({ awaitingBaseline: "all" }),
121+
broken,
122+
noRemarks,
123+
);
124+
assert.deepEqual(baseline.changes, []);
125+
assert.deepEqual(baseline.next.failedChecks, ["lint"]);
126+
assert.isFalse(baseline.next.passed);
127+
assert.isTrue(baseline.next.conflicting);
128+
assert.deepEqual(evaluatePullRequestWatch(baseline.next, broken, noRemarks).changes, []);
129+
130+
const failed = evaluatePullRequestWatch(
131+
baseline.next,
132+
{ ...broken, checks: [check("lint", "failure"), check("test", "failure")] },
133+
noRemarks,
134+
);
135+
assert.deepEqual(failed.changes, [
136+
{ kind: "checks-failed", failed: [check("test", "failure")] },
137+
]);
138+
const clean = evaluatePullRequestWatch(failed.next, detail(), noRemarks);
139+
assert.deepEqual(clean.changes, []);
140+
assert.deepEqual(evaluatePullRequestWatch(clean.next, broken, noRemarks).changes, [
141+
{ kind: "checks-failed", failed: [check("lint", "failure")] },
142+
{ kind: "conflicting" },
143+
]);
144+
});
145+
146+
it("baselines checks already awaiting approval", () => {
147+
const gated = detail({ checks: [{ ...check("CI", "action-required"), url: run(1) }] });
148+
const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), gated, noRemarks);
149+
assert.deepEqual(baseline.changes, []);
150+
assert.deepEqual(evaluatePullRequestWatch(baseline.next, gated, noRemarks).changes, []);
151+
});
152+
153+
it("reports remarks after the watch started while baselining checks and conflicts", () => {
154+
const green = detail({
155+
checks: [check("test", "success")],
156+
mergeability: "conflicting",
157+
});
158+
const comments = [
159+
remark("reviewer", "2026-10-02T11:00:00Z"),
160+
remark("reviewer", "2026-10-02T12:06:00Z"),
161+
];
162+
const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), green, comments);
163+
assert.deepEqual(baseline.changes, [{ kind: "remarks", remarks: [comments[1]!] }]);
164+
assert.isTrue(baseline.next.passed);
165+
assert.isTrue(baseline.next.conflicting);
166+
assert.equal(baseline.next.wakes, 1);
167+
assert.deepEqual(evaluatePullRequestWatch(baseline.next, green, comments).changes, []);
168+
});
169+
170+
it("reports a failure once the head SHA appears after a baseline without one", () => {
171+
const waiting = evaluatePullRequestWatch(
172+
watch({ awaitingBaseline: "all" }),
173+
detail({ headSha: undefined, checks: [check("test", "pending")] }),
174+
noRemarks,
175+
);
176+
assert.deepEqual(waiting.changes, []);
177+
const failed = evaluatePullRequestWatch(
178+
waiting.next,
179+
detail({ checks: [check("test", "failure")] }),
180+
noRemarks,
181+
);
182+
assert.deepEqual(failed.changes, [
183+
{ kind: "checks-failed", failed: [check("test", "failure")] },
184+
]);
185+
});
186+
187+
it("keeps a baseline taken without a head SHA once the SHA appears", () => {
188+
const green = detail({ checks: [check("test", "success")] });
189+
const baseline = evaluatePullRequestWatch(
190+
watch({ awaitingBaseline: "all" }),
191+
{ ...green, headSha: undefined },
192+
noRemarks,
193+
);
194+
const known = evaluatePullRequestWatch(baseline.next, green, noRemarks);
195+
assert.deepEqual(known.changes, []);
196+
assert.equal(known.next.headSha, green.headSha);
197+
const unknown = evaluatePullRequestWatch(
198+
known.next,
199+
{ ...green, headSha: undefined },
200+
noRemarks,
201+
);
202+
assert.equal(unknown.next.headSha, green.headSha);
203+
assert.deepEqual(
204+
evaluatePullRequestWatch(unknown.next, { ...green, headSha: "bbbbbbbbbb" }, noRemarks)
205+
.changes,
206+
[{ kind: "checks-passed", count: 1, required: false }],
207+
);
208+
});
209+
210+
it.each(["success", "failure"] as const)(
211+
"reports %s on checks that appear after a first read without any",
212+
(status) => {
213+
const empty = evaluatePullRequestWatch(
214+
watch({ awaitingBaseline: "all" }),
215+
detail({ checks: [] }),
216+
noRemarks,
217+
);
218+
assert.deepEqual(empty.changes, []);
219+
assert.isUndefined(empty.next.awaitingBaseline);
220+
assert.deepEqual(
221+
evaluatePullRequestWatch(empty.next, detail({ checks: [check("test", status)] }), noRemarks)
222+
.changes,
223+
status === "success"
224+
? [{ kind: "checks-passed", count: 1, required: false }]
225+
: [{ kind: "checks-failed", failed: [check("test", "failure")] }],
226+
);
227+
},
228+
);
229+
230+
it("keeps the check baseline through a read whose approval lookup failed", () => {
231+
const bot = check("bot", "success");
232+
const unavailable = { ...check("Workflow approval status", "pending"), placeholder: true };
233+
const partial = evaluatePullRequestWatch(
234+
watch({ awaitingBaseline: "all" }),
235+
detail({ checks: [bot, unavailable] }),
236+
noRemarks,
237+
);
238+
assert.deepEqual(partial.changes, []);
239+
assert.equal(partial.next.awaitingBaseline, "checks");
240+
const approval = { ...check("CI", "action-required"), url: run(1) };
241+
for (const checks of [[bot], [bot, approval]]) {
242+
assert.deepEqual(
243+
evaluatePullRequestWatch(partial.next, detail({ checks }), noRemarks).changes,
244+
[],
245+
);
246+
}
247+
});
248+
249+
it("reports pending advisory checks finishing after a required-check baseline", () => {
250+
const initial = detail({
251+
checks: [{ ...check("test", "success"), required: true }, check("bot", "pending")],
252+
});
253+
const baseline = evaluatePullRequestWatch(
254+
watch({ awaitingBaseline: "all" }),
255+
initial,
256+
noRemarks,
257+
);
258+
assert.deepEqual(baseline.changes, []);
259+
const settled = evaluatePullRequestWatch(
260+
baseline.next,
261+
{
262+
...initial,
263+
checks: [initial.checks[0]!, check("bot", "success")],
264+
},
265+
noRemarks,
266+
);
267+
assert.deepEqual(settled.changes, [{ kind: "checks-passed", count: 2, required: false }]);
268+
assert.deepEqual(
269+
evaluatePullRequestWatch(
270+
settled.next,
271+
{
272+
...initial,
273+
checks: [initial.checks[0]!, check("bot", "success")],
274+
},
275+
noRemarks,
276+
).changes,
277+
[],
278+
);
279+
});
280+
84281
it("waits for workflow approval once, through reads that miss it, until the run finishes", () => {
85282
const bot = check("bot", "success");
86283
const approval = { ...check("CI", "action-required"), url: run(1) };
@@ -571,14 +768,22 @@ describe("evaluatePullRequestWatch", () => {
571768
const unknown = detail({
572769
checks: [{ ...check("Workflow approval status", "action-required"), placeholder: true }],
573770
});
574-
assert.deepEqual(anHourLater(watch(), unknown).flat(), []);
575-
const unread = evaluate(watch(), unknown, noRemarks, 0);
771+
const fresh = watch({ awaitingBaseline: "all" });
772+
assert.deepEqual(anHourLater(fresh, unknown).flat(), []);
773+
const unread = evaluate(fresh, unknown, noRemarks, 0);
576774
assert.deepEqual(
577775
evaluate(unread.next, detail({ checks: [] }), noRemarks, 60 * 60_000).changes,
578776
[{ kind: "no-checks", minutes: 60 }],
579777
);
580778
});
581779

780+
it("starts the no-checks clock while a fresh watch awaits its baseline", () => {
781+
assert.deepEqual(
782+
anHourLater(watch({ awaitingBaseline: "all" }), detail({ checks: [] })).flat(),
783+
["no-checks"],
784+
);
785+
});
786+
582787
it("never reports missing checks on a host where an empty list says nothing", () => {
583788
const silent = detail({ capabilities: capabilities(false), checks: [] });
584789
assert.isTrue(anHourLater(watch(), silent).every((kinds) => kinds.length === 0));

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

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ export function evaluatePullRequestWatch(
4545
now: number,
4646
): PullRequestWatchReport {
4747
const changes: Array<PullRequestWatchChange> = [];
48-
// Azure DevOps omits the head when its iteration read fails; that is not a push.
48+
// A read without a head says nothing about pushes, so it keeps the last known one.
4949
const headSha = detail.headSha ?? watch.headSha;
50-
const headMoved = headSha !== watch.headSha;
50+
const headMoved = watch.headSha !== null && headSha !== watch.headSha;
51+
const checksBaseline = watch.awaitingBaseline !== undefined && !headMoved;
5152

5253
// Hosts can return no checks on read failure; preserve prior state.
5354
let failedChecks = headMoved ? [] : watch.failedChecks;
@@ -63,9 +64,11 @@ export function evaluatePullRequestWatch(
6364
const awaiting = checks.filter((check) => check.status === "action-required");
6465
if (checks.length > 0) {
6566
const newlyFailed = failed.filter((check) => !failedChecks.includes(check.name));
66-
if (newlyFailed.length > 0) changes.push({ kind: "checks-failed", failed: newlyFailed });
67+
if (!checksBaseline && newlyFailed.length > 0) {
68+
changes.push({ kind: "checks-failed", failed: newlyFailed });
69+
}
6770
if (!approvalAnnounced && awaiting.length > 0) {
68-
changes.push({ kind: "checks-awaiting-approval", checks: awaiting });
71+
if (!checksBaseline) changes.push({ kind: "checks-awaiting-approval", checks: awaiting });
6972
approvalAnnounced = true;
7073
}
7174
failedChecks = failed.map((check) => check.name);
@@ -125,7 +128,9 @@ export function evaluatePullRequestWatch(
125128
checks.every((check) => check.status !== "pending");
126129
owedChecks = owed;
127130
if (passedNow && (!passed || gateGrew)) {
128-
changes.push({ kind: "checks-passed", count: gate.length, required: required.length > 0 });
131+
if (!checksBaseline) {
132+
changes.push({ kind: "checks-passed", count: gate.length, required: required.length > 0 });
133+
}
129134
owedChecks =
130135
required.length > 0
131136
? checks
@@ -146,9 +151,10 @@ export function evaluatePullRequestWatch(
146151
// repository runs no checks at all, nothing waits.
147152
let checksAwaitedSince = watch.checksAwaitedSince;
148153
if (detail.capabilities.checksAlwaysReport === true) {
149-
const awaitedBefore = headMoved
150-
? DateTime.formatIso(DateTime.makeUnsafe(now))
151-
: (watch.checksAwaitedSince ?? null);
154+
const awaitedBefore =
155+
headMoved || watch.awaitingBaseline === "all"
156+
? DateTime.formatIso(DateTime.makeUnsafe(now))
157+
: (watch.checksAwaitedSince ?? null);
152158
checksAwaitedSince = checks.length > 0 ? null : awaitedBefore;
153159
const awaitedMs = checksAwaitedSince === null ? 0 : now - Date.parse(checksAwaitedSince);
154160
if (!unread && awaitedMs >= PULL_REQUEST_WATCH_NO_CHECKS_MS) {
@@ -179,7 +185,7 @@ export function evaluatePullRequestWatch(
179185

180186
// Like a failed check, a conflict a push did not resolve is news for the new head.
181187
const toldConflict = !headMoved && watch.conflicting;
182-
if (detail.mergeability === "conflicting" && !toldConflict) {
188+
if (watch.awaitingBaseline !== "all" && detail.mergeability === "conflicting" && !toldConflict) {
183189
changes.push({ kind: "conflicting" });
184190
}
185191
// "unknown" is a host still computing after a push; only a clean answer clears a conflict.
@@ -204,6 +210,11 @@ export function evaluatePullRequestWatch(
204210
remarkIds,
205211
conflicting,
206212
wakes,
213+
// Only a read that cannot be trusted to show the checks leaves the baseline to the next one.
214+
...(checksBaseline &&
215+
(unread || (detail.headSha === undefined && checks.length === 0)) && {
216+
awaitingBaseline: "checks" as const,
217+
}),
207218
checksAwaitedSince,
208219
},
209220
exhausted: commentsOnly && wakes >= PULL_REQUEST_WATCH_WAKE_LIMIT,

0 commit comments

Comments
 (0)