Skip to content

Commit f515485

Browse files
committed
fix(server): baseline existing pull request checks when watching
1 parent ed9b7ca commit f515485

3 files changed

Lines changed: 121 additions & 15 deletions

File tree

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

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const STARTED = "2026-10-02T12:00:00.000Z";
1111

1212
const watch = (overrides: Partial<ThreadPullRequestWatch> = {}): ThreadPullRequestWatch => ({
1313
startedAt: STARTED,
14-
headSha: null,
14+
headSha: "aaaaaaaaaa",
1515
failedChecks: [],
1616
passed: false,
1717
passedChecks: [],
@@ -58,6 +58,103 @@ const remark = (
5858
const noRemarks: ReadonlyArray<PullRequestComment> = [];
5959

6060
describe("evaluatePullRequestWatch", () => {
61+
it.each([false, true])("baselines already-passed checks (required: %s)", (required) => {
62+
const green = detail({ checks: [{ ...check("test", "success"), required }] });
63+
const baseline = evaluatePullRequestWatch(watch({ headSha: null }), green, noRemarks);
64+
assert.deepEqual(baseline.changes, []);
65+
assert.equal(baseline.next.headSha, green.headSha);
66+
assert.isTrue(baseline.next.passed);
67+
assert.deepEqual(baseline.next.passedChecks, ["test"]);
68+
assert.isFalse(baseline.exhausted);
69+
assert.deepEqual(evaluatePullRequestWatch(baseline.next, green, noRemarks).changes, []);
70+
71+
const pending = evaluatePullRequestWatch(
72+
baseline.next,
73+
detail({ checks: [{ ...check("test", "pending"), required }] }),
74+
noRemarks,
75+
);
76+
assert.deepEqual(pending.changes, []);
77+
const failed = evaluatePullRequestWatch(
78+
pending.next,
79+
detail({ checks: [{ ...check("test", "failure"), required }] }),
80+
noRemarks,
81+
);
82+
assert.equal(failed.changes[0]?.kind, "checks-failed");
83+
const passed = evaluatePullRequestWatch(failed.next, green, noRemarks);
84+
assert.deepEqual(passed.changes, [{ kind: "checks-passed", count: 1, required }]);
85+
assert.deepEqual(
86+
evaluatePullRequestWatch(passed.next, { ...green, headSha: "bbbbbbbbbb" }, noRemarks).changes,
87+
[{ kind: "checks-passed", count: 1, required }],
88+
);
89+
});
90+
91+
it("baselines existing failures and conflicts, then reports new ones", () => {
92+
const broken = detail({
93+
checks: [check("lint", "failure"), check("test", "pending")],
94+
mergeability: "conflicting",
95+
});
96+
const baseline = evaluatePullRequestWatch(watch({ headSha: null }), broken, noRemarks);
97+
assert.deepEqual(baseline.changes, []);
98+
assert.deepEqual(baseline.next.failedChecks, ["lint"]);
99+
assert.isFalse(baseline.next.passed);
100+
assert.isTrue(baseline.next.conflicting);
101+
assert.deepEqual(evaluatePullRequestWatch(baseline.next, broken, noRemarks).changes, []);
102+
103+
const failed = evaluatePullRequestWatch(
104+
baseline.next,
105+
{ ...broken, checks: [check("lint", "failure"), check("test", "failure")] },
106+
noRemarks,
107+
);
108+
assert.deepEqual(failed.changes, [
109+
{ kind: "checks-failed", failed: [check("test", "failure")] },
110+
]);
111+
const clean = evaluatePullRequestWatch(failed.next, detail(), noRemarks);
112+
assert.deepEqual(clean.changes, []);
113+
assert.deepEqual(evaluatePullRequestWatch(clean.next, broken, noRemarks).changes, [
114+
{ kind: "checks-failed", failed: [check("lint", "failure")] },
115+
{ kind: "conflicting" },
116+
]);
117+
});
118+
119+
it("reports remarks after the watch started while baselining checks and conflicts", () => {
120+
const green = detail({
121+
checks: [check("test", "success")],
122+
mergeability: "conflicting",
123+
});
124+
const comments = [
125+
remark("reviewer", "2026-10-02T11:00:00Z"),
126+
remark("reviewer", "2026-10-02T12:06:00Z"),
127+
];
128+
const baseline = evaluatePullRequestWatch(watch({ headSha: null }), green, comments);
129+
assert.deepEqual(baseline.changes, [{ kind: "remarks", remarks: [comments[1]!] }]);
130+
assert.isTrue(baseline.next.passed);
131+
assert.isTrue(baseline.next.conflicting);
132+
assert.equal(baseline.next.wakes, 1);
133+
assert.deepEqual(evaluatePullRequestWatch(baseline.next, green, comments).changes, []);
134+
});
135+
136+
it("keeps reporting check changes when the host omits the head SHA", () => {
137+
const pending = detail({ headSha: undefined, checks: [check("test", "pending")] });
138+
const waiting = evaluatePullRequestWatch(watch({ headSha: null }), pending, noRemarks);
139+
assert.deepEqual(waiting.changes, []);
140+
const failed = evaluatePullRequestWatch(
141+
waiting.next,
142+
{ ...pending, checks: [check("test", "failure")] },
143+
noRemarks,
144+
);
145+
assert.deepEqual(failed.changes, [
146+
{ kind: "checks-failed", failed: [check("test", "failure")] },
147+
]);
148+
assert.deepEqual(
149+
evaluatePullRequestWatch(
150+
failed.next,
151+
{ ...pending, checks: [check("test", "success")] },
152+
noRemarks,
153+
).changes,
154+
[{ kind: "checks-passed", count: 1, required: false }],
155+
);
156+
});
157+
61158
it("reports each failure at once, even while another check never finishes", () => {
62159
const bot = check("CodeRabbit", "pending");
63160
const first = detail({ checks: [check("lint", "failure"), check("test", "pending"), bot] });

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export function evaluatePullRequestWatch(
3737
): PullRequestWatchReport {
3838
const changes: Array<PullRequestWatchChange> = [];
3939
const headSha = detail.headSha ?? null;
40+
// Only a read with a head can persist a fresh watch's baseline. Remarks use startedAt.
41+
const baseline = watch.headSha === null && headSha !== null;
4042
const headMoved = headSha !== watch.headSha;
4143

4244
// An empty list keeps the last state: a host can answer with one when its check read fails.
@@ -46,7 +48,9 @@ export function evaluatePullRequestWatch(
4648
if (detail.checks.length > 0) {
4749
const failed = detail.checks.filter(isFailedCheck);
4850
const newlyFailed = failed.filter((check) => !failedChecks.includes(check.name));
49-
if (newlyFailed.length > 0) changes.push({ kind: "checks-failed", failed: newlyFailed });
51+
if (!baseline && newlyFailed.length > 0) {
52+
changes.push({ kind: "checks-failed", failed: newlyFailed });
53+
}
5054
failedChecks = failed.map((check) => check.name);
5155

5256
const required = detail.checks.filter((check) => check.required === true);
@@ -57,7 +61,7 @@ export function evaluatePullRequestWatch(
5761
// A required job created and finished between two passes is never seen pending. Without
5862
// required checks, any check counts, and advisory bots keep adding passed ones: no wake.
5963
const gateGrew = required.length > 0 && gateNames.some((name) => !told.includes(name));
60-
if (passedNow && (!passed || gateGrew)) {
64+
if (!baseline && passedNow && (!passed || gateGrew)) {
6165
changes.push({ kind: "checks-passed", count: gate.length, required: required.length > 0 });
6266
}
6367
passed = passedNow;
@@ -84,7 +88,7 @@ export function evaluatePullRequestWatch(
8488
...atLatest.map((remark) => remark.id),
8589
];
8690

87-
if (detail.mergeability === "conflicting" && !watch.conflicting) {
91+
if (!baseline && detail.mergeability === "conflicting" && !watch.conflicting) {
8892
changes.push({ kind: "conflicting" });
8993
}
9094
// "unknown" is GitHub still computing after a push; only a clean answer clears a conflict.

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
25232523
}),
25242524
);
25252525

2526-
it.effect("wakes once for a state it reported, across a restart and a new watch", () =>
2526+
it.effect("baselines a new watch across restarts and reports later changes", () =>
25272527
Effect.gen(function* () {
25282528
const orchestrator = yield* Orchestrator.OrchestratorV2;
25292529
const threadId = ThreadId.make("runtime-pull-request-watch-told");
@@ -2599,13 +2599,23 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
25992599

26002600
const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
26012601
yield* reactor.sweep;
2602+
assert.deepEqual(yield* summaries, []);
2603+
const baseline = (yield* orchestrator.getThreadShell(threadId))?.pullRequests?.[0]?.watch;
2604+
assert.equal(baseline?.headSha, headSha);
2605+
assert.isTrue(baseline?.passed);
2606+
assert.deepEqual(baseline?.passedChecks, ["lint", "test"]);
26022607
yield* TestClock.adjust("10 minutes");
26032608
yield* reactor.sweep;
2604-
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
2609+
assert.deepEqual(yield* summaries, []);
26052610

26062611
// A restart keeps nothing in memory; the watch state is all it has.
26072612
const restarted = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
26082613
yield* restarted.sweep;
2614+
assert.deepEqual(yield* summaries, []);
2615+
2616+
headSha = "bbbbbbbbbb";
2617+
yield* TestClock.adjust("10 minutes");
2618+
yield* restarted.sweep;
26092619
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
26102620

26112621
// A new watch on the same pull request knows what the last one told.
@@ -2614,7 +2624,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
26142624
yield* restarted.sweep;
26152625
assert.deepEqual(yield* summaries, ["#12: checks passed"]);
26162626

2617-
headSha = "bbbbbbbbbb";
2627+
headSha = "cccccccccc";
26182628
yield* TestClock.adjust("10 minutes");
26192629
yield* restarted.sweep;
26202630
assert.deepEqual(yield* summaries, ["#12: checks passed", "#12: checks passed"]);
@@ -2966,10 +2976,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
29662976
assert.deepEqual(held?.remarkIds, initialWatch?.remarkIds);
29672977
assert.deepEqual(held?.failedChecks, ["lint"]);
29682978
const { messages } = yield* orchestrator.getThreadRecords(threadId, ["messages"]);
2969-
assert.deepEqual(
2970-
messages.map((message) => message.notification?.summary),
2971-
["#7: checks failed"],
2972-
);
2979+
assert.deepEqual(messages, []);
29732980
yield* TestClock.adjust("1 millis");
29742981
recovering = true;
29752982
}
@@ -2983,9 +2990,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
29832990
messages.flatMap((message) =>
29842991
message.notification === undefined ? [] : [message.notification.summary],
29852992
),
2986-
incomplete
2987-
? ["#7: checks failed", "#7: new comments"]
2988-
: ["#7: checks failed, new comments"],
2993+
["#7: new comments"],
29892994
);
29902995
if (mode !== "single page") {
29912996
const wake = messages.at(-1);
@@ -2998,7 +3003,7 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
29983003
assert.deepEqual(watch?.remarkIds, [remark.id]);
29993004
assert.deepEqual(
30003005
{ headSha: watch?.headSha, failedChecks: watch?.failedChecks, wakes: watch?.wakes },
3001-
{ headSha: "abc1234def", failedChecks: ["lint"], wakes: incomplete ? 1 : 0 },
3006+
{ headSha: "abc1234def", failedChecks: ["lint"], wakes: 1 },
30023007
);
30033008
}),
30043009
);

0 commit comments

Comments
 (0)