TypeScript
perf(server): bound streaming recovery reads by active runs (#30357)
Yash Singh2c9991432f8a
diff --git a/apps/server/src/orchestration-v2/ProjectionRecovery.test.ts b/apps/server/src/orchestration-v2/ProjectionRecovery.test.tsindex f1169f1e910..733238e62d2 100644--- a/apps/server/src/orchestration-v2/ProjectionRecovery.test.ts+++ b/apps/server/src/orchestration-v2/ProjectionRecovery.test.ts@@ -658,6 +658,106 @@ it.effect("includes shared sessions and provider-owned background rosters in rec}).pipe(Effect.provide(layerTest)),);+it.effect("bounds streaming recovery reads by candidate runs and preserves message ordering", () =>+ Effect.gen(function* () {+ const projections = yield* ProjectionStore.ProjectionStoreV2;+ const sql = yield* SqlClient.SqlClient;+ const now = yield* DateTime.now;+ const threadId = yield* createThread("streaming");+ const other = yield* createThread("streaming-other");+ const empty = yield* createThread("streaming-empty");+ const statements: Array<ReturnType<Statement.Statement<unknown>["compile"]>> = [];+ const observe: Statement.Transformer = (statement) => {+ const compiled = statement.compile();+ if (compiled[0].includes("SELECT message.payload_json")) statements.push(compiled);+ return Effect.succeed(statement);+ };+ const expected: MessageId[] = [];+ const statuses = [+ "queued",+ "preparing",+ "starting",+ "running",+ "waiting",+ "completed",+ "interrupted",+ "rolled_back",+ "failed",+ "cancelled",+ ] as const;+ for (const [index, status] of statuses.entries()) {+ const runId = yield* createRun(threadId, status, { ordinal: index + 1 });+ for (const suffix of ["b", "a", "settled", "wrong-thread"]) {+ const id = MessageId.make(`message:streaming:${index}:${suffix}`);+ const streaming = suffix !== "settled";+ const messageThread = suffix === "wrong-thread" ? other : threadId;+ const createdAt = DateTime.add(now, { seconds: -index });+ yield* projections.apply({+ id: EventId.make(`event:${id}`),+ type: "message.updated",+ threadId: messageThread,+ runId,+ occurredAt: now,+ payload: {+ id,+ threadId: messageThread,+ runId,+ nodeId: null,+ role: "assistant",+ text: id,+ attachments: [],+ streaming,+ createdAt,+ updatedAt: now,+ createdBy: "agent",+ creationSource: "provider",+ },+ });+ }+ if (index < 5)+ expected.unshift(+ MessageId.make(`message:streaming:${index}:a`),+ MessageId.make(`message:streaming:${index}:b`),+ );+ }+ const foreignRun = yield* createRun(other, "running");+ yield* sql`+ INSERT INTO orchestration_v2_projection_messages+ (message_id, thread_id, run_id, role, streaming, created_at, updated_at, payload_json)+ VALUES ('foreign-run', ${threadId}, ${foreignRun}, 'assistant', 1, '', '', '{}')+ `;+ yield* sql`+ INSERT INTO orchestration_v2_projection_messages+ (message_id, thread_id, run_id, role, streaming, created_at, updated_at, payload_json)+ VALUES ('old-history', ${empty}, NULL, 'assistant', 1, '', '', '{}')+ `;+ const projection = yield* projections+ .getRuntimeRecoveryProjection(threadId)+ .pipe(Effect.provideService(Statement.CurrentTransformer, observe));+ assert.deepEqual(+ projection.messages.map((message) => message.id),+ expected,+ );+ const emptyProjection = yield* projections+ .getRuntimeRecoveryProjection(empty)+ .pipe(Effect.provideService(Statement.CurrentTransformer, observe));+ assert.deepEqual(emptyProjection.messages, []);+ assert.equal(statements.length, 2);+ for (const [query, params] of statements) {+ const plan = yield* sql.unsafe<{ detail: string }>(`EXPLAIN QUERY PLAN ${query}`, params);+ assert.isTrue(plan[0]?.detail.includes("SEARCH run "), JSON.stringify(plan));+ assert.isTrue(+ plan.some((row) => row.detail.includes("messages_run_idx")),+ JSON.stringify(plan),+ );+ assert.isFalse(+ plan.some((row) => row.detail.includes("messages_thread_created_idx")),+ JSON.stringify(plan),+ );+ }+ }).pipe(Effect.provide(layerTest)),+);+it.effect("bounds runtime provider-thread reads by ownership and preserves recovery liveness", () =>Effect.gen(function* () {const projections = yield* ProjectionStore.ProjectionStoreV2;diff --git a/apps/server/src/orchestration-v2/ProjectionStore.ts b/apps/server/src/orchestration-v2/ProjectionStore.tsindex 6186db8c6e3..d7a5fc4a18d 100644--- a/apps/server/src/orchestration-v2/ProjectionStore.ts+++ b/apps/server/src/orchestration-v2/ProjectionStore.ts@@ -4730,14 +4730,14 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =WHERE thread_id = ${threadId} AND status = 'pending'ORDER BY created_at ASC, runtime_request_id ASC`,+ // CROSS JOIN keeps empty run cohorts out of message history; the run index bounds each lookup.sql<PayloadRow>`- SELECT message.payload_json FROM orchestration_v2_projection_messages AS message- WHERE message.thread_id = ${threadId} AND message.streaming = 1- AND message.run_id IN (- SELECT run_id FROM orchestration_v2_projection_runs- WHERE thread_id = ${threadId}- AND status IN ('queued', 'preparing', 'starting', 'running', 'waiting')- )+ SELECT message.payload_json FROM orchestration_v2_projection_runs AS run+ CROSS JOIN orchestration_v2_projection_messages AS message+ INDEXED BY orchestration_v2_projection_messages_run_idx ON message.run_id = run.run_id+ WHERE run.thread_id = ${threadId}+ AND run.status IN ('queued', 'preparing', 'starting', 'running', 'waiting')+ AND message.thread_id = ${threadId} AND message.streaming = 1ORDER BY message.created_at ASC, message.message_id ASC`,sql<PayloadRow>`