Skip to content

Commit 038fdd4

Browse files
tarasclaude
andcommitted
Pin a checks watch to the head observed before collecting
A watch learned its head only from a complete checks collection, so a failed page discarded the head the provider had already observed: a retry after the pull request moved pinned the new head and could report its checks as success. The watch now pins the head from a details read before any collection, reads the head again before every later collection and verifies it before accepting one, so a move is reported as head-changed for the original head without paging the new head's checks. Adds regressions for a failed page followed by a move, a move after pinning, recovery on the same head, and continuation after a cut during the retry. Refs #1459. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent df7501d commit 038fdd4

3 files changed

Lines changed: 182 additions & 45 deletions

File tree

‎packages/cli/tests/repl-pull-request-checks-watch.test.ts‎

Lines changed: 121 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ interface Ci {
8383
refuses?: number;
8484
/** Answer check runs with a first page whose next page fails. */
8585
partial?: boolean;
86+
/** A status the pull request itself answers with instead of its record. */
87+
pullRefuses?: number;
8688
/** Called before a request is answered; a row holds or rewrites state here. */
8789
before?: (request: GitHubHttpRequest) => Operation<void>;
8890
}
@@ -247,7 +249,11 @@ function useGitHub(
247249
if (ci.before !== undefined) {
248250
yield* ci.before(request);
249251
}
250-
const answer = checksAnswer(store, ci, request) ?? respond(store, request);
252+
const pull = /\/pulls\/[1-9][0-9]*$/.test(new URL(request.url).pathname);
253+
const answer =
254+
pull && ci.pullRefuses !== undefined
255+
? { status: ci.pullRefuses, body: JSON.stringify({ message: "Server Error" }) }
256+
: (checksAnswer(store, ci, request) ?? respond(store, request));
251257
outgoing.writeHead(answer.status, {
252258
"content-type": "application/json",
253259
...(answer.link === undefined ? {} : { link: answer.link }),
@@ -748,14 +754,15 @@ describe("a watch that cannot settle", () => {
748754
{ outcome: "success", headSha: HEAD, checks: [statusRecord(42, "green", "success")] },
749755
]);
750756
expect(progress(ran.live)).toEqual([
751-
"Checks: retrying after a read error",
757+
"Checks at aaaaaaa: retrying after a read error",
752758
"Checks at aaaaaaa: 1 passed, 0 failed, 0 pending",
753759
]);
754760
});
755761

756762
it("W8: no head before the deadline binds a null head and no records", function* () {
757763
const fixture = yield* useFixture();
758764
fixture.ci.refuses = 500;
765+
fixture.ci.pullRefuses = 500;
759766
const stream = new InMemoryStream();
760767
const task = yield* started(fixture, watching(10), stream);
761768
yield* fixture.clock.waitFor((at) => at === START + CHECK_WATCH_RETRY_MS);
@@ -1119,3 +1126,115 @@ describe("a retained watch this entry cannot account for", () => {
11191126
}
11201127
});
11211128
});
1129+
1130+
/** Whether the watch read anything at this head's commits. */
1131+
function pagedAt(fixture: Fixture, head: string): boolean {
1132+
return fixture.store.requests.some((request) =>
1133+
new URL(request.url).pathname.includes(`/commits/${head}/`),
1134+
);
1135+
}
1136+
1137+
describe("the head a watch observed before its collection failed", () => {
1138+
it("W19: head A, a failed page, then green checks on B: head-changed for A, nothing of B", function* () {
1139+
const fixture = yield* useFixture();
1140+
fixture.ci.runs = [checkRun(31, "build", "completed", "success")];
1141+
fixture.ci.partial = true;
1142+
const stream = new InMemoryStream();
1143+
const task = yield* started(fixture, watching(), stream);
1144+
// The pull request answered with A, and A's check-run paging failed.
1145+
const retry = yield* fixture.clock.waitFor((at) => at === START + CHECK_WATCH_RETRY_MS);
1146+
expect(pagedAt(fixture, HEAD)).toBe(true);
1147+
1148+
pullRequest(fixture).headSha = MOVED;
1149+
delete fixture.ci.partial;
1150+
fixture.ci.statuses = [commitStatus(42, "green", "success")];
1151+
fixture.clock.advanceTo(retry);
1152+
const ran = yield* finished(task);
1153+
1154+
expect(ran.refusal).toBe(undefined);
1155+
expect(results(ran.events)).toEqual([
1156+
{ outcome: "incomplete", reason: "head-changed", headSha: HEAD, checks: [] },
1157+
]);
1158+
expect(retained(ran.events, PULL_REQUEST_CHECKS_WATCH_HEAD)).toEqual([
1159+
{ headSha: HEAD, failing: false },
1160+
]);
1161+
// The watch saw B and reported A: B's green checks were never collected.
1162+
expect(pullRequest(fixture).headSha).toBe(MOVED);
1163+
expect(pagedAt(fixture, MOVED)).toBe(false);
1164+
expect(JSON.stringify(rendered(ran.output))).not.toContain(MOVED);
1165+
});
1166+
1167+
it("W20: a head that moves after pinning ends the watch before its collection is paged", function* () {
1168+
const fixture = yield* useFixture();
1169+
fixture.ci.statuses = [commitStatus(42, "green", "pending")];
1170+
const stream = new InMemoryStream();
1171+
const task = yield* started(fixture, watching(), stream);
1172+
const poll = yield* pollAt(fixture);
1173+
pullRequest(fixture).headSha = MOVED;
1174+
// B's paging would fail; the watch must not reach it.
1175+
fixture.ci.partial = true;
1176+
fixture.clock.advanceTo(poll);
1177+
const ran = yield* finished(task);
1178+
expect(results(ran.events)).toEqual([
1179+
{
1180+
outcome: "incomplete",
1181+
reason: "head-changed",
1182+
headSha: HEAD,
1183+
checks: [statusRecord(42, "green", "pending")],
1184+
},
1185+
]);
1186+
expect(pagedAt(fixture, MOVED)).toBe(false);
1187+
});
1188+
1189+
it("W21: a transient paging failure on A that recovers on A settles for A", function* () {
1190+
const fixture = yield* useFixture();
1191+
fixture.ci.runs = [checkRun(31, "build", "completed", "success")];
1192+
fixture.ci.partial = true;
1193+
const stream = new InMemoryStream();
1194+
const task = yield* started(fixture, watching(), stream);
1195+
const retry = yield* fixture.clock.waitFor((at) => at === START + CHECK_WATCH_RETRY_MS);
1196+
delete fixture.ci.partial;
1197+
fixture.clock.advanceTo(retry);
1198+
const ran = yield* finished(task);
1199+
expect(results(ran.events)).toEqual([
1200+
{
1201+
outcome: "success",
1202+
headSha: HEAD,
1203+
checks: [runRecord(31, "build", "completed", "success")],
1204+
},
1205+
]);
1206+
expect(progress(ran.live)).toEqual([
1207+
"Checks at aaaaaaa: retrying after a read error",
1208+
"Checks at aaaaaaa: 1 passed, 0 failed, 0 pending",
1209+
]);
1210+
});
1211+
1212+
it("W22: a watch cut while retrying a failed page resumes on A and reports B's move", function* () {
1213+
const fixture = yield* useFixture();
1214+
fixture.ci.runs = [checkRun(31, "build", "completed", "success")];
1215+
fixture.ci.partial = true;
1216+
const first = new InMemoryStream();
1217+
const task = yield* started(fixture, watching(), first);
1218+
yield* fixture.clock.waitFor((at) => at === START + CHECK_WATCH_RETRY_MS);
1219+
yield* task.halt();
1220+
const prefix = [...(yield* first.readAll())];
1221+
expect(retained(prefix, PULL_REQUEST_CHECKS_WATCH_HEAD)).toEqual([
1222+
{ headSha: HEAD, failing: false },
1223+
]);
1224+
expect(results(prefix)).toEqual([]);
1225+
1226+
fixture.clock.now = START + 20_000;
1227+
pullRequest(fixture).headSha = MOVED;
1228+
delete fixture.ci.partial;
1229+
fixture.ci.statuses = [commitStatus(42, "green", "success")];
1230+
const again = yield* run(fixture, watching(), new InMemoryStream([...prefix]));
1231+
expect(again.refusal).toBe(undefined);
1232+
expect(results(again.events)).toEqual([
1233+
{ outcome: "incomplete", reason: "head-changed", headSha: HEAD, checks: [] },
1234+
]);
1235+
expect(retained(again.events, PULL_REQUEST_CHECKS_WATCH_DEADLINE)).toEqual([
1236+
{ deadline: START + 60_000 },
1237+
]);
1238+
expect(pagedAt(fixture, MOVED)).toBe(false);
1239+
});
1240+
});

‎packages/git/src/composition/pull-request-checks-watch.ts‎

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
*
1313
* ## The head is pinned
1414
*
15-
* The first successful collection names the head it was read at, and that head
16-
* is the one the watch reports on. Every later collection is held to it, and a
17-
* collection is accepted only after a separate read of the pull request still
18-
* names it. A different head ends the watch as `head-changed` even when the
15+
* The first successful read of the pull request names the head, and that head is
16+
* the one the watch reports on, however many collections fail after it. The
17+
* head is read again before every later collection and once more before a
18+
* collection is accepted; the collection itself must be at the pinned head too. A different head ends the watch as `head-changed` even when the
1919
* collection just read would have settled it, because that collection describes
2020
* a revision nobody asked about.
2121
*
@@ -266,14 +266,14 @@ export interface CheckWatchReads {
266266
export interface CheckWatchState {
267267
/** When the budget ends, in the clock's milliseconds. */
268268
readonly deadline: number;
269-
/** The pinned head, once a collection has named one. */
269+
/** The pinned head, once the pull request has named one. */
270270
headSha: string | null;
271271
/** The last complete collection accepted for the pinned head. */
272272
checks: readonly CheckEvidence[];
273273
/** Whether the latest read failed and no later one has succeeded. */
274274
failing: boolean;
275-
/** A collection read at the pinned head and not yet verified against it. */
276-
unverified?: readonly CheckEvidence[];
275+
/** The pinned head was just observed, so the next collection needs no fresh look. */
276+
observed?: boolean;
277277
/** The last progress line written, so a repeated one is suppressed. */
278278
shown?: string;
279279
}
@@ -286,11 +286,12 @@ export function checkWatchState(deadline: number): CheckWatchState {
286286
}
287287

288288
/**
289-
* Poll until a collection names the head, or the budget ends.
289+
* Poll until the pull request names its head, or the budget ends.
290290
*
291291
* The first half of a watch, separated so a host can retain the pinned head
292-
* before it continues: an unfinished watch resumes with the head it pinned
293-
* rather than whatever the pull request names now.
292+
* before any collection is read: a collection that fails afterwards, a retry,
293+
* and an unfinished watch that resumes all keep the head first observed rather
294+
* than whatever the pull request names by then.
294295
*/
295296
export function* pinCheckHead(
296297
state: CheckWatchState,
@@ -299,13 +300,13 @@ export function* pinCheckHead(
299300
): Operation<void> {
300301
yield* withinDeadline(state, function* (): Operation<void> {
301302
while (state.headSha === null) {
302-
const collected = yield* collection(reads);
303-
if (!collected.ok) {
303+
const head = yield* observedHead(reads);
304+
if (!head.ok) {
304305
yield* failed(state, progress);
305306
continue;
306307
}
307-
state.headSha = collected.value.headSha;
308-
state.unverified = collected.value.items;
308+
state.headSha = head.value;
309+
state.observed = true;
309310
}
310311
});
311312
}
@@ -328,22 +329,37 @@ export function* settleChecks(
328329
let ended: CheckWatchResult | undefined;
329330
yield* withinDeadline(state, function* (): Operation<void> {
330331
while (ended === undefined) {
331-
const items = state.unverified ?? (yield* pinnedCollection(reads, pinned));
332-
delete state.unverified;
333-
if (items === "moved") {
334-
ended = incomplete(state, "head-changed");
335-
continue;
332+
// The head is looked at before every collection, so a move is reported
333+
// without paging the new head's checks — even when that paging would fail
334+
// or never finish.
335+
if (state.observed !== true) {
336+
const head = yield* observedHead(reads);
337+
if (!head.ok) {
338+
yield* failed(state, progress);
339+
continue;
340+
}
341+
if (head.value !== pinned) {
342+
ended = incomplete(state, "head-changed");
343+
continue;
344+
}
336345
}
337-
if (items === undefined) {
346+
delete state.observed;
347+
const collected = yield* collection(reads);
348+
if (!collected.ok) {
338349
yield* failed(state, progress);
339350
continue;
340351
}
341-
const verified = yield* reads.read("details");
342-
if (!verified.ok || verified.value.kind !== "details") {
352+
if (collected.value.headSha !== pinned) {
353+
ended = incomplete(state, "head-changed");
354+
continue;
355+
}
356+
const items = collected.value.items;
357+
const verified = yield* observedHead(reads);
358+
if (!verified.ok) {
343359
yield* failed(state, progress);
344360
continue;
345361
}
346-
if (verified.value.details.headSha !== pinned) {
362+
if (verified.value !== pinned) {
347363
ended = incomplete(state, "head-changed");
348364
continue;
349365
}
@@ -361,19 +377,18 @@ export function* settleChecks(
361377
return ended ?? incomplete(state, state.failing ? "read-error" : "timeout");
362378
}
363379

364-
/** One collection at the pinned head, `"moved"` for another head, or `undefined`. */
365-
function* pinnedCollection(
366-
reads: CheckWatchReads,
367-
pinned: string,
368-
): Operation<readonly CheckEvidence[] | "moved" | undefined> {
369-
const collected = yield* collection(reads);
370-
if (!collected.ok) {
371-
return undefined;
380+
/** The head the pull request names now, from its own record. */
381+
function* observedHead(reads: CheckWatchReads): Operation<Result<string>> {
382+
const read = yield* reads.read("details");
383+
if (!read.ok) {
384+
return read;
372385
}
373-
if (collected.value.headSha !== pinned) {
374-
return "moved";
386+
if (read.value.kind !== "details") {
387+
return Err(
388+
new PullRequestReadError("protocol", ELEMENT, "a details read answered another kind."),
389+
);
375390
}
376-
return collected.value.items;
391+
return Ok(read.value.details.headSha);
377392
}
378393

379394
/** One complete checks read, narrowed to the collection it must be. */

‎specs/workflow-workspace-spec.md‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,14 +2271,17 @@ checks keeps the watch waiting: nothing reported is not everything passed. Once
22712271
every reported check has settled, any failed one makes the outcome `failure`;
22722272
otherwise it is `success`.
22732273

2274-
**Pinning.** Each cycle is the ordinary `checks` read, which observes the pull
2275-
request's head and reads every result at that SHA, followed by a `details` read
2276-
of the same pull request. The first successful collection pins its head. A
2277-
collection is accepted only when the `details` read still names the pinned
2278-
head; a different head on either read ends the watch as `head-changed`, with
2279-
the pinned SHA and the collection accepted before it, even when the collection
2280-
just read would have settled the watch. A watch never follows a moved head;
2281-
starting another watch is the caller's decision.
2274+
**Pinning.** The first successful `details` read of the pull request pins its
2275+
head, before any collection is read. That head is the one the watch reports on
2276+
through every collection failure, retry and continuation that follows. Each
2277+
cycle then reads the head again with `details` — except the first, which follows
2278+
the pinning read — reads the ordinary `checks` collection, which observes the
2279+
head itself and reads every result at that SHA, and reads `details` once more
2280+
before accepting it. A different head on any of the three ends the watch as
2281+
`head-changed`, with the pinned SHA and the collection accepted before it, even
2282+
when the collection just read would have settled the watch; a head that moved
2283+
before a collection is reported without paging the new head's checks. A watch
2284+
never follows a moved head; starting another watch is the caller's decision.
22822285

22832286
**Budget and failures.** The deadline is taken before the first read, and one
22842287
race against it bounds every request, page, retry delay and poll sleep. A cycle
@@ -2317,7 +2320,7 @@ provider, budget, owner and site — as input:
23172320
| Type | Retained value |
23182321
| --- | --- |
23192322
| `pull_request_checks_watch_deadline` | `{ deadline }`, milliseconds since the epoch |
2320-
| `pull_request_checks_watch_head` | `{ headSha, failing }` once the first collection names a head or the budget ends |
2323+
| `pull_request_checks_watch_head` | `{ headSha, failing }` once the pull request names a head or the budget ends |
23212324
| `pull_request_checks_watch_result` | the result above |
23222325

23232326
Polling happens only inside the bodies of the second and third effects, which

0 commit comments

Comments
 (0)