Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions apps/server/src/orchestration-v2/pullRequestWatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@ describe("evaluatePullRequestWatch", () => {
assert.deepEqual(evaluatePullRequestWatch(final.next, recovered, noRemarks).changes, []);
});

it("reports once when the checks still running at a failure finish", () => {
const lint = check("lint", "failure");
const running = detail({ checks: [lint, check("test", "pending"), check("e2e", "pending")] });
const failure = evaluatePullRequestWatch(watch(), running, noRemarks);
assert.deepEqual(failure.changes, [{ kind: "checks-failed", failed: [lint] }]);
const partial = detail({ checks: [lint, check("test", "success"), check("e2e", "pending")] });
const waiting = evaluatePullRequestWatch(failure.next, partial, noRemarks);
assert.deepEqual(waiting.changes, []);
const finished = detail({ checks: [lint, check("test", "success"), check("e2e", "success")] });
const settled = evaluatePullRequestWatch(waiting.next, finished, noRemarks);
assert.deepEqual(settled.changes, [{ kind: "checks-finished", count: 3, failed: [lint] }]);
assert.deepEqual(evaluatePullRequestWatch(settled.next, finished, noRemarks).changes, []);

const e2e = check("e2e", "failure");
const lastFailed = detail({ checks: [lint, check("test", "success"), e2e] });
const failedLast = evaluatePullRequestWatch(waiting.next, lastFailed, noRemarks);
assert.deepEqual(failedLast.changes, [{ kind: "checks-failed", failed: [e2e] }]);
assert.deepEqual(evaluatePullRequestWatch(failedLast.next, lastFailed, noRemarks).changes, []);
});

it("clears old-head owed checks when a new head is pushed", () => {
const required = { ...check("test", "success"), required: true };
const early = detail({ checks: [required, check("e2e", "pending")] });
Expand Down
39 changes: 30 additions & 9 deletions apps/server/src/orchestration-v2/pullRequestWatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const SNIPPET_LENGTH = 200;
export type PullRequestWatchChange =
| { readonly kind: "checks-failed"; readonly failed: ReadonlyArray<PullRequestCheck> }
| { readonly kind: "checks-passed"; readonly count: number; readonly required: boolean }
| {
readonly kind: "checks-finished";
readonly count: number;
readonly failed: ReadonlyArray<PullRequestCheck>;
}
| { readonly kind: "remarks"; readonly remarks: ReadonlyArray<PullRequestComment> }
| { readonly kind: "no-checks"; readonly minutes: number }
| { readonly kind: "conflicting" };
Expand Down Expand Up @@ -73,6 +78,10 @@ export function evaluatePullRequestWatch(
: [];
const settled =
failed.length === 0 && detail.checks.every((check) => check.status !== "pending");
const pending = detail.checks
.filter((check) => check.status === "pending")
.map((check) => check.name);
const owedAtFailure = owedChecks;
owedChecks = owed;
if (passedNow && (!passed || gateGrew)) {
changes.push({ kind: "checks-passed", count: gate.length, required: required.length > 0 });
Expand All @@ -85,6 +94,13 @@ export function evaluatePullRequestWatch(
} else if (settled && owed.length > 0) {
changes.push({ kind: "checks-passed", count: detail.checks.length, required: false });
owedChecks = [];
} else if (!passedNow && failed.length > 0) {
// A failure wake ends the turn while other checks run, so their finish is owed one wake.
owedChecks =
newlyFailed.length > 0 ? pending : owedAtFailure.filter((name) => pending.includes(name));
if (newlyFailed.length === 0 && owedAtFailure.length > 0 && owedChecks.length === 0) {
changes.push({ kind: "checks-finished", count: detail.checks.length, failed });
}
}
passed = passedNow;
passedChecks = passedNow ? gateNames : [];
Expand Down Expand Up @@ -176,24 +192,25 @@ function listed<T>(items: ReadonlyArray<T>, line: (item: T) => string): Array<st
return lines;
}

const checkLine = (check: PullRequestCheck) =>
` - ${check.name}${check.status === "failure" ? "" : ` (${check.status})`}${check.url ? ` ${check.url}` : ""}`;

function changeLines(
change: PullRequestWatchChange,
context: { readonly baseBranch: string; readonly commit: string },
): Array<string> {
switch (change.kind) {
case "checks-failed":
return [
`- Checks failed${context.commit}:`,
...listed(
change.failed,
(check) =>
` - ${check.name}${check.status === "failure" ? "" : ` (${check.status})`}${check.url ? ` ${check.url}` : ""}`,
),
];
return [`- Checks failed${context.commit}:`, ...listed(change.failed, checkLine)];
case "checks-passed":
return [
`- All ${change.count} ${change.required ? "required " : ""}${change.count === 1 ? "check" : "checks"} passed${context.commit}.`,
];
case "checks-finished":
return [
`- All ${change.count} ${change.count === 1 ? "check" : "checks"} finished${context.commit}, and ${change.failed.length} failed:`,
...listed(change.failed, checkLine),
];
case "remarks":
return [
`- ${change.remarks.length} new ${change.remarks.length === 1 ? "comment" : "comments"}:`,
Expand All @@ -216,6 +233,7 @@ function changeLines(
const SUMMARY: Record<PullRequestWatchChange["kind"], string> = {
"checks-failed": "checks failed",
"checks-passed": "checks passed",
"checks-finished": "checks finished",
remarks: "new comments",
"no-checks": "no checks reported",
conflicting: "merge conflict",
Expand All @@ -242,7 +260,10 @@ export function pullRequestWatchMessage(input: {
: "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.",
].join("\n");
const failed = changes.some(
(change) => change.kind === "checks-failed" || change.kind === "conflicting",
(change) =>
change.kind === "checks-failed" ||
change.kind === "checks-finished" ||
change.kind === "conflicting",
);
const summary = changes.map((change) => SUMMARY[change.kind]);
if (exhausted) summary.push("stopped watching");
Expand Down
4 changes: 2 additions & 2 deletions docs/user/source-control.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ review is terminal. An open or unsynced link keeps it active.

Ask the agent to watch, monitor, or babysit a pull request and it calls `watch_pull_request`. While
the thread is active, the server checks the pull request every two minutes and wakes the agent when a
check fails, the required checks pass, someone else comments or reviews, or the branch starts to
conflict. Threads in a project that watch the same pull request share one check. On GitHub, a check
check fails, the required checks pass, the checks still running after a failure finish, someone else
comments or reviews, or the branch starts to conflict. Threads in a project that watch the same pull request share one check. On GitHub, a check
first asks whether anything changed and reads the pull request only when it did, which keeps
watching inside GitHub's rate limit. Comments from your own account do not wake it. Watching ends
when the pull request merges or closes, after 10 wakes in a row that bring only comments, after 8
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/threadPullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export const ThreadPullRequestWatch = Schema.Struct({
passedChecks: Schema.Array(TrimmedNonEmptyString).pipe(
Schema.withDecodingDefault(Effect.succeed([])),
),
/** Checks still pending when required ones passed early, owed one wake once all settle. */
/** Checks still pending when required ones passed early or a failure was told, owed one wake once they settle. */
owedChecks: Schema.optional(Schema.Array(TrimmedNonEmptyString)),
remarksThrough: IsoDateTime,
/** Reported remarks at remarksThrough, so late arrivals at that timestamp still count. */
Expand Down
Loading