diff --git a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts index 5b69a7efa3377..27eb6c73f7814 100644 --- a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts +++ b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts @@ -46,6 +46,8 @@ import { /** Checks take minutes; faster sweeps waste the account's shared host quota. */ const SWEEP_MINUTES = 2; +/** Between sweeps, pending checks get a fingerprint probe this often; a full read only follows a change. */ +const PENDING_PROBE_MS = 30_000; const READ_FAILURE_LIMIT = 8; /** Group reads in flight at once, inside the host's own request limit. */ const GROUP_READS = 4; @@ -88,6 +90,8 @@ interface LastRead { readonly at: number; readonly fingerprint: string; readonly inFlight: boolean; + /** Some watch still waits on checks, including a head whose checks have not registered yet. */ + readonly checksPending: boolean; readonly remarksComplete: boolean; readonly watches: ReadonlySet; readonly status: string | null; @@ -681,6 +685,12 @@ export const make = Effect.gen(function* () { fingerprint === null ? QUIET_REREAD_MS : FINGERPRINT_REREAD_MS, ) : null; + const reports = new Map( + group.targets.map((target) => [ + target, + evaluatePullRequestWatch(target.watch, detail, remarks, now), + ]), + ); lastReads.set(group.key, { at: plan.activity || last === undefined @@ -690,13 +700,14 @@ export const make = Effect.gen(function* () { inFlight: detail.mergeability === "unknown" || detail.checks.some((check) => check.status === "pending"), + checksPending: [...reports.values()].some((report) => report.next.checksPending === true), remarksComplete: plan.activity ? remarks !== null : (last?.remarksComplete ?? false), watches: new Set(group.targets.map(watchKey)), status: fingerprint?.status ?? null, remarks: fingerprint?.remarks ?? null, }); yield* eachTarget(group, (target) => { - const report = evaluatePullRequestWatch(target.watch, detail, remarks, now); + const report = reports.get(target)!; if (report.changes.length > 0) { const wake = { ...pullRequestWatchMessage({ @@ -839,6 +850,34 @@ export const make = Effect.gen(function* () { Effect.withSpan("PullRequestWatchReactor.readOnChange"), ); + // Hosts without fingerprints are left to the sweep, since every probe there would be a full read. + // One pass per moved pull request covers every project watching it. + const probePending = Effect.gen(function* () { + const moved = new Set(); + yield* Effect.forEach( + currentGroups.filter((group) => { + const last = lastReads.get(group.key); + return last !== undefined && (last.inFlight || last.checksPending) && last.status !== null; + }), + (group) => + fingerprintOf(group).pipe( + Effect.map((fingerprint) => { + const last = lastReads.get(group.key); + if ( + fingerprint !== null && + fingerprint !== "paused" && + last !== undefined && + (last.status !== fingerprint.status || last.remarks !== fingerprint.remarks) + ) { + moved.add(pullRequestOf(group)); + } + }), + ), + { concurrency: "unbounded", discard: true }, + ); + yield* Effect.forEach(moved, readOnChange, { concurrency: "unbounded", discard: true }); + }); + const start: PullRequestWatchReactor["Service"]["start"] = Effect.fn( "PullRequestWatchReactor.start", )(function* () { @@ -870,6 +909,9 @@ export const make = Effect.gen(function* () { Effect.asVoid, ), ); + // A host without a live feed leaves the sweep as the only reader, so a pending watch + // would wait the whole sweep for its checks; the probes catch the conclusion sooner. + yield* forkParked(probePending.pipe(Effect.delay(PENDING_PROBE_MS), Effect.forever)); }); return { start, sweep } satisfies PullRequestWatchReactor["Service"]; diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts index 0614a14861abd..2562cafd7b19f 100644 --- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts +++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts @@ -5779,6 +5779,137 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => { }), ); + // A fresh head has no checks yet; the watch waits for them all the same. + it.effect.each(["pending", "not yet registered"] as const)( + "wakes %s checks from the interim probe when the host has no live feed", + (start) => + Effect.gen(function* () { + const orchestrator = yield* Orchestrator.OrchestratorV2; + const threadId = ThreadId.make(`runtime-pull-request-watch-probe-${start}`); + const projectId = ProjectId.make(`pr-watch-probe-project-${start}`); + yield* seedProject({ + projectId, + title: "Watch probe", + workspaceRoot: "/workspace/watch-probe", + defaultModelSelection: null, + createdAt: "2026-10-01T00:00:00.000Z", + }); + yield* orchestrator.dispatch({ + type: "thread.create", + createdBy: "user", + creationSource: "web", + commandId: CommandId.make(`pr-watch-probe-create-${start}`), + threadId, + projectId, + title: "Watch probe", + modelSelection, + runtimeMode: "full-access", + interactionMode: "default", + branch: null, + worktreePath: null, + }); + const key = { host: "github.com", repository: "pingdotgg/t3code", number: 14 }; + yield* orchestrator.dispatch({ + type: "thread.pull-request.watch", + commandId: CommandId.make(`pr-watch-probe-start-${start}`), + threadId, + ...key, + watching: true, + link: { url: "https://github.com/pingdotgg/t3code/pull/14", source: "agent" }, + }); + + let checks: PullRequestDetail["checks"] = + start === "pending" + ? [{ name: "lint", status: "pending", description: null, url: null, required: true }] + : []; + let status: string = start; + const detailReads = yield* Queue.unbounded(); + const fingerprintReads = yield* Queue.unbounded(); + const host = Layer.mergeAll( + NodeServices.layer, + NodeCrypto.layer, + Layer.mock(PullRequestService.PullRequestService)({ + detail: (input) => + // Watches left by earlier cases in this layer are swept too. + (input.projectId === projectId + ? Queue.offer(detailReads, undefined) + : Effect.void + ).pipe( + Effect.as({ + ...watchedPullRequestDetail({ + projectId, + number: 14, + at: "2026-10-02T12:00:00.000Z", + }), + headSha: "6074dd48ff", + checks, + }), + ), + activity: () => + Effect.succeed({ + comments: [], + commentCount: 0, + commentsTruncated: false, + reviewThreads: [], + commits: [], + }), + watchFingerprint: (input) => + (input.projectId === projectId + ? Queue.offer(fingerprintReads, undefined) + : Effect.void + ).pipe(Effect.andThen(Effect.sync(() => ({ status, remarks: "none" })))), + invalidate: () => Effect.void, + // Without the GitHub app sign-in no feed is on, so only the sweep hears checks. + changes: () => Stream.empty, + }), + ); + const wakes = Effect.map( + orchestrator.getThreadRecords(threadId, ["messages"]), + ({ messages }) => + messages.filter( + (message) => message.notification !== undefined && message.notification !== null, + ), + ); + const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host)); + const worker = yield* EffectWorker.OrchestrationEffectWorkerV2; + yield* reactor.start(); + yield* Queue.take(fingerprintReads); + yield* Queue.take(detailReads); + yield* worker.drain(); + assert.lengthOf(yield* wakes, 0); + + // A probe whose fingerprint has not moved reads nothing more. + yield* TestClock.adjust("30 seconds"); + yield* Queue.take(fingerprintReads); + status = "passed"; + checks = [ + { name: "lint", status: "success", description: null, url: null, required: true }, + ]; + // The probe must hear the concluded checks long before the 2-minute sweep could. + yield* TestClock.adjust("30 seconds"); + yield* Queue.take(detailReads); + yield* worker.drain(); + const woken = yield* wakes; + assert.lengthOf(woken, 1); + assert.equal(woken[0]!.notification?.summary, "#14: checks passed"); + assert.equal(yield* Queue.size(detailReads), 0); + + // The settled pull request stays quiet until the sweep. + yield* TestClock.adjust("30 seconds"); + yield* worker.drain(); + assert.lengthOf(yield* wakes, 1); + + yield* orchestrator.dispatch({ + type: "thread.pull-request.watch", + commandId: CommandId.make(`pr-watch-probe-end-${start}`), + threadId, + ...key, + watching: false, + }); + yield* worker.drain(); + }), + ); + // regression: #21155 watching a pull request again repeated news the thread already got it.effect("watching a dismissed stack layer again keeps what its last watch told", () => Effect.gen(function* () {