Skip to content

Commit b4c5e2c

Browse files
aglowienkaclaude
andcommitted
fix(server): watch_pull_request wakes once more and stops when the checks finish after a failure
A reported failure could keep the required checks from passing while the other checks finished, and nothing woke the thread again: no check was newly failed and the gate could not pass. The watch stayed armed and the thread stayed in Working until someone noticed. Once every check has finished and a reported failure still blocks the gate, the watch now sends one last wake that lists what still fails, and ends, so the thread returns to the user's inbox. A rerun that goes pending arms the notice again. The flag rides on the ended watch, so watching again does not repeat it. An advisory failure after the required checks passed does not end the watch: the agent already had its completion signal. Fixes #25497 Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_01MrRujdnDgAgch35FAe2w5T
1 parent 3f94487 commit b4c5e2c

7 files changed

Lines changed: 154 additions & 20 deletions

File tree

‎apps/server/src/mcp/toolkits/pullRequests/tools.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ const ListThreadPullRequestsTool = Tool.make("list_thread_pull_requests", {
308308

309309
const WatchPullRequestTool = Tool.make("watch_pull_request", {
310310
description:
311-
"Watch an open PR for this thread, linking it first if needed. T3 wakes you on failed checks, passed required checks, new comments or reviews, or conflicts. Handle old comments first. Only later ones wake you. After this call, end your turn. The watch stays on after each wake, so do not call this again on a wake. A wake is not a merge decision. Check readiness yourself.",
311+
"Watch an open PR for this thread, linking it first if needed. T3 wakes you on failed checks, passed required checks, new comments or reviews, or conflicts. Handle old comments first. Only later ones wake you. After this call, end your turn. The watch stays on after each wake, so do not call this again on a wake. Once every check has finished and a reported failure keeps the required ones from passing, T3 wakes you one last time and stops watching; call this again after you push a fix. A wake is not a merge decision. Check readiness yourself.",
312312
parameters: PullRequestTargetInput,
313313
success: WatchPullRequestResult,
314314
failure: PullRequestToolError,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3581,6 +3581,9 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
35813581
...(existing.endedWatch?.owedChecks !== undefined && {
35823582
owedChecks: existing.endedWatch.owedChecks,
35833583
}),
3584+
...(existing.endedWatch?.finished !== undefined && {
3585+
finished: existing.endedWatch.finished,
3586+
}),
35843587
...(existing.endedWatch?.checksAwaitedSince !== undefined && {
35853588
checksAwaitedSince: existing.endedWatch.checksAwaitedSince,
35863589
}),

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ type WatchEndReason =
9696
| "closed"
9797
| "unreadable"
9898
| "comment-limit"
99+
| "checks-finished"
99100
| "settled"
100101
| "subagent"
101102
| "stopped";
@@ -162,6 +163,7 @@ function watchesEqual(left: ThreadPullRequestWatch, right: ThreadPullRequestWatc
162163
left.passed === right.passed &&
163164
left.passedChecks.join("\n") === right.passedChecks.join("\n") &&
164165
(left.owedChecks ?? []).join("\n") === (right.owedChecks ?? []).join("\n") &&
166+
(left.finished ?? false) === (right.finished ?? false) &&
165167
left.remarksThrough === right.remarksThrough &&
166168
left.remarkIds.join("\n") === right.remarkIds.join("\n") &&
167169
left.conflicting === right.conflicting &&
@@ -539,14 +541,19 @@ export const make = Effect.gen(function* () {
539541
headSha: report.next.headSha,
540542
report,
541543
});
544+
const end: WatchEndReason | undefined = report.exhausted
545+
? "comment-limit"
546+
: report.finished
547+
? "checks-finished"
548+
: undefined;
542549
// An ending watch keeps what this pass saw, so watching again starts from it.
543550
return (
544-
report.exhausted
545-
? record(target, null, wake, report.next)
546-
: record(target, report.next, wake)
551+
end === undefined
552+
? record(target, report.next, wake)
553+
: record(target, null, wake, report.next)
547554
).pipe(
548555
Effect.tap(() => woke(target)),
549-
Effect.tap(() => (report.exhausted ? ended(target, "comment-limit") : Effect.void)),
556+
Effect.tap(() => (end === undefined ? Effect.void : ended(target, end))),
550557
);
551558
}
552559
return watchesEqual(report.next, target.watch) ? Effect.void : record(target, report.next);

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

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { assert, describe, it } from "@effect/vitest";
88
import {
99
PULL_REQUEST_WATCH_WAKE_LIMIT,
1010
evaluatePullRequestWatch as evaluate,
11+
pullRequestWatchMessage,
1112
} from "./pullRequestWatch.ts";
1213

1314
// These cases read the head once or at one moment, so the no-checks notice never comes due.
@@ -223,6 +224,91 @@ describe("evaluatePullRequestWatch", () => {
223224
assert.deepEqual(evaluatePullRequestWatch(final.next, lateResult, noRemarks).changes, []);
224225
});
225226

227+
// regression: #25497 a reported failure stood while the rest finished, and nothing woke the thread
228+
it("wakes once more and ends when the rest finish after a reported failure", () => {
229+
const labels = { ...check("labels", "failure"), required: true };
230+
const early = detail({ checks: [labels, check("test", "pending"), check("lint", "pending")] });
231+
const first = evaluatePullRequestWatch(watch(), early, noRemarks);
232+
assert.deepEqual(first.changes, [{ kind: "checks-failed", failed: [labels] }]);
233+
assert.isFalse(first.finished);
234+
assert.deepEqual(evaluatePullRequestWatch(first.next, early, noRemarks).changes, []);
235+
236+
const partly = detail({ checks: [labels, check("test", "success"), check("lint", "pending")] });
237+
assert.deepEqual(evaluatePullRequestWatch(first.next, partly, noRemarks).changes, []);
238+
239+
const done = detail({ checks: [labels, check("test", "success"), check("lint", "success")] });
240+
const final = evaluatePullRequestWatch(first.next, done, noRemarks);
241+
assert.deepEqual(final.changes, [{ kind: "checks-finished", count: 3, failed: [labels] }]);
242+
assert.isTrue(final.finished);
243+
assert.isTrue(final.next.finished);
244+
assert.deepEqual(evaluatePullRequestWatch(final.next, done, noRemarks).changes, []);
245+
246+
// A rerun arms the notice again, and its result is news either way.
247+
const rerun = detail({ checks: [{ ...labels, status: "pending" }, ...done.checks.slice(1)] });
248+
const waiting = evaluatePullRequestWatch(final.next, rerun, noRemarks);
249+
assert.deepEqual(waiting.changes, []);
250+
assert.isFalse(waiting.next.finished);
251+
const green = detail({ checks: [{ ...labels, status: "success" }, ...done.checks.slice(1)] });
252+
assert.deepEqual(evaluatePullRequestWatch(waiting.next, green, noRemarks).changes, [
253+
{ kind: "checks-passed", count: 1, required: true },
254+
]);
255+
const red = evaluatePullRequestWatch(waiting.next, done, noRemarks);
256+
assert.deepEqual(red.changes, [{ kind: "checks-failed", failed: [labels] }]);
257+
assert.isTrue(evaluatePullRequestWatch(red.next, done, noRemarks).finished);
258+
});
259+
260+
it("tells the agent the checks finished on the pass after a failure that came last", () => {
261+
const done = detail({ checks: [check("lint", "success"), check("test", "failure")] });
262+
const first = evaluatePullRequestWatch(watch(), done, noRemarks);
263+
assert.deepEqual(first.changes, [
264+
{ kind: "checks-failed", failed: [check("test", "failure")] },
265+
]);
266+
assert.isFalse(first.finished);
267+
const final = evaluatePullRequestWatch(first.next, done, noRemarks);
268+
assert.deepEqual(final.changes, [
269+
{ kind: "checks-finished", count: 2, failed: [check("test", "failure")] },
270+
]);
271+
const pushed = detail({ headSha: "bbbbbbbbbb", checks: [check("test", "pending")] });
272+
assert.isFalse(evaluatePullRequestWatch(final.next, pushed, noRemarks).next.finished);
273+
});
274+
275+
it("leaves a watch alone when only an advisory check fails after the required ones passed", () => {
276+
const required = { ...check("test", "success"), required: true };
277+
const failed = detail({ checks: [required, check("bot", "failure")] });
278+
const first = evaluatePullRequestWatch(watch(), failed, noRemarks);
279+
assert.deepEqual(
280+
first.changes.map((change) => change.kind),
281+
["checks-failed", "checks-passed"],
282+
);
283+
const again = evaluatePullRequestWatch(first.next, failed, noRemarks);
284+
assert.deepEqual(again.changes, []);
285+
assert.isFalse(again.finished);
286+
});
287+
288+
it("does not repeat the finished notice when the agent watches again", () => {
289+
const done = detail({ checks: [check("lint", "success"), check("test", "failure")] });
290+
const told = watch({ headSha: "aaaaaaaaaa", failedChecks: ["test"], finished: true });
291+
assert.deepEqual(evaluatePullRequestWatch(told, done, noRemarks).changes, []);
292+
});
293+
294+
it("ends the message with the stop notice when the checks finished", () => {
295+
const done = detail({ checks: [check("lint", "success"), check("test", "failure")] });
296+
const first = evaluatePullRequestWatch(watch(), done, noRemarks);
297+
const report = evaluatePullRequestWatch(first.next, done, noRemarks);
298+
const { text, notification } = pullRequestWatchMessage({
299+
number: 1,
300+
url: "https://github.com/o/r/pull/1",
301+
baseBranch: "main",
302+
headSha: report.next.headSha,
303+
report,
304+
});
305+
assert.include(text, "- All 2 checks finished on aaaaaaa. 1 passed. Still failing:");
306+
assert.include(text, " - test https://ci.example/test");
307+
assert.include(text, "T3 Code stopped watching: every check has finished");
308+
assert.equal(notification.outcome, "failed");
309+
assert.equal(notification.summary, "#1: checks finished, stopped watching");
310+
});
311+
226312
// regression: #16208 PR watches burned GitHub's rate limit and gave up
227313
it("does not report passed again for a new passed check where none is required", () => {
228314
const first = evaluatePullRequestWatch(
@@ -310,7 +396,15 @@ describe("evaluatePullRequestWatch", () => {
310396
const reported = evaluatePullRequestWatch(watch(), failed, noRemarks);
311397
assert.equal(reported.changes.length, 1);
312398
const unreadable = evaluatePullRequestWatch(reported.next, detail({ checks: [] }), noRemarks);
313-
assert.deepEqual(evaluatePullRequestWatch(unreadable.next, failed, noRemarks).changes, []);
399+
const direct = evaluatePullRequestWatch(reported.next, failed, noRemarks);
400+
assert.notInclude(
401+
direct.changes.map((change) => change.kind),
402+
"checks-failed",
403+
);
404+
assert.deepEqual(
405+
evaluatePullRequestWatch(unreadable.next, failed, noRemarks).changes,
406+
direct.changes,
407+
);
314408
});
315409

316410
it("wakes for the pull request's author when the agent is someone else", () => {

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

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,19 @@ export type PullRequestWatchChange =
1919
| { readonly kind: "checks-passed"; readonly count: number; readonly required: boolean }
2020
| { readonly kind: "remarks"; readonly remarks: ReadonlyArray<PullRequestComment> }
2121
| { readonly kind: "no-checks"; readonly minutes: number }
22+
| {
23+
readonly kind: "checks-finished";
24+
readonly count: number;
25+
readonly failed: ReadonlyArray<PullRequestCheck>;
26+
}
2227
| { readonly kind: "conflicting" };
2328

2429
export interface PullRequestWatchReport {
2530
readonly changes: ReadonlyArray<PullRequestWatchChange>;
2631
readonly next: ThreadPullRequestWatch;
2732
readonly exhausted: boolean;
33+
/** Every check finished with a failure blocking the gate: only a push can bring check news. */
34+
readonly finished: boolean;
2835
}
2936

3037
// action-required is a finished check needing human input.
@@ -49,6 +56,7 @@ export function evaluatePullRequestWatch(
4956
let passed = headMoved ? false : watch.passed;
5057
let passedChecks = headMoved ? [] : watch.passedChecks;
5158
let owedChecks = headMoved ? [] : (watch.owedChecks ?? []);
59+
let finished = headMoved ? false : (watch.finished ?? false);
5260
if (detail.checks.length > 0) {
5361
const failed = detail.checks.filter(isFailedCheck);
5462
const newlyFailed = failed.filter((check) => !failedChecks.includes(check.name));
@@ -58,6 +66,16 @@ export function evaluatePullRequestWatch(
5866
const required = detail.checks.filter((check) => check.required === true);
5967
const gate = required.length > 0 ? required : detail.checks;
6068
const passedNow = gate.every((check) => check.status !== "pending" && !isFailedCheck(check));
69+
// A gate failure the agent already heard of can stand while the rest finish, and then nothing
70+
// short of a push can wake it: no check is new and the gate cannot pass. A rerun goes pending,
71+
// which arms this once more.
72+
const pending = detail.checks.some((check) => check.status === "pending");
73+
if (pending) {
74+
finished = false;
75+
} else if (!passedNow && failed.length > 0 && newlyFailed.length === 0 && !finished) {
76+
changes.push({ kind: "checks-finished", count: detail.checks.length, failed });
77+
finished = true;
78+
}
6179
const gateNames = gate.map((check) => check.name);
6280
const told = passed && passedChecks.length === 0 ? gateNames : passedChecks;
6381
// Required checks can finish between sweeps; detect new names. With no required checks, ignore advisory gate growth.
@@ -145,13 +163,15 @@ export function evaluatePullRequestWatch(
145163
passed,
146164
passedChecks,
147165
owedChecks,
166+
finished,
148167
remarksThrough,
149168
remarkIds,
150169
conflicting,
151170
wakes,
152171
checksAwaitedSince,
153172
},
154173
exhausted: commentsOnly && wakes >= PULL_REQUEST_WATCH_WAKE_LIMIT,
174+
finished: changes.some((change) => change.kind === "checks-finished"),
155175
};
156176
}
157177

@@ -169,20 +189,16 @@ function listed<T>(items: ReadonlyArray<T>, line: (item: T) => string): Array<st
169189
return lines;
170190
}
171191

192+
const checkLine = (check: PullRequestCheck) =>
193+
` - ${check.name}${check.status === "failure" ? "" : ` (${check.status})`}${check.url ? ` ${check.url}` : ""}`;
194+
172195
function changeLines(
173196
change: PullRequestWatchChange,
174197
context: { readonly baseBranch: string; readonly commit: string },
175198
): Array<string> {
176199
switch (change.kind) {
177200
case "checks-failed":
178-
return [
179-
`- Checks failed${context.commit}:`,
180-
...listed(
181-
change.failed,
182-
(check) =>
183-
` - ${check.name}${check.status === "failure" ? "" : ` (${check.status})`}${check.url ? ` ${check.url}` : ""}`,
184-
),
185-
];
201+
return [`- Checks failed${context.commit}:`, ...listed(change.failed, checkLine)];
186202
case "checks-passed":
187203
return [
188204
`- All ${change.count} ${change.required ? "required " : ""}${change.count === 1 ? "check" : "checks"} passed${context.commit}.`,
@@ -201,6 +217,11 @@ function changeLines(
201217
return [
202218
`- No checks have reported${context.commit} after ${change.minutes} minutes. None may be configured to run on this change.`,
203219
];
220+
case "checks-finished":
221+
return [
222+
`- All ${change.count} checks finished${context.commit}. ${change.count - change.failed.length} passed. Still failing:`,
223+
...listed(change.failed, checkLine),
224+
];
204225
case "conflicting":
205226
return [`- The branch now conflicts with ${context.baseBranch}.`];
206227
}
@@ -211,6 +232,7 @@ const SUMMARY: Record<PullRequestWatchChange["kind"], string> = {
211232
"checks-passed": "checks passed",
212233
remarks: "new comments",
213234
"no-checks": "no checks reported",
235+
"checks-finished": "checks finished",
214236
conflicting: "merge conflict",
215237
};
216238

@@ -221,7 +243,7 @@ export function pullRequestWatchMessage(input: {
221243
readonly headSha: string | null;
222244
readonly report: PullRequestWatchReport;
223245
}): { readonly text: string; readonly notification: OrchestrationV2Notification } {
224-
const { changes, exhausted } = input.report;
246+
const { changes, exhausted, finished } = input.report;
225247
const context = {
226248
baseBranch: input.baseBranch,
227249
commit: input.headSha === null ? "" : ` on ${input.headSha.slice(0, 7)}`,
@@ -232,13 +254,18 @@ export function pullRequestWatchMessage(input: {
232254
"",
233255
exhausted
234256
? `T3 Code stopped watching after ${PULL_REQUEST_WATCH_WAKE_LIMIT} comment-only updates in a row. Call watch_pull_request to watch it again.`
235-
: "Look into each item and act on it as your task requires. This PR stays linked and watched, so do not link, watch, or list it again. End your turn when you are done, and T3 Code wakes you on the next change. When you hand the work back to the user, call unwatch_pull_request first so the thread returns to their inbox.",
257+
: finished
258+
? "T3 Code stopped watching: every check has finished and the failures above stand, so no check news can come until the head moves. If you push a fix, call watch_pull_request again. Otherwise hand the work back to the user; the thread is already back in their inbox."
259+
: "Look into each item and act on it as your task requires. This PR stays linked and watched, so do not link, watch, or list it again. End your turn when you are done, and T3 Code wakes you on the next change. When you hand the work back to the user, call unwatch_pull_request first so the thread returns to their inbox.",
236260
].join("\n");
237261
const failed = changes.some(
238-
(change) => change.kind === "checks-failed" || change.kind === "conflicting",
262+
(change) =>
263+
change.kind === "checks-failed" ||
264+
change.kind === "checks-finished" ||
265+
change.kind === "conflicting",
239266
);
240267
const summary = changes.map((change) => SUMMARY[change.kind]);
241-
if (exhausted) summary.push("stopped watching");
268+
if (exhausted || finished) summary.push("stopped watching");
242269
return {
243270
text,
244271
notification: {

‎docs/user/source-control.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ check fails, the required checks pass, someone else comments or reviews, or the
290290
conflict. Threads in a project that watch the same pull request share one check. On GitHub, a check
291291
first asks whether anything changed and reads the pull request only when it did, which keeps
292292
watching inside GitHub's rate limit. Comments from your own account do not wake it. Watching ends
293-
when the pull request merges or closes, after 10 wakes in a row that bring only comments, after 8
294-
failed reads in a row, or when you press Stop on the thread. A rate limit only pauses watching.
293+
when the pull request merges or closes, when every check has finished and a reported failure keeps
294+
the required ones from passing, after 10 wakes in a row that bring only comments, after 8 failed
295+
reads in a row, or when you press Stop on the thread. A rate limit only pauses watching.
295296
Settling or archiving a thread also ends all its watches. Unsettle the thread before starting a new
296297
watch. Watching a pull request again does not repeat check or conflict news its last
297298
watch already reported; unlinking the pull request clears that. Subagents cannot watch pull requests; the thread that delegated to them does. To start or stop

‎packages/contracts/src/threadPullRequest.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ export const ThreadPullRequestWatch = Schema.Struct({
8989
),
9090
/** Checks still pending when required ones passed early, owed one wake once all settle. */
9191
owedChecks: Schema.optional(Schema.Array(TrimmedNonEmptyString)),
92+
/** The agent was told every check on this head finished with a failure blocking the gate. */
93+
finished: Schema.optional(Schema.Boolean),
9294
remarksThrough: IsoDateTime,
9395
/** Reported remarks at remarksThrough, so late arrivals at that timestamp still count. */
9496
remarkIds: Schema.Array(TrimmedNonEmptyString),

0 commit comments

Comments
 (0)