|
| 1 | +import { assert, it } from "@effect/vitest"; |
| 2 | +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient"; |
| 3 | +import * as Effect from "effect/Effect"; |
| 4 | +import * as SqlClient from "effect/sql/SqlClient"; |
| 5 | + |
| 6 | +import { runMigrations } from "../Migrations.ts"; |
| 7 | + |
| 8 | +const layer = it.layer(NodeSqliteClient.layer({ filename: ":memory:" })); |
| 9 | + |
| 10 | +layer("067_OrchestrationSlowQueryIndexes", (it) => { |
| 11 | + it.effect("creates the indexes and the planner uses them for the slow queries", () => |
| 12 | + Effect.gen(function* () { |
| 13 | + const sql = yield* SqlClient.SqlClient; |
| 14 | + yield* runMigrations({ toMigrationInclusive: 66 }); |
| 15 | + yield* sql` |
| 16 | + INSERT INTO orchestration_v2_projection_turn_items ( |
| 17 | + turn_item_id, thread_id, run_id, ordinal, type, status, payload_json, updated_at |
| 18 | + ) VALUES ( |
| 19 | + 'item-1', 'thread-1', NULL, 1, 'user_message', 'completed', |
| 20 | + '{"messageId":"message-1"}', '2026-01-01' |
| 21 | + ) |
| 22 | + `; |
| 23 | + const beforeMessages = yield* sql` |
| 24 | + SELECT json_extract(payload_json, '$.messageId') AS message_id, MIN(ordinal) AS ordinal |
| 25 | + FROM orchestration_v2_projection_turn_items |
| 26 | + WHERE thread_id = 'thread-1' AND type IN ('user_message', 'assistant_message') |
| 27 | + GROUP BY json_extract(payload_json, '$.messageId') |
| 28 | + `; |
| 29 | + const beforeRunning = yield* sql<{ readonly detail: string }>` |
| 30 | + EXPLAIN QUERY PLAN |
| 31 | + SELECT thread_id FROM orchestration_v2_projection_runs WHERE status = 'running' |
| 32 | + `; |
| 33 | + assert.isFalse( |
| 34 | + beforeRunning.some((row) => row.detail.includes("runs_recovery_idx")), |
| 35 | + beforeRunning.map((row) => row.detail).join("\n"), |
| 36 | + ); |
| 37 | + yield* runMigrations({ toMigrationInclusive: 67 }); |
| 38 | + assert.deepEqual(yield* runMigrations(), []); |
| 39 | + |
| 40 | + const afterRunning = yield* sql<{ readonly detail: string }>` |
| 41 | + EXPLAIN QUERY PLAN |
| 42 | + SELECT thread_id FROM orchestration_v2_projection_runs WHERE status = 'running' |
| 43 | + `; |
| 44 | + assert.isTrue( |
| 45 | + afterRunning.some((row) => row.detail.includes("runs_running_idx")), |
| 46 | + afterRunning.map((row) => row.detail).join("\n"), |
| 47 | + ); |
| 48 | + const recoveryPlan = yield* sql<{ readonly detail: string }>` |
| 49 | + EXPLAIN QUERY PLAN |
| 50 | + SELECT thread_id FROM orchestration_v2_projection_runs |
| 51 | + WHERE thread_id = 'thread-1' |
| 52 | + AND status IN ('queued', 'preparing', 'starting', 'running', 'waiting') |
| 53 | + `; |
| 54 | + assert.isTrue( |
| 55 | + recoveryPlan.some((row) => row.detail.includes("runs_recovery_idx")), |
| 56 | + recoveryPlan.map((row) => row.detail).join("\n"), |
| 57 | + ); |
| 58 | + |
| 59 | + const rows = yield* sql<{ readonly name: string }>` |
| 60 | + SELECT name FROM sqlite_master WHERE type = 'index' |
| 61 | + `; |
| 62 | + const indexes = new Set(rows.map((row) => row.name)); |
| 63 | + for (const name of [ |
| 64 | + "orchestration_v2_projection_turn_items_thread_type_idx", |
| 65 | + "orchestration_v2_projection_turn_items_thread_message_idx", |
| 66 | + "orchestration_v2_projection_turn_items_run_status_idx", |
| 67 | + "orchestration_v2_projection_runs_running_idx", |
| 68 | + "orchestration_v2_projection_runs_user_message_idx", |
| 69 | + "orchestration_v2_projection_provider_turns_thread_completed_idx", |
| 70 | + "idx_orchestration_events_aggregate_version_sequence", |
| 71 | + ]) { |
| 72 | + assert.isTrue(indexes.has(name), name); |
| 73 | + } |
| 74 | + |
| 75 | + const afterMessages = yield* sql` |
| 76 | + SELECT json_extract(payload_json, '$.messageId') AS message_id, MIN(ordinal) AS ordinal |
| 77 | + FROM orchestration_v2_projection_turn_items |
| 78 | + WHERE thread_id = 'thread-1' AND type IN ('user_message', 'assistant_message') |
| 79 | + GROUP BY json_extract(payload_json, '$.messageId') |
| 80 | + `; |
| 81 | + assert.deepEqual(afterMessages, beforeMessages); |
| 82 | + const messagePlan = yield* sql<{ readonly detail: string }>` |
| 83 | + EXPLAIN QUERY PLAN |
| 84 | + SELECT json_extract(payload_json, '$.messageId'), MIN(ordinal) |
| 85 | + FROM orchestration_v2_projection_turn_items |
| 86 | + WHERE thread_id = 'thread-1' AND type IN ('user_message', 'assistant_message') |
| 87 | + GROUP BY json_extract(payload_json, '$.messageId') |
| 88 | + `; |
| 89 | + assert.isTrue( |
| 90 | + messagePlan.some((row) => |
| 91 | + row.detail.includes("orchestration_v2_projection_turn_items_thread_message_idx"), |
| 92 | + ), |
| 93 | + messagePlan.map((row) => row.detail).join("\n"), |
| 94 | + ); |
| 95 | + |
| 96 | + const runPlan = yield* sql<{ readonly detail: string }>` |
| 97 | + EXPLAIN QUERY PLAN |
| 98 | + SELECT thread_id FROM orchestration_v2_projection_runs |
| 99 | + WHERE json_extract(payload_json, '$.userMessageId') = 'message-1' |
| 100 | + `; |
| 101 | + assert.isTrue( |
| 102 | + runPlan.some((row) => |
| 103 | + row.detail.includes("orchestration_v2_projection_runs_user_message_idx"), |
| 104 | + ), |
| 105 | + runPlan.map((row) => row.detail).join("\n"), |
| 106 | + ); |
| 107 | + }), |
| 108 | + ); |
| 109 | +}); |
0 commit comments