Skip to content

Commit 780c52f

Browse files
committed
fix(server): end deferred watch baselines when the head changes
1 parent 7c2663f commit 780c52f

4 files changed

Lines changed: 124 additions & 25 deletions

File tree

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

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3571,18 +3571,16 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
35713571
passedChecks: [],
35723572
conflicting: false,
35733573
// Watching again must not repeat check or conflict news the last watch told.
3574-
...(existing.endedWatch
3575-
? {
3576-
headSha: existing.endedWatch.headSha,
3577-
failedChecks: existing.endedWatch.failedChecks,
3578-
passed: existing.endedWatch.passed,
3579-
passedChecks: existing.endedWatch.passedChecks,
3580-
conflicting: existing.endedWatch.conflicting,
3581-
...(existing.endedWatch.awaitingBaseline && {
3582-
awaitingBaseline: existing.endedWatch.awaitingBaseline,
3583-
}),
3584-
}
3585-
: { awaitingBaseline: "all" as const }),
3574+
...(existing.endedWatch && {
3575+
headSha: existing.endedWatch.headSha,
3576+
failedChecks: existing.endedWatch.failedChecks,
3577+
passed: existing.endedWatch.passed,
3578+
passedChecks: existing.endedWatch.passedChecks,
3579+
conflicting: existing.endedWatch.conflicting,
3580+
}),
3581+
awaitingBaseline: existing.endedWatch
3582+
? existing.endedWatch.awaitingBaseline
3583+
: "all",
35863584
...(existing.endedWatch?.owedChecks !== undefined && {
35873585
owedChecks: existing.endedWatch.owedChecks,
35883586
}),

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,72 @@ describe("evaluatePullRequestWatch", () => {
247247
);
248248
});
249249

250+
it.each(["success", "failure"] as const)(
251+
"reports %s after a known head changes before checks were baselined",
252+
(status) => {
253+
const empty = evaluatePullRequestWatch(
254+
watch({ awaitingBaseline: "all" }),
255+
detail({ checks: [] }),
256+
noRemarks,
257+
);
258+
const pushed = evaluatePullRequestWatch(
259+
empty.next,
260+
detail({ headSha: "bbbbbbbbbb", checks: [] }),
261+
noRemarks,
262+
);
263+
assert.isUndefined(pushed.next.awaitingBaseline);
264+
const result = evaluatePullRequestWatch(
265+
pushed.next,
266+
detail({ headSha: "bbbbbbbbbb", checks: [check("test", status)] }),
267+
noRemarks,
268+
);
269+
assert.deepEqual(
270+
result.changes,
271+
status === "success"
272+
? [{ kind: "checks-passed", count: 1, required: false }]
273+
: [{ kind: "checks-failed", failed: [check("test", "failure")] }],
274+
);
275+
const immediate = evaluatePullRequestWatch(
276+
empty.next,
277+
detail({ headSha: "bbbbbbbbbb", checks: [check("test", status)] }),
278+
noRemarks,
279+
);
280+
assert.deepEqual(immediate.changes, result.changes);
281+
},
282+
);
283+
284+
it("reports pending advisory checks finishing after a required-check baseline", () => {
285+
const initial = detail({
286+
checks: [{ ...check("test", "success"), required: true }, check("bot", "pending")],
287+
});
288+
const baseline = evaluatePullRequestWatch(
289+
watch({ awaitingBaseline: "all" }),
290+
initial,
291+
noRemarks,
292+
);
293+
assert.deepEqual(baseline.changes, []);
294+
const settled = evaluatePullRequestWatch(
295+
baseline.next,
296+
{
297+
...initial,
298+
checks: [initial.checks[0]!, check("bot", "success")],
299+
},
300+
noRemarks,
301+
);
302+
assert.deepEqual(settled.changes, [{ kind: "checks-passed", count: 2, required: false }]);
303+
assert.deepEqual(
304+
evaluatePullRequestWatch(
305+
settled.next,
306+
{
307+
...initial,
308+
checks: [initial.checks[0]!, check("bot", "success")],
309+
},
310+
noRemarks,
311+
).changes,
312+
[],
313+
);
314+
});
315+
250316
it("reports each failure at once, even while another check never finishes", () => {
251317
const bot = check("CodeRabbit", "pending");
252318
const first = detail({ checks: [check("lint", "failure"), check("test", "pending"), bot] });
@@ -565,6 +631,13 @@ describe("evaluatePullRequestWatch", () => {
565631
assert.deepEqual(pushed.flat(), ["no-checks"]);
566632
});
567633

634+
it("starts the no-checks clock while a fresh watch awaits its baseline", () => {
635+
assert.deepEqual(
636+
anHourLater(watch({ awaitingBaseline: "all" }), detail({ checks: [] })).flat(),
637+
["no-checks"],
638+
);
639+
});
640+
568641
it("never reports missing checks on a host where an empty list says nothing", () => {
569642
const silent = detail({ capabilities: capabilities(false), checks: [] });
570643
assert.isTrue(anHourLater(watch(), silent).every((kinds) => kinds.length === 0));

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,8 @@ export function evaluatePullRequestWatch(
4343
const changes: Array<PullRequestWatchChange> = [];
4444
// A read without a head says nothing about pushes, so it keeps the last known one.
4545
const headSha = detail.headSha ?? watch.headSha;
46-
// Remarks already have their baseline in startedAt.
47-
const checksBaseline = watch.awaitingBaseline !== undefined;
4846
const headMoved = watch.headSha !== null && headSha !== watch.headSha;
47+
const checksBaseline = watch.awaitingBaseline !== undefined && !headMoved;
4948

5049
// Hosts can return no checks on read failure; preserve prior state.
5150
let failedChecks = headMoved ? [] : watch.failedChecks;
@@ -78,8 +77,10 @@ export function evaluatePullRequestWatch(
7877
const settled =
7978
failed.length === 0 && detail.checks.every((check) => check.status !== "pending");
8079
owedChecks = owed;
81-
if (!checksBaseline && passedNow && (!passed || gateGrew)) {
82-
changes.push({ kind: "checks-passed", count: gate.length, required: required.length > 0 });
80+
if (passedNow && (!passed || gateGrew)) {
81+
if (!checksBaseline) {
82+
changes.push({ kind: "checks-passed", count: gate.length, required: required.length > 0 });
83+
}
8384
owedChecks =
8485
required.length > 0
8586
? detail.checks
@@ -100,9 +101,10 @@ export function evaluatePullRequestWatch(
100101
// repository runs no checks at all, nothing waits.
101102
let checksAwaitedSince = watch.checksAwaitedSince;
102103
if (detail.capabilities.checksAlwaysReport === true) {
103-
const awaitedBefore = headMoved
104-
? DateTime.formatIso(DateTime.makeUnsafe(now))
105-
: (watch.checksAwaitedSince ?? null);
104+
const awaitedBefore =
105+
headMoved || watch.awaitingBaseline === "all"
106+
? DateTime.formatIso(DateTime.makeUnsafe(now))
107+
: (watch.checksAwaitedSince ?? null);
106108
checksAwaitedSince = detail.checks.length > 0 ? null : awaitedBefore;
107109
const awaitedMs = checksAwaitedSince === null ? 0 : now - Date.parse(checksAwaitedSince);
108110
if (awaitedMs >= PULL_REQUEST_WATCH_NO_CHECKS_MS) {

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,6 +2673,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
26732673
it.effect("baselines a new watch across restarts and reports later changes", () =>
26742674
Effect.gen(function* () {
26752675
const orchestrator = yield* Orchestrator.OrchestratorV2;
2676+
const worker = yield* EffectWorker.OrchestrationEffectWorkerV2;
26762677
const threadId = ThreadId.make("runtime-pull-request-watch-told");
26772678
const projectId = ProjectId.make("pr-watch-told-project");
26782679
yield* seedProject({
@@ -2710,6 +2711,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
27102711
yield* watch(true, "start");
27112712

27122713
let headSha = "ad70160aaa";
2714+
let hasChecks = false;
27132715
const host = Layer.mergeAll(
27142716
NodeServices.layer,
27152717
Layer.mock(PullRequestService.PullRequestService)({
@@ -2721,7 +2723,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
27212723
at: "2026-10-02T12:00:00.000Z",
27222724
}),
27232725
headSha,
2724-
checks: ["lint", "test"].map((name) => ({
2726+
checks: (hasChecks ? ["lint", "test"] : []).map((name) => ({
27252727
name,
27262728
status: "success" as const,
27272729
description: null,
@@ -2746,32 +2748,56 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
27462748

27472749
const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
27482750
yield* reactor.sweep;
2751+
yield* worker.drain();
2752+
assert.deepEqual(yield* summaries, []);
2753+
assert.equal(
2754+
(yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch?.awaitingBaseline,
2755+
"checks",
2756+
);
2757+
yield* watch(false, "stop-before-baseline");
2758+
yield* watch(true, "resume-before-baseline");
2759+
const waitingRestart = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
2760+
yield* waitingRestart.sweep;
2761+
yield* worker.drain();
2762+
assert.equal(
2763+
(yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch?.awaitingBaseline,
2764+
"checks",
2765+
);
2766+
hasChecks = true;
2767+
yield* TestClock.adjust("10 minutes");
2768+
yield* waitingRestart.sweep;
2769+
yield* worker.drain();
27492770
assert.deepEqual(yield* summaries, []);
27502771
const baseline = (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch;
27512772
assert.equal(baseline?.headSha, headSha);
27522773
assert.isTrue(baseline?.passed);
27532774
assert.deepEqual(baseline?.passedChecks, ["lint", "test"]);
27542775
yield* TestClock.adjust("10 minutes");
27552776
yield* reactor.sweep;
2777+
yield* worker.drain();
27562778
assert.deepEqual(yield* summaries, []);
27572779

27582780
const restarted = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
27592781
yield* restarted.sweep;
2782+
yield* worker.drain();
27602783
assert.deepEqual(yield* summaries, []);
27612784

27622785
headSha = "bbbbbbbbbb";
27632786
yield* TestClock.adjust("10 minutes");
27642787
yield* restarted.sweep;
2788+
yield* worker.drain();
27652789
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
27662790

27672791
yield* watch(false, "stop");
27682792
yield* watch(true, "again");
27692793
yield* restarted.sweep;
2794+
yield* worker.drain();
27702795
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
27712796

27722797
headSha = "cccccccccc";
27732798
yield* TestClock.adjust("10 minutes");
27742799
yield* restarted.sweep;
2800+
yield* worker.drain();
27752801
assert.deepEqual(yield* summaries, ["#12: checks passed", "#12: checks passed"]);
27762802
yield* watch(false, "end");
27772803
}),
@@ -2858,7 +2884,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
28582884
yield* TestClock.adjust("10 minutes");
28592885
yield* reactor.sweep;
28602886
yield* worker.drain();
2861-
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
2887+
assert.deepEqual(yield* summaries, []);
28622888

28632889
assert.deepEqual(
28642890
(yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch?.owedChecks,
@@ -2868,14 +2894,14 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
28682894
const restarted = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
28692895
yield* restarted.sweep;
28702896
yield* worker.drain();
2871-
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
2897+
assert.deepEqual(yield* summaries, []);
28722898

28732899
// A new watch on the same pull request knows what the last one told.
28742900
yield* watch(false, "stop");
28752901
yield* watch(true, "again");
28762902
yield* restarted.sweep;
28772903
yield* worker.drain();
2878-
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
2904+
assert.deepEqual(yield* summaries, []);
28792905

28802906
assert.deepEqual(
28812907
(yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch?.owedChecks,
@@ -2885,11 +2911,11 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
28852911
yield* TestClock.adjust("10 minutes");
28862912
yield* restarted.sweep;
28872913
yield* worker.drain();
2888-
assert.deepEqual(yield* summaries, ["#12: checks passed", "#12: checks passed"]);
2914+
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
28892915
yield* TestClock.adjust("10 minutes");
28902916
yield* restarted.sweep;
28912917
yield* worker.drain();
2892-
assert.deepEqual(yield* summaries, ["#12: checks passed", "#12: checks passed"]);
2918+
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
28932919
assert.deepEqual(
28942920
(yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch?.owedChecks,
28952921
[],

0 commit comments

Comments
 (0)