diff --git a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts b/apps/server/src/mcp/toolkits/pullRequests/handlers.ts index f85d8f0abac4a..49089b85b703e 100644 --- a/apps/server/src/mcp/toolkits/pullRequests/handlers.ts +++ b/apps/server/src/mcp/toolkits/pullRequests/handlers.ts @@ -66,6 +66,7 @@ import { PullRequestsToolkit, type ThreadPullRequestEntry, alreadyEndedWatchNext, + checksAtStartWatchNext, CHILD_PULL_REQUEST_REPORT_NEXT, } from "./tools.ts"; @@ -332,10 +333,12 @@ const make = Effect.gen(function* () { return yield* watches .setWatching({ threadId, target: input, watching, until: input.until }) .pipe( - Effect.map((result) => - result.state === undefined - ? result - : { ...result, next: alreadyEndedWatchNext(result.state) }, + Effect.map(({ checksAtStart, ...result }) => + result.state !== undefined + ? { ...result, next: alreadyEndedWatchNext(result.state) } + : checksAtStart !== undefined + ? { ...result, next: checksAtStartWatchNext(checksAtStart) } + : result, ), Effect.catchTags({ PullRequestWatchThreadNotFoundError: (error) => diff --git a/apps/server/src/mcp/toolkits/pullRequests/tools.ts b/apps/server/src/mcp/toolkits/pullRequests/tools.ts index 28f3ce51c5452..9630c0009e9b3 100644 --- a/apps/server/src/mcp/toolkits/pullRequests/tools.ts +++ b/apps/server/src/mcp/toolkits/pullRequests/tools.ts @@ -269,6 +269,10 @@ export type WatchPullRequestResult = typeof WatchPullRequestResult.Type; export const alreadyEndedWatchNext = (state: "merged" | "closed") => `This PR is already ${state}, so there is nothing to watch. Report that and end your turn.`; +// The rollup can miss checks GitHub holds for approval, so the agent checks before it acts. +export const checksAtStartWatchNext = (checks: "passing" | "failing") => + `The checks rollup already showed ${checks} when this watch started, and no wake will report that. Check the PR's readiness now and act on it. The watch still wakes you on later changes.`; + export const ThreadPullRequestEntry = Schema.Struct({ ...PullRequestIdentity, source: ThreadPullRequestLinkSource, diff --git a/apps/server/src/orchestration-v2/pullRequestWatch.test.ts b/apps/server/src/orchestration-v2/pullRequestWatch.test.ts index d81d2348c5d21..8ed1196d67bff 100644 --- a/apps/server/src/orchestration-v2/pullRequestWatch.test.ts +++ b/apps/server/src/orchestration-v2/pullRequestWatch.test.ts @@ -195,7 +195,7 @@ describe("evaluatePullRequestWatch", () => { ]); }); - it("reports failures on the first read only when CI was pending at the start", () => { + it("reports failures and passes on the first read only when CI was pending at the start", () => { const red = detail({ checks: [check("lint", "failure"), check("test", "pending")] }); const pending = evaluatePullRequestWatch( watch({ awaitingBaseline: "all", checksPending: true }), @@ -221,6 +221,23 @@ describe("evaluatePullRequestWatch", () => { ).changes, [], ); + const green = detail({ checks: [check("lint", "success"), check("test", "success")] }); + assert.deepEqual( + evaluatePullRequestWatch( + watch({ awaitingBaseline: "all", checksPending: true }), + green, + noRemarks, + ).changes, + [{ kind: "checks-passed", count: 2, required: false }], + ); + assert.deepEqual( + evaluatePullRequestWatch( + watch({ awaitingBaseline: "all", checksPending: false }), + green, + noRemarks, + ).changes, + [], + ); // A read the host could not finish keeps the baseline and must keep the seed with it. const placeholder = { ...check("Workflow approval status", "pending"), placeholder: true }; diff --git a/apps/server/src/orchestration-v2/pullRequestWatch.ts b/apps/server/src/orchestration-v2/pullRequestWatch.ts index 5999996ec5dda..a0cd945d22131 100644 --- a/apps/server/src/orchestration-v2/pullRequestWatch.ts +++ b/apps/server/src/orchestration-v2/pullRequestWatch.ts @@ -154,7 +154,8 @@ export function evaluatePullRequestWatch( checks.every((check) => check.status !== "pending"); owedChecks = owed; if (passedNow && (!passed || gateGrew)) { - if (!checksBaseline) { + // Like a failure, a pass the first read finds is news when CI was pending at the start. + if (!checksBaseline || watch.checksPending === true) { changes.push({ kind: "checks-passed", count: gate.length, required: required.length > 0 }); } owedChecks = diff --git a/apps/server/src/pullRequest/PullRequestWatchService.test.ts b/apps/server/src/pullRequest/PullRequestWatchService.test.ts index edf4fee9c5c56..d3ed0cb6952f1 100644 --- a/apps/server/src/pullRequest/PullRequestWatchService.test.ts +++ b/apps/server/src/pullRequest/PullRequestWatchService.test.ts @@ -168,9 +168,12 @@ const makeHarness = Effect.fn("makePullRequestWatchHarness")(function* ( describe("PullRequestWatchService", () => { it.effect("links an unlinked watch even while the host has a transient read failure", () => Effect.gen(function* () { - const cases: ReadonlyArray = [ + const cases: ReadonlyArray< + readonly [HarnessOptions, boolean | undefined, ("passing" | "failing")?] + > = [ [{ readChecksState: "pending" }, true], - [{ readChecksState: "failing" }, false], + [{ readChecksState: "failing" }, false, "failing"], + [{ readChecksState: "passing" }, false, "passing"], [{}, false], [ { @@ -182,12 +185,13 @@ describe("PullRequestWatchService", () => { undefined, ], ]; - for (const [options, checksPending] of cases) { + for (const [options, checksPending, checksAtStart] of cases) { const harness = yield* makeHarness(options); const result = yield* harness.setWatching(true, { url: "https://github.com/t3tools/t3code/pull/9", }); expect(result).toMatchObject({ number: 9, wasWatching: false }); + expect(result.checksAtStart).toBe(checksAtStart); const commands = yield* Ref.get(harness.commands); expect(commands).toMatchObject([ { @@ -202,6 +206,30 @@ describe("PullRequestWatchService", () => { checksPending, ); } + + const rewatched = yield* makeHarness({ + readChecksState: "passing", + thread: makeThread([ + makeLink(9, { + endedWatch: { + startedAt: "2026-08-20T00:00:00.000Z", + headSha: "aaaaaaaaaa", + failedChecks: [], + passed: false, + passedChecks: [], + conflicting: false, + checksPending: true, + remarksThrough: "2026-08-20T00:00:00.000Z", + remarkIds: [], + wakes: 0, + }, + }), + ]), + }); + const again = yield* rewatched.setWatching(true, { + url: "https://github.com/t3tools/t3code/pull/9", + }); + expect(again.checksAtStart).toBeUndefined(); }), ); diff --git a/apps/server/src/pullRequest/PullRequestWatchService.ts b/apps/server/src/pullRequest/PullRequestWatchService.ts index 5e64ac4206896..0894158b3b386 100644 --- a/apps/server/src/pullRequest/PullRequestWatchService.ts +++ b/apps/server/src/pullRequest/PullRequestWatchService.ts @@ -68,6 +68,8 @@ export class PullRequestWatchService extends Context.Service< readonly wasWatching: boolean; /** Set when the PR already ended, so no watch was registered. */ readonly state?: "merged" | "closed"; + /** A first watch's finished rollup, which its baseline never reports. */ + readonly checksAtStart?: "passing" | "failing"; }, | PullRequestWatchFailedError | PullRequestWatchFromSubagentError @@ -130,6 +132,7 @@ const make = Effect.gen(function* () { if (watching && before?.snapshot?.state === "merged") return alreadyEnded("merged"); // A pending rollup has no failure yet, so any failure the first read finds is news. let checksPendingAtStart: boolean | undefined; + let checksAtStart: "passing" | "failing" | undefined; if (watching && !wasWatching) { const read = yield* pullRequests .summary( @@ -151,6 +154,13 @@ const make = Effect.gen(function* () { ); if (read?.state === "merged" || read?.state === "closed") return alreadyEnded(read.state); checksPendingAtStart = read === undefined ? undefined : read.checksState === "pending"; + // A watch again keeps its last watch's news, so it reports an untold result itself. + const watchedBefore = threadPullRequestsOf(thread).some( + (link) => threadPullRequestKeysEqual(link, target) && link.endedWatch !== undefined, + ); + if (!watchedBefore && (read?.checksState === "passing" || read?.checksState === "failing")) { + checksAtStart = read.checksState; + } } const uuid = yield* random.uuidV4().pipe(Effect.orDie); yield* engine @@ -180,6 +190,7 @@ const make = Effect.gen(function* () { ...target, watching: watchedLink(after)?.watch !== undefined, wasWatching, + ...(checksAtStart !== undefined && { checksAtStart }), }; });