diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index c71addfa57c07..0d75998f19f3b 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -5255,7 +5255,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio approvalAnnounced: existing.endedWatch.approvalAnnounced, }), // A finished-checks answer may be an older head's; a fresh read decides. - ...(existing.endedWatch?.checksPending === true && { checksPending: true }), + // Prefer the host rollup from watch_pull_request so a pending→failed race + // before the first sweep still wakes; endedWatch only fills when that is absent. + ...(command.type === "thread.pull-request.watch" && + command.checksPending !== undefined + ? { checksPending: command.checksPending } + : existing.endedWatch?.checksPending === true + ? { checksPending: true } + : {}), ...(existing.endedWatch?.checksAwaitedSince !== undefined && { checksAwaitedSince: existing.endedWatch.checksAwaitedSince, }), diff --git a/apps/server/src/orchestration-v2/pullRequestWatch.test.ts b/apps/server/src/orchestration-v2/pullRequestWatch.test.ts index c0ab327d41af4..5737bb1a9eade 100644 --- a/apps/server/src/orchestration-v2/pullRequestWatch.test.ts +++ b/apps/server/src/orchestration-v2/pullRequestWatch.test.ts @@ -190,6 +190,50 @@ describe("evaluatePullRequestWatch", () => { ]); }); + it("reports failures that settle after a watch started while checks were pending", () => { + // Race: watch_pull_request while CI is running, then the pipeline fails before the first + // reactor sweep. Without checksPending seeded at start, awaitingBaseline would swallow this. + const red = detail({ + checks: [check("lint", "failure"), check("test", "failure")], + }); + const reported = evaluatePullRequestWatch( + watch({ awaitingBaseline: "all", checksPending: true }), + red, + noRemarks, + ); + assert.deepEqual(reported.changes, [ + { + kind: "checks-failed", + failed: [check("lint", "failure"), check("test", "failure")], + }, + ]); + assert.isFalse(reported.next.checksPending); + assert.isUndefined(reported.next.awaitingBaseline); + + // Already terminal when the watch started: still baselined (no wake). + assert.deepEqual( + evaluatePullRequestWatch( + watch({ awaitingBaseline: "all", checksPending: false }), + red, + noRemarks, + ).changes, + [], + ); + + // Still pending on the first read: keep baselining partial failures until they settle. + const partial = detail({ + checks: [check("lint", "failure"), check("test", "pending")], + }); + const waiting = evaluatePullRequestWatch( + watch({ awaitingBaseline: "all", checksPending: true }), + partial, + noRemarks, + ); + assert.deepEqual(waiting.changes, []); + assert.deepEqual(waiting.next.failedChecks, ["lint"]); + assert.isTrue(waiting.next.checksPending); + }); + it("baselines checks already awaiting approval", () => { const gated = detail({ checks: [{ ...check("CI", "action-required"), url: run(1) }] }); const baseline = evaluatePullRequestWatch(watch({ awaitingBaseline: "all" }), gated, noRemarks); diff --git a/apps/server/src/orchestration-v2/pullRequestWatch.ts b/apps/server/src/orchestration-v2/pullRequestWatch.ts index eedbe47d1a01f..19bad7614a355 100644 --- a/apps/server/src/orchestration-v2/pullRequestWatch.ts +++ b/apps/server/src/orchestration-v2/pullRequestWatch.ts @@ -78,7 +78,13 @@ export function evaluatePullRequestWatch( // A run awaiting approval waits on a person, like a review, so only running checks count. checksPending = checks.some((check) => check.status === "pending"); const newlyFailed = failed.filter((check) => !failedChecks.includes(check.name)); - if (!checksBaseline && newlyFailed.length > 0) { + // Baseline normally suppresses whatever was already terminal when the watch started. A watch + // that began while checks were still pending (seeded at watch_pull_request) must still wake + // when those checks settle to failure before the first reactor sweep — otherwise a fast CI + // fail is recorded as the baseline and never reported. + const settledAfterPendingStart = + checksBaseline && watch.checksPending === true && !checksPending && newlyFailed.length > 0; + if ((!checksBaseline || settledAfterPendingStart) && newlyFailed.length > 0) { changes.push({ kind: "checks-failed", failed: newlyFailed }); } if (!approvalAnnounced && awaiting.length > 0) { diff --git a/apps/server/src/pullRequest/PullRequestWatchService.ts b/apps/server/src/pullRequest/PullRequestWatchService.ts index c50de1a847f98..89766ec7620b5 100644 --- a/apps/server/src/pullRequest/PullRequestWatchService.ts +++ b/apps/server/src/pullRequest/PullRequestWatchService.ts @@ -126,8 +126,11 @@ const make = Effect.gen(function* () { if (watching && before?.snapshot?.state === "merged") { return yield* new PullRequestNotOpenError({ state: "merged" }); } + // When CI was still running (or the rollup was unread) at watch start, seed checksPending so + // a failure that lands before the first reactor sweep is reported instead of baselined away. + let checksPendingAtStart: boolean | undefined; if (watching && before?.watch === undefined) { - yield* pullRequests + checksPendingAtStart = yield* pullRequests .summary( { projectId: thread.projectId, @@ -142,11 +145,14 @@ const make = Effect.gen(function* () { Effect.flatMap((summary) => summary.state === "merged" ? Effect.fail(new PullRequestNotOpenError({ state: "merged" })) - : Effect.void, + : Effect.succeed( + // Unknown rollup: prefer pending so a fast fail still wakes. + summary.checksState === "pending" || summary.checksState == null, + ), ), Effect.catchTags({ // Host and transport errors can recover while the watch is active. - PullRequestOperationError: () => Effect.void, + PullRequestOperationError: () => Effect.succeed(true as const), }), ); } @@ -162,6 +168,9 @@ const make = Effect.gen(function* () { watching, ...(watching ? { link: { url: target.url, source: "agent" as const } } : {}), ...(watching && input.until !== undefined && { until: input.until }), + ...(watching && checksPendingAtStart !== undefined + ? { checksPending: checksPendingAtStart } + : {}), }) .pipe( Effect.catchCause((cause) => diff --git a/packages/contracts/src/orchestrationV2.ts b/packages/contracts/src/orchestrationV2.ts index c5286116655dd..5506747789c23 100644 --- a/packages/contracts/src/orchestrationV2.ts +++ b/packages/contracts/src/orchestrationV2.ts @@ -3323,6 +3323,12 @@ export const OrchestrationV2Command = Schema.Union([ watching: Schema.Boolean, /** With `watching`, sets how long the watch holds its thread; absent keeps the current one. */ until: Schema.optional(PullRequestWatchUntil), + /** + * Host check rollup when the watch starts. `true` when CI was still running (or unknown); + * `false` when it had already passed or failed. Seeds `checksPending` so a failure that + * settles before the first reactor sweep is still news, not a silent baseline. + */ + checksPending: Schema.optional(Schema.Boolean), link: Schema.optional( Schema.Struct({ url: TrimmedNonEmptyString,