Skip to content

Commit 3735eb1

Browse files
authored
fix(server): look up a run's turn-start items by run, not by walking thread history (pingdotgg#28343)
1 parent b5c83cd commit 3735eb1

2 files changed

Lines changed: 212 additions & 0 deletions

File tree

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

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3439,6 +3439,216 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => {
34393439
}),
34403440
);
34413441

3442+
it.effect("reads a run's turn-start items by thread and run, not by walking thread history", () =>
3443+
Effect.gen(function* () {
3444+
const projectionStore = yield* ProjectionStore.ProjectionStoreV2;
3445+
const sql = yield* SqlClient.SqlClient;
3446+
const now = yield* DateTime.now;
3447+
const nowIso = DateTime.formatIso(now);
3448+
const threadId = ThreadId.make("thread:turn-start-items");
3449+
const otherThreadId = ThreadId.make("thread:turn-start-items:other");
3450+
const selectedRunId = RunId.make("run:turn-start-items:selected");
3451+
const emptyRunId = RunId.make("run:turn-start-items:empty");
3452+
const siblingRunId = RunId.make("run:turn-start-items:sibling");
3453+
3454+
for (const id of [threadId, otherThreadId]) {
3455+
yield* projectionStore.apply({
3456+
id: EventId.make(`event:${id}:created`),
3457+
type: "thread.created",
3458+
threadId: id,
3459+
occurredAt: now,
3460+
payload: {
3461+
createdBy: "user",
3462+
creationSource: "web",
3463+
id,
3464+
projectId: ProjectId.make("project:turn-start-items"),
3465+
title: id,
3466+
providerInstanceId,
3467+
modelSelection,
3468+
runtimeMode: "full-access",
3469+
interactionMode: "default",
3470+
branch: null,
3471+
worktreePath: null,
3472+
activeProviderThreadId: null,
3473+
lineage: { parentThreadId: null, relationshipToParent: null, rootThreadId: id },
3474+
forkedFrom: null,
3475+
createdAt: now,
3476+
updatedAt: now,
3477+
archivedAt: null,
3478+
settledOverride: null,
3479+
settledAt: null,
3480+
lastVisitedAt: null,
3481+
deletedAt: null,
3482+
},
3483+
});
3484+
}
3485+
for (const [ordinal, runId] of [selectedRunId, emptyRunId, siblingRunId].entries()) {
3486+
yield* sql`
3487+
INSERT INTO orchestration_v2_projection_runs (
3488+
run_id, thread_id, ordinal, provider, provider_thread_id, status,
3489+
requested_at, completed_at, payload_json
3490+
) VALUES (
3491+
${runId}, ${threadId}, ${ordinal + 1}, 'codex', NULL, 'completed', ${nowIso}, ${nowIso},
3492+
${encodeUnknownJsonString({
3493+
id: runId,
3494+
threadId,
3495+
ordinal: ordinal + 1,
3496+
providerInstanceId,
3497+
modelSelection,
3498+
providerThreadId: null,
3499+
userMessageId: `message:${runId}`,
3500+
rootNodeId: `node:${runId}`,
3501+
activeAttemptId: null,
3502+
status: "completed",
3503+
requestedAt: nowIso,
3504+
startedAt: nowIso,
3505+
completedAt: nowIso,
3506+
checkpointId: null,
3507+
contextHandoffId: null,
3508+
})}
3509+
)
3510+
`;
3511+
}
3512+
3513+
const insertItem = (
3514+
id: string,
3515+
itemThreadId: ThreadId,
3516+
runId: string | null,
3517+
ordinal: number,
3518+
) =>
3519+
sql`
3520+
INSERT INTO orchestration_v2_projection_turn_items (
3521+
turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
3522+
parent_item_id, ordinal, type, status, updated_at, payload_json
3523+
) VALUES (
3524+
${id}, ${itemThreadId}, ${runId}, NULL, NULL, NULL, NULL, ${ordinal},
3525+
'command_execution', 'completed', ${nowIso},
3526+
${encodeUnknownJsonString({
3527+
id,
3528+
threadId: itemThreadId,
3529+
runId,
3530+
nodeId: null,
3531+
providerThreadId: null,
3532+
providerTurnId: null,
3533+
nativeItemRef: null,
3534+
parentItemId: null,
3535+
ordinal,
3536+
status: "completed",
3537+
title: id,
3538+
input: "echo",
3539+
output: "ok",
3540+
exitCode: 0,
3541+
startedAt: nowIso,
3542+
completedAt: nowIso,
3543+
updatedAt: nowIso,
3544+
type: "command_execution",
3545+
})}
3546+
)
3547+
`;
3548+
const addHistory = (from: number, to: number) => sql`
3549+
WITH RECURSIVE history(position) AS (
3550+
SELECT ${from} UNION ALL SELECT position + 1 FROM history WHERE position < ${to}
3551+
)
3552+
INSERT INTO orchestration_v2_projection_turn_items (
3553+
turn_item_id, thread_id, run_id, node_id, provider_thread_id, provider_turn_id,
3554+
parent_item_id, ordinal, type, status, updated_at, payload_json
3555+
)
3556+
SELECT 'item:turn-start-items:history:' || position, ${threadId},
3557+
'run:turn-start-items:history:' || (position % 100), NULL, NULL, NULL, NULL, position,
3558+
'command_execution', 'completed', ${nowIso},
3559+
json_object(
3560+
'id', 'item:turn-start-items:history:' || position,
3561+
'threadId', ${threadId},
3562+
'runId', 'run:turn-start-items:history:' || (position % 100),
3563+
'nodeId', NULL, 'providerThreadId', NULL, 'providerTurnId', NULL,
3564+
'nativeItemRef', NULL, 'parentItemId', NULL, 'ordinal', position,
3565+
'status', 'completed', 'title', 'history', 'input', 'echo', 'output', 'ok',
3566+
'exitCode', 0, 'startedAt', ${nowIso}, 'completedAt', ${nowIso},
3567+
'updatedAt', ${nowIso}, 'type', 'command_execution'
3568+
)
3569+
FROM history
3570+
`;
3571+
3572+
// Twelve ordinals with two items each, inserted in reverse id order.
3573+
const selectedIds: Array<string> = [];
3574+
for (let slot = 1; slot <= 12; slot += 1) {
3575+
for (const suffix of ["b", "a"]) {
3576+
yield* insertItem(
3577+
`item:turn-start-items:selected:${String(slot).padStart(2, "0")}:${suffix}`,
3578+
threadId,
3579+
selectedRunId,
3580+
slot * 100,
3581+
);
3582+
}
3583+
selectedIds.push(
3584+
`item:turn-start-items:selected:${String(slot).padStart(2, "0")}:a`,
3585+
`item:turn-start-items:selected:${String(slot).padStart(2, "0")}:b`,
3586+
);
3587+
}
3588+
yield* insertItem("item:turn-start-items:no-run", threadId, null, 100);
3589+
yield* insertItem("item:turn-start-items:sibling:1", threadId, siblingRunId, 100);
3590+
yield* insertItem("item:turn-start-items:sibling:2", threadId, siblingRunId, 101);
3591+
yield* insertItem("item:turn-start-items:other-thread:b", otherThreadId, selectedRunId, 100);
3592+
yield* insertItem("item:turn-start-items:other-thread:a", otherThreadId, selectedRunId, 100);
3593+
3594+
const readItems = Effect.fn("readTurnStartItems")(function* (
3595+
readThreadId: ThreadId,
3596+
readRunId: RunId,
3597+
) {
3598+
const { statements, tracer } = traceSqlStatements();
3599+
const context = yield* projectionStore
3600+
.getTurnStartContext(readThreadId, readRunId)
3601+
.pipe(Effect.withTracer(tracer));
3602+
const lookups = statements.filter((statement) =>
3603+
statement.includes("FROM orchestration_v2_projection_turn_items"),
3604+
);
3605+
assert.lengthOf(lookups, 1);
3606+
const plan = (yield* sql.unsafe<{ readonly detail: string }>(
3607+
`EXPLAIN QUERY PLAN ${lookups[0]}`,
3608+
[readThreadId, readRunId],
3609+
)).map((row) => row.detail);
3610+
return { ids: context.turnItems.map((item) => item.id), plan };
3611+
});
3612+
3613+
const lookupPlans: Array<ReadonlyArray<string>> = [];
3614+
for (const [from, to] of [
3615+
[1, 1_000],
3616+
[1_001, 10_000],
3617+
] as const) {
3618+
yield* addHistory(from, to);
3619+
const { ids, plan } = yield* readItems(threadId, selectedRunId);
3620+
assert.deepEqual(ids, selectedIds);
3621+
lookupPlans.push(plan);
3622+
}
3623+
3624+
assert.deepEqual((yield* readItems(threadId, emptyRunId)).ids, []);
3625+
assert.deepEqual(
3626+
(yield* readItems(threadId, RunId.make("run:turn-start-items:missing"))).ids,
3627+
[],
3628+
);
3629+
assert.deepEqual((yield* readItems(threadId, siblingRunId)).ids, [
3630+
"item:turn-start-items:sibling:1",
3631+
"item:turn-start-items:sibling:2",
3632+
]);
3633+
assert.deepEqual((yield* readItems(otherThreadId, selectedRunId)).ids, [
3634+
"item:turn-start-items:other-thread:a",
3635+
"item:turn-start-items:other-thread:b",
3636+
]);
3637+
assert.deepEqual((yield* readItems(otherThreadId, siblingRunId)).ids, []);
3638+
3639+
for (const plan of lookupPlans) {
3640+
const lookup = plan.filter((detail) =>
3641+
detail.includes("orchestration_v2_projection_turn_items"),
3642+
);
3643+
assert.lengthOf(lookup, 1);
3644+
assert.match(
3645+
lookup[0]!,
3646+
/^SEARCH .*\((?:thread_id=\? AND )?run_id=\?(?: AND thread_id=\?)?\)$/,
3647+
);
3648+
}
3649+
}),
3650+
);
3651+
34423652
it.effect(
34433653
"counts each shell's own runless and live-run items, skipping rolled back and missing runs",
34443654
() =>

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

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

0 commit comments

Comments
 (0)