TypeScript
perf(server): sending a message no longer rereads the thread from the database after settle, snooze or title updates (#28301)
Victor Vogelb796da9b7e78
diff --git a/apps/server/src/orchestration-v2/Orchestrator.ts b/apps/server/src/orchestration-v2/Orchestrator.tsindex 3d735bcd756..86f6cea5fdd 100644--- a/apps/server/src/orchestration-v2/Orchestrator.ts+++ b/apps/server/src/orchestration-v2/Orchestrator.ts@@ -5483,7 +5483,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio: now,updatedAt: now,};- yield* emit(+ const event = yield* emit(events,command,)({@@ -5493,7 +5493,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functiooccurredAt: now,payload: thread,});- projection = yield* getProjectionWithPendingEvents(command.threadId, events);+ projection = applyToProjection(projection, event);}// Background wakes and delegated completions continue work the user already snoozed, so// only a message someone actually sent ends the snooze.@@ -5507,7 +5507,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functiosnoozedAt: null,updatedAt: now,};- yield* emit(+ const event = yield* emit(events,command,)({@@ -5517,7 +5517,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functiooccurredAt: now,payload: thread,});- projection = yield* getProjectionWithPendingEvents(command.threadId, events);+ projection = applyToProjection(projection, event);}const userMessages = projection.messages.filter((message) => message.role === "user");const onlyMaintenanceHistory =@@ -5537,7 +5537,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functiotitleRegeneration: { requestId: command.commandId, startedAt: now },updatedAt: now,};- yield* emit(+ const event = yield* emit(events,command,)({@@ -5554,7 +5554,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functiomessageId: command.messageId,}),]);- projection = yield* getProjectionWithPendingEvents(command.threadId, events);+ projection = applyToProjection(projection, event);}const modelSelection = command.modelSelection ?? projection.thread.modelSelection;let dispatchMode = resolveMessageDispatchIntent(diff --git a/apps/server/src/orchestration-v2/runtimeLayer.test.ts b/apps/server/src/orchestration-v2/runtimeLayer.test.tsindex d48afee71ec..dbbdd5c66ba 100644--- a/apps/server/src/orchestration-v2/runtimeLayer.test.ts+++ b/apps/server/src/orchestration-v2/runtimeLayer.test.ts@@ -6940,7 +6940,7 @@ it.layer(layerScratchPromotionTest)("turning a No project thread into a project"});it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {- it.effect("carries snooze state through the V2 shell projection", () =>+ it.effect("carries snooze, settle and title state through the V2 shell projection", () =>Effect.gen(function* () {const projects = yield* ProjectService.ProjectService;const orchestrator = yield* Orchestrator.OrchestratorV2;@@ -7009,6 +7009,11 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {);const [candidate] = yield* projections.getSettlementCandidates(threadId);assert.deepEqual(candidate?.lastSnoozeWakeAt, yield* DateTime.now);+ yield* orchestrator.dispatch({+ type: "thread.settle",+ commandId: CommandId.make("runtime-layer-snoozed-thread-settle"),+ threadId,+ });yield* orchestrator.dispatch({type: "thread.snooze",commandId: CommandId.make("runtime-layer-snoozed-thread-snooze-after-wake"),@@ -7024,6 +7029,7 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {threadId,messageId: MessageId.make("runtime-layer-snoozed-message"),text: "Wake this thread.",+ titleSeed: "Woken thread",attachments: [],modelSelection,dispatchMode: { type: "start_immediately" },@@ -7032,6 +7038,13 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {const awakened = yield* orchestrator.getThreadProjection(threadId);assert.isNull(awakened.thread.snoozedUntil);assert.isNull(awakened.thread.snoozedAt);+ assert.isNull(awakened.thread.settledOverride);+ assert.isNull(awakened.thread.settledAt);+ assert.equal(awakened.thread.title, "Woken thread");+ assert.equal(+ awakened.thread.titleRegeneration?.requestId,+ CommandId.make("runtime-layer-snoozed-message"),+ );}),);