diff --git a/apps/server/src/persistence/Migrations.ts b/apps/server/src/persistence/Migrations.ts index 36762e9059b58..5c24577ad53ec 100644 --- a/apps/server/src/persistence/Migrations.ts +++ b/apps/server/src/persistence/Migrations.ts @@ -118,6 +118,7 @@ import Migration0107 from "./Migrations/107_DeferredSlashCommandIndex.ts"; import Migration0108 from "./Migrations/108_AgentFeedback.ts"; import Migration0109 from "./Migrations/109_IssueTrackerAccounts.ts"; import Migration0110 from "./Migrations/110_Workflows.ts"; +import Migration0111 from "./Migrations/111_SupersededRunAttemptIndex.ts"; const migrationEntries = [ [1, "OrchestrationEvents", Migration0001], @@ -231,6 +232,7 @@ const migrationEntries = [ [108, "AgentFeedback", Migration0108], [109, "IssueTrackerAccounts", Migration0109], [110, "Workflows", Migration0110], + [111, "SupersededRunAttemptIndex", Migration0111], ] as const; export const migrationManifest = migrationEntries.map(([id, name]) => [id, name] as const); diff --git a/apps/server/src/persistence/Migrations/111_SupersededRunAttemptIndex.ts b/apps/server/src/persistence/Migrations/111_SupersededRunAttemptIndex.ts new file mode 100644 index 0000000000000..ad2f9f4a3817e --- /dev/null +++ b/apps/server/src/persistence/Migrations/111_SupersededRunAttemptIndex.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; + + // Opening a thread's history looks up its few superseded attempts. Without this index that + // reads every attempt row of the thread, which is slow on a long thread with a cold cache. + yield* sql` + CREATE INDEX IF NOT EXISTS orchestration_v2_projection_run_attempts_superseded_idx + ON orchestration_v2_projection_run_attempts(thread_id, run_id, root_node_id) + WHERE status = 'superseded' + `; +}); diff --git a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts index 1b8cf040b5fbc..de0d6a3f63ff5 100644 --- a/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts +++ b/apps/server/src/persistence/reconcileV2PreviewMigration.test.ts @@ -90,6 +90,7 @@ describe("V2 preview upgrade", () => { [108, "AgentFeedback"], [109, "IssueTrackerAccounts"], [110, "Workflows"], + [111, "SupersededRunAttemptIndex"], ]); assert.deepStrictEqual(yield* runMigrations(), []); assert.deepStrictEqual(yield* sql`SELECT * FROM orchestration_v2_legacy_imports`, imports); @@ -223,6 +224,7 @@ describe("V2 preview upgrade", () => { [108, "AgentFeedback"], [109, "IssueTrackerAccounts"], [110, "Workflows"], + [111, "SupersededRunAttemptIndex"], ]); }).pipe(Effect.provide(NodeSqliteClient.layer({ filename: ":memory:" }))), );