diff --git a/apps/server/src/orchestration-v2/ProjectionStore.test.ts b/apps/server/src/orchestration-v2/ProjectionStore.test.ts index 83f33269f3992..384f8e93f5e4e 100644 --- a/apps/server/src/orchestration-v2/ProjectionStore.test.ts +++ b/apps/server/src/orchestration-v2/ProjectionStore.test.ts @@ -3812,6 +3812,110 @@ it.layer(layerTest)("ProjectionStoreV2", (it) => { }), ); + it.effect("flags a stop request only on the latest run, using the interrupt index", () => + Effect.gen(function* () { + const projectionStore = yield* ProjectionStore.ProjectionStoreV2; + const sql = yield* SqlClient.SqlClient; + const suffix = "shell-stop-request"; + const threadId = yield* addRolledBackRecoveryCandidate(suffix); + const olderRunId = RunId.make(`run:${suffix}:rolled-back`); + const latestRunId = RunId.make(`run:${suffix}:latest`); + const rootNodeId = NodeId.make(`node:${suffix}:latest`); + const now = yield* DateTime.now; + yield* projectionStore.apply({ + id: EventId.make(`event:${suffix}:latest-run`), + type: "run.created", + threadId, + runId: latestRunId, + nodeId: rootNodeId, + driver, + providerInstanceId, + occurredAt: now, + payload: { + id: latestRunId, + threadId, + ordinal: 2, + providerInstanceId, + modelSelection, + providerThreadId: null, + userMessageId: MessageId.make(`message:${suffix}:latest`), + rootNodeId, + activeAttemptId: null, + status: "running", + requestedAt: now, + startedAt: now, + completedAt: null, + checkpointId: null, + contextHandoffId: null, + }, + }); + const requestStop = (runId: RunId, ordinal: number) => + projectionStore.apply({ + id: EventId.make(`event:${suffix}:stop:${runId}`), + type: "turn-item.updated", + threadId, + runId, + nodeId: rootNodeId, + driver, + occurredAt: now, + payload: { + id: TurnItemId.make(`item:${suffix}:stop:${runId}`), + threadId, + runId, + nodeId: rootNodeId, + providerThreadId: null, + providerTurnId: null, + nativeItemRef: null, + parentItemId: null, + ordinal, + status: "completed", + title: "Stop requested", + startedAt: now, + completedAt: now, + updatedAt: now, + type: "run_interrupt_request", + message: "Stopping", + }, + }); + const expectStopRequested = Effect.fn("expectStopRequested")(function* (expected: boolean) { + const memoryShell = ProjectionStore.threadShellFromProjection( + yield* projectionStore.getThreadProjection(threadId), + ); + const sqlShell = (yield* projectionStore.getShellSnapshot()).threads.find( + (thread) => thread.id === threadId, + ); + assert.equal(sqlShell?.latestRunId, latestRunId); + assert.equal(memoryShell.latestRunStopRequested, expected); + assert.equal(sqlShell?.latestRunStopRequested, expected); + }); + + yield* expectStopRequested(false); + yield* requestStop(olderRunId, 2); + yield* expectStopRequested(false); + yield* requestStop(latestRunId, 3); + yield* expectStopRequested(true); + + const { statements, tracer } = traceSqlStatements(); + yield* projectionStore.getShellSnapshot().pipe(Effect.withTracer(tracer)); + const statement = statements.find((query) => query.includes("AS latest_run_stop_requested")); + assert.isDefined(statement); + const stopLookups = (yield* sql.unsafe<{ readonly detail: string }>( + `EXPLAIN QUERY PLAN ${statement}`, + )) + .map((row) => row.detail) + .filter((detail) => detail.startsWith("SEARCH stop_request ")); + // Latest and blocking runs: each is one probe of the small partial index, + // not a read of every item row in the run. + assert.lengthOf(stopLookups, 2); + for (const lookup of stopLookups) { + assert.include( + lookup, + "COVERING INDEX orchestration_v2_projection_turn_items_run_interrupt_idx (thread_id=? AND run_id=?)", + ); + } + }), + ); + it.effect("selects the latest waiting secret only from active runs", () => Effect.gen(function* () { const projectionStore = yield* ProjectionStore.ProjectionStoreV2; diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index 5979eee6c18d7..8d5a7b3a1f6e4 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -70,6 +70,7 @@ import Migration0064 from "./Migrations/064_CanonicalizeThreadPullRequestLinks.t import Migration0065 from "./Migrations/065_CodexUsageNativeTurnIndex.ts"; import Migration0066 from "./Migrations/066_RepairClientSettlementTimestamps.ts"; import Migration0067 from "./Migrations/067_WebhookDedupe.ts"; +import Migration0068 from "./Migrations/068_RunInterruptRequestIndex.ts"; const migrationEntries = [ [1, "OrchestrationEvents", Migration0001], @@ -140,6 +141,7 @@ const migrationEntries = [ [65, "CodexUsageNativeTurnIndex", Migration0065], [66, "RepairClientSettlementTimestamps", Migration0066], [67, "WebhookDedupe", Migration0067], + [68, "RunInterruptRequestIndex", Migration0068], ] as const; export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); diff --git a/apps/server/src/persistence/Migrations/068_RunInterruptRequestIndex.ts b/apps/server/src/persistence/Migrations/068_RunInterruptRequestIndex.ts new file mode 100644 index 0000000000000..7cd9f1162a935 --- /dev/null +++ b/apps/server/src/persistence/Migrations/068_RunInterruptRequestIndex.ts @@ -0,0 +1,14 @@ +import * as Effect from "effect/Effect"; +import * as SqlClient from "effect/sql/SqlClient"; + +export default Effect.gen(function* () { + const sql = yield* SqlClient.SqlClient; + + // The thread shell checks every thread's latest run for a stop request. + // Without this, that check reads each item row of the run to test its type. + yield* sql` + CREATE INDEX orchestration_v2_projection_turn_items_run_interrupt_idx + ON orchestration_v2_projection_turn_items(thread_id, run_id) + WHERE type = 'run_interrupt_request' + `; +}); diff --git a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts index 990d450cde6b5..640ad6ccfe630 100644 --- a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts +++ b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts @@ -47,6 +47,7 @@ describe("V2 preview upgrade", () => { [65, "CodexUsageNativeTurnIndex"], [66, "RepairClientSettlementTimestamps"], [67, "WebhookDedupe"], + [68, "RunInterruptRequestIndex"], ]); assert.deepStrictEqual(yield* runMigrations(), []); assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports); @@ -137,6 +138,7 @@ describe("V2 preview upgrade", () => { [65, "CodexUsageNativeTurnIndex"], [66, "RepairClientSettlementTimestamps"], [67, "WebhookDedupe"], + [68, "RunInterruptRequestIndex"], ]); }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), );