Skip to content
TypeScript

perf(server): a slow host no longer delays other watched PRs' wakes (935 ms → 65 ms median) (#30291)

Andrei56a85428d5b4
diff --git a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
index 2b588b1d2e2..369bead0ec4 100644
--- a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
+++ b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts
@@ -24,6 +24,7 @@ import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schedule from "effect/Schedule";
import * as Schema from "effect/Schema";
+import * as Semaphore from "effect/Semaphore";
import type * as Scope from "effect/Scope";
import {
@@ -43,6 +44,8 @@ import {
/** Checks take minutes; faster sweeps waste the account's shared host quota. */
const SWEEP_MINUTES = 2;
const READ_FAILURE_LIMIT = 8;
+/** Group reads in flight at once, inside the host's own request limit. */
+const GROUP_READS = 4;
/** Without fingerprints, reread quiet PRs on snapshot changes or this interval to catch edits. */
const QUIET_REREAD_MS = 10 * 60_000;
/** Reread despite unchanged fingerprints; they miss review-thread comment edits. */
@@ -626,22 +629,26 @@ export const make = Effect.gen(function* () {
),
{ discard: true },
);
- const fingerprints = yield* Effect.forEach(groups, fingerprintOf, {
- concurrency: "unbounded",
- });
+ // Every fingerprint is asked for at once so the host can batch them, and each group reads
+ // as soon as its own answer lands rather than waiting on the slowest host.
+ const reads = yield* Semaphore.make(GROUP_READS);
yield* Effect.forEach(
groups,
- (group, index) => {
- const fingerprint = fingerprints[index]!;
- return fingerprint === "paused"
- ? Effect.void
- : readGroup(group, fingerprint).pipe(
- Effect.catchCause(
- logFailure("pull request watch check failed", { pullRequest: group.key }),
- ),
- );
- },
- { concurrency: 4, discard: true },
+ (group) =>
+ fingerprintOf(group).pipe(
+ Effect.flatMap((fingerprint) =>
+ fingerprint === "paused"
+ ? Effect.void
+ : reads.withPermit(
+ readGroup(group, fingerprint).pipe(
+ Effect.catchCause(
+ logFailure("pull request watch check failed", { pullRequest: group.key }),
+ ),
+ ),
+ ),
+ ),
+ ),
+ { concurrency: "unbounded", discard: true },
);
}).pipe(
Effect.catchCause(logFailure("pull request watch sweep failed", {})),
diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.ts
index 83f77d00730..af95d71e612 100644
--- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts
+++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts
@@ -4334,6 +4334,83 @@ it.layer(layerTest)("OrchestrationV2LayerLive lifecycle", (it) => {
}),
);
+ it.effect("reads each watched PR as soon as its own fingerprint lands", () =>
+ Effect.gen(function* () {
+ const orchestrator = yield* Orchestrator.OrchestratorV2;
+ const projectId = ProjectId.make("pr-watch-pipeline-project");
+ yield* seedProject({
+ projectId,
+ title: "Watch pipeline",
+ workspaceRoot: "/workspace/watch-pipeline",
+ defaultModelSelection: null,
+ createdAt: "2026-10-01T00:00:00.000Z",
+ });
+ for (const number of [21, 22]) {
+ const threadId = ThreadId.make(`runtime-pull-request-watch-pipeline-${number}`);
+ yield* orchestrator.dispatch({
+ type: "thread.create",
+ createdBy: "user",
+ creationSource: "web",
+ commandId: CommandId.make(`pr-watch-pipeline-create-${number}`),
+ threadId,
+ projectId,
+ title: "Watch pipeline",
+ modelSelection,
+ runtimeMode: "full-access",
+ interactionMode: "default",
+ branch: null,
+ worktreePath: null,
+ });
+ yield* orchestrator.dispatch({
+ type: "thread.pull-request.watch",
+ commandId: CommandId.make(`pr-watch-pipeline-start-${number}`),
+ threadId,
+ host: "github.com",
+ repository: "pingdotgg/t3code",
+ number,
+ watching: true,
+ link: { url: `https://github.com/pingdotgg/t3code/pull/${number}`, source: "agent" },
+ });
+ }
+ const at = "2026-10-02T12:00:00.000Z";
+ const texts = (number: number) =>
+ orchestrator
+ .getThreadRecords(ThreadId.make(`runtime-pull-request-watch-pipeline-${number}`), [
+ "messages",
+ ])
+ .pipe(Effect.map(({ messages }) => messages.map((message) => message.text).join("\n")));
+ // #21's host answers only after #22 has been read, so waiting on every fingerprint first
+ // never finishes.
+ const read22 = yield* Deferred.make<void>();
+ const host = Layer.mergeAll(
+ NodeServices.layer,
+ NodeCrypto.layer,
+ Layer.mock(PullRequestService.PullRequestService)({
+ watchFingerprint: (input) =>
+ input.number === 21
+ ? Deferred.await(read22).pipe(Effect.as(null))
+ : Effect.succeed(null),
+ detail: (input) =>
+ (input.number === 22 ? Deferred.succeed(read22, undefined) : Effect.void).pipe(
+ Effect.as(watchedPullRequestDetail({ projectId, number: input.number, at })),
+ ),
+ activity: () =>
+ Effect.succeed({
+ comments: [],
+ commentCount: 0,
+ commentsTruncated: false,
+ reviewThreads: [],
+ commits: [],
+ }),
+ }),
+ );
+ const reactor = yield* PullRequestWatchReactor.make.pipe(Effect.provide(host));
+ yield* reactor.sweep;
+ assert.include(yield* texts(21), "#21");
+ assert.include(yield* texts(22), "#22");
+ }),
+ );
+
it.effect("persists rejected command receipts across retries", () =>
Effect.gen(function* () {
const orchestrator = yield* Orchestrator.OrchestratorV2;