Skip to content

Commit 647bd14

Browse files
authored
fix(server): avoid message ordering scans during recovery and settlement (#30582)
1 parent 059804d commit 647bd14

6 files changed

Lines changed: 115 additions & 21 deletions

File tree

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3331,16 +3331,19 @@ const makeOrchestrator = Effect.fn("orchestrationV2.Orchestrator.layer")(functio
33313331
}
33323332
}
33333333
if (command.type === "thread.settle") {
3334-
const projection = yield* loadProjectionForCommand(
3335-
command,
3336-
["runs", "runtimeRequests", "messages"],
3337-
{ turnItemTypes: [], messageRoles: ["user"] },
3338-
);
3334+
const projection = yield* loadProjectionForCommand(command, ["runs", "runtimeRequests"]);
3335+
const queuedMessages = yield* projectionStore
3336+
.getMessagesById(
3337+
command.threadId,
3338+
projection.runs.filter((run) => run.status === "queued").map((run) => run.userMessageId),
3339+
)
3340+
.pipe(mapDispatchError(command));
33393341
const automaticMessageIds = new Set(
3340-
projection.messages
3342+
[...queuedMessages.values()]
33413343
.filter(
33423344
(message) =>
3343-
message.notification !== undefined || message.delegatedCompletion !== undefined,
3345+
message.role === "user" &&
3346+
(message.notification !== undefined || message.delegatedCompletion !== undefined),
33443347
)
33453348
.map((message) => message.id),
33463349
);

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

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,27 @@ it.effect("routes full projection reads to readers and retains uncommitted write
282282
forkedFrom: { type: "run", threadId: parentId, runId: parent.runs[0]!.id },
283283
},
284284
});
285+
const messageId = MessageId.make("message:reader-routing");
286+
yield* store.apply({
287+
id: EventId.make("event:reader-routing:message"),
288+
type: "message.updated",
289+
threadId,
290+
occurredAt: parent.thread.createdAt,
291+
payload: {
292+
id: messageId,
293+
threadId,
294+
runId: null,
295+
nodeId: null,
296+
role: "user",
297+
text: "Committed message",
298+
attachments: [],
299+
streaming: false,
300+
createdBy: "user",
301+
creationSource: "web",
302+
createdAt: parent.thread.createdAt,
303+
updatedAt: parent.thread.createdAt,
304+
},
305+
});
285306
const { queries, trace } = traceSqlRouting();
286307
yield* store.getAgentAwarenessSnapshot().pipe(trace);
287308
const projection = yield* store.getThreadProjection(threadId).pipe(trace);
@@ -292,6 +313,10 @@ it.effect("routes full projection reads to readers and retains uncommitted write
292313
.map((row) => row.sourceItemId),
293314
parent.turnItems.map((item) => item.id),
294315
);
316+
assert.equal(
317+
(yield* store.getMessagesById(threadId, [messageId]).pipe(trace)).get(messageId)?.text,
318+
"Committed message",
319+
);
295320
assert.isAbove(queries.length, 1);
296321
assert.deepEqual([...new Set(queries.map((row) => row.connection))], ["reader"]);
297322
const commitRequested = yield* Deferred.make<void>();
@@ -343,6 +368,15 @@ it.effect("routes full projection reads to readers and retains uncommitted write
343368
SET payload_json = json_set(payload_json, '$.title', 'Uncommitted title')
344369
WHERE thread_id = ${threadId}
345370
`;
371+
yield* sql`
372+
UPDATE orchestration_v2_projection_messages
373+
SET payload_json = json_set(payload_json, '$.text', 'Uncommitted message')
374+
WHERE message_id = ${messageId}
375+
`;
376+
assert.equal(
377+
(yield* store.getMessagesById(threadId, [messageId])).get(messageId)?.text,
378+
"Uncommitted message",
379+
);
346380
assert.equal(
347381
(yield* store.getAgentAwarenessSnapshot()).threads.find(
348382
(thread) => thread.id === threadId,
@@ -2638,10 +2672,28 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
26382672
);
26392673
yield* insertMessageItem("null-id", threadId, -2, null);
26402674
yield* insertMessageItem("missing-id", threadId, -3, undefined);
2675+
const absentMessageId = MessageId.make("message:bounded-sql-history:absent");
2676+
const messageReadTrace = traceSqlStatements();
2677+
const selected = yield* projectionStore
2678+
.getMessagesById(threadId, [firstMessageId, lastMessageId, firstMessageId, absentMessageId])
2679+
.pipe(Effect.withTracer(messageReadTrace.tracer));
2680+
assert.isFalse(messageReadTrace.statements.some((query) => query.includes("turn_items")));
2681+
assert.equal(selected.size, 2);
2682+
assert.equal(selected.get(firstMessageId)?.id, firstMessageId);
2683+
assert.equal(selected.get(lastMessageId)?.id, lastMessageId);
2684+
assert.isFalse(selected.has(absentMessageId));
2685+
assert.equal((yield* projectionStore.getMessagesById(threadId, [])).size, 0);
2686+
assert.equal(
2687+
(yield* projectionStore.getMessagesById(ThreadId.make("other-thread"), [firstMessageId]))
2688+
.size,
2689+
0,
2690+
);
26412691
const readMessageIds = (filter?: ProjectionStore.ProjectionRecordFilter) =>
26422692
projectionStore
26432693
.getThreadRecords(threadId, ["messages"], filter)
26442694
.pipe(Effect.map(({ messages }) => messages.map((message) => message.id)));
2695+
const canonical = yield* projectionStore.getThreadRecords(threadId, ["messages"]);
2696+
for (const message of canonical.messages) assert.deepEqual(selected.get(message.id), message);
26452697
assert.deepEqual(yield* readMessageIds(), [lastMessageId, firstMessageId]);
26462698
assert.deepEqual(yield* readMessageIds({ messageRoles: ["user", "assistant"] }), [
26472699
lastMessageId,

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,13 @@ export interface ProjectionStoreV2Shape {
473473
readonly threadId: ThreadId;
474474
readonly itemId: TurnItemId;
475475
}) => Effect.Effect<OrchestrationV2TurnItem | null, ProjectionStoreV2Error>;
476+
readonly getMessagesById: (
477+
threadId: ThreadId,
478+
messageIds: ReadonlyArray<MessageId>,
479+
) => Effect.Effect<
480+
ReadonlyMap<MessageId, OrchestrationV2ConversationMessage>,
481+
ProjectionStoreV2Error
482+
>;
476483
readonly getThreadRecords: <K extends ProjectionRecordField>(
477484
threadId: ThreadId,
478485
fields: ReadonlyArray<K>,
@@ -5563,6 +5570,23 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
55635570
Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })),
55645571
);
55655572

5573+
const getMessagesById = Effect.fn("ProjectionStore.getMessagesById")(
5574+
function* (threadId: ThreadId, messageIds: ReadonlyArray<MessageId>) {
5575+
if (messageIds.length === 0)
5576+
return new Map<MessageId, OrchestrationV2ConversationMessage>();
5577+
const rows = yield* sql<PayloadRow>`
5578+
SELECT message.payload_json
5579+
FROM json_each(${encodeIdList([...new Set(messageIds)])}) AS requested
5580+
CROSS JOIN orchestration_v2_projection_messages AS message
5581+
ON message.message_id = requested.value
5582+
WHERE message.thread_id = ${threadId}
5583+
`;
5584+
const messages = yield* decodeRows(decodeMessagePayload, threadId)(rows);
5585+
return new Map(messages.map((message) => [message.id, message]));
5586+
},
5587+
(effect, threadId) => effect.pipe(Effect.mapError(controlReadError(threadId))),
5588+
);
5589+
55665590
const getThreadRecords: ProjectionStoreV2Shape["getThreadRecords"] = (
55675591
threadId,
55685592
fields,
@@ -7385,6 +7409,7 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
73857409
listAppOwnedChildThreadIds: readOnReader(listAppOwnedChildThreadIds),
73867410
getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal),
73877411
getTurnItem: readOnReader(getTurnItem),
7412+
getMessagesById: readOnReader(getMessagesById),
73887413
getThreadRecords: (threadId, fields, filter) =>
73897414
getThreadRecords(threadId, fields, filter).pipe(NodeSqliteClient.readOnly),
73907415
getRuntimeRequest: readOnReader(getRuntimeRequest),

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,16 @@ it.effect.each([
601601
Layer.mock(ProjectionStore.ProjectionStoreV2)({
602602
getRecoveryThreadIds: () => Effect.succeed([threadId]),
603603
getRuntimeRecoveryProjection: () => Effect.succeed(projection),
604+
getMessagesById: (requestedThreadId, messageIds) =>
605+
Effect.succeed(
606+
new Map(
607+
projection.messages
608+
.filter(
609+
(message) => requestedThreadId === threadId && messageIds.includes(message.id),
610+
)
611+
.map((message) => [message.id, message]),
612+
),
613+
),
604614
getThreadRecords: () => Effect.succeed(projection),
605615
}),
606616
Layer.mock(EventSink.EventSinkV2)({

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

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,23 @@ export const make = Effect.gen(function* () {
342342
const queuedMessages =
343343
pendingRuns.length === 0
344344
? []
345-
: (yield* projections
346-
.getThreadRecords(projection.thread.id, ["messages"], {
347-
messageIds: pendingRuns.map((run) => run.userMessageId),
348-
})
349-
.pipe(
350-
Effect.mapError(
351-
(cause) =>
352-
new ProviderRuntimeRecoveryError({
353-
operation: "read-projections",
354-
threadId: projection.thread.id,
355-
cause,
356-
}),
357-
),
358-
)).messages;
345+
: [
346+
...(yield* projections
347+
.getMessagesById(
348+
projection.thread.id,
349+
pendingRuns.map((run) => run.userMessageId),
350+
)
351+
.pipe(
352+
Effect.mapError(
353+
(cause) =>
354+
new ProviderRuntimeRecoveryError({
355+
operation: "read-projections",
356+
threadId: projection.thread.id,
357+
cause,
358+
}),
359+
),
360+
)).values(),
361+
];
359362
const bufferedMessageIds = new Set(
360363
queuedMessages.filter(isBufferedProviderNotification).map((message) => message.id),
361364
);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@ it.effect(
241241
listAppOwnedChildThreadIds: () => Effect.die("unused listAppOwnedChildThreadIds"),
242242
getNextTurnItemOrdinal: () => Effect.die("unused ordinal read"),
243243
getTurnItem: () => Effect.die("unused turn item read"),
244+
getMessagesById: () => Effect.die("unused getMessagesById"),
244245
getThreadRecords: () => Effect.die("unused record read"),
245246
getRuntimeRequest: () => Effect.die("unused getRuntimeRequest"),
246247
getRunningTurnContext: () => Effect.die("unused getRunningTurnContext"),

0 commit comments

Comments
 (0)