From 0f23473ad99ebb4fbd8ce8115f700a836e487ec0 Mon Sep 17 00:00:00 2001 From: Andrey170170 Date: Fri, 13 Nov 2026 09:37:48 -0700 Subject: [PATCH] perf(server): PR watch folds no longer read every run of the thread The reactor's fold check and the orchestrator's queued-wake rewrite each loaded all of a thread's runs to find one wake. The reactor now reads only queued runs, and the orchestrator finds the wake by its message ID. Co-Authored-By: Claude Opus 5.5 --- .../src/orchestration-v2/Orchestrator.ts | 39 +++++++------------ .../PullRequestWatchReactor.ts | 6 ++- 2 files changed, 19 insertions(+), 26 deletions(-) diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.ts index e873ce2a8d264..c9e096bf02f58 100644 --- a/apps/server/src/orchestration-v2/Orchestrator.ts +++ b/apps/server/src/orchestration-v2/Orchestrator.ts @@ -168,6 +168,7 @@ import { type ProjectionTimelinePageOptions, type ProjectionRecordFilter, type ProjectionRecords, + type ProjectionStoreV2Error, type ProjectionCheckpointContext, type OlderSettledThreadsListing, type ActiveShellIndexEntry, @@ -3802,18 +3803,22 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio cause: "The pull request watch ended or its thread settled while it was read.", }); } + const projectionError = (cause: ProjectionStoreV2Error) => + new OrchestratorProjectionError({ threadId: command.threadId, cause }); // A wake that reuses a queued wake's message folds into it rather than queuing another run. - const replacedRun = + const replacedMessage = wake === undefined ? undefined : (yield* projectionStore - .getThreadRecords(command.threadId, ["runs"]) - .pipe( - Effect.mapError( - (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }), - ), - )).runs.find((run) => run.userMessageId === wake.messageId); - if (replacedRun !== undefined && replacedRun.status !== "queued") { + .getMessagesById(command.threadId, [wake.messageId]) + .pipe(Effect.mapError(projectionError))).get(wake.messageId); + const replacedRun = + replacedMessage?.runId == null + ? undefined + : yield* projectionStore + .getRun(command.threadId, replacedMessage.runId) + .pipe(Effect.mapError(projectionError)); + if (replacedMessage !== undefined && replacedRun?.status !== "queued") { return yield* new OrchestratorDispatchError({ commandId: command.commandId, commandType: command.type, @@ -3847,21 +3852,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio } : wake.notification.source, }; - if (replacedRun !== undefined) { - const message = yield* projectionStore - .getRunMessage(command.threadId, replacedRun.id) - .pipe( - Effect.mapError( - (cause) => new OrchestratorProjectionError({ threadId: command.threadId, cause }), - ), - ); - if (message === undefined) { - return yield* new OrchestratorDispatchError({ - commandId: command.commandId, - commandType: command.type, - cause: `Queued run ${replacedRun.id} has no user message.`, - }); - } + if (replacedMessage !== undefined && replacedRun !== undefined) { const now = yield* DateTime.now; yield* emit( events, @@ -3874,7 +3865,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio providerInstanceId: replacedRun.providerInstanceId, occurredAt: now, payload: { - ...message, + ...replacedMessage, text: wakeMessage.text, notification, updatedAt: now, diff --git a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts index 521ca27aed3d7..42edef3f7fc39 100644 --- a/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts +++ b/apps/server/src/orchestration-v2/PullRequestWatchReactor.ts @@ -396,8 +396,10 @@ export const make = Effect.gen(function* () { const key = lifeKey(target); const queued = queuedWakes.get(key); if (queued === undefined) return undefined; - const { runs } = yield* projections.getThreadRecords(target.thread.id, ["runs"]); - if (runs.some((run) => run.userMessageId === queued.messageId && run.status === "queued")) { + const { runs } = yield* projections.getThreadRecords(target.thread.id, ["runs"], { + runStatuses: ["queued"], + }); + if (runs.some((run) => run.userMessageId === queued.messageId)) { return queued; } queuedWakes.delete(key);