Skip to content

Commit 4a02ef8

Browse files
committed
fix(server): look up a run's turn-start items by thread and run
1 parent 5fe18d7 commit 4a02ef8

2 files changed

Lines changed: 209 additions & 0 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.test.ts‎

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3204,6 +3204,213 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
32043204
}),
32053205
);
32063206

3207+
it.effect("reads a run's turn-start items by thread and run, not by walking thread history", () =>
3208+
Effect.gen(function* () {
3209+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
3210+
const sql = yield* SqlClient.SqlClient;
3211+
const now = yield* DateTime.now;
3212+
const nowIso = DateTime.formatIso(now);
3213+
const threadId = ThreadId.make("thread:turn-start-items");
3214+
const otherThreadId = ThreadId.make("thread:turn-start-items:other");
3215+
const selectedRunId = RunId.make("run:turn-start-items:selected");
3216+
const emptyRunId = RunId.make("run:turn-start-items:empty");
3217+
const siblingRunId = RunId.make("run:turn-start-items:sibling");
3218+
3219+
for (const id of [threadId, otherThreadId]) {
3220+
yield* projectionStore.apply({
3221+
id: EventId.make(`event:${id}:created`),
3222+
type: "thread.created",
3223+
threadId: id,
3224+
occurredAt: now,
3225+
payload: {
3226+
createdBy: "user",
3227+
creationSource: "web",
3228+
id,
3229+
projectId: ProjectId.make("project:turn-start-items"),
3230+
title: id,
3231+
providerInstanceId,
3232+
modelSelection,
3233+
runtimeMode: "full-access",
3234+
interactionMode: "default",
3235+
branch: null,
3236+
worktreePath: null,
3237+
activeProviderThreadId: null,
3238+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: id },
3239+
forkedFrom: null,
3240+
createdAt: now,
3241+
updatedAt: now,
3242+
archivedAt: null,
3243+
settledOverride: null,
3244+
settledAt: null,
3245+
lastVisitedAt: null,
3246+
deletedAt: null,
3247+
},
3248+
});
3249+
}
3250+
for (const [ordinal, runId] of [selectedRunId, emptyRunId, siblingRunId].entries()) {
3251+
yield* sql`
3252+
INSERT INTO orchestration_v2_projection_runs (
3253+
run_id, thread_id, ordinal, provider, provider_thread_id, status,
3254+
requested_at, completed_at, payload_json
3255+
) VALUES (
3256+
${runId}, ${threadId}, ${ordinal + 1}, 'codex', NULL, 'completed', ${nowIso}, ${nowIso},
3257+
${encodeUnknownJsonString({
3258+
id: runId,
3259+
threadId,
3260+
ordinal: ordinal + 1,
3261+
providerInstanceId,
3262+
modelSelection,
3263+
providerThreadId: null,
3264+
userMessageId: `message:${runId}`,
3265+
rootNodeId: `node:${runId}`,
3266+
activeAttemptId: null,
3267+
status: "completed",
3268+
requestedAt: nowIso,
3269+
startedAt: nowIso,
3270+
completedAt: nowIso,
3271+
checkpointId: null,
3272+
contextHandoffId: null,
3273+
})}
3274+
)
3275+
`;
3276+
}
3277+
3278+
const insertItem = (
3279+
id: string,
3280+
itemThreadId: ThreadId,
3281+
runId: string | null,
3282+
ordinal: number,
3283+
) =>
3284+
sql`
3285+
INSERT INTO orchestration_v2_projection_turn_items (
3286+
turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
3287+
parent_item_id, ordinal, type, status, updated_at, payload_json
3288+
) VALUES (
3289+
${id}, ${itemThreadId}, ${runId}, NULL, NULL, NULL, NULL, ${ordinal},
3290+
'command_execution', 'completed', ${nowIso},
3291+
${encodeUnknownJsonString({
3292+
id,
3293+
threadId: itemThreadId,
3294+
runId,
3295+
nodeId: null,
3296+
providerThreadId: null,
3297+
providerTurnId: null,
3298+
nativeItemRef: null,
3299+
parentItemId: null,
3300+
ordinal,
3301+
status: "completed",
3302+
title: id,
3303+
input: "echo",
3304+
output: "ok",
3305+
exitCode: 0,
3306+
startedAt: nowIso,
3307+
completedAt: nowIso,
3308+
updatedAt: nowIso,
3309+
type: "command_execution",
3310+
})}
3311+
)
3312+
`;
3313+
const addHistory = (from: number, to: number) => sql`
3314+
WITH RECURSIVE history(position) AS (
3315+
SELECT ${from} UNION ALL SELECT position + 1 FROM history WHERE position < ${to}
3316+
)
3317+
INSERT INTO orchestration_v2_projection_turn_items (
3318+
turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
3319+
parent_item_id, ordinal, type, status, updated_at, payload_json
3320+
)
3321+
SELECT 'item:turn-start-items:history:' || position, ${threadId},
3322+
'run:turn-start-items:history:' || (position % 100), NULL, NULL, NULL, NULL, position,
3323+
'command_execution', 'completed', ${nowIso},
3324+
json_object(
3325+
'id', 'item:turn-start-items:history:' || position,
3326+
'threadId', ${threadId},
3327+
'runId', 'run:turn-start-items:history:' || (position % 100),
3328+
'nodeId', NULL, 'providerThreadId', NULL, 'providerTurnId', NULL,
3329+
'nativeItemRef', NULL, 'parentItemId', NULL, 'ordinal', position,
3330+
'status', 'completed', 'title', 'history', 'input', 'echo', 'output', 'ok',
3331+
'exitCode', 0, 'startedAt', ${nowIso}, 'completedAt', ${nowIso},
3332+
'updatedAt', ${nowIso}, 'type', 'command_execution'
3333+
)
3334+
FROM history
3335+
`;
3336+
3337+
// Twelve ordinals with two items each, inserted in reverse id order.
3338+
const selectedIds: Array<string> = [];
3339+
for (let slot = 1; slot <= 12; slot += 1) {
3340+
for (const suffix of ["b", "a"]) {
3341+
yield* insertItem(
3342+
`item:turn-start-items:selected:${String(slot).padStart(2, "0")}:${suffix}`,
3343+
threadId,
3344+
selectedRunId,
3345+
slot * 100,
3346+
);
3347+
}
3348+
selectedIds.push(
3349+
`item:turn-start-items:selected:${String(slot).padStart(2, "0")}:a`,
3350+
`item:turn-start-items:selected:${String(slot).padStart(2, "0")}:b`,
3351+
);
3352+
}
3353+
yield* insertItem("item:turn-start-items:no-run", threadId, null, 100);
3354+
yield* insertItem("item:turn-start-items:sibling:1", threadId, siblingRunId, 100);
3355+
yield* insertItem("item:turn-start-items:sibling:2", threadId, siblingRunId, 101);
3356+
yield* insertItem("item:turn-start-items:other-thread:b", otherThreadId, selectedRunId, 100);
3357+
yield* insertItem("item:turn-start-items:other-thread:a", otherThreadId, selectedRunId, 100);
3358+
3359+
const readItems = Effect.fn("readTurnStartItems")(function* (
3360+
readThreadId: ThreadId,
3361+
readRunId: RunId,
3362+
) {
3363+
const { statements, tracer } = traceSqlStatements();
3364+
const context = yield* projectionStore
3365+
.getTurnStartContext(readThreadId, readRunId)
3366+
.pipe(Effect.withTracer(tracer));
3367+
const lookups = statements.filter((statement) =>
3368+
statement.includes("FROM orchestration_v2_projection_turn_items"),
3369+
);
3370+
assert.lengthOf(lookups, 1);
3371+
const plan = (yield* sql.unsafe<{ readonly detail: string }>(
3372+
`EXPLAIN QUERY PLAN ${lookups[0]}`,
3373+
[readThreadId, readRunId],
3374+
)).map((row) => row.detail);
3375+
return { ids: context.turnItems.map((item) => item.id), plan };
3376+
});
3377+
3378+
const lookupPlans: Array<ReadonlyArray<string>> = [];
3379+
for (const [from, to] of [
3380+
[1, 1_000],
3381+
[1_001, 10_000],
3382+
] as const) {
3383+
yield* addHistory(from, to);
3384+
const { ids, plan } = yield* readItems(threadId, selectedRunId);
3385+
assert.deepEqual(ids, selectedIds);
3386+
lookupPlans.push(plan);
3387+
}
3388+
3389+
assert.deepEqual((yield* readItems(threadId, emptyRunId)).ids, []);
3390+
assert.deepEqual(
3391+
(yield* readItems(threadId, RunId.make("run:turn-start-items:missing"))).ids,
3392+
[],
3393+
);
3394+
assert.deepEqual((yield* readItems(threadId, siblingRunId)).ids, [
3395+
"item:turn-start-items:sibling:1",
3396+
"item:turn-start-items:sibling:2",
3397+
]);
3398+
assert.deepEqual((yield* readItems(otherThreadId, selectedRunId)).ids, [
3399+
"item:turn-start-items:other-thread:a",
3400+
"item:turn-start-items:other-thread:b",
3401+
]);
3402+
assert.deepEqual((yield* readItems(otherThreadId, siblingRunId)).ids, []);
3403+
3404+
for (const plan of lookupPlans) {
3405+
const lookup = plan.filter((detail) =>
3406+
detail.includes("orchestration_v2_projection_turn_items"),
3407+
);
3408+
assert.lengthOf(lookup, 1);
3409+
assert.match(lookup[0]!, /^SEARCH .*\(thread_id=\? AND run_id=\?\)$/);
3410+
}
3411+
}),
3412+
);
3413+
32073414
it.effect(
32083415
"counts each shell's own runless and live-run items, skipping rolled back and missing runs",
32093416
() =>

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4298,6 +4298,8 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
42984298
`.pipe(Effect.flatMap(decodeRows(decodeContextTransferPayload, threadId)));
42994299
const turnItems = yield* sql<PayloadRow>`
43004300
SELECT payload_json FROM orchestration_v2_projection_turn_items
4301+
-- Look up one run's items by key, not by walking the thread's whole history in ordinal order.
4302+
INDEXED BY orchestration_v2_projection_turn_items_thread_run_idx
43014303
WHERE thread_id = ${threadId} AND run_id = ${runId} ORDER BY ordinal ASC, turn_item_id ASC
43024304
`.pipe(Effect.flatMap(decodeRows(decodeTurnItemPayload, threadId)));
43034305
const providerSessions = yield* sql<PayloadRow>`

0 commit comments

Comments
 (0)