@devries-cameron20 / t3code Mirror · read-only / commits / 8855a6dd05
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 23, 14:15 UTC

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

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

Tanner Polley committed Oct 14, 2026 at 01:58 UTC 8855a6dd052a8cbc9aac125dc2e8d14a0c2decd6
2 files changed +66
apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts new
+58
@@ -0,0 +1,58 @@
1 +import { assert, it } from "@effect/vitest";
2 +import { RunId, ThreadId, TurnItemId } from "@t3tools/contracts";
3 +import * as Effect from "effect/Effect";
4 +import * as Layer from "effect/Layer";
5 +import * as SqlClient from "effect/sql/SqlClient";
6 +
7 +import * as SqlitePersistence from "../persistence/Sqlite.ts";
8 +import * as TurnItemPositionStore from "./TurnItemPositionStore.ts";
9 +
10 +const layerTest = TurnItemPositionStore.layer.pipe(
11 + Layer.provideMerge(SqlitePersistence.layerMemory),
12 +);
13 +
14 +it.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
46 runOrdinal: suppliedRunOrdinal,
47 }) =>
48 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 +
57 const runRows =
58 runId === null
59 ? []