From 277e135da9fb7dbbcce0f2ccdbf5819e73f0f078 Mon Sep 17 00:00:00 2001 From: Navid Nadali Date: Wed, 18 Nov 2026 23:59:15 +0000 Subject: [PATCH] perf(server): place turn items with one read and no read-back Placing a turn item read its position and its run's ordinal in two statements, and read the position back after inserting or moving it. In a provider commit each statement is a round trip to the writer, so a streamed snapshot of an existing item paid one extra round trip and a new item two. Read both in one statement and take the written position from RETURNING. Co-Authored-By: Claude Opus 5.5 --- .../TurnItemPositionStore.test.ts | 41 ++++++++++++ .../orchestration-v2/TurnItemPositionStore.ts | 64 +++++++++++-------- 2 files changed, 80 insertions(+), 25 deletions(-) diff --git a/apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts b/apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts index e4eb2ada8eb6e..0090246c03bf4 100644 --- a/apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts +++ b/apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts @@ -3,6 +3,7 @@ import { RunId, ThreadId, TurnItemId } from "@t3tools/contracts"; import * as Effect from "effect/Effect"; import * as Layer from "effect/Layer"; import * as SqlClient from "effect/sql/SqlClient"; +import * as Statement from "effect/sql/Statement"; import * as SqlitePersistence from "../persistence/Sqlite.ts"; import * as TurnItemPositionStore from "./TurnItemPositionStore.ts"; @@ -57,6 +58,46 @@ it.effect("reuses an item's position without attempting another allocation inser }).pipe(Effect.provide(layerTest)), ); +it.effect("places, reuses and moves an item's position with one read and at most one write", () => + Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + const positions = yield* TurnItemPositionStore.TurnItemPositionStoreV2; + const threadId = ThreadId.make("thread:position-statements"); + const runId = RunId.make("run:position-statements"); + yield* sql` + INSERT INTO orchestration_v2_projection_runs ( + run_id, thread_id, ordinal, provider, status, requested_at, payload_json + ) VALUES (${runId}, ${threadId}, 3, 'codex', 'running', '2026-01-01T00:00:00.000Z', '{}') + `; + const counted = (input: { readonly runId: RunId; readonly runOrdinal?: number }) => + Effect.suspend(() => { + let statements = 0; + const count: Statement.Transformer = (statement) => { + statements++; + return Effect.succeed(statement); + }; + return positions + .allocate({ threadId, turnItemId: TurnItemId.make("item:streaming"), ...input }) + .pipe( + Effect.provideService(Statement.CurrentTransformer, count), + Effect.map((ordinal) => [ordinal, statements]), + ); + }); + assert.deepEqual( + [ + yield* counted({ runId }), + yield* counted({ runId }), + yield* counted({ runId: RunId.make("run:next"), runOrdinal: 4 }), + ], + [ + [3_000_001, 2], + [3_000_001, 1], + [4_000_001, 2], + ], + ); + }).pipe(Effect.provide(layerTest)), +); + it.effect("places a run-start item ahead of the run's items, then appends once taken", () => Effect.gen(function* () { const positions = yield* TurnItemPositionStore.TurnItemPositionStoreV2; diff --git a/apps/server/src/orchestration-v2/TurnItemPositionStore.ts b/apps/server/src/orchestration-v2/TurnItemPositionStore.ts index fb6a026f9287a..07f5d2537e18e 100644 --- a/apps/server/src/orchestration-v2/TurnItemPositionStore.ts +++ b/apps/server/src/orchestration-v2/TurnItemPositionStore.ts @@ -54,26 +54,34 @@ export const layer: Layer.Layer` - SELECT ordinal - FROM orchestration_v2_turn_item_positions - WHERE thread_id = ${threadId} AND turn_item_id = ${turnItemId} - LIMIT 1 + const [existing] = yield* sql<{ + readonly ordinal: number | null; + readonly run_ordinal: number | null; + }>` + SELECT + ( + SELECT ordinal + FROM orchestration_v2_turn_item_positions + WHERE thread_id = ${threadId} AND turn_item_id = ${turnItemId} + LIMIT 1 + ) AS ordinal, + ${ + runId === null || suppliedRunOrdinal !== undefined + ? sql`NULL` + : sql`( + SELECT ordinal + FROM orchestration_v2_projection_runs + WHERE thread_id = ${threadId} AND run_id = ${runId} + LIMIT 1 + )` + } AS run_ordinal `; - const runRows = - runId === null || suppliedRunOrdinal !== undefined - ? [] - : yield* sql<{ readonly ordinal: number }>` - SELECT ordinal - FROM orchestration_v2_projection_runs - WHERE thread_id = ${threadId} AND run_id = ${runId} - LIMIT 1 - `; - const runOrdinal = suppliedRunOrdinal ?? runRows[0]?.ordinal ?? null; + const runOrdinal = suppliedRunOrdinal ?? existing?.run_ordinal ?? null; const lowerBound = runOrdinal === null ? 0 : runOrdinal * 1_000_000; const upperBound = runOrdinal === null ? 999_999 : lowerBound + 999_999; - const current = existing[0]?.ordinal; - if (current !== undefined) { + const current = existing?.ordinal ?? null; + let written: ReadonlyArray<{ readonly ordinal: number }>; + if (current !== null) { // A steer that missed its turn starts the next run; it moves into that run's range. if ( keepExisting || @@ -82,7 +90,7 @@ export const layer: Layer.Layer` UPDATE orchestration_v2_turn_item_positions SET ordinal = ( SELECT COALESCE(MAX(ordinal), ${lowerBound}) + 1 @@ -92,9 +100,10 @@ export const layer: Layer.Layer` INSERT INTO orchestration_v2_turn_item_positions (thread_id, turn_item_id, ordinal) SELECT ${threadId}, @@ -109,14 +118,19 @@ export const layer: Layer.Layer= ${lowerBound} AND ordinal <= ${upperBound} ON CONFLICT(thread_id, turn_item_id) DO NOTHING + RETURNING ordinal `; } - const rows = yield* sql<{ readonly ordinal: number }>` - SELECT ordinal - FROM orchestration_v2_turn_item_positions - WHERE thread_id = ${threadId} AND turn_item_id = ${turnItemId} - LIMIT 1 - `; + // ON CONFLICT returns no row when another allocation placed the item first. + const rows = + written.length > 0 + ? written + : yield* sql<{ readonly ordinal: number }>` + SELECT ordinal + FROM orchestration_v2_turn_item_positions + WHERE thread_id = ${threadId} AND turn_item_id = ${turnItemId} + LIMIT 1 + `; const ordinal = rows[0]?.ordinal; if (ordinal === undefined) { return yield* new TurnItemPositionStoreError({