@devries-cameron20 / t3code Mirror · read-only / commits / 6ed47b538c
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 19:30 UTC

fix(server): route SQLite reads around writer contention (#23902)

Yash Singh committed Oct 23, 2026 at 00:20 UTC 6ed47b538c1ac1d94043d96d3b1faf79c024811d
21 files changed +305 -69
apps/server/src/mcpApps/McpAppModelContext.ts
+2 -1
@@ -1,5 +1,6 @@
1 import type { ThreadId, TurnItemId } from "@t3tools/contracts";
2 import * as Context from "effect/Context";
3 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
4 import * as DateTime from "effect/DateTime";
5 import * as Effect from "effect/Effect";
6 import * as Layer from "effect/Layer";
@@ -83,7 +84,7 @@ export const layer = Layer.effect(
84 OR run.thread_id <> context.thread_id
85 )
86 ORDER BY context.updated_at, context.item_id
86 - `.pipe(Effect.mapError(fail));
87 + `.pipe(NodeSqliteClient.readOnly, Effect.mapError(fail));
88 });
89
90 return McpAppModelContext.of({ set, forThread });
apps/server/src/orchestration-v2/EffectOutbox.ts
+4 -1
@@ -15,6 +15,7 @@ import {
15 ThreadId,
16 } from "@t3tools/contracts";
17 import * as Context from "effect/Context";
18 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
19 import * as DateTime from "effect/DateTime";
20 import * as Deferred from "effect/Deferred";
21 import * as Duration from "effect/Duration";
@@ -420,6 +421,7 @@ export const layer: Layer.Layer<EffectOutboxV2, never, SqlClient.SqlClient> = La
421 WHERE effect_id = ${effectId}
422 LIMIT 1
423 `.pipe(
424 + NodeSqliteClient.readOnly,
425 Effect.flatMap((rows) => {
426 const row = rows[0];
427 return row === undefined
@@ -437,6 +439,7 @@ export const layer: Layer.Layer<EffectOutboxV2, never, SqlClient.SqlClient> = La
439 WHERE command_id = ${commandId}
440 ORDER BY created_at ASC, effect_id ASC
441 `.pipe(
442 + NodeSqliteClient.readOnly,
443 Effect.flatMap((rows) => decodeRows("list", rows)),
444 Effect.mapError((cause) =>
445 isEffectOutboxError(cause)
@@ -571,7 +574,7 @@ export const layer: Layer.Layer<EffectOutboxV2, never, SqlClient.SqlClient> = La
574 SELECT MIN(candidate.available_at) AS available_at
575 FROM orchestration_v2_effect_outbox AS candidate
576 WHERE ${claimableCandidatePredicate()}
574 - `.pipe(Effect.withTracerEnabled(false));
577 + `.pipe(NodeSqliteClient.readOnly, Effect.withTracerEnabled(false));
578 const availableAt = rows[0]?.available_at;
579 if (availableAt === undefined || availableAt === null) return Option.none();
580 const parsed = DateTime.make(availableAt);
apps/server/src/orchestration-v2/FoundationPersistence.test.ts
+80
@@ -3218,3 +3218,83 @@ it.effect.each(["events", "receipts"] as const)(
3218 }).pipe(Effect.provide(maintenanceLayer));
3219 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
3220 );
3221 +
3222 +it.effect("routes projection node lookups to the reader connection", () =>
3223 + Effect.gen(function* () {
3224 + const fs = yield* FileSystem.FileSystem;
3225 + const path = yield* Path.Path;
3226 + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "projection-reader-" });
3227 + const database = SqlitePersistence.layerFromPath(path.join(directory, "statev2.sqlite"));
3228 + const projectionLayer = ProjectionStore.layer.pipe(Layer.provideMerge(database));
3229 + yield* Effect.gen(function* () {
3230 + const projections = yield* ProjectionStore.ProjectionStoreV2;
3231 + const now = yield* DateTime.now;
3232 + const nodeId = NodeId.make("node:reader-routing");
3233 + const node = {
3234 + id: nodeId,
3235 + threadId: ThreadId.make("thread:reader-routing"),
3236 + runId: null,
3237 + parentNodeId: null,
3238 + rootNodeId: nodeId,
3239 + kind: "subagent" as const,
3240 + status: "running" as const,
3241 + countsForRun: false,
3242 + providerThreadId: null,
3243 + providerTurnId: null,
3244 + nativeItemRef: null,
3245 + runtimeRequestId: null,
3246 + checkpointScopeId: null,
3247 + startedAt: now,
3248 + completedAt: null,
3249 + };
3250 + const spans: Array<Tracer.Span> = [];
3251 + const observe: Statement.Transformer = (statement, _sql, _fiber, span) => {
3252 + const [query] = statement.compile();
3253 + if (/orchestration_v2_projection_nodes WHERE node_id/.test(query)) {
3254 + spans.push(span);
3255 + }
3256 + return Effect.succeed(statement);
3257 + };
3258 + yield* Effect.gen(function* () {
3259 + assert.isFalse(yield* projections.isNodeStored(node));
3260 + }).pipe(
3261 + Effect.provideService(Statement.CurrentTransformer, observe),
3262 + Effect.provideService(Statement.SpanPropagationEnabled, true),
3263 + );
3264 + assert.equal(spans.length, 1);
3265 + assert.isTrue(
3266 + spans.every((span) => span.attributes.get("db.connection") === "reader"),
3267 + JSON.stringify(spans.map((span) => span.attributes.get("db.connection"))),
3268 + );
3269 + }).pipe(Effect.provide(projectionLayer));
3270 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
3271 +);
3272 +
3273 +it.effect("loads a shell snapshot while the writer connection is occupied", () =>
3274 + Effect.gen(function* () {
3275 + const fs = yield* FileSystem.FileSystem;
3276 + const path = yield* Path.Path;
3277 + const directory = yield* fs.makeTempDirectoryScoped({ prefix: "shell-reader-" });
3278 + const database = SqlitePersistence.layerFromPath(path.join(directory, "statev2.sqlite"));
3279 + const projectionLayer = ProjectionStore.layer.pipe(Layer.provideMerge(database));
3280 + yield* Effect.gen(function* () {
3281 + const sql = yield* SqlClient.SqlClient;
3282 + const projections = yield* ProjectionStore.ProjectionStoreV2;
3283 + const held = yield* Deferred.make<void>();
3284 + const release = yield* Deferred.make<void>();
3285 + const writer = yield* sql
3286 + .withTransaction(
3287 + sql`SELECT 1`.pipe(
3288 + Effect.andThen(Deferred.succeed(held, undefined)),
3289 + Effect.andThen(Deferred.await(release)),
3290 + ),
3291 + )
3292 + .pipe(Effect.forkChild);
3293 + yield* Deferred.await(held);
3294 + const snapshot = yield* projections.getShellSnapshot();
3295 + yield* Deferred.succeed(release, undefined);
3296 + yield* Fiber.join(writer);
3297 + assert.deepEqual(snapshot.threads, []);
3298 + }).pipe(Effect.provide(projectionLayer));
3299 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
3300 +);
apps/server/src/orchestration-v2/ProjectStore.ts
+4 -1
@@ -9,6 +9,7 @@ import {
9 ThreadEnvMode,
10 } from "@t3tools/contracts";
11 import * as Context from "effect/Context";
12 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
13 import * as Effect from "effect/Effect";
14 import * as Layer from "effect/Layer";
15 import * as Option from "effect/Option";
@@ -191,6 +192,7 @@ export const make = Effect.gen(function* () {
192
193 const get: ProjectStoreV2["Service"]["get"] = (projectId, options) =>
194 selectRows({ projectId, includeDeleted: options?.includeDeleted === true }).pipe(
195 + NodeSqliteClient.readOnly,
196 Effect.map((rows) => Option.fromUndefinedOr(rows[0])),
197 mapError("get"),
198 );
@@ -199,12 +201,13 @@ export const make = Effect.gen(function* () {
201 selectRows({
202 ...(options?.projectIds === undefined ? {} : { projectIds: options.projectIds }),
203 includeDeleted: options?.includeDeleted === true,
202 - }).pipe(mapError("list"));
204 + }).pipe(NodeSqliteClient.readOnly, mapError("list"));
205
206 const findActiveByWorkspaceRoot: ProjectStoreV2["Service"]["findActiveByWorkspaceRoot"] = (
207 workspaceRoot,
208 ) =>
209 selectRows({ workspaceRoot, includeDeleted: false }).pipe(
210 + NodeSqliteClient.readOnly,
211 Effect.map((rows) => Option.fromUndefinedOr(rows[0])),
212 mapError("findActiveByWorkspaceRoot"),
213 );
apps/server/src/orchestration-v2/ProjectionStore.ts
+46 -37
@@ -1,6 +1,7 @@
1 import { applyThreadPullRequestLinkUpdate } from "@t3tools/shared/threadPullRequests";
2 import { projectTurnItemForWire } from "./WireProjection.ts";
3 import * as Stream from "effect/Stream";
4 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
5 import { makeThreadFind, findProjectedThreadItems } from "./ThreadFind.ts";
6 import type {
7 OrchestrationV2SearchThreadInput,
@@ -6511,47 +6512,55 @@ export const layer: Layer.Layer<ProjectionStoreV2, never, SqlClient.SqlClient> =
6512 )
6513 .pipe(Effect.mapError((cause) => new ProjectionStoreReadError({ threadId, cause })));
6514
6515 + const readOnReader =
6516 + <Args extends ReadonlyArray<unknown>, A, E, R>(
6517 + method: (...args: Args) => Effect.Effect<A, E, R>,
6518 + ) =>
6519 + (...args: Args): Effect.Effect<A, E, R> =>
6520 + method(...args).pipe(NodeSqliteClient.readOnly);
6521 +
6522 return {
6523 apply,
6516 - getShellSnapshot,
6517 - readShellSnapshot,
6518 - getThreadShell,
6519 - getThread,
6520 - getSettlementCandidates,
6521 - getThreadsWithPullRequests,
6522 - getThreadWorktreePaths,
6524 + getShellSnapshot: readOnReader(getShellSnapshot),
6525 + readShellSnapshot: readOnReader(readShellSnapshot),
6526 + getThreadShell: readOnReader(getThreadShell),
6527 + getThread: readOnReader(getThread),
6528 + getSettlementCandidates: readOnReader(getSettlementCandidates),
6529 + getThreadsWithPullRequests: readOnReader(getThreadsWithPullRequests),
6530 + getThreadWorktreePaths: readOnReader(getThreadWorktreePaths),
6531 getThreadProjection,
6524 - getTurnStartContext,
6525 - getTurnStartHistory,
6526 - getRuntimeRecoveryProjection,
6527 - getRunningTurnContext,
6528 - getThreadProviderContext,
6529 - getRuntimeResponseContext,
6530 - getCheckpointContext,
6531 - getCheckpointCaptureContext,
6532 - getCheckpointRollbackContext,
6533 - getRunMessage,
6534 - canStartQueuedRun,
6535 - getPendingNativeUserInputs,
6536 - hasUnpairedRunInterruptRequest,
6537 - isNodeStored,
6538 - getMessageCount,
6539 - getNextTurnItemOrdinal,
6540 - getTurnItem,
6541 - getThreadRecords,
6542 - getRuntimeRequest,
6543 - getPlan,
6544 - getProviderControlContext,
6545 - getLimitRecoveryCandidates,
6546 - getRecoveryThreadIds,
6547 - getUnreadableThreadIds,
6548 - getThreadSnapshot,
6549 - getThreadSnapshotWindow,
6550 - getTimelinePage,
6551 - getThreadHistoryPage,
6552 - searchThread,
6532 + getTurnStartContext: readOnReader(getTurnStartContext),
6533 + getTurnStartHistory: readOnReader(getTurnStartHistory),
6534 + getRuntimeRecoveryProjection: readOnReader(getRuntimeRecoveryProjection),
6535 + getRunningTurnContext: readOnReader(getRunningTurnContext),
6536 + getThreadProviderContext: readOnReader(getThreadProviderContext),
6537 + getRuntimeResponseContext: readOnReader(getRuntimeResponseContext),
6538 + getCheckpointContext: readOnReader(getCheckpointContext),
6539 + getCheckpointCaptureContext: readOnReader(getCheckpointCaptureContext),
6540 + getCheckpointRollbackContext: readOnReader(getCheckpointRollbackContext),
6541 + getRunMessage: readOnReader(getRunMessage),
6542 + canStartQueuedRun: readOnReader(canStartQueuedRun),
6543 + getPendingNativeUserInputs: readOnReader(getPendingNativeUserInputs),
6544 + hasUnpairedRunInterruptRequest: readOnReader(hasUnpairedRunInterruptRequest),
6545 + isNodeStored: readOnReader(isNodeStored),
6546 + getMessageCount: readOnReader(getMessageCount),
6547 + getNextTurnItemOrdinal: readOnReader(getNextTurnItemOrdinal),
6548 + getTurnItem: readOnReader(getTurnItem),
6549 + getThreadRecords: (threadId, fields, filter) =>
6550 + getThreadRecords(threadId, fields, filter).pipe(NodeSqliteClient.readOnly),
6551 + getRuntimeRequest: readOnReader(getRuntimeRequest),
6552 + getPlan: readOnReader(getPlan),
6553 + getProviderControlContext: readOnReader(getProviderControlContext),
6554 + getLimitRecoveryCandidates: readOnReader(getLimitRecoveryCandidates),
6555 + getRecoveryThreadIds: readOnReader(getRecoveryThreadIds),
6556 + getUnreadableThreadIds: readOnReader(getUnreadableThreadIds),
6557 + getThreadSnapshot: readOnReader(getThreadSnapshot),
6558 + getThreadSnapshotWindow: readOnReader(getThreadSnapshotWindow),
6559 + getTimelinePage: readOnReader(getTimelinePage),
6560 + getThreadHistoryPage: readOnReader(getThreadHistoryPage),
6561 + searchThread: readOnReader(searchThread),
6562 searchThreadStream,
6554 - getThreadAttachmentIds,
6563 + getThreadAttachmentIds: readOnReader(getThreadAttachmentIds),
6564 } satisfies ProjectionStoreV2Shape;
6565 }),
6566 );
apps/server/src/orchestration-v2/ThreadFind.ts
+2 -1
@@ -14,6 +14,7 @@ import * as Data from "effect/Data";
14 import * as Effect from "effect/Effect";
15 import * as Schema from "effect/Schema";
16 import * as Stream from "effect/Stream";
17 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
18 import * as SqlClient from "effect/sql/SqlClient";
19 import {
20 ProjectionStoreReadError,
@@ -345,7 +346,7 @@ export const makeThreadFind = Effect.fn("makeThreadFind")(function* (
346 }
347 return { payloads, cwd, sequence, cacheKey, sourceThreadIds };
348 },
348 - sql.withTransaction,
349 + (effect) => sql.withTransaction(effect).pipe(NodeSqliteClient.readOnly),
350 (effect, input) =>
351 effect.pipe(
352 Effect.mapError((cause) =>
apps/server/src/orchestration-v2/legacy/LegacyV1ThreadImporter.ts
+6 -4
@@ -22,6 +22,7 @@ import {
22 TurnItemId,
23 } from "@t3tools/contracts";
24 import * as KeyedLock from "@t3tools/shared/KeyedLock";
25 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
26 import * as Cause from "effect/Cause";
27 import * as Context from "effect/Context";
28 import * as DateTime from "effect/DateTime";
@@ -359,7 +360,7 @@ const make = Effect.gen(function* () {
360 WHERE thread_id = ${threadId}
361 AND role IN ('user', 'assistant')
362 ORDER BY created_at ASC, message_id ASC
362 - `;
363 + `.pipe(NodeSqliteClient.readOnly);
364
365 const listShellMessages = Effect.fn("LegacyV1ThreadImporter.listShellMessages")(function* (
366 threadId: ThreadId,
@@ -669,6 +670,7 @@ const make = Effect.gen(function* () {
670 WHERE legacy_import.transcript_imported_at IS NULL
671 )
672 `.pipe(
673 + NodeSqliteClient.readOnly,
674 Effect.map((rows) => rows[0]?.count ?? 0),
675 Effect.mapError(
676 (cause) => new LegacyV1ThreadImportError({ operation: "inspect pending", cause }),
@@ -690,7 +692,7 @@ const make = Effect.gen(function* () {
692 FROM orchestration_v2_legacy_imports
693 WHERE thread_id = ${threadId}
694 LIMIT 1
693 - `;
695 + `.pipe(NodeSqliteClient.readOnly);
696 const imported = imports[0];
697 if (imported === undefined || imported.transcript_imported_at !== null) {
698 if (imported !== undefined) {
@@ -708,7 +710,7 @@ const make = Effect.gen(function* () {
710 AND aggregate_kind = 'thread'
711 AND stream_id = ${threadId}
712 AND event_id LIKE ${`${IMPORT_EVENT_PREFIX}:message:%`}
711 - `;
713 + `.pipe(NodeSqliteClient.readOnly);
714 const existing = new Set(existingRows.map((row) => row.event_id));
715 const missing = messages.filter(
716 (message) => !existing.has(`${IMPORT_EVENT_PREFIX}:message:${message.message_id}`),
@@ -772,7 +774,7 @@ const make = Effect.gen(function* () {
774 FROM orchestration_v2_legacy_imports
775 WHERE transcript_imported_at IS NULL
776 ORDER BY shell_imported_at ASC, thread_id ASC
775 - `;
777 + `.pipe(NodeSqliteClient.readOnly);
778 let importedThreadCount = 0;
779 let importedMessageCount = 0;
780 for (const row of rows) {
apps/server/src/persistence/AuthPairingLinks.ts
+3
@@ -1,4 +1,5 @@
1 import * as Context from "effect/Context";
2 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
@@ -294,6 +295,7 @@ export const make = Effect.gen(function* () {
295
296 const listActive: AuthPairingLinkRepository["Service"]["listActive"] = (input) =>
297 listActivePairingLinkRows(input).pipe(
298 + NodeSqliteClient.readOnly,
299 Effect.mapError(
300 toPersistenceSqlOrDecodeError(
301 "AuthPairingLinkRepository.listActive:query",
@@ -329,6 +331,7 @@ export const make = Effect.gen(function* () {
331
332 const getByCredential: AuthPairingLinkRepository["Service"]["getByCredential"] = (input) =>
333 getPairingLinkRowByCredential(input).pipe(
334 + NodeSqliteClient.readOnly,
335 Effect.mapError(
336 toPersistenceSqlOrDecodeError(
337 "AuthPairingLinkRepository.getByCredential:query",
apps/server/src/persistence/AuthSessions.ts
+3
@@ -1,4 +1,5 @@
1 import * as Context from "effect/Context";
2 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
@@ -416,6 +417,7 @@ export const make = Effect.gen(function* () {
417
418 const getById: AuthSessionRepository["Service"]["getById"] = (input) =>
419 getSessionRowById(input).pipe(
420 + NodeSqliteClient.readOnly,
421 Effect.mapError(
422 toPersistenceSqlOrDecodeError(
423 "AuthSessionRepository.getById:query",
@@ -443,6 +445,7 @@ export const make = Effect.gen(function* () {
445
446 const listActive: AuthSessionRepository["Service"]["listActive"] = (input) =>
447 listActiveSessionRows(input).pipe(
448 + NodeSqliteClient.readOnly,
449 Effect.mapError(
450 toPersistenceSqlOrDecodeError(
451 "AuthSessionRepository.listActive:query",
apps/server/src/persistence/OrchestrationCommandReceipts.ts
+2
@@ -1,4 +1,5 @@
1 import { CommandId, IsoDateTime, NonNegativeInt, ProjectId, ThreadId } from "@t3tools/contracts";
2 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as SqlClient from "effect/sql/SqlClient";
4 import * as SqlSchema from "effect/sql/SqlSchema";
5 import * as Context from "effect/Context";
@@ -149,6 +150,7 @@ const makeOrchestrationCommandReceiptRepository = Effect.gen(function* () {
150 input,
151 ) =>
152 findReceiptByCommandId(input).pipe(
153 + NodeSqliteClient.readOnly,
154 Effect.mapError(
155 toPersistenceSqlError("OrchestrationCommandReceiptRepository.getByCommandId:query"),
156 ),
apps/server/src/persistence/OrchestrationEventStore.ts
+6 -1
@@ -15,6 +15,7 @@ import {
15 type OrchestrationV2DomainEvent,
16 } from "@t3tools/contracts";
17 import * as Context from "effect/Context";
18 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
19 import * as SqlClient from "effect/sql/SqlClient";
20 import * as SqlSchema from "effect/sql/SqlSchema";
21 import * as Effect from "effect/Effect";
@@ -441,7 +442,7 @@ const makeEventStore = Effect.gen(function* () {
442 ])}
443 ORDER BY sequence ASC
444 LIMIT ${input.limit}
444 - `;
445 + `.pipe(NodeSqliteClient.readOnly);
446
447 const appendAgentEvents: OrchestrationEventStore["Service"]["appendAgentEvents"] = (input) =>
448 Effect.forEach(
@@ -617,6 +618,7 @@ const makeEventStore = Effect.gen(function* () {
618 LIMIT ${Math.max(0, Math.floor(input.maxEvents)) + 1}
619 )
620 `.pipe(
621 + NodeSqliteClient.readOnly,
622 Effect.mapError(toPersistenceSqlError("OrchestrationEventStore.getAgentReplayStats:query")),
623 Effect.map((rows) => ({
624 eventCount: rows[0]?.eventCount ?? 0,
@@ -638,6 +640,7 @@ const makeEventStore = Effect.gen(function* () {
640 OR (application_event_version = 2 AND aggregate_kind = 'thread')
641 )
642 `.pipe(
643 + NodeSqliteClient.readOnly,
644 Effect.mapError(toPersistenceSqlError("OrchestrationEventStore.getReplayStats:query")),
645 Effect.map((rows) => ({
646 eventCount: rows[0]?.eventCount ?? 0,
@@ -660,6 +663,7 @@ const makeEventStore = Effect.gen(function* () {
663 AND aggregate_kind = 'thread'
664 ${threadId === undefined ? sql`` : sql`AND stream_id = ${threadId}`}
665 `.pipe(
666 + NodeSqliteClient.readOnly,
667 Effect.map((rows) => rows[0]?.sequence ?? 0),
668 Effect.mapError(toPersistenceSqlError("OrchestrationEventStore.latestAgentSequence:query")),
669 );
@@ -671,6 +675,7 @@ const makeEventStore = Effect.gen(function* () {
675 WHERE aggregate_kind = 'project'
676 OR (application_event_version = 2 AND aggregate_kind = 'thread')
677 `.pipe(
678 + NodeSqliteClient.readOnly,
679 Effect.map((rows) => rows[0]?.sequence ?? 0),
680 Effect.mapError(
681 toPersistenceSqlError("OrchestrationEventStore.latestApplicationSequence:query"),
apps/server/src/persistence/ProviderSessionRuntime.ts
+3
@@ -1,4 +1,5 @@
1 import * as Arr from "effect/Array";
2 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Context from "effect/Context";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
@@ -357,6 +358,7 @@ export const make = Effect.gen(function* () {
358
359 const getByThreadId: ProviderSessionRuntimeRepository["Service"]["getByThreadId"] = (input) =>
360 getRuntimeRowByThreadId(input).pipe(
361 + NodeSqliteClient.readOnly,
362 Effect.mapError(
363 toPersistenceSqlOrDecodeError(
364 "ProviderSessionRuntimeRepository.getByThreadId:query",
@@ -384,6 +386,7 @@ export const make = Effect.gen(function* () {
386
387 const list: ProviderSessionRuntimeRepository["Service"]["list"] = (options) =>
388 listRuntimeRows({ excludeStopped: options?.excludeStopped === true }).pipe(
389 + NodeSqliteClient.readOnly,
390 Effect.mapError(
391 toPersistenceSqlOrDecodeError(
392 "ProviderSessionRuntimeRepository.list:query",
apps/server/src/persistence/PullRequestFilesViewed.ts
+2
@@ -1,4 +1,5 @@
1 import * as Context from "effect/Context";
2 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Schema from "effect/Schema";
@@ -90,6 +91,7 @@ const make = Effect.gen(function* () {
91 return PullRequestFilesViewedRepository.of({
92 list: (input) =>
93 listRows(input).pipe(
94 + NodeSqliteClient.readOnly,
95 Effect.map((rows) => ({
96 files: rows.slice(0, MAX_FILES_VIEWED_ROWS),
97 truncated: rows.length > MAX_FILES_VIEWED_ROWS,
apps/server/src/persistence/PullRequestReviewGuides.ts
+2
@@ -1,4 +1,5 @@
1 import * as Context from "effect/Context";
2 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
@@ -52,6 +53,7 @@ const make = Effect.gen(function* () {
53 return PullRequestReviewGuideRepository.of({
54 get: (key) =>
55 findGuide(key).pipe(
56 + NodeSqliteClient.readOnly,
57 Effect.map(Option.map((row) => row.guide)),
58 Effect.mapError((cause) =>
59 Schema.isSchemaError(cause)
apps/server/src/pullRequest/linkedThreads.ts
+2 -1
@@ -8,6 +8,7 @@ import {
8 type ThreadPullRequestKey,
9 } from "@t3tools/contracts";
10 import * as Effect from "effect/Effect";
11 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
12 import * as Schema from "effect/Schema";
13 import * as SqlClient from "effect/sql/SqlClient";
14
@@ -39,7 +40,7 @@ export const listLinkedPullRequestThreads = Effect.fn("listLinkedPullRequestThre
40 AND json_extract(link.value, '$.source') != 'stack-dismissed'
41 AND t.deleted_at IS NULL
42 ORDER BY "updatedAt" DESC, id ASC
42 - `;
43 + `.pipe(NodeSqliteClient.readOnly);
44 const threads = rows
45 .filter((row) => threadPullRequestKeysEqual(row, key))
46 .map(({ id, projectId, title, archivedAt }) => ({ id, projectId, title, archivedAt }));
apps/server/src/scheduledTasks/ScheduledTaskService.ts
+15 -9
@@ -29,6 +29,7 @@ import * as Crypto from "effect/Crypto";
29 import * as Data from "effect/Data";
30 import * as DateTime from "effect/DateTime";
31 import * as Effect from "effect/Effect";
32 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
33 import * as Exit from "effect/Exit";
34 import * as Duration from "effect/Duration";
35 import * as Metric from "effect/Metric";
@@ -370,7 +371,7 @@ export const listDueTasks = Effect.fn("ScheduledTaskService.listDueTasks")(funct
371 WHERE enabled = 1 AND next_run_at IS NOT NULL
372 AND next_run_at <= ${iso(now)} AND last_run_status <> 'running'
373 ORDER BY next_run_at ASC, task_id ASC
373 - `;
374 + `.pipe(NodeSqliteClient.readOnly);
375 const tasks: ScheduledTask[] = [];
376 for (const row of rows) {
377 const decoded = yield* Effect.result(decodeRow(row));
@@ -431,7 +432,8 @@ export const layer = Layer.effect(
432 const changesPubSub = yield* PubSub.sliding<void>(1);
433 const notifyChanged = PubSub.publish(changesPubSub, undefined).pipe(Effect.asVoid);
434
434 - const selectAllRows = () => sql<ScheduledTaskRow>`
435 + const selectAllRows = () =>
436 + sql<ScheduledTaskRow>`
437 SELECT
438 task_id,
439 title,
@@ -459,7 +461,7 @@ export const layer = Layer.effect(
461 -- Not updated_at: toggles, runs and edits rewrite it, and clients render
462 -- this order as-is, so rows would move under the pointer.
463 ORDER BY created_at DESC, task_id ASC
462 - `;
464 + `.pipe(NodeSqliteClient.readOnly);
465
466 const listRows = Effect.fn("ScheduledTaskService.listRows")(function* () {
467 const rows = yield* selectAllRows();
@@ -467,7 +469,8 @@ export const layer = Layer.effect(
469 return yield* Effect.forEach(rows, (row) => decodeRow(row, origin), { concurrency: 1 });
470 });
471
470 - const getRows = (id: ScheduledTaskId) => sql<ScheduledTaskRow>`
472 + const getRows = (id: ScheduledTaskId) =>
473 + sql<ScheduledTaskRow>`
474 SELECT
475 task_id,
476 title,
@@ -493,7 +496,7 @@ export const layer = Layer.effect(
496 webhook_secret
497 FROM scheduled_tasks
498 WHERE task_id = ${id}
496 - `;
499 + `.pipe(NodeSqliteClient.readOnly);
500
501 const findTask = Effect.fn("ScheduledTaskService.findTask")(function* (id: ScheduledTaskId) {
502 const rows = yield* getRows(id).pipe(
@@ -768,8 +771,9 @@ export const layer = Layer.effect(
771 }
772 if (
773 webhook !== undefined &&
771 - (yield* sql`SELECT 1 FROM scheduled_task_webhook_pending WHERE delivery_id = ${webhook.deliveryId} AND state = 'completed'`)
772 - .length > 0
774 + (yield* sql`SELECT 1 FROM scheduled_task_webhook_pending WHERE delivery_id = ${webhook.deliveryId} AND state = 'completed'`.pipe(
775 + NodeSqliteClient.readOnly,
776 + )).length > 0
777 )
778 return { task: { ...active, lastRunStatus: "succeeded" as const }, threadId: null };
779 const parsedNextRunAt =
@@ -1351,6 +1355,7 @@ export const layer = Layer.effect(
1355 FROM scheduled_task_webhook_deliveries d WHERE task_id = ${input.id}
1356 ORDER BY received_at DESC, rowid DESC
1357 `.pipe(
1358 + NodeSqliteClient.readOnly,
1359 Effect.map((rows) => ({
1360 deliveries: rows
1361 .filter(
@@ -1371,6 +1376,7 @@ export const layer = Layer.effect(
1376 WHERE p.delivery_id = d.delivery_id AND p.state IN ('pending', 'dispatching')) AS pending_window
1377 FROM scheduled_task_webhook_deliveries d WHERE task_id = ${input.id} AND delivery_id = ${input.deliveryId}
1378 `.pipe(
1379 + NodeSqliteClient.readOnly,
1380 Effect.mapError((cause) =>
1381 taskError("Could not load webhook delivery.", { taskId: input.id, cause }),
1382 ),
@@ -2065,7 +2071,7 @@ export const layer = Layer.effect(
2071 yield* sql`UPDATE scheduled_task_webhook_pending SET state = 'dispatching' WHERE state = 'pending' AND due_at <= ${now}`;
2072 const tasks = yield* sql<{ task_id: ScheduledTaskId }>`
2073 SELECT DISTINCT task_id FROM scheduled_task_webhook_pending WHERE state IN ('dispatching', 'running', 'completed', 'failed')
2068 - `;
2074 + `.pipe(NodeSqliteClient.readOnly);
2075 yield* Effect.forEach(
2076 tasks,
2077 ({ task_id: taskId }) =>
@@ -2089,7 +2095,7 @@ export const layer = Layer.effect(
2095 }>`
2096 SELECT delivery_id, task_json, request_json, state FROM scheduled_task_webhook_pending
2097 WHERE task_id = ${taskId} AND state IN ('dispatching', 'running', 'completed', 'failed') ORDER BY due_at, rowid LIMIT 1
2092 - `;
2098 + `.pipe(NodeSqliteClient.readOnly);
2099 if (entry === undefined) return;
2100 const remove = sql`DELETE FROM scheduled_task_webhook_pending WHERE delivery_id = ${entry.delivery_id}`;
2101 if (entry.state === "completed" || entry.state === "failed") {
apps/server/src/serverSettings.ts
+3
@@ -25,6 +25,7 @@ import * as Deferred from "effect/Deferred";
25 import * as Duration from "effect/Duration";
26 import * as Equal from "effect/Equal";
27 import * as Effect from "effect/Effect";
28 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
29 import * as Exit from "effect/Exit";
30 import * as FileSystem from "effect/FileSystem";
31 import * as Layer from "effect/Layer";
@@ -711,6 +712,7 @@ const make = Effect.gen(function* () {
712 FROM provider_session_runtime
713 WHERE provider_name IN ('cursor', 'grok', 'opencode')
714 `.pipe(
715 + NodeSqliteClient.readOnly,
716 Effect.mapError(
717 (cause) =>
718 new ServerSettingsError({
@@ -734,6 +736,7 @@ const make = Effect.gen(function* () {
736 FROM projection_projects
737 WHERE deleted_at IS NULL
738 `.pipe(
739 + NodeSqliteClient.readOnly,
740 Effect.mapError(
741 (cause) =>
742 new ServerSettingsError({
apps/server/src/storageCleanup.ts
+3 -3
@@ -737,7 +737,7 @@ const make = Effect.gen(function* () {
737 FROM orchestration_v2_projection_threads t
738 JOIN projection_projects p ON p.project_id = t.project_id
739 WHERE t.deleted_at IS NOT NULL
740 - `;
740 + `.pipe(NodeSqliteClient.readOnly);
741 const deletedThreads = (yield* Effect.forEach(deletedRows, (row) =>
742 decodeCleanupThread(row.payload_json).pipe(
743 Effect.map((thread) => ({ ...thread, workspaceRoot: row.workspaceRoot })),
@@ -938,7 +938,7 @@ const make = Effect.gen(function* () {
938 const pendingCleanup = yield* sql`
939 SELECT 1 FROM orchestration_v2_effect_outbox
940 WHERE thread_id = ${thread.id} AND status NOT IN ('succeeded', 'cancelled') LIMIT 1
941 - `;
941 + `.pipe(NodeSqliteClient.readOnly);
942 if (pendingCleanup.length > 0) return "thread deletion is still pending";
943 } else {
944 const latestShells = yield* Effect.forEach(latest, (entry) =>
@@ -970,7 +970,7 @@ const make = Effect.gen(function* () {
970 const sessionRows = yield* sql<{ payload_json: string }>`
971 SELECT payload_json FROM orchestration_v2_projection_provider_sessions
972 WHERE status != 'stopped'
973 - `;
973 + `.pipe(NodeSqliteClient.readOnly);
974 const sessions = yield* Effect.forEach(sessionRows, (row) =>
975 decodeCleanupSession(row.payload_json),
976 );
apps/server/src/workspace/KnownWorkspaces.ts
+5 -1
@@ -8,6 +8,7 @@
8 * @module KnownWorkspaces
9 */
10 import * as HostProcess from "@t3tools/shared/HostProcess";
11 +import * as NodeSqliteClient from "@t3tools/shared/nodeSqliteClient";
12 import * as Context from "effect/Context";
13 import * as Effect from "effect/Effect";
14 import * as FileSystem from "effect/FileSystem";
@@ -105,7 +106,10 @@ export const make = Effect.gen(function* () {
106 project_id AS "projectId"
107 FROM orchestration_v2_projection_threads
108 WHERE deleted_at IS NULL AND json_extract(payload_json, '$.worktreePath') IS NOT NULL
108 - `.pipe(Effect.orElseSucceed(() => []));
109 + `.pipe(
110 + NodeSqliteClient.readOnly,
111 + Effect.orElseSucceed(() => []),
112 + );
113 const projectWorktrees = new Map<string, ReadonlyArray<string>>();
114 const isProjectWorktree = Effect.fnUntraced(function* (
115 projectId: string,
packages/shared/src/nodeSqliteClient.test.ts
+37
@@ -8,6 +8,8 @@ import * as Fiber from "effect/Fiber";
8 import * as FileSystem from "effect/FileSystem";
9 import * as Path from "effect/Path";
10 import * as SqlClient from "effect/sql/SqlClient";
11 +import * as Statement from "effect/sql/Statement";
12 +import type * as Tracer from "effect/Tracer";
13 import { vi } from "vite-plus/test";
14
15 import * as SqliteClient from "./nodeSqliteClient.ts";
@@ -64,6 +66,41 @@ const countEntries = (sql: SqlClient.SqlClient) =>
66 const layer = it.layer(SqliteClient.layer({ filename: ":memory:" }));
67
68 layer("NodeSqliteClient", (it) => {
69 + it.effect("records the writer connection owner on a waiting query", () =>
70 + Effect.gen(function* () {
71 + const sql = yield* SqlClient.SqlClient;
72 + const held = yield* Deferred.make<void>();
73 + const release = yield* Deferred.make<void>();
74 + const holder = yield* sql
75 + .withTransaction(
76 + sql`SELECT 1`.pipe(
77 + Effect.andThen(Deferred.succeed(held, undefined)),
78 + Effect.andThen(Deferred.await(release)),
79 + ),
80 + )
81 + .pipe(Effect.withSpan("test.writerHolder"), Effect.forkChild);
82 + yield* Deferred.await(held);
83 +
84 + const waitingSpan = yield* Deferred.make<Tracer.Span>();
85 + const observeWaiter: Statement.Transformer = (statement, _sql, _fiber, span) =>
86 + Deferred.succeed(waitingSpan, span).pipe(Effect.as(statement));
87 + const waiter = yield* sql`SELECT 1`.pipe(
88 + Effect.provideService(Statement.CurrentTransformer, observeWaiter),
89 + Effect.provideService(Statement.SpanPropagationEnabled, true),
90 + (effect) => Effect.forkChild(effect, { startImmediately: true }),
91 + );
92 + const span = yield* Deferred.await(waitingSpan);
93 + yield* Deferred.succeed(release, undefined);
94 + yield* Fiber.join(holder);
95 + yield* Fiber.join(waiter);
96 +
97 + assert.equal(span.attributes.get("db.lock_owner_at_start.name"), "test.writerHolder");
98 + assert.typeOf(span.attributes.get("db.lock_owner_at_start.fiber_id"), "number");
99 + assert.typeOf(span.attributes.get("db.lock_owner_at_start.trace_id"), "string");
100 + assert.typeOf(span.attributes.get("db.lock_owner_at_start.span_id"), "string");
101 + }),
102 + );
103 +
104 it.effect("retries preparing a query after the missing schema becomes available", () =>
105 Effect.gen(function* () {
106 const sql = yield* SqlClient.SqlClient;
packages/shared/src/nodeSqliteClient.ts
+75 -9
@@ -70,15 +70,53 @@ interface Acquired {
70 readonly lockWaitMs: number;
71 }
72
73 -const annotateAcquired = (fiber: Fiber.Fiber<unknown, unknown>, acquired: Acquired | undefined) => {
73 +interface ConnectionOwner {
74 + readonly fiberId: number;
75 + readonly name: string;
76 + readonly traceId?: string;
77 + readonly spanId?: string;
78 +}
79 +
80 +const connectionOwner = (fiber: Fiber.Fiber<unknown, unknown>): ConnectionOwner => {
81 + const current = Context.getOrUndefined(fiber.context, Tracer.ParentSpan);
82 + if (current?._tag !== "Span") return { fiberId: fiber.id, name: "untraced" };
83 + let span: Tracer.Span = current;
84 + while (span.name === "sqlite" || span.name === "sql.transaction") {
85 + const parent: Tracer.AnySpan | undefined = Option.getOrUndefined(span.parent);
86 + if (parent?._tag !== "Span") break;
87 + span = parent;
88 + }
89 + return { fiberId: fiber.id, name: span.name, traceId: span.traceId, spanId: span.spanId };
90 +};
91 +
92 +const annotateAcquired = (
93 + fiber: Fiber.Fiber<unknown, unknown>,
94 + acquired: Acquired | undefined,
95 + ownerAtWaitStart?: ConnectionOwner,
96 +) => {
97 const span = Context.getOrUndefined(fiber.context, Tracer.ParentSpan);
98 if (span?._tag !== "Span" || acquired === undefined) return;
99 span.attribute("db.connection", acquired.connection);
100 span.attribute("db.lock_wait_ms", acquired.lockWaitMs);
101 + if (ownerAtWaitStart !== undefined) {
102 + span.attribute("db.lock_owner_at_start.fiber_id", ownerAtWaitStart.fiberId);
103 + span.attribute("db.lock_owner_at_start.name", ownerAtWaitStart.name);
104 + if (ownerAtWaitStart.traceId !== undefined) {
105 + span.attribute("db.lock_owner_at_start.trace_id", ownerAtWaitStart.traceId);
106 + }
107 + if (ownerAtWaitStart.spanId !== undefined) {
108 + span.attribute("db.lock_owner_at_start.span_id", ownerAtWaitStart.spanId);
109 + }
110 + }
111 };
112
113 const TransactionAcquire = Context.Reference<
81 - { acquired?: Acquired; restore: typeof Effect.interruptible | undefined } | undefined
114 + | {
115 + acquired?: Acquired;
116 + ownerAtWaitStart: ConnectionOwner | undefined;
117 + restore: typeof Effect.interruptible | undefined;
118 + }
119 + | undefined
120 >("@t3tools/shared/nodeSqliteClient/TransactionAcquire", { defaultValue: () => undefined });
121
122 export class UnsupportedNodeSqliteVersionError extends Schema.TaggedError<UnsupportedNodeSqliteVersionError>()(
@@ -1070,6 +1108,7 @@ const make = Effect.fn("makeWithDatabase")(function* (
1108 const readerSemaphore = yield* Semaphore.make(1);
1109
1110 const writer = { name: "writer" as const, lock: semaphore, connection };
1111 + let activeWriter: ConnectionOwner | undefined;
1112 const route = Effect.withFiber((fiber) =>
1113 acquireReader !== undefined && Context.get(fiber.context, ReadOnly)
1114 ? Effect.map(acquireReader, (readerConnection) =>
@@ -1096,19 +1135,30 @@ const make = Effect.fn("makeWithDatabase")(function* (
1135 Effect.withFiber((fiber) => {
1136 const clock = fiber.getRef(Clock.Clock);
1137 const start = clock.monotonicTimeNanosUnsafe();
1138 + const ownerAtWaitStart = target.name === "writer" ? activeWriter : undefined;
1139 + const owner = target.name === "writer" ? connectionOwner(fiber) : undefined;
1140 const useConnection = Effect.suspend(() => {
1141 + if (target.name === "writer") activeWriter = owner;
1142 if (
1143 fiber.getRef(References.TracerEnabled) &&
1144 fiber.getRef(Statement.SpanPropagationEnabled)
1145 ) {
1104 - annotateAcquired(fiber, acquired(target, clock, start));
1146 + annotateAcquired(fiber, acquired(target, clock, start), ownerAtWaitStart);
1147 }
1148 return use(target.connection);
1149 });
1150 + const useOwnedConnection =
1151 + target.name === "writer"
1152 + ? Effect.onExit(useConnection, () =>
1153 + Effect.sync(() => {
1154 + if (activeWriter === owner) activeWriter = undefined;
1155 + }),
1156 + )
1157 + : useConnection;
1158 if (!options.checkpointWorker || target.name !== "writer") {
1109 - return target.lock.withPermits(1)(useConnection);
1159 + return target.lock.withPermits(1)(useOwnedConnection);
1160 }
1111 - const writerUse = Effect.onExit(useConnection, (exit) =>
1161 + const writerUse = Effect.onExit(useOwnedConnection, (exit) =>
1162 Effect.sync(() => {
1163 writerBorrowsSinceYield = Exit.hasInterrupts(exit)
1164 ? 0
@@ -1142,9 +1192,20 @@ const make = Effect.fn("makeWithDatabase")(function* (
1192 const clock = fiber.getRef(Clock.Clock);
1193 const start = clock.monotonicTimeNanosUnsafe();
1194 const pending = fiber.getRef(TransactionAcquire);
1195 + const ownerAtWaitStart = target.name === "writer" ? activeWriter : undefined;
1196 + const owner = target.name === "writer" ? connectionOwner(fiber) : undefined;
1197 const acquire = Effect.tap((pending?.restore ?? restore)(target.lock.take(1)), () => {
1146 - if (pending !== undefined) pending.acquired = acquired(target, clock, start);
1147 - return Scope.addFinalizer(scope, target.lock.release(1));
1198 + if (target.name === "writer") activeWriter = owner;
1199 + if (pending !== undefined) {
1200 + pending.acquired = acquired(target, clock, start);
1201 + pending.ownerAtWaitStart = ownerAtWaitStart;
1202 + }
1203 + return Scope.addFinalizer(
1204 + scope,
1205 + Effect.sync(() => {
1206 + if (target.name === "writer" && activeWriter === owner) activeWriter = undefined;
1207 + }).pipe(Effect.andThen(target.lock.release(1))),
1208 + );
1209 });
1210 return Effect.as(
1211 // A scheduler yield after taking the permit must not precede its release finalizer.
@@ -1174,11 +1235,16 @@ const make = Effect.fn("makeWithDatabase")(function* (
1235 withTransaction: <A, E, R>(effect: Effect.Effect<A, E, R>) =>
1236 Effect.uninterruptibleMask((restore) => {
1237 // SqlClient masks setup; acquisition must preserve the caller's interruptibility.
1177 - const pending: { acquired?: Acquired; restore: typeof Effect.interruptible | undefined } = {
1238 + const pending: {
1239 + acquired?: Acquired;
1240 + ownerAtWaitStart: ConnectionOwner | undefined;
1241 + restore: typeof Effect.interruptible | undefined;
1242 + } = {
1243 + ownerAtWaitStart: undefined,
1244 restore,
1245 };
1246 const body = Effect.withFiber<A, E, R>((fiber) => {
1181 - annotateAcquired(fiber, pending.acquired);
1247 + annotateAcquired(fiber, pending.acquired, pending.ownerAtWaitStart);
1248 pending.restore = undefined;
1249 return effect;
1250 });