Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions apps/server/src/orchestration-v2/TurnItemPositionStore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
64 changes: 39 additions & 25 deletions apps/server/src/orchestration-v2/TurnItemPositionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,34 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
placement = "append",
keepExisting = false,
}) {
const existing = 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 [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 ||
Expand All @@ -82,7 +90,7 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
) {
return current;
}
yield* sql`
written = yield* sql<{ readonly ordinal: number }>`
UPDATE orchestration_v2_turn_item_positions
SET ordinal = (
SELECT COALESCE(MAX(ordinal), ${lowerBound}) + 1
Expand All @@ -92,9 +100,10 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
AND ordinal <= ${upperBound}
)
WHERE thread_id = ${threadId} AND turn_item_id = ${turnItemId}
RETURNING ordinal
`;
} else {
yield* sql`
written = yield* sql<{ readonly ordinal: number }>`
INSERT INTO orchestration_v2_turn_item_positions (thread_id, turn_item_id, ordinal)
SELECT
${threadId},
Expand All @@ -109,14 +118,19 @@ export const layer: Layer.Layer<TurnItemPositionStoreV2, never, SqlClient.SqlCli
AND ordinal >= ${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({
Expand Down
Loading