diff --git a/apps/server/src/persistence/Migrations/038_BackfillProjectionThreadsLatestTurn.test.ts b/apps/server/src/persistence/Migrations/038_BackfillProjectionThreadsLatestTurn.test.ts index c2095d337f..c86b204b7a 100644 --- a/apps/server/src/persistence/Migrations/038_BackfillProjectionThreadsLatestTurn.test.ts +++ b/apps/server/src/persistence/Migrations/038_BackfillProjectionThreadsLatestTurn.test.ts @@ -71,6 +71,32 @@ const insertTurn = ( ) `; +const insertPendingTurnStart = (sql: SqlClient.SqlClient, threadId: string, requestedAt: string) => + sql` + INSERT INTO projection_turns ( + thread_id, + turn_id, + pending_message_id, + assistant_message_id, + state, + requested_at, + started_at, + completed_at, + checkpoint_files_json + ) + VALUES ( + ${threadId}, + NULL, + 'pending-message-038', + NULL, + 'pending', + ${requestedAt}, + NULL, + NULL, + '[]' + ) + `; + const readLatestTurnId = (sql: SqlClient.SqlClient, threadId: string) => sql<{ readonly latestTurnId: string | null }>` SELECT latest_turn_id AS "latestTurnId" @@ -79,56 +105,60 @@ const readLatestTurnId = (sql: SqlClient.SqlClient, threadId: string) => `; layer("038_BackfillProjectionThreadsLatestTurn", (it) => { - it.effect("restores the newest turn on a thread whose pointer was nulled at turn end", () => + // A single test on purpose: `it.layer` shares one in-memory database across + // the block, so a second `it.effect` would find the migration already run + // and assert against a no-op. All fixtures are written between migration 37 + // and 38 so every scenario exercises the actual backfill. + it.effect("backfills nulled pointers without touching set or turnless ones", () => Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; yield* runMigrations({ toMigrationInclusive: 37 }); + // Pointer nulled at turn end (the Hermes case): restore the newest turn. yield* insertThread(sql, "thread-hermes", null); yield* insertTurn(sql, "thread-hermes", "hermes-turn-old", "2026-07-01T00:00:00.000Z"); yield* insertTurn(sql, "thread-hermes", "hermes-turn-new", "2026-07-01T00:05:00.000Z"); + // A correct pointer must survive: the backfill is a heuristic, and the + // projector's own value is authoritative wherever it exists. + yield* insertThread(sql, "thread-codex", "turn-kept"); + yield* insertTurn(sql, "thread-codex", "turn-kept", "2026-07-01T00:00:00.000Z"); + yield* insertTurn(sql, "thread-codex", "turn-newer", "2026-07-01T00:09:00.000Z"); + + // A requested-but-never-adopted turn leaves a `turn_id IS NULL` row, + // and on the stranded threads this migration targets it is often the + // NEWEST row. Selecting it would write NULL over NULL — silently + // skipping the repair of the completed turn underneath. + yield* insertThread(sql, "thread-dangling", null); + yield* insertTurn(sql, "thread-dangling", "turn-completed", "2026-07-01T00:00:00.000Z"); + yield* insertPendingTurnStart(sql, "thread-dangling", "2026-07-01T00:05:00.000Z"); + + // Inventing a turn for a thread that never ran one would make a fresh + // thread look like it had already run one. + yield* insertThread(sql, "thread-empty", null); + + // A thread whose ONLY row is a dangling pending start has no turn to + // restore — it must stay null rather than "repair" to null noisily. + yield* insertThread(sql, "thread-pending-only", null); + yield* insertPendingTurnStart(sql, "thread-pending-only", "2026-07-01T00:05:00.000Z"); + yield* runMigrations({ toMigrationInclusive: 38 }); assert.deepStrictEqual(yield* readLatestTurnId(sql, "thread-hermes"), [ { latestTurnId: "hermes-turn-new" }, ]); - }), - ); - - it.effect("never overwrites a pointer that is already set", () => - Effect.gen(function* () { - // A correct pointer must survive: the backfill is a heuristic, and the - // projector's own value is authoritative wherever it exists. - const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 37 }); - - yield* insertThread(sql, "thread-codex", "turn-kept"); - yield* insertTurn(sql, "thread-codex", "turn-kept", "2026-07-01T00:00:00.000Z"); - yield* insertTurn(sql, "thread-codex", "turn-newer", "2026-07-01T00:09:00.000Z"); - - yield* runMigrations({ toMigrationInclusive: 38 }); - assert.deepStrictEqual(yield* readLatestTurnId(sql, "thread-codex"), [ { latestTurnId: "turn-kept" }, ]); - }), - ); - - it.effect("leaves a thread that has never run a turn at null", () => - Effect.gen(function* () { - // Inventing a turn here would make a fresh thread look like it had - // already run one. - const sql = yield* SqlClient.SqlClient; - yield* runMigrations({ toMigrationInclusive: 37 }); - - yield* insertThread(sql, "thread-empty", null); - - yield* runMigrations({ toMigrationInclusive: 38 }); - + assert.deepStrictEqual(yield* readLatestTurnId(sql, "thread-dangling"), [ + { latestTurnId: "turn-completed" }, + ]); assert.deepStrictEqual(yield* readLatestTurnId(sql, "thread-empty"), [ { latestTurnId: null }, ]); + assert.deepStrictEqual(yield* readLatestTurnId(sql, "thread-pending-only"), [ + { latestTurnId: null }, + ]); }), ); }); diff --git a/apps/server/src/persistence/Migrations/038_BackfillProjectionThreadsLatestTurn.ts b/apps/server/src/persistence/Migrations/038_BackfillProjectionThreadsLatestTurn.ts index ddf3fc36dc..c4aff9d8c5 100644 --- a/apps/server/src/persistence/Migrations/038_BackfillProjectionThreadsLatestTurn.ts +++ b/apps/server/src/persistence/Migrations/038_BackfillProjectionThreadsLatestTurn.ts @@ -28,6 +28,11 @@ import * as Effect from "effect/Effect"; * inventing one. * - Ties on `requested_at` (possible when a turn is steered within the same * millisecond) break on `row_id`, which is monotonic per insert. + * - Rows with a null `turn_id` are skipped: those are pending turn-starts + * (requested but never adopted), and a dangling one is often the *newest* + * row on exactly the threads this bug stranded. Without the filter the + * subquery would select it and write NULL over NULL — silently skipping + * the repair of the completed turn underneath. */ export default Effect.gen(function* () { const sql = yield* SqlClient.SqlClient; @@ -38,6 +43,7 @@ export default Effect.gen(function* () { SELECT turns.turn_id FROM projection_turns turns WHERE turns.thread_id = projection_threads.thread_id + AND turns.turn_id IS NOT NULL ORDER BY turns.requested_at DESC, turns.row_id DESC LIMIT 1 ) @@ -46,6 +52,7 @@ export default Effect.gen(function* () { SELECT 1 FROM projection_turns turns WHERE turns.thread_id = projection_threads.thread_id + AND turns.turn_id IS NOT NULL ) `; });