Skip to content

Commit 91d7cfa

Browse files
only21milclaude
andcommitted
perf(server): sending a message no longer rereads the thread from the database after settle, snooze or title updates
dispatchMessage reran the 12-table command projection read and refolded every pending event after emitting thread.unsettled, thread.unsnoozed and the title-seed thread.metadata-updated. The projection it already holds plus the event emit returns is the same fold, so apply the event locally, as the lastVisitedAt branch already does. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 15fe283 commit 91d7cfa

2 files changed

Lines changed: 20 additions & 7 deletions

File tree

‎apps/server/src/orchestration-v2/Orchestrator.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5395,7 +5395,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
53955395
: now,
53965396
updatedAt: now,
53975397
};
5398-
yield* emit(
5398+
const event = yield* emit(
53995399
events,
54005400
command,
54015401
)({
@@ -5405,7 +5405,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54055405
occurredAt: now,
54065406
payload: thread,
54075407
});
5408-
projection = yield* getProjectionWithPendingEvents(command.threadId, events);
5408+
projection = applyToProjection(projection, event);
54095409
}
54105410
// Background wakes and delegated completions continue work the user already snoozed, so
54115411
// only a message someone actually sent ends the snooze.
@@ -5419,7 +5419,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54195419
snoozedAt: null,
54205420
updatedAt: now,
54215421
};
5422-
yield* emit(
5422+
const event = yield* emit(
54235423
events,
54245424
command,
54255425
)({
@@ -5429,7 +5429,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54295429
occurredAt: now,
54305430
payload: thread,
54315431
});
5432-
projection = yield* getProjectionWithPendingEvents(command.threadId, events);
5432+
projection = applyToProjection(projection, event);
54335433
}
54345434
const userMessages = projection.messages.filter((message) => message.role === "user");
54355435
const onlyMaintenanceHistory =
@@ -5449,7 +5449,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54495449
titleRegeneration: { requestId: command.commandId, startedAt: now },
54505450
updatedAt: now,
54515451
};
5452-
yield* emit(
5452+
const event = yield* emit(
54535453
events,
54545454
command,
54555455
)({
@@ -5466,7 +5466,7 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
54665466
messageId: command.messageId,
54675467
}),
54685468
]);
5469-
projection = yield* getProjectionWithPendingEvents(command.threadId, events);
5469+
projection = applyToProjection(projection, event);
54705470
}
54715471
const modelSelection = command.modelSelection ?? projection.thread.modelSelection;
54725472
let dispatchMode = resolveMessageDispatchIntent(

‎apps/server/src/orchestration-v2/runtimeLayer.test.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6828,7 +6828,7 @@ it.layer(layerScratchPromotionTest)("turning a No project thread into a project"
68286828
});
68296829

68306830
it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {
6831-
it.effect("carries snooze state through the V2 shell projection", () =>
6831+
it.effect("carries snooze, settle and title state through the V2 shell projection", () =>
68326832
Effect.gen(function* () {
68336833
const projects = yield* ProjectService.ProjectService;
68346834
const orchestrator = yield* Orchestrator.OrchestratorV2;
@@ -6897,6 +6897,11 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {
68976897
);
68986898
const [candidate] = yield* projections.getSettlementCandidates(threadId);
68996899
assert.deepEqual(candidate?.lastSnoozeWakeAt, yield* DateTime.now);
6900+
yield* orchestrator.dispatch({
6901+
type: "thread.settle",
6902+
commandId: CommandId.make("runtime-layer-snoozed-thread-settle"),
6903+
threadId,
6904+
});
69006905
yield* orchestrator.dispatch({
69016906
type: "thread.snooze",
69026907
commandId: CommandId.make("runtime-layer-snoozed-thread-snooze-after-wake"),
@@ -6912,6 +6917,7 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {
69126917
threadId,
69136918
messageId: MessageId.make("runtime-layer-snoozed-message"),
69146919
text: "Wake this thread.",
6920+
titleSeed: "Woken thread",
69156921
attachments: [],
69166922
modelSelection,
69176923
dispatchMode: { type: "start_immediately" },
@@ -6920,6 +6926,13 @@ it.layer(layerSharedApplicationDataPlaneTest)("snooze projection", (it) => {
69206926
const awakened = yield* orchestrator.getThreadProjection(threadId);
69216927
assert.isNull(awakened.thread.snoozedUntil);
69226928
assert.isNull(awakened.thread.snoozedAt);
6929+
assert.isNull(awakened.thread.settledOverride);
6930+
assert.isNull(awakened.thread.settledAt);
6931+
assert.equal(awakened.thread.title, "Woken thread");
6932+
assert.equal(
6933+
awakened.thread.titleRegeneration?.requestId,
6934+
CommandId.make("runtime-layer-snoozed-message"),
6935+
);
69236936
}),
69246937
);
69256938

0 commit comments

Comments
 (0)