fix: migration 038 skipped threads with a dangling pending turn-start
`projection_turns` legitimately holds rows with a null `turn_id`: pending turn-starts inserted at request time and never adopted — and on exactly the stranded threads this migration targets, such a row is often the NEWEST one. The backfill subquery ordered by `requested_at DESC` with no turn_id filter, so it selected the pending row and wrote NULL over NULL: a silent no-op instead of a repair of the completed turn underneath. Never corruption (the outer `latest_turn_id IS NULL` guard bounds the damage at nothing), just a missed repair. Both the subquery and the EXISTS now require `turn_id IS NOT NULL`, matching the projector's own `thread.reverted` recompute, which skips those rows explicitly. The test file needed restructuring to prove it: `it.layer` shares one in-memory database across the block, so only the first `it.effect` ever exercised the migration — the second and third asserted against a database where 038 had already run, and any new case would silently do the same. Now a single test seeds all five scenarios (repair, already-set pointer, dangling pending row, never-ran, pending-only) between migration 37 and 38. Co-Authored-By: Claude Fable 5 <[email protected]>
This commit is contained in:
1 parent
a8e904d4ba
commit
d66bc536b8
2 files changed
+68
-31
No files matched your search
+61
-31
@@ -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 },
|
||||
]);
|
||||
}),
|
||||
);
|
||||
});
|
||||
@@ -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
|
||||
)
|
||||
`;
|
||||
});
|
||||
Reference in new issue
Block a user