@devries-cameron20 / t3code Mirror · read-only / commits / daed91f264
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 21, 17:15 UTC

fix(shared): batch SQLite worker result transfers (#36067)

Yash Singh committed Nov 13, 2026 at 12:25 UTC daed91f26464a280796e38911b7b97455226ef9e
2 files changed +116 -6
packages/shared/src/nodeSqliteClient.test.ts
+66
@@ -174,6 +174,72 @@ const makeTempDatabase = Effect.gen(function* () {
174 return { filename, other };
175 });
176
177 +it.effect.each([false, true])("batches worker results without losing rows (reader=%s)", (reader) =>
178 + Effect.gen(function* () {
179 + const { filename } = yield* makeTempDatabase;
180 + yield* Effect.gen(function* () {
181 + const sql = yield* SqlClient.SqlClient;
182 + const route = <A, E, R>(effect: Effect.Effect<A, E, R>) =>
183 + reader ? effect.pipe(SqliteClient.readOnly) : effect;
184 + yield* route(sql`SELECT 1`);
185 + const worker = startedWorkers
186 + .filter((candidate) => !checkpointWorkers.includes(candidate))
187 + .at(-1)!;
188 + const batches: Array<ReadonlyArray<unknown>> = [];
189 + const record = (reply: { rows?: ReadonlyArray<unknown> }) => {
190 + if (Array.isArray(reply.rows) && reply.rows.length > 0) batches.push(reply.rows);
191 + };
192 + worker.prependListener("message", record);
193 + yield* Effect.gen(function* () {
194 + const text = "x".repeat(16 * 1024);
195 + const query = sql<{ n: number; text: string }>`
196 + WITH RECURSIVE sequence(n) AS (
197 + SELECT 1 UNION ALL SELECT n + 1 FROM sequence WHERE n < 300
198 + ) SELECT n, ${text} AS text FROM sequence
199 + `;
200 + const rows = yield* reader ? route(query.values) : query;
201 + assert.equal(rows.length, 300);
202 + for (const [index, row] of rows.entries()) {
203 + assert.deepEqual(row, reader ? [index + 1, text] : { n: index + 1, text });
204 + }
205 + assert.isAbove(batches.length, 1);
206 + for (const batch of batches) {
207 + assert.isAtMost(batch.length, 16_384);
208 + assert.isAtMost(batch.length * text.length * 2, 8 * 1024 * 1024);
209 + }
210 + batches.length = 0;
211 + const narrow = sql<{ n: number }>`
212 + WITH RECURSIVE sequence(n) AS (
213 + SELECT 1 UNION ALL SELECT n + 1 FROM sequence WHERE n < 10000
214 + ) SELECT n FROM sequence
215 + `;
216 + const narrowRows = yield* reader ? route(narrow.values) : narrow;
217 + assert.equal(narrowRows.length, 10_000);
218 + assert.deepEqual(narrowRows.at(-1), reader ? [10_000] : { n: 10_000 });
219 + assert.equal(batches.length, 1);
220 + }).pipe(Effect.ensuring(Effect.sync(() => worker.off("message", record))));
221 + }).pipe(
222 + Effect.provide(SqliteClient.layer({ filename, writerWorker: true, readerWorker: true })),
223 + );
224 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
225 +);
226 +
227 +it.effect("reuses a worker query after an iterator constraint failure", () =>
228 + Effect.gen(function* () {
229 + const { filename } = yield* makeTempDatabase;
230 + yield* Effect.gen(function* () {
231 + const sql = yield* SqlClient.SqlClient;
232 + yield* sql`CREATE TABLE unique_entries(name TEXT UNIQUE)`;
233 + const insert = (name: string) =>
234 + sql<{ name: string }>`INSERT INTO unique_entries VALUES (${name}) RETURNING name`;
235 + yield* insert("first");
236 + const error = yield* insert("first").pipe(Effect.flip);
237 + assert.equal(error.reason._tag, "UniqueViolation");
238 + assert.deepEqual(yield* insert("second"), [{ name: "second" }]);
239 + }).pipe(Effect.provide(SqliteClient.layer({ filename, writerWorker: true })));
240 + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
241 +);
242 +
243 it.effect("keeps another connection from committing between a transaction's read and write", () =>
244 Effect.gen(function* () {
245 const { filename, other } = yield* makeTempDatabase;
packages/shared/src/nodeSqliteClient.ts
+50 -6
@@ -242,7 +242,7 @@ parentPort.on("message", (request) => {
242 parentPort.close();
243 return;
244 }
245 - const { id, sql, params, cached, values, raw, safeIntegers } = request;
245 + const { id, sql, params, cached, values, raw, safeIntegers, received } = request;
246 let operation = "open";
247 try {
248 if (db === undefined) throw new Error("database connection is unavailable");
@@ -252,12 +252,37 @@ parentPort.on("message", (request) => {
252 statement.setReadBigInts(safeIntegers);
253 if (statement.columns().length > 0) {
254 statement.setReturnArrays(values);
255 - parentPort.postMessage({ id, rows: statement.all(...params) });
255 + let rows = [];
256 + let bytes = 0;
257 + let pendingBatch = false;
258 + for (const row of statement.iterate(...params)) {
259 + let rowBytes = 0;
260 + for (const key in row) {
261 + const value = row[key];
262 + rowBytes += typeof value === "string" ? value.length * 2
263 + : ArrayBuffer.isView(value) ? value.byteLength : 8;
264 + }
265 + if (rows.length > 0 && (rows.length >= 16384 || bytes + rowBytes > 8 * 1024 * 1024)) {
266 + // Build the next batch during receipt, but allow only one unacknowledged message.
267 + if (pendingBatch) Atomics.wait(received, 0, 0);
268 + Atomics.store(received, 0, 0);
269 + parentPort.postMessage({ id, rows, more: true });
270 + rows = [];
271 + bytes = 0;
272 + pendingBatch = true;
273 + }
274 + rows.push(row);
275 + bytes += rowBytes;
276 + }
277 + if (pendingBatch) Atomics.wait(received, 0, 0);
278 + parentPort.postMessage({ id, rows });
279 } else {
280 const result = statement.run(...params);
281 parentPort.postMessage({ id, rows: raw ? result : [] });
282 }
283 } catch (error) {
284 + // A failed iterator can report its previous SQLite error when the statement is reused.
285 + statements.delete(sql);
286 parentPort.postMessage({ id, error: { operation, ...describe(error) } });
287 }
288 });
@@ -283,7 +308,7 @@ type WorkerStartup =
308 | ({ readonly type: "unavailable" } & WorkerFailure);
309
310 type WorkerReply =
286 - | { readonly id: number; readonly rows: ReadonlyArray<any> }
311 + | { readonly id: number; readonly rows: ReadonlyArray<any>; readonly more?: boolean }
312 | {
313 readonly id: number;
314 readonly error: WorkerFailure & { readonly operation: "open" | "prepare" | "execute" };
@@ -353,7 +378,12 @@ const makeWorkerConnection = Effect.fnUntraced(function* (options: {
378 let stoppedMessage: string | undefined;
379 const pending = new Map<
380 number,
356 - { readonly endsTransaction: boolean; readonly resume: (reply: WorkerReply) => void }
381 + {
382 + readonly endsTransaction: boolean;
383 + readonly received: Int32Array;
384 + readonly rows: Array<unknown>;
385 + readonly resume: (reply: WorkerReply) => void;
386 + }
387 >();
388 const stop = (message: string) => {
389 stoppedMessage = message;
@@ -377,10 +407,21 @@ const makeWorkerConnection = Effect.fnUntraced(function* (options: {
407 }
408 const request = pending.get(reply.id);
409 if (request === undefined) return;
410 + if ("rows" in reply && reply.more) {
411 + for (const row of reply.rows) request.rows.push(row);
412 + Atomics.store(request.received, 0, 1);
413 + Atomics.notify(request.received, 0);
414 + return;
415 + }
416 pending.delete(reply.id);
417 if (pending.size === 0) worker.unref();
418 options.onReply?.();
383 - request.resume(reply);
419 + if ("rows" in reply && request.rows.length > 0) {
420 + for (const row of reply.rows) request.rows.push(row);
421 + request.resume({ id: reply.id, rows: request.rows });
422 + } else {
423 + request.resume(reply);
424 + }
425 });
426 worker.on("error", (cause) => stop(`${label} failed: ${cause.message}`));
427 worker.on("exit", (code) => stop(`${label} exited with code ${code}`));
@@ -399,8 +440,11 @@ const makeWorkerConnection = Effect.fnUntraced(function* (options: {
440 const safeIntegers = Boolean(Context.get(fiber.context, Client.SafeIntegers));
441 const reply = Effect.callback<ReadonlyArray<any>, SqlError>((resume) => {
442 const id = nextRequestId++;
443 + const received = new Int32Array(new SharedArrayBuffer(Int32Array.BYTES_PER_ELEMENT));
444 pending.set(id, {
445 endsTransaction,
446 + received,
447 + rows: [],
448 resume: (reply) =>
449 resume(
450 "rows" in reply
@@ -411,7 +455,7 @@ const makeWorkerConnection = Effect.fnUntraced(function* (options: {
455 if (pending.size === 1) worker.ref();
456 const cached = input.cached && !hasLargeBinding(input.params);
457 // oxlint-disable-next-line unicorn/require-post-message-target-origin -- Node workers do not accept a target origin.
414 - worker.postMessage({ id, safeIntegers, ...input, cached });
458 + worker.postMessage({ id, safeIntegers, ...input, cached, received });
459 });
460 // A posted write runs to the end, as it did on the event loop; the caller and its lock
461 // must not move on while the worker is still applying it. Reads stay cancellable.