authorgravatar for tannerwpolley@gmail.comTanner Polley <tannerwpolley@gmail.com> 2026-10-14 01:58:55-06:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2026-10-14 00:58:55-07:00
log8855a6dd052a8cbc9aac125dc2e8d14a0c2decd6
tree483f14ab9154594e93c239bc01de41743eba545c
parent570542662a3f9f3fb44f88778cb24753072f78aa
signature Signed by PGP key B5690EEEBB952194

perf(server): reuse stored turn item positions (#19230)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

2 files changed, 66 insertions(+), 0 deletions(-)

apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts created+58
......@@ -0,0 +1,58 @@
1import { assert, it } from "@effect/vitest";
2import { RunId, ThreadId, TurnItemId } from "@t3tools/contracts";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as SqlClient from "effect/sql/SqlClient";
6
7import * as SqlitePersistence from "../persistence/Sqlite.ts";
8import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
9
10const layerTest = TurnItemPositionStore.layer.pipe(
11 Layer.provideMerge(SqlitePersistence.layerMemory),
12);
13
14it.effect("reuses an item's position without attempting another allocation insert", () =>
15 Effect.gen(function* () {
16 const sql = yield* SqlClient.SqlClient;
17 const positions = yield* TurnItemPositionStore.TurnItemPositionStoreV2;
18 const input = {
19 threadId: ThreadId.make("thread:position-reuse"),
20 turnItemId: TurnItemId.make("item:streaming"),
21 runId: null,
22 };
23 const first = yield* positions.allocate(input);
24 assert.equal(first, 1);
25
26 // BEFORE INSERT runs even when ON CONFLICT would discard the allocation.
27 yield* sql`
28 CREATE TEMP TRIGGER reject_redundant_position_insert
29 BEFORE INSERT ON orchestration_v2_turn_item_positions
30 WHEN EXISTS (
31 SELECT 1 FROM orchestration_v2_turn_item_positions
32 WHERE thread_id = NEW.thread_id AND turn_item_id = NEW.turn_item_id
33 )
34 BEGIN
35 SELECT RAISE(ABORT, 'Existing item positions must be reused');
36 END
37 `;
38
39 const next = yield* positions.allocate({
40 ...input,
41 turnItemId: TurnItemId.make("item:next"),
42 });
43 assert.equal(next, first + 1);
44 assert.equal(yield* positions.allocate(input), first);
45 assert.equal(
46 yield* positions.allocate({
47 ...input,
48 runId: RunId.make("run:moved"),
49 runOrdinal: 2,
50 }),
51 first,
52 );
53 assert.equal(
54 yield* positions.allocate({ ...input, threadId: ThreadId.make("thread:other") }),
55 1,
56 );
57 }).pipe(Effect.provide(layerTest)),
58);
apps/server/src/orchestration-v2/TurnItemPositionStore.ts+8
......@@ -46,6 +46,14 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
4646 runOrdinal: suppliedRunOrdinal,
4747 }) =>
4848 Effect.gen(function* () {
49 const existing = yield* sql<{ readonly ordinal: number }>`
50 SELECT ordinal
51 FROM orchestration_v2_turn_item_positions
52 WHERE thread_id = ${threadId} AND turn_item_id = ${turnItemId}
53 LIMIT 1
54 `;
55 if (existing[0] !== undefined) return existing[0].ordinal;
56
4957 const runRows =
5058 runId === null
5159 ? []