Skip to content

Commit f70f891

Browse files
tannerpolleyclaude
andcommitted
perf(server): reuse stored turn item positions
Return an item's durable position before attempting another allocation. Cover repeated updates with a SQLite trigger that rejects redundant inserts. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 9c9ae43 commit f70f891

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
? []

0 commit comments

Comments
 (0)