@devries-cameron20 / t3code Mirror · read-only / commits / e6d521012d
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

fix(server): a PR watch tells the agent when the checks finish after a failure (#39596)

Co-authored-by: Ryan McDonough <Ryan@r2n.io> Co-authored-by: Julius Marminge <julius0216@outlook.com>

Adam Głowienka committed Nov 20, 2026 at 11:16 UTC e6d521012d3a2f0bf5093333bd906b989c029f5f
4 files changed +189 -10
apps/server/src/orchestration-v2/pullRequestWatch.test.ts
+149 -1
@@ -176,6 +176,11 @@ describe("evaluatePullRequestWatch", () => {
176 );
177 assert.deepEqual(failed.changes, [
178 { kind: "checks-failed", failed: [check("test", "failure")] },
179 + {
180 + kind: "checks-finished",
181 + passed: 0,
182 + failing: [check("lint", "failure"), check("test", "failure")],
183 + },
184 ]);
185 const clean = evaluatePullRequestWatch(
186 failed.next,
@@ -206,7 +211,7 @@ describe("evaluatePullRequestWatch", () => {
211 detail({ checks: [check("lint", "failure"), check("test", "success")] }),
212 noRemarks,
213 ).changes,
209 - [],
214 + [{ kind: "checks-finished", passed: 1, failing: [check("lint", "failure")] }],
215 );
216 assert.deepEqual(
217 evaluatePullRequestWatch(
@@ -653,6 +658,149 @@ describe("evaluatePullRequestWatch", () => {
658 assert.deepEqual(evaluatePullRequestWatch(final.next, lateResult, noRemarks).changes, []);
659 });
660
661 + // regression: #30530 a failure told while other checks ran left the agent waiting for them forever
662 + it.each([true, false])(
663 + "wakes once when the other checks finish after one failed (required: %s)",
664 + (required) => {
665 + const gated = (name: string, status: PullRequestCheck["status"]) => ({
666 + ...check(name, status),
667 + required,
668 + });
669 + const labels = gated("labels", "failure");
670 + const running = detail({
671 + checks: [labels, gated("test", "pending"), gated("docs", "pending")],
672 + });
673 + const failure = evaluatePullRequestWatch(watch(), running, noRemarks);
674 + assert.deepEqual(failure.changes, [{ kind: "checks-failed", failed: [labels] }]);
675 + assert.deepEqual(evaluatePullRequestWatch(failure.next, running, noRemarks).changes, []);
676 +
677 + const done = detail({ checks: [labels, gated("test", "success"), gated("docs", "skipped")] });
678 + const finished = evaluatePullRequestWatch(failure.next, done, noRemarks);
679 + assert.deepEqual(finished.changes, [
680 + { kind: "checks-finished", passed: 1, failing: [labels] },
681 + ]);
682 + assert.equal(
683 + pullRequestWatchMessage({
684 + number: 1,
685 + url: "https://github.com/o/r/pull/1",
686 + baseBranch: "main",
687 + headSha: finished.next.headSha,
688 + report: finished,
689 + }).notification.outcome,
690 + "failed",
691 + );
692 + assert.deepEqual(evaluatePullRequestWatch(finished.next, done, noRemarks).changes, []);
693 +
694 + // A later job running and passing is not more news.
695 + const botPending = detail({ checks: [...done.checks, check("bot", "pending")] });
696 + const churn = evaluatePullRequestWatch(finished.next, botPending, noRemarks);
697 + assert.deepEqual(churn.changes, []);
698 + const botDone = detail({ checks: [...done.checks, check("bot", "success")] });
699 + assert.deepEqual(evaluatePullRequestWatch(churn.next, botDone, noRemarks).changes, []);
700 +
701 + // A watch started after the failure is still owed the news that the rest finished.
702 + const late = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), running, noRemarks);
703 + assert.deepEqual(late.changes, []);
704 + assert.deepEqual(
705 + evaluatePullRequestWatch(late.next, done, noRemarks).changes,
706 + finished.changes,
707 + );
708 + },
709 + );
710 +
711 + it("says the checks finished only once every check has, a maintainer's approval included", () => {
712 + const required = (name: string, status: PullRequestCheck["status"]) => ({
713 + ...check(name, status),
714 + required: true,
715 + });
716 + const lint = required("lint", "failure");
717 + const unit = required("unit", "success");
718 + const failure = evaluatePullRequestWatch(
719 + watch(),
720 + detail({ checks: [lint, required("unit", "pending"), check("e2e", "pending")] }),
721 + noRemarks,
722 + );
723 + assert.deepEqual(failure.changes, [{ kind: "checks-failed", failed: [lint] }]);
724 +
725 + const advisory = detail({ checks: [lint, unit, check("e2e", "pending")] });
726 + const waiting = evaluatePullRequestWatch(failure.next, advisory, noRemarks);
727 + assert.deepEqual(waiting.changes, []);
728 +
729 + const approval = { ...check("deploy", "action-required"), url: run(1) };
730 + const held = detail({ checks: [lint, unit, check("e2e", "success"), approval] });
731 + const holding = evaluatePullRequestWatch(waiting.next, held, noRemarks);
732 + assert.deepEqual(holding.changes, [{ kind: "checks-awaiting-approval", checks: [approval] }]);
733 +
734 + const unknown = {
735 + ...check("Workflow approval status", "action-required"),
736 + url: null,
737 + placeholder: true,
738 + };
739 + const deployed = { ...approval, status: "success" as const };
740 + const e2e = check("e2e", "failure");
741 + const unread = evaluatePullRequestWatch(
742 + holding.next,
743 + detail({ checks: [lint, unit, check("e2e", "success"), deployed, unknown] }),
744 + noRemarks,
745 + );
746 + assert.deepEqual(unread.changes, []);
747 +
748 + // A failure that arrives with the last result shares its wake.
749 + const done = detail({ checks: [lint, unit, e2e, deployed] });
750 + const finished = evaluatePullRequestWatch(unread.next, done, noRemarks);
751 + assert.deepEqual(finished.changes, [
752 + { kind: "checks-failed", failed: [e2e] },
753 + { kind: "checks-finished", passed: 2, failing: [lint, e2e] },
754 + ]);
755 + assert.deepEqual(evaluatePullRequestWatch(finished.next, done, noRemarks).changes, []);
756 +
757 + // The wake is owed just the same when only an approval the read omits is outstanding.
758 + const gated = { ...check("deploy", "action-required"), url: run(2) };
759 + const asked = evaluatePullRequestWatch(
760 + watch(),
761 + detail({ checks: [required("lint", "pending"), gated] }),
762 + noRemarks,
763 + );
764 + const omitted = evaluatePullRequestWatch(asked.next, detail({ checks: [lint] }), noRemarks);
765 + assert.deepEqual(omitted.changes, [{ kind: "checks-failed", failed: [lint] }]);
766 + const job = { ...check("deploy-job", "success"), url: `${run(2)}/job/1` };
767 + assert.deepEqual(
768 + evaluatePullRequestWatch(omitted.next, detail({ checks: [lint, job] }), noRemarks).changes,
769 + [{ kind: "checks-finished", passed: 1, failing: [lint] }],
770 + );
771 + });
772 +
773 + it("wakes when a rerun finishes while another failure still blocks the checks", () => {
774 + const labels = check("labels", "failure");
775 + const test = check("test", "failure");
776 + const both = detail({ checks: [labels, test] });
777 + // Nothing else ran when these failed, so their own wake is the last word.
778 + const failed = evaluatePullRequestWatch(watch(), both, noRemarks);
779 + assert.deepEqual(failed.changes, [{ kind: "checks-failed", failed: [labels, test] }]);
780 + assert.deepEqual(evaluatePullRequestWatch(failed.next, both, noRemarks).changes, []);
781 +
782 + const rerun = evaluatePullRequestWatch(
783 + failed.next,
784 + detail({ checks: [labels, check("test", "pending")] }),
785 + noRemarks,
786 + );
787 + assert.deepEqual(rerun.changes, []);
788 + const recovered = detail({ checks: [labels, check("test", "success")] });
789 + assert.deepEqual(evaluatePullRequestWatch(rerun.next, recovered, noRemarks).changes, [
790 + { kind: "checks-finished", passed: 1, failing: [labels] },
791 + ]);
792 +
793 + // A rerun that starts and ends between two reads is the same news.
794 + assert.deepEqual(evaluatePullRequestWatch(failed.next, recovered, noRemarks).changes, [
795 + { kind: "checks-finished", passed: 1, failing: [labels] },
796 + ]);
797 +
798 + const pushed = detail({ ...recovered, headSha: "bbbbbbbbbb" });
799 + assert.deepEqual(evaluatePullRequestWatch(rerun.next, pushed, noRemarks).changes, [
800 + { kind: "checks-failed", failed: [labels] },
801 + ]);
802 + });
803 +
804 // regression: #16208 PR watches burned GitHub's rate limit and gave up
805 it("does not report passed again for a new passed check where none is required", () => {
806 const first = evaluatePullRequestWatch(
apps/server/src/orchestration-v2/pullRequestWatch.ts
+35 -7
@@ -17,6 +17,11 @@ const SNIPPET_LENGTH = 200;
17 export type PullRequestWatchChange =
18 | { readonly kind: "checks-failed"; readonly failed: ReadonlyArray<PullRequestCheck> }
19 | { readonly kind: "checks-passed"; readonly count: number; readonly required: boolean }
20 + | {
21 + readonly kind: "checks-finished";
22 + readonly passed: number;
23 + readonly failing: ReadonlyArray<PullRequestCheck>;
24 + }
25 | { readonly kind: "checks-awaiting-approval"; readonly checks: ReadonlyArray<PullRequestCheck> }
26 | { readonly kind: "remarks"; readonly remarks: ReadonlyArray<PullRequestComment> }
27 | { readonly kind: "no-checks"; readonly minutes: number }
@@ -64,6 +69,7 @@ export function evaluatePullRequestWatch(
69 let passed = headMoved ? false : watch.passed;
70 let passedChecks = headMoved ? [] : watch.passedChecks;
71 let owedChecks = headMoved ? [] : (watch.owedChecks ?? []);
72 + const owedBefore = owedChecks;
73 let awaitingApproval = headMoved ? [] : (watch.awaitingApproval ?? []);
74 let approvalAnnounced = headMoved ? false : (watch.approvalAnnounced ?? false);
75 // A watch with no answer (saved before the field, or since a push) waits only while the clock
@@ -74,13 +80,14 @@ export function evaluatePullRequestWatch(
80 const checks = detail.checks.filter((check) => check.placeholder !== true);
81 const failed = checks.filter(isFailedCheck);
82 const awaiting = checks.filter((check) => check.status === "action-required");
83 + const failedBefore = failedChecks;
84 + const newlyFailed = failed.filter((check) => !failedBefore.includes(check.name));
85 if (checks.length > 0) {
86 // A run awaiting approval waits on a person, like a review, so only running checks count.
87 // An unfinished baseline read keeps the seed below for the next one.
88 checksPending =
89 checks.some((check) => check.status === "pending") ||
90 (checksBaseline && unread && watch.checksPending === true);
83 - const newlyFailed = failed.filter((check) => !failedChecks.includes(check.name));
91 // The baseline hides failures from before the watch, but CI running at the start can fail
92 // before the first read.
93 if (newlyFailed.length > 0 && (!checksBaseline || watch.checksPending === true)) {
@@ -159,6 +166,24 @@ export function evaluatePullRequestWatch(
166 } else if (settled && owed.length > 0) {
167 changes.push({ kind: "checks-passed", count: checks.length, required: false });
168 owedChecks = [];
169 + } else if (gate.some(isFailedCheck)) {
170 + // A failed gate never sends the passed wake. A failure seen while other checks still run, or
171 + // a failed check that ran again, owes one wake once every check has finished. Only whether
172 + // the list is empty matters here, and the failed checks are never empty.
173 + const rerun = failedBefore.some((name) => !failedChecks.includes(name));
174 + const running =
175 + unread ||
176 + awaitingApproval.length > 0 ||
177 + checks.some((check) => check.status === "pending" || check.status === "action-required");
178 + if (running) {
179 + if (owedBefore.length > 0 || rerun || newlyFailed.length > 0) owedChecks = failedChecks;
180 + } else if (owedBefore.length > 0 || rerun) {
181 + changes.push({
182 + kind: "checks-finished",
183 + passed: checks.filter((check) => check.status === "success").length,
184 + failing: failed,
185 + });
186 + }
187 }
188 passed = passedNow;
189 passedChecks = passedNow ? gateNames : [];
@@ -357,19 +382,20 @@ function listed<T>(items: ReadonlyArray<T>, line: (item: T) => string): Array<st
382 return lines;
383 }
384
385 +const failedCheckLine = (check: PullRequestCheck) =>
386 + ` - ${check.name}${check.status === "failure" ? "" : ` (${check.status})`}${check.url ? ` ${check.url}` : ""}`;
387 +
388 function changeLines(
389 change: PullRequestWatchChange,
390 context: { readonly baseBranch: string; readonly commit: string },
391 ): Array<string> {
392 switch (change.kind) {
393 case "checks-failed":
394 + return [`- Checks failed${context.commit}:`, ...listed(change.failed, failedCheckLine)];
395 + case "checks-finished":
396 return [
367 - `- Checks failed${context.commit}:`,
368 - ...listed(
369 - change.failed,
370 - (check) =>
371 - ` - ${check.name}${check.status === "failure" ? "" : ` (${check.status})`}${check.url ? ` ${check.url}` : ""}`,
372 - ),
397 + `- Every check has finished${context.commit}: ${change.passed} passed, ${change.failing.length} still failing:`,
398 + ...listed(change.failing, failedCheckLine),
399 ];
400 case "checks-awaiting-approval":
401 return [
@@ -408,6 +434,7 @@ function changeLines(
434 const SUMMARY: Record<PullRequestWatchChange["kind"], string> = {
435 "checks-failed": "checks failed",
436 "checks-passed": "checks passed",
437 + "checks-finished": "checks finished",
438 "checks-awaiting-approval": "checks await approval",
439 remarks: "new comments",
440 "no-checks": "no checks reported",
@@ -503,6 +530,7 @@ export function pullRequestWatchMessage(input: {
530 const failed = changes.some(
531 (change) =>
532 change.kind === "checks-failed" ||
533 + change.kind === "checks-finished" ||
534 change.kind === "conflicting" ||
535 change.kind === "needs-rebase" ||
536 change.kind === "merge-queue-removed",
docs/user/source-control.md
+1 -1
@@ -338,7 +338,7 @@ review is terminal. An open or unsynced link keeps it active.
338
339 Ask the agent to watch, monitor, or babysit a pull request and it calls `watch_pull_request`. While
340 the thread is active, the server checks the pull request every two minutes and wakes the agent when a
341 -check fails or waits for a maintainer's approval, the required checks pass, someone else comments or reviews, or the branch starts to
341 +check fails or waits for a maintainer's approval, the required checks pass, the other checks finish after a failure, someone else comments or reviews, or the branch starts to
342 conflict. On github.com it also wakes the agent, with GitHub's reason, when the merge queue drops
343 the pull request without merging it. Threads in a project that watch the same pull request share one check. On GitHub, a check
344 first asks whether anything changed and reads the pull request only when it did, which keeps
packages/contracts/src/threadPullRequest.ts
+4 -1
@@ -88,7 +88,10 @@ export const ThreadPullRequestWatch = Schema.Struct({
88 passedChecks: Schema.Array(TrimmedNonEmptyString).pipe(
89 Schema.withDecodingDefault(Effect.succeed([])),
90 ),
91 - /** Checks still pending when required ones passed early, owed one wake once all settle. */
91 + /**
92 + * Marks one wake owed once every check has finished: the checks still pending when the required
93 + * ones passed early, or the failed ones while a failure keeps the required ones from passing.
94 + */
95 owedChecks: Schema.optional(Schema.Array(TrimmedNonEmptyString)),
96 /** Approval holds keyed by workflow run URL, or check name when no run URL is known. */
97 awaitingApproval: Schema.optional(Schema.Array(TrimmedNonEmptyString)),