diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index c71addfa57c0..c1cd138c10a1 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -5254,8 +5254,14 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio ...(existing.endedWatch?.approvalAnnounced !== undefined && { approvalAnnounced: existing.endedWatch.approvalAnnounced, }), - // A finished-checks answer may be an older head's; a fresh read decides. - ...(existing.endedWatch?.checksPending === true && { checksPending: true }), + // CI pending with no failure yet at the start may fail before the first + // read. A caller's fresh read beats the link's last sync; a finished-checks + // answer kept from the last watch may be an older head's. + ...((command.type === "thread.pull-request.watch" && + command.checksPending !== undefined + ? command.checksPending + : existing.snapshot?.checksState === "pending" || + 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 c0ab327d41af..78d5c42e9a6d 100644 --- a/apps/server/src/orchestration-v2/pullRequestWatch.test.ts +++ b/apps/server/src/orchestration-v2/pullRequestWatch.test.ts @@ -190,6 +190,51 @@ describe("evaluatePullRequestWatch", () => { ]); }); + it("reports failures 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 }), + red, + noRemarks, + ); + assert.deepEqual(pending.changes, [ + { kind: "checks-failed", failed: [check("lint", "failure")] }, + ]); + assert.deepEqual( + evaluatePullRequestWatch( + pending.next, + detail({ checks: [check("lint", "failure"), check("test", "success")] }), + noRemarks, + ).changes, + [], + ); + assert.deepEqual( + evaluatePullRequestWatch( + watch({ awaitingBaseline: "all", checksPending: false }), + red, + 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 }; + const incomplete = evaluatePullRequestWatch( + watch({ awaitingBaseline: "all", checksPending: true }), + detail({ checks: [check("lint", "success"), placeholder] }), + noRemarks, + ); + assert.deepEqual(incomplete.changes, []); + assert.deepEqual( + evaluatePullRequestWatch( + incomplete.next, + detail({ checks: [check("lint", "success"), check("gated", "failure")] }), + noRemarks, + ).changes, + [{ kind: "checks-failed", failed: [check("gated", "failure")] }], + ); + }); + 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 eedbe47d1a01..a4afbb035b5d 100644 --- a/apps/server/src/orchestration-v2/pullRequestWatch.ts +++ b/apps/server/src/orchestration-v2/pullRequestWatch.ts @@ -76,9 +76,14 @@ export function evaluatePullRequestWatch( const awaiting = checks.filter((check) => check.status === "action-required"); if (checks.length > 0) { // A run awaiting approval waits on a person, like a review, so only running checks count. - checksPending = checks.some((check) => check.status === "pending"); + // An unfinished baseline read keeps the seed below for the next one. + checksPending = + checks.some((check) => check.status === "pending") || + (checksBaseline && unread && watch.checksPending === true); const newlyFailed = failed.filter((check) => !failedChecks.includes(check.name)); - if (!checksBaseline && newlyFailed.length > 0) { + // The baseline hides failures from before the watch, but CI running at the start can fail + // before the first read. + if (newlyFailed.length > 0 && (!checksBaseline || watch.checksPending === true)) { changes.push({ kind: "checks-failed", failed: newlyFailed }); } if (!approvalAnnounced && awaiting.length > 0) { diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index 0fedb489f451..f7e6e03bb460 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -4278,6 +4278,33 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { assert.isUndefined(yield* watchOf); const { messages } = yield* orchestrator.getThreadRecords(threadId, ["messages"]); assert.deepEqual(messages, []); + + // A client start sends no CI state, so the link's last sync seeds it. + yield* orchestrator.dispatch({ + type: "thread.pull-request-link.sync", + commandId: CommandId.make("pr-watch-ci-running"), + threadId, + ...key, + snapshot: { + state: "open", + title: "Watched pull request", + headBranch: "feature", + baseBranch: "main", + isDraft: false, + updatedAt: null, + syncedAt: "2026-10-02T12:30:00.000Z", + checksState: "pending", + }, + stack: null, + }); + yield* orchestrator.dispatch({ + type: "thread.pull-request.watch", + commandId: CommandId.make("pr-watch-client-start"), + threadId, + ...key, + watching: true, + }); + assert.isTrue((yield* watchOf)?.checksPending); }), ); @@ -5219,6 +5246,154 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { }), ); + it.effect.each([ + // Failures from before the watch stay baseline, even with checks still running. + { + label: "finished failing", + atStart: "failing", + reads: [ + { lint: "failure", test: "success" }, + { lint: "failure", test: "success" }, + ], + told: [[], []], + }, + { + label: "failing and still running", + atStart: "failing", + reads: [ + { lint: "failure", test: "pending" }, + { lint: "failure", test: "failure" }, + ], + told: [[], ["test"]], + }, + // Pending CI had no failure, so one the first read finds landed after the start. + { + label: "pending", + atStart: "pending", + reads: [ + { lint: "failure", test: "pending" }, + { lint: "failure", test: "failure" }, + ], + told: [["lint"], ["test"]], + }, + ] as const)( + "reports only failures after a watch that starts on $label CI", + ({ label, atStart, reads, told }) => + Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + const threadId = ThreadId.make(`runtime-pull-request-watch-start-${label}`); + const projectId = ProjectId.make("pr-watch-start-project"); + yield* seedProject({ + projectId, + title: "Watch start", + workspaceRoot: "/workspace/watch-start", + defaultModelSelection: null, + createdAt: "2026-10-01T00:00:00.000Z", + }); + yield* orchestrator.dispatch({ + type: "thread.create", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make(`pr-watch-start-create-${label}`), + threadId, + projectId, + title: "Watch start", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + }); + const key = { host: "github.com", repository: "pingdotgg/t3code", number: 13 }; + yield* orchestrator.dispatch({ + type: "thread.pull-request.link", + commandId: CommandId.make(`pr-watch-start-link-${label}`), + threadId, + ...key, + url: "https://github.com/pingdotgg/t3code/pull/13", + source: "manual", + }); + yield* orchestrator.dispatch({ + type: "thread.pull-request-link.sync", + commandId: CommandId.make(`pr-watch-start-sync-${label}`), + threadId, + ...key, + snapshot: { + state: "open", + title: "Watched pull request", + headBranch: "feature", + baseBranch: "main", + isDraft: false, + updatedAt: null, + syncedAt: "2026-10-02T12:00:00.000Z", + checksState: atStart, + }, + stack: null, + }); + // A client start, which carries no CI state of its own. + yield* orchestrator.dispatch({ + type: "thread.pull-request.watch", + commandId: CommandId.make(`pr-watch-start-watch-${label}`), + threadId, + ...key, + watching: true, + }); + + let read = 0; + const host = Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + Layer.mock(PullRequestService.PullRequestService)({ + detail: () => + Effect.sync(() => ({ + ...watchedPullRequestDetail({ + projectId, + number: 13, + at: "2026-10-02T12:00:00.000Z", + }), + headSha: "ad70160aaa", + checks: Object.entries(reads[read]!).map(([name, status]) => ({ + name, + status, + description: null, + url: null, + })), + })), + activity: () => + Effect.succeed({ + comments: [], + commentCount: 0, + commentsTruncated: false, + reviewThreads: [], + commits: [], + }), + }), + ); + const texts = Effect.map( + orchestrator.getThreadRecords(threadId, ["messages"]), + ({ messages }) => messages.map((message) => message.text), + ); + const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host)); + for (const [index, names] of told.entries()) { + read = index; + const before = (yield* texts).length; + if (index > 0) yield* TestClock.adjust("10 minutes"); + yield* reactor.sweep; + yield* worker.drain(); + const wakes = (yield* texts).slice(before); + assert.equal(wakes.length, names.length === 0 ? 0 : 1); + for (const name of ["lint", "test"]) { + assert.equal( + wakes.some((text) => text.includes(` - ${name}`)), + names.includes(name as never), + `read ${index} told ${name}`, + ); + } + } + }), + ); + it.effect("baselines a new watch across restarts and reports later changes", () => Effect.gen(function* () { const orchestrator = yield* Orchestrator.OrchestratorV2; diff --git a/apps/server/src/pullRequest/PullRequestWatchService.test.ts b/apps/server/src/pullRequest/PullRequestWatchService.test.ts index 2c4f4d92e469..3aa614ed8f29 100644 --- a/apps/server/src/pullRequest/PullRequestWatchService.test.ts +++ b/apps/server/src/pullRequest/PullRequestWatchService.test.ts @@ -114,6 +114,7 @@ interface HarnessOptions { readonly project?: OrchestrationProjectShell | null; readonly readFailure?: PullRequestService.PullRequestError; readonly readState?: PullRequestSummary["state"]; + readonly readChecksState?: PullRequestSummary["checksState"]; } const makeHarness = Effect.fn("makePullRequestWatchHarness")(function* ( @@ -144,6 +145,9 @@ const makeHarness = Effect.fn("makePullRequestWatchHarness")(function* ( title: "Pull request", url: `https://github.com/${input.repository}/pull/${input.number}`, state: options.readState ?? "open", + ...(options.readChecksState !== undefined && { + checksState: options.readChecksState, + }), headBranch: "feature", baseBranch: "main", updatedAt: "2026-08-20T00:00:00.000Z", @@ -164,21 +168,28 @@ 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* () { - for (const options of [ - {}, - { - readFailure: new PullRequestOperationError({ - operation: "summary", - detail: "The host is temporarily unavailable.", - }), - }, - ]) { + const cases: ReadonlyArray = [ + [{ readChecksState: "pending" }, true], + [{ readChecksState: "failing" }, false], + [{}, false], + [ + { + readFailure: new PullRequestOperationError({ + operation: "summary", + detail: "The host is temporarily unavailable.", + }), + }, + undefined, + ], + ]; + for (const [options, checksPending] 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(yield* Ref.get(harness.commands)).toMatchObject([ + const commands = yield* Ref.get(harness.commands); + expect(commands).toMatchObject([ { type: "thread.pull-request.watch", number: 9, @@ -186,6 +197,10 @@ describe("PullRequestWatchService", () => { link: { url: "https://github.com/t3tools/t3code/pull/9", source: "agent" }, }, ]); + const [command] = commands; + expect(command?.type === "thread.pull-request.watch" ? command.checksPending : null).toBe( + checksPending, + ); } }), ); diff --git a/apps/server/src/pullRequest/PullRequestWatchService.ts b/apps/server/src/pullRequest/PullRequestWatchService.ts index c50de1a847f9..b38075baeb7b 100644 --- a/apps/server/src/pullRequest/PullRequestWatchService.ts +++ b/apps/server/src/pullRequest/PullRequestWatchService.ts @@ -126,8 +126,10 @@ const make = Effect.gen(function* () { if (watching && before?.snapshot?.state === "merged") { return yield* new PullRequestNotOpenError({ state: "merged" }); } + // A pending rollup has no failure yet, so any failure the first read finds is news. + let checksPendingAtStart: boolean | undefined; if (watching && before?.watch === undefined) { - yield* pullRequests + checksPendingAtStart = yield* pullRequests .summary( { projectId: thread.projectId, @@ -142,11 +144,12 @@ const make = Effect.gen(function* () { Effect.flatMap((summary) => summary.state === "merged" ? Effect.fail(new PullRequestNotOpenError({ state: "merged" })) - : Effect.void, + : Effect.succeed(summary.checksState === "pending"), ), Effect.catchTags({ // Host and transport errors can recover while the watch is active. - PullRequestOperationError: () => Effect.void, + // A failed read leaves the seed to the link's last sync. + PullRequestOperationError: () => Effect.undefined, }), ); } @@ -162,6 +165,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 c5286116655d..f914656a202b 100644 --- a/packages/contracts/src/orchestrationV2.ts +++ b/packages/contracts/src/orchestrationV2.ts @@ -3323,6 +3323,8 @@ 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), + /** Whether CI was pending with no failure yet when the watch started, so any failure is news. */ + checksPending: Schema.optional(Schema.Boolean), link: Schema.optional( Schema.Struct({ url: TrimmedNonEmptyString,