Skip to content

Commit 7ca41e9

Browse files
juliusmarmingevifarclaude
committed
fix(server): pure projection reads run on the reader connection
getThreadRecords, getShellSnapshot, getThreadShell, getRecoveryThreadIds and getSettlementCandidates are pure reads but acquired the writer, so they queued behind any open BEGIN IMMEDIATE. Wrap each in NodeSqliteClient.readOnly; inside an open write transaction they still use the writer. Fixes #23687. Implementation taken from #23689. Co-Authored-By: Victor I <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent ffcf9a0 commit 7ca41e9

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

‎apps/server/src/orchestration-v2/ProjectionStore.ts‎

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ import * as Equal from "effect/Equal";
8383
import * as Layer from "effect/Layer";
8484
import * as Ref from "effect/Ref";
8585
import * as Schema from "effect/Schema";
86+
import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
8687
import * as SqlClient from "effect/sql/SqlClient";
8788
import type * as Statement from "effect/sql/Statement";
8889

@@ -4014,7 +4015,11 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
40144015
AND thread_id IN (${candidates})
40154016
${kind === "queued-runs" ? sql`AND archived_at IS NULL` : sql``}
40164017
ORDER BY updated_at ASC, thread_id ASC
4017-
`;
4018+
`.pipe(
4019+
// A pure read, on the reader connection: startup recovery must not
4020+
// take the write lock.
4021+
NodeSqliteClient.readOnly,
4022+
);
40184023
return rows.map((row) => ThreadId.make(row.thread_id));
40194024
},
40204025
Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })),
@@ -5245,6 +5250,10 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
52455250
filter,
52465251
) =>
52475252
sql.withTransaction(readCanonicalProjection(threadId, undefined, fields, filter)).pipe(
5253+
// The MCP read path runs this for every thread read. It is a pure
5254+
// read, so it belongs on the reader connection: on the writer it
5255+
// contends with the writes that the same load is producing.
5256+
NodeSqliteClient.readOnly,
52485257
Effect.map(
52495258
(projection) =>
52505259
Object.fromEntries([
@@ -6079,7 +6088,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
60796088
);
60806089
}),
60816090
)
6082-
.pipe(Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })));
6091+
.pipe(
6092+
// A pure read, on the reader connection: the settlement sweep must
6093+
// not take the write lock.
6094+
NodeSqliteClient.readOnly,
6095+
Effect.mapError((cause) => new ProjectionStoreSetupError({ cause })),
6096+
);
60836097

60846098
const getThreadsWithPullRequests: ProjectionStoreV2Shape["getThreadsWithPullRequests"] =
60856099
Effect.fn("ProjectionStore.getThreadsWithPullRequests")(
@@ -6404,9 +6418,13 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64046418
);
64056419

64066420
const getShellSnapshot: ProjectionStoreV2Shape["getShellSnapshot"] = (options) =>
6407-
sql
6408-
.withTransaction(readShellSnapshotRows(options))
6409-
.pipe(Effect.mapError(shellSnapshotReadError), Effect.flatMap(decodeShellSnapshot));
6421+
sql.withTransaction(readShellSnapshotRows(options)).pipe(
6422+
// A pure read: the shell list is read far more often than it is
6423+
// written, and on the writer it queues behind the writes.
6424+
NodeSqliteClient.readOnly,
6425+
Effect.mapError(shellSnapshotReadError),
6426+
Effect.flatMap(decodeShellSnapshot),
6427+
);
64106428

64116429
const getThreadShell: ProjectionStoreV2Shape["getThreadShell"] = (threadId) =>
64126430
sql
@@ -6480,7 +6498,12 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
64806498
});
64816499
}),
64826500
)
6483-
.pipe(Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })));
6501+
.pipe(
6502+
// A pure read: the shell row for one thread is read on every
6503+
// thread read, and on the writer it queues behind the writes.
6504+
NodeSqliteClient.readOnly,
6505+
Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })),
6506+
);
64846507

64856508
return {
64866509
apply,

0 commit comments

Comments
 (0)